blob: 22831a4a90994f29d9f1861b35bc334888dbf07e [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 Kirsherb40c82e62015-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 Kirsherb40c82e62015-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 Jaine25d00b82015-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 Nelson13fd9772013-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 Brandeburgb32bfa172016-04-18 11:33:42 -07003009 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003010
Jesse Brandeburgb32bfa172016-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;
3088 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3089 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003090
3091 /* set up individual rings */
3092 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003093 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003094
3095 return err;
3096}
3097
3098/**
3099 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3100 * @vsi: ptr to the VSI
3101 **/
3102static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3103{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003104 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003105 u16 qoffset, qcount;
3106 int i, n;
3107
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003108 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3109 /* Reset the TC information */
3110 for (i = 0; i < vsi->num_queue_pairs; i++) {
3111 rx_ring = vsi->rx_rings[i];
3112 tx_ring = vsi->tx_rings[i];
3113 rx_ring->dcb_tc = 0;
3114 tx_ring->dcb_tc = 0;
3115 }
3116 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003117
3118 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003119 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003120 continue;
3121
3122 qoffset = vsi->tc_config.tc_info[n].qoffset;
3123 qcount = vsi->tc_config.tc_info[n].qcount;
3124 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003125 rx_ring = vsi->rx_rings[i];
3126 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003127 rx_ring->dcb_tc = n;
3128 tx_ring->dcb_tc = n;
3129 }
3130 }
3131}
3132
3133/**
3134 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3135 * @vsi: ptr to the VSI
3136 **/
3137static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3138{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003139 struct i40e_pf *pf = vsi->back;
3140 int err;
3141
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003142 if (vsi->netdev)
3143 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003144
3145 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3146 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3147 if (err) {
3148 dev_warn(&pf->pdev->dev,
3149 "could not set up macaddr; err %d\n", err);
3150 }
3151 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003152}
3153
3154/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003155 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3156 * @vsi: Pointer to the targeted VSI
3157 *
3158 * This function replays the hlist on the hw where all the SB Flow Director
3159 * filters were saved.
3160 **/
3161static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3162{
3163 struct i40e_fdir_filter *filter;
3164 struct i40e_pf *pf = vsi->back;
3165 struct hlist_node *node;
3166
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003167 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3168 return;
3169
Jacob Keller6d069422017-02-06 14:38:44 -08003170 /* Reset FDir counters as we're replaying all existing filters */
Jacob Keller097dbf52017-02-06 14:38:46 -08003171 pf->fd_tcp4_filter_cnt = 0;
3172 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08003173 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08003174 pf->fd_ip4_filter_cnt = 0;
Jacob Keller6d069422017-02-06 14:38:44 -08003175
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003176 hlist_for_each_entry_safe(filter, node,
3177 &pf->fdir_filter_list, fdir_node) {
3178 i40e_add_del_fdir(vsi, filter, true);
3179 }
3180}
3181
3182/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003183 * i40e_vsi_configure - Set up the VSI for action
3184 * @vsi: the VSI being configured
3185 **/
3186static int i40e_vsi_configure(struct i40e_vsi *vsi)
3187{
3188 int err;
3189
3190 i40e_set_vsi_rx_mode(vsi);
3191 i40e_restore_vlan(vsi);
3192 i40e_vsi_config_dcb_rings(vsi);
3193 err = i40e_vsi_configure_tx(vsi);
3194 if (!err)
3195 err = i40e_vsi_configure_rx(vsi);
3196
3197 return err;
3198}
3199
3200/**
3201 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3202 * @vsi: the VSI being configured
3203 **/
3204static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3205{
3206 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003207 struct i40e_hw *hw = &pf->hw;
3208 u16 vector;
3209 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003210 u32 qp;
3211
3212 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3213 * and PFINT_LNKLSTn registers, e.g.:
3214 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3215 */
3216 qp = vsi->base_queue;
3217 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003218 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003219 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3220
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003221 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003222 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003223 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3224 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3225 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003226 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003227 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3228 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3229 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003230 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003231 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003232
3233 /* Linked list for the queuepairs assigned to this vector */
3234 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3235 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003236 u32 val;
3237
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003238 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3239 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3240 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3241 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3242 (I40E_QUEUE_TYPE_TX
3243 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3244
3245 wr32(hw, I40E_QINT_RQCTL(qp), val);
3246
3247 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3248 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3249 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3250 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3251 (I40E_QUEUE_TYPE_RX
3252 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3253
3254 /* Terminate the linked list */
3255 if (q == (q_vector->num_ringpairs - 1))
3256 val |= (I40E_QUEUE_END_OF_LIST
3257 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3258
3259 wr32(hw, I40E_QINT_TQCTL(qp), val);
3260 qp++;
3261 }
3262 }
3263
3264 i40e_flush(hw);
3265}
3266
3267/**
3268 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3269 * @hw: ptr to the hardware info
3270 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003271static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003272{
Jacob Kellerab437b52014-12-14 01:55:08 +00003273 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003274 u32 val;
3275
3276 /* clear things first */
3277 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3278 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3279
3280 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3281 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3282 I40E_PFINT_ICR0_ENA_GRST_MASK |
3283 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3284 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003285 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3286 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3287 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3288
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003289 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3290 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3291
Jacob Kellerab437b52014-12-14 01:55:08 +00003292 if (pf->flags & I40E_FLAG_PTP)
3293 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3294
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003295 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3296
3297 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003298 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3299 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003300
3301 /* OTHER_ITR_IDX = 0 */
3302 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3303}
3304
3305/**
3306 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3307 * @vsi: the VSI being configured
3308 **/
3309static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3310{
Alexander Duyck493fb302013-09-28 07:01:44 +00003311 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003312 struct i40e_pf *pf = vsi->back;
3313 struct i40e_hw *hw = &pf->hw;
3314 u32 val;
3315
3316 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003317 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003318 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003319 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3320 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003321 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003322 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3323 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3324
Jacob Kellerab437b52014-12-14 01:55:08 +00003325 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003326
3327 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3328 wr32(hw, I40E_PFINT_LNKLST0, 0);
3329
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003330 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003331 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3332 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3333 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3334
3335 wr32(hw, I40E_QINT_RQCTL(0), val);
3336
3337 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3338 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3339 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3340
3341 wr32(hw, I40E_QINT_TQCTL(0), val);
3342 i40e_flush(hw);
3343}
3344
3345/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003346 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3347 * @pf: board private structure
3348 **/
3349void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3350{
3351 struct i40e_hw *hw = &pf->hw;
3352
3353 wr32(hw, I40E_PFINT_DYN_CTL0,
3354 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3355 i40e_flush(hw);
3356}
3357
3358/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003359 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3360 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003361 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003362 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003363void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003364{
3365 struct i40e_hw *hw = &pf->hw;
3366 u32 val;
3367
3368 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003369 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003370 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3371
3372 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3373 i40e_flush(hw);
3374}
3375
3376/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003377 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3378 * @irq: interrupt number
3379 * @data: pointer to a q_vector
3380 **/
3381static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3382{
3383 struct i40e_q_vector *q_vector = data;
3384
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003385 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003386 return IRQ_HANDLED;
3387
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003388 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003389
3390 return IRQ_HANDLED;
3391}
3392
3393/**
Alan Brady96db7762016-09-14 16:24:38 -07003394 * i40e_irq_affinity_notify - Callback for affinity changes
3395 * @notify: context as to what irq was changed
3396 * @mask: the new affinity mask
3397 *
3398 * This is a callback function used by the irq_set_affinity_notifier function
3399 * so that we may register to receive changes to the irq affinity masks.
3400 **/
3401static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3402 const cpumask_t *mask)
3403{
3404 struct i40e_q_vector *q_vector =
3405 container_of(notify, struct i40e_q_vector, affinity_notify);
3406
3407 q_vector->affinity_mask = *mask;
3408}
3409
3410/**
3411 * i40e_irq_affinity_release - Callback for affinity notifier release
3412 * @ref: internal core kernel usage
3413 *
3414 * This is a callback function used by the irq_set_affinity_notifier function
3415 * to inform the current notification subscriber that they will no longer
3416 * receive notifications.
3417 **/
3418static void i40e_irq_affinity_release(struct kref *ref) {}
3419
3420/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003421 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3422 * @vsi: the VSI being configured
3423 * @basename: name for the vector
3424 *
3425 * Allocates MSI-X vectors and requests interrupts from the kernel.
3426 **/
3427static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3428{
3429 int q_vectors = vsi->num_q_vectors;
3430 struct i40e_pf *pf = vsi->back;
3431 int base = vsi->base_vector;
3432 int rx_int_idx = 0;
3433 int tx_int_idx = 0;
3434 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003435 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003436
3437 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003438 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003439
Alan Brady96db7762016-09-14 16:24:38 -07003440 irq_num = pf->msix_entries[base + vector].vector;
3441
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003442 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003443 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3444 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3445 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003446 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003447 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3448 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003449 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003450 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3451 "%s-%s-%d", basename, "tx", tx_int_idx++);
3452 } else {
3453 /* skip this unused q_vector */
3454 continue;
3455 }
Alan Brady96db7762016-09-14 16:24:38 -07003456 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003457 vsi->irq_handler,
3458 0,
3459 q_vector->name,
3460 q_vector);
3461 if (err) {
3462 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003463 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003464 goto free_queue_irqs;
3465 }
Alan Brady96db7762016-09-14 16:24:38 -07003466
3467 /* register for affinity change notifications */
3468 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3469 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3470 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003471 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003472 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003473 }
3474
Shannon Nelson63741842014-04-23 04:50:16 +00003475 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003476 return 0;
3477
3478free_queue_irqs:
3479 while (vector) {
3480 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003481 irq_num = pf->msix_entries[base + vector].vector;
3482 irq_set_affinity_notifier(irq_num, NULL);
3483 irq_set_affinity_hint(irq_num, NULL);
3484 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003485 }
3486 return err;
3487}
3488
3489/**
3490 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3491 * @vsi: the VSI being un-configured
3492 **/
3493static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3494{
3495 struct i40e_pf *pf = vsi->back;
3496 struct i40e_hw *hw = &pf->hw;
3497 int base = vsi->base_vector;
3498 int i;
3499
3500 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003501 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3502 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003503 }
3504
3505 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3506 for (i = vsi->base_vector;
3507 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3508 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3509
3510 i40e_flush(hw);
3511 for (i = 0; i < vsi->num_q_vectors; i++)
3512 synchronize_irq(pf->msix_entries[i + base].vector);
3513 } else {
3514 /* Legacy and MSI mode - this stops all interrupt handling */
3515 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3516 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3517 i40e_flush(hw);
3518 synchronize_irq(pf->pdev->irq);
3519 }
3520}
3521
3522/**
3523 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3524 * @vsi: the VSI being configured
3525 **/
3526static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3527{
3528 struct i40e_pf *pf = vsi->back;
3529 int i;
3530
3531 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003532 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003533 i40e_irq_dynamic_enable(vsi, i);
3534 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003535 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003536 }
3537
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003538 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003539 return 0;
3540}
3541
3542/**
3543 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3544 * @pf: board private structure
3545 **/
3546static void i40e_stop_misc_vector(struct i40e_pf *pf)
3547{
3548 /* Disable ICR 0 */
3549 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3550 i40e_flush(&pf->hw);
3551}
3552
3553/**
3554 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3555 * @irq: interrupt number
3556 * @data: pointer to a q_vector
3557 *
3558 * This is the handler used for all MSI/Legacy interrupts, and deals
3559 * with both queue and non-queue interrupts. This is also used in
3560 * MSIX mode to handle the non-queue interrupts.
3561 **/
3562static irqreturn_t i40e_intr(int irq, void *data)
3563{
3564 struct i40e_pf *pf = (struct i40e_pf *)data;
3565 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003566 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003567 u32 icr0, icr0_remaining;
3568 u32 val, ena_mask;
3569
3570 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003571 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003572
Shannon Nelson116a57d2013-09-28 07:13:59 +00003573 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3574 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003575 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003576
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003577 /* if interrupt but no bits showing, must be SWINT */
3578 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3579 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3580 pf->sw_int_count++;
3581
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003582 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3583 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3584 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3585 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003586 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003587 }
3588
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003589 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3590 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003591 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3592 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003593
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003594 /* We do not have a way to disarm Queue causes while leaving
3595 * interrupt enabled for all other causes, ideally
3596 * interrupt should be disabled while we are in NAPI but
3597 * this is not a performance path and napi_schedule()
3598 * can deal with rescheduling.
3599 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003600 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003601 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003602 }
3603
3604 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3605 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3606 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003607 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003608 }
3609
3610 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3611 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3612 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3613 }
3614
3615 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3616 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3617 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3618 }
3619
3620 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3621 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3622 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3623 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3624 val = rd32(hw, I40E_GLGEN_RSTAT);
3625 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3626 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003627 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003628 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003629 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003630 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003631 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003632 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003633 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003634 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003635 }
3636
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003637 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3638 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3639 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003640 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3641 rd32(hw, I40E_PFHMC_ERRORINFO),
3642 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003643 }
3644
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003645 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3646 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3647
3648 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003649 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003650 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003651 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003652 }
3653
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003654 /* If a critical error is pending we have no choice but to reset the
3655 * device.
3656 * Report and mask out any remaining unexpected interrupts.
3657 */
3658 icr0_remaining = icr0 & ena_mask;
3659 if (icr0_remaining) {
3660 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3661 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003662 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003663 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003664 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003665 dev_info(&pf->pdev->dev, "device will be reset\n");
3666 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3667 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003668 }
3669 ena_mask &= ~icr0_remaining;
3670 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003671 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003672
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003673enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003674 /* re-enable interrupt causes */
3675 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003676 if (!test_bit(__I40E_DOWN, &pf->state)) {
3677 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003678 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003679 }
3680
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003681 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003682}
3683
3684/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003685 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3686 * @tx_ring: tx ring to clean
3687 * @budget: how many cleans we're allowed
3688 *
3689 * Returns true if there's any budget left (e.g. the clean is finished)
3690 **/
3691static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3692{
3693 struct i40e_vsi *vsi = tx_ring->vsi;
3694 u16 i = tx_ring->next_to_clean;
3695 struct i40e_tx_buffer *tx_buf;
3696 struct i40e_tx_desc *tx_desc;
3697
3698 tx_buf = &tx_ring->tx_bi[i];
3699 tx_desc = I40E_TX_DESC(tx_ring, i);
3700 i -= tx_ring->count;
3701
3702 do {
3703 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3704
3705 /* if next_to_watch is not set then there is no work pending */
3706 if (!eop_desc)
3707 break;
3708
3709 /* prevent any other reads prior to eop_desc */
3710 read_barrier_depends();
3711
3712 /* if the descriptor isn't done, no work yet to do */
3713 if (!(eop_desc->cmd_type_offset_bsz &
3714 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3715 break;
3716
3717 /* clear next_to_watch to prevent false hangs */
3718 tx_buf->next_to_watch = NULL;
3719
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003720 tx_desc->buffer_addr = 0;
3721 tx_desc->cmd_type_offset_bsz = 0;
3722 /* move past filter desc */
3723 tx_buf++;
3724 tx_desc++;
3725 i++;
3726 if (unlikely(!i)) {
3727 i -= tx_ring->count;
3728 tx_buf = tx_ring->tx_bi;
3729 tx_desc = I40E_TX_DESC(tx_ring, 0);
3730 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003731 /* unmap skb header data */
3732 dma_unmap_single(tx_ring->dev,
3733 dma_unmap_addr(tx_buf, dma),
3734 dma_unmap_len(tx_buf, len),
3735 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003736 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3737 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003738
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003739 tx_buf->raw_buf = NULL;
3740 tx_buf->tx_flags = 0;
3741 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003742 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003743 tx_desc->buffer_addr = 0;
3744 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003745
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003746 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003747 tx_buf++;
3748 tx_desc++;
3749 i++;
3750 if (unlikely(!i)) {
3751 i -= tx_ring->count;
3752 tx_buf = tx_ring->tx_bi;
3753 tx_desc = I40E_TX_DESC(tx_ring, 0);
3754 }
3755
3756 /* update budget accounting */
3757 budget--;
3758 } while (likely(budget));
3759
3760 i += tx_ring->count;
3761 tx_ring->next_to_clean = i;
3762
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003763 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003764 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003765
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003766 return budget > 0;
3767}
3768
3769/**
3770 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3771 * @irq: interrupt number
3772 * @data: pointer to a q_vector
3773 **/
3774static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3775{
3776 struct i40e_q_vector *q_vector = data;
3777 struct i40e_vsi *vsi;
3778
3779 if (!q_vector->tx.ring)
3780 return IRQ_HANDLED;
3781
3782 vsi = q_vector->tx.ring->vsi;
3783 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3784
3785 return IRQ_HANDLED;
3786}
3787
3788/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003789 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003790 * @vsi: the VSI being configured
3791 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003792 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003793 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003794static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003795{
Alexander Duyck493fb302013-09-28 07:01:44 +00003796 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003797 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3798 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003799
3800 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003801 tx_ring->next = q_vector->tx.ring;
3802 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003803 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003804
3805 rx_ring->q_vector = q_vector;
3806 rx_ring->next = q_vector->rx.ring;
3807 q_vector->rx.ring = rx_ring;
3808 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003809}
3810
3811/**
3812 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3813 * @vsi: the VSI being configured
3814 *
3815 * This function maps descriptor rings to the queue-specific vectors
3816 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3817 * one vector per queue pair, but on a constrained vector budget, we
3818 * group the queue pairs as "efficiently" as possible.
3819 **/
3820static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3821{
3822 int qp_remaining = vsi->num_queue_pairs;
3823 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003824 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003825 int v_start = 0;
3826 int qp_idx = 0;
3827
3828 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3829 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003830 * It is also important to go through all the vectors available to be
3831 * sure that if we don't use all the vectors, that the remaining vectors
3832 * are cleared. This is especially important when decreasing the
3833 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003834 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003835 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003836 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3837
3838 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3839
3840 q_vector->num_ringpairs = num_ringpairs;
3841
3842 q_vector->rx.count = 0;
3843 q_vector->tx.count = 0;
3844 q_vector->rx.ring = NULL;
3845 q_vector->tx.ring = NULL;
3846
3847 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003848 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003849 qp_idx++;
3850 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003851 }
3852 }
3853}
3854
3855/**
3856 * i40e_vsi_request_irq - Request IRQ from the OS
3857 * @vsi: the VSI being configured
3858 * @basename: name for the vector
3859 **/
3860static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3861{
3862 struct i40e_pf *pf = vsi->back;
3863 int err;
3864
3865 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3866 err = i40e_vsi_request_irq_msix(vsi, basename);
3867 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3868 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003869 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003870 else
3871 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003872 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003873
3874 if (err)
3875 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3876
3877 return err;
3878}
3879
3880#ifdef CONFIG_NET_POLL_CONTROLLER
3881/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003882 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003883 * @netdev: network interface device structure
3884 *
3885 * This is used by netconsole to send skbs without having to re-enable
3886 * interrupts. It's not called while the normal interrupt routine is executing.
3887 **/
3888static void i40e_netpoll(struct net_device *netdev)
3889{
3890 struct i40e_netdev_priv *np = netdev_priv(netdev);
3891 struct i40e_vsi *vsi = np->vsi;
3892 struct i40e_pf *pf = vsi->back;
3893 int i;
3894
3895 /* if interface is down do nothing */
3896 if (test_bit(__I40E_DOWN, &vsi->state))
3897 return;
3898
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003899 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3900 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003901 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003902 } else {
3903 i40e_intr(pf->pdev->irq, netdev);
3904 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003905}
3906#endif
3907
3908/**
Neerav Parikh23527302014-06-03 23:50:15 +00003909 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3910 * @pf: the PF being configured
3911 * @pf_q: the PF queue
3912 * @enable: enable or disable state of the queue
3913 *
3914 * This routine will wait for the given Tx queue of the PF to reach the
3915 * enabled or disabled state.
3916 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3917 * multiple retries; else will return 0 in case of success.
3918 **/
3919static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3920{
3921 int i;
3922 u32 tx_reg;
3923
3924 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3925 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3926 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3927 break;
3928
Neerav Parikhf98a2002014-09-13 07:40:44 +00003929 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003930 }
3931 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3932 return -ETIMEDOUT;
3933
3934 return 0;
3935}
3936
3937/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003938 * i40e_vsi_control_tx - Start or stop a VSI's rings
3939 * @vsi: the VSI being configured
3940 * @enable: start or stop the rings
3941 **/
3942static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3943{
3944 struct i40e_pf *pf = vsi->back;
3945 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003946 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003947 u32 tx_reg;
3948
3949 pf_q = vsi->base_queue;
3950 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003951
3952 /* warn the TX unit of coming changes */
3953 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3954 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003955 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00003956
Mitch Williams6c5ef622014-02-20 19:29:16 -08003957 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003958 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003959 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3960 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3961 break;
3962 usleep_range(1000, 2000);
3963 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003964 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003965 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003966 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003967
3968 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003969 if (enable) {
3970 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003971 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003972 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003973 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003974 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003975
3976 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003977 /* No waiting for the Tx queue to disable */
3978 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3979 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003980
3981 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003982 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3983 if (ret) {
3984 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003985 "VSI seid %d Tx ring %d %sable timeout\n",
3986 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003987 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003988 }
3989 }
3990
Neerav Parikh23527302014-06-03 23:50:15 +00003991 return ret;
3992}
3993
3994/**
3995 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3996 * @pf: the PF being configured
3997 * @pf_q: the PF queue
3998 * @enable: enable or disable state of the queue
3999 *
4000 * This routine will wait for the given Rx queue of the PF to reach the
4001 * enabled or disabled state.
4002 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4003 * multiple retries; else will return 0 in case of success.
4004 **/
4005static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4006{
4007 int i;
4008 u32 rx_reg;
4009
4010 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4011 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4012 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4013 break;
4014
Neerav Parikhf98a2002014-09-13 07:40:44 +00004015 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004016 }
4017 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4018 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004019
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004020 return 0;
4021}
4022
4023/**
4024 * i40e_vsi_control_rx - Start or stop a VSI's rings
4025 * @vsi: the VSI being configured
4026 * @enable: start or stop the rings
4027 **/
4028static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4029{
4030 struct i40e_pf *pf = vsi->back;
4031 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00004032 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004033 u32 rx_reg;
4034
4035 pf_q = vsi->base_queue;
4036 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08004037 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004038 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004039 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4040 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4041 break;
4042 usleep_range(1000, 2000);
4043 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004044
Catherine Sullivan7c122002014-03-14 07:32:29 +00004045 /* Skip if the queue is already in the requested state */
4046 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4047 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004048
4049 /* turn on/off the queue */
4050 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08004051 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004052 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08004053 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004054 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004055 /* No waiting for the Tx queue to disable */
4056 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4057 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004058
4059 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004060 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4061 if (ret) {
4062 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004063 "VSI seid %d Rx ring %d %sable timeout\n",
4064 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004065 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004066 }
4067 }
4068
Wyborny, Carolynd08a9f62017-03-28 08:00:48 -07004069 /* Due to HW errata, on Rx disable only, the register can indicate done
4070 * before it really is. Needs 50ms to be sure
4071 */
4072 if (!enable)
4073 mdelay(50);
4074
Neerav Parikh23527302014-06-03 23:50:15 +00004075 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004076}
4077
4078/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004079 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004080 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004081 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004082int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004083{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004084 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004085
4086 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004087 ret = i40e_vsi_control_rx(vsi, true);
4088 if (ret)
4089 return ret;
4090 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004091
4092 return ret;
4093}
4094
4095/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004096 * i40e_vsi_stop_rings - Stop a VSI's rings
4097 * @vsi: the VSI being configured
4098 **/
4099void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4100{
4101 /* do rx first for enable and last for disable
4102 * Ignore return value, we need to shutdown whatever we can
4103 */
4104 i40e_vsi_control_tx(vsi, false);
4105 i40e_vsi_control_rx(vsi, false);
4106}
4107
4108/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004109 * i40e_vsi_free_irq - Free the irq association with the OS
4110 * @vsi: the VSI being configured
4111 **/
4112static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4113{
4114 struct i40e_pf *pf = vsi->back;
4115 struct i40e_hw *hw = &pf->hw;
4116 int base = vsi->base_vector;
4117 u32 val, qp;
4118 int i;
4119
4120 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4121 if (!vsi->q_vectors)
4122 return;
4123
Shannon Nelson63741842014-04-23 04:50:16 +00004124 if (!vsi->irqs_ready)
4125 return;
4126
4127 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004128 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004129 int irq_num;
4130 u16 vector;
4131
4132 vector = i + base;
4133 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004134
4135 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004136 if (!vsi->q_vectors[i] ||
4137 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004138 continue;
4139
Alan Brady96db7762016-09-14 16:24:38 -07004140 /* clear the affinity notifier in the IRQ descriptor */
4141 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004142 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004143 irq_set_affinity_hint(irq_num, NULL);
4144 synchronize_irq(irq_num);
4145 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004146
4147 /* Tear down the interrupt queue link list
4148 *
4149 * We know that they come in pairs and always
4150 * the Rx first, then the Tx. To clear the
4151 * link list, stick the EOL value into the
4152 * next_q field of the registers.
4153 */
4154 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4155 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4156 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4157 val |= I40E_QUEUE_END_OF_LIST
4158 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4159 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4160
4161 while (qp != I40E_QUEUE_END_OF_LIST) {
4162 u32 next;
4163
4164 val = rd32(hw, I40E_QINT_RQCTL(qp));
4165
4166 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4167 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4168 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4169 I40E_QINT_RQCTL_INTEVENT_MASK);
4170
4171 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4172 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4173
4174 wr32(hw, I40E_QINT_RQCTL(qp), val);
4175
4176 val = rd32(hw, I40E_QINT_TQCTL(qp));
4177
4178 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4179 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4180
4181 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4182 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4183 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4184 I40E_QINT_TQCTL_INTEVENT_MASK);
4185
4186 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4187 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4188
4189 wr32(hw, I40E_QINT_TQCTL(qp), val);
4190 qp = next;
4191 }
4192 }
4193 } else {
4194 free_irq(pf->pdev->irq, pf);
4195
4196 val = rd32(hw, I40E_PFINT_LNKLST0);
4197 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4198 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4199 val |= I40E_QUEUE_END_OF_LIST
4200 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4201 wr32(hw, I40E_PFINT_LNKLST0, val);
4202
4203 val = rd32(hw, I40E_QINT_RQCTL(qp));
4204 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4205 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4206 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4207 I40E_QINT_RQCTL_INTEVENT_MASK);
4208
4209 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4210 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4211
4212 wr32(hw, I40E_QINT_RQCTL(qp), val);
4213
4214 val = rd32(hw, I40E_QINT_TQCTL(qp));
4215
4216 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4217 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4218 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4219 I40E_QINT_TQCTL_INTEVENT_MASK);
4220
4221 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4222 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4223
4224 wr32(hw, I40E_QINT_TQCTL(qp), val);
4225 }
4226}
4227
4228/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004229 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4230 * @vsi: the VSI being configured
4231 * @v_idx: Index of vector to be freed
4232 *
4233 * This function frees the memory allocated to the q_vector. In addition if
4234 * NAPI is enabled it will delete any references to the NAPI struct prior
4235 * to freeing the q_vector.
4236 **/
4237static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4238{
4239 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004240 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004241
4242 if (!q_vector)
4243 return;
4244
4245 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004246 i40e_for_each_ring(ring, q_vector->tx)
4247 ring->q_vector = NULL;
4248
4249 i40e_for_each_ring(ring, q_vector->rx)
4250 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004251
4252 /* only VSI w/ an associated netdev is set up w/ NAPI */
4253 if (vsi->netdev)
4254 netif_napi_del(&q_vector->napi);
4255
4256 vsi->q_vectors[v_idx] = NULL;
4257
4258 kfree_rcu(q_vector, rcu);
4259}
4260
4261/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004262 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4263 * @vsi: the VSI being un-configured
4264 *
4265 * This frees the memory allocated to the q_vectors and
4266 * deletes references to the NAPI struct.
4267 **/
4268static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4269{
4270 int v_idx;
4271
Alexander Duyck493fb302013-09-28 07:01:44 +00004272 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4273 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004274}
4275
4276/**
4277 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4278 * @pf: board private structure
4279 **/
4280static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4281{
4282 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4283 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4284 pci_disable_msix(pf->pdev);
4285 kfree(pf->msix_entries);
4286 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004287 kfree(pf->irq_pile);
4288 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004289 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4290 pci_disable_msi(pf->pdev);
4291 }
4292 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4293}
4294
4295/**
4296 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4297 * @pf: board private structure
4298 *
4299 * We go through and clear interrupt specific resources and reset the structure
4300 * to pre-load conditions
4301 **/
4302static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4303{
4304 int i;
4305
Shannon Nelsone1477582015-02-21 06:44:33 +00004306 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004307 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004308 synchronize_irq(pf->msix_entries[0].vector);
4309 free_irq(pf->msix_entries[0].vector, pf);
4310 }
4311
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004312 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4313 I40E_IWARP_IRQ_PILE_ID);
4314
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004315 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004316 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004317 if (pf->vsi[i])
4318 i40e_vsi_free_q_vectors(pf->vsi[i]);
4319 i40e_reset_interrupt_capability(pf);
4320}
4321
4322/**
4323 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4324 * @vsi: the VSI being configured
4325 **/
4326static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4327{
4328 int q_idx;
4329
4330 if (!vsi->netdev)
4331 return;
4332
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004333 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4334 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4335
4336 if (q_vector->rx.ring || q_vector->tx.ring)
4337 napi_enable(&q_vector->napi);
4338 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004339}
4340
4341/**
4342 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4343 * @vsi: the VSI being configured
4344 **/
4345static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4346{
4347 int q_idx;
4348
4349 if (!vsi->netdev)
4350 return;
4351
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004352 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4353 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4354
4355 if (q_vector->rx.ring || q_vector->tx.ring)
4356 napi_disable(&q_vector->napi);
4357 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004358}
4359
4360/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004361 * i40e_vsi_close - Shut down a VSI
4362 * @vsi: the vsi to be quelled
4363 **/
4364static void i40e_vsi_close(struct i40e_vsi *vsi)
4365{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004366 struct i40e_pf *pf = vsi->back;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004367 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4368 i40e_down(vsi);
4369 i40e_vsi_free_irq(vsi);
4370 i40e_vsi_free_tx_resources(vsi);
4371 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004372 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004373 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
4374 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
4375 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004376}
4377
4378/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004379 * i40e_quiesce_vsi - Pause a given VSI
4380 * @vsi: the VSI being paused
4381 **/
4382static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4383{
4384 if (test_bit(__I40E_DOWN, &vsi->state))
4385 return;
4386
4387 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004388 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004389 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004390 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004391 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004392}
4393
4394/**
4395 * i40e_unquiesce_vsi - Resume a given VSI
4396 * @vsi: the VSI being resumed
4397 **/
4398static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4399{
4400 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4401 return;
4402
4403 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4404 if (vsi->netdev && netif_running(vsi->netdev))
4405 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4406 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004407 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004408}
4409
4410/**
4411 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4412 * @pf: the PF
4413 **/
4414static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4415{
4416 int v;
4417
Mitch Williams505682c2014-05-20 08:01:37 +00004418 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004419 if (pf->vsi[v])
4420 i40e_quiesce_vsi(pf->vsi[v]);
4421 }
4422}
4423
4424/**
4425 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4426 * @pf: the PF
4427 **/
4428static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4429{
4430 int v;
4431
Mitch Williams505682c2014-05-20 08:01:37 +00004432 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004433 if (pf->vsi[v])
4434 i40e_unquiesce_vsi(pf->vsi[v]);
4435 }
4436}
4437
Neerav Parikh69129dc2014-11-12 00:18:46 +00004438#ifdef CONFIG_I40E_DCB
4439/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004440 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004441 * @vsi: the VSI being configured
4442 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004443 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004444 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004445static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004446{
4447 struct i40e_pf *pf = vsi->back;
4448 int i, pf_q, ret;
4449
4450 pf_q = vsi->base_queue;
4451 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4452 /* Check and wait for the disable status of the queue */
4453 ret = i40e_pf_txq_wait(pf, pf_q, false);
4454 if (ret) {
4455 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004456 "VSI seid %d Tx ring %d disable timeout\n",
4457 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004458 return ret;
4459 }
4460 }
4461
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004462 pf_q = vsi->base_queue;
4463 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4464 /* Check and wait for the disable status of the queue */
4465 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4466 if (ret) {
4467 dev_info(&pf->pdev->dev,
4468 "VSI seid %d Rx ring %d disable timeout\n",
4469 vsi->seid, pf_q);
4470 return ret;
4471 }
4472 }
4473
Neerav Parikh69129dc2014-11-12 00:18:46 +00004474 return 0;
4475}
4476
4477/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004478 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004479 * @pf: the PF
4480 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004481 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004482 * VSIs that are managed by this PF.
4483 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004484static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004485{
4486 int v, ret = 0;
4487
4488 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08004489 if (pf->vsi[v]) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004490 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004491 if (ret)
4492 break;
4493 }
4494 }
4495
4496 return ret;
4497}
4498
4499#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004500
4501/**
4502 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4503 * @q_idx: TX queue number
4504 * @vsi: Pointer to VSI struct
4505 *
4506 * This function checks specified queue for given VSI. Detects hung condition.
Alan Brady17daabb2017-04-05 07:50:56 -04004507 * We proactively detect hung TX queues by checking if interrupts are disabled
4508 * but there are pending descriptors. If it appears hung, attempt to recover
4509 * by triggering a SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004510 **/
4511static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4512{
4513 struct i40e_ring *tx_ring = NULL;
4514 struct i40e_pf *pf;
Alan Brady17daabb2017-04-05 07:50:56 -04004515 u32 val, tx_pending;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004516 int i;
4517
4518 pf = vsi->back;
4519
4520 /* now that we have an index, find the tx_ring struct */
4521 for (i = 0; i < vsi->num_queue_pairs; i++) {
4522 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4523 if (q_idx == vsi->tx_rings[i]->queue_index) {
4524 tx_ring = vsi->tx_rings[i];
4525 break;
4526 }
4527 }
4528 }
4529
4530 if (!tx_ring)
4531 return;
4532
4533 /* Read interrupt register */
4534 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4535 val = rd32(&pf->hw,
4536 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4537 tx_ring->vsi->base_vector - 1));
4538 else
4539 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4540
Alan Brady17daabb2017-04-05 07:50:56 -04004541 tx_pending = i40e_get_tx_pending(tx_ring);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004542
Alan Brady17daabb2017-04-05 07:50:56 -04004543 /* Interrupts are disabled and TX pending is non-zero,
4544 * trigger the SW interrupt (don't wait). Worst case
4545 * there will be one extra interrupt which may result
4546 * into not cleaning any queues because queues are cleaned.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004547 */
Alan Brady17daabb2017-04-05 07:50:56 -04004548 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4549 i40e_force_wb(vsi, tx_ring->q_vector);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004550}
4551
4552/**
4553 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4554 * @pf: pointer to PF struct
4555 *
4556 * LAN VSI has netdev and netdev has TX queues. This function is to check
4557 * each of those TX queues if they are hung, trigger recovery by issuing
4558 * SW interrupt.
4559 **/
4560static void i40e_detect_recover_hung(struct i40e_pf *pf)
4561{
4562 struct net_device *netdev;
4563 struct i40e_vsi *vsi;
4564 int i;
4565
4566 /* Only for LAN VSI */
4567 vsi = pf->vsi[pf->lan_vsi];
4568
4569 if (!vsi)
4570 return;
4571
4572 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4573 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4574 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4575 return;
4576
4577 /* Make sure type is MAIN VSI */
4578 if (vsi->type != I40E_VSI_MAIN)
4579 return;
4580
4581 netdev = vsi->netdev;
4582 if (!netdev)
4583 return;
4584
4585 /* Bail out if netif_carrier is not OK */
4586 if (!netif_carrier_ok(netdev))
4587 return;
4588
4589 /* Go thru' TX queues for netdev */
4590 for (i = 0; i < netdev->num_tx_queues; i++) {
4591 struct netdev_queue *q;
4592
4593 q = netdev_get_tx_queue(netdev, i);
4594 if (q)
4595 i40e_detect_recover_hung_queue(i, vsi);
4596 }
4597}
4598
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004599/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004600 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004601 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004602 *
4603 * Get TC map for ISCSI PF type that will include iSCSI TC
4604 * and LAN TC.
4605 **/
4606static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4607{
4608 struct i40e_dcb_app_priority_table app;
4609 struct i40e_hw *hw = &pf->hw;
4610 u8 enabled_tc = 1; /* TC0 is always enabled */
4611 u8 tc, i;
4612 /* Get the iSCSI APP TLV */
4613 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4614
4615 for (i = 0; i < dcbcfg->numapps; i++) {
4616 app = dcbcfg->app[i];
4617 if (app.selector == I40E_APP_SEL_TCPIP &&
4618 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4619 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004620 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004621 break;
4622 }
4623 }
4624
4625 return enabled_tc;
4626}
4627
4628/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004629 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4630 * @dcbcfg: the corresponding DCBx configuration structure
4631 *
4632 * Return the number of TCs from given DCBx configuration
4633 **/
4634static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4635{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004636 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004637 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004638 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004639
4640 /* Scan the ETS Config Priority Table to find
4641 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004642 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004643 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004644 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4645 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4646
4647 /* Now scan the bitmask to check for
4648 * contiguous TCs starting with TC0
4649 */
4650 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4651 if (num_tc & BIT(i)) {
4652 if (!tc_unused) {
4653 ret++;
4654 } else {
4655 pr_err("Non-contiguous TC - Disabling DCB\n");
4656 return 1;
4657 }
4658 } else {
4659 tc_unused = 1;
4660 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004661 }
4662
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004663 /* There is always at least TC0 */
4664 if (!ret)
4665 ret = 1;
4666
4667 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004668}
4669
4670/**
4671 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4672 * @dcbcfg: the corresponding DCBx configuration structure
4673 *
4674 * Query the current DCB configuration and return the number of
4675 * traffic classes enabled from the given DCBX config
4676 **/
4677static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4678{
4679 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4680 u8 enabled_tc = 1;
4681 u8 i;
4682
4683 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004684 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004685
4686 return enabled_tc;
4687}
4688
4689/**
4690 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4691 * @pf: PF being queried
4692 *
4693 * Return number of traffic classes enabled for the given PF
4694 **/
4695static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4696{
4697 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004698 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004699 u8 num_tc = 0;
4700 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4701
4702 /* If DCB is not enabled then always in single TC */
4703 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4704 return 1;
4705
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004706 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004707 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4708 return i40e_dcb_get_num_tc(dcbcfg);
4709
4710 /* MFP mode return count of enabled TCs for this PF */
4711 if (pf->hw.func_caps.iscsi)
4712 enabled_tc = i40e_get_iscsi_tc_map(pf);
4713 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004714 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004715
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004716 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004717 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004718 num_tc++;
4719 }
4720 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004721}
4722
4723/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004724 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4725 * @pf: PF being queried
4726 *
4727 * Return a bitmap for enabled traffic classes for this PF.
4728 **/
4729static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4730{
4731 /* If DCB is not enabled for this PF then just return default TC */
4732 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004733 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004734
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004735 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004736 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4737 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4738
Neerav Parikhfc51de92015-02-24 06:58:53 +00004739 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004740 if (pf->hw.func_caps.iscsi)
4741 return i40e_get_iscsi_tc_map(pf);
4742 else
David Ertmanea6acb72016-09-20 07:10:50 -07004743 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004744}
4745
4746/**
4747 * i40e_vsi_get_bw_info - Query VSI BW Information
4748 * @vsi: the VSI being queried
4749 *
4750 * Returns 0 on success, negative value on failure
4751 **/
4752static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4753{
4754 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4755 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4756 struct i40e_pf *pf = vsi->back;
4757 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004758 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004759 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004760 int i;
4761
4762 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004763 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4764 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004765 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004766 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4767 i40e_stat_str(&pf->hw, ret),
4768 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004769 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004770 }
4771
4772 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004773 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4774 NULL);
4775 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004776 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004777 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4778 i40e_stat_str(&pf->hw, ret),
4779 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004780 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004781 }
4782
4783 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4784 dev_info(&pf->pdev->dev,
4785 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4786 bw_config.tc_valid_bits,
4787 bw_ets_config.tc_valid_bits);
4788 /* Still continuing */
4789 }
4790
4791 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4792 vsi->bw_max_quanta = bw_config.max_bw;
4793 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4794 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4795 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4796 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4797 vsi->bw_ets_limit_credits[i] =
4798 le16_to_cpu(bw_ets_config.credits[i]);
4799 /* 3 bits out of 4 for each TC */
4800 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4801 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004802
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004803 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004804}
4805
4806/**
4807 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4808 * @vsi: the VSI being configured
4809 * @enabled_tc: TC bitmap
4810 * @bw_credits: BW shared credits per TC
4811 *
4812 * Returns 0 on success, negative value on failure
4813 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004814static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004815 u8 *bw_share)
4816{
4817 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004818 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004819 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004820
4821 bw_data.tc_valid_bits = enabled_tc;
4822 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4823 bw_data.tc_bw_credits[i] = bw_share[i];
4824
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004825 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4826 NULL);
4827 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004828 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004829 "AQ command Config VSI BW allocation per TC failed = %d\n",
4830 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004831 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004832 }
4833
4834 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4835 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4836
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004837 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004838}
4839
4840/**
4841 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4842 * @vsi: the VSI being configured
4843 * @enabled_tc: TC map to be enabled
4844 *
4845 **/
4846static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4847{
4848 struct net_device *netdev = vsi->netdev;
4849 struct i40e_pf *pf = vsi->back;
4850 struct i40e_hw *hw = &pf->hw;
4851 u8 netdev_tc = 0;
4852 int i;
4853 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4854
4855 if (!netdev)
4856 return;
4857
4858 if (!enabled_tc) {
4859 netdev_reset_tc(netdev);
4860 return;
4861 }
4862
4863 /* Set up actual enabled TCs on the VSI */
4864 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4865 return;
4866
4867 /* set per TC queues for the VSI */
4868 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4869 /* Only set TC queues for enabled tcs
4870 *
4871 * e.g. For a VSI that has TC0 and TC3 enabled the
4872 * enabled_tc bitmap would be 0x00001001; the driver
4873 * will set the numtc for netdev as 2 that will be
4874 * referenced by the netdev layer as TC 0 and 1.
4875 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004876 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004877 netdev_set_tc_queue(netdev,
4878 vsi->tc_config.tc_info[i].netdev_tc,
4879 vsi->tc_config.tc_info[i].qcount,
4880 vsi->tc_config.tc_info[i].qoffset);
4881 }
4882
4883 /* Assign UP2TC map for the VSI */
4884 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4885 /* Get the actual TC# for the UP */
4886 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4887 /* Get the mapped netdev TC# for the UP */
4888 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4889 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4890 }
4891}
4892
4893/**
4894 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4895 * @vsi: the VSI being configured
4896 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4897 **/
4898static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4899 struct i40e_vsi_context *ctxt)
4900{
4901 /* copy just the sections touched not the entire info
4902 * since not all sections are valid as returned by
4903 * update vsi params
4904 */
4905 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4906 memcpy(&vsi->info.queue_mapping,
4907 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4908 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4909 sizeof(vsi->info.tc_mapping));
4910}
4911
4912/**
4913 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4914 * @vsi: VSI to be configured
4915 * @enabled_tc: TC bitmap
4916 *
4917 * This configures a particular VSI for TCs that are mapped to the
4918 * given TC bitmap. It uses default bandwidth share for TCs across
4919 * VSIs to configure TC for a particular VSI.
4920 *
4921 * NOTE:
4922 * It is expected that the VSI queues have been quisced before calling
4923 * this function.
4924 **/
4925static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4926{
4927 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4928 struct i40e_vsi_context ctxt;
4929 int ret = 0;
4930 int i;
4931
4932 /* Check if enabled_tc is same as existing or new TCs */
4933 if (vsi->tc_config.enabled_tc == enabled_tc)
4934 return ret;
4935
4936 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4937 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004938 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004939 bw_share[i] = 1;
4940 }
4941
4942 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4943 if (ret) {
4944 dev_info(&vsi->back->pdev->dev,
4945 "Failed configuring TC map %d for VSI %d\n",
4946 enabled_tc, vsi->seid);
4947 goto out;
4948 }
4949
4950 /* Update Queue Pairs Mapping for currently enabled UPs */
4951 ctxt.seid = vsi->seid;
4952 ctxt.pf_num = vsi->back->hw.pf_id;
4953 ctxt.vf_num = 0;
4954 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004955 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004956 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4957
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004958 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4959 ctxt.info.valid_sections |=
4960 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4961 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4962 }
4963
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004964 /* Update the VSI after updating the VSI queue-mapping information */
4965 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4966 if (ret) {
4967 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004968 "Update vsi tc config failed, err %s aq_err %s\n",
4969 i40e_stat_str(&vsi->back->hw, ret),
4970 i40e_aq_str(&vsi->back->hw,
4971 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004972 goto out;
4973 }
4974 /* update the local VSI info with updated queue map */
4975 i40e_vsi_update_queue_map(vsi, &ctxt);
4976 vsi->info.valid_sections = 0;
4977
4978 /* Update current VSI BW information */
4979 ret = i40e_vsi_get_bw_info(vsi);
4980 if (ret) {
4981 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004982 "Failed updating vsi bw info, err %s aq_err %s\n",
4983 i40e_stat_str(&vsi->back->hw, ret),
4984 i40e_aq_str(&vsi->back->hw,
4985 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004986 goto out;
4987 }
4988
4989 /* Update the netdev TC setup */
4990 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4991out:
4992 return ret;
4993}
4994
4995/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004996 * i40e_veb_config_tc - Configure TCs for given VEB
4997 * @veb: given VEB
4998 * @enabled_tc: TC bitmap
4999 *
5000 * Configures given TC bitmap for VEB (switching) element
5001 **/
5002int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5003{
5004 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5005 struct i40e_pf *pf = veb->pf;
5006 int ret = 0;
5007 int i;
5008
5009 /* No TCs or already enabled TCs just return */
5010 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5011 return ret;
5012
5013 bw_data.tc_valid_bits = enabled_tc;
5014 /* bw_data.absolute_credits is not set (relative) */
5015
5016 /* Enable ETS TCs with equal BW Share for now */
5017 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005018 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005019 bw_data.tc_bw_share_credits[i] = 1;
5020 }
5021
5022 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5023 &bw_data, NULL);
5024 if (ret) {
5025 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005026 "VEB bw config failed, err %s aq_err %s\n",
5027 i40e_stat_str(&pf->hw, ret),
5028 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005029 goto out;
5030 }
5031
5032 /* Update the BW information */
5033 ret = i40e_veb_get_bw_info(veb);
5034 if (ret) {
5035 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005036 "Failed getting veb bw config, err %s aq_err %s\n",
5037 i40e_stat_str(&pf->hw, ret),
5038 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005039 }
5040
5041out:
5042 return ret;
5043}
5044
5045#ifdef CONFIG_I40E_DCB
5046/**
5047 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5048 * @pf: PF struct
5049 *
5050 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5051 * the caller would've quiesce all the VSIs before calling
5052 * this function
5053 **/
5054static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5055{
5056 u8 tc_map = 0;
5057 int ret;
5058 u8 v;
5059
5060 /* Enable the TCs available on PF to all VEBs */
5061 tc_map = i40e_pf_get_tc_map(pf);
5062 for (v = 0; v < I40E_MAX_VEB; v++) {
5063 if (!pf->veb[v])
5064 continue;
5065 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5066 if (ret) {
5067 dev_info(&pf->pdev->dev,
5068 "Failed configuring TC for VEB seid=%d\n",
5069 pf->veb[v]->seid);
5070 /* Will try to configure as many components */
5071 }
5072 }
5073
5074 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005075 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005076 if (!pf->vsi[v])
5077 continue;
5078
5079 /* - Enable all TCs for the LAN VSI
5080 * - For all others keep them at TC0 for now
5081 */
5082 if (v == pf->lan_vsi)
5083 tc_map = i40e_pf_get_tc_map(pf);
5084 else
David Ertmanea6acb72016-09-20 07:10:50 -07005085 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005086
5087 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5088 if (ret) {
5089 dev_info(&pf->pdev->dev,
5090 "Failed configuring TC for VSI seid=%d\n",
5091 pf->vsi[v]->seid);
5092 /* Will try to configure as many components */
5093 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005094 /* Re-configure VSI vectors based on updated TC map */
5095 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005096 if (pf->vsi[v]->netdev)
5097 i40e_dcbnl_set_all(pf->vsi[v]);
5098 }
5099 }
5100}
5101
5102/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005103 * i40e_resume_port_tx - Resume port Tx
5104 * @pf: PF struct
5105 *
5106 * Resume a port's Tx and issue a PF reset in case of failure to
5107 * resume.
5108 **/
5109static int i40e_resume_port_tx(struct i40e_pf *pf)
5110{
5111 struct i40e_hw *hw = &pf->hw;
5112 int ret;
5113
5114 ret = i40e_aq_resume_port_tx(hw, NULL);
5115 if (ret) {
5116 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005117 "Resume Port Tx failed, err %s aq_err %s\n",
5118 i40e_stat_str(&pf->hw, ret),
5119 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005120 /* Schedule PF reset to recover */
5121 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5122 i40e_service_event_schedule(pf);
5123 }
5124
5125 return ret;
5126}
5127
5128/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005129 * i40e_init_pf_dcb - Initialize DCB configuration
5130 * @pf: PF being configured
5131 *
5132 * Query the current DCB configuration and cache it
5133 * in the hardware structure
5134 **/
5135static int i40e_init_pf_dcb(struct i40e_pf *pf)
5136{
5137 struct i40e_hw *hw = &pf->hw;
5138 int err = 0;
5139
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005140 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005141 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005142 goto out;
5143
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005144 /* Get the initial DCB configuration */
5145 err = i40e_init_dcb(hw);
5146 if (!err) {
5147 /* Device/Function is not DCBX capable */
5148 if ((!hw->func_caps.dcb) ||
5149 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5150 dev_info(&pf->pdev->dev,
5151 "DCBX offload is not supported or is disabled for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005152 } else {
5153 /* When status is not DISABLED then DCBX in FW */
5154 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5155 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005156
5157 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005158 /* Enable DCB tagging only when more than one TC
5159 * or explicitly disable if only one TC
5160 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005161 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5162 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005163 else
5164 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005165 dev_dbg(&pf->pdev->dev,
5166 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005167 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005168 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005169 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005170 "Query for DCB configuration failed, err %s aq_err %s\n",
5171 i40e_stat_str(&pf->hw, err),
5172 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005173 }
5174
5175out:
5176 return err;
5177}
5178#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005179#define SPEED_SIZE 14
5180#define FC_SIZE 8
5181/**
5182 * i40e_print_link_message - print link up or down
5183 * @vsi: the VSI for which link needs a message
5184 */
Matt Jaredc156f852015-08-27 11:42:39 -04005185void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005186{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005187 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005188 char *speed = "Unknown";
5189 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005190 char *fec = "";
5191 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005192
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005193 new_speed = vsi->back->hw.phy.link_info.link_speed;
5194
5195 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005196 return;
5197 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005198 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005199 if (!isup) {
5200 netdev_info(vsi->netdev, "NIC Link is Down\n");
5201 return;
5202 }
5203
Greg Rose148c2d82014-12-11 07:06:27 +00005204 /* Warn user if link speed on NPAR enabled partition is not at
5205 * least 10GB
5206 */
5207 if (vsi->back->hw.func_caps.npar_enable &&
5208 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5209 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5210 netdev_warn(vsi->netdev,
5211 "The partition detected link speed that is less than 10Gbps\n");
5212
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005213 switch (vsi->back->hw.phy.link_info.link_speed) {
5214 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005215 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005216 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005217 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005218 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005219 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005220 case I40E_LINK_SPEED_25GB:
5221 speed = "25 G";
5222 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005223 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005224 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005225 break;
5226 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005227 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005228 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005229 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005230 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005231 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005232 default:
5233 break;
5234 }
5235
5236 switch (vsi->back->hw.fc.current_mode) {
5237 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005238 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005239 break;
5240 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005241 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005242 break;
5243 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005244 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005245 break;
5246 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005247 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005248 break;
5249 }
5250
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005251 if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
5252 fec = ", FEC: None";
5253 an = ", Autoneg: False";
5254
5255 if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
5256 an = ", Autoneg: True";
5257
5258 if (vsi->back->hw.phy.link_info.fec_info &
5259 I40E_AQ_CONFIG_FEC_KR_ENA)
5260 fec = ", FEC: CL74 FC-FEC/BASE-R";
5261 else if (vsi->back->hw.phy.link_info.fec_info &
5262 I40E_AQ_CONFIG_FEC_RS_ENA)
5263 fec = ", FEC: CL108 RS-FEC";
5264 }
5265
5266 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
5267 speed, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005268}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005269
5270/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005271 * i40e_up_complete - Finish the last steps of bringing up a connection
5272 * @vsi: the VSI being configured
5273 **/
5274static int i40e_up_complete(struct i40e_vsi *vsi)
5275{
5276 struct i40e_pf *pf = vsi->back;
5277 int err;
5278
5279 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5280 i40e_vsi_configure_msix(vsi);
5281 else
5282 i40e_configure_msi_and_legacy(vsi);
5283
5284 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005285 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005286 if (err)
5287 return err;
5288
5289 clear_bit(__I40E_DOWN, &vsi->state);
5290 i40e_napi_enable_all(vsi);
5291 i40e_vsi_enable_irq(vsi);
5292
5293 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5294 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005295 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005296 netif_tx_start_all_queues(vsi->netdev);
5297 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005298 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005299 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005300 /* need to check for qualified module here*/
5301 if ((pf->hw.phy.link_info.link_info &
5302 I40E_AQ_MEDIA_AVAILABLE) &&
5303 (!(pf->hw.phy.link_info.an_info &
5304 I40E_AQ_QUALIFIED_MODULE)))
5305 netdev_err(vsi->netdev,
5306 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005307 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005308
5309 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005310 if (vsi->type == I40E_VSI_FDIR) {
5311 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08005312 pf->fd_add_err = 0;
5313 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005314 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005315 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005316
5317 /* On the next run of the service_task, notify any clients of the new
5318 * opened netdev
5319 */
5320 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005321 i40e_service_event_schedule(pf);
5322
5323 return 0;
5324}
5325
5326/**
5327 * i40e_vsi_reinit_locked - Reset the VSI
5328 * @vsi: the VSI being configured
5329 *
5330 * Rebuild the ring structs after some configuration
5331 * has changed, e.g. MTU size.
5332 **/
5333static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5334{
5335 struct i40e_pf *pf = vsi->back;
5336
5337 WARN_ON(in_interrupt());
5338 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5339 usleep_range(1000, 2000);
5340 i40e_down(vsi);
5341
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005342 i40e_up(vsi);
5343 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5344}
5345
5346/**
5347 * i40e_up - Bring the connection back up after being down
5348 * @vsi: the VSI being configured
5349 **/
5350int i40e_up(struct i40e_vsi *vsi)
5351{
5352 int err;
5353
5354 err = i40e_vsi_configure(vsi);
5355 if (!err)
5356 err = i40e_up_complete(vsi);
5357
5358 return err;
5359}
5360
5361/**
5362 * i40e_down - Shutdown the connection processing
5363 * @vsi: the VSI being stopped
5364 **/
5365void i40e_down(struct i40e_vsi *vsi)
5366{
5367 int i;
5368
5369 /* It is assumed that the caller of this function
5370 * sets the vsi->state __I40E_DOWN bit.
5371 */
5372 if (vsi->netdev) {
5373 netif_carrier_off(vsi->netdev);
5374 netif_tx_disable(vsi->netdev);
5375 }
5376 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005377 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005378 i40e_napi_disable_all(vsi);
5379
5380 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005381 i40e_clean_tx_ring(vsi->tx_rings[i]);
5382 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005383 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005384
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005385}
5386
5387/**
5388 * i40e_setup_tc - configure multiple traffic classes
5389 * @netdev: net device to configure
5390 * @tc: number of traffic classes to enable
5391 **/
5392static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5393{
5394 struct i40e_netdev_priv *np = netdev_priv(netdev);
5395 struct i40e_vsi *vsi = np->vsi;
5396 struct i40e_pf *pf = vsi->back;
5397 u8 enabled_tc = 0;
5398 int ret = -EINVAL;
5399 int i;
5400
5401 /* Check if DCB enabled to continue */
5402 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5403 netdev_info(netdev, "DCB is not enabled for adapter\n");
5404 goto exit;
5405 }
5406
5407 /* Check if MFP enabled */
5408 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5409 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5410 goto exit;
5411 }
5412
5413 /* Check whether tc count is within enabled limit */
5414 if (tc > i40e_pf_get_num_tc(pf)) {
5415 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5416 goto exit;
5417 }
5418
5419 /* Generate TC map for number of tc requested */
5420 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005421 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005422
5423 /* Requesting same TC configuration as already enabled */
5424 if (enabled_tc == vsi->tc_config.enabled_tc)
5425 return 0;
5426
5427 /* Quiesce VSI queues */
5428 i40e_quiesce_vsi(vsi);
5429
5430 /* Configure VSI for enabled TCs */
5431 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5432 if (ret) {
5433 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5434 vsi->seid);
5435 goto exit;
5436 }
5437
5438 /* Unquiesce VSI */
5439 i40e_unquiesce_vsi(vsi);
5440
5441exit:
5442 return ret;
5443}
5444
John Fastabend16e5cc62016-02-16 21:16:43 -08005445static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5446 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005447{
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005448 if (tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005449 return -EINVAL;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005450
5451 tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
5452
5453 return i40e_setup_tc(netdev, tc->mqprio->num_tc);
John Fastabende4c67342016-02-16 21:16:15 -08005454}
5455
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005456/**
5457 * i40e_open - Called when a network interface is made active
5458 * @netdev: network interface device structure
5459 *
5460 * The open entry point is called when a network interface is made
5461 * active by the system (IFF_UP). At this point all resources needed
5462 * for transmit and receive operations are allocated, the interrupt
5463 * handler is registered with the OS, the netdev watchdog subtask is
5464 * enabled, and the stack is notified that the interface is ready.
5465 *
5466 * Returns 0 on success, negative value on failure
5467 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005468int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005469{
5470 struct i40e_netdev_priv *np = netdev_priv(netdev);
5471 struct i40e_vsi *vsi = np->vsi;
5472 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005473 int err;
5474
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005475 /* disallow open during test or if eeprom is broken */
5476 if (test_bit(__I40E_TESTING, &pf->state) ||
5477 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005478 return -EBUSY;
5479
5480 netif_carrier_off(netdev);
5481
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005482 err = i40e_vsi_open(vsi);
5483 if (err)
5484 return err;
5485
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005486 /* configure global TSO hardware offload settings */
5487 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5488 TCP_FLAG_FIN) >> 16);
5489 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5490 TCP_FLAG_FIN |
5491 TCP_FLAG_CWR) >> 16);
5492 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5493
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005494 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005495
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005496 return 0;
5497}
5498
5499/**
5500 * i40e_vsi_open -
5501 * @vsi: the VSI to open
5502 *
5503 * Finish initialization of the VSI.
5504 *
5505 * Returns 0 on success, negative value on failure
Maciej Sosin373149f2017-04-05 07:50:55 -04005506 *
5507 * Note: expects to be called while under rtnl_lock()
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005508 **/
5509int i40e_vsi_open(struct i40e_vsi *vsi)
5510{
5511 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005512 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005513 int err;
5514
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005515 /* allocate descriptors */
5516 err = i40e_vsi_setup_tx_resources(vsi);
5517 if (err)
5518 goto err_setup_tx;
5519 err = i40e_vsi_setup_rx_resources(vsi);
5520 if (err)
5521 goto err_setup_rx;
5522
5523 err = i40e_vsi_configure(vsi);
5524 if (err)
5525 goto err_setup_rx;
5526
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005527 if (vsi->netdev) {
5528 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5529 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5530 err = i40e_vsi_request_irq(vsi, int_name);
5531 if (err)
5532 goto err_setup_rx;
5533
5534 /* Notify the stack of the actual queue counts. */
5535 err = netif_set_real_num_tx_queues(vsi->netdev,
5536 vsi->num_queue_pairs);
5537 if (err)
5538 goto err_set_queues;
5539
5540 err = netif_set_real_num_rx_queues(vsi->netdev,
5541 vsi->num_queue_pairs);
5542 if (err)
5543 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005544
5545 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005546 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005547 dev_driver_string(&pf->pdev->dev),
5548 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005549 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005550
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005551 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005552 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005553 goto err_setup_rx;
5554 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005555
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005556 err = i40e_up_complete(vsi);
5557 if (err)
5558 goto err_up_complete;
5559
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005560 return 0;
5561
5562err_up_complete:
5563 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005564err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005565 i40e_vsi_free_irq(vsi);
5566err_setup_rx:
5567 i40e_vsi_free_rx_resources(vsi);
5568err_setup_tx:
5569 i40e_vsi_free_tx_resources(vsi);
5570 if (vsi == pf->vsi[pf->lan_vsi])
Maciej Sosin373149f2017-04-05 07:50:55 -04005571 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005572
5573 return err;
5574}
5575
5576/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005577 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005578 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005579 *
5580 * This function destroys the hlist where all the Flow Director
5581 * filters were saved.
5582 **/
5583static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5584{
5585 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08005586 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005587 struct hlist_node *node2;
5588
5589 hlist_for_each_entry_safe(filter, node2,
5590 &pf->fdir_filter_list, fdir_node) {
5591 hlist_del(&filter->fdir_node);
5592 kfree(filter);
5593 }
Jacob Keller097dbf52017-02-06 14:38:46 -08005594
Jacob Keller0e588de2017-02-06 14:38:50 -08005595 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
5596 list_del(&pit_entry->list);
5597 kfree(pit_entry);
5598 }
5599 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
5600
5601 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
5602 list_del(&pit_entry->list);
5603 kfree(pit_entry);
5604 }
5605 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
5606
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005607 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005608 pf->fd_tcp4_filter_cnt = 0;
5609 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08005610 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005611 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08005612
5613 /* Reprogram the default input set for TCP/IPv4 */
5614 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
5615 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5616 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5617
5618 /* Reprogram the default input set for UDP/IPv4 */
5619 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
5620 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5621 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5622
5623 /* Reprogram the default input set for SCTP/IPv4 */
5624 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
5625 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5626 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5627
5628 /* Reprogram the default input set for Other/IPv4 */
5629 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
5630 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005631}
5632
5633/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005634 * i40e_close - Disables a network interface
5635 * @netdev: network interface device structure
5636 *
5637 * The close entry point is called when an interface is de-activated
5638 * by the OS. The hardware is still under the driver's control, but
5639 * this netdev interface is disabled.
5640 *
5641 * Returns 0, this is not allowed to fail
5642 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005643int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005644{
5645 struct i40e_netdev_priv *np = netdev_priv(netdev);
5646 struct i40e_vsi *vsi = np->vsi;
5647
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005648 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005649
5650 return 0;
5651}
5652
5653/**
5654 * i40e_do_reset - Start a PF or Core Reset sequence
5655 * @pf: board private structure
5656 * @reset_flags: which reset is requested
Maciej Sosin373149f2017-04-05 07:50:55 -04005657 * @lock_acquired: indicates whether or not the lock has been acquired
5658 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005659 *
5660 * The essential difference in resets is that the PF Reset
5661 * doesn't clear the packet buffers, doesn't reset the PE
5662 * firmware, and doesn't bother the other PFs on the chip.
5663 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04005664void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005665{
5666 u32 val;
5667
5668 WARN_ON(in_interrupt());
5669
Mitch Williams263fc482014-04-23 04:50:11 +00005670
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005671 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005672 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005673
5674 /* Request a Global Reset
5675 *
5676 * This will start the chip's countdown to the actual full
5677 * chip reset event, and a warning interrupt to be sent
5678 * to all PFs, including the requestor. Our handler
5679 * for the warning interrupt will deal with the shutdown
5680 * and recovery of the switch setup.
5681 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005682 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005683 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5684 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5685 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5686
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005687 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005688
5689 /* Request a Core Reset
5690 *
5691 * Same as Global Reset, except does *not* include the MAC/PHY
5692 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005693 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005694 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5695 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5696 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5697 i40e_flush(&pf->hw);
5698
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005699 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005700
5701 /* Request a PF Reset
5702 *
5703 * Resets only the PF-specific registers
5704 *
5705 * This goes directly to the tear-down and rebuild of
5706 * the switch, since we need to do all the recovery as
5707 * for the Core Reset.
5708 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005709 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Maciej Sosin373149f2017-04-05 07:50:55 -04005710 i40e_handle_reset_warning(pf, lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005711
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005712 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005713 int v;
5714
5715 /* Find the VSI(s) that requested a re-init */
5716 dev_info(&pf->pdev->dev,
5717 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005718 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005719 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005720
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005721 if (vsi != NULL &&
5722 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5723 i40e_vsi_reinit_locked(pf->vsi[v]);
5724 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5725 }
5726 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005727 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005728 int v;
5729
5730 /* Find the VSI(s) that needs to be brought down */
5731 dev_info(&pf->pdev->dev, "VSI down requested\n");
5732 for (v = 0; v < pf->num_alloc_vsi; v++) {
5733 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005734
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005735 if (vsi != NULL &&
5736 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5737 set_bit(__I40E_DOWN, &vsi->state);
5738 i40e_down(vsi);
5739 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5740 }
5741 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005742 } else {
5743 dev_info(&pf->pdev->dev,
5744 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005745 }
5746}
5747
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005748#ifdef CONFIG_I40E_DCB
5749/**
5750 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5751 * @pf: board private structure
5752 * @old_cfg: current DCB config
5753 * @new_cfg: new DCB config
5754 **/
5755bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5756 struct i40e_dcbx_config *old_cfg,
5757 struct i40e_dcbx_config *new_cfg)
5758{
5759 bool need_reconfig = false;
5760
5761 /* Check if ETS configuration has changed */
5762 if (memcmp(&new_cfg->etscfg,
5763 &old_cfg->etscfg,
5764 sizeof(new_cfg->etscfg))) {
5765 /* If Priority Table has changed reconfig is needed */
5766 if (memcmp(&new_cfg->etscfg.prioritytable,
5767 &old_cfg->etscfg.prioritytable,
5768 sizeof(new_cfg->etscfg.prioritytable))) {
5769 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005770 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005771 }
5772
5773 if (memcmp(&new_cfg->etscfg.tcbwtable,
5774 &old_cfg->etscfg.tcbwtable,
5775 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005776 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005777
5778 if (memcmp(&new_cfg->etscfg.tsatable,
5779 &old_cfg->etscfg.tsatable,
5780 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005781 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005782 }
5783
5784 /* Check if PFC configuration has changed */
5785 if (memcmp(&new_cfg->pfc,
5786 &old_cfg->pfc,
5787 sizeof(new_cfg->pfc))) {
5788 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005789 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005790 }
5791
5792 /* Check if APP Table has changed */
5793 if (memcmp(&new_cfg->app,
5794 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005795 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005796 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005797 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005798 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005799
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005800 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005801 return need_reconfig;
5802}
5803
5804/**
5805 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5806 * @pf: board private structure
5807 * @e: event info posted on ARQ
5808 **/
5809static int i40e_handle_lldp_event(struct i40e_pf *pf,
5810 struct i40e_arq_event_info *e)
5811{
5812 struct i40e_aqc_lldp_get_mib *mib =
5813 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5814 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005815 struct i40e_dcbx_config tmp_dcbx_cfg;
5816 bool need_reconfig = false;
5817 int ret = 0;
5818 u8 type;
5819
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005820 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005821 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005822 return ret;
5823
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005824 /* Ignore if event is not for Nearest Bridge */
5825 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5826 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005827 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005828 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5829 return ret;
5830
5831 /* Check MIB Type and return if event for Remote MIB update */
5832 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005833 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005834 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005835 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5836 /* Update the remote cached instance and return */
5837 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5838 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5839 &hw->remote_dcbx_config);
5840 goto exit;
5841 }
5842
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005843 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005844 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005845
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005846 /* Reset the old DCBx configuration data */
5847 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005848 /* Get updated DCBX data from firmware */
5849 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005850 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005851 dev_info(&pf->pdev->dev,
5852 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5853 i40e_stat_str(&pf->hw, ret),
5854 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005855 goto exit;
5856 }
5857
5858 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005859 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5860 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005861 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005862 goto exit;
5863 }
5864
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005865 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5866 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005867
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005868 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005869
5870 if (!need_reconfig)
5871 goto exit;
5872
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005873 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005874 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005875 pf->flags |= I40E_FLAG_DCB_ENABLED;
5876 else
5877 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5878
Neerav Parikh69129dc2014-11-12 00:18:46 +00005879 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005880 /* Reconfiguration needed quiesce all VSIs */
5881 i40e_pf_quiesce_all_vsi(pf);
5882
5883 /* Changes in configuration update VEB/VSI */
5884 i40e_dcb_reconfigure(pf);
5885
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005886 ret = i40e_resume_port_tx(pf);
5887
Neerav Parikh69129dc2014-11-12 00:18:46 +00005888 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005889 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005890 if (ret)
5891 goto exit;
5892
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005893 /* Wait for the PF's queues to be disabled */
5894 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005895 if (ret) {
5896 /* Schedule PF reset to recover */
5897 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5898 i40e_service_event_schedule(pf);
5899 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005900 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08005901 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
5902 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005903 }
5904
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005905exit:
5906 return ret;
5907}
5908#endif /* CONFIG_I40E_DCB */
5909
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005910/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005911 * i40e_do_reset_safe - Protected reset path for userland calls.
5912 * @pf: board private structure
5913 * @reset_flags: which reset is requested
5914 *
5915 **/
5916void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5917{
5918 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -04005919 i40e_do_reset(pf, reset_flags, true);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005920 rtnl_unlock();
5921}
5922
5923/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005924 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5925 * @pf: board private structure
5926 * @e: event info posted on ARQ
5927 *
5928 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5929 * and VF queues
5930 **/
5931static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5932 struct i40e_arq_event_info *e)
5933{
5934 struct i40e_aqc_lan_overflow *data =
5935 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5936 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5937 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5938 struct i40e_hw *hw = &pf->hw;
5939 struct i40e_vf *vf;
5940 u16 vf_id;
5941
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005942 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5943 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005944
5945 /* Queue belongs to VF, find the VF and issue VF reset */
5946 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5947 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5948 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5949 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5950 vf_id -= hw->func_caps.vf_base_id;
5951 vf = &pf->vf[vf_id];
5952 i40e_vc_notify_vf_reset(vf);
5953 /* Allow VF to process pending reset notification */
5954 msleep(20);
5955 i40e_reset_vf(vf, false);
5956 }
5957}
5958
5959/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005960 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5961 * @pf: board private structure
5962 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005963u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005964{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005965 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005966
5967 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5968 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5969 return fcnt_prog;
5970}
5971
5972/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005973 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005974 * @pf: board private structure
5975 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005976u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005977{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005978 u32 val, fcnt_prog;
5979
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005980 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5981 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5982 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5983 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5984 return fcnt_prog;
5985}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005986
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005987/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005988 * i40e_get_global_fd_count - Get total FD filters programmed on device
5989 * @pf: board private structure
5990 **/
5991u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5992{
5993 u32 val, fcnt_prog;
5994
5995 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5996 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5997 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5998 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5999 return fcnt_prog;
6000}
6001
6002/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006003 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6004 * @pf: board private structure
6005 **/
6006void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6007{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006008 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006009 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006010 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006011
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006012 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
6013 return;
6014
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006015 /* Check if, FD SB or ATR was auto disabled and if there is enough room
6016 * to re-enable
6017 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006018 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006019 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006020 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6021 (pf->fd_add_err == 0) ||
6022 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006023 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006024 (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)) {
6025 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006026 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6027 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 +00006028 }
6029 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006030
6031 /* Wait for some more space to be available to turn on ATR. We also
6032 * must check that no existing ntuple rules for TCP are in effect
6033 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006034 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
6035 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006036 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller097dbf52017-02-06 14:38:46 -08006037 (pf->fd_tcp4_filter_cnt == 0)) {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006038 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006039 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07006040 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 +00006041 }
6042 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006043
6044 /* if hw had a problem adding a filter, delete it */
6045 if (pf->fd_inv > 0) {
6046 hlist_for_each_entry_safe(filter, node,
6047 &pf->fdir_filter_list, fdir_node) {
6048 if (filter->fd_id == pf->fd_inv) {
6049 hlist_del(&filter->fdir_node);
6050 kfree(filter);
6051 pf->fdir_pf_active_filters--;
6052 }
6053 }
6054 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006055}
6056
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006057#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006058#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006059/**
6060 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6061 * @pf: board private structure
6062 **/
6063static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6064{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006065 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006066 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006067 bool disable_atr = false;
6068 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006069 int reg;
6070
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006071 if (!time_after(jiffies, pf->fd_flush_timestamp +
6072 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6073 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006074
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006075 /* If the flush is happening too quick and we have mostly SB rules we
6076 * should not re-enable ATR for some time.
6077 */
6078 min_flush_time = pf->fd_flush_timestamp +
6079 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6080 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006081
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006082 if (!(time_after(jiffies, min_flush_time)) &&
6083 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6084 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6085 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6086 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006087 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006088
6089 pf->fd_flush_timestamp = jiffies;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006090 pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006091 /* flush all filters */
6092 wr32(&pf->hw, I40E_PFQF_CTL_1,
6093 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6094 i40e_flush(&pf->hw);
6095 pf->fd_flush_cnt++;
6096 pf->fd_add_err = 0;
6097 do {
6098 /* Check FD flush status every 5-6msec */
6099 usleep_range(5000, 6000);
6100 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6101 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6102 break;
6103 } while (flush_wait_retry--);
6104 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6105 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6106 } else {
6107 /* replay sideband filters */
6108 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08006109 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006110 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006111 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6112 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6113 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6114 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006115}
6116
6117/**
6118 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6119 * @pf: board private structure
6120 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006121u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006122{
6123 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6124}
6125
6126/* We can see up to 256 filter programming desc in transit if the filters are
6127 * being applied really fast; before we see the first
6128 * filter miss error on Rx queue 0. Accumulating enough error messages before
6129 * reacting will make sure we don't cause flush too often.
6130 */
6131#define I40E_MAX_FD_PROGRAM_ERROR 256
6132
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006133/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006134 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6135 * @pf: board private structure
6136 **/
6137static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6138{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006139
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006140 /* if interface is down do nothing */
6141 if (test_bit(__I40E_DOWN, &pf->state))
6142 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006143
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006144 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006145 i40e_fdir_flush_and_replay(pf);
6146
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006147 i40e_fdir_check_and_reenable(pf);
6148
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006149}
6150
6151/**
6152 * i40e_vsi_link_event - notify VSI of a link event
6153 * @vsi: vsi to be notified
6154 * @link_up: link up or down
6155 **/
6156static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6157{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006158 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006159 return;
6160
6161 switch (vsi->type) {
6162 case I40E_VSI_MAIN:
6163 if (!vsi->netdev || !vsi->netdev_registered)
6164 break;
6165
6166 if (link_up) {
6167 netif_carrier_on(vsi->netdev);
6168 netif_tx_wake_all_queues(vsi->netdev);
6169 } else {
6170 netif_carrier_off(vsi->netdev);
6171 netif_tx_stop_all_queues(vsi->netdev);
6172 }
6173 break;
6174
6175 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006176 case I40E_VSI_VMDQ2:
6177 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006178 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006179 case I40E_VSI_MIRROR:
6180 default:
6181 /* there is no notification for other VSIs */
6182 break;
6183 }
6184}
6185
6186/**
6187 * i40e_veb_link_event - notify elements on the veb of a link event
6188 * @veb: veb to be notified
6189 * @link_up: link up or down
6190 **/
6191static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6192{
6193 struct i40e_pf *pf;
6194 int i;
6195
6196 if (!veb || !veb->pf)
6197 return;
6198 pf = veb->pf;
6199
6200 /* depth first... */
6201 for (i = 0; i < I40E_MAX_VEB; i++)
6202 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6203 i40e_veb_link_event(pf->veb[i], link_up);
6204
6205 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006206 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006207 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6208 i40e_vsi_link_event(pf->vsi[i], link_up);
6209}
6210
6211/**
6212 * i40e_link_event - Update netif_carrier status
6213 * @pf: board private structure
6214 **/
6215static void i40e_link_event(struct i40e_pf *pf)
6216{
Mitch Williams320684c2014-10-17 03:14:43 +00006217 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006218 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006219 i40e_status status;
6220 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006221
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006222 /* save off old link status information */
6223 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6224
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006225 /* set this to force the get_link_status call to refresh state */
6226 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006227
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006228 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006229
6230 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006231
6232 /* On success, disable temp link polling */
6233 if (status == I40E_SUCCESS) {
6234 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
6235 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
6236 } else {
6237 /* Enable link polling temporarily until i40e_get_link_status
6238 * returns I40E_SUCCESS
6239 */
6240 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006241 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6242 status);
6243 return;
6244 }
6245
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006246 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6247 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006248
6249 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006250 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006251 (test_bit(__I40E_DOWN, &vsi->state) ||
6252 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006253 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006254
6255 if (!test_bit(__I40E_DOWN, &vsi->state))
6256 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006257
6258 /* Notify the base of the switch tree connected to
6259 * the link. Floating VEBs are not notified.
6260 */
6261 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6262 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6263 else
Mitch Williams320684c2014-10-17 03:14:43 +00006264 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006265
6266 if (pf->vf)
6267 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006268
6269 if (pf->flags & I40E_FLAG_PTP)
6270 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006271}
6272
6273/**
Shannon Nelson21536712014-10-25 10:35:25 +00006274 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006275 * @pf: board private structure
6276 **/
6277static void i40e_watchdog_subtask(struct i40e_pf *pf)
6278{
6279 int i;
6280
6281 /* if interface is down do nothing */
6282 if (test_bit(__I40E_DOWN, &pf->state) ||
6283 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6284 return;
6285
Shannon Nelson21536712014-10-25 10:35:25 +00006286 /* make sure we don't do these things too often */
6287 if (time_before(jiffies, (pf->service_timer_previous +
6288 pf->service_timer_period)))
6289 return;
6290 pf->service_timer_previous = jiffies;
6291
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006292 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
6293 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04006294 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006295
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006296 /* Update the stats for active netdevs so the network stack
6297 * can look at updated numbers whenever it cares to
6298 */
Mitch Williams505682c2014-05-20 08:01:37 +00006299 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006300 if (pf->vsi[i] && pf->vsi[i]->netdev)
6301 i40e_update_stats(pf->vsi[i]);
6302
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006303 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6304 /* Update the stats for the active switching components */
6305 for (i = 0; i < I40E_MAX_VEB; i++)
6306 if (pf->veb[i])
6307 i40e_update_veb_stats(pf->veb[i]);
6308 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006309
6310 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006311}
6312
6313/**
6314 * i40e_reset_subtask - Set up for resetting the device and driver
6315 * @pf: board private structure
6316 **/
6317static void i40e_reset_subtask(struct i40e_pf *pf)
6318{
6319 u32 reset_flags = 0;
6320
6321 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006322 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006323 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6324 }
6325 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006326 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006327 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6328 }
6329 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006330 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006331 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6332 }
6333 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006334 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006335 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6336 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006337 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006338 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006339 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6340 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006341
6342 /* If there's a recovery already waiting, it takes
6343 * precedence before starting a new reset sequence.
6344 */
6345 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
Maciej Sosin373149f2017-04-05 07:50:55 -04006346 i40e_prep_for_reset(pf, false);
6347 i40e_reset(pf);
6348 i40e_rebuild(pf, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006349 }
6350
6351 /* If we're already down or resetting, just bail */
6352 if (reset_flags &&
6353 !test_bit(__I40E_DOWN, &pf->state) &&
Maciej Sosin373149f2017-04-05 07:50:55 -04006354 !test_bit(__I40E_CONFIG_BUSY, &pf->state)) {
6355 rtnl_lock();
6356 i40e_do_reset(pf, reset_flags, true);
6357 rtnl_unlock();
6358 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006359}
6360
6361/**
6362 * i40e_handle_link_event - Handle link event
6363 * @pf: board private structure
6364 * @e: event info posted on ARQ
6365 **/
6366static void i40e_handle_link_event(struct i40e_pf *pf,
6367 struct i40e_arq_event_info *e)
6368{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006369 struct i40e_aqc_get_link_status *status =
6370 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006371
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006372 /* Do a new status request to re-enable LSE reporting
6373 * and load new status information into the hw struct
6374 * This completely ignores any state information
6375 * in the ARQ event info, instead choosing to always
6376 * issue the AQ update link status command.
6377 */
6378 i40e_link_event(pf);
6379
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006380 /* check for unqualified module, if link is down */
6381 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6382 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6383 (!(status->link_info & I40E_AQ_LINK_UP)))
6384 dev_err(&pf->pdev->dev,
6385 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006386}
6387
6388/**
6389 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6390 * @pf: board private structure
6391 **/
6392static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6393{
6394 struct i40e_arq_event_info event;
6395 struct i40e_hw *hw = &pf->hw;
6396 u16 pending, i = 0;
6397 i40e_status ret;
6398 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006399 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006400 u32 val;
6401
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006402 /* Do not run clean AQ when PF reset fails */
6403 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6404 return;
6405
Shannon Nelson86df2422014-05-20 08:01:35 +00006406 /* check for error indications */
6407 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6408 oldval = val;
6409 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006410 if (hw->debug_mask & I40E_DEBUG_AQ)
6411 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006412 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6413 }
6414 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006415 if (hw->debug_mask & I40E_DEBUG_AQ)
6416 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006417 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006418 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006419 }
6420 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006421 if (hw->debug_mask & I40E_DEBUG_AQ)
6422 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006423 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6424 }
6425 if (oldval != val)
6426 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6427
6428 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6429 oldval = val;
6430 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006431 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6432 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006433 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6434 }
6435 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006436 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6437 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006438 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6439 }
6440 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006441 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6442 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006443 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6444 }
6445 if (oldval != val)
6446 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6447
Mitch Williams1001dc32014-11-11 20:02:19 +00006448 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6449 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006450 if (!event.msg_buf)
6451 return;
6452
6453 do {
6454 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006455 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006456 break;
Mitch Williams56497972014-06-04 08:45:18 +00006457 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006458 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6459 break;
6460 }
6461
6462 opcode = le16_to_cpu(event.desc.opcode);
6463 switch (opcode) {
6464
6465 case i40e_aqc_opc_get_link_status:
6466 i40e_handle_link_event(pf, &event);
6467 break;
6468 case i40e_aqc_opc_send_msg_to_pf:
6469 ret = i40e_vc_process_vf_msg(pf,
6470 le16_to_cpu(event.desc.retval),
6471 le32_to_cpu(event.desc.cookie_high),
6472 le32_to_cpu(event.desc.cookie_low),
6473 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006474 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006475 break;
6476 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006477 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006478#ifdef CONFIG_I40E_DCB
6479 rtnl_lock();
6480 ret = i40e_handle_lldp_event(pf, &event);
6481 rtnl_unlock();
6482#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006483 break;
6484 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006485 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006486 i40e_handle_lan_overflow_event(pf, &event);
6487 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006488 case i40e_aqc_opc_send_msg_to_peer:
6489 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6490 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006491 case i40e_aqc_opc_nvm_erase:
6492 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006493 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006494 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6495 "ARQ NVM operation 0x%04x completed\n",
6496 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006497 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006498 default:
6499 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006500 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006501 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006502 break;
6503 }
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006504 } while (i++ < pf->adminq_work_limit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006505
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006506 if (i < pf->adminq_work_limit)
6507 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6508
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006509 /* re-enable Admin queue interrupt cause */
6510 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6511 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6512 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6513 i40e_flush(hw);
6514
6515 kfree(event.msg_buf);
6516}
6517
6518/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006519 * i40e_verify_eeprom - make sure eeprom is good to use
6520 * @pf: board private structure
6521 **/
6522static void i40e_verify_eeprom(struct i40e_pf *pf)
6523{
6524 int err;
6525
6526 err = i40e_diag_eeprom_test(&pf->hw);
6527 if (err) {
6528 /* retry in case of garbage read */
6529 err = i40e_diag_eeprom_test(&pf->hw);
6530 if (err) {
6531 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6532 err);
6533 set_bit(__I40E_BAD_EEPROM, &pf->state);
6534 }
6535 }
6536
6537 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6538 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6539 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6540 }
6541}
6542
6543/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006544 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006545 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006546 *
6547 * enable switch loop back or die - no point in a return value
6548 **/
6549static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6550{
6551 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6552 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006553 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006554
6555 ctxt.seid = pf->main_vsi_seid;
6556 ctxt.pf_num = pf->hw.pf_id;
6557 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006558 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6559 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006560 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006561 "couldn't get PF vsi config, err %s aq_err %s\n",
6562 i40e_stat_str(&pf->hw, ret),
6563 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006564 return;
6565 }
6566 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6567 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6568 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6569
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006570 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6571 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006572 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006573 "update vsi switch failed, err %s aq_err %s\n",
6574 i40e_stat_str(&pf->hw, ret),
6575 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006576 }
6577}
6578
6579/**
6580 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006581 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006582 *
6583 * disable switch loop back or die - no point in a return value
6584 **/
6585static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6586{
6587 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6588 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006589 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006590
6591 ctxt.seid = pf->main_vsi_seid;
6592 ctxt.pf_num = pf->hw.pf_id;
6593 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006594 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6595 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006596 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006597 "couldn't get PF vsi config, err %s aq_err %s\n",
6598 i40e_stat_str(&pf->hw, ret),
6599 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006600 return;
6601 }
6602 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6603 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6604 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6605
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006606 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6607 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006608 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006609 "update vsi switch failed, err %s aq_err %s\n",
6610 i40e_stat_str(&pf->hw, ret),
6611 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006612 }
6613}
6614
6615/**
Neerav Parikh51616012015-02-06 08:52:14 +00006616 * i40e_config_bridge_mode - Configure the HW bridge mode
6617 * @veb: pointer to the bridge instance
6618 *
6619 * Configure the loop back mode for the LAN VSI that is downlink to the
6620 * specified HW bridge instance. It is expected this function is called
6621 * when a new HW bridge is instantiated.
6622 **/
6623static void i40e_config_bridge_mode(struct i40e_veb *veb)
6624{
6625 struct i40e_pf *pf = veb->pf;
6626
Shannon Nelson6dec1012015-09-28 14:12:30 -04006627 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6628 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6629 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006630 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6631 i40e_disable_pf_switch_lb(pf);
6632 else
6633 i40e_enable_pf_switch_lb(pf);
6634}
6635
6636/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006637 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6638 * @veb: pointer to the VEB instance
6639 *
6640 * This is a recursive function that first builds the attached VSIs then
6641 * recurses in to build the next layer of VEB. We track the connections
6642 * through our own index numbers because the seid's from the HW could
6643 * change across the reset.
6644 **/
6645static int i40e_reconstitute_veb(struct i40e_veb *veb)
6646{
6647 struct i40e_vsi *ctl_vsi = NULL;
6648 struct i40e_pf *pf = veb->pf;
6649 int v, veb_idx;
6650 int ret;
6651
6652 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006653 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006654 if (pf->vsi[v] &&
6655 pf->vsi[v]->veb_idx == veb->idx &&
6656 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6657 ctl_vsi = pf->vsi[v];
6658 break;
6659 }
6660 }
6661 if (!ctl_vsi) {
6662 dev_info(&pf->pdev->dev,
6663 "missing owner VSI for veb_idx %d\n", veb->idx);
6664 ret = -ENOENT;
6665 goto end_reconstitute;
6666 }
6667 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6668 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6669 ret = i40e_add_vsi(ctl_vsi);
6670 if (ret) {
6671 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006672 "rebuild of veb_idx %d owner VSI failed: %d\n",
6673 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006674 goto end_reconstitute;
6675 }
6676 i40e_vsi_reset_stats(ctl_vsi);
6677
6678 /* create the VEB in the switch and move the VSI onto the VEB */
6679 ret = i40e_add_veb(veb, ctl_vsi);
6680 if (ret)
6681 goto end_reconstitute;
6682
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006683 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6684 veb->bridge_mode = BRIDGE_MODE_VEB;
6685 else
6686 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006687 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006688
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006689 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006690 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006691 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6692 continue;
6693
6694 if (pf->vsi[v]->veb_idx == veb->idx) {
6695 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006696
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006697 vsi->uplink_seid = veb->seid;
6698 ret = i40e_add_vsi(vsi);
6699 if (ret) {
6700 dev_info(&pf->pdev->dev,
6701 "rebuild of vsi_idx %d failed: %d\n",
6702 v, ret);
6703 goto end_reconstitute;
6704 }
6705 i40e_vsi_reset_stats(vsi);
6706 }
6707 }
6708
6709 /* create any VEBs attached to this VEB - RECURSION */
6710 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6711 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6712 pf->veb[veb_idx]->uplink_seid = veb->seid;
6713 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6714 if (ret)
6715 break;
6716 }
6717 }
6718
6719end_reconstitute:
6720 return ret;
6721}
6722
6723/**
6724 * i40e_get_capabilities - get info about the HW
6725 * @pf: the PF struct
6726 **/
6727static int i40e_get_capabilities(struct i40e_pf *pf)
6728{
6729 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6730 u16 data_size;
6731 int buf_len;
6732 int err;
6733
6734 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6735 do {
6736 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6737 if (!cap_buf)
6738 return -ENOMEM;
6739
6740 /* this loads the data into the hw struct for us */
6741 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6742 &data_size,
6743 i40e_aqc_opc_list_func_capabilities,
6744 NULL);
6745 /* data loaded, buffer no longer needed */
6746 kfree(cap_buf);
6747
6748 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6749 /* retry with a larger buffer */
6750 buf_len = data_size;
6751 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6752 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006753 "capability discovery failed, err %s aq_err %s\n",
6754 i40e_stat_str(&pf->hw, err),
6755 i40e_aq_str(&pf->hw,
6756 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006757 return -ENODEV;
6758 }
6759 } while (err);
6760
6761 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6762 dev_info(&pf->pdev->dev,
6763 "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",
6764 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6765 pf->hw.func_caps.num_msix_vectors,
6766 pf->hw.func_caps.num_msix_vectors_vf,
6767 pf->hw.func_caps.fd_filters_guaranteed,
6768 pf->hw.func_caps.fd_filters_best_effort,
6769 pf->hw.func_caps.num_tx_qp,
6770 pf->hw.func_caps.num_vsis);
6771
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006772#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6773 + pf->hw.func_caps.num_vfs)
6774 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6775 dev_info(&pf->pdev->dev,
6776 "got num_vsis %d, setting num_vsis to %d\n",
6777 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6778 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6779 }
6780
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006781 return 0;
6782}
6783
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006784static int i40e_vsi_clear(struct i40e_vsi *vsi);
6785
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006786/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006787 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006788 * @pf: board private structure
6789 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006790static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006791{
6792 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006793
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006794 /* quick workaround for an NVM issue that leaves a critical register
6795 * uninitialized
6796 */
6797 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6798 static const u32 hkey[] = {
6799 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6800 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6801 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6802 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006803 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006804
6805 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6806 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6807 }
6808
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006809 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006810 return;
6811
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006812 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006813 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006814
6815 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006816 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006817 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6818 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006819 if (!vsi) {
6820 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006821 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6822 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006823 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006824 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006825
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006826 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006827}
6828
6829/**
6830 * i40e_fdir_teardown - release the Flow Director resources
6831 * @pf: board private structure
6832 **/
6833static void i40e_fdir_teardown(struct i40e_pf *pf)
6834{
Alexander Duyck4b816442016-10-11 15:26:53 -07006835 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006836
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006837 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07006838 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6839 if (vsi)
6840 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006841}
6842
6843/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006844 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006845 * @pf: board private structure
Maciej Sosin373149f2017-04-05 07:50:55 -04006846 * @lock_acquired: indicates whether or not the lock has been acquired
6847 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006848 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006849 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006850 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04006851static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006852{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006853 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006854 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006855 u32 v;
6856
6857 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6858 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006859 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006860 if (i40e_check_asq_alive(&pf->hw))
6861 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006862
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006863 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006864
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006865 /* quiesce the VSIs and their queues that are not already DOWN */
Maciej Sosin373149f2017-04-05 07:50:55 -04006866 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
6867 if (!lock_acquired)
6868 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006869 i40e_pf_quiesce_all_vsi(pf);
Maciej Sosin373149f2017-04-05 07:50:55 -04006870 if (!lock_acquired)
6871 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006872
Mitch Williams505682c2014-05-20 08:01:37 +00006873 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006874 if (pf->vsi[v])
6875 pf->vsi[v]->seid = 0;
6876 }
6877
6878 i40e_shutdown_adminq(&pf->hw);
6879
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006880 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006881 if (hw->hmc.hmc_obj) {
6882 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006883 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006884 dev_warn(&pf->pdev->dev,
6885 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006886 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006887}
6888
6889/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006890 * i40e_send_version - update firmware with driver version
6891 * @pf: PF struct
6892 */
6893static void i40e_send_version(struct i40e_pf *pf)
6894{
6895 struct i40e_driver_version dv;
6896
6897 dv.major_version = DRV_VERSION_MAJOR;
6898 dv.minor_version = DRV_VERSION_MINOR;
6899 dv.build_version = DRV_VERSION_BUILD;
6900 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006901 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006902 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6903}
6904
6905/**
Maciej Sosin373149f2017-04-05 07:50:55 -04006906 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
6907 * @pf: board private structure
6908 **/
6909static int i40e_reset(struct i40e_pf *pf)
6910{
6911 struct i40e_hw *hw = &pf->hw;
6912 i40e_status ret;
6913
6914 ret = i40e_pf_reset(hw);
6915 if (ret) {
6916 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
6917 set_bit(__I40E_RESET_FAILED, &pf->state);
6918 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6919 } else {
6920 pf->pfr_count++;
6921 }
6922 return ret;
6923}
6924
6925/**
6926 * i40e_rebuild - rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006927 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006928 * @reinit: if the Main VSI needs to re-initialized.
Maciej Sosin373149f2017-04-05 07:50:55 -04006929 * @lock_acquired: indicates whether or not the lock has been acquired
6930 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006931 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04006932static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006933{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006934 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006935 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006936 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006937 u32 val;
Maciej Sosin373149f2017-04-05 07:50:55 -04006938 int v;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006939
6940 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006941 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006942 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006943
6944 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6945 ret = i40e_init_adminq(&pf->hw);
6946 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006947 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6948 i40e_stat_str(&pf->hw, ret),
6949 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006950 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006951 }
6952
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006953 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006954 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006955 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006956
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006957 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006958 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006959 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006960 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006961
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006962 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08006963 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006964 if (ret) {
6965 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6966 goto end_core_reset;
6967 }
6968 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6969 if (ret) {
6970 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6971 goto end_core_reset;
6972 }
6973
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006974#ifdef CONFIG_I40E_DCB
6975 ret = i40e_init_pf_dcb(pf);
6976 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006977 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6978 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6979 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006980 }
6981#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006982 /* do basic switch setup */
Maciej Sosin373149f2017-04-05 07:50:55 -04006983 if (!lock_acquired)
6984 rtnl_lock();
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006985 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006986 if (ret)
Maciej Sosin373149f2017-04-05 07:50:55 -04006987 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006988
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006989 /* The driver only wants link up/down and module qualification
6990 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006991 */
6992 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006993 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07006994 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006995 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006996 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006997 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6998 i40e_stat_str(&pf->hw, ret),
6999 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007000
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007001 /* make sure our flow control settings are restored */
7002 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7003 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007004 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7005 i40e_stat_str(&pf->hw, ret),
7006 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007007
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007008 /* Rebuild the VSIs and VEBs that existed before reset.
7009 * They are still in our local switch element arrays, so only
7010 * need to rebuild the switch model in the HW.
7011 *
7012 * If there were VEBs but the reconstitution failed, we'll try
7013 * try to recover minimal use by getting the basic PF VSI working.
7014 */
7015 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007016 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007017 /* find the one VEB connected to the MAC, and find orphans */
7018 for (v = 0; v < I40E_MAX_VEB; v++) {
7019 if (!pf->veb[v])
7020 continue;
7021
7022 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7023 pf->veb[v]->uplink_seid == 0) {
7024 ret = i40e_reconstitute_veb(pf->veb[v]);
7025
7026 if (!ret)
7027 continue;
7028
7029 /* If Main VEB failed, we're in deep doodoo,
7030 * so give up rebuilding the switch and set up
7031 * for minimal rebuild of PF VSI.
7032 * If orphan failed, we'll report the error
7033 * but try to keep going.
7034 */
7035 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7036 dev_info(&pf->pdev->dev,
7037 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7038 ret);
7039 pf->vsi[pf->lan_vsi]->uplink_seid
7040 = pf->mac_seid;
7041 break;
7042 } else if (pf->veb[v]->uplink_seid == 0) {
7043 dev_info(&pf->pdev->dev,
7044 "rebuild of orphan VEB failed: %d\n",
7045 ret);
7046 }
7047 }
7048 }
7049 }
7050
7051 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007052 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007053 /* no VEB, so rebuild only the Main VSI */
7054 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7055 if (ret) {
7056 dev_info(&pf->pdev->dev,
7057 "rebuild of Main VSI failed: %d\n", ret);
Maciej Sosin373149f2017-04-05 07:50:55 -04007058 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007059 }
7060 }
7061
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007062 /* Reconfigure hardware for allowing smaller MSS in the case
7063 * of TSO, so that we avoid the MDD being fired and causing
7064 * a reset in the case of small MSS+TSO.
7065 */
7066#define I40E_REG_MSS 0x000E64DC
7067#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7068#define I40E_64BYTE_MSS 0x400000
7069 val = rd32(hw, I40E_REG_MSS);
7070 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7071 val &= ~I40E_REG_MSS_MIN_MASK;
7072 val |= I40E_64BYTE_MSS;
7073 wr32(hw, I40E_REG_MSS, val);
7074 }
7075
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08007076 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007077 msleep(75);
7078 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7079 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007080 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7081 i40e_stat_str(&pf->hw, ret),
7082 i40e_aq_str(&pf->hw,
7083 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007084 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007085 /* reinit the misc interrupt */
7086 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7087 ret = i40e_setup_misc_vector(pf);
7088
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007089 /* Add a filter to drop all Flow control frames from any VSI from being
7090 * transmitted. By doing so we stop a malicious VF from sending out
7091 * PAUSE or PFC frames and potentially controlling traffic for other
7092 * PF/VF VSIs.
7093 * The FW can still send Flow control frames if enabled.
7094 */
7095 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7096 pf->main_vsi_seid);
7097
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007098 /* restart the VSIs that were rebuilt and running before the reset */
7099 i40e_pf_unquiesce_all_vsi(pf);
7100
Mitch Williams69f64b22014-02-13 03:48:46 -08007101 if (pf->num_alloc_vfs) {
7102 for (v = 0; v < pf->num_alloc_vfs; v++)
7103 i40e_reset_vf(&pf->vf[v], true);
7104 }
7105
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007106 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007107 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007108
Maciej Sosin373149f2017-04-05 07:50:55 -04007109end_unlock:
7110if (!lock_acquired)
7111 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007112end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007113 clear_bit(__I40E_RESET_FAILED, &pf->state);
7114clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007115 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7116}
7117
7118/**
Maciej Sosin373149f2017-04-05 07:50:55 -04007119 * i40e_reset_and_rebuild - reset and rebuild using a saved config
7120 * @pf: board private structure
7121 * @reinit: if the Main VSI needs to re-initialized.
7122 * @lock_acquired: indicates whether or not the lock has been acquired
7123 * before this function was called.
7124 **/
7125static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
7126 bool lock_acquired)
7127{
7128 int ret;
7129 /* Now we wait for GRST to settle out.
7130 * We don't have to delete the VEBs or VSIs from the hw switch
7131 * because the reset will make them disappear.
7132 */
7133 ret = i40e_reset(pf);
7134 if (!ret)
7135 i40e_rebuild(pf, reinit, lock_acquired);
7136}
7137
7138/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007139 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007140 * @pf: board private structure
7141 *
7142 * Close up the VFs and other things in prep for a Core Reset,
7143 * then get ready to rebuild the world.
Maciej Sosin373149f2017-04-05 07:50:55 -04007144 * @lock_acquired: indicates whether or not the lock has been acquired
7145 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007146 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007147static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007148{
Maciej Sosin373149f2017-04-05 07:50:55 -04007149 i40e_prep_for_reset(pf, lock_acquired);
7150 i40e_reset_and_rebuild(pf, false, lock_acquired);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007151}
7152
7153/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007154 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007155 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007156 *
7157 * Called from the MDD irq handler to identify possibly malicious vfs
7158 **/
7159static void i40e_handle_mdd_event(struct i40e_pf *pf)
7160{
7161 struct i40e_hw *hw = &pf->hw;
7162 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007163 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007164 struct i40e_vf *vf;
7165 u32 reg;
7166 int i;
7167
7168 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7169 return;
7170
7171 /* find what triggered the MDD event */
7172 reg = rd32(hw, I40E_GL_MDET_TX);
7173 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007174 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7175 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007176 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007177 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007178 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007179 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007180 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7181 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7182 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007183 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007184 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 +00007185 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007186 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7187 mdd_detected = true;
7188 }
7189 reg = rd32(hw, I40E_GL_MDET_RX);
7190 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007191 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7192 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007193 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007194 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007195 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7196 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7197 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007198 if (netif_msg_rx_err(pf))
7199 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7200 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007201 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7202 mdd_detected = true;
7203 }
7204
Neerav Parikhdf430b12014-06-04 01:23:15 +00007205 if (mdd_detected) {
7206 reg = rd32(hw, I40E_PF_MDET_TX);
7207 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7208 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007209 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007210 pf_mdd_detected = true;
7211 }
7212 reg = rd32(hw, I40E_PF_MDET_RX);
7213 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7214 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007215 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007216 pf_mdd_detected = true;
7217 }
7218 /* Queue belongs to the PF, initiate a reset */
7219 if (pf_mdd_detected) {
7220 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7221 i40e_service_event_schedule(pf);
7222 }
7223 }
7224
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007225 /* see if one of the VFs needs its hand slapped */
7226 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7227 vf = &(pf->vf[i]);
7228 reg = rd32(hw, I40E_VP_MDET_TX(i));
7229 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7230 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7231 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007232 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7233 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007234 }
7235
7236 reg = rd32(hw, I40E_VP_MDET_RX(i));
7237 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7238 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7239 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007240 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7241 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007242 }
7243
7244 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7245 dev_info(&pf->pdev->dev,
7246 "Too many MDD events on VF %d, disabled\n", i);
7247 dev_info(&pf->pdev->dev,
7248 "Use PF Control I/F to re-enable the VF\n");
7249 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7250 }
7251 }
7252
7253 /* re-enable mdd interrupt cause */
7254 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7255 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7256 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7257 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7258 i40e_flush(hw);
7259}
7260
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007261/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007262 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007263 * @pf: board private structure
7264 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007265static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007266{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007267 struct i40e_hw *hw = &pf->hw;
7268 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007269 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007270 int i;
7271
Singhai, Anjali6a899022015-12-14 12:21:18 -08007272 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007273 return;
7274
Singhai, Anjali6a899022015-12-14 12:21:18 -08007275 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007276
7277 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007278 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7279 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7280 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007281 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007282 ret = i40e_aq_add_udp_tunnel(hw, port,
7283 pf->udp_ports[i].type,
7284 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007285 else
7286 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007287
7288 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007289 dev_dbg(&pf->pdev->dev,
7290 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7291 pf->udp_ports[i].type ? "vxlan" : "geneve",
7292 port ? "add" : "delete",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007293 port, i,
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007294 i40e_stat_str(&pf->hw, ret),
7295 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007296 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007297 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007298 }
7299 }
7300 }
7301}
7302
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007303/**
7304 * i40e_service_task - Run the driver's async subtasks
7305 * @work: pointer to work_struct containing our data
7306 **/
7307static void i40e_service_task(struct work_struct *work)
7308{
7309 struct i40e_pf *pf = container_of(work,
7310 struct i40e_pf,
7311 service_task);
7312 unsigned long start_time = jiffies;
7313
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007314 /* don't bother with service tasks if a reset is in progress */
7315 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007316 return;
7317 }
7318
Mitch Williams91089032016-11-21 13:03:51 -08007319 if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
7320 return;
7321
Kiran Patilb03a8c12015-09-24 18:13:15 -04007322 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007323 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007324 i40e_reset_subtask(pf);
7325 i40e_handle_mdd_event(pf);
7326 i40e_vc_process_vflr_event(pf);
7327 i40e_watchdog_subtask(pf);
7328 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007329 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
7330 /* Client subtask will reopen next time through. */
7331 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
7332 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
7333 } else {
7334 i40e_client_subtask(pf);
7335 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
7336 i40e_notify_client_of_l2_param_changes(
7337 pf->vsi[pf->lan_vsi]);
7338 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
7339 }
7340 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007341 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007342 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007343 i40e_clean_adminq_subtask(pf);
7344
Mitch Williams91089032016-11-21 13:03:51 -08007345 /* flush memory to make sure state is correct before next watchdog */
7346 smp_mb__before_atomic();
7347 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007348
7349 /* If the tasks have taken longer than one timer cycle or there
7350 * is more work to be done, reschedule the service task now
7351 * rather than wait for the timer to tick again.
7352 */
7353 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7354 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7355 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7356 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7357 i40e_service_event_schedule(pf);
7358}
7359
7360/**
7361 * i40e_service_timer - timer callback
7362 * @data: pointer to PF struct
7363 **/
7364static void i40e_service_timer(unsigned long data)
7365{
7366 struct i40e_pf *pf = (struct i40e_pf *)data;
7367
7368 mod_timer(&pf->service_timer,
7369 round_jiffies(jiffies + pf->service_timer_period));
7370 i40e_service_event_schedule(pf);
7371}
7372
7373/**
7374 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7375 * @vsi: the VSI being configured
7376 **/
7377static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7378{
7379 struct i40e_pf *pf = vsi->back;
7380
7381 switch (vsi->type) {
7382 case I40E_VSI_MAIN:
7383 vsi->alloc_queue_pairs = pf->num_lan_qps;
7384 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7385 I40E_REQ_DESCRIPTOR_MULTIPLE);
7386 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7387 vsi->num_q_vectors = pf->num_lan_msix;
7388 else
7389 vsi->num_q_vectors = 1;
7390
7391 break;
7392
7393 case I40E_VSI_FDIR:
7394 vsi->alloc_queue_pairs = 1;
7395 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7396 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007397 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007398 break;
7399
7400 case I40E_VSI_VMDQ2:
7401 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7402 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7403 I40E_REQ_DESCRIPTOR_MULTIPLE);
7404 vsi->num_q_vectors = pf->num_vmdq_msix;
7405 break;
7406
7407 case I40E_VSI_SRIOV:
7408 vsi->alloc_queue_pairs = pf->num_vf_qps;
7409 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7410 I40E_REQ_DESCRIPTOR_MULTIPLE);
7411 break;
7412
7413 default:
7414 WARN_ON(1);
7415 return -ENODATA;
7416 }
7417
7418 return 0;
7419}
7420
7421/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007422 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7423 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007424 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007425 *
7426 * On error: returns error code (negative)
7427 * On success: returns 0
7428 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007429static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007430{
7431 int size;
7432 int ret = 0;
7433
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007434 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007435 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7436 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7437 if (!vsi->tx_rings)
7438 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007439 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7440
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007441 if (alloc_qvectors) {
7442 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007443 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007444 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7445 if (!vsi->q_vectors) {
7446 ret = -ENOMEM;
7447 goto err_vectors;
7448 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007449 }
7450 return ret;
7451
7452err_vectors:
7453 kfree(vsi->tx_rings);
7454 return ret;
7455}
7456
7457/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007458 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7459 * @pf: board private structure
7460 * @type: type of VSI
7461 *
7462 * On error: returns error code (negative)
7463 * On success: returns vsi index in PF (positive)
7464 **/
7465static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7466{
7467 int ret = -ENODEV;
7468 struct i40e_vsi *vsi;
7469 int vsi_idx;
7470 int i;
7471
7472 /* Need to protect the allocation of the VSIs at the PF level */
7473 mutex_lock(&pf->switch_mutex);
7474
7475 /* VSI list may be fragmented if VSI creation/destruction has
7476 * been happening. We can afford to do a quick scan to look
7477 * for any free VSIs in the list.
7478 *
7479 * find next empty vsi slot, looping back around if necessary
7480 */
7481 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007482 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007483 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007484 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007485 i = 0;
7486 while (i < pf->next_vsi && pf->vsi[i])
7487 i++;
7488 }
7489
Mitch Williams505682c2014-05-20 08:01:37 +00007490 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007491 vsi_idx = i; /* Found one! */
7492 } else {
7493 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007494 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007495 }
7496 pf->next_vsi = ++i;
7497
7498 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7499 if (!vsi) {
7500 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007501 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007502 }
7503 vsi->type = type;
7504 vsi->back = pf;
7505 set_bit(__I40E_DOWN, &vsi->state);
7506 vsi->flags = 0;
7507 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007508 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007509 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7510 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007511 vsi->netdev_registered = false;
7512 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007513 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007514 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007515
Alexander Duyck9f65e152013-09-28 06:00:58 +00007516 ret = i40e_set_num_rings_in_vsi(vsi);
7517 if (ret)
7518 goto err_rings;
7519
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007520 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007521 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007522 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007523
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007524 /* Setup default MSIX irq handler for VSI */
7525 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7526
Kiran Patil21659032015-09-30 14:09:03 -04007527 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007528 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007529 pf->vsi[vsi_idx] = vsi;
7530 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007531 goto unlock_pf;
7532
Alexander Duyck9f65e152013-09-28 06:00:58 +00007533err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007534 pf->next_vsi = i - 1;
7535 kfree(vsi);
7536unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007537 mutex_unlock(&pf->switch_mutex);
7538 return ret;
7539}
7540
7541/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007542 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7543 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007544 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007545 *
7546 * On error: returns error code (negative)
7547 * On success: returns 0
7548 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007549static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007550{
7551 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007552 if (free_qvectors) {
7553 kfree(vsi->q_vectors);
7554 vsi->q_vectors = NULL;
7555 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007556 kfree(vsi->tx_rings);
7557 vsi->tx_rings = NULL;
7558 vsi->rx_rings = NULL;
7559}
7560
7561/**
Helin Zhang28c58692015-10-26 19:44:27 -04007562 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7563 * and lookup table
7564 * @vsi: Pointer to VSI structure
7565 */
7566static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7567{
7568 if (!vsi)
7569 return;
7570
7571 kfree(vsi->rss_hkey_user);
7572 vsi->rss_hkey_user = NULL;
7573
7574 kfree(vsi->rss_lut_user);
7575 vsi->rss_lut_user = NULL;
7576}
7577
7578/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007579 * i40e_vsi_clear - Deallocate the VSI provided
7580 * @vsi: the VSI being un-configured
7581 **/
7582static int i40e_vsi_clear(struct i40e_vsi *vsi)
7583{
7584 struct i40e_pf *pf;
7585
7586 if (!vsi)
7587 return 0;
7588
7589 if (!vsi->back)
7590 goto free_vsi;
7591 pf = vsi->back;
7592
7593 mutex_lock(&pf->switch_mutex);
7594 if (!pf->vsi[vsi->idx]) {
7595 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7596 vsi->idx, vsi->idx, vsi, vsi->type);
7597 goto unlock_vsi;
7598 }
7599
7600 if (pf->vsi[vsi->idx] != vsi) {
7601 dev_err(&pf->pdev->dev,
7602 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7603 pf->vsi[vsi->idx]->idx,
7604 pf->vsi[vsi->idx],
7605 pf->vsi[vsi->idx]->type,
7606 vsi->idx, vsi, vsi->type);
7607 goto unlock_vsi;
7608 }
7609
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007610 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007611 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7612 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7613
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007614 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007615 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007616
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007617 pf->vsi[vsi->idx] = NULL;
7618 if (vsi->idx < pf->next_vsi)
7619 pf->next_vsi = vsi->idx;
7620
7621unlock_vsi:
7622 mutex_unlock(&pf->switch_mutex);
7623free_vsi:
7624 kfree(vsi);
7625
7626 return 0;
7627}
7628
7629/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007630 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7631 * @vsi: the VSI being cleaned
7632 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007633static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007634{
7635 int i;
7636
Greg Rose8e9dca52013-12-18 13:45:53 +00007637 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007638 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007639 kfree_rcu(vsi->tx_rings[i], rcu);
7640 vsi->tx_rings[i] = NULL;
7641 vsi->rx_rings[i] = NULL;
7642 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007643 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007644}
7645
7646/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007647 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7648 * @vsi: the VSI being configured
7649 **/
7650static int i40e_alloc_rings(struct i40e_vsi *vsi)
7651{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007652 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007653 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007654 int i;
7655
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007656 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007657 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007658 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007659 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7660 if (!tx_ring)
7661 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007662
7663 tx_ring->queue_index = i;
7664 tx_ring->reg_idx = vsi->base_queue + i;
7665 tx_ring->ring_active = false;
7666 tx_ring->vsi = vsi;
7667 tx_ring->netdev = vsi->netdev;
7668 tx_ring->dev = &pf->pdev->dev;
7669 tx_ring->count = vsi->num_desc;
7670 tx_ring->size = 0;
7671 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007672 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7673 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007674 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007675 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007676
Alexander Duyck9f65e152013-09-28 06:00:58 +00007677 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007678 rx_ring->queue_index = i;
7679 rx_ring->reg_idx = vsi->base_queue + i;
7680 rx_ring->ring_active = false;
7681 rx_ring->vsi = vsi;
7682 rx_ring->netdev = vsi->netdev;
7683 rx_ring->dev = &pf->pdev->dev;
7684 rx_ring->count = vsi->num_desc;
7685 rx_ring->size = 0;
7686 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007687 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007688 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007689 }
7690
7691 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007692
7693err_out:
7694 i40e_vsi_clear_rings(vsi);
7695 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007696}
7697
7698/**
7699 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7700 * @pf: board private structure
7701 * @vectors: the number of MSI-X vectors to request
7702 *
7703 * Returns the number of vectors reserved, or error
7704 **/
7705static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7706{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007707 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7708 I40E_MIN_MSIX, vectors);
7709 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007710 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007711 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007712 vectors = 0;
7713 }
7714
7715 return vectors;
7716}
7717
7718/**
7719 * i40e_init_msix - Setup the MSIX capability
7720 * @pf: board private structure
7721 *
7722 * Work with the OS to set up the MSIX vectors needed.
7723 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007724 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007725 **/
7726static int i40e_init_msix(struct i40e_pf *pf)
7727{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007728 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007729 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007730 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007731 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007732 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007733 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007734
7735 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7736 return -ENODEV;
7737
7738 /* The number of vectors we'll request will be comprised of:
7739 * - Add 1 for "other" cause for Admin Queue events, etc.
7740 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007741 * - Queues being used for RSS.
7742 * We don't need as many as max_rss_size vectors.
7743 * use rss_size instead in the calculation since that
7744 * is governed by number of cpus in the system.
7745 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007746 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007747 * - The CPU count within the NUMA node if iWARP is enabled
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007748 * Once we count this up, try the request.
7749 *
7750 * If we can't get what we want, we'll simplify to nearly nothing
7751 * and try again. If that still fails, we punt.
7752 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007753 vectors_left = hw->func_caps.num_msix_vectors;
7754 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007755
Shannon Nelson1e200e42015-02-27 09:15:24 +00007756 /* reserve one vector for miscellaneous handler */
7757 if (vectors_left) {
7758 v_budget++;
7759 vectors_left--;
7760 }
7761
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007762 /* reserve some vectors for the main PF traffic queues. Initially we
7763 * only reserve at most 50% of the available vectors, in the case that
7764 * the number of online CPUs is large. This ensures that we can enable
7765 * extra features as well. Once we've enabled the other features, we
7766 * will use any remaining vectors to reach as close as we can to the
7767 * number of online CPUs.
7768 */
7769 cpus = num_online_cpus();
7770 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00007771 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007772
7773 /* reserve one vector for sideband flow director */
7774 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7775 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007776 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007777 v_budget++;
7778 vectors_left--;
7779 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007780 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007781 }
7782 }
John W Linville83840e42015-01-14 03:06:28 +00007783
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007784 /* can we reserve enough for iWARP? */
7785 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007786 iwarp_requested = pf->num_iwarp_msix;
7787
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007788 if (!vectors_left)
7789 pf->num_iwarp_msix = 0;
7790 else if (vectors_left < pf->num_iwarp_msix)
7791 pf->num_iwarp_msix = 1;
7792 v_budget += pf->num_iwarp_msix;
7793 vectors_left -= pf->num_iwarp_msix;
7794 }
7795
Shannon Nelson1e200e42015-02-27 09:15:24 +00007796 /* any vectors left over go for VMDq support */
7797 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7798 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7799 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7800
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007801 if (!vectors_left) {
7802 pf->num_vmdq_msix = 0;
7803 pf->num_vmdq_qps = 0;
7804 } else {
7805 /* if we're short on vectors for what's desired, we limit
7806 * the queues per vmdq. If this is still more than are
7807 * available, the user will need to change the number of
7808 * queues/vectors used by the PF later with the ethtool
7809 * channels command
7810 */
7811 if (vmdq_vecs < vmdq_vecs_wanted)
7812 pf->num_vmdq_qps = 1;
7813 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007814
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007815 v_budget += vmdq_vecs;
7816 vectors_left -= vmdq_vecs;
7817 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007818 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007819
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007820 /* On systems with a large number of SMP cores, we previously limited
7821 * the number of vectors for num_lan_msix to be at most 50% of the
7822 * available vectors, to allow for other features. Now, we add back
7823 * the remaining vectors. However, we ensure that the total
7824 * num_lan_msix will not exceed num_online_cpus(). To do this, we
7825 * calculate the number of vectors we can add without going over the
7826 * cap of CPUs. For systems with a small number of CPUs this will be
7827 * zero.
7828 */
7829 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
7830 pf->num_lan_msix += extra_vectors;
7831 vectors_left -= extra_vectors;
7832
7833 WARN(vectors_left < 0,
7834 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
7835
7836 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007837 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7838 GFP_KERNEL);
7839 if (!pf->msix_entries)
7840 return -ENOMEM;
7841
7842 for (i = 0; i < v_budget; i++)
7843 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007844 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007845
Shannon Nelson3b444392015-02-26 16:15:57 +00007846 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007847 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7848 kfree(pf->msix_entries);
7849 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007850 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007851 return -ENODEV;
7852
Shannon Nelson3b444392015-02-26 16:15:57 +00007853 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007854 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007855 pf->num_vmdq_vsis = 0;
7856 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007857 pf->num_lan_qps = 1;
7858 pf->num_lan_msix = 1;
7859
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007860 } else if (!vectors_left) {
7861 /* If we have limited resources, we will start with no vectors
7862 * for the special features and then allocate vectors to some
7863 * of these features based on the policy and at the end disable
7864 * the features that did not get any vectors.
7865 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007866 int vec;
7867
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007868 dev_info(&pf->pdev->dev,
7869 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007870 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007871 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007872
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007873 /* Scale vector usage down */
7874 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007875 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007876 pf->num_vmdq_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007877
7878 /* partition out the remaining vectors */
7879 switch (vec) {
7880 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007881 pf->num_lan_msix = 1;
7882 break;
7883 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007884 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7885 pf->num_lan_msix = 1;
7886 pf->num_iwarp_msix = 1;
7887 } else {
7888 pf->num_lan_msix = 2;
7889 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007890 break;
7891 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007892 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7893 pf->num_iwarp_msix = min_t(int, (vec / 3),
7894 iwarp_requested);
7895 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7896 I40E_DEFAULT_NUM_VMDQ_VSI);
7897 } else {
7898 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7899 I40E_DEFAULT_NUM_VMDQ_VSI);
7900 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007901 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7902 pf->num_fdsb_msix = 1;
7903 vec--;
7904 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007905 pf->num_lan_msix = min_t(int,
7906 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7907 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007908 pf->num_lan_qps = pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007909 break;
7910 }
7911 }
7912
Stefan Assmannabd97a92016-09-19 13:37:51 +02007913 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7914 (pf->num_fdsb_msix == 0)) {
7915 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7916 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7917 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007918 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7919 (pf->num_vmdq_msix == 0)) {
7920 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7921 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7922 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007923
7924 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7925 (pf->num_iwarp_msix == 0)) {
7926 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7927 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7928 }
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007929 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7930 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7931 pf->num_lan_msix,
7932 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7933 pf->num_fdsb_msix,
7934 pf->num_iwarp_msix);
7935
Shannon Nelson3b444392015-02-26 16:15:57 +00007936 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007937}
7938
7939/**
Greg Rose90e04072014-03-06 08:59:57 +00007940 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007941 * @vsi: the VSI being configured
7942 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007943 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007944 *
7945 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7946 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007947static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007948{
7949 struct i40e_q_vector *q_vector;
7950
7951 /* allocate q_vector */
7952 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7953 if (!q_vector)
7954 return -ENOMEM;
7955
7956 q_vector->vsi = vsi;
7957 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007958 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7959
Alexander Duyck493fb302013-09-28 07:01:44 +00007960 if (vsi->netdev)
7961 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007962 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007963
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007964 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7965 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7966
Alexander Duyck493fb302013-09-28 07:01:44 +00007967 /* tie q_vector and vsi together */
7968 vsi->q_vectors[v_idx] = q_vector;
7969
7970 return 0;
7971}
7972
7973/**
Greg Rose90e04072014-03-06 08:59:57 +00007974 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007975 * @vsi: the VSI being configured
7976 *
7977 * We allocate one q_vector per queue interrupt. If allocation fails we
7978 * return -ENOMEM.
7979 **/
Greg Rose90e04072014-03-06 08:59:57 +00007980static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007981{
7982 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007983 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007984
7985 /* if not MSIX, give the one vector only to the LAN VSI */
7986 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7987 num_q_vectors = vsi->num_q_vectors;
7988 else if (vsi == pf->vsi[pf->lan_vsi])
7989 num_q_vectors = 1;
7990 else
7991 return -EINVAL;
7992
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007993 current_cpu = cpumask_first(cpu_online_mask);
7994
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007995 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007996 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00007997 if (err)
7998 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007999 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
8000 if (unlikely(current_cpu >= nr_cpu_ids))
8001 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008002 }
8003
8004 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00008005
8006err_out:
8007 while (v_idx--)
8008 i40e_free_q_vector(vsi, v_idx);
8009
8010 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008011}
8012
8013/**
8014 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
8015 * @pf: board private structure to initialize
8016 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008017static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008018{
Shannon Nelson3b444392015-02-26 16:15:57 +00008019 int vectors = 0;
8020 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008021
8022 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00008023 vectors = i40e_init_msix(pf);
8024 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008025 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008026 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008027 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008028 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008029 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008030 I40E_FLAG_SRIOV_ENABLED |
8031 I40E_FLAG_FD_SB_ENABLED |
8032 I40E_FLAG_FD_ATR_ENABLED |
8033 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008034
8035 /* rework the queue expectations without MSIX */
8036 i40e_determine_queue_usage(pf);
8037 }
8038 }
8039
8040 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8041 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008042 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008043 vectors = pci_enable_msi(pf->pdev);
8044 if (vectors < 0) {
8045 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8046 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008047 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8048 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008049 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008050 }
8051
Shannon Nelson958a3e32013-09-28 07:13:28 +00008052 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008053 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008054
Shannon Nelson3b444392015-02-26 16:15:57 +00008055 /* set up vector assignment tracking */
8056 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8057 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008058 if (!pf->irq_pile) {
8059 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8060 return -ENOMEM;
8061 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008062 pf->irq_pile->num_entries = vectors;
8063 pf->irq_pile->search_hint = 0;
8064
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008065 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008066 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008067
8068 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008069}
8070
8071/**
8072 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8073 * @pf: board private structure
8074 *
8075 * This sets up the handler for MSIX 0, which is used to manage the
8076 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8077 * when in MSI or Legacy interrupt mode.
8078 **/
8079static int i40e_setup_misc_vector(struct i40e_pf *pf)
8080{
8081 struct i40e_hw *hw = &pf->hw;
8082 int err = 0;
8083
8084 /* Only request the irq if this is the first time through, and
8085 * not when we're rebuilding after a Reset
8086 */
8087 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
8088 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008089 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008090 if (err) {
8091 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008092 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008093 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008094 return -EFAULT;
8095 }
8096 }
8097
Jacob Kellerab437b52014-12-14 01:55:08 +00008098 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008099
8100 /* associate no queues to the misc vector */
8101 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8102 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8103
8104 i40e_flush(hw);
8105
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008106 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008107
8108 return err;
8109}
8110
8111/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008112 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8113 * @vsi: vsi structure
8114 * @seed: RSS hash seed
8115 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008116static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8117 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008118{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008119 struct i40e_pf *pf = vsi->back;
8120 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008121 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008122
Jacob Keller776b2e12016-07-19 16:23:30 -07008123 if (seed) {
8124 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8125 (struct i40e_aqc_get_set_rss_key_data *)seed;
8126 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8127 if (ret) {
8128 dev_info(&pf->pdev->dev,
8129 "Cannot set RSS key, err %s aq_err %s\n",
8130 i40e_stat_str(hw, ret),
8131 i40e_aq_str(hw, hw->aq.asq_last_status));
8132 return ret;
8133 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008134 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008135 if (lut) {
8136 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008137
Jacob Keller776b2e12016-07-19 16:23:30 -07008138 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8139 if (ret) {
8140 dev_info(&pf->pdev->dev,
8141 "Cannot set RSS lut, err %s aq_err %s\n",
8142 i40e_stat_str(hw, ret),
8143 i40e_aq_str(hw, hw->aq.asq_last_status));
8144 return ret;
8145 }
8146 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008147 return ret;
8148}
8149
8150/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008151 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8152 * @vsi: Pointer to vsi structure
8153 * @seed: Buffter to store the hash keys
8154 * @lut: Buffer to store the lookup table entries
8155 * @lut_size: Size of buffer to store the lookup table entries
8156 *
8157 * Return 0 on success, negative on failure
8158 */
8159static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8160 u8 *lut, u16 lut_size)
8161{
8162 struct i40e_pf *pf = vsi->back;
8163 struct i40e_hw *hw = &pf->hw;
8164 int ret = 0;
8165
8166 if (seed) {
8167 ret = i40e_aq_get_rss_key(hw, vsi->id,
8168 (struct i40e_aqc_get_set_rss_key_data *)seed);
8169 if (ret) {
8170 dev_info(&pf->pdev->dev,
8171 "Cannot get RSS key, err %s aq_err %s\n",
8172 i40e_stat_str(&pf->hw, ret),
8173 i40e_aq_str(&pf->hw,
8174 pf->hw.aq.asq_last_status));
8175 return ret;
8176 }
8177 }
8178
8179 if (lut) {
8180 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8181
8182 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8183 if (ret) {
8184 dev_info(&pf->pdev->dev,
8185 "Cannot get RSS lut, err %s aq_err %s\n",
8186 i40e_stat_str(&pf->hw, ret),
8187 i40e_aq_str(&pf->hw,
8188 pf->hw.aq.asq_last_status));
8189 return ret;
8190 }
8191 }
8192
8193 return ret;
8194}
8195
8196/**
Jacob Keller0582b962016-07-19 16:23:29 -07008197 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8198 * @vsi: VSI structure
8199 **/
8200static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8201{
8202 u8 seed[I40E_HKEY_ARRAY_SIZE];
8203 struct i40e_pf *pf = vsi->back;
8204 u8 *lut;
8205 int ret;
8206
8207 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8208 return 0;
8209
Jacob Keller552b9962016-07-19 16:23:31 -07008210 if (!vsi->rss_size)
8211 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8212 vsi->num_queue_pairs);
8213 if (!vsi->rss_size)
8214 return -EINVAL;
8215
Jacob Keller0582b962016-07-19 16:23:29 -07008216 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8217 if (!lut)
8218 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008219 /* Use the user configured hash keys and lookup table if there is one,
8220 * otherwise use default
8221 */
8222 if (vsi->rss_lut_user)
8223 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8224 else
8225 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8226 if (vsi->rss_hkey_user)
8227 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8228 else
8229 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008230 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8231 kfree(lut);
8232
8233 return ret;
8234}
8235
8236/**
Helin Zhang043dd652015-10-21 19:56:23 -04008237 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008238 * @vsi: Pointer to vsi structure
8239 * @seed: RSS hash seed
8240 * @lut: Lookup table
8241 * @lut_size: Lookup table size
8242 *
8243 * Returns 0 on success, negative on failure
8244 **/
8245static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8246 const u8 *lut, u16 lut_size)
8247{
8248 struct i40e_pf *pf = vsi->back;
8249 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008250 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008251 u8 i;
8252
8253 /* Fill out hash function seed */
8254 if (seed) {
8255 u32 *seed_dw = (u32 *)seed;
8256
Mitch Williamsc4e18682016-04-12 08:30:40 -07008257 if (vsi->type == I40E_VSI_MAIN) {
8258 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008259 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008260 } else if (vsi->type == I40E_VSI_SRIOV) {
8261 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008262 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008263 } else {
8264 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8265 }
Helin Zhange69ff812015-10-21 19:56:22 -04008266 }
8267
8268 if (lut) {
8269 u32 *lut_dw = (u32 *)lut;
8270
Mitch Williamsc4e18682016-04-12 08:30:40 -07008271 if (vsi->type == I40E_VSI_MAIN) {
8272 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8273 return -EINVAL;
8274 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8275 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8276 } else if (vsi->type == I40E_VSI_SRIOV) {
8277 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8278 return -EINVAL;
8279 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008280 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008281 } else {
8282 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8283 }
Helin Zhange69ff812015-10-21 19:56:22 -04008284 }
8285 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008286
8287 return 0;
8288}
8289
8290/**
Helin Zhang043dd652015-10-21 19:56:23 -04008291 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8292 * @vsi: Pointer to VSI structure
8293 * @seed: Buffer to store the keys
8294 * @lut: Buffer to store the lookup table entries
8295 * @lut_size: Size of buffer to store the lookup table entries
8296 *
8297 * Returns 0 on success, negative on failure
8298 */
8299static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8300 u8 *lut, u16 lut_size)
8301{
8302 struct i40e_pf *pf = vsi->back;
8303 struct i40e_hw *hw = &pf->hw;
8304 u16 i;
8305
8306 if (seed) {
8307 u32 *seed_dw = (u32 *)seed;
8308
8309 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008310 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008311 }
8312 if (lut) {
8313 u32 *lut_dw = (u32 *)lut;
8314
8315 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8316 return -EINVAL;
8317 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8318 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8319 }
8320
8321 return 0;
8322}
8323
8324/**
8325 * i40e_config_rss - Configure RSS keys and lut
8326 * @vsi: Pointer to VSI structure
8327 * @seed: RSS hash seed
8328 * @lut: Lookup table
8329 * @lut_size: Lookup table size
8330 *
8331 * Returns 0 on success, negative on failure
8332 */
8333int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8334{
8335 struct i40e_pf *pf = vsi->back;
8336
8337 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8338 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8339 else
8340 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8341}
8342
8343/**
8344 * i40e_get_rss - Get RSS keys and lut
8345 * @vsi: Pointer to VSI structure
8346 * @seed: Buffer to store the keys
8347 * @lut: Buffer to store the lookup table entries
8348 * lut_size: Size of buffer to store the lookup table entries
8349 *
8350 * Returns 0 on success, negative on failure
8351 */
8352int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8353{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008354 struct i40e_pf *pf = vsi->back;
8355
8356 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8357 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8358 else
8359 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008360}
8361
8362/**
Helin Zhange69ff812015-10-21 19:56:22 -04008363 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8364 * @pf: Pointer to board private structure
8365 * @lut: Lookup table
8366 * @rss_table_size: Lookup table size
8367 * @rss_size: Range of queue number for hashing
8368 */
Alan Bradyf1582352016-08-24 11:33:46 -07008369void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8370 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008371{
Helin Zhange69ff812015-10-21 19:56:22 -04008372 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008373
Helin Zhange69ff812015-10-21 19:56:22 -04008374 for (i = 0; i < rss_table_size; i++)
8375 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008376}
8377
8378/**
Helin Zhang043dd652015-10-21 19:56:23 -04008379 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008380 * @pf: board private structure
8381 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008382static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008383{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008384 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008385 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008386 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008387 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008388 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008389 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008390 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008391
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008392 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008393 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8394 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008395 hena |= i40e_pf_get_default_rss_hena(pf);
8396
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008397 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8398 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008399
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008400 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008401 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008402 reg_val = (pf->rss_table_size == 512) ?
8403 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8404 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008405 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008406
Helin Zhang28c58692015-10-26 19:44:27 -04008407 /* Determine the RSS size of the VSI */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008408 if (!vsi->rss_size) {
8409 u16 qcount;
8410
8411 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8412 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
8413 }
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008414 if (!vsi->rss_size)
8415 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008416
Helin Zhange69ff812015-10-21 19:56:22 -04008417 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8418 if (!lut)
8419 return -ENOMEM;
8420
Helin Zhang28c58692015-10-26 19:44:27 -04008421 /* Use user configured lut if there is one, otherwise use default */
8422 if (vsi->rss_lut_user)
8423 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8424 else
8425 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008426
Helin Zhang28c58692015-10-26 19:44:27 -04008427 /* Use user configured hash key if there is one, otherwise
8428 * use default.
8429 */
8430 if (vsi->rss_hkey_user)
8431 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8432 else
8433 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008434 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008435 kfree(lut);
8436
8437 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008438}
8439
8440/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008441 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8442 * @pf: board private structure
8443 * @queue_count: the requested queue count for rss.
8444 *
8445 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8446 * count which may be different from the requested queue count.
Maciej Sosin373149f2017-04-05 07:50:55 -04008447 * Note: expects to be called while under rtnl_lock()
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008448 **/
8449int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8450{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008451 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8452 int new_rss_size;
8453
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008454 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8455 return 0;
8456
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008457 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008458
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008459 if (queue_count != vsi->num_queue_pairs) {
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008460 u16 qcount;
8461
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008462 vsi->req_queue_pairs = queue_count;
Maciej Sosin373149f2017-04-05 07:50:55 -04008463 i40e_prep_for_reset(pf, true);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008464
Helin Zhangacd65442015-10-26 19:44:28 -04008465 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008466
Maciej Sosin373149f2017-04-05 07:50:55 -04008467 i40e_reset_and_rebuild(pf, true, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008468
8469 /* Discard the user configured hash keys and lut, if less
8470 * queues are enabled.
8471 */
8472 if (queue_count < vsi->rss_size) {
8473 i40e_clear_rss_config_user(vsi);
8474 dev_dbg(&pf->pdev->dev,
8475 "discard user configured hash keys and lut\n");
8476 }
8477
8478 /* Reset vsi->rss_size, as number of enabled queues changed */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008479 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8480 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
Helin Zhang28c58692015-10-26 19:44:27 -04008481
Helin Zhang043dd652015-10-21 19:56:23 -04008482 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008483 }
Lihong Yang12815052016-09-27 11:28:48 -07008484 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8485 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008486 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008487}
8488
8489/**
Greg Rosef4492db2015-02-06 08:52:12 +00008490 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8491 * @pf: board private structure
8492 **/
8493i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8494{
8495 i40e_status status;
8496 bool min_valid, max_valid;
8497 u32 max_bw, min_bw;
8498
8499 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8500 &min_valid, &max_valid);
8501
8502 if (!status) {
8503 if (min_valid)
8504 pf->npar_min_bw = min_bw;
8505 if (max_valid)
8506 pf->npar_max_bw = max_bw;
8507 }
8508
8509 return status;
8510}
8511
8512/**
8513 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8514 * @pf: board private structure
8515 **/
8516i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8517{
8518 struct i40e_aqc_configure_partition_bw_data bw_data;
8519 i40e_status status;
8520
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008521 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008522 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008523 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8524 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8525
8526 /* Set the new bandwidths */
8527 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8528
8529 return status;
8530}
8531
8532/**
8533 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8534 * @pf: board private structure
8535 **/
8536i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8537{
8538 /* Commit temporary BW setting to permanent NVM image */
8539 enum i40e_admin_queue_err last_aq_status;
8540 i40e_status ret;
8541 u16 nvm_word;
8542
8543 if (pf->hw.partition_id != 1) {
8544 dev_info(&pf->pdev->dev,
8545 "Commit BW only works on partition 1! This is partition %d",
8546 pf->hw.partition_id);
8547 ret = I40E_NOT_SUPPORTED;
8548 goto bw_commit_out;
8549 }
8550
8551 /* Acquire NVM for read access */
8552 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8553 last_aq_status = pf->hw.aq.asq_last_status;
8554 if (ret) {
8555 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008556 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8557 i40e_stat_str(&pf->hw, ret),
8558 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008559 goto bw_commit_out;
8560 }
8561
8562 /* Read word 0x10 of NVM - SW compatibility word 1 */
8563 ret = i40e_aq_read_nvm(&pf->hw,
8564 I40E_SR_NVM_CONTROL_WORD,
8565 0x10, sizeof(nvm_word), &nvm_word,
8566 false, NULL);
8567 /* Save off last admin queue command status before releasing
8568 * the NVM
8569 */
8570 last_aq_status = pf->hw.aq.asq_last_status;
8571 i40e_release_nvm(&pf->hw);
8572 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008573 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8574 i40e_stat_str(&pf->hw, ret),
8575 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008576 goto bw_commit_out;
8577 }
8578
8579 /* Wait a bit for NVM release to complete */
8580 msleep(50);
8581
8582 /* Acquire NVM for write access */
8583 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8584 last_aq_status = pf->hw.aq.asq_last_status;
8585 if (ret) {
8586 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008587 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8588 i40e_stat_str(&pf->hw, ret),
8589 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008590 goto bw_commit_out;
8591 }
8592 /* Write it back out unchanged to initiate update NVM,
8593 * which will force a write of the shadow (alt) RAM to
8594 * the NVM - thus storing the bandwidth values permanently.
8595 */
8596 ret = i40e_aq_update_nvm(&pf->hw,
8597 I40E_SR_NVM_CONTROL_WORD,
8598 0x10, sizeof(nvm_word),
8599 &nvm_word, true, NULL);
8600 /* Save off last admin queue command status before releasing
8601 * the NVM
8602 */
8603 last_aq_status = pf->hw.aq.asq_last_status;
8604 i40e_release_nvm(&pf->hw);
8605 if (ret)
8606 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008607 "BW settings NOT SAVED, err %s aq_err %s\n",
8608 i40e_stat_str(&pf->hw, ret),
8609 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008610bw_commit_out:
8611
8612 return ret;
8613}
8614
8615/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008616 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8617 * @pf: board private structure to initialize
8618 *
8619 * i40e_sw_init initializes the Adapter private data structure.
8620 * Fields are initialized based on PCI device information and
8621 * OS network device settings (MTU size).
8622 **/
8623static int i40e_sw_init(struct i40e_pf *pf)
8624{
8625 int err = 0;
8626 int size;
8627
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008628 /* Set default capability flags */
8629 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8630 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008631 I40E_FLAG_MSIX_ENABLED;
8632
Mitch Williamsca99eb92014-04-04 04:43:07 +00008633 /* Set default ITR */
8634 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8635 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8636
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008637 /* Depending on PF configurations, it is possible that the RSS
8638 * maximum might end up larger than the available queues
8639 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008640 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008641 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008642 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008643 pf->rss_size_max = min_t(int, pf->rss_size_max,
8644 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008645 if (pf->hw.func_caps.rss) {
8646 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008647 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8648 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008649 }
8650
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008651 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008652 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008653 pf->flags |= I40E_FLAG_MFP_ENABLED;
8654 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008655 if (i40e_get_npar_bw_setting(pf))
8656 dev_warn(&pf->pdev->dev,
8657 "Could not get NPAR bw settings\n");
8658 else
8659 dev_info(&pf->pdev->dev,
8660 "Min BW = %8.8x, Max BW = %8.8x\n",
8661 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008662 }
8663
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008664 /* FW/NVM is not yet fixed in this regard */
8665 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8666 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8667 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8668 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008669 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8670 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008671 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008672 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008673 else
8674 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008675 pf->fdir_pf_filter_count =
8676 pf->hw.func_caps.fd_filters_guaranteed;
8677 pf->hw.fdir_shared_filter_count =
8678 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008679 }
8680
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008681 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008682 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008683 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008684 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008685 /* No DCB support for FW < v4.33 */
8686 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8687 }
8688
8689 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008690 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008691 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8692 (pf->hw.aq.fw_maj_ver < 4)))
8693 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8694
8695 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008696 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008697 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8698 (pf->hw.aq.fw_maj_ver >= 5)))
8699 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008700
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008701 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008702 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008703 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008704 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008705 }
8706
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008707 if (pf->hw.func_caps.iwarp) {
8708 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8709 /* IWARP needs one extra vector for CQP just like MISC.*/
8710 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8711 }
8712
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008713#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008714 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008715 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8716 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8717 pf->num_req_vfs = min_t(int,
8718 pf->hw.func_caps.num_vfs,
8719 I40E_MAX_VF_COUNT);
8720 }
8721#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008722 if (pf->hw.mac.type == I40E_MAC_X722) {
Joshua Hay1d680052016-12-12 15:44:08 -08008723 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
8724 | I40E_FLAG_128_QP_RSS_CAPABLE
8725 | I40E_FLAG_HW_ATR_EVICT_CAPABLE
8726 | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
8727 | I40E_FLAG_WB_ON_ITR_CAPABLE
8728 | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
8729 | I40E_FLAG_NO_PCI_LINK_CHECK
8730 | I40E_FLAG_USE_SET_LLDP_MIB
8731 | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
8732 | I40E_FLAG_PTP_L4_CAPABLE
8733 | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008734 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8735 ((pf->hw.aq.api_maj_ver == 1) &&
8736 (pf->hw.aq.api_min_ver > 4))) {
8737 /* Supported in FW API version higher than 1.4 */
8738 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008739 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008740 } else {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008741 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008742 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008743
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008744 pf->eeprom_version = 0xDEAD;
8745 pf->lan_veb = I40E_NO_VEB;
8746 pf->lan_vsi = I40E_NO_VSI;
8747
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008748 /* By default FW has this off for performance reasons */
8749 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8750
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008751 /* set up queue assignment tracking */
8752 size = sizeof(struct i40e_lump_tracking)
8753 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8754 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8755 if (!pf->qp_pile) {
8756 err = -ENOMEM;
8757 goto sw_init_done;
8758 }
8759 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8760 pf->qp_pile->search_hint = 0;
8761
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008762 pf->tx_timeout_recovery_level = 1;
8763
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008764 mutex_init(&pf->switch_mutex);
8765
Greg Rosec668a122015-02-26 16:10:39 +00008766 /* If NPAR is enabled nudge the Tx scheduler */
8767 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8768 i40e_set_npar_bw_setting(pf);
8769
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008770sw_init_done:
8771 return err;
8772}
8773
8774/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008775 * i40e_set_ntuple - set the ntuple feature flag and take action
8776 * @pf: board private structure to initialize
8777 * @features: the feature set that the stack is suggesting
8778 *
8779 * returns a bool to indicate if reset needs to happen
8780 **/
8781bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8782{
8783 bool need_reset = false;
8784
8785 /* Check if Flow Director n-tuple support was enabled or disabled. If
8786 * the state changed, we need to reset.
8787 */
8788 if (features & NETIF_F_NTUPLE) {
8789 /* Enable filters and mark for reset */
8790 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8791 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008792 /* enable FD_SB only if there is MSI-X vector */
8793 if (pf->num_fdsb_msix > 0)
8794 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008795 } else {
8796 /* turn off filters, mark for reset and clear SW filter list */
8797 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8798 need_reset = true;
8799 i40e_fdir_filter_exit(pf);
8800 }
8801 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008802 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008803 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08008804 pf->fd_add_err = 0;
8805 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008806 /* if ATR was auto disabled it can be re-enabled. */
8807 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008808 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED)) {
8809 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008810 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8811 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8812 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008813 }
8814 return need_reset;
8815}
8816
8817/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008818 * i40e_clear_rss_lut - clear the rx hash lookup table
8819 * @vsi: the VSI being configured
8820 **/
8821static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8822{
8823 struct i40e_pf *pf = vsi->back;
8824 struct i40e_hw *hw = &pf->hw;
8825 u16 vf_id = vsi->vf_id;
8826 u8 i;
8827
8828 if (vsi->type == I40E_VSI_MAIN) {
8829 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8830 wr32(hw, I40E_PFQF_HLUT(i), 0);
8831 } else if (vsi->type == I40E_VSI_SRIOV) {
8832 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8833 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8834 } else {
8835 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8836 }
8837}
8838
8839/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008840 * i40e_set_features - set the netdev feature flags
8841 * @netdev: ptr to the netdev being adjusted
8842 * @features: the feature set that the stack is suggesting
Maciej Sosin373149f2017-04-05 07:50:55 -04008843 * Note: expects to be called while under rtnl_lock()
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008844 **/
8845static int i40e_set_features(struct net_device *netdev,
8846 netdev_features_t features)
8847{
8848 struct i40e_netdev_priv *np = netdev_priv(netdev);
8849 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008850 struct i40e_pf *pf = vsi->back;
8851 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008852
Alan Bradyd8ec9862016-07-27 12:02:38 -07008853 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8854 i40e_pf_config_rss(pf);
8855 else if (!(features & NETIF_F_RXHASH) &&
8856 netdev->features & NETIF_F_RXHASH)
8857 i40e_clear_rss_lut(vsi);
8858
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008859 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8860 i40e_vlan_stripping_enable(vsi);
8861 else
8862 i40e_vlan_stripping_disable(vsi);
8863
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008864 need_reset = i40e_set_ntuple(pf, features);
8865
8866 if (need_reset)
Maciej Sosin373149f2017-04-05 07:50:55 -04008867 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008868
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008869 return 0;
8870}
8871
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008872/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008873 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008874 * @pf: board private structure
8875 * @port: The UDP port to look up
8876 *
8877 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8878 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008879static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008880{
8881 u8 i;
8882
8883 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008884 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008885 return i;
8886 }
8887
8888 return i;
8889}
8890
8891/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008892 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008893 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008894 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008895 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008896static void i40e_udp_tunnel_add(struct net_device *netdev,
8897 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008898{
8899 struct i40e_netdev_priv *np = netdev_priv(netdev);
8900 struct i40e_vsi *vsi = np->vsi;
8901 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008902 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008903 u8 next_idx;
8904 u8 idx;
8905
Singhai, Anjali6a899022015-12-14 12:21:18 -08008906 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008907
8908 /* Check if port already exists */
8909 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008910 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008911 return;
8912 }
8913
8914 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008915 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008916
8917 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008918 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008919 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008920 return;
8921 }
8922
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008923 switch (ti->type) {
8924 case UDP_TUNNEL_TYPE_VXLAN:
8925 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8926 break;
8927 case UDP_TUNNEL_TYPE_GENEVE:
8928 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8929 return;
8930 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8931 break;
8932 default:
8933 return;
8934 }
8935
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008936 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008937 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008938 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8939 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008940}
8941
8942/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008943 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008944 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008945 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008946 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008947static void i40e_udp_tunnel_del(struct net_device *netdev,
8948 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008949{
8950 struct i40e_netdev_priv *np = netdev_priv(netdev);
8951 struct i40e_vsi *vsi = np->vsi;
8952 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008953 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008954 u8 idx;
8955
Singhai, Anjali6a899022015-12-14 12:21:18 -08008956 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008957
8958 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008959 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8960 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008961
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008962 switch (ti->type) {
8963 case UDP_TUNNEL_TYPE_VXLAN:
8964 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8965 goto not_found;
8966 break;
8967 case UDP_TUNNEL_TYPE_GENEVE:
8968 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8969 goto not_found;
8970 break;
8971 default:
8972 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008973 }
8974
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008975 /* if port exists, set it to 0 (mark for deletion)
8976 * and make it pending
8977 */
8978 pf->udp_ports[idx].index = 0;
8979 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008980 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8981
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008982 return;
8983not_found:
8984 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008985 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008986}
8987
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008988static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008989 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008990{
8991 struct i40e_netdev_priv *np = netdev_priv(netdev);
8992 struct i40e_pf *pf = np->vsi->back;
8993 struct i40e_hw *hw = &pf->hw;
8994
8995 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8996 return -EOPNOTSUPP;
8997
8998 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8999 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
9000
9001 return 0;
9002}
9003
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08009004/**
9005 * i40e_ndo_fdb_add - add an entry to the hardware database
9006 * @ndm: the input from the stack
9007 * @tb: pointer to array of nladdr (unused)
9008 * @dev: the net device pointer
9009 * @addr: the MAC address entry being added
9010 * @flags: instructions from stack about fdb operation
9011 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00009012static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
9013 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01009014 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009015 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00009016{
9017 struct i40e_netdev_priv *np = netdev_priv(dev);
9018 struct i40e_pf *pf = np->vsi->back;
9019 int err = 0;
9020
9021 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
9022 return -EOPNOTSUPP;
9023
Or Gerlitz65891fe2014-12-14 18:19:05 +02009024 if (vid) {
9025 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9026 return -EINVAL;
9027 }
9028
Greg Rose4ba0dea2014-03-06 08:59:55 +00009029 /* Hardware does not support aging addresses so if a
9030 * ndm_state is given only allow permanent addresses
9031 */
9032 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9033 netdev_info(dev, "FDB only supports static addresses\n");
9034 return -EINVAL;
9035 }
9036
9037 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9038 err = dev_uc_add_excl(dev, addr);
9039 else if (is_multicast_ether_addr(addr))
9040 err = dev_mc_add_excl(dev, addr);
9041 else
9042 err = -EINVAL;
9043
9044 /* Only return duplicate errors if NLM_F_EXCL is set */
9045 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9046 err = 0;
9047
9048 return err;
9049}
9050
Neerav Parikh51616012015-02-06 08:52:14 +00009051/**
9052 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9053 * @dev: the netdev being configured
9054 * @nlh: RTNL message
9055 *
9056 * Inserts a new hardware bridge if not already created and
9057 * enables the bridging mode requested (VEB or VEPA). If the
9058 * hardware bridge has already been inserted and the request
9059 * is to change the mode then that requires a PF reset to
9060 * allow rebuild of the components with required hardware
9061 * bridge mode enabled.
Maciej Sosin373149f2017-04-05 07:50:55 -04009062 *
9063 * Note: expects to be called while under rtnl_lock()
Neerav Parikh51616012015-02-06 08:52:14 +00009064 **/
9065static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009066 struct nlmsghdr *nlh,
9067 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009068{
9069 struct i40e_netdev_priv *np = netdev_priv(dev);
9070 struct i40e_vsi *vsi = np->vsi;
9071 struct i40e_pf *pf = vsi->back;
9072 struct i40e_veb *veb = NULL;
9073 struct nlattr *attr, *br_spec;
9074 int i, rem;
9075
9076 /* Only for PF VSI for now */
9077 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9078 return -EOPNOTSUPP;
9079
9080 /* Find the HW bridge for PF VSI */
9081 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9082 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9083 veb = pf->veb[i];
9084 }
9085
9086 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9087
9088 nla_for_each_nested(attr, br_spec, rem) {
9089 __u16 mode;
9090
9091 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9092 continue;
9093
9094 mode = nla_get_u16(attr);
9095 if ((mode != BRIDGE_MODE_VEPA) &&
9096 (mode != BRIDGE_MODE_VEB))
9097 return -EINVAL;
9098
9099 /* Insert a new HW bridge */
9100 if (!veb) {
9101 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9102 vsi->tc_config.enabled_tc);
9103 if (veb) {
9104 veb->bridge_mode = mode;
9105 i40e_config_bridge_mode(veb);
9106 } else {
9107 /* No Bridge HW offload available */
9108 return -ENOENT;
9109 }
9110 break;
9111 } else if (mode != veb->bridge_mode) {
9112 /* Existing HW bridge but different mode needs reset */
9113 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009114 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9115 if (mode == BRIDGE_MODE_VEB)
9116 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9117 else
9118 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
Maciej Sosin373149f2017-04-05 07:50:55 -04009119 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED),
9120 true);
Neerav Parikh51616012015-02-06 08:52:14 +00009121 break;
9122 }
9123 }
9124
9125 return 0;
9126}
9127
9128/**
9129 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9130 * @skb: skb buff
9131 * @pid: process id
9132 * @seq: RTNL message seq #
9133 * @dev: the netdev being configured
9134 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009135 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009136 *
9137 * Return the mode in which the hardware bridge is operating in
9138 * i.e VEB or VEPA.
9139 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009140static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9141 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009142 u32 __always_unused filter_mask,
9143 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009144{
9145 struct i40e_netdev_priv *np = netdev_priv(dev);
9146 struct i40e_vsi *vsi = np->vsi;
9147 struct i40e_pf *pf = vsi->back;
9148 struct i40e_veb *veb = NULL;
9149 int i;
9150
9151 /* Only for PF VSI for now */
9152 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9153 return -EOPNOTSUPP;
9154
9155 /* Find the HW bridge for the PF VSI */
9156 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9157 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9158 veb = pf->veb[i];
9159 }
9160
9161 if (!veb)
9162 return 0;
9163
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009164 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009165 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009166}
Neerav Parikh51616012015-02-06 08:52:14 +00009167
Joe Stringerf44a75e2015-04-14 17:09:14 -07009168/**
9169 * i40e_features_check - Validate encapsulated packet conforms to limits
9170 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009171 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009172 * @features: Offload features that the stack believes apply
9173 **/
9174static netdev_features_t i40e_features_check(struct sk_buff *skb,
9175 struct net_device *dev,
9176 netdev_features_t features)
9177{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009178 size_t len;
9179
9180 /* No point in doing any of this if neither checksum nor GSO are
9181 * being requested for this frame. We can rule out both by just
9182 * checking for CHECKSUM_PARTIAL
9183 */
9184 if (skb->ip_summed != CHECKSUM_PARTIAL)
9185 return features;
9186
9187 /* We cannot support GSO if the MSS is going to be less than
9188 * 64 bytes. If it is then we need to drop support for GSO.
9189 */
9190 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9191 features &= ~NETIF_F_GSO_MASK;
9192
9193 /* MACLEN can support at most 63 words */
9194 len = skb_network_header(skb) - skb->data;
9195 if (len & ~(63 * 2))
9196 goto out_err;
9197
9198 /* IPLEN and EIPLEN can support at most 127 dwords */
9199 len = skb_transport_header(skb) - skb_network_header(skb);
9200 if (len & ~(127 * 4))
9201 goto out_err;
9202
9203 if (skb->encapsulation) {
9204 /* L4TUNLEN can support 127 words */
9205 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9206 if (len & ~(127 * 2))
9207 goto out_err;
9208
9209 /* IPLEN can support at most 127 dwords */
9210 len = skb_inner_transport_header(skb) -
9211 skb_inner_network_header(skb);
9212 if (len & ~(127 * 4))
9213 goto out_err;
9214 }
9215
9216 /* No need to validate L4LEN as TCP is the only protocol with a
9217 * a flexible value and we support all possible values supported
9218 * by TCP, which is at most 15 dwords
9219 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009220
9221 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009222out_err:
9223 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009224}
9225
Shannon Nelson37a29732015-02-27 09:15:19 +00009226static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009227 .ndo_open = i40e_open,
9228 .ndo_stop = i40e_close,
9229 .ndo_start_xmit = i40e_lan_xmit_frame,
9230 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9231 .ndo_set_rx_mode = i40e_set_rx_mode,
9232 .ndo_validate_addr = eth_validate_addr,
9233 .ndo_set_mac_address = i40e_set_mac,
9234 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009235 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009236 .ndo_tx_timeout = i40e_tx_timeout,
9237 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9238 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9239#ifdef CONFIG_NET_POLL_CONTROLLER
9240 .ndo_poll_controller = i40e_netpoll,
9241#endif
John Fastabende4c67342016-02-16 21:16:15 -08009242 .ndo_setup_tc = __i40e_setup_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009243 .ndo_set_features = i40e_set_features,
9244 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9245 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009246 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009247 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009248 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009249 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009250 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009251 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9252 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009253 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009254 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009255 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009256 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9257 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009258};
9259
9260/**
9261 * i40e_config_netdev - Setup the netdev flags
9262 * @vsi: the VSI being configured
9263 *
9264 * Returns 0 on success, negative value on failure
9265 **/
9266static int i40e_config_netdev(struct i40e_vsi *vsi)
9267{
9268 struct i40e_pf *pf = vsi->back;
9269 struct i40e_hw *hw = &pf->hw;
9270 struct i40e_netdev_priv *np;
9271 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009272 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009273 u8 mac_addr[ETH_ALEN];
9274 int etherdev_size;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009275 netdev_features_t hw_enc_features;
9276 netdev_features_t hw_features;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009277
9278 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009279 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009280 if (!netdev)
9281 return -ENOMEM;
9282
9283 vsi->netdev = netdev;
9284 np = netdev_priv(netdev);
9285 np->vsi = vsi;
9286
Preethi Banalabacd75c2017-03-27 14:43:18 -07009287 hw_enc_features = NETIF_F_SG |
9288 NETIF_F_IP_CSUM |
9289 NETIF_F_IPV6_CSUM |
9290 NETIF_F_HIGHDMA |
9291 NETIF_F_SOFT_FEATURES |
9292 NETIF_F_TSO |
9293 NETIF_F_TSO_ECN |
9294 NETIF_F_TSO6 |
9295 NETIF_F_GSO_GRE |
9296 NETIF_F_GSO_GRE_CSUM |
9297 NETIF_F_GSO_PARTIAL |
9298 NETIF_F_GSO_UDP_TUNNEL |
9299 NETIF_F_GSO_UDP_TUNNEL_CSUM |
9300 NETIF_F_SCTP_CRC |
9301 NETIF_F_RXHASH |
9302 NETIF_F_RXCSUM |
9303 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009304
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009305 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009306 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9307
9308 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009309
Preethi Banalabacd75c2017-03-27 14:43:18 -07009310 netdev->hw_enc_features |= hw_enc_features;
9311
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009312 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07009313 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009314
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009315 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009316 netdev->hw_features |= NETIF_F_NTUPLE;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009317 hw_features = hw_enc_features |
9318 NETIF_F_HW_VLAN_CTAG_TX |
9319 NETIF_F_HW_VLAN_CTAG_RX;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009320
Preethi Banalabacd75c2017-03-27 14:43:18 -07009321 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009322
Preethi Banalabacd75c2017-03-27 14:43:18 -07009323 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009324 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009325
9326 if (vsi->type == I40E_VSI_MAIN) {
9327 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009328 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Filip Sadowski3954b392017-01-30 12:29:34 -08009329 /* The following steps are necessary to properly keep track of
9330 * MAC-VLAN filters loaded into firmware - first we remove
9331 * filter that is automatically generated by firmware and then
9332 * add new filter both to the driver hash table and firmware.
Jacob Keller1596b5d2016-11-08 13:05:15 -08009333 */
9334 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009335 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009336 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009337 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009338 } else {
9339 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9340 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9341 pf->vsi[pf->lan_vsi]->netdev->name);
9342 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009343
Jacob Keller278e7d02016-10-05 09:30:37 -07009344 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009345 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009346 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009347 }
Kiran Patil21659032015-09-30 14:09:03 -04009348
Jacob Keller435c0842016-11-08 13:05:10 -08009349 /* Add the broadcast filter so that we initially will receive
9350 * broadcast packets. Note that when a new VLAN is first added the
9351 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9352 * specific filters as part of transitioning into "vlan" operation.
9353 * When more VLANs are added, the driver will copy each existing MAC
9354 * filter and add it for the new VLAN.
9355 *
9356 * Broadcast filters are handled specially by
9357 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9358 * promiscuous bit instead of adding this directly as a MAC/VLAN
9359 * filter. The subtask will update the correct broadcast promiscuous
9360 * bits as VLANs become active or inactive.
9361 */
9362 eth_broadcast_addr(broadcast);
9363 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009364 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -08009365 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9366
Greg Rose9a173902014-05-22 06:32:02 +00009367 ether_addr_copy(netdev->dev_addr, mac_addr);
9368 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009369
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009370 netdev->priv_flags |= IFF_UNICAST_FLT;
9371 netdev->priv_flags |= IFF_SUPP_NOFCS;
9372 /* Setup netdev TC information */
9373 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9374
9375 netdev->netdev_ops = &i40e_netdev_ops;
9376 netdev->watchdog_timeo = 5 * HZ;
9377 i40e_set_ethtool_ops(netdev);
9378
Jarod Wilson91c527a2016-10-17 15:54:05 -04009379 /* MTU range: 68 - 9706 */
9380 netdev->min_mtu = ETH_MIN_MTU;
9381 netdev->max_mtu = I40E_MAX_RXBUFFER -
9382 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9383
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009384 return 0;
9385}
9386
9387/**
9388 * i40e_vsi_delete - Delete a VSI from the switch
9389 * @vsi: the VSI being removed
9390 *
9391 * Returns 0 on success, negative value on failure
9392 **/
9393static void i40e_vsi_delete(struct i40e_vsi *vsi)
9394{
9395 /* remove default VSI is not allowed */
9396 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9397 return;
9398
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009399 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009400}
9401
9402/**
Neerav Parikh51616012015-02-06 08:52:14 +00009403 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9404 * @vsi: the VSI being queried
9405 *
9406 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9407 **/
9408int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9409{
9410 struct i40e_veb *veb;
9411 struct i40e_pf *pf = vsi->back;
9412
9413 /* Uplink is not a bridge so default to VEB */
9414 if (vsi->veb_idx == I40E_NO_VEB)
9415 return 1;
9416
9417 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009418 if (!veb) {
9419 dev_info(&pf->pdev->dev,
9420 "There is no veb associated with the bridge\n");
9421 return -ENOENT;
9422 }
Neerav Parikh51616012015-02-06 08:52:14 +00009423
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009424 /* Uplink is a bridge in VEPA mode */
9425 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9426 return 0;
9427 } else {
9428 /* Uplink is a bridge in VEB mode */
9429 return 1;
9430 }
9431
9432 /* VEPA is now default bridge, so return 0 */
9433 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009434}
9435
9436/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009437 * i40e_add_vsi - Add a VSI to the switch
9438 * @vsi: the VSI being configured
9439 *
9440 * This initializes a VSI context depending on the VSI type to be added and
9441 * passes it down to the add_vsi aq command.
9442 **/
9443static int i40e_add_vsi(struct i40e_vsi *vsi)
9444{
9445 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009446 struct i40e_pf *pf = vsi->back;
9447 struct i40e_hw *hw = &pf->hw;
9448 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009449 struct i40e_mac_filter *f;
9450 struct hlist_node *h;
9451 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009452
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009453 u8 enabled_tc = 0x1; /* TC0 enabled */
9454 int f_count = 0;
9455
9456 memset(&ctxt, 0, sizeof(ctxt));
9457 switch (vsi->type) {
9458 case I40E_VSI_MAIN:
9459 /* The PF's main VSI is already setup as part of the
9460 * device initialization, so we'll not bother with
9461 * the add_vsi call, but we will retrieve the current
9462 * VSI context.
9463 */
9464 ctxt.seid = pf->main_vsi_seid;
9465 ctxt.pf_num = pf->hw.pf_id;
9466 ctxt.vf_num = 0;
9467 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9468 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9469 if (ret) {
9470 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009471 "couldn't get PF vsi config, err %s aq_err %s\n",
9472 i40e_stat_str(&pf->hw, ret),
9473 i40e_aq_str(&pf->hw,
9474 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009475 return -ENOENT;
9476 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009477 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009478 vsi->info.valid_sections = 0;
9479
9480 vsi->seid = ctxt.seid;
9481 vsi->id = ctxt.vsi_number;
9482
9483 enabled_tc = i40e_pf_get_tc_map(pf);
9484
9485 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009486 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9487 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009488 memset(&ctxt, 0, sizeof(ctxt));
9489 ctxt.seid = pf->main_vsi_seid;
9490 ctxt.pf_num = pf->hw.pf_id;
9491 ctxt.vf_num = 0;
9492 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9493 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9494 if (ret) {
9495 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009496 "update vsi failed, err %s aq_err %s\n",
9497 i40e_stat_str(&pf->hw, ret),
9498 i40e_aq_str(&pf->hw,
9499 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009500 ret = -ENOENT;
9501 goto err;
9502 }
9503 /* update the local VSI info queue map */
9504 i40e_vsi_update_queue_map(vsi, &ctxt);
9505 vsi->info.valid_sections = 0;
9506 } else {
9507 /* Default/Main VSI is only enabled for TC0
9508 * reconfigure it to enable all TCs that are
9509 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009510 * For MFP case the iSCSI PF would use this
9511 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009512 */
9513 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9514 if (ret) {
9515 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009516 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9517 enabled_tc,
9518 i40e_stat_str(&pf->hw, ret),
9519 i40e_aq_str(&pf->hw,
9520 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009521 ret = -ENOENT;
9522 }
9523 }
9524 break;
9525
9526 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009527 ctxt.pf_num = hw->pf_id;
9528 ctxt.vf_num = 0;
9529 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009530 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009531 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009532 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9533 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009534 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009535 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009536 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009537 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009538 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009539 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009540 break;
9541
9542 case I40E_VSI_VMDQ2:
9543 ctxt.pf_num = hw->pf_id;
9544 ctxt.vf_num = 0;
9545 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009546 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009547 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9548
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009549 /* This VSI is connected to VEB so the switch_id
9550 * should be set to zero by default.
9551 */
Neerav Parikh51616012015-02-06 08:52:14 +00009552 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9553 ctxt.info.valid_sections |=
9554 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9555 ctxt.info.switch_id =
9556 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9557 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009558
9559 /* Setup the VSI tx/rx queue map for TC0 only for now */
9560 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9561 break;
9562
9563 case I40E_VSI_SRIOV:
9564 ctxt.pf_num = hw->pf_id;
9565 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9566 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009567 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009568 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9569
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009570 /* This VSI is connected to VEB so the switch_id
9571 * should be set to zero by default.
9572 */
Neerav Parikh51616012015-02-06 08:52:14 +00009573 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9574 ctxt.info.valid_sections |=
9575 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9576 ctxt.info.switch_id =
9577 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9578 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009579
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009580 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9581 ctxt.info.valid_sections |=
9582 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9583 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009584 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9585 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009586 }
9587
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009588 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9589 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009590 if (pf->vf[vsi->vf_id].spoofchk) {
9591 ctxt.info.valid_sections |=
9592 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9593 ctxt.info.sec_flags |=
9594 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9595 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9596 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009597 /* Setup the VSI tx/rx queue map for TC0 only for now */
9598 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9599 break;
9600
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009601 case I40E_VSI_IWARP:
9602 /* send down message to iWARP */
9603 break;
9604
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009605 default:
9606 return -ENODEV;
9607 }
9608
9609 if (vsi->type != I40E_VSI_MAIN) {
9610 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9611 if (ret) {
9612 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009613 "add vsi failed, err %s aq_err %s\n",
9614 i40e_stat_str(&pf->hw, ret),
9615 i40e_aq_str(&pf->hw,
9616 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009617 ret = -ENOENT;
9618 goto err;
9619 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009620 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009621 vsi->info.valid_sections = 0;
9622 vsi->seid = ctxt.seid;
9623 vsi->id = ctxt.vsi_number;
9624 }
9625
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009626 vsi->active_filters = 0;
9627 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009628 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009629 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009630 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009631 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009632 f_count++;
9633 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009634 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009635
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009636 if (f_count) {
9637 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9638 pf->flags |= I40E_FLAG_FILTER_SYNC;
9639 }
9640
9641 /* Update VSI BW information */
9642 ret = i40e_vsi_get_bw_info(vsi);
9643 if (ret) {
9644 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009645 "couldn't get vsi bw info, err %s aq_err %s\n",
9646 i40e_stat_str(&pf->hw, ret),
9647 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009648 /* VSI is already added so not tearing that up */
9649 ret = 0;
9650 }
9651
9652err:
9653 return ret;
9654}
9655
9656/**
9657 * i40e_vsi_release - Delete a VSI and free its resources
9658 * @vsi: the VSI being removed
9659 *
9660 * Returns 0 on success or < 0 on error
9661 **/
9662int i40e_vsi_release(struct i40e_vsi *vsi)
9663{
Jacob Keller278e7d02016-10-05 09:30:37 -07009664 struct i40e_mac_filter *f;
9665 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009666 struct i40e_veb *veb = NULL;
9667 struct i40e_pf *pf;
9668 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009669 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009670
9671 pf = vsi->back;
9672
9673 /* release of a VEB-owner or last VSI is not allowed */
9674 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9675 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9676 vsi->seid, vsi->uplink_seid);
9677 return -ENODEV;
9678 }
9679 if (vsi == pf->vsi[pf->lan_vsi] &&
9680 !test_bit(__I40E_DOWN, &pf->state)) {
9681 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9682 return -ENODEV;
9683 }
9684
9685 uplink_seid = vsi->uplink_seid;
9686 if (vsi->type != I40E_VSI_SRIOV) {
9687 if (vsi->netdev_registered) {
9688 vsi->netdev_registered = false;
9689 if (vsi->netdev) {
9690 /* results in a call to i40e_close() */
9691 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009692 }
9693 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009694 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009695 }
9696 i40e_vsi_disable_irq(vsi);
9697 }
9698
Jacob Keller278e7d02016-10-05 09:30:37 -07009699 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009700
9701 /* clear the sync flag on all filters */
9702 if (vsi->netdev) {
9703 __dev_uc_unsync(vsi->netdev, NULL);
9704 __dev_mc_unsync(vsi->netdev, NULL);
9705 }
9706
9707 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009708 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009709 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009710
Jacob Keller278e7d02016-10-05 09:30:37 -07009711 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009712
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009713 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009714
9715 i40e_vsi_delete(vsi);
9716 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009717 if (vsi->netdev) {
9718 free_netdev(vsi->netdev);
9719 vsi->netdev = NULL;
9720 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009721 i40e_vsi_clear_rings(vsi);
9722 i40e_vsi_clear(vsi);
9723
9724 /* If this was the last thing on the VEB, except for the
9725 * controlling VSI, remove the VEB, which puts the controlling
9726 * VSI onto the next level down in the switch.
9727 *
9728 * Well, okay, there's one more exception here: don't remove
9729 * the orphan VEBs yet. We'll wait for an explicit remove request
9730 * from up the network stack.
9731 */
Mitch Williams505682c2014-05-20 08:01:37 +00009732 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009733 if (pf->vsi[i] &&
9734 pf->vsi[i]->uplink_seid == uplink_seid &&
9735 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9736 n++; /* count the VSIs */
9737 }
9738 }
9739 for (i = 0; i < I40E_MAX_VEB; i++) {
9740 if (!pf->veb[i])
9741 continue;
9742 if (pf->veb[i]->uplink_seid == uplink_seid)
9743 n++; /* count the VEBs */
9744 if (pf->veb[i]->seid == uplink_seid)
9745 veb = pf->veb[i];
9746 }
9747 if (n == 0 && veb && veb->uplink_seid != 0)
9748 i40e_veb_release(veb);
9749
9750 return 0;
9751}
9752
9753/**
9754 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9755 * @vsi: ptr to the VSI
9756 *
9757 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9758 * corresponding SW VSI structure and initializes num_queue_pairs for the
9759 * newly allocated VSI.
9760 *
9761 * Returns 0 on success or negative on failure
9762 **/
9763static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9764{
9765 int ret = -ENOENT;
9766 struct i40e_pf *pf = vsi->back;
9767
Alexander Duyck493fb302013-09-28 07:01:44 +00009768 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009769 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9770 vsi->seid);
9771 return -EEXIST;
9772 }
9773
9774 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009775 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009776 vsi->seid, vsi->base_vector);
9777 return -EEXIST;
9778 }
9779
Greg Rose90e04072014-03-06 08:59:57 +00009780 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009781 if (ret) {
9782 dev_info(&pf->pdev->dev,
9783 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9784 vsi->num_q_vectors, vsi->seid, ret);
9785 vsi->num_q_vectors = 0;
9786 goto vector_setup_out;
9787 }
9788
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009789 /* In Legacy mode, we do not have to get any other vector since we
9790 * piggyback on the misc/ICR0 for queue interrupts.
9791 */
9792 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9793 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009794 if (vsi->num_q_vectors)
9795 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9796 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009797 if (vsi->base_vector < 0) {
9798 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009799 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9800 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009801 i40e_vsi_free_q_vectors(vsi);
9802 ret = -ENOENT;
9803 goto vector_setup_out;
9804 }
9805
9806vector_setup_out:
9807 return ret;
9808}
9809
9810/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009811 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9812 * @vsi: pointer to the vsi.
9813 *
9814 * This re-allocates a vsi's queue resources.
9815 *
9816 * Returns pointer to the successfully allocated and configured VSI sw struct
9817 * on success, otherwise returns NULL on failure.
9818 **/
9819static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9820{
John Underwoodf5340392016-02-18 09:19:24 -08009821 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009822 u8 enabled_tc;
9823 int ret;
9824
John Underwoodf5340392016-02-18 09:19:24 -08009825 if (!vsi)
9826 return NULL;
9827
9828 pf = vsi->back;
9829
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009830 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9831 i40e_vsi_clear_rings(vsi);
9832
9833 i40e_vsi_free_arrays(vsi, false);
9834 i40e_set_num_rings_in_vsi(vsi);
9835 ret = i40e_vsi_alloc_arrays(vsi, false);
9836 if (ret)
9837 goto err_vsi;
9838
9839 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9840 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009841 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009842 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009843 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009844 goto err_vsi;
9845 }
9846 vsi->base_queue = ret;
9847
9848 /* Update the FW view of the VSI. Force a reset of TC and queue
9849 * layout configurations.
9850 */
9851 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9852 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9853 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9854 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -08009855 if (vsi->type == I40E_VSI_MAIN)
9856 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009857
9858 /* assign it some queues */
9859 ret = i40e_alloc_rings(vsi);
9860 if (ret)
9861 goto err_rings;
9862
9863 /* map all of the rings to the q_vectors */
9864 i40e_vsi_map_rings_to_vectors(vsi);
9865 return vsi;
9866
9867err_rings:
9868 i40e_vsi_free_q_vectors(vsi);
9869 if (vsi->netdev_registered) {
9870 vsi->netdev_registered = false;
9871 unregister_netdev(vsi->netdev);
9872 free_netdev(vsi->netdev);
9873 vsi->netdev = NULL;
9874 }
9875 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9876err_vsi:
9877 i40e_vsi_clear(vsi);
9878 return NULL;
9879}
9880
9881/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009882 * i40e_vsi_setup - Set up a VSI by a given type
9883 * @pf: board private structure
9884 * @type: VSI type
9885 * @uplink_seid: the switch element to link to
9886 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9887 *
9888 * This allocates the sw VSI structure and its queue resources, then add a VSI
9889 * to the identified VEB.
9890 *
9891 * Returns pointer to the successfully allocated and configure VSI sw struct on
9892 * success, otherwise returns NULL on failure.
9893 **/
9894struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9895 u16 uplink_seid, u32 param1)
9896{
9897 struct i40e_vsi *vsi = NULL;
9898 struct i40e_veb *veb = NULL;
9899 int ret, i;
9900 int v_idx;
9901
9902 /* The requested uplink_seid must be either
9903 * - the PF's port seid
9904 * no VEB is needed because this is the PF
9905 * or this is a Flow Director special case VSI
9906 * - seid of an existing VEB
9907 * - seid of a VSI that owns an existing VEB
9908 * - seid of a VSI that doesn't own a VEB
9909 * a new VEB is created and the VSI becomes the owner
9910 * - seid of the PF VSI, which is what creates the first VEB
9911 * this is a special case of the previous
9912 *
9913 * Find which uplink_seid we were given and create a new VEB if needed
9914 */
9915 for (i = 0; i < I40E_MAX_VEB; i++) {
9916 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9917 veb = pf->veb[i];
9918 break;
9919 }
9920 }
9921
9922 if (!veb && uplink_seid != pf->mac_seid) {
9923
Mitch Williams505682c2014-05-20 08:01:37 +00009924 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009925 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9926 vsi = pf->vsi[i];
9927 break;
9928 }
9929 }
9930 if (!vsi) {
9931 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9932 uplink_seid);
9933 return NULL;
9934 }
9935
9936 if (vsi->uplink_seid == pf->mac_seid)
9937 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9938 vsi->tc_config.enabled_tc);
9939 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9940 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9941 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009942 if (veb) {
9943 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9944 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009945 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009946 return NULL;
9947 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009948 /* We come up by default in VEPA mode if SRIOV is not
9949 * already enabled, in which case we can't force VEPA
9950 * mode.
9951 */
9952 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9953 veb->bridge_mode = BRIDGE_MODE_VEPA;
9954 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9955 }
Neerav Parikh51616012015-02-06 08:52:14 +00009956 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009957 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009958 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9959 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9960 veb = pf->veb[i];
9961 }
9962 if (!veb) {
9963 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9964 return NULL;
9965 }
9966
9967 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9968 uplink_seid = veb->seid;
9969 }
9970
9971 /* get vsi sw struct */
9972 v_idx = i40e_vsi_mem_alloc(pf, type);
9973 if (v_idx < 0)
9974 goto err_alloc;
9975 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009976 if (!vsi)
9977 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009978 vsi->type = type;
9979 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9980
9981 if (type == I40E_VSI_MAIN)
9982 pf->lan_vsi = v_idx;
9983 else if (type == I40E_VSI_SRIOV)
9984 vsi->vf_id = param1;
9985 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009986 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9987 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009988 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009989 dev_info(&pf->pdev->dev,
9990 "failed to get tracking for %d queues for VSI %d err=%d\n",
9991 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009992 goto err_vsi;
9993 }
9994 vsi->base_queue = ret;
9995
9996 /* get a VSI from the hardware */
9997 vsi->uplink_seid = uplink_seid;
9998 ret = i40e_add_vsi(vsi);
9999 if (ret)
10000 goto err_vsi;
10001
10002 switch (vsi->type) {
10003 /* setup the netdev if needed */
10004 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010005 /* Apply relevant filters if a platform-specific mac
10006 * address was selected.
10007 */
10008 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
10009 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
10010 if (ret) {
10011 dev_warn(&pf->pdev->dev,
10012 "could not set up macaddr; err %d\n",
10013 ret);
10014 }
10015 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010016 case I40E_VSI_VMDQ2:
10017 ret = i40e_config_netdev(vsi);
10018 if (ret)
10019 goto err_netdev;
10020 ret = register_netdev(vsi->netdev);
10021 if (ret)
10022 goto err_netdev;
10023 vsi->netdev_registered = true;
10024 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010025#ifdef CONFIG_I40E_DCB
10026 /* Setup DCB netlink interface */
10027 i40e_dcbnl_setup(vsi);
10028#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010029 /* fall through */
10030
10031 case I40E_VSI_FDIR:
10032 /* set up vectors and rings if needed */
10033 ret = i40e_vsi_setup_vectors(vsi);
10034 if (ret)
10035 goto err_msix;
10036
10037 ret = i40e_alloc_rings(vsi);
10038 if (ret)
10039 goto err_rings;
10040
10041 /* map all of the rings to the q_vectors */
10042 i40e_vsi_map_rings_to_vectors(vsi);
10043
10044 i40e_vsi_reset_stats(vsi);
10045 break;
10046
10047 default:
10048 /* no netdev or rings for the other VSI types */
10049 break;
10050 }
10051
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010052 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10053 (vsi->type == I40E_VSI_VMDQ2)) {
10054 ret = i40e_vsi_config_rss(vsi);
10055 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010056 return vsi;
10057
10058err_rings:
10059 i40e_vsi_free_q_vectors(vsi);
10060err_msix:
10061 if (vsi->netdev_registered) {
10062 vsi->netdev_registered = false;
10063 unregister_netdev(vsi->netdev);
10064 free_netdev(vsi->netdev);
10065 vsi->netdev = NULL;
10066 }
10067err_netdev:
10068 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10069err_vsi:
10070 i40e_vsi_clear(vsi);
10071err_alloc:
10072 return NULL;
10073}
10074
10075/**
10076 * i40e_veb_get_bw_info - Query VEB BW information
10077 * @veb: the veb to query
10078 *
10079 * Query the Tx scheduler BW configuration data for given VEB
10080 **/
10081static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10082{
10083 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10084 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10085 struct i40e_pf *pf = veb->pf;
10086 struct i40e_hw *hw = &pf->hw;
10087 u32 tc_bw_max;
10088 int ret = 0;
10089 int i;
10090
10091 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10092 &bw_data, NULL);
10093 if (ret) {
10094 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010095 "query veb bw config failed, err %s aq_err %s\n",
10096 i40e_stat_str(&pf->hw, ret),
10097 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010098 goto out;
10099 }
10100
10101 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10102 &ets_data, NULL);
10103 if (ret) {
10104 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010105 "query veb bw ets config failed, err %s aq_err %s\n",
10106 i40e_stat_str(&pf->hw, ret),
10107 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010108 goto out;
10109 }
10110
10111 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10112 veb->bw_max_quanta = ets_data.tc_bw_max;
10113 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010114 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010115 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10116 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10117 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10118 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10119 veb->bw_tc_limit_credits[i] =
10120 le16_to_cpu(bw_data.tc_bw_limits[i]);
10121 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10122 }
10123
10124out:
10125 return ret;
10126}
10127
10128/**
10129 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10130 * @pf: board private structure
10131 *
10132 * On error: returns error code (negative)
10133 * On success: returns vsi index in PF (positive)
10134 **/
10135static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10136{
10137 int ret = -ENOENT;
10138 struct i40e_veb *veb;
10139 int i;
10140
10141 /* Need to protect the allocation of switch elements at the PF level */
10142 mutex_lock(&pf->switch_mutex);
10143
10144 /* VEB list may be fragmented if VEB creation/destruction has
10145 * been happening. We can afford to do a quick scan to look
10146 * for any free slots in the list.
10147 *
10148 * find next empty veb slot, looping back around if necessary
10149 */
10150 i = 0;
10151 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10152 i++;
10153 if (i >= I40E_MAX_VEB) {
10154 ret = -ENOMEM;
10155 goto err_alloc_veb; /* out of VEB slots! */
10156 }
10157
10158 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10159 if (!veb) {
10160 ret = -ENOMEM;
10161 goto err_alloc_veb;
10162 }
10163 veb->pf = pf;
10164 veb->idx = i;
10165 veb->enabled_tc = 1;
10166
10167 pf->veb[i] = veb;
10168 ret = i;
10169err_alloc_veb:
10170 mutex_unlock(&pf->switch_mutex);
10171 return ret;
10172}
10173
10174/**
10175 * i40e_switch_branch_release - Delete a branch of the switch tree
10176 * @branch: where to start deleting
10177 *
10178 * This uses recursion to find the tips of the branch to be
10179 * removed, deleting until we get back to and can delete this VEB.
10180 **/
10181static void i40e_switch_branch_release(struct i40e_veb *branch)
10182{
10183 struct i40e_pf *pf = branch->pf;
10184 u16 branch_seid = branch->seid;
10185 u16 veb_idx = branch->idx;
10186 int i;
10187
10188 /* release any VEBs on this VEB - RECURSION */
10189 for (i = 0; i < I40E_MAX_VEB; i++) {
10190 if (!pf->veb[i])
10191 continue;
10192 if (pf->veb[i]->uplink_seid == branch->seid)
10193 i40e_switch_branch_release(pf->veb[i]);
10194 }
10195
10196 /* Release the VSIs on this VEB, but not the owner VSI.
10197 *
10198 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10199 * the VEB itself, so don't use (*branch) after this loop.
10200 */
Mitch Williams505682c2014-05-20 08:01:37 +000010201 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010202 if (!pf->vsi[i])
10203 continue;
10204 if (pf->vsi[i]->uplink_seid == branch_seid &&
10205 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10206 i40e_vsi_release(pf->vsi[i]);
10207 }
10208 }
10209
10210 /* There's one corner case where the VEB might not have been
10211 * removed, so double check it here and remove it if needed.
10212 * This case happens if the veb was created from the debugfs
10213 * commands and no VSIs were added to it.
10214 */
10215 if (pf->veb[veb_idx])
10216 i40e_veb_release(pf->veb[veb_idx]);
10217}
10218
10219/**
10220 * i40e_veb_clear - remove veb struct
10221 * @veb: the veb to remove
10222 **/
10223static void i40e_veb_clear(struct i40e_veb *veb)
10224{
10225 if (!veb)
10226 return;
10227
10228 if (veb->pf) {
10229 struct i40e_pf *pf = veb->pf;
10230
10231 mutex_lock(&pf->switch_mutex);
10232 if (pf->veb[veb->idx] == veb)
10233 pf->veb[veb->idx] = NULL;
10234 mutex_unlock(&pf->switch_mutex);
10235 }
10236
10237 kfree(veb);
10238}
10239
10240/**
10241 * i40e_veb_release - Delete a VEB and free its resources
10242 * @veb: the VEB being removed
10243 **/
10244void i40e_veb_release(struct i40e_veb *veb)
10245{
10246 struct i40e_vsi *vsi = NULL;
10247 struct i40e_pf *pf;
10248 int i, n = 0;
10249
10250 pf = veb->pf;
10251
10252 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010253 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010254 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10255 n++;
10256 vsi = pf->vsi[i];
10257 }
10258 }
10259 if (n != 1) {
10260 dev_info(&pf->pdev->dev,
10261 "can't remove VEB %d with %d VSIs left\n",
10262 veb->seid, n);
10263 return;
10264 }
10265
10266 /* move the remaining VSI to uplink veb */
10267 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10268 if (veb->uplink_seid) {
10269 vsi->uplink_seid = veb->uplink_seid;
10270 if (veb->uplink_seid == pf->mac_seid)
10271 vsi->veb_idx = I40E_NO_VEB;
10272 else
10273 vsi->veb_idx = veb->veb_idx;
10274 } else {
10275 /* floating VEB */
10276 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10277 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10278 }
10279
10280 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10281 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010282}
10283
10284/**
10285 * i40e_add_veb - create the VEB in the switch
10286 * @veb: the VEB to be instantiated
10287 * @vsi: the controlling VSI
10288 **/
10289static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10290{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010291 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010292 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010293 int ret;
10294
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010295 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010296 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010297 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010298
10299 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010300 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010301 dev_info(&pf->pdev->dev,
10302 "couldn't add VEB, err %s aq_err %s\n",
10303 i40e_stat_str(&pf->hw, ret),
10304 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010305 return -EPERM;
10306 }
10307
10308 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010309 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010310 &veb->stats_idx, NULL, NULL, NULL);
10311 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010312 dev_info(&pf->pdev->dev,
10313 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10314 i40e_stat_str(&pf->hw, ret),
10315 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010316 return -EPERM;
10317 }
10318 ret = i40e_veb_get_bw_info(veb);
10319 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010320 dev_info(&pf->pdev->dev,
10321 "couldn't get VEB bw info, err %s aq_err %s\n",
10322 i40e_stat_str(&pf->hw, ret),
10323 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10324 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010325 return -ENOENT;
10326 }
10327
10328 vsi->uplink_seid = veb->seid;
10329 vsi->veb_idx = veb->idx;
10330 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10331
10332 return 0;
10333}
10334
10335/**
10336 * i40e_veb_setup - Set up a VEB
10337 * @pf: board private structure
10338 * @flags: VEB setup flags
10339 * @uplink_seid: the switch element to link to
10340 * @vsi_seid: the initial VSI seid
10341 * @enabled_tc: Enabled TC bit-map
10342 *
10343 * This allocates the sw VEB structure and links it into the switch
10344 * It is possible and legal for this to be a duplicate of an already
10345 * existing VEB. It is also possible for both uplink and vsi seids
10346 * to be zero, in order to create a floating VEB.
10347 *
10348 * Returns pointer to the successfully allocated VEB sw struct on
10349 * success, otherwise returns NULL on failure.
10350 **/
10351struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10352 u16 uplink_seid, u16 vsi_seid,
10353 u8 enabled_tc)
10354{
10355 struct i40e_veb *veb, *uplink_veb = NULL;
10356 int vsi_idx, veb_idx;
10357 int ret;
10358
10359 /* if one seid is 0, the other must be 0 to create a floating relay */
10360 if ((uplink_seid == 0 || vsi_seid == 0) &&
10361 (uplink_seid + vsi_seid != 0)) {
10362 dev_info(&pf->pdev->dev,
10363 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10364 uplink_seid, vsi_seid);
10365 return NULL;
10366 }
10367
10368 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010369 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010370 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10371 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010372 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010373 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10374 vsi_seid);
10375 return NULL;
10376 }
10377
10378 if (uplink_seid && uplink_seid != pf->mac_seid) {
10379 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10380 if (pf->veb[veb_idx] &&
10381 pf->veb[veb_idx]->seid == uplink_seid) {
10382 uplink_veb = pf->veb[veb_idx];
10383 break;
10384 }
10385 }
10386 if (!uplink_veb) {
10387 dev_info(&pf->pdev->dev,
10388 "uplink seid %d not found\n", uplink_seid);
10389 return NULL;
10390 }
10391 }
10392
10393 /* get veb sw struct */
10394 veb_idx = i40e_veb_mem_alloc(pf);
10395 if (veb_idx < 0)
10396 goto err_alloc;
10397 veb = pf->veb[veb_idx];
10398 veb->flags = flags;
10399 veb->uplink_seid = uplink_seid;
10400 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10401 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10402
10403 /* create the VEB in the switch */
10404 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10405 if (ret)
10406 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010407 if (vsi_idx == pf->lan_vsi)
10408 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010409
10410 return veb;
10411
10412err_veb:
10413 i40e_veb_clear(veb);
10414err_alloc:
10415 return NULL;
10416}
10417
10418/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010419 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010420 * @pf: board private structure
10421 * @ele: element we are building info from
10422 * @num_reported: total number of elements
10423 * @printconfig: should we print the contents
10424 *
10425 * helper function to assist in extracting a few useful SEID values.
10426 **/
10427static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10428 struct i40e_aqc_switch_config_element_resp *ele,
10429 u16 num_reported, bool printconfig)
10430{
10431 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10432 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10433 u8 element_type = ele->element_type;
10434 u16 seid = le16_to_cpu(ele->seid);
10435
10436 if (printconfig)
10437 dev_info(&pf->pdev->dev,
10438 "type=%d seid=%d uplink=%d downlink=%d\n",
10439 element_type, seid, uplink_seid, downlink_seid);
10440
10441 switch (element_type) {
10442 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10443 pf->mac_seid = seid;
10444 break;
10445 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10446 /* Main VEB? */
10447 if (uplink_seid != pf->mac_seid)
10448 break;
10449 if (pf->lan_veb == I40E_NO_VEB) {
10450 int v;
10451
10452 /* find existing or else empty VEB */
10453 for (v = 0; v < I40E_MAX_VEB; v++) {
10454 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10455 pf->lan_veb = v;
10456 break;
10457 }
10458 }
10459 if (pf->lan_veb == I40E_NO_VEB) {
10460 v = i40e_veb_mem_alloc(pf);
10461 if (v < 0)
10462 break;
10463 pf->lan_veb = v;
10464 }
10465 }
10466
10467 pf->veb[pf->lan_veb]->seid = seid;
10468 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10469 pf->veb[pf->lan_veb]->pf = pf;
10470 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10471 break;
10472 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10473 if (num_reported != 1)
10474 break;
10475 /* This is immediately after a reset so we can assume this is
10476 * the PF's VSI
10477 */
10478 pf->mac_seid = uplink_seid;
10479 pf->pf_seid = downlink_seid;
10480 pf->main_vsi_seid = seid;
10481 if (printconfig)
10482 dev_info(&pf->pdev->dev,
10483 "pf_seid=%d main_vsi_seid=%d\n",
10484 pf->pf_seid, pf->main_vsi_seid);
10485 break;
10486 case I40E_SWITCH_ELEMENT_TYPE_PF:
10487 case I40E_SWITCH_ELEMENT_TYPE_VF:
10488 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10489 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10490 case I40E_SWITCH_ELEMENT_TYPE_PE:
10491 case I40E_SWITCH_ELEMENT_TYPE_PA:
10492 /* ignore these for now */
10493 break;
10494 default:
10495 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10496 element_type, seid);
10497 break;
10498 }
10499}
10500
10501/**
10502 * i40e_fetch_switch_configuration - Get switch config from firmware
10503 * @pf: board private structure
10504 * @printconfig: should we print the contents
10505 *
10506 * Get the current switch configuration from the device and
10507 * extract a few useful SEID values.
10508 **/
10509int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10510{
10511 struct i40e_aqc_get_switch_config_resp *sw_config;
10512 u16 next_seid = 0;
10513 int ret = 0;
10514 u8 *aq_buf;
10515 int i;
10516
10517 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10518 if (!aq_buf)
10519 return -ENOMEM;
10520
10521 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10522 do {
10523 u16 num_reported, num_total;
10524
10525 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10526 I40E_AQ_LARGE_BUF,
10527 &next_seid, NULL);
10528 if (ret) {
10529 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010530 "get switch config failed err %s aq_err %s\n",
10531 i40e_stat_str(&pf->hw, ret),
10532 i40e_aq_str(&pf->hw,
10533 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010534 kfree(aq_buf);
10535 return -ENOENT;
10536 }
10537
10538 num_reported = le16_to_cpu(sw_config->header.num_reported);
10539 num_total = le16_to_cpu(sw_config->header.num_total);
10540
10541 if (printconfig)
10542 dev_info(&pf->pdev->dev,
10543 "header: %d reported %d total\n",
10544 num_reported, num_total);
10545
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010546 for (i = 0; i < num_reported; i++) {
10547 struct i40e_aqc_switch_config_element_resp *ele =
10548 &sw_config->element[i];
10549
10550 i40e_setup_pf_switch_element(pf, ele, num_reported,
10551 printconfig);
10552 }
10553 } while (next_seid != 0);
10554
10555 kfree(aq_buf);
10556 return ret;
10557}
10558
10559/**
10560 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10561 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010562 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010563 *
10564 * Returns 0 on success, negative value on failure
10565 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010566static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010567{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010568 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010569 int ret;
10570
10571 /* find out what's out there already */
10572 ret = i40e_fetch_switch_configuration(pf, false);
10573 if (ret) {
10574 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010575 "couldn't fetch switch config, err %s aq_err %s\n",
10576 i40e_stat_str(&pf->hw, ret),
10577 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010578 return ret;
10579 }
10580 i40e_pf_reset_stats(pf);
10581
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010582 /* set the switch config bit for the whole device to
10583 * support limited promisc or true promisc
10584 * when user requests promisc. The default is limited
10585 * promisc.
10586 */
10587
10588 if ((pf->hw.pf_id == 0) &&
10589 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10590 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10591
10592 if (pf->hw.pf_id == 0) {
10593 u16 valid_flags;
10594
10595 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10596 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10597 NULL);
10598 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10599 dev_info(&pf->pdev->dev,
10600 "couldn't set switch config bits, err %s aq_err %s\n",
10601 i40e_stat_str(&pf->hw, ret),
10602 i40e_aq_str(&pf->hw,
10603 pf->hw.aq.asq_last_status));
10604 /* not a fatal problem, just keep going */
10605 }
10606 }
10607
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010608 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010609 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010610 struct i40e_vsi *vsi = NULL;
10611 u16 uplink_seid;
10612
10613 /* Set up the PF VSI associated with the PF's main VSI
10614 * that is already in the HW switch
10615 */
10616 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10617 uplink_seid = pf->veb[pf->lan_veb]->seid;
10618 else
10619 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010620 if (pf->lan_vsi == I40E_NO_VSI)
10621 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10622 else if (reinit)
10623 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010624 if (!vsi) {
10625 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10626 i40e_fdir_teardown(pf);
10627 return -EAGAIN;
10628 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010629 } else {
10630 /* force a reset of TC and queue layout configurations */
10631 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010632
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010633 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10634 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10635 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10636 }
10637 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10638
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010639 i40e_fdir_sb_setup(pf);
10640
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010641 /* Setup static PF queue filter control settings */
10642 ret = i40e_setup_pf_filter_control(pf);
10643 if (ret) {
10644 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10645 ret);
10646 /* Failure here should not stop continuing other steps */
10647 }
10648
10649 /* enable RSS in the HW, even for only one queue, as the stack can use
10650 * the hash
10651 */
10652 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010653 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010654
10655 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010656 i40e_link_event(pf);
10657
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010658 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010659 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10660 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010661
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010662 i40e_ptp_init(pf);
10663
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010664 return ret;
10665}
10666
10667/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010668 * i40e_determine_queue_usage - Work out queue distribution
10669 * @pf: board private structure
10670 **/
10671static void i40e_determine_queue_usage(struct i40e_pf *pf)
10672{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010673 int queues_left;
10674
10675 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010676
10677 /* Find the max queues to be put into basic use. We'll always be
10678 * using TC0, whether or not DCB is running, and TC0 will get the
10679 * big RSS set.
10680 */
10681 queues_left = pf->hw.func_caps.num_tx_qp;
10682
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010683 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010684 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010685 /* one qp for PF, no queues for anything else */
10686 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010687 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010688
10689 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010690 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010691 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010692 I40E_FLAG_FD_SB_ENABLED |
10693 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010694 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010695 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010696 I40E_FLAG_SRIOV_ENABLED |
10697 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010698 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10699 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010700 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010701 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010702 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010703 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010704 queues_left -= pf->num_lan_qps;
10705
10706 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010707 I40E_FLAG_IWARP_ENABLED |
Frank Zhang9aa7e932014-05-20 08:01:42 +000010708 I40E_FLAG_FD_SB_ENABLED |
10709 I40E_FLAG_FD_ATR_ENABLED |
10710 I40E_FLAG_DCB_ENABLED |
10711 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010712 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010713 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010714 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010715 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010716 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10717 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010718 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10719 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010720 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10721 num_online_cpus());
10722 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10723 pf->hw.func_caps.num_tx_qp);
10724
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010725 queues_left -= pf->num_lan_qps;
10726 }
10727
10728 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10729 if (queues_left > 1) {
10730 queues_left -= 1; /* save 1 queue for FD */
10731 } else {
10732 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10733 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10734 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010735 }
10736
10737 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10738 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010739 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10740 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010741 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10742 }
10743
10744 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10745 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10746 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10747 (queues_left / pf->num_vmdq_qps));
10748 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10749 }
10750
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010751 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010752 dev_dbg(&pf->pdev->dev,
10753 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10754 pf->hw.func_caps.num_tx_qp,
10755 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010756 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10757 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10758 queues_left);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010759}
10760
10761/**
10762 * i40e_setup_pf_filter_control - Setup PF static filter control
10763 * @pf: PF to be setup
10764 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010765 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010766 * settings. If PE/FCoE are enabled then it will also set the per PF
10767 * based filter sizes required for them. It also enables Flow director,
10768 * ethertype and macvlan type filter settings for the pf.
10769 *
10770 * Returns 0 on success, negative on failure
10771 **/
10772static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10773{
10774 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10775
10776 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10777
10778 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010779 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010780 settings->enable_fdir = true;
10781
10782 /* Ethtype and MACVLAN filters enabled for PF */
10783 settings->enable_ethtype = true;
10784 settings->enable_macvlan = true;
10785
10786 if (i40e_set_filter_control(&pf->hw, settings))
10787 return -ENOENT;
10788
10789 return 0;
10790}
10791
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010792#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010793#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010794static void i40e_print_features(struct i40e_pf *pf)
10795{
10796 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010797 char *buf;
10798 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010799
Joe Perches3b195842015-12-03 04:20:57 -080010800 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10801 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010802 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010803
Joe Perches3b195842015-12-03 04:20:57 -080010804 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010805#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010806 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010807#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010808 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010809 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010810 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010811 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010812 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010813 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010814 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010815 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010816 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10817 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010818 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010819 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010820 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010821 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010822 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010823 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010824 i += snprintf(&buf[i], REMAIN(i), " PTP");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010825 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010826 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010827 else
Joe Perches3b195842015-12-03 04:20:57 -080010828 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010829
Joe Perches3b195842015-12-03 04:20:57 -080010830 dev_info(&pf->pdev->dev, "%s\n", buf);
10831 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010832 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010833}
10834
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010835/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010836 * i40e_get_platform_mac_addr - get platform-specific MAC address
10837 *
10838 * @pdev: PCI device information struct
10839 * @pf: board private structure
10840 *
10841 * Look up the MAC address in Open Firmware on systems that support it,
10842 * and use IDPROM on SPARC if no OF address is found. On return, the
10843 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10844 * has been selected.
10845 **/
10846static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10847{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010848 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010849 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010850 pf->flags |= I40E_FLAG_PF_MAC;
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 Kirsherb40c82e62015-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
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011064 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011065 /* allow a platform config to override the HW addr */
11066 i40e_get_platform_mac_addr(pdev, pf);
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);
11095 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011096
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011097 /* NVM bit on means WoL disabled for the port */
11098 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011099 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011100 pf->wol_en = false;
11101 else
11102 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011103 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11104
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011105 /* set up the main switch operations */
11106 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011107 err = i40e_init_interrupt_scheme(pf);
11108 if (err)
11109 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011110
Mitch Williams505682c2014-05-20 08:01:37 +000011111 /* The number of VSIs reported by the FW is the minimum guaranteed
11112 * to us; HW supports far more and we share the remaining pool with
11113 * the other PFs. We allocate space for more than the guarantee with
11114 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011115 */
Mitch Williams505682c2014-05-20 08:01:37 +000011116 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11117 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11118 else
11119 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11120
11121 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011122 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11123 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011124 if (!pf->vsi) {
11125 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011126 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011127 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011128
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011129#ifdef CONFIG_PCI_IOV
11130 /* prep for VF support */
11131 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11132 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11133 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11134 if (pci_num_vf(pdev))
11135 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11136 }
11137#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011138 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011139 if (err) {
11140 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11141 goto err_vsis;
11142 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011143
11144 /* Make sure flow control is set according to current settings */
11145 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11146 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11147 dev_dbg(&pf->pdev->dev,
11148 "Set fc with err %s aq_err %s on get_phy_cap\n",
11149 i40e_stat_str(hw, err),
11150 i40e_aq_str(hw, hw->aq.asq_last_status));
11151 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11152 dev_dbg(&pf->pdev->dev,
11153 "Set fc with err %s aq_err %s on set_phy_config\n",
11154 i40e_stat_str(hw, err),
11155 i40e_aq_str(hw, hw->aq.asq_last_status));
11156 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11157 dev_dbg(&pf->pdev->dev,
11158 "Set fc with err %s aq_err %s on get_link_info\n",
11159 i40e_stat_str(hw, err),
11160 i40e_aq_str(hw, hw->aq.asq_last_status));
11161
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011162 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011163 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011164 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11165 i40e_vsi_open(pf->vsi[i]);
11166 break;
11167 }
11168 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011169
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011170 /* The driver only wants link up/down and module qualification
11171 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011172 */
11173 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011174 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011175 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011176 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011177 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011178 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11179 i40e_stat_str(&pf->hw, err),
11180 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011181
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011182 /* Reconfigure hardware for allowing smaller MSS in the case
11183 * of TSO, so that we avoid the MDD being fired and causing
11184 * a reset in the case of small MSS+TSO.
11185 */
11186 val = rd32(hw, I40E_REG_MSS);
11187 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11188 val &= ~I40E_REG_MSS_MIN_MASK;
11189 val |= I40E_64BYTE_MSS;
11190 wr32(hw, I40E_REG_MSS, val);
11191 }
11192
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011193 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011194 msleep(75);
11195 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11196 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011197 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11198 i40e_stat_str(&pf->hw, err),
11199 i40e_aq_str(&pf->hw,
11200 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011201 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011202 /* The main driver is (mostly) up and happy. We need to set this state
11203 * before setting up the misc vector or we get a race and the vector
11204 * ends up disabled forever.
11205 */
11206 clear_bit(__I40E_DOWN, &pf->state);
11207
11208 /* In case of MSIX we are going to setup the misc vector right here
11209 * to handle admin queue events etc. In case of legacy and MSI
11210 * the misc functionality and queue processing is combined in
11211 * the same vector and that gets setup at open.
11212 */
11213 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11214 err = i40e_setup_misc_vector(pf);
11215 if (err) {
11216 dev_info(&pdev->dev,
11217 "setup of misc vector failed: %d\n", err);
11218 goto err_vsis;
11219 }
11220 }
11221
Greg Rosedf805f62014-04-04 04:43:16 +000011222#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011223 /* prep for VF support */
11224 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011225 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11226 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011227 /* disable link interrupts for VFs */
11228 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11229 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11230 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11231 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011232
11233 if (pci_num_vf(pdev)) {
11234 dev_info(&pdev->dev,
11235 "Active VFs found, allocating resources.\n");
11236 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11237 if (err)
11238 dev_info(&pdev->dev,
11239 "Error %d allocating resources for existing VFs\n",
11240 err);
11241 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011242 }
Greg Rosedf805f62014-04-04 04:43:16 +000011243#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011244
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011245 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11246 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11247 pf->num_iwarp_msix,
11248 I40E_IWARP_IRQ_PILE_ID);
11249 if (pf->iwarp_base_vector < 0) {
11250 dev_info(&pdev->dev,
11251 "failed to get tracking for %d vectors for IWARP err=%d\n",
11252 pf->num_iwarp_msix, pf->iwarp_base_vector);
11253 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11254 }
11255 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011256
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011257 i40e_dbg_pf_init(pf);
11258
11259 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011260 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011261
11262 /* since everything's happy, start the service_task timer */
11263 mod_timer(&pf->service_timer,
11264 round_jiffies(jiffies + pf->service_timer_period));
11265
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011266 /* add this PF to client device list and launch a client service task */
Mitch Williams004eb612017-04-04 12:40:16 -070011267 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11268 err = i40e_lan_add_device(pf);
11269 if (err)
11270 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11271 err);
11272 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011273
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011274#define PCI_SPEED_SIZE 8
11275#define PCI_WIDTH_SIZE 8
11276 /* Devices on the IOSF bus do not have this information
11277 * and will report PCI Gen 1 x 1 by default so don't bother
11278 * checking them.
11279 */
11280 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11281 char speed[PCI_SPEED_SIZE] = "Unknown";
11282 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011283
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011284 /* Get the negotiated link width and speed from PCI config
11285 * space
11286 */
11287 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11288 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011289
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011290 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011291
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011292 switch (hw->bus.speed) {
11293 case i40e_bus_speed_8000:
11294 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11295 case i40e_bus_speed_5000:
11296 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11297 case i40e_bus_speed_2500:
11298 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11299 default:
11300 break;
11301 }
11302 switch (hw->bus.width) {
11303 case i40e_bus_width_pcie_x8:
11304 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11305 case i40e_bus_width_pcie_x4:
11306 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11307 case i40e_bus_width_pcie_x2:
11308 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11309 case i40e_bus_width_pcie_x1:
11310 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11311 default:
11312 break;
11313 }
11314
11315 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11316 speed, width);
11317
11318 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11319 hw->bus.speed < i40e_bus_speed_8000) {
11320 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11321 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11322 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011323 }
11324
Catherine Sullivane8278452015-02-06 08:52:08 +000011325 /* get the requested speeds from the fw */
11326 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11327 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011328 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11329 i40e_stat_str(&pf->hw, err),
11330 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011331 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11332
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011333 /* get the supported phy types from the fw */
11334 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11335 if (err)
11336 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11337 i40e_stat_str(&pf->hw, err),
11338 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011339
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011340 /* Add a filter to drop all Flow control frames from any VSI from being
11341 * transmitted. By doing so we stop a malicious VF from sending out
11342 * PAUSE or PFC frames and potentially controlling traffic for other
11343 * PF/VF VSIs.
11344 * The FW can still send Flow control frames if enabled.
11345 */
11346 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11347 pf->main_vsi_seid);
11348
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011349 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011350 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11351 pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011352 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11353 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011354 /* print a string summarizing features */
11355 i40e_print_features(pf);
11356
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011357 return 0;
11358
11359 /* Unwind what we've done if something failed in the setup */
11360err_vsis:
11361 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011362 i40e_clear_interrupt_scheme(pf);
11363 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011364err_switch_setup:
11365 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011366 del_timer_sync(&pf->service_timer);
11367err_mac_addr:
11368err_configure_lan_hmc:
11369 (void)i40e_shutdown_lan_hmc(hw);
11370err_init_lan_hmc:
11371 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011372err_sw_init:
11373err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011374err_pf_reset:
11375 iounmap(hw->hw_addr);
11376err_ioremap:
11377 kfree(pf);
11378err_pf_alloc:
11379 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011380 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011381err_pci_reg:
11382err_dma:
11383 pci_disable_device(pdev);
11384 return err;
11385}
11386
11387/**
11388 * i40e_remove - Device removal routine
11389 * @pdev: PCI device information struct
11390 *
11391 * i40e_remove is called by the PCI subsystem to alert the driver
11392 * that is should release a PCI device. This could be caused by a
11393 * Hot-Plug event, or because the driver is going to be removed from
11394 * memory.
11395 **/
11396static void i40e_remove(struct pci_dev *pdev)
11397{
11398 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011399 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011400 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011401 int i;
11402
11403 i40e_dbg_pf_exit(pf);
11404
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011405 i40e_ptp_stop(pf);
11406
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011407 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011408 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11409 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011410
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011411 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011412 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011413 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011414 if (pf->service_timer.data)
11415 del_timer_sync(&pf->service_timer);
11416 if (pf->service_task.func)
11417 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011418
Mitch Williams921c4672017-03-30 00:46:08 -070011419 /* Client close must be called explicitly here because the timer
11420 * has been stopped.
11421 */
11422 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
11423
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011424 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11425 i40e_free_vfs(pf);
11426 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11427 }
11428
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011429 i40e_fdir_teardown(pf);
11430
11431 /* If there is a switch structure or any orphans, remove them.
11432 * This will leave only the PF's VSI remaining.
11433 */
11434 for (i = 0; i < I40E_MAX_VEB; i++) {
11435 if (!pf->veb[i])
11436 continue;
11437
11438 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11439 pf->veb[i]->uplink_seid == 0)
11440 i40e_switch_branch_release(pf->veb[i]);
11441 }
11442
11443 /* Now we can shutdown the PF's VSI, just before we kill
11444 * adminq and hmc.
11445 */
11446 if (pf->vsi[pf->lan_vsi])
11447 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11448
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011449 /* remove attached clients */
Mitch Williams004eb612017-04-04 12:40:16 -070011450 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11451 ret_code = i40e_lan_del_device(pf);
11452 if (ret_code)
11453 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11454 ret_code);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011455 }
11456
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011457 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011458 if (hw->hmc.hmc_obj) {
11459 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011460 if (ret_code)
11461 dev_warn(&pdev->dev,
11462 "Failed to destroy the HMC resources: %d\n",
11463 ret_code);
11464 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011465
11466 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011467 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011468
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011469 /* destroy the locks only once, here */
11470 mutex_destroy(&hw->aq.arq_mutex);
11471 mutex_destroy(&hw->aq.asq_mutex);
11472
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011473 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11474 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011475 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011476 if (pf->vsi[i]) {
11477 i40e_vsi_clear_rings(pf->vsi[i]);
11478 i40e_vsi_clear(pf->vsi[i]);
11479 pf->vsi[i] = NULL;
11480 }
11481 }
11482
11483 for (i = 0; i < I40E_MAX_VEB; i++) {
11484 kfree(pf->veb[i]);
11485 pf->veb[i] = NULL;
11486 }
11487
11488 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011489 kfree(pf->vsi);
11490
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011491 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011492 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011493 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011494
11495 pci_disable_pcie_error_reporting(pdev);
11496 pci_disable_device(pdev);
11497}
11498
11499/**
11500 * i40e_pci_error_detected - warning that something funky happened in PCI land
11501 * @pdev: PCI device information struct
11502 *
11503 * Called to warn that something happened and the error handling steps
11504 * are in progress. Allows the driver to quiesce things, be ready for
11505 * remediation.
11506 **/
11507static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11508 enum pci_channel_state error)
11509{
11510 struct i40e_pf *pf = pci_get_drvdata(pdev);
11511
11512 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11513
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011514 if (!pf) {
11515 dev_info(&pdev->dev,
11516 "Cannot recover - error happened during device probe\n");
11517 return PCI_ERS_RESULT_DISCONNECT;
11518 }
11519
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011520 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011521 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11522 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011523 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011524 rtnl_unlock();
11525 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011526
11527 /* Request a slot reset */
11528 return PCI_ERS_RESULT_NEED_RESET;
11529}
11530
11531/**
11532 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11533 * @pdev: PCI device information struct
11534 *
11535 * Called to find if the driver can work with the device now that
11536 * the pci slot has been reset. If a basic connection seems good
11537 * (registers are readable and have sane content) then return a
11538 * happy little PCI_ERS_RESULT_xxx.
11539 **/
11540static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11541{
11542 struct i40e_pf *pf = pci_get_drvdata(pdev);
11543 pci_ers_result_t result;
11544 int err;
11545 u32 reg;
11546
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011547 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011548 if (pci_enable_device_mem(pdev)) {
11549 dev_info(&pdev->dev,
11550 "Cannot re-enable PCI device after reset.\n");
11551 result = PCI_ERS_RESULT_DISCONNECT;
11552 } else {
11553 pci_set_master(pdev);
11554 pci_restore_state(pdev);
11555 pci_save_state(pdev);
11556 pci_wake_from_d3(pdev, false);
11557
11558 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11559 if (reg == 0)
11560 result = PCI_ERS_RESULT_RECOVERED;
11561 else
11562 result = PCI_ERS_RESULT_DISCONNECT;
11563 }
11564
11565 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11566 if (err) {
11567 dev_info(&pdev->dev,
11568 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11569 err);
11570 /* non-fatal, continue */
11571 }
11572
11573 return result;
11574}
11575
11576/**
11577 * i40e_pci_error_resume - restart operations after PCI error recovery
11578 * @pdev: PCI device information struct
11579 *
11580 * Called to allow the driver to bring things back up after PCI error
11581 * and/or reset recovery has finished.
11582 **/
11583static void i40e_pci_error_resume(struct pci_dev *pdev)
11584{
11585 struct i40e_pf *pf = pci_get_drvdata(pdev);
11586
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011587 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011588 if (test_bit(__I40E_SUSPENDED, &pf->state))
11589 return;
11590
11591 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011592 i40e_handle_reset_warning(pf, true);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011593 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011594}
11595
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011596/**
Joshua Hay1d680052016-12-12 15:44:08 -080011597 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
11598 * using the mac_address_write admin q function
11599 * @pf: pointer to i40e_pf struct
11600 **/
11601static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
11602{
11603 struct i40e_hw *hw = &pf->hw;
11604 i40e_status ret;
11605 u8 mac_addr[6];
11606 u16 flags = 0;
11607
11608 /* Get current MAC address in case it's an LAA */
11609 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
11610 ether_addr_copy(mac_addr,
11611 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
11612 } else {
11613 dev_err(&pf->pdev->dev,
11614 "Failed to retrieve MAC address; using default\n");
11615 ether_addr_copy(mac_addr, hw->mac.addr);
11616 }
11617
11618 /* The FW expects the mac address write cmd to first be called with
11619 * one of these flags before calling it again with the multicast
11620 * enable flags.
11621 */
11622 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
11623
11624 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
11625 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
11626
11627 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11628 if (ret) {
11629 dev_err(&pf->pdev->dev,
11630 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
11631 return;
11632 }
11633
11634 flags = I40E_AQC_MC_MAG_EN
11635 | I40E_AQC_WOL_PRESERVE_ON_PFR
11636 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
11637 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11638 if (ret)
11639 dev_err(&pf->pdev->dev,
11640 "Failed to enable Multicast Magic Packet wake up\n");
11641}
11642
11643/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011644 * i40e_shutdown - PCI callback for shutting down
11645 * @pdev: PCI device information struct
11646 **/
11647static void i40e_shutdown(struct pci_dev *pdev)
11648{
11649 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011650 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011651
11652 set_bit(__I40E_SUSPENDED, &pf->state);
11653 set_bit(__I40E_DOWN, &pf->state);
11654 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011655 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011656 rtnl_unlock();
11657
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011658 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11659 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11660
Catherine Sullivan02b42492015-07-10 19:35:59 -040011661 del_timer_sync(&pf->service_timer);
11662 cancel_work_sync(&pf->service_task);
11663 i40e_fdir_teardown(pf);
11664
Mitch Williams921c4672017-03-30 00:46:08 -070011665 /* Client close must be called explicitly here because the timer
11666 * has been stopped.
11667 */
11668 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
11669
Joshua Hay1d680052016-12-12 15:44:08 -080011670 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11671 i40e_enable_mc_magic_wake(pf);
11672
Catherine Sullivan02b42492015-07-10 19:35:59 -040011673 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011674 i40e_prep_for_reset(pf, true);
Catherine Sullivan02b42492015-07-10 19:35:59 -040011675 rtnl_unlock();
11676
11677 wr32(hw, I40E_PFPM_APM,
11678 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11679 wr32(hw, I40E_PFPM_WUFC,
11680 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11681
Shannon Nelsone1477582015-02-21 06:44:33 +000011682 i40e_clear_interrupt_scheme(pf);
11683
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011684 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011685 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011686 pci_set_power_state(pdev, PCI_D3hot);
11687 }
11688}
11689
11690#ifdef CONFIG_PM
11691/**
11692 * i40e_suspend - PCI callback for moving to D3
11693 * @pdev: PCI device information struct
11694 **/
11695static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11696{
11697 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011698 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011699 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011700
11701 set_bit(__I40E_SUSPENDED, &pf->state);
11702 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011703
Joshua Hay1d680052016-12-12 15:44:08 -080011704 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11705 i40e_enable_mc_magic_wake(pf);
11706
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011707 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011708 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011709 rtnl_unlock();
11710
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011711 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11712 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11713
Greg Roseb33d3b72016-05-16 10:26:37 -070011714 i40e_stop_misc_vector(pf);
11715
Greg Rose059ff692016-05-16 10:26:38 -070011716 retval = pci_save_state(pdev);
11717 if (retval)
11718 return retval;
11719
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011720 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011721 pci_set_power_state(pdev, PCI_D3hot);
11722
Greg Rose059ff692016-05-16 10:26:38 -070011723 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011724}
11725
11726/**
11727 * i40e_resume - PCI callback for waking up from D3
11728 * @pdev: PCI device information struct
11729 **/
11730static int i40e_resume(struct pci_dev *pdev)
11731{
11732 struct i40e_pf *pf = pci_get_drvdata(pdev);
11733 u32 err;
11734
11735 pci_set_power_state(pdev, PCI_D0);
11736 pci_restore_state(pdev);
11737 /* pci_restore_state() clears dev->state_saves, so
11738 * call pci_save_state() again to restore it.
11739 */
11740 pci_save_state(pdev);
11741
11742 err = pci_enable_device_mem(pdev);
11743 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011744 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011745 return err;
11746 }
11747 pci_set_master(pdev);
11748
11749 /* no wakeup events while running */
11750 pci_wake_from_d3(pdev, false);
11751
11752 /* handling the reset will rebuild the device state */
11753 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11754 clear_bit(__I40E_DOWN, &pf->state);
11755 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011756 i40e_reset_and_rebuild(pf, false, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011757 rtnl_unlock();
11758 }
11759
11760 return 0;
11761}
11762
11763#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011764static const struct pci_error_handlers i40e_err_handler = {
11765 .error_detected = i40e_pci_error_detected,
11766 .slot_reset = i40e_pci_error_slot_reset,
11767 .resume = i40e_pci_error_resume,
11768};
11769
11770static struct pci_driver i40e_driver = {
11771 .name = i40e_driver_name,
11772 .id_table = i40e_pci_tbl,
11773 .probe = i40e_probe,
11774 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011775#ifdef CONFIG_PM
11776 .suspend = i40e_suspend,
11777 .resume = i40e_resume,
11778#endif
11779 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011780 .err_handler = &i40e_err_handler,
11781 .sriov_configure = i40e_pci_sriov_configure,
11782};
11783
11784/**
11785 * i40e_init_module - Driver registration routine
11786 *
11787 * i40e_init_module is the first routine called when the driver is
11788 * loaded. All it does is register with the PCI subsystem.
11789 **/
11790static int __init i40e_init_module(void)
11791{
11792 pr_info("%s: %s - version %s\n", i40e_driver_name,
11793 i40e_driver_string, i40e_driver_version_str);
11794 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011795
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011796 /* we will see if single thread per module is enough for now,
11797 * it can't be any worse than using the system workqueue which
11798 * was already single threaded
11799 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011800 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11801 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011802 if (!i40e_wq) {
11803 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11804 return -ENOMEM;
11805 }
11806
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011807 i40e_dbg_init();
11808 return pci_register_driver(&i40e_driver);
11809}
11810module_init(i40e_init_module);
11811
11812/**
11813 * i40e_exit_module - Driver exit cleanup routine
11814 *
11815 * i40e_exit_module is called just before the driver is removed
11816 * from memory.
11817 **/
11818static void __exit i40e_exit_module(void)
11819{
11820 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011821 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011822 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011823}
11824module_exit(i40e_exit_module);