blob: 3b1dab7a7cc9aac1d17bbf9f3e2c071ce602f773 [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 *
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004443 * Wait until all queues on a given VSI have been 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++) {
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004452 /* Check and wait for the Tx queue */
Neerav Parikh69129dc2014-11-12 00:18:46 +00004453 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 }
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004460 /* Check and wait for the Tx queue */
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004461 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4462 if (ret) {
4463 dev_info(&pf->pdev->dev,
4464 "VSI seid %d Rx ring %d disable timeout\n",
4465 vsi->seid, pf_q);
4466 return ret;
4467 }
4468 }
4469
Neerav Parikh69129dc2014-11-12 00:18:46 +00004470 return 0;
4471}
4472
4473/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004474 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004475 * @pf: the PF
4476 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004477 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004478 * VSIs that are managed by this PF.
4479 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004480static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004481{
4482 int v, ret = 0;
4483
4484 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08004485 if (pf->vsi[v]) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004486 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004487 if (ret)
4488 break;
4489 }
4490 }
4491
4492 return ret;
4493}
4494
4495#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004496
4497/**
4498 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4499 * @q_idx: TX queue number
4500 * @vsi: Pointer to VSI struct
4501 *
4502 * This function checks specified queue for given VSI. Detects hung condition.
Alan Brady17daabb2017-04-05 07:50:56 -04004503 * We proactively detect hung TX queues by checking if interrupts are disabled
4504 * but there are pending descriptors. If it appears hung, attempt to recover
4505 * by triggering a SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004506 **/
4507static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4508{
4509 struct i40e_ring *tx_ring = NULL;
4510 struct i40e_pf *pf;
Alan Brady17daabb2017-04-05 07:50:56 -04004511 u32 val, tx_pending;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004512 int i;
4513
4514 pf = vsi->back;
4515
4516 /* now that we have an index, find the tx_ring struct */
4517 for (i = 0; i < vsi->num_queue_pairs; i++) {
4518 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4519 if (q_idx == vsi->tx_rings[i]->queue_index) {
4520 tx_ring = vsi->tx_rings[i];
4521 break;
4522 }
4523 }
4524 }
4525
4526 if (!tx_ring)
4527 return;
4528
4529 /* Read interrupt register */
4530 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4531 val = rd32(&pf->hw,
4532 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4533 tx_ring->vsi->base_vector - 1));
4534 else
4535 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4536
Alan Brady17daabb2017-04-05 07:50:56 -04004537 tx_pending = i40e_get_tx_pending(tx_ring);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004538
Alan Brady17daabb2017-04-05 07:50:56 -04004539 /* Interrupts are disabled and TX pending is non-zero,
4540 * trigger the SW interrupt (don't wait). Worst case
4541 * there will be one extra interrupt which may result
4542 * into not cleaning any queues because queues are cleaned.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004543 */
Alan Brady17daabb2017-04-05 07:50:56 -04004544 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4545 i40e_force_wb(vsi, tx_ring->q_vector);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004546}
4547
4548/**
4549 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4550 * @pf: pointer to PF struct
4551 *
4552 * LAN VSI has netdev and netdev has TX queues. This function is to check
4553 * each of those TX queues if they are hung, trigger recovery by issuing
4554 * SW interrupt.
4555 **/
4556static void i40e_detect_recover_hung(struct i40e_pf *pf)
4557{
4558 struct net_device *netdev;
4559 struct i40e_vsi *vsi;
4560 int i;
4561
4562 /* Only for LAN VSI */
4563 vsi = pf->vsi[pf->lan_vsi];
4564
4565 if (!vsi)
4566 return;
4567
4568 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4569 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4570 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4571 return;
4572
4573 /* Make sure type is MAIN VSI */
4574 if (vsi->type != I40E_VSI_MAIN)
4575 return;
4576
4577 netdev = vsi->netdev;
4578 if (!netdev)
4579 return;
4580
4581 /* Bail out if netif_carrier is not OK */
4582 if (!netif_carrier_ok(netdev))
4583 return;
4584
4585 /* Go thru' TX queues for netdev */
4586 for (i = 0; i < netdev->num_tx_queues; i++) {
4587 struct netdev_queue *q;
4588
4589 q = netdev_get_tx_queue(netdev, i);
4590 if (q)
4591 i40e_detect_recover_hung_queue(i, vsi);
4592 }
4593}
4594
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004595/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004596 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004597 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004598 *
4599 * Get TC map for ISCSI PF type that will include iSCSI TC
4600 * and LAN TC.
4601 **/
4602static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4603{
4604 struct i40e_dcb_app_priority_table app;
4605 struct i40e_hw *hw = &pf->hw;
4606 u8 enabled_tc = 1; /* TC0 is always enabled */
4607 u8 tc, i;
4608 /* Get the iSCSI APP TLV */
4609 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4610
4611 for (i = 0; i < dcbcfg->numapps; i++) {
4612 app = dcbcfg->app[i];
4613 if (app.selector == I40E_APP_SEL_TCPIP &&
4614 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4615 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004616 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004617 break;
4618 }
4619 }
4620
4621 return enabled_tc;
4622}
4623
4624/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004625 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4626 * @dcbcfg: the corresponding DCBx configuration structure
4627 *
4628 * Return the number of TCs from given DCBx configuration
4629 **/
4630static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4631{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004632 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004633 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004634 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004635
4636 /* Scan the ETS Config Priority Table to find
4637 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004638 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004639 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004640 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4641 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4642
4643 /* Now scan the bitmask to check for
4644 * contiguous TCs starting with TC0
4645 */
4646 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4647 if (num_tc & BIT(i)) {
4648 if (!tc_unused) {
4649 ret++;
4650 } else {
4651 pr_err("Non-contiguous TC - Disabling DCB\n");
4652 return 1;
4653 }
4654 } else {
4655 tc_unused = 1;
4656 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004657 }
4658
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004659 /* There is always at least TC0 */
4660 if (!ret)
4661 ret = 1;
4662
4663 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004664}
4665
4666/**
4667 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4668 * @dcbcfg: the corresponding DCBx configuration structure
4669 *
4670 * Query the current DCB configuration and return the number of
4671 * traffic classes enabled from the given DCBX config
4672 **/
4673static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4674{
4675 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4676 u8 enabled_tc = 1;
4677 u8 i;
4678
4679 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004680 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004681
4682 return enabled_tc;
4683}
4684
4685/**
4686 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4687 * @pf: PF being queried
4688 *
4689 * Return number of traffic classes enabled for the given PF
4690 **/
4691static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4692{
4693 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004694 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004695 u8 num_tc = 0;
4696 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4697
4698 /* If DCB is not enabled then always in single TC */
4699 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4700 return 1;
4701
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004702 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004703 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4704 return i40e_dcb_get_num_tc(dcbcfg);
4705
4706 /* MFP mode return count of enabled TCs for this PF */
4707 if (pf->hw.func_caps.iscsi)
4708 enabled_tc = i40e_get_iscsi_tc_map(pf);
4709 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004710 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004711
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004712 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004713 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004714 num_tc++;
4715 }
4716 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004717}
4718
4719/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004720 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4721 * @pf: PF being queried
4722 *
4723 * Return a bitmap for enabled traffic classes for this PF.
4724 **/
4725static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4726{
4727 /* If DCB is not enabled for this PF then just return default TC */
4728 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004729 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004730
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004731 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004732 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4733 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4734
Neerav Parikhfc51de92015-02-24 06:58:53 +00004735 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004736 if (pf->hw.func_caps.iscsi)
4737 return i40e_get_iscsi_tc_map(pf);
4738 else
David Ertmanea6acb72016-09-20 07:10:50 -07004739 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004740}
4741
4742/**
4743 * i40e_vsi_get_bw_info - Query VSI BW Information
4744 * @vsi: the VSI being queried
4745 *
4746 * Returns 0 on success, negative value on failure
4747 **/
4748static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4749{
4750 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4751 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4752 struct i40e_pf *pf = vsi->back;
4753 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004754 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004755 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004756 int i;
4757
4758 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004759 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4760 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004761 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004762 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4763 i40e_stat_str(&pf->hw, ret),
4764 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004765 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004766 }
4767
4768 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004769 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4770 NULL);
4771 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004772 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004773 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4774 i40e_stat_str(&pf->hw, ret),
4775 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004776 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004777 }
4778
4779 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4780 dev_info(&pf->pdev->dev,
4781 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4782 bw_config.tc_valid_bits,
4783 bw_ets_config.tc_valid_bits);
4784 /* Still continuing */
4785 }
4786
4787 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4788 vsi->bw_max_quanta = bw_config.max_bw;
4789 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4790 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4791 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4792 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4793 vsi->bw_ets_limit_credits[i] =
4794 le16_to_cpu(bw_ets_config.credits[i]);
4795 /* 3 bits out of 4 for each TC */
4796 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4797 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004798
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004799 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004800}
4801
4802/**
4803 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4804 * @vsi: the VSI being configured
4805 * @enabled_tc: TC bitmap
4806 * @bw_credits: BW shared credits per TC
4807 *
4808 * Returns 0 on success, negative value on failure
4809 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004810static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004811 u8 *bw_share)
4812{
4813 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004814 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004815 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004816
4817 bw_data.tc_valid_bits = enabled_tc;
4818 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4819 bw_data.tc_bw_credits[i] = bw_share[i];
4820
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004821 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4822 NULL);
4823 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004824 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004825 "AQ command Config VSI BW allocation per TC failed = %d\n",
4826 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004827 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004828 }
4829
4830 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4831 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4832
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004833 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004834}
4835
4836/**
4837 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4838 * @vsi: the VSI being configured
4839 * @enabled_tc: TC map to be enabled
4840 *
4841 **/
4842static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4843{
4844 struct net_device *netdev = vsi->netdev;
4845 struct i40e_pf *pf = vsi->back;
4846 struct i40e_hw *hw = &pf->hw;
4847 u8 netdev_tc = 0;
4848 int i;
4849 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4850
4851 if (!netdev)
4852 return;
4853
4854 if (!enabled_tc) {
4855 netdev_reset_tc(netdev);
4856 return;
4857 }
4858
4859 /* Set up actual enabled TCs on the VSI */
4860 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4861 return;
4862
4863 /* set per TC queues for the VSI */
4864 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4865 /* Only set TC queues for enabled tcs
4866 *
4867 * e.g. For a VSI that has TC0 and TC3 enabled the
4868 * enabled_tc bitmap would be 0x00001001; the driver
4869 * will set the numtc for netdev as 2 that will be
4870 * referenced by the netdev layer as TC 0 and 1.
4871 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004872 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004873 netdev_set_tc_queue(netdev,
4874 vsi->tc_config.tc_info[i].netdev_tc,
4875 vsi->tc_config.tc_info[i].qcount,
4876 vsi->tc_config.tc_info[i].qoffset);
4877 }
4878
4879 /* Assign UP2TC map for the VSI */
4880 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4881 /* Get the actual TC# for the UP */
4882 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4883 /* Get the mapped netdev TC# for the UP */
4884 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4885 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4886 }
4887}
4888
4889/**
4890 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4891 * @vsi: the VSI being configured
4892 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4893 **/
4894static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4895 struct i40e_vsi_context *ctxt)
4896{
4897 /* copy just the sections touched not the entire info
4898 * since not all sections are valid as returned by
4899 * update vsi params
4900 */
4901 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4902 memcpy(&vsi->info.queue_mapping,
4903 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4904 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4905 sizeof(vsi->info.tc_mapping));
4906}
4907
4908/**
4909 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4910 * @vsi: VSI to be configured
4911 * @enabled_tc: TC bitmap
4912 *
4913 * This configures a particular VSI for TCs that are mapped to the
4914 * given TC bitmap. It uses default bandwidth share for TCs across
4915 * VSIs to configure TC for a particular VSI.
4916 *
4917 * NOTE:
4918 * It is expected that the VSI queues have been quisced before calling
4919 * this function.
4920 **/
4921static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4922{
4923 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4924 struct i40e_vsi_context ctxt;
4925 int ret = 0;
4926 int i;
4927
4928 /* Check if enabled_tc is same as existing or new TCs */
4929 if (vsi->tc_config.enabled_tc == enabled_tc)
4930 return ret;
4931
4932 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4933 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004934 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004935 bw_share[i] = 1;
4936 }
4937
4938 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4939 if (ret) {
4940 dev_info(&vsi->back->pdev->dev,
4941 "Failed configuring TC map %d for VSI %d\n",
4942 enabled_tc, vsi->seid);
4943 goto out;
4944 }
4945
4946 /* Update Queue Pairs Mapping for currently enabled UPs */
4947 ctxt.seid = vsi->seid;
4948 ctxt.pf_num = vsi->back->hw.pf_id;
4949 ctxt.vf_num = 0;
4950 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004951 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004952 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4953
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004954 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4955 ctxt.info.valid_sections |=
4956 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4957 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4958 }
4959
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004960 /* Update the VSI after updating the VSI queue-mapping information */
4961 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4962 if (ret) {
4963 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004964 "Update vsi tc config failed, err %s aq_err %s\n",
4965 i40e_stat_str(&vsi->back->hw, ret),
4966 i40e_aq_str(&vsi->back->hw,
4967 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004968 goto out;
4969 }
4970 /* update the local VSI info with updated queue map */
4971 i40e_vsi_update_queue_map(vsi, &ctxt);
4972 vsi->info.valid_sections = 0;
4973
4974 /* Update current VSI BW information */
4975 ret = i40e_vsi_get_bw_info(vsi);
4976 if (ret) {
4977 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004978 "Failed updating vsi bw info, err %s aq_err %s\n",
4979 i40e_stat_str(&vsi->back->hw, ret),
4980 i40e_aq_str(&vsi->back->hw,
4981 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004982 goto out;
4983 }
4984
4985 /* Update the netdev TC setup */
4986 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4987out:
4988 return ret;
4989}
4990
4991/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004992 * i40e_veb_config_tc - Configure TCs for given VEB
4993 * @veb: given VEB
4994 * @enabled_tc: TC bitmap
4995 *
4996 * Configures given TC bitmap for VEB (switching) element
4997 **/
4998int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4999{
5000 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5001 struct i40e_pf *pf = veb->pf;
5002 int ret = 0;
5003 int i;
5004
5005 /* No TCs or already enabled TCs just return */
5006 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5007 return ret;
5008
5009 bw_data.tc_valid_bits = enabled_tc;
5010 /* bw_data.absolute_credits is not set (relative) */
5011
5012 /* Enable ETS TCs with equal BW Share for now */
5013 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005014 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005015 bw_data.tc_bw_share_credits[i] = 1;
5016 }
5017
5018 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5019 &bw_data, NULL);
5020 if (ret) {
5021 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005022 "VEB bw config failed, err %s aq_err %s\n",
5023 i40e_stat_str(&pf->hw, ret),
5024 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005025 goto out;
5026 }
5027
5028 /* Update the BW information */
5029 ret = i40e_veb_get_bw_info(veb);
5030 if (ret) {
5031 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005032 "Failed getting veb bw config, err %s aq_err %s\n",
5033 i40e_stat_str(&pf->hw, ret),
5034 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005035 }
5036
5037out:
5038 return ret;
5039}
5040
5041#ifdef CONFIG_I40E_DCB
5042/**
5043 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5044 * @pf: PF struct
5045 *
5046 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5047 * the caller would've quiesce all the VSIs before calling
5048 * this function
5049 **/
5050static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5051{
5052 u8 tc_map = 0;
5053 int ret;
5054 u8 v;
5055
5056 /* Enable the TCs available on PF to all VEBs */
5057 tc_map = i40e_pf_get_tc_map(pf);
5058 for (v = 0; v < I40E_MAX_VEB; v++) {
5059 if (!pf->veb[v])
5060 continue;
5061 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5062 if (ret) {
5063 dev_info(&pf->pdev->dev,
5064 "Failed configuring TC for VEB seid=%d\n",
5065 pf->veb[v]->seid);
5066 /* Will try to configure as many components */
5067 }
5068 }
5069
5070 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005071 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005072 if (!pf->vsi[v])
5073 continue;
5074
5075 /* - Enable all TCs for the LAN VSI
5076 * - For all others keep them at TC0 for now
5077 */
5078 if (v == pf->lan_vsi)
5079 tc_map = i40e_pf_get_tc_map(pf);
5080 else
David Ertmanea6acb72016-09-20 07:10:50 -07005081 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005082
5083 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5084 if (ret) {
5085 dev_info(&pf->pdev->dev,
5086 "Failed configuring TC for VSI seid=%d\n",
5087 pf->vsi[v]->seid);
5088 /* Will try to configure as many components */
5089 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005090 /* Re-configure VSI vectors based on updated TC map */
5091 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005092 if (pf->vsi[v]->netdev)
5093 i40e_dcbnl_set_all(pf->vsi[v]);
5094 }
5095 }
5096}
5097
5098/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005099 * i40e_resume_port_tx - Resume port Tx
5100 * @pf: PF struct
5101 *
5102 * Resume a port's Tx and issue a PF reset in case of failure to
5103 * resume.
5104 **/
5105static int i40e_resume_port_tx(struct i40e_pf *pf)
5106{
5107 struct i40e_hw *hw = &pf->hw;
5108 int ret;
5109
5110 ret = i40e_aq_resume_port_tx(hw, NULL);
5111 if (ret) {
5112 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005113 "Resume Port Tx failed, err %s aq_err %s\n",
5114 i40e_stat_str(&pf->hw, ret),
5115 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005116 /* Schedule PF reset to recover */
5117 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5118 i40e_service_event_schedule(pf);
5119 }
5120
5121 return ret;
5122}
5123
5124/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005125 * i40e_init_pf_dcb - Initialize DCB configuration
5126 * @pf: PF being configured
5127 *
5128 * Query the current DCB configuration and cache it
5129 * in the hardware structure
5130 **/
5131static int i40e_init_pf_dcb(struct i40e_pf *pf)
5132{
5133 struct i40e_hw *hw = &pf->hw;
5134 int err = 0;
5135
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005136 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005137 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005138 goto out;
5139
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005140 /* Get the initial DCB configuration */
5141 err = i40e_init_dcb(hw);
5142 if (!err) {
5143 /* Device/Function is not DCBX capable */
5144 if ((!hw->func_caps.dcb) ||
5145 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5146 dev_info(&pf->pdev->dev,
5147 "DCBX offload is not supported or is disabled for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005148 } else {
5149 /* When status is not DISABLED then DCBX in FW */
5150 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5151 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005152
5153 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005154 /* Enable DCB tagging only when more than one TC
5155 * or explicitly disable if only one TC
5156 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005157 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5158 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005159 else
5160 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005161 dev_dbg(&pf->pdev->dev,
5162 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005163 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005164 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005165 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005166 "Query for DCB configuration failed, err %s aq_err %s\n",
5167 i40e_stat_str(&pf->hw, err),
5168 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005169 }
5170
5171out:
5172 return err;
5173}
5174#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005175#define SPEED_SIZE 14
5176#define FC_SIZE 8
5177/**
5178 * i40e_print_link_message - print link up or down
5179 * @vsi: the VSI for which link needs a message
5180 */
Matt Jaredc156f852015-08-27 11:42:39 -04005181void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005182{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005183 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005184 char *speed = "Unknown";
5185 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005186 char *fec = "";
5187 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005188
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005189 new_speed = vsi->back->hw.phy.link_info.link_speed;
5190
5191 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005192 return;
5193 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005194 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005195 if (!isup) {
5196 netdev_info(vsi->netdev, "NIC Link is Down\n");
5197 return;
5198 }
5199
Greg Rose148c2d82014-12-11 07:06:27 +00005200 /* Warn user if link speed on NPAR enabled partition is not at
5201 * least 10GB
5202 */
5203 if (vsi->back->hw.func_caps.npar_enable &&
5204 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5205 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5206 netdev_warn(vsi->netdev,
5207 "The partition detected link speed that is less than 10Gbps\n");
5208
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005209 switch (vsi->back->hw.phy.link_info.link_speed) {
5210 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005211 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005212 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005213 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005214 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005215 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005216 case I40E_LINK_SPEED_25GB:
5217 speed = "25 G";
5218 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005219 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005220 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005221 break;
5222 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005223 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005224 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005225 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005226 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005227 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005228 default:
5229 break;
5230 }
5231
5232 switch (vsi->back->hw.fc.current_mode) {
5233 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005234 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005235 break;
5236 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005237 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005238 break;
5239 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005240 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005241 break;
5242 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005243 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005244 break;
5245 }
5246
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005247 if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
5248 fec = ", FEC: None";
5249 an = ", Autoneg: False";
5250
5251 if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
5252 an = ", Autoneg: True";
5253
5254 if (vsi->back->hw.phy.link_info.fec_info &
5255 I40E_AQ_CONFIG_FEC_KR_ENA)
5256 fec = ", FEC: CL74 FC-FEC/BASE-R";
5257 else if (vsi->back->hw.phy.link_info.fec_info &
5258 I40E_AQ_CONFIG_FEC_RS_ENA)
5259 fec = ", FEC: CL108 RS-FEC";
5260 }
5261
5262 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
5263 speed, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005264}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005265
5266/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005267 * i40e_up_complete - Finish the last steps of bringing up a connection
5268 * @vsi: the VSI being configured
5269 **/
5270static int i40e_up_complete(struct i40e_vsi *vsi)
5271{
5272 struct i40e_pf *pf = vsi->back;
5273 int err;
5274
5275 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5276 i40e_vsi_configure_msix(vsi);
5277 else
5278 i40e_configure_msi_and_legacy(vsi);
5279
5280 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005281 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005282 if (err)
5283 return err;
5284
5285 clear_bit(__I40E_DOWN, &vsi->state);
5286 i40e_napi_enable_all(vsi);
5287 i40e_vsi_enable_irq(vsi);
5288
5289 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5290 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005291 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005292 netif_tx_start_all_queues(vsi->netdev);
5293 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005294 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005295 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005296 /* need to check for qualified module here*/
5297 if ((pf->hw.phy.link_info.link_info &
5298 I40E_AQ_MEDIA_AVAILABLE) &&
5299 (!(pf->hw.phy.link_info.an_info &
5300 I40E_AQ_QUALIFIED_MODULE)))
5301 netdev_err(vsi->netdev,
5302 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005303 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005304
5305 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005306 if (vsi->type == I40E_VSI_FDIR) {
5307 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08005308 pf->fd_add_err = 0;
5309 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005310 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005311 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005312
5313 /* On the next run of the service_task, notify any clients of the new
5314 * opened netdev
5315 */
5316 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005317 i40e_service_event_schedule(pf);
5318
5319 return 0;
5320}
5321
5322/**
5323 * i40e_vsi_reinit_locked - Reset the VSI
5324 * @vsi: the VSI being configured
5325 *
5326 * Rebuild the ring structs after some configuration
5327 * has changed, e.g. MTU size.
5328 **/
5329static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5330{
5331 struct i40e_pf *pf = vsi->back;
5332
5333 WARN_ON(in_interrupt());
5334 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5335 usleep_range(1000, 2000);
5336 i40e_down(vsi);
5337
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005338 i40e_up(vsi);
5339 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5340}
5341
5342/**
5343 * i40e_up - Bring the connection back up after being down
5344 * @vsi: the VSI being configured
5345 **/
5346int i40e_up(struct i40e_vsi *vsi)
5347{
5348 int err;
5349
5350 err = i40e_vsi_configure(vsi);
5351 if (!err)
5352 err = i40e_up_complete(vsi);
5353
5354 return err;
5355}
5356
5357/**
5358 * i40e_down - Shutdown the connection processing
5359 * @vsi: the VSI being stopped
5360 **/
5361void i40e_down(struct i40e_vsi *vsi)
5362{
5363 int i;
5364
5365 /* It is assumed that the caller of this function
5366 * sets the vsi->state __I40E_DOWN bit.
5367 */
5368 if (vsi->netdev) {
5369 netif_carrier_off(vsi->netdev);
5370 netif_tx_disable(vsi->netdev);
5371 }
5372 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005373 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005374 i40e_napi_disable_all(vsi);
5375
5376 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005377 i40e_clean_tx_ring(vsi->tx_rings[i]);
5378 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005379 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005380
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005381}
5382
5383/**
5384 * i40e_setup_tc - configure multiple traffic classes
5385 * @netdev: net device to configure
5386 * @tc: number of traffic classes to enable
5387 **/
5388static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5389{
5390 struct i40e_netdev_priv *np = netdev_priv(netdev);
5391 struct i40e_vsi *vsi = np->vsi;
5392 struct i40e_pf *pf = vsi->back;
5393 u8 enabled_tc = 0;
5394 int ret = -EINVAL;
5395 int i;
5396
5397 /* Check if DCB enabled to continue */
5398 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5399 netdev_info(netdev, "DCB is not enabled for adapter\n");
5400 goto exit;
5401 }
5402
5403 /* Check if MFP enabled */
5404 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5405 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5406 goto exit;
5407 }
5408
5409 /* Check whether tc count is within enabled limit */
5410 if (tc > i40e_pf_get_num_tc(pf)) {
5411 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5412 goto exit;
5413 }
5414
5415 /* Generate TC map for number of tc requested */
5416 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005417 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005418
5419 /* Requesting same TC configuration as already enabled */
5420 if (enabled_tc == vsi->tc_config.enabled_tc)
5421 return 0;
5422
5423 /* Quiesce VSI queues */
5424 i40e_quiesce_vsi(vsi);
5425
5426 /* Configure VSI for enabled TCs */
5427 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5428 if (ret) {
5429 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5430 vsi->seid);
5431 goto exit;
5432 }
5433
5434 /* Unquiesce VSI */
5435 i40e_unquiesce_vsi(vsi);
5436
5437exit:
5438 return ret;
5439}
5440
John Fastabend16e5cc62016-02-16 21:16:43 -08005441static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5442 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005443{
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005444 if (tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005445 return -EINVAL;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005446
5447 tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
5448
5449 return i40e_setup_tc(netdev, tc->mqprio->num_tc);
John Fastabende4c67342016-02-16 21:16:15 -08005450}
5451
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005452/**
5453 * i40e_open - Called when a network interface is made active
5454 * @netdev: network interface device structure
5455 *
5456 * The open entry point is called when a network interface is made
5457 * active by the system (IFF_UP). At this point all resources needed
5458 * for transmit and receive operations are allocated, the interrupt
5459 * handler is registered with the OS, the netdev watchdog subtask is
5460 * enabled, and the stack is notified that the interface is ready.
5461 *
5462 * Returns 0 on success, negative value on failure
5463 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005464int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005465{
5466 struct i40e_netdev_priv *np = netdev_priv(netdev);
5467 struct i40e_vsi *vsi = np->vsi;
5468 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005469 int err;
5470
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005471 /* disallow open during test or if eeprom is broken */
5472 if (test_bit(__I40E_TESTING, &pf->state) ||
5473 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005474 return -EBUSY;
5475
5476 netif_carrier_off(netdev);
5477
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005478 err = i40e_vsi_open(vsi);
5479 if (err)
5480 return err;
5481
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005482 /* configure global TSO hardware offload settings */
5483 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5484 TCP_FLAG_FIN) >> 16);
5485 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5486 TCP_FLAG_FIN |
5487 TCP_FLAG_CWR) >> 16);
5488 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5489
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005490 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005491
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005492 return 0;
5493}
5494
5495/**
5496 * i40e_vsi_open -
5497 * @vsi: the VSI to open
5498 *
5499 * Finish initialization of the VSI.
5500 *
5501 * Returns 0 on success, negative value on failure
Maciej Sosin373149f2017-04-05 07:50:55 -04005502 *
5503 * Note: expects to be called while under rtnl_lock()
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005504 **/
5505int i40e_vsi_open(struct i40e_vsi *vsi)
5506{
5507 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005508 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005509 int err;
5510
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005511 /* allocate descriptors */
5512 err = i40e_vsi_setup_tx_resources(vsi);
5513 if (err)
5514 goto err_setup_tx;
5515 err = i40e_vsi_setup_rx_resources(vsi);
5516 if (err)
5517 goto err_setup_rx;
5518
5519 err = i40e_vsi_configure(vsi);
5520 if (err)
5521 goto err_setup_rx;
5522
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005523 if (vsi->netdev) {
5524 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5525 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5526 err = i40e_vsi_request_irq(vsi, int_name);
5527 if (err)
5528 goto err_setup_rx;
5529
5530 /* Notify the stack of the actual queue counts. */
5531 err = netif_set_real_num_tx_queues(vsi->netdev,
5532 vsi->num_queue_pairs);
5533 if (err)
5534 goto err_set_queues;
5535
5536 err = netif_set_real_num_rx_queues(vsi->netdev,
5537 vsi->num_queue_pairs);
5538 if (err)
5539 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005540
5541 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005542 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005543 dev_driver_string(&pf->pdev->dev),
5544 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005545 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005546
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005547 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005548 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005549 goto err_setup_rx;
5550 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005551
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005552 err = i40e_up_complete(vsi);
5553 if (err)
5554 goto err_up_complete;
5555
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005556 return 0;
5557
5558err_up_complete:
5559 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005560err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005561 i40e_vsi_free_irq(vsi);
5562err_setup_rx:
5563 i40e_vsi_free_rx_resources(vsi);
5564err_setup_tx:
5565 i40e_vsi_free_tx_resources(vsi);
5566 if (vsi == pf->vsi[pf->lan_vsi])
Maciej Sosin373149f2017-04-05 07:50:55 -04005567 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005568
5569 return err;
5570}
5571
5572/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005573 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005574 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005575 *
5576 * This function destroys the hlist where all the Flow Director
5577 * filters were saved.
5578 **/
5579static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5580{
5581 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08005582 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005583 struct hlist_node *node2;
5584
5585 hlist_for_each_entry_safe(filter, node2,
5586 &pf->fdir_filter_list, fdir_node) {
5587 hlist_del(&filter->fdir_node);
5588 kfree(filter);
5589 }
Jacob Keller097dbf52017-02-06 14:38:46 -08005590
Jacob Keller0e588de2017-02-06 14:38:50 -08005591 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
5592 list_del(&pit_entry->list);
5593 kfree(pit_entry);
5594 }
5595 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
5596
5597 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
5598 list_del(&pit_entry->list);
5599 kfree(pit_entry);
5600 }
5601 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
5602
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005603 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005604 pf->fd_tcp4_filter_cnt = 0;
5605 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08005606 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005607 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08005608
5609 /* Reprogram the default input set for TCP/IPv4 */
5610 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
5611 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5612 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5613
5614 /* Reprogram the default input set for UDP/IPv4 */
5615 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
5616 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5617 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5618
5619 /* Reprogram the default input set for SCTP/IPv4 */
5620 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
5621 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5622 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5623
5624 /* Reprogram the default input set for Other/IPv4 */
5625 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
5626 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005627}
5628
5629/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005630 * i40e_close - Disables a network interface
5631 * @netdev: network interface device structure
5632 *
5633 * The close entry point is called when an interface is de-activated
5634 * by the OS. The hardware is still under the driver's control, but
5635 * this netdev interface is disabled.
5636 *
5637 * Returns 0, this is not allowed to fail
5638 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005639int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005640{
5641 struct i40e_netdev_priv *np = netdev_priv(netdev);
5642 struct i40e_vsi *vsi = np->vsi;
5643
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005644 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005645
5646 return 0;
5647}
5648
5649/**
5650 * i40e_do_reset - Start a PF or Core Reset sequence
5651 * @pf: board private structure
5652 * @reset_flags: which reset is requested
Maciej Sosin373149f2017-04-05 07:50:55 -04005653 * @lock_acquired: indicates whether or not the lock has been acquired
5654 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005655 *
5656 * The essential difference in resets is that the PF Reset
5657 * doesn't clear the packet buffers, doesn't reset the PE
5658 * firmware, and doesn't bother the other PFs on the chip.
5659 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04005660void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005661{
5662 u32 val;
5663
5664 WARN_ON(in_interrupt());
5665
Mitch Williams263fc482014-04-23 04:50:11 +00005666
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005667 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005668 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005669
5670 /* Request a Global Reset
5671 *
5672 * This will start the chip's countdown to the actual full
5673 * chip reset event, and a warning interrupt to be sent
5674 * to all PFs, including the requestor. Our handler
5675 * for the warning interrupt will deal with the shutdown
5676 * and recovery of the switch setup.
5677 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005678 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005679 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5680 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5681 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5682
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005683 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005684
5685 /* Request a Core Reset
5686 *
5687 * Same as Global Reset, except does *not* include the MAC/PHY
5688 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005689 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005690 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5691 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5692 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5693 i40e_flush(&pf->hw);
5694
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005695 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005696
5697 /* Request a PF Reset
5698 *
5699 * Resets only the PF-specific registers
5700 *
5701 * This goes directly to the tear-down and rebuild of
5702 * the switch, since we need to do all the recovery as
5703 * for the Core Reset.
5704 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005705 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Maciej Sosin373149f2017-04-05 07:50:55 -04005706 i40e_handle_reset_warning(pf, lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005707
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005708 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005709 int v;
5710
5711 /* Find the VSI(s) that requested a re-init */
5712 dev_info(&pf->pdev->dev,
5713 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005714 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005715 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005716
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005717 if (vsi != NULL &&
5718 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5719 i40e_vsi_reinit_locked(pf->vsi[v]);
5720 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5721 }
5722 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005723 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005724 int v;
5725
5726 /* Find the VSI(s) that needs to be brought down */
5727 dev_info(&pf->pdev->dev, "VSI down requested\n");
5728 for (v = 0; v < pf->num_alloc_vsi; v++) {
5729 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005730
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005731 if (vsi != NULL &&
5732 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5733 set_bit(__I40E_DOWN, &vsi->state);
5734 i40e_down(vsi);
5735 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5736 }
5737 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005738 } else {
5739 dev_info(&pf->pdev->dev,
5740 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005741 }
5742}
5743
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005744#ifdef CONFIG_I40E_DCB
5745/**
5746 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5747 * @pf: board private structure
5748 * @old_cfg: current DCB config
5749 * @new_cfg: new DCB config
5750 **/
5751bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5752 struct i40e_dcbx_config *old_cfg,
5753 struct i40e_dcbx_config *new_cfg)
5754{
5755 bool need_reconfig = false;
5756
5757 /* Check if ETS configuration has changed */
5758 if (memcmp(&new_cfg->etscfg,
5759 &old_cfg->etscfg,
5760 sizeof(new_cfg->etscfg))) {
5761 /* If Priority Table has changed reconfig is needed */
5762 if (memcmp(&new_cfg->etscfg.prioritytable,
5763 &old_cfg->etscfg.prioritytable,
5764 sizeof(new_cfg->etscfg.prioritytable))) {
5765 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005766 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005767 }
5768
5769 if (memcmp(&new_cfg->etscfg.tcbwtable,
5770 &old_cfg->etscfg.tcbwtable,
5771 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005772 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005773
5774 if (memcmp(&new_cfg->etscfg.tsatable,
5775 &old_cfg->etscfg.tsatable,
5776 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005777 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005778 }
5779
5780 /* Check if PFC configuration has changed */
5781 if (memcmp(&new_cfg->pfc,
5782 &old_cfg->pfc,
5783 sizeof(new_cfg->pfc))) {
5784 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005785 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005786 }
5787
5788 /* Check if APP Table has changed */
5789 if (memcmp(&new_cfg->app,
5790 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005791 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005792 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005793 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005794 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005795
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005796 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005797 return need_reconfig;
5798}
5799
5800/**
5801 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5802 * @pf: board private structure
5803 * @e: event info posted on ARQ
5804 **/
5805static int i40e_handle_lldp_event(struct i40e_pf *pf,
5806 struct i40e_arq_event_info *e)
5807{
5808 struct i40e_aqc_lldp_get_mib *mib =
5809 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5810 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005811 struct i40e_dcbx_config tmp_dcbx_cfg;
5812 bool need_reconfig = false;
5813 int ret = 0;
5814 u8 type;
5815
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005816 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005817 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005818 return ret;
5819
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005820 /* Ignore if event is not for Nearest Bridge */
5821 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5822 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005823 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005824 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5825 return ret;
5826
5827 /* Check MIB Type and return if event for Remote MIB update */
5828 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005829 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005830 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005831 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5832 /* Update the remote cached instance and return */
5833 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5834 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5835 &hw->remote_dcbx_config);
5836 goto exit;
5837 }
5838
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005839 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005840 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005841
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005842 /* Reset the old DCBx configuration data */
5843 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005844 /* Get updated DCBX data from firmware */
5845 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005846 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005847 dev_info(&pf->pdev->dev,
5848 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5849 i40e_stat_str(&pf->hw, ret),
5850 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005851 goto exit;
5852 }
5853
5854 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005855 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5856 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005857 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005858 goto exit;
5859 }
5860
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005861 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5862 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005863
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005864 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005865
5866 if (!need_reconfig)
5867 goto exit;
5868
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005869 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005870 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005871 pf->flags |= I40E_FLAG_DCB_ENABLED;
5872 else
5873 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5874
Neerav Parikh69129dc2014-11-12 00:18:46 +00005875 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005876 /* Reconfiguration needed quiesce all VSIs */
5877 i40e_pf_quiesce_all_vsi(pf);
5878
5879 /* Changes in configuration update VEB/VSI */
5880 i40e_dcb_reconfigure(pf);
5881
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005882 ret = i40e_resume_port_tx(pf);
5883
Neerav Parikh69129dc2014-11-12 00:18:46 +00005884 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005885 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005886 if (ret)
5887 goto exit;
5888
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005889 /* Wait for the PF's queues to be disabled */
5890 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005891 if (ret) {
5892 /* Schedule PF reset to recover */
5893 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5894 i40e_service_event_schedule(pf);
5895 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005896 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08005897 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
5898 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005899 }
5900
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005901exit:
5902 return ret;
5903}
5904#endif /* CONFIG_I40E_DCB */
5905
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005906/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005907 * i40e_do_reset_safe - Protected reset path for userland calls.
5908 * @pf: board private structure
5909 * @reset_flags: which reset is requested
5910 *
5911 **/
5912void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5913{
5914 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -04005915 i40e_do_reset(pf, reset_flags, true);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005916 rtnl_unlock();
5917}
5918
5919/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005920 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5921 * @pf: board private structure
5922 * @e: event info posted on ARQ
5923 *
5924 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5925 * and VF queues
5926 **/
5927static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5928 struct i40e_arq_event_info *e)
5929{
5930 struct i40e_aqc_lan_overflow *data =
5931 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5932 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5933 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5934 struct i40e_hw *hw = &pf->hw;
5935 struct i40e_vf *vf;
5936 u16 vf_id;
5937
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005938 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5939 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005940
5941 /* Queue belongs to VF, find the VF and issue VF reset */
5942 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5943 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5944 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5945 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5946 vf_id -= hw->func_caps.vf_base_id;
5947 vf = &pf->vf[vf_id];
5948 i40e_vc_notify_vf_reset(vf);
5949 /* Allow VF to process pending reset notification */
5950 msleep(20);
5951 i40e_reset_vf(vf, false);
5952 }
5953}
5954
5955/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005956 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5957 * @pf: board private structure
5958 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005959u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005960{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005961 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005962
5963 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5964 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5965 return fcnt_prog;
5966}
5967
5968/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005969 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005970 * @pf: board private structure
5971 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005972u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005973{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005974 u32 val, fcnt_prog;
5975
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005976 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5977 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5978 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5979 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5980 return fcnt_prog;
5981}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005982
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005983/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005984 * i40e_get_global_fd_count - Get total FD filters programmed on device
5985 * @pf: board private structure
5986 **/
5987u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5988{
5989 u32 val, fcnt_prog;
5990
5991 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5992 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5993 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5994 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5995 return fcnt_prog;
5996}
5997
5998/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005999 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6000 * @pf: board private structure
6001 **/
6002void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6003{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006004 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006005 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006006 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006007
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006008 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
6009 return;
6010
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006011 /* Check if, FD SB or ATR was auto disabled and if there is enough room
6012 * to re-enable
6013 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006014 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006015 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006016 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6017 (pf->fd_add_err == 0) ||
6018 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006019 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006020 (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)) {
6021 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006022 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6023 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 +00006024 }
6025 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006026
6027 /* Wait for some more space to be available to turn on ATR. We also
6028 * must check that no existing ntuple rules for TCP are in effect
6029 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006030 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
6031 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006032 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller097dbf52017-02-06 14:38:46 -08006033 (pf->fd_tcp4_filter_cnt == 0)) {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006034 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006035 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07006036 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 +00006037 }
6038 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006039
6040 /* if hw had a problem adding a filter, delete it */
6041 if (pf->fd_inv > 0) {
6042 hlist_for_each_entry_safe(filter, node,
6043 &pf->fdir_filter_list, fdir_node) {
6044 if (filter->fd_id == pf->fd_inv) {
6045 hlist_del(&filter->fdir_node);
6046 kfree(filter);
6047 pf->fdir_pf_active_filters--;
6048 }
6049 }
6050 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006051}
6052
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006053#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006054#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006055/**
6056 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6057 * @pf: board private structure
6058 **/
6059static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6060{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006061 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006062 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006063 bool disable_atr = false;
6064 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006065 int reg;
6066
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006067 if (!time_after(jiffies, pf->fd_flush_timestamp +
6068 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6069 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006070
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006071 /* If the flush is happening too quick and we have mostly SB rules we
6072 * should not re-enable ATR for some time.
6073 */
6074 min_flush_time = pf->fd_flush_timestamp +
6075 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6076 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006077
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006078 if (!(time_after(jiffies, min_flush_time)) &&
6079 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6080 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6081 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6082 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006083 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006084
6085 pf->fd_flush_timestamp = jiffies;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006086 pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006087 /* flush all filters */
6088 wr32(&pf->hw, I40E_PFQF_CTL_1,
6089 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6090 i40e_flush(&pf->hw);
6091 pf->fd_flush_cnt++;
6092 pf->fd_add_err = 0;
6093 do {
6094 /* Check FD flush status every 5-6msec */
6095 usleep_range(5000, 6000);
6096 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6097 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6098 break;
6099 } while (flush_wait_retry--);
6100 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6101 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6102 } else {
6103 /* replay sideband filters */
6104 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08006105 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006106 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006107 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6108 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6109 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6110 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006111}
6112
6113/**
6114 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6115 * @pf: board private structure
6116 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006117u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006118{
6119 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6120}
6121
6122/* We can see up to 256 filter programming desc in transit if the filters are
6123 * being applied really fast; before we see the first
6124 * filter miss error on Rx queue 0. Accumulating enough error messages before
6125 * reacting will make sure we don't cause flush too often.
6126 */
6127#define I40E_MAX_FD_PROGRAM_ERROR 256
6128
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006129/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006130 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6131 * @pf: board private structure
6132 **/
6133static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6134{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006135
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006136 /* if interface is down do nothing */
6137 if (test_bit(__I40E_DOWN, &pf->state))
6138 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006139
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006140 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006141 i40e_fdir_flush_and_replay(pf);
6142
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006143 i40e_fdir_check_and_reenable(pf);
6144
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006145}
6146
6147/**
6148 * i40e_vsi_link_event - notify VSI of a link event
6149 * @vsi: vsi to be notified
6150 * @link_up: link up or down
6151 **/
6152static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6153{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006154 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006155 return;
6156
6157 switch (vsi->type) {
6158 case I40E_VSI_MAIN:
6159 if (!vsi->netdev || !vsi->netdev_registered)
6160 break;
6161
6162 if (link_up) {
6163 netif_carrier_on(vsi->netdev);
6164 netif_tx_wake_all_queues(vsi->netdev);
6165 } else {
6166 netif_carrier_off(vsi->netdev);
6167 netif_tx_stop_all_queues(vsi->netdev);
6168 }
6169 break;
6170
6171 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006172 case I40E_VSI_VMDQ2:
6173 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006174 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006175 case I40E_VSI_MIRROR:
6176 default:
6177 /* there is no notification for other VSIs */
6178 break;
6179 }
6180}
6181
6182/**
6183 * i40e_veb_link_event - notify elements on the veb of a link event
6184 * @veb: veb to be notified
6185 * @link_up: link up or down
6186 **/
6187static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6188{
6189 struct i40e_pf *pf;
6190 int i;
6191
6192 if (!veb || !veb->pf)
6193 return;
6194 pf = veb->pf;
6195
6196 /* depth first... */
6197 for (i = 0; i < I40E_MAX_VEB; i++)
6198 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6199 i40e_veb_link_event(pf->veb[i], link_up);
6200
6201 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006202 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006203 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6204 i40e_vsi_link_event(pf->vsi[i], link_up);
6205}
6206
6207/**
6208 * i40e_link_event - Update netif_carrier status
6209 * @pf: board private structure
6210 **/
6211static void i40e_link_event(struct i40e_pf *pf)
6212{
Mitch Williams320684c2014-10-17 03:14:43 +00006213 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006214 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006215 i40e_status status;
6216 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006217
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006218 /* save off old link status information */
6219 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6220
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006221 /* set this to force the get_link_status call to refresh state */
6222 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006223
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006224 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006225
6226 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006227
6228 /* On success, disable temp link polling */
6229 if (status == I40E_SUCCESS) {
6230 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
6231 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
6232 } else {
6233 /* Enable link polling temporarily until i40e_get_link_status
6234 * returns I40E_SUCCESS
6235 */
6236 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006237 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6238 status);
6239 return;
6240 }
6241
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006242 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6243 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006244
6245 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006246 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006247 (test_bit(__I40E_DOWN, &vsi->state) ||
6248 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006249 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006250
6251 if (!test_bit(__I40E_DOWN, &vsi->state))
6252 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006253
6254 /* Notify the base of the switch tree connected to
6255 * the link. Floating VEBs are not notified.
6256 */
6257 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6258 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6259 else
Mitch Williams320684c2014-10-17 03:14:43 +00006260 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006261
6262 if (pf->vf)
6263 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006264
6265 if (pf->flags & I40E_FLAG_PTP)
6266 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006267}
6268
6269/**
Shannon Nelson21536712014-10-25 10:35:25 +00006270 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006271 * @pf: board private structure
6272 **/
6273static void i40e_watchdog_subtask(struct i40e_pf *pf)
6274{
6275 int i;
6276
6277 /* if interface is down do nothing */
6278 if (test_bit(__I40E_DOWN, &pf->state) ||
6279 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6280 return;
6281
Shannon Nelson21536712014-10-25 10:35:25 +00006282 /* make sure we don't do these things too often */
6283 if (time_before(jiffies, (pf->service_timer_previous +
6284 pf->service_timer_period)))
6285 return;
6286 pf->service_timer_previous = jiffies;
6287
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006288 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
6289 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04006290 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006291
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006292 /* Update the stats for active netdevs so the network stack
6293 * can look at updated numbers whenever it cares to
6294 */
Mitch Williams505682c2014-05-20 08:01:37 +00006295 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006296 if (pf->vsi[i] && pf->vsi[i]->netdev)
6297 i40e_update_stats(pf->vsi[i]);
6298
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006299 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6300 /* Update the stats for the active switching components */
6301 for (i = 0; i < I40E_MAX_VEB; i++)
6302 if (pf->veb[i])
6303 i40e_update_veb_stats(pf->veb[i]);
6304 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006305
6306 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006307}
6308
6309/**
6310 * i40e_reset_subtask - Set up for resetting the device and driver
6311 * @pf: board private structure
6312 **/
6313static void i40e_reset_subtask(struct i40e_pf *pf)
6314{
6315 u32 reset_flags = 0;
6316
6317 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006318 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006319 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6320 }
6321 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006322 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006323 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6324 }
6325 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006326 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006327 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6328 }
6329 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006330 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006331 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6332 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006333 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006334 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006335 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6336 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006337
6338 /* If there's a recovery already waiting, it takes
6339 * precedence before starting a new reset sequence.
6340 */
6341 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
Maciej Sosin373149f2017-04-05 07:50:55 -04006342 i40e_prep_for_reset(pf, false);
6343 i40e_reset(pf);
6344 i40e_rebuild(pf, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006345 }
6346
6347 /* If we're already down or resetting, just bail */
6348 if (reset_flags &&
6349 !test_bit(__I40E_DOWN, &pf->state) &&
Maciej Sosin373149f2017-04-05 07:50:55 -04006350 !test_bit(__I40E_CONFIG_BUSY, &pf->state)) {
6351 rtnl_lock();
6352 i40e_do_reset(pf, reset_flags, true);
6353 rtnl_unlock();
6354 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006355}
6356
6357/**
6358 * i40e_handle_link_event - Handle link event
6359 * @pf: board private structure
6360 * @e: event info posted on ARQ
6361 **/
6362static void i40e_handle_link_event(struct i40e_pf *pf,
6363 struct i40e_arq_event_info *e)
6364{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006365 struct i40e_aqc_get_link_status *status =
6366 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006367
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006368 /* Do a new status request to re-enable LSE reporting
6369 * and load new status information into the hw struct
6370 * This completely ignores any state information
6371 * in the ARQ event info, instead choosing to always
6372 * issue the AQ update link status command.
6373 */
6374 i40e_link_event(pf);
6375
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006376 /* check for unqualified module, if link is down */
6377 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6378 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6379 (!(status->link_info & I40E_AQ_LINK_UP)))
6380 dev_err(&pf->pdev->dev,
6381 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006382}
6383
6384/**
6385 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6386 * @pf: board private structure
6387 **/
6388static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6389{
6390 struct i40e_arq_event_info event;
6391 struct i40e_hw *hw = &pf->hw;
6392 u16 pending, i = 0;
6393 i40e_status ret;
6394 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006395 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006396 u32 val;
6397
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006398 /* Do not run clean AQ when PF reset fails */
6399 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6400 return;
6401
Shannon Nelson86df2422014-05-20 08:01:35 +00006402 /* check for error indications */
6403 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6404 oldval = val;
6405 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006406 if (hw->debug_mask & I40E_DEBUG_AQ)
6407 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006408 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6409 }
6410 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006411 if (hw->debug_mask & I40E_DEBUG_AQ)
6412 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006413 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006414 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006415 }
6416 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006417 if (hw->debug_mask & I40E_DEBUG_AQ)
6418 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006419 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6420 }
6421 if (oldval != val)
6422 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6423
6424 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6425 oldval = val;
6426 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006427 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6428 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006429 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6430 }
6431 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006432 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6433 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006434 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6435 }
6436 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006437 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6438 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006439 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6440 }
6441 if (oldval != val)
6442 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6443
Mitch Williams1001dc32014-11-11 20:02:19 +00006444 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6445 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006446 if (!event.msg_buf)
6447 return;
6448
6449 do {
6450 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006451 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006452 break;
Mitch Williams56497972014-06-04 08:45:18 +00006453 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006454 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6455 break;
6456 }
6457
6458 opcode = le16_to_cpu(event.desc.opcode);
6459 switch (opcode) {
6460
6461 case i40e_aqc_opc_get_link_status:
6462 i40e_handle_link_event(pf, &event);
6463 break;
6464 case i40e_aqc_opc_send_msg_to_pf:
6465 ret = i40e_vc_process_vf_msg(pf,
6466 le16_to_cpu(event.desc.retval),
6467 le32_to_cpu(event.desc.cookie_high),
6468 le32_to_cpu(event.desc.cookie_low),
6469 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006470 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006471 break;
6472 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006473 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006474#ifdef CONFIG_I40E_DCB
6475 rtnl_lock();
6476 ret = i40e_handle_lldp_event(pf, &event);
6477 rtnl_unlock();
6478#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006479 break;
6480 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006481 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006482 i40e_handle_lan_overflow_event(pf, &event);
6483 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006484 case i40e_aqc_opc_send_msg_to_peer:
6485 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6486 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006487 case i40e_aqc_opc_nvm_erase:
6488 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006489 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006490 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6491 "ARQ NVM operation 0x%04x completed\n",
6492 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006493 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006494 default:
6495 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006496 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006497 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006498 break;
6499 }
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006500 } while (i++ < pf->adminq_work_limit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006501
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006502 if (i < pf->adminq_work_limit)
6503 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6504
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006505 /* re-enable Admin queue interrupt cause */
6506 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6507 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6508 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6509 i40e_flush(hw);
6510
6511 kfree(event.msg_buf);
6512}
6513
6514/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006515 * i40e_verify_eeprom - make sure eeprom is good to use
6516 * @pf: board private structure
6517 **/
6518static void i40e_verify_eeprom(struct i40e_pf *pf)
6519{
6520 int err;
6521
6522 err = i40e_diag_eeprom_test(&pf->hw);
6523 if (err) {
6524 /* retry in case of garbage read */
6525 err = i40e_diag_eeprom_test(&pf->hw);
6526 if (err) {
6527 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6528 err);
6529 set_bit(__I40E_BAD_EEPROM, &pf->state);
6530 }
6531 }
6532
6533 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6534 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6535 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6536 }
6537}
6538
6539/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006540 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006541 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006542 *
6543 * enable switch loop back or die - no point in a return value
6544 **/
6545static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6546{
6547 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6548 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006549 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006550
6551 ctxt.seid = pf->main_vsi_seid;
6552 ctxt.pf_num = pf->hw.pf_id;
6553 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006554 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6555 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006556 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006557 "couldn't get PF vsi config, err %s aq_err %s\n",
6558 i40e_stat_str(&pf->hw, ret),
6559 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006560 return;
6561 }
6562 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6563 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6564 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6565
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006566 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6567 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006568 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006569 "update vsi switch failed, err %s aq_err %s\n",
6570 i40e_stat_str(&pf->hw, ret),
6571 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006572 }
6573}
6574
6575/**
6576 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006577 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006578 *
6579 * disable switch loop back or die - no point in a return value
6580 **/
6581static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6582{
6583 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6584 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006585 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006586
6587 ctxt.seid = pf->main_vsi_seid;
6588 ctxt.pf_num = pf->hw.pf_id;
6589 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006590 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6591 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006592 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006593 "couldn't get PF vsi config, err %s aq_err %s\n",
6594 i40e_stat_str(&pf->hw, ret),
6595 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006596 return;
6597 }
6598 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6599 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6600 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6601
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006602 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6603 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006604 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006605 "update vsi switch failed, err %s aq_err %s\n",
6606 i40e_stat_str(&pf->hw, ret),
6607 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006608 }
6609}
6610
6611/**
Neerav Parikh51616012015-02-06 08:52:14 +00006612 * i40e_config_bridge_mode - Configure the HW bridge mode
6613 * @veb: pointer to the bridge instance
6614 *
6615 * Configure the loop back mode for the LAN VSI that is downlink to the
6616 * specified HW bridge instance. It is expected this function is called
6617 * when a new HW bridge is instantiated.
6618 **/
6619static void i40e_config_bridge_mode(struct i40e_veb *veb)
6620{
6621 struct i40e_pf *pf = veb->pf;
6622
Shannon Nelson6dec1012015-09-28 14:12:30 -04006623 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6624 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6625 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006626 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6627 i40e_disable_pf_switch_lb(pf);
6628 else
6629 i40e_enable_pf_switch_lb(pf);
6630}
6631
6632/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006633 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6634 * @veb: pointer to the VEB instance
6635 *
6636 * This is a recursive function that first builds the attached VSIs then
6637 * recurses in to build the next layer of VEB. We track the connections
6638 * through our own index numbers because the seid's from the HW could
6639 * change across the reset.
6640 **/
6641static int i40e_reconstitute_veb(struct i40e_veb *veb)
6642{
6643 struct i40e_vsi *ctl_vsi = NULL;
6644 struct i40e_pf *pf = veb->pf;
6645 int v, veb_idx;
6646 int ret;
6647
6648 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006649 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006650 if (pf->vsi[v] &&
6651 pf->vsi[v]->veb_idx == veb->idx &&
6652 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6653 ctl_vsi = pf->vsi[v];
6654 break;
6655 }
6656 }
6657 if (!ctl_vsi) {
6658 dev_info(&pf->pdev->dev,
6659 "missing owner VSI for veb_idx %d\n", veb->idx);
6660 ret = -ENOENT;
6661 goto end_reconstitute;
6662 }
6663 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6664 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6665 ret = i40e_add_vsi(ctl_vsi);
6666 if (ret) {
6667 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006668 "rebuild of veb_idx %d owner VSI failed: %d\n",
6669 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006670 goto end_reconstitute;
6671 }
6672 i40e_vsi_reset_stats(ctl_vsi);
6673
6674 /* create the VEB in the switch and move the VSI onto the VEB */
6675 ret = i40e_add_veb(veb, ctl_vsi);
6676 if (ret)
6677 goto end_reconstitute;
6678
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006679 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6680 veb->bridge_mode = BRIDGE_MODE_VEB;
6681 else
6682 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006683 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006684
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006685 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006686 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006687 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6688 continue;
6689
6690 if (pf->vsi[v]->veb_idx == veb->idx) {
6691 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006692
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006693 vsi->uplink_seid = veb->seid;
6694 ret = i40e_add_vsi(vsi);
6695 if (ret) {
6696 dev_info(&pf->pdev->dev,
6697 "rebuild of vsi_idx %d failed: %d\n",
6698 v, ret);
6699 goto end_reconstitute;
6700 }
6701 i40e_vsi_reset_stats(vsi);
6702 }
6703 }
6704
6705 /* create any VEBs attached to this VEB - RECURSION */
6706 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6707 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6708 pf->veb[veb_idx]->uplink_seid = veb->seid;
6709 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6710 if (ret)
6711 break;
6712 }
6713 }
6714
6715end_reconstitute:
6716 return ret;
6717}
6718
6719/**
6720 * i40e_get_capabilities - get info about the HW
6721 * @pf: the PF struct
6722 **/
6723static int i40e_get_capabilities(struct i40e_pf *pf)
6724{
6725 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6726 u16 data_size;
6727 int buf_len;
6728 int err;
6729
6730 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6731 do {
6732 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6733 if (!cap_buf)
6734 return -ENOMEM;
6735
6736 /* this loads the data into the hw struct for us */
6737 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6738 &data_size,
6739 i40e_aqc_opc_list_func_capabilities,
6740 NULL);
6741 /* data loaded, buffer no longer needed */
6742 kfree(cap_buf);
6743
6744 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6745 /* retry with a larger buffer */
6746 buf_len = data_size;
6747 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6748 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006749 "capability discovery failed, err %s aq_err %s\n",
6750 i40e_stat_str(&pf->hw, err),
6751 i40e_aq_str(&pf->hw,
6752 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006753 return -ENODEV;
6754 }
6755 } while (err);
6756
6757 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6758 dev_info(&pf->pdev->dev,
6759 "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",
6760 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6761 pf->hw.func_caps.num_msix_vectors,
6762 pf->hw.func_caps.num_msix_vectors_vf,
6763 pf->hw.func_caps.fd_filters_guaranteed,
6764 pf->hw.func_caps.fd_filters_best_effort,
6765 pf->hw.func_caps.num_tx_qp,
6766 pf->hw.func_caps.num_vsis);
6767
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006768#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6769 + pf->hw.func_caps.num_vfs)
6770 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6771 dev_info(&pf->pdev->dev,
6772 "got num_vsis %d, setting num_vsis to %d\n",
6773 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6774 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6775 }
6776
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006777 return 0;
6778}
6779
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006780static int i40e_vsi_clear(struct i40e_vsi *vsi);
6781
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006782/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006783 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006784 * @pf: board private structure
6785 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006786static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006787{
6788 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006789
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006790 /* quick workaround for an NVM issue that leaves a critical register
6791 * uninitialized
6792 */
6793 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6794 static const u32 hkey[] = {
6795 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6796 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6797 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6798 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006799 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006800
6801 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6802 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6803 }
6804
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006805 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006806 return;
6807
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006808 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006809 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006810
6811 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006812 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006813 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6814 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006815 if (!vsi) {
6816 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006817 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6818 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006819 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006820 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006821
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006822 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006823}
6824
6825/**
6826 * i40e_fdir_teardown - release the Flow Director resources
6827 * @pf: board private structure
6828 **/
6829static void i40e_fdir_teardown(struct i40e_pf *pf)
6830{
Alexander Duyck4b816442016-10-11 15:26:53 -07006831 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006832
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006833 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07006834 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6835 if (vsi)
6836 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006837}
6838
6839/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006840 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006841 * @pf: board private structure
Maciej Sosin373149f2017-04-05 07:50:55 -04006842 * @lock_acquired: indicates whether or not the lock has been acquired
6843 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006844 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006845 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006846 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04006847static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006848{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006849 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006850 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006851 u32 v;
6852
6853 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6854 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006855 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006856 if (i40e_check_asq_alive(&pf->hw))
6857 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006858
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006859 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006860
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006861 /* quiesce the VSIs and their queues that are not already DOWN */
Maciej Sosin373149f2017-04-05 07:50:55 -04006862 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
6863 if (!lock_acquired)
6864 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006865 i40e_pf_quiesce_all_vsi(pf);
Maciej Sosin373149f2017-04-05 07:50:55 -04006866 if (!lock_acquired)
6867 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006868
Mitch Williams505682c2014-05-20 08:01:37 +00006869 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006870 if (pf->vsi[v])
6871 pf->vsi[v]->seid = 0;
6872 }
6873
6874 i40e_shutdown_adminq(&pf->hw);
6875
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006876 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006877 if (hw->hmc.hmc_obj) {
6878 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006879 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006880 dev_warn(&pf->pdev->dev,
6881 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006882 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006883}
6884
6885/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006886 * i40e_send_version - update firmware with driver version
6887 * @pf: PF struct
6888 */
6889static void i40e_send_version(struct i40e_pf *pf)
6890{
6891 struct i40e_driver_version dv;
6892
6893 dv.major_version = DRV_VERSION_MAJOR;
6894 dv.minor_version = DRV_VERSION_MINOR;
6895 dv.build_version = DRV_VERSION_BUILD;
6896 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006897 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006898 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6899}
6900
6901/**
Maciej Sosin373149f2017-04-05 07:50:55 -04006902 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
6903 * @pf: board private structure
6904 **/
6905static int i40e_reset(struct i40e_pf *pf)
6906{
6907 struct i40e_hw *hw = &pf->hw;
6908 i40e_status ret;
6909
6910 ret = i40e_pf_reset(hw);
6911 if (ret) {
6912 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
6913 set_bit(__I40E_RESET_FAILED, &pf->state);
6914 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6915 } else {
6916 pf->pfr_count++;
6917 }
6918 return ret;
6919}
6920
6921/**
6922 * i40e_rebuild - rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006923 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006924 * @reinit: if the Main VSI needs to re-initialized.
Maciej Sosin373149f2017-04-05 07:50:55 -04006925 * @lock_acquired: indicates whether or not the lock has been acquired
6926 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006927 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04006928static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006929{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006930 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006931 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006932 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006933 u32 val;
Maciej Sosin373149f2017-04-05 07:50:55 -04006934 int v;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006935
6936 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006937 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006938 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006939
6940 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6941 ret = i40e_init_adminq(&pf->hw);
6942 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006943 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6944 i40e_stat_str(&pf->hw, ret),
6945 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006946 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006947 }
6948
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006949 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006950 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006951 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006952
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006953 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006954 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006955 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006956 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006957
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006958 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08006959 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006960 if (ret) {
6961 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6962 goto end_core_reset;
6963 }
6964 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6965 if (ret) {
6966 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6967 goto end_core_reset;
6968 }
6969
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006970#ifdef CONFIG_I40E_DCB
6971 ret = i40e_init_pf_dcb(pf);
6972 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006973 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6974 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6975 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006976 }
6977#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006978 /* do basic switch setup */
Maciej Sosin373149f2017-04-05 07:50:55 -04006979 if (!lock_acquired)
6980 rtnl_lock();
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006981 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006982 if (ret)
Maciej Sosin373149f2017-04-05 07:50:55 -04006983 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006984
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006985 /* The driver only wants link up/down and module qualification
6986 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006987 */
6988 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006989 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07006990 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006991 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006992 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006993 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6994 i40e_stat_str(&pf->hw, ret),
6995 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006996
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006997 /* make sure our flow control settings are restored */
6998 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6999 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007000 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7001 i40e_stat_str(&pf->hw, ret),
7002 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007003
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007004 /* Rebuild the VSIs and VEBs that existed before reset.
7005 * They are still in our local switch element arrays, so only
7006 * need to rebuild the switch model in the HW.
7007 *
7008 * If there were VEBs but the reconstitution failed, we'll try
7009 * try to recover minimal use by getting the basic PF VSI working.
7010 */
7011 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007012 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007013 /* find the one VEB connected to the MAC, and find orphans */
7014 for (v = 0; v < I40E_MAX_VEB; v++) {
7015 if (!pf->veb[v])
7016 continue;
7017
7018 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7019 pf->veb[v]->uplink_seid == 0) {
7020 ret = i40e_reconstitute_veb(pf->veb[v]);
7021
7022 if (!ret)
7023 continue;
7024
7025 /* If Main VEB failed, we're in deep doodoo,
7026 * so give up rebuilding the switch and set up
7027 * for minimal rebuild of PF VSI.
7028 * If orphan failed, we'll report the error
7029 * but try to keep going.
7030 */
7031 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7032 dev_info(&pf->pdev->dev,
7033 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7034 ret);
7035 pf->vsi[pf->lan_vsi]->uplink_seid
7036 = pf->mac_seid;
7037 break;
7038 } else if (pf->veb[v]->uplink_seid == 0) {
7039 dev_info(&pf->pdev->dev,
7040 "rebuild of orphan VEB failed: %d\n",
7041 ret);
7042 }
7043 }
7044 }
7045 }
7046
7047 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007048 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007049 /* no VEB, so rebuild only the Main VSI */
7050 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7051 if (ret) {
7052 dev_info(&pf->pdev->dev,
7053 "rebuild of Main VSI failed: %d\n", ret);
Maciej Sosin373149f2017-04-05 07:50:55 -04007054 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007055 }
7056 }
7057
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007058 /* Reconfigure hardware for allowing smaller MSS in the case
7059 * of TSO, so that we avoid the MDD being fired and causing
7060 * a reset in the case of small MSS+TSO.
7061 */
7062#define I40E_REG_MSS 0x000E64DC
7063#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7064#define I40E_64BYTE_MSS 0x400000
7065 val = rd32(hw, I40E_REG_MSS);
7066 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7067 val &= ~I40E_REG_MSS_MIN_MASK;
7068 val |= I40E_64BYTE_MSS;
7069 wr32(hw, I40E_REG_MSS, val);
7070 }
7071
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08007072 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007073 msleep(75);
7074 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7075 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007076 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7077 i40e_stat_str(&pf->hw, ret),
7078 i40e_aq_str(&pf->hw,
7079 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007080 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007081 /* reinit the misc interrupt */
7082 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7083 ret = i40e_setup_misc_vector(pf);
7084
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007085 /* Add a filter to drop all Flow control frames from any VSI from being
7086 * transmitted. By doing so we stop a malicious VF from sending out
7087 * PAUSE or PFC frames and potentially controlling traffic for other
7088 * PF/VF VSIs.
7089 * The FW can still send Flow control frames if enabled.
7090 */
7091 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7092 pf->main_vsi_seid);
7093
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007094 /* restart the VSIs that were rebuilt and running before the reset */
7095 i40e_pf_unquiesce_all_vsi(pf);
7096
Mitch Williams69f64b22014-02-13 03:48:46 -08007097 if (pf->num_alloc_vfs) {
7098 for (v = 0; v < pf->num_alloc_vfs; v++)
7099 i40e_reset_vf(&pf->vf[v], true);
7100 }
7101
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007102 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007103 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007104
Maciej Sosin373149f2017-04-05 07:50:55 -04007105end_unlock:
7106if (!lock_acquired)
7107 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007108end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007109 clear_bit(__I40E_RESET_FAILED, &pf->state);
7110clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007111 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7112}
7113
7114/**
Maciej Sosin373149f2017-04-05 07:50:55 -04007115 * i40e_reset_and_rebuild - reset and rebuild using a saved config
7116 * @pf: board private structure
7117 * @reinit: if the Main VSI needs to re-initialized.
7118 * @lock_acquired: indicates whether or not the lock has been acquired
7119 * before this function was called.
7120 **/
7121static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
7122 bool lock_acquired)
7123{
7124 int ret;
7125 /* Now we wait for GRST to settle out.
7126 * We don't have to delete the VEBs or VSIs from the hw switch
7127 * because the reset will make them disappear.
7128 */
7129 ret = i40e_reset(pf);
7130 if (!ret)
7131 i40e_rebuild(pf, reinit, lock_acquired);
7132}
7133
7134/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007135 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007136 * @pf: board private structure
7137 *
7138 * Close up the VFs and other things in prep for a Core Reset,
7139 * then get ready to rebuild the world.
Maciej Sosin373149f2017-04-05 07:50:55 -04007140 * @lock_acquired: indicates whether or not the lock has been acquired
7141 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007142 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007143static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007144{
Maciej Sosin373149f2017-04-05 07:50:55 -04007145 i40e_prep_for_reset(pf, lock_acquired);
7146 i40e_reset_and_rebuild(pf, false, lock_acquired);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007147}
7148
7149/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007150 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007151 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007152 *
7153 * Called from the MDD irq handler to identify possibly malicious vfs
7154 **/
7155static void i40e_handle_mdd_event(struct i40e_pf *pf)
7156{
7157 struct i40e_hw *hw = &pf->hw;
7158 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007159 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007160 struct i40e_vf *vf;
7161 u32 reg;
7162 int i;
7163
7164 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7165 return;
7166
7167 /* find what triggered the MDD event */
7168 reg = rd32(hw, I40E_GL_MDET_TX);
7169 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007170 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7171 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007172 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007173 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007174 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007175 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007176 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7177 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7178 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007179 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007180 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 +00007181 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007182 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7183 mdd_detected = true;
7184 }
7185 reg = rd32(hw, I40E_GL_MDET_RX);
7186 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007187 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7188 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007189 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007190 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007191 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7192 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7193 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007194 if (netif_msg_rx_err(pf))
7195 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7196 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007197 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7198 mdd_detected = true;
7199 }
7200
Neerav Parikhdf430b12014-06-04 01:23:15 +00007201 if (mdd_detected) {
7202 reg = rd32(hw, I40E_PF_MDET_TX);
7203 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7204 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007205 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007206 pf_mdd_detected = true;
7207 }
7208 reg = rd32(hw, I40E_PF_MDET_RX);
7209 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7210 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007211 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007212 pf_mdd_detected = true;
7213 }
7214 /* Queue belongs to the PF, initiate a reset */
7215 if (pf_mdd_detected) {
7216 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7217 i40e_service_event_schedule(pf);
7218 }
7219 }
7220
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007221 /* see if one of the VFs needs its hand slapped */
7222 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7223 vf = &(pf->vf[i]);
7224 reg = rd32(hw, I40E_VP_MDET_TX(i));
7225 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7226 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7227 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007228 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7229 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007230 }
7231
7232 reg = rd32(hw, I40E_VP_MDET_RX(i));
7233 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7234 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7235 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007236 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7237 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007238 }
7239
7240 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7241 dev_info(&pf->pdev->dev,
7242 "Too many MDD events on VF %d, disabled\n", i);
7243 dev_info(&pf->pdev->dev,
7244 "Use PF Control I/F to re-enable the VF\n");
7245 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7246 }
7247 }
7248
7249 /* re-enable mdd interrupt cause */
7250 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7251 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7252 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7253 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7254 i40e_flush(hw);
7255}
7256
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007257/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007258 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007259 * @pf: board private structure
7260 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007261static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007262{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007263 struct i40e_hw *hw = &pf->hw;
7264 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007265 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007266 int i;
7267
Singhai, Anjali6a899022015-12-14 12:21:18 -08007268 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007269 return;
7270
Singhai, Anjali6a899022015-12-14 12:21:18 -08007271 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007272
7273 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007274 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7275 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7276 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007277 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007278 ret = i40e_aq_add_udp_tunnel(hw, port,
7279 pf->udp_ports[i].type,
7280 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007281 else
7282 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007283
7284 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007285 dev_dbg(&pf->pdev->dev,
7286 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7287 pf->udp_ports[i].type ? "vxlan" : "geneve",
7288 port ? "add" : "delete",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007289 port, i,
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007290 i40e_stat_str(&pf->hw, ret),
7291 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007292 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007293 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007294 }
7295 }
7296 }
7297}
7298
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007299/**
7300 * i40e_service_task - Run the driver's async subtasks
7301 * @work: pointer to work_struct containing our data
7302 **/
7303static void i40e_service_task(struct work_struct *work)
7304{
7305 struct i40e_pf *pf = container_of(work,
7306 struct i40e_pf,
7307 service_task);
7308 unsigned long start_time = jiffies;
7309
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007310 /* don't bother with service tasks if a reset is in progress */
7311 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007312 return;
7313 }
7314
Mitch Williams91089032016-11-21 13:03:51 -08007315 if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
7316 return;
7317
Kiran Patilb03a8c12015-09-24 18:13:15 -04007318 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007319 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007320 i40e_reset_subtask(pf);
7321 i40e_handle_mdd_event(pf);
7322 i40e_vc_process_vflr_event(pf);
7323 i40e_watchdog_subtask(pf);
7324 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007325 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
7326 /* Client subtask will reopen next time through. */
7327 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
7328 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
7329 } else {
7330 i40e_client_subtask(pf);
7331 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
7332 i40e_notify_client_of_l2_param_changes(
7333 pf->vsi[pf->lan_vsi]);
7334 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
7335 }
7336 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007337 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007338 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007339 i40e_clean_adminq_subtask(pf);
7340
Mitch Williams91089032016-11-21 13:03:51 -08007341 /* flush memory to make sure state is correct before next watchdog */
7342 smp_mb__before_atomic();
7343 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007344
7345 /* If the tasks have taken longer than one timer cycle or there
7346 * is more work to be done, reschedule the service task now
7347 * rather than wait for the timer to tick again.
7348 */
7349 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7350 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7351 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7352 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7353 i40e_service_event_schedule(pf);
7354}
7355
7356/**
7357 * i40e_service_timer - timer callback
7358 * @data: pointer to PF struct
7359 **/
7360static void i40e_service_timer(unsigned long data)
7361{
7362 struct i40e_pf *pf = (struct i40e_pf *)data;
7363
7364 mod_timer(&pf->service_timer,
7365 round_jiffies(jiffies + pf->service_timer_period));
7366 i40e_service_event_schedule(pf);
7367}
7368
7369/**
7370 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7371 * @vsi: the VSI being configured
7372 **/
7373static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7374{
7375 struct i40e_pf *pf = vsi->back;
7376
7377 switch (vsi->type) {
7378 case I40E_VSI_MAIN:
7379 vsi->alloc_queue_pairs = pf->num_lan_qps;
7380 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7381 I40E_REQ_DESCRIPTOR_MULTIPLE);
7382 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7383 vsi->num_q_vectors = pf->num_lan_msix;
7384 else
7385 vsi->num_q_vectors = 1;
7386
7387 break;
7388
7389 case I40E_VSI_FDIR:
7390 vsi->alloc_queue_pairs = 1;
7391 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7392 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007393 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007394 break;
7395
7396 case I40E_VSI_VMDQ2:
7397 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7398 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7399 I40E_REQ_DESCRIPTOR_MULTIPLE);
7400 vsi->num_q_vectors = pf->num_vmdq_msix;
7401 break;
7402
7403 case I40E_VSI_SRIOV:
7404 vsi->alloc_queue_pairs = pf->num_vf_qps;
7405 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7406 I40E_REQ_DESCRIPTOR_MULTIPLE);
7407 break;
7408
7409 default:
7410 WARN_ON(1);
7411 return -ENODATA;
7412 }
7413
7414 return 0;
7415}
7416
7417/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007418 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7419 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007420 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007421 *
7422 * On error: returns error code (negative)
7423 * On success: returns 0
7424 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007425static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007426{
7427 int size;
7428 int ret = 0;
7429
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007430 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007431 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7432 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7433 if (!vsi->tx_rings)
7434 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007435 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7436
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007437 if (alloc_qvectors) {
7438 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007439 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007440 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7441 if (!vsi->q_vectors) {
7442 ret = -ENOMEM;
7443 goto err_vectors;
7444 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007445 }
7446 return ret;
7447
7448err_vectors:
7449 kfree(vsi->tx_rings);
7450 return ret;
7451}
7452
7453/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007454 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7455 * @pf: board private structure
7456 * @type: type of VSI
7457 *
7458 * On error: returns error code (negative)
7459 * On success: returns vsi index in PF (positive)
7460 **/
7461static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7462{
7463 int ret = -ENODEV;
7464 struct i40e_vsi *vsi;
7465 int vsi_idx;
7466 int i;
7467
7468 /* Need to protect the allocation of the VSIs at the PF level */
7469 mutex_lock(&pf->switch_mutex);
7470
7471 /* VSI list may be fragmented if VSI creation/destruction has
7472 * been happening. We can afford to do a quick scan to look
7473 * for any free VSIs in the list.
7474 *
7475 * find next empty vsi slot, looping back around if necessary
7476 */
7477 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007478 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007479 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007480 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007481 i = 0;
7482 while (i < pf->next_vsi && pf->vsi[i])
7483 i++;
7484 }
7485
Mitch Williams505682c2014-05-20 08:01:37 +00007486 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007487 vsi_idx = i; /* Found one! */
7488 } else {
7489 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007490 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007491 }
7492 pf->next_vsi = ++i;
7493
7494 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7495 if (!vsi) {
7496 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007497 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007498 }
7499 vsi->type = type;
7500 vsi->back = pf;
7501 set_bit(__I40E_DOWN, &vsi->state);
7502 vsi->flags = 0;
7503 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007504 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007505 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7506 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007507 vsi->netdev_registered = false;
7508 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007509 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007510 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007511
Alexander Duyck9f65e152013-09-28 06:00:58 +00007512 ret = i40e_set_num_rings_in_vsi(vsi);
7513 if (ret)
7514 goto err_rings;
7515
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007516 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007517 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007518 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007519
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007520 /* Setup default MSIX irq handler for VSI */
7521 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7522
Kiran Patil21659032015-09-30 14:09:03 -04007523 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007524 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007525 pf->vsi[vsi_idx] = vsi;
7526 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007527 goto unlock_pf;
7528
Alexander Duyck9f65e152013-09-28 06:00:58 +00007529err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007530 pf->next_vsi = i - 1;
7531 kfree(vsi);
7532unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007533 mutex_unlock(&pf->switch_mutex);
7534 return ret;
7535}
7536
7537/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007538 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7539 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007540 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007541 *
7542 * On error: returns error code (negative)
7543 * On success: returns 0
7544 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007545static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007546{
7547 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007548 if (free_qvectors) {
7549 kfree(vsi->q_vectors);
7550 vsi->q_vectors = NULL;
7551 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007552 kfree(vsi->tx_rings);
7553 vsi->tx_rings = NULL;
7554 vsi->rx_rings = NULL;
7555}
7556
7557/**
Helin Zhang28c58692015-10-26 19:44:27 -04007558 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7559 * and lookup table
7560 * @vsi: Pointer to VSI structure
7561 */
7562static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7563{
7564 if (!vsi)
7565 return;
7566
7567 kfree(vsi->rss_hkey_user);
7568 vsi->rss_hkey_user = NULL;
7569
7570 kfree(vsi->rss_lut_user);
7571 vsi->rss_lut_user = NULL;
7572}
7573
7574/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007575 * i40e_vsi_clear - Deallocate the VSI provided
7576 * @vsi: the VSI being un-configured
7577 **/
7578static int i40e_vsi_clear(struct i40e_vsi *vsi)
7579{
7580 struct i40e_pf *pf;
7581
7582 if (!vsi)
7583 return 0;
7584
7585 if (!vsi->back)
7586 goto free_vsi;
7587 pf = vsi->back;
7588
7589 mutex_lock(&pf->switch_mutex);
7590 if (!pf->vsi[vsi->idx]) {
7591 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7592 vsi->idx, vsi->idx, vsi, vsi->type);
7593 goto unlock_vsi;
7594 }
7595
7596 if (pf->vsi[vsi->idx] != vsi) {
7597 dev_err(&pf->pdev->dev,
7598 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7599 pf->vsi[vsi->idx]->idx,
7600 pf->vsi[vsi->idx],
7601 pf->vsi[vsi->idx]->type,
7602 vsi->idx, vsi, vsi->type);
7603 goto unlock_vsi;
7604 }
7605
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007606 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007607 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7608 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7609
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007610 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007611 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007612
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007613 pf->vsi[vsi->idx] = NULL;
7614 if (vsi->idx < pf->next_vsi)
7615 pf->next_vsi = vsi->idx;
7616
7617unlock_vsi:
7618 mutex_unlock(&pf->switch_mutex);
7619free_vsi:
7620 kfree(vsi);
7621
7622 return 0;
7623}
7624
7625/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007626 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7627 * @vsi: the VSI being cleaned
7628 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007629static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007630{
7631 int i;
7632
Greg Rose8e9dca52013-12-18 13:45:53 +00007633 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007634 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007635 kfree_rcu(vsi->tx_rings[i], rcu);
7636 vsi->tx_rings[i] = NULL;
7637 vsi->rx_rings[i] = NULL;
7638 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007639 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007640}
7641
7642/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007643 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7644 * @vsi: the VSI being configured
7645 **/
7646static int i40e_alloc_rings(struct i40e_vsi *vsi)
7647{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007648 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007649 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007650 int i;
7651
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007652 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007653 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007654 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007655 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7656 if (!tx_ring)
7657 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007658
7659 tx_ring->queue_index = i;
7660 tx_ring->reg_idx = vsi->base_queue + i;
7661 tx_ring->ring_active = false;
7662 tx_ring->vsi = vsi;
7663 tx_ring->netdev = vsi->netdev;
7664 tx_ring->dev = &pf->pdev->dev;
7665 tx_ring->count = vsi->num_desc;
7666 tx_ring->size = 0;
7667 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007668 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7669 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007670 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007671 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007672
Alexander Duyck9f65e152013-09-28 06:00:58 +00007673 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007674 rx_ring->queue_index = i;
7675 rx_ring->reg_idx = vsi->base_queue + i;
7676 rx_ring->ring_active = false;
7677 rx_ring->vsi = vsi;
7678 rx_ring->netdev = vsi->netdev;
7679 rx_ring->dev = &pf->pdev->dev;
7680 rx_ring->count = vsi->num_desc;
7681 rx_ring->size = 0;
7682 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007683 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007684 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007685 }
7686
7687 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007688
7689err_out:
7690 i40e_vsi_clear_rings(vsi);
7691 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007692}
7693
7694/**
7695 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7696 * @pf: board private structure
7697 * @vectors: the number of MSI-X vectors to request
7698 *
7699 * Returns the number of vectors reserved, or error
7700 **/
7701static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7702{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007703 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7704 I40E_MIN_MSIX, vectors);
7705 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007706 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007707 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007708 vectors = 0;
7709 }
7710
7711 return vectors;
7712}
7713
7714/**
7715 * i40e_init_msix - Setup the MSIX capability
7716 * @pf: board private structure
7717 *
7718 * Work with the OS to set up the MSIX vectors needed.
7719 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007720 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007721 **/
7722static int i40e_init_msix(struct i40e_pf *pf)
7723{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007724 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007725 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007726 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007727 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007728 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007729 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007730
7731 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7732 return -ENODEV;
7733
7734 /* The number of vectors we'll request will be comprised of:
7735 * - Add 1 for "other" cause for Admin Queue events, etc.
7736 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007737 * - Queues being used for RSS.
7738 * We don't need as many as max_rss_size vectors.
7739 * use rss_size instead in the calculation since that
7740 * is governed by number of cpus in the system.
7741 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007742 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007743 * - The CPU count within the NUMA node if iWARP is enabled
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007744 * Once we count this up, try the request.
7745 *
7746 * If we can't get what we want, we'll simplify to nearly nothing
7747 * and try again. If that still fails, we punt.
7748 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007749 vectors_left = hw->func_caps.num_msix_vectors;
7750 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007751
Shannon Nelson1e200e42015-02-27 09:15:24 +00007752 /* reserve one vector for miscellaneous handler */
7753 if (vectors_left) {
7754 v_budget++;
7755 vectors_left--;
7756 }
7757
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007758 /* reserve some vectors for the main PF traffic queues. Initially we
7759 * only reserve at most 50% of the available vectors, in the case that
7760 * the number of online CPUs is large. This ensures that we can enable
7761 * extra features as well. Once we've enabled the other features, we
7762 * will use any remaining vectors to reach as close as we can to the
7763 * number of online CPUs.
7764 */
7765 cpus = num_online_cpus();
7766 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00007767 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007768
7769 /* reserve one vector for sideband flow director */
7770 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7771 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007772 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007773 v_budget++;
7774 vectors_left--;
7775 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007776 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007777 }
7778 }
John W Linville83840e42015-01-14 03:06:28 +00007779
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007780 /* can we reserve enough for iWARP? */
7781 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007782 iwarp_requested = pf->num_iwarp_msix;
7783
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007784 if (!vectors_left)
7785 pf->num_iwarp_msix = 0;
7786 else if (vectors_left < pf->num_iwarp_msix)
7787 pf->num_iwarp_msix = 1;
7788 v_budget += pf->num_iwarp_msix;
7789 vectors_left -= pf->num_iwarp_msix;
7790 }
7791
Shannon Nelson1e200e42015-02-27 09:15:24 +00007792 /* any vectors left over go for VMDq support */
7793 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7794 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7795 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7796
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007797 if (!vectors_left) {
7798 pf->num_vmdq_msix = 0;
7799 pf->num_vmdq_qps = 0;
7800 } else {
7801 /* if we're short on vectors for what's desired, we limit
7802 * the queues per vmdq. If this is still more than are
7803 * available, the user will need to change the number of
7804 * queues/vectors used by the PF later with the ethtool
7805 * channels command
7806 */
7807 if (vmdq_vecs < vmdq_vecs_wanted)
7808 pf->num_vmdq_qps = 1;
7809 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007810
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007811 v_budget += vmdq_vecs;
7812 vectors_left -= vmdq_vecs;
7813 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007814 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007815
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007816 /* On systems with a large number of SMP cores, we previously limited
7817 * the number of vectors for num_lan_msix to be at most 50% of the
7818 * available vectors, to allow for other features. Now, we add back
7819 * the remaining vectors. However, we ensure that the total
7820 * num_lan_msix will not exceed num_online_cpus(). To do this, we
7821 * calculate the number of vectors we can add without going over the
7822 * cap of CPUs. For systems with a small number of CPUs this will be
7823 * zero.
7824 */
7825 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
7826 pf->num_lan_msix += extra_vectors;
7827 vectors_left -= extra_vectors;
7828
7829 WARN(vectors_left < 0,
7830 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
7831
7832 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007833 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7834 GFP_KERNEL);
7835 if (!pf->msix_entries)
7836 return -ENOMEM;
7837
7838 for (i = 0; i < v_budget; i++)
7839 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007840 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007841
Shannon Nelson3b444392015-02-26 16:15:57 +00007842 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007843 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7844 kfree(pf->msix_entries);
7845 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007846 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007847 return -ENODEV;
7848
Shannon Nelson3b444392015-02-26 16:15:57 +00007849 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007850 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007851 pf->num_vmdq_vsis = 0;
7852 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007853 pf->num_lan_qps = 1;
7854 pf->num_lan_msix = 1;
7855
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007856 } else if (!vectors_left) {
7857 /* If we have limited resources, we will start with no vectors
7858 * for the special features and then allocate vectors to some
7859 * of these features based on the policy and at the end disable
7860 * the features that did not get any vectors.
7861 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007862 int vec;
7863
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007864 dev_info(&pf->pdev->dev,
7865 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007866 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007867 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007868
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007869 /* Scale vector usage down */
7870 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007871 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007872 pf->num_vmdq_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007873
7874 /* partition out the remaining vectors */
7875 switch (vec) {
7876 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007877 pf->num_lan_msix = 1;
7878 break;
7879 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007880 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7881 pf->num_lan_msix = 1;
7882 pf->num_iwarp_msix = 1;
7883 } else {
7884 pf->num_lan_msix = 2;
7885 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007886 break;
7887 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007888 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7889 pf->num_iwarp_msix = min_t(int, (vec / 3),
7890 iwarp_requested);
7891 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7892 I40E_DEFAULT_NUM_VMDQ_VSI);
7893 } else {
7894 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7895 I40E_DEFAULT_NUM_VMDQ_VSI);
7896 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007897 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7898 pf->num_fdsb_msix = 1;
7899 vec--;
7900 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007901 pf->num_lan_msix = min_t(int,
7902 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7903 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007904 pf->num_lan_qps = pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007905 break;
7906 }
7907 }
7908
Stefan Assmannabd97a92016-09-19 13:37:51 +02007909 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7910 (pf->num_fdsb_msix == 0)) {
7911 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7912 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7913 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007914 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7915 (pf->num_vmdq_msix == 0)) {
7916 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7917 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7918 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007919
7920 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7921 (pf->num_iwarp_msix == 0)) {
7922 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7923 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7924 }
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007925 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7926 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7927 pf->num_lan_msix,
7928 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7929 pf->num_fdsb_msix,
7930 pf->num_iwarp_msix);
7931
Shannon Nelson3b444392015-02-26 16:15:57 +00007932 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007933}
7934
7935/**
Greg Rose90e04072014-03-06 08:59:57 +00007936 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007937 * @vsi: the VSI being configured
7938 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007939 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007940 *
7941 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7942 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007943static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007944{
7945 struct i40e_q_vector *q_vector;
7946
7947 /* allocate q_vector */
7948 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7949 if (!q_vector)
7950 return -ENOMEM;
7951
7952 q_vector->vsi = vsi;
7953 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007954 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7955
Alexander Duyck493fb302013-09-28 07:01:44 +00007956 if (vsi->netdev)
7957 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007958 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007959
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007960 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7961 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7962
Alexander Duyck493fb302013-09-28 07:01:44 +00007963 /* tie q_vector and vsi together */
7964 vsi->q_vectors[v_idx] = q_vector;
7965
7966 return 0;
7967}
7968
7969/**
Greg Rose90e04072014-03-06 08:59:57 +00007970 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007971 * @vsi: the VSI being configured
7972 *
7973 * We allocate one q_vector per queue interrupt. If allocation fails we
7974 * return -ENOMEM.
7975 **/
Greg Rose90e04072014-03-06 08:59:57 +00007976static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007977{
7978 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007979 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007980
7981 /* if not MSIX, give the one vector only to the LAN VSI */
7982 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7983 num_q_vectors = vsi->num_q_vectors;
7984 else if (vsi == pf->vsi[pf->lan_vsi])
7985 num_q_vectors = 1;
7986 else
7987 return -EINVAL;
7988
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007989 current_cpu = cpumask_first(cpu_online_mask);
7990
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007991 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007992 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00007993 if (err)
7994 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007995 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
7996 if (unlikely(current_cpu >= nr_cpu_ids))
7997 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007998 }
7999
8000 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00008001
8002err_out:
8003 while (v_idx--)
8004 i40e_free_q_vector(vsi, v_idx);
8005
8006 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008007}
8008
8009/**
8010 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
8011 * @pf: board private structure to initialize
8012 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008013static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008014{
Shannon Nelson3b444392015-02-26 16:15:57 +00008015 int vectors = 0;
8016 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008017
8018 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00008019 vectors = i40e_init_msix(pf);
8020 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008021 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008022 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008023 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008024 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008025 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008026 I40E_FLAG_SRIOV_ENABLED |
8027 I40E_FLAG_FD_SB_ENABLED |
8028 I40E_FLAG_FD_ATR_ENABLED |
8029 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008030
8031 /* rework the queue expectations without MSIX */
8032 i40e_determine_queue_usage(pf);
8033 }
8034 }
8035
8036 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8037 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008038 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008039 vectors = pci_enable_msi(pf->pdev);
8040 if (vectors < 0) {
8041 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8042 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008043 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8044 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008045 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008046 }
8047
Shannon Nelson958a3e32013-09-28 07:13:28 +00008048 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008049 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008050
Shannon Nelson3b444392015-02-26 16:15:57 +00008051 /* set up vector assignment tracking */
8052 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8053 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008054 if (!pf->irq_pile) {
8055 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8056 return -ENOMEM;
8057 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008058 pf->irq_pile->num_entries = vectors;
8059 pf->irq_pile->search_hint = 0;
8060
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008061 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008062 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008063
8064 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008065}
8066
8067/**
8068 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8069 * @pf: board private structure
8070 *
8071 * This sets up the handler for MSIX 0, which is used to manage the
8072 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8073 * when in MSI or Legacy interrupt mode.
8074 **/
8075static int i40e_setup_misc_vector(struct i40e_pf *pf)
8076{
8077 struct i40e_hw *hw = &pf->hw;
8078 int err = 0;
8079
8080 /* Only request the irq if this is the first time through, and
8081 * not when we're rebuilding after a Reset
8082 */
8083 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
8084 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008085 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008086 if (err) {
8087 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008088 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008089 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008090 return -EFAULT;
8091 }
8092 }
8093
Jacob Kellerab437b52014-12-14 01:55:08 +00008094 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008095
8096 /* associate no queues to the misc vector */
8097 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8098 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8099
8100 i40e_flush(hw);
8101
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008102 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008103
8104 return err;
8105}
8106
8107/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008108 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8109 * @vsi: vsi structure
8110 * @seed: RSS hash seed
8111 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008112static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8113 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008114{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008115 struct i40e_pf *pf = vsi->back;
8116 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008117 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008118
Jacob Keller776b2e12016-07-19 16:23:30 -07008119 if (seed) {
8120 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8121 (struct i40e_aqc_get_set_rss_key_data *)seed;
8122 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8123 if (ret) {
8124 dev_info(&pf->pdev->dev,
8125 "Cannot set RSS key, err %s aq_err %s\n",
8126 i40e_stat_str(hw, ret),
8127 i40e_aq_str(hw, hw->aq.asq_last_status));
8128 return ret;
8129 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008130 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008131 if (lut) {
8132 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008133
Jacob Keller776b2e12016-07-19 16:23:30 -07008134 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8135 if (ret) {
8136 dev_info(&pf->pdev->dev,
8137 "Cannot set RSS lut, err %s aq_err %s\n",
8138 i40e_stat_str(hw, ret),
8139 i40e_aq_str(hw, hw->aq.asq_last_status));
8140 return ret;
8141 }
8142 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008143 return ret;
8144}
8145
8146/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008147 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8148 * @vsi: Pointer to vsi structure
8149 * @seed: Buffter to store the hash keys
8150 * @lut: Buffer to store the lookup table entries
8151 * @lut_size: Size of buffer to store the lookup table entries
8152 *
8153 * Return 0 on success, negative on failure
8154 */
8155static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8156 u8 *lut, u16 lut_size)
8157{
8158 struct i40e_pf *pf = vsi->back;
8159 struct i40e_hw *hw = &pf->hw;
8160 int ret = 0;
8161
8162 if (seed) {
8163 ret = i40e_aq_get_rss_key(hw, vsi->id,
8164 (struct i40e_aqc_get_set_rss_key_data *)seed);
8165 if (ret) {
8166 dev_info(&pf->pdev->dev,
8167 "Cannot get RSS key, err %s aq_err %s\n",
8168 i40e_stat_str(&pf->hw, ret),
8169 i40e_aq_str(&pf->hw,
8170 pf->hw.aq.asq_last_status));
8171 return ret;
8172 }
8173 }
8174
8175 if (lut) {
8176 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8177
8178 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8179 if (ret) {
8180 dev_info(&pf->pdev->dev,
8181 "Cannot get RSS lut, err %s aq_err %s\n",
8182 i40e_stat_str(&pf->hw, ret),
8183 i40e_aq_str(&pf->hw,
8184 pf->hw.aq.asq_last_status));
8185 return ret;
8186 }
8187 }
8188
8189 return ret;
8190}
8191
8192/**
Jacob Keller0582b962016-07-19 16:23:29 -07008193 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8194 * @vsi: VSI structure
8195 **/
8196static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8197{
8198 u8 seed[I40E_HKEY_ARRAY_SIZE];
8199 struct i40e_pf *pf = vsi->back;
8200 u8 *lut;
8201 int ret;
8202
8203 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8204 return 0;
8205
Jacob Keller552b9962016-07-19 16:23:31 -07008206 if (!vsi->rss_size)
8207 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8208 vsi->num_queue_pairs);
8209 if (!vsi->rss_size)
8210 return -EINVAL;
8211
Jacob Keller0582b962016-07-19 16:23:29 -07008212 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8213 if (!lut)
8214 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008215 /* Use the user configured hash keys and lookup table if there is one,
8216 * otherwise use default
8217 */
8218 if (vsi->rss_lut_user)
8219 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8220 else
8221 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8222 if (vsi->rss_hkey_user)
8223 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8224 else
8225 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008226 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8227 kfree(lut);
8228
8229 return ret;
8230}
8231
8232/**
Helin Zhang043dd652015-10-21 19:56:23 -04008233 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008234 * @vsi: Pointer to vsi structure
8235 * @seed: RSS hash seed
8236 * @lut: Lookup table
8237 * @lut_size: Lookup table size
8238 *
8239 * Returns 0 on success, negative on failure
8240 **/
8241static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8242 const u8 *lut, u16 lut_size)
8243{
8244 struct i40e_pf *pf = vsi->back;
8245 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008246 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008247 u8 i;
8248
8249 /* Fill out hash function seed */
8250 if (seed) {
8251 u32 *seed_dw = (u32 *)seed;
8252
Mitch Williamsc4e18682016-04-12 08:30:40 -07008253 if (vsi->type == I40E_VSI_MAIN) {
8254 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008255 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008256 } else if (vsi->type == I40E_VSI_SRIOV) {
8257 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008258 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008259 } else {
8260 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8261 }
Helin Zhange69ff812015-10-21 19:56:22 -04008262 }
8263
8264 if (lut) {
8265 u32 *lut_dw = (u32 *)lut;
8266
Mitch Williamsc4e18682016-04-12 08:30:40 -07008267 if (vsi->type == I40E_VSI_MAIN) {
8268 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8269 return -EINVAL;
8270 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8271 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8272 } else if (vsi->type == I40E_VSI_SRIOV) {
8273 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8274 return -EINVAL;
8275 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008276 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008277 } else {
8278 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8279 }
Helin Zhange69ff812015-10-21 19:56:22 -04008280 }
8281 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008282
8283 return 0;
8284}
8285
8286/**
Helin Zhang043dd652015-10-21 19:56:23 -04008287 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8288 * @vsi: Pointer to VSI structure
8289 * @seed: Buffer to store the keys
8290 * @lut: Buffer to store the lookup table entries
8291 * @lut_size: Size of buffer to store the lookup table entries
8292 *
8293 * Returns 0 on success, negative on failure
8294 */
8295static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8296 u8 *lut, u16 lut_size)
8297{
8298 struct i40e_pf *pf = vsi->back;
8299 struct i40e_hw *hw = &pf->hw;
8300 u16 i;
8301
8302 if (seed) {
8303 u32 *seed_dw = (u32 *)seed;
8304
8305 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008306 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008307 }
8308 if (lut) {
8309 u32 *lut_dw = (u32 *)lut;
8310
8311 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8312 return -EINVAL;
8313 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8314 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8315 }
8316
8317 return 0;
8318}
8319
8320/**
8321 * i40e_config_rss - Configure RSS keys and lut
8322 * @vsi: Pointer to VSI structure
8323 * @seed: RSS hash seed
8324 * @lut: Lookup table
8325 * @lut_size: Lookup table size
8326 *
8327 * Returns 0 on success, negative on failure
8328 */
8329int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8330{
8331 struct i40e_pf *pf = vsi->back;
8332
8333 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8334 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8335 else
8336 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8337}
8338
8339/**
8340 * i40e_get_rss - Get RSS keys and lut
8341 * @vsi: Pointer to VSI structure
8342 * @seed: Buffer to store the keys
8343 * @lut: Buffer to store the lookup table entries
8344 * lut_size: Size of buffer to store the lookup table entries
8345 *
8346 * Returns 0 on success, negative on failure
8347 */
8348int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8349{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008350 struct i40e_pf *pf = vsi->back;
8351
8352 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8353 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8354 else
8355 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008356}
8357
8358/**
Helin Zhange69ff812015-10-21 19:56:22 -04008359 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8360 * @pf: Pointer to board private structure
8361 * @lut: Lookup table
8362 * @rss_table_size: Lookup table size
8363 * @rss_size: Range of queue number for hashing
8364 */
Alan Bradyf1582352016-08-24 11:33:46 -07008365void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8366 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008367{
Helin Zhange69ff812015-10-21 19:56:22 -04008368 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008369
Helin Zhange69ff812015-10-21 19:56:22 -04008370 for (i = 0; i < rss_table_size; i++)
8371 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008372}
8373
8374/**
Helin Zhang043dd652015-10-21 19:56:23 -04008375 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008376 * @pf: board private structure
8377 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008378static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008379{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008380 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008381 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008382 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008383 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008384 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008385 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008386 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008387
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008388 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008389 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8390 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008391 hena |= i40e_pf_get_default_rss_hena(pf);
8392
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008393 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8394 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008395
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008396 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008397 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008398 reg_val = (pf->rss_table_size == 512) ?
8399 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8400 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008401 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008402
Helin Zhang28c58692015-10-26 19:44:27 -04008403 /* Determine the RSS size of the VSI */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008404 if (!vsi->rss_size) {
8405 u16 qcount;
8406
8407 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8408 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
8409 }
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008410 if (!vsi->rss_size)
8411 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008412
Helin Zhange69ff812015-10-21 19:56:22 -04008413 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8414 if (!lut)
8415 return -ENOMEM;
8416
Helin Zhang28c58692015-10-26 19:44:27 -04008417 /* Use user configured lut if there is one, otherwise use default */
8418 if (vsi->rss_lut_user)
8419 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8420 else
8421 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008422
Helin Zhang28c58692015-10-26 19:44:27 -04008423 /* Use user configured hash key if there is one, otherwise
8424 * use default.
8425 */
8426 if (vsi->rss_hkey_user)
8427 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8428 else
8429 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008430 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008431 kfree(lut);
8432
8433 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008434}
8435
8436/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008437 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8438 * @pf: board private structure
8439 * @queue_count: the requested queue count for rss.
8440 *
8441 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8442 * count which may be different from the requested queue count.
Maciej Sosin373149f2017-04-05 07:50:55 -04008443 * Note: expects to be called while under rtnl_lock()
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008444 **/
8445int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8446{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008447 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8448 int new_rss_size;
8449
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008450 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8451 return 0;
8452
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008453 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008454
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008455 if (queue_count != vsi->num_queue_pairs) {
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008456 u16 qcount;
8457
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008458 vsi->req_queue_pairs = queue_count;
Maciej Sosin373149f2017-04-05 07:50:55 -04008459 i40e_prep_for_reset(pf, true);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008460
Helin Zhangacd65442015-10-26 19:44:28 -04008461 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008462
Maciej Sosin373149f2017-04-05 07:50:55 -04008463 i40e_reset_and_rebuild(pf, true, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008464
8465 /* Discard the user configured hash keys and lut, if less
8466 * queues are enabled.
8467 */
8468 if (queue_count < vsi->rss_size) {
8469 i40e_clear_rss_config_user(vsi);
8470 dev_dbg(&pf->pdev->dev,
8471 "discard user configured hash keys and lut\n");
8472 }
8473
8474 /* Reset vsi->rss_size, as number of enabled queues changed */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008475 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8476 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
Helin Zhang28c58692015-10-26 19:44:27 -04008477
Helin Zhang043dd652015-10-21 19:56:23 -04008478 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008479 }
Lihong Yang12815052016-09-27 11:28:48 -07008480 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8481 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008482 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008483}
8484
8485/**
Greg Rosef4492db2015-02-06 08:52:12 +00008486 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8487 * @pf: board private structure
8488 **/
8489i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8490{
8491 i40e_status status;
8492 bool min_valid, max_valid;
8493 u32 max_bw, min_bw;
8494
8495 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8496 &min_valid, &max_valid);
8497
8498 if (!status) {
8499 if (min_valid)
8500 pf->npar_min_bw = min_bw;
8501 if (max_valid)
8502 pf->npar_max_bw = max_bw;
8503 }
8504
8505 return status;
8506}
8507
8508/**
8509 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8510 * @pf: board private structure
8511 **/
8512i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8513{
8514 struct i40e_aqc_configure_partition_bw_data bw_data;
8515 i40e_status status;
8516
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008517 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008518 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008519 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8520 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8521
8522 /* Set the new bandwidths */
8523 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8524
8525 return status;
8526}
8527
8528/**
8529 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8530 * @pf: board private structure
8531 **/
8532i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8533{
8534 /* Commit temporary BW setting to permanent NVM image */
8535 enum i40e_admin_queue_err last_aq_status;
8536 i40e_status ret;
8537 u16 nvm_word;
8538
8539 if (pf->hw.partition_id != 1) {
8540 dev_info(&pf->pdev->dev,
8541 "Commit BW only works on partition 1! This is partition %d",
8542 pf->hw.partition_id);
8543 ret = I40E_NOT_SUPPORTED;
8544 goto bw_commit_out;
8545 }
8546
8547 /* Acquire NVM for read access */
8548 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8549 last_aq_status = pf->hw.aq.asq_last_status;
8550 if (ret) {
8551 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008552 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8553 i40e_stat_str(&pf->hw, ret),
8554 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008555 goto bw_commit_out;
8556 }
8557
8558 /* Read word 0x10 of NVM - SW compatibility word 1 */
8559 ret = i40e_aq_read_nvm(&pf->hw,
8560 I40E_SR_NVM_CONTROL_WORD,
8561 0x10, sizeof(nvm_word), &nvm_word,
8562 false, NULL);
8563 /* Save off last admin queue command status before releasing
8564 * the NVM
8565 */
8566 last_aq_status = pf->hw.aq.asq_last_status;
8567 i40e_release_nvm(&pf->hw);
8568 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008569 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8570 i40e_stat_str(&pf->hw, ret),
8571 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008572 goto bw_commit_out;
8573 }
8574
8575 /* Wait a bit for NVM release to complete */
8576 msleep(50);
8577
8578 /* Acquire NVM for write access */
8579 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8580 last_aq_status = pf->hw.aq.asq_last_status;
8581 if (ret) {
8582 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008583 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8584 i40e_stat_str(&pf->hw, ret),
8585 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008586 goto bw_commit_out;
8587 }
8588 /* Write it back out unchanged to initiate update NVM,
8589 * which will force a write of the shadow (alt) RAM to
8590 * the NVM - thus storing the bandwidth values permanently.
8591 */
8592 ret = i40e_aq_update_nvm(&pf->hw,
8593 I40E_SR_NVM_CONTROL_WORD,
8594 0x10, sizeof(nvm_word),
8595 &nvm_word, true, NULL);
8596 /* Save off last admin queue command status before releasing
8597 * the NVM
8598 */
8599 last_aq_status = pf->hw.aq.asq_last_status;
8600 i40e_release_nvm(&pf->hw);
8601 if (ret)
8602 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008603 "BW settings NOT SAVED, err %s aq_err %s\n",
8604 i40e_stat_str(&pf->hw, ret),
8605 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008606bw_commit_out:
8607
8608 return ret;
8609}
8610
8611/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008612 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8613 * @pf: board private structure to initialize
8614 *
8615 * i40e_sw_init initializes the Adapter private data structure.
8616 * Fields are initialized based on PCI device information and
8617 * OS network device settings (MTU size).
8618 **/
8619static int i40e_sw_init(struct i40e_pf *pf)
8620{
8621 int err = 0;
8622 int size;
8623
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008624 /* Set default capability flags */
8625 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8626 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008627 I40E_FLAG_MSIX_ENABLED;
8628
Mitch Williamsca99eb92014-04-04 04:43:07 +00008629 /* Set default ITR */
8630 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8631 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8632
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008633 /* Depending on PF configurations, it is possible that the RSS
8634 * maximum might end up larger than the available queues
8635 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008636 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008637 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008638 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008639 pf->rss_size_max = min_t(int, pf->rss_size_max,
8640 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008641 if (pf->hw.func_caps.rss) {
8642 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008643 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8644 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008645 }
8646
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008647 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008648 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008649 pf->flags |= I40E_FLAG_MFP_ENABLED;
8650 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008651 if (i40e_get_npar_bw_setting(pf))
8652 dev_warn(&pf->pdev->dev,
8653 "Could not get NPAR bw settings\n");
8654 else
8655 dev_info(&pf->pdev->dev,
8656 "Min BW = %8.8x, Max BW = %8.8x\n",
8657 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008658 }
8659
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008660 /* FW/NVM is not yet fixed in this regard */
8661 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8662 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8663 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8664 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008665 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8666 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008667 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008668 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008669 else
8670 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008671 pf->fdir_pf_filter_count =
8672 pf->hw.func_caps.fd_filters_guaranteed;
8673 pf->hw.fdir_shared_filter_count =
8674 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008675 }
8676
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008677 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008678 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008679 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008680 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008681 /* No DCB support for FW < v4.33 */
8682 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8683 }
8684
8685 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008686 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008687 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8688 (pf->hw.aq.fw_maj_ver < 4)))
8689 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8690
8691 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008692 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008693 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8694 (pf->hw.aq.fw_maj_ver >= 5)))
8695 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008696
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008697 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008698 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008699 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008700 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008701 }
8702
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008703 if (pf->hw.func_caps.iwarp) {
8704 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8705 /* IWARP needs one extra vector for CQP just like MISC.*/
8706 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8707 }
8708
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008709#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008710 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008711 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8712 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8713 pf->num_req_vfs = min_t(int,
8714 pf->hw.func_caps.num_vfs,
8715 I40E_MAX_VF_COUNT);
8716 }
8717#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008718 if (pf->hw.mac.type == I40E_MAC_X722) {
Joshua Hay1d680052016-12-12 15:44:08 -08008719 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
8720 | I40E_FLAG_128_QP_RSS_CAPABLE
8721 | I40E_FLAG_HW_ATR_EVICT_CAPABLE
8722 | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
8723 | I40E_FLAG_WB_ON_ITR_CAPABLE
8724 | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
8725 | I40E_FLAG_NO_PCI_LINK_CHECK
8726 | I40E_FLAG_USE_SET_LLDP_MIB
8727 | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
8728 | I40E_FLAG_PTP_L4_CAPABLE
8729 | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008730 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8731 ((pf->hw.aq.api_maj_ver == 1) &&
8732 (pf->hw.aq.api_min_ver > 4))) {
8733 /* Supported in FW API version higher than 1.4 */
8734 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008735 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008736 } else {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008737 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008738 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008739
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008740 pf->eeprom_version = 0xDEAD;
8741 pf->lan_veb = I40E_NO_VEB;
8742 pf->lan_vsi = I40E_NO_VSI;
8743
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008744 /* By default FW has this off for performance reasons */
8745 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8746
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008747 /* set up queue assignment tracking */
8748 size = sizeof(struct i40e_lump_tracking)
8749 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8750 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8751 if (!pf->qp_pile) {
8752 err = -ENOMEM;
8753 goto sw_init_done;
8754 }
8755 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8756 pf->qp_pile->search_hint = 0;
8757
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008758 pf->tx_timeout_recovery_level = 1;
8759
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008760 mutex_init(&pf->switch_mutex);
8761
Greg Rosec668a122015-02-26 16:10:39 +00008762 /* If NPAR is enabled nudge the Tx scheduler */
8763 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8764 i40e_set_npar_bw_setting(pf);
8765
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008766sw_init_done:
8767 return err;
8768}
8769
8770/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008771 * i40e_set_ntuple - set the ntuple feature flag and take action
8772 * @pf: board private structure to initialize
8773 * @features: the feature set that the stack is suggesting
8774 *
8775 * returns a bool to indicate if reset needs to happen
8776 **/
8777bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8778{
8779 bool need_reset = false;
8780
8781 /* Check if Flow Director n-tuple support was enabled or disabled. If
8782 * the state changed, we need to reset.
8783 */
8784 if (features & NETIF_F_NTUPLE) {
8785 /* Enable filters and mark for reset */
8786 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8787 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008788 /* enable FD_SB only if there is MSI-X vector */
8789 if (pf->num_fdsb_msix > 0)
8790 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008791 } else {
8792 /* turn off filters, mark for reset and clear SW filter list */
8793 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8794 need_reset = true;
8795 i40e_fdir_filter_exit(pf);
8796 }
8797 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008798 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008799 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08008800 pf->fd_add_err = 0;
8801 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008802 /* if ATR was auto disabled it can be re-enabled. */
8803 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008804 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED)) {
8805 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008806 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8807 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8808 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008809 }
8810 return need_reset;
8811}
8812
8813/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008814 * i40e_clear_rss_lut - clear the rx hash lookup table
8815 * @vsi: the VSI being configured
8816 **/
8817static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8818{
8819 struct i40e_pf *pf = vsi->back;
8820 struct i40e_hw *hw = &pf->hw;
8821 u16 vf_id = vsi->vf_id;
8822 u8 i;
8823
8824 if (vsi->type == I40E_VSI_MAIN) {
8825 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8826 wr32(hw, I40E_PFQF_HLUT(i), 0);
8827 } else if (vsi->type == I40E_VSI_SRIOV) {
8828 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8829 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8830 } else {
8831 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8832 }
8833}
8834
8835/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008836 * i40e_set_features - set the netdev feature flags
8837 * @netdev: ptr to the netdev being adjusted
8838 * @features: the feature set that the stack is suggesting
Maciej Sosin373149f2017-04-05 07:50:55 -04008839 * Note: expects to be called while under rtnl_lock()
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008840 **/
8841static int i40e_set_features(struct net_device *netdev,
8842 netdev_features_t features)
8843{
8844 struct i40e_netdev_priv *np = netdev_priv(netdev);
8845 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008846 struct i40e_pf *pf = vsi->back;
8847 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008848
Alan Bradyd8ec9862016-07-27 12:02:38 -07008849 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8850 i40e_pf_config_rss(pf);
8851 else if (!(features & NETIF_F_RXHASH) &&
8852 netdev->features & NETIF_F_RXHASH)
8853 i40e_clear_rss_lut(vsi);
8854
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008855 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8856 i40e_vlan_stripping_enable(vsi);
8857 else
8858 i40e_vlan_stripping_disable(vsi);
8859
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008860 need_reset = i40e_set_ntuple(pf, features);
8861
8862 if (need_reset)
Maciej Sosin373149f2017-04-05 07:50:55 -04008863 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008864
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008865 return 0;
8866}
8867
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008868/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008869 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008870 * @pf: board private structure
8871 * @port: The UDP port to look up
8872 *
8873 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8874 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008875static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008876{
8877 u8 i;
8878
8879 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008880 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008881 return i;
8882 }
8883
8884 return i;
8885}
8886
8887/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008888 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008889 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008890 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008891 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008892static void i40e_udp_tunnel_add(struct net_device *netdev,
8893 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008894{
8895 struct i40e_netdev_priv *np = netdev_priv(netdev);
8896 struct i40e_vsi *vsi = np->vsi;
8897 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008898 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008899 u8 next_idx;
8900 u8 idx;
8901
Singhai, Anjali6a899022015-12-14 12:21:18 -08008902 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008903
8904 /* Check if port already exists */
8905 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008906 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008907 return;
8908 }
8909
8910 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008911 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008912
8913 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008914 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008915 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008916 return;
8917 }
8918
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008919 switch (ti->type) {
8920 case UDP_TUNNEL_TYPE_VXLAN:
8921 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8922 break;
8923 case UDP_TUNNEL_TYPE_GENEVE:
8924 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8925 return;
8926 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8927 break;
8928 default:
8929 return;
8930 }
8931
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008932 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008933 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008934 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8935 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008936}
8937
8938/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008939 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008940 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008941 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008942 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008943static void i40e_udp_tunnel_del(struct net_device *netdev,
8944 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008945{
8946 struct i40e_netdev_priv *np = netdev_priv(netdev);
8947 struct i40e_vsi *vsi = np->vsi;
8948 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008949 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008950 u8 idx;
8951
Singhai, Anjali6a899022015-12-14 12:21:18 -08008952 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008953
8954 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008955 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8956 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008957
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008958 switch (ti->type) {
8959 case UDP_TUNNEL_TYPE_VXLAN:
8960 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8961 goto not_found;
8962 break;
8963 case UDP_TUNNEL_TYPE_GENEVE:
8964 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8965 goto not_found;
8966 break;
8967 default:
8968 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008969 }
8970
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008971 /* if port exists, set it to 0 (mark for deletion)
8972 * and make it pending
8973 */
8974 pf->udp_ports[idx].index = 0;
8975 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008976 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8977
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008978 return;
8979not_found:
8980 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008981 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008982}
8983
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008984static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008985 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008986{
8987 struct i40e_netdev_priv *np = netdev_priv(netdev);
8988 struct i40e_pf *pf = np->vsi->back;
8989 struct i40e_hw *hw = &pf->hw;
8990
8991 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8992 return -EOPNOTSUPP;
8993
8994 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8995 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8996
8997 return 0;
8998}
8999
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08009000/**
9001 * i40e_ndo_fdb_add - add an entry to the hardware database
9002 * @ndm: the input from the stack
9003 * @tb: pointer to array of nladdr (unused)
9004 * @dev: the net device pointer
9005 * @addr: the MAC address entry being added
9006 * @flags: instructions from stack about fdb operation
9007 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00009008static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
9009 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01009010 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009011 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00009012{
9013 struct i40e_netdev_priv *np = netdev_priv(dev);
9014 struct i40e_pf *pf = np->vsi->back;
9015 int err = 0;
9016
9017 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
9018 return -EOPNOTSUPP;
9019
Or Gerlitz65891fe2014-12-14 18:19:05 +02009020 if (vid) {
9021 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9022 return -EINVAL;
9023 }
9024
Greg Rose4ba0dea2014-03-06 08:59:55 +00009025 /* Hardware does not support aging addresses so if a
9026 * ndm_state is given only allow permanent addresses
9027 */
9028 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9029 netdev_info(dev, "FDB only supports static addresses\n");
9030 return -EINVAL;
9031 }
9032
9033 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9034 err = dev_uc_add_excl(dev, addr);
9035 else if (is_multicast_ether_addr(addr))
9036 err = dev_mc_add_excl(dev, addr);
9037 else
9038 err = -EINVAL;
9039
9040 /* Only return duplicate errors if NLM_F_EXCL is set */
9041 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9042 err = 0;
9043
9044 return err;
9045}
9046
Neerav Parikh51616012015-02-06 08:52:14 +00009047/**
9048 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9049 * @dev: the netdev being configured
9050 * @nlh: RTNL message
9051 *
9052 * Inserts a new hardware bridge if not already created and
9053 * enables the bridging mode requested (VEB or VEPA). If the
9054 * hardware bridge has already been inserted and the request
9055 * is to change the mode then that requires a PF reset to
9056 * allow rebuild of the components with required hardware
9057 * bridge mode enabled.
Maciej Sosin373149f2017-04-05 07:50:55 -04009058 *
9059 * Note: expects to be called while under rtnl_lock()
Neerav Parikh51616012015-02-06 08:52:14 +00009060 **/
9061static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009062 struct nlmsghdr *nlh,
9063 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009064{
9065 struct i40e_netdev_priv *np = netdev_priv(dev);
9066 struct i40e_vsi *vsi = np->vsi;
9067 struct i40e_pf *pf = vsi->back;
9068 struct i40e_veb *veb = NULL;
9069 struct nlattr *attr, *br_spec;
9070 int i, rem;
9071
9072 /* Only for PF VSI for now */
9073 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9074 return -EOPNOTSUPP;
9075
9076 /* Find the HW bridge for PF VSI */
9077 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9078 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9079 veb = pf->veb[i];
9080 }
9081
9082 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9083
9084 nla_for_each_nested(attr, br_spec, rem) {
9085 __u16 mode;
9086
9087 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9088 continue;
9089
9090 mode = nla_get_u16(attr);
9091 if ((mode != BRIDGE_MODE_VEPA) &&
9092 (mode != BRIDGE_MODE_VEB))
9093 return -EINVAL;
9094
9095 /* Insert a new HW bridge */
9096 if (!veb) {
9097 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9098 vsi->tc_config.enabled_tc);
9099 if (veb) {
9100 veb->bridge_mode = mode;
9101 i40e_config_bridge_mode(veb);
9102 } else {
9103 /* No Bridge HW offload available */
9104 return -ENOENT;
9105 }
9106 break;
9107 } else if (mode != veb->bridge_mode) {
9108 /* Existing HW bridge but different mode needs reset */
9109 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009110 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9111 if (mode == BRIDGE_MODE_VEB)
9112 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9113 else
9114 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
Maciej Sosin373149f2017-04-05 07:50:55 -04009115 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED),
9116 true);
Neerav Parikh51616012015-02-06 08:52:14 +00009117 break;
9118 }
9119 }
9120
9121 return 0;
9122}
9123
9124/**
9125 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9126 * @skb: skb buff
9127 * @pid: process id
9128 * @seq: RTNL message seq #
9129 * @dev: the netdev being configured
9130 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009131 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009132 *
9133 * Return the mode in which the hardware bridge is operating in
9134 * i.e VEB or VEPA.
9135 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009136static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9137 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009138 u32 __always_unused filter_mask,
9139 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009140{
9141 struct i40e_netdev_priv *np = netdev_priv(dev);
9142 struct i40e_vsi *vsi = np->vsi;
9143 struct i40e_pf *pf = vsi->back;
9144 struct i40e_veb *veb = NULL;
9145 int i;
9146
9147 /* Only for PF VSI for now */
9148 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9149 return -EOPNOTSUPP;
9150
9151 /* Find the HW bridge for the PF VSI */
9152 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9153 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9154 veb = pf->veb[i];
9155 }
9156
9157 if (!veb)
9158 return 0;
9159
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009160 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009161 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009162}
Neerav Parikh51616012015-02-06 08:52:14 +00009163
Joe Stringerf44a75e2015-04-14 17:09:14 -07009164/**
9165 * i40e_features_check - Validate encapsulated packet conforms to limits
9166 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009167 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009168 * @features: Offload features that the stack believes apply
9169 **/
9170static netdev_features_t i40e_features_check(struct sk_buff *skb,
9171 struct net_device *dev,
9172 netdev_features_t features)
9173{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009174 size_t len;
9175
9176 /* No point in doing any of this if neither checksum nor GSO are
9177 * being requested for this frame. We can rule out both by just
9178 * checking for CHECKSUM_PARTIAL
9179 */
9180 if (skb->ip_summed != CHECKSUM_PARTIAL)
9181 return features;
9182
9183 /* We cannot support GSO if the MSS is going to be less than
9184 * 64 bytes. If it is then we need to drop support for GSO.
9185 */
9186 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9187 features &= ~NETIF_F_GSO_MASK;
9188
9189 /* MACLEN can support at most 63 words */
9190 len = skb_network_header(skb) - skb->data;
9191 if (len & ~(63 * 2))
9192 goto out_err;
9193
9194 /* IPLEN and EIPLEN can support at most 127 dwords */
9195 len = skb_transport_header(skb) - skb_network_header(skb);
9196 if (len & ~(127 * 4))
9197 goto out_err;
9198
9199 if (skb->encapsulation) {
9200 /* L4TUNLEN can support 127 words */
9201 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9202 if (len & ~(127 * 2))
9203 goto out_err;
9204
9205 /* IPLEN can support at most 127 dwords */
9206 len = skb_inner_transport_header(skb) -
9207 skb_inner_network_header(skb);
9208 if (len & ~(127 * 4))
9209 goto out_err;
9210 }
9211
9212 /* No need to validate L4LEN as TCP is the only protocol with a
9213 * a flexible value and we support all possible values supported
9214 * by TCP, which is at most 15 dwords
9215 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009216
9217 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009218out_err:
9219 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009220}
9221
Shannon Nelson37a29732015-02-27 09:15:19 +00009222static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009223 .ndo_open = i40e_open,
9224 .ndo_stop = i40e_close,
9225 .ndo_start_xmit = i40e_lan_xmit_frame,
9226 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9227 .ndo_set_rx_mode = i40e_set_rx_mode,
9228 .ndo_validate_addr = eth_validate_addr,
9229 .ndo_set_mac_address = i40e_set_mac,
9230 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009231 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009232 .ndo_tx_timeout = i40e_tx_timeout,
9233 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9234 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9235#ifdef CONFIG_NET_POLL_CONTROLLER
9236 .ndo_poll_controller = i40e_netpoll,
9237#endif
John Fastabende4c67342016-02-16 21:16:15 -08009238 .ndo_setup_tc = __i40e_setup_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009239 .ndo_set_features = i40e_set_features,
9240 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9241 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009242 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009243 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009244 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009245 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009246 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009247 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9248 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009249 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009250 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009251 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009252 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9253 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009254};
9255
9256/**
9257 * i40e_config_netdev - Setup the netdev flags
9258 * @vsi: the VSI being configured
9259 *
9260 * Returns 0 on success, negative value on failure
9261 **/
9262static int i40e_config_netdev(struct i40e_vsi *vsi)
9263{
9264 struct i40e_pf *pf = vsi->back;
9265 struct i40e_hw *hw = &pf->hw;
9266 struct i40e_netdev_priv *np;
9267 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009268 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009269 u8 mac_addr[ETH_ALEN];
9270 int etherdev_size;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009271 netdev_features_t hw_enc_features;
9272 netdev_features_t hw_features;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009273
9274 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009275 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009276 if (!netdev)
9277 return -ENOMEM;
9278
9279 vsi->netdev = netdev;
9280 np = netdev_priv(netdev);
9281 np->vsi = vsi;
9282
Preethi Banalabacd75c2017-03-27 14:43:18 -07009283 hw_enc_features = NETIF_F_SG |
9284 NETIF_F_IP_CSUM |
9285 NETIF_F_IPV6_CSUM |
9286 NETIF_F_HIGHDMA |
9287 NETIF_F_SOFT_FEATURES |
9288 NETIF_F_TSO |
9289 NETIF_F_TSO_ECN |
9290 NETIF_F_TSO6 |
9291 NETIF_F_GSO_GRE |
9292 NETIF_F_GSO_GRE_CSUM |
9293 NETIF_F_GSO_PARTIAL |
9294 NETIF_F_GSO_UDP_TUNNEL |
9295 NETIF_F_GSO_UDP_TUNNEL_CSUM |
9296 NETIF_F_SCTP_CRC |
9297 NETIF_F_RXHASH |
9298 NETIF_F_RXCSUM |
9299 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009300
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009301 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009302 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9303
9304 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009305
Preethi Banalabacd75c2017-03-27 14:43:18 -07009306 netdev->hw_enc_features |= hw_enc_features;
9307
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009308 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07009309 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009310
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009311 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009312 netdev->hw_features |= NETIF_F_NTUPLE;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009313 hw_features = hw_enc_features |
9314 NETIF_F_HW_VLAN_CTAG_TX |
9315 NETIF_F_HW_VLAN_CTAG_RX;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009316
Preethi Banalabacd75c2017-03-27 14:43:18 -07009317 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009318
Preethi Banalabacd75c2017-03-27 14:43:18 -07009319 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009320 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009321
9322 if (vsi->type == I40E_VSI_MAIN) {
9323 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009324 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller41c4c2b2017-04-05 07:50:57 -04009325 /* The following steps are necessary for two reasons. First,
9326 * some older NVM configurations load a default MAC-VLAN
9327 * filter that will accept any tagged packet, and we want to
9328 * replace this with a normal filter. Additionally, it is
9329 * possible our MAC address was provided by the platform using
9330 * Open Firmware or similar.
9331 *
9332 * Thus, we need to remove the default filter and install one
9333 * specific to the MAC address.
Jacob Keller1596b5d2016-11-08 13:05:15 -08009334 */
9335 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009336 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009337 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009338 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009339 } else {
9340 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9341 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9342 pf->vsi[pf->lan_vsi]->netdev->name);
9343 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009344
Jacob Keller278e7d02016-10-05 09:30:37 -07009345 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009346 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009347 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009348 }
Kiran Patil21659032015-09-30 14:09:03 -04009349
Jacob Keller435c0842016-11-08 13:05:10 -08009350 /* Add the broadcast filter so that we initially will receive
9351 * broadcast packets. Note that when a new VLAN is first added the
9352 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9353 * specific filters as part of transitioning into "vlan" operation.
9354 * When more VLANs are added, the driver will copy each existing MAC
9355 * filter and add it for the new VLAN.
9356 *
9357 * Broadcast filters are handled specially by
9358 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9359 * promiscuous bit instead of adding this directly as a MAC/VLAN
9360 * filter. The subtask will update the correct broadcast promiscuous
9361 * bits as VLANs become active or inactive.
9362 */
9363 eth_broadcast_addr(broadcast);
9364 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009365 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -08009366 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9367
Greg Rose9a173902014-05-22 06:32:02 +00009368 ether_addr_copy(netdev->dev_addr, mac_addr);
9369 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009370
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009371 netdev->priv_flags |= IFF_UNICAST_FLT;
9372 netdev->priv_flags |= IFF_SUPP_NOFCS;
9373 /* Setup netdev TC information */
9374 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9375
9376 netdev->netdev_ops = &i40e_netdev_ops;
9377 netdev->watchdog_timeo = 5 * HZ;
9378 i40e_set_ethtool_ops(netdev);
9379
Jarod Wilson91c527a2016-10-17 15:54:05 -04009380 /* MTU range: 68 - 9706 */
9381 netdev->min_mtu = ETH_MIN_MTU;
9382 netdev->max_mtu = I40E_MAX_RXBUFFER -
9383 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9384
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009385 return 0;
9386}
9387
9388/**
9389 * i40e_vsi_delete - Delete a VSI from the switch
9390 * @vsi: the VSI being removed
9391 *
9392 * Returns 0 on success, negative value on failure
9393 **/
9394static void i40e_vsi_delete(struct i40e_vsi *vsi)
9395{
9396 /* remove default VSI is not allowed */
9397 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9398 return;
9399
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009400 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009401}
9402
9403/**
Neerav Parikh51616012015-02-06 08:52:14 +00009404 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9405 * @vsi: the VSI being queried
9406 *
9407 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9408 **/
9409int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9410{
9411 struct i40e_veb *veb;
9412 struct i40e_pf *pf = vsi->back;
9413
9414 /* Uplink is not a bridge so default to VEB */
9415 if (vsi->veb_idx == I40E_NO_VEB)
9416 return 1;
9417
9418 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009419 if (!veb) {
9420 dev_info(&pf->pdev->dev,
9421 "There is no veb associated with the bridge\n");
9422 return -ENOENT;
9423 }
Neerav Parikh51616012015-02-06 08:52:14 +00009424
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009425 /* Uplink is a bridge in VEPA mode */
9426 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9427 return 0;
9428 } else {
9429 /* Uplink is a bridge in VEB mode */
9430 return 1;
9431 }
9432
9433 /* VEPA is now default bridge, so return 0 */
9434 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009435}
9436
9437/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009438 * i40e_add_vsi - Add a VSI to the switch
9439 * @vsi: the VSI being configured
9440 *
9441 * This initializes a VSI context depending on the VSI type to be added and
9442 * passes it down to the add_vsi aq command.
9443 **/
9444static int i40e_add_vsi(struct i40e_vsi *vsi)
9445{
9446 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009447 struct i40e_pf *pf = vsi->back;
9448 struct i40e_hw *hw = &pf->hw;
9449 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009450 struct i40e_mac_filter *f;
9451 struct hlist_node *h;
9452 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009453
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009454 u8 enabled_tc = 0x1; /* TC0 enabled */
9455 int f_count = 0;
9456
9457 memset(&ctxt, 0, sizeof(ctxt));
9458 switch (vsi->type) {
9459 case I40E_VSI_MAIN:
9460 /* The PF's main VSI is already setup as part of the
9461 * device initialization, so we'll not bother with
9462 * the add_vsi call, but we will retrieve the current
9463 * VSI context.
9464 */
9465 ctxt.seid = pf->main_vsi_seid;
9466 ctxt.pf_num = pf->hw.pf_id;
9467 ctxt.vf_num = 0;
9468 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9469 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9470 if (ret) {
9471 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009472 "couldn't get PF vsi config, err %s aq_err %s\n",
9473 i40e_stat_str(&pf->hw, ret),
9474 i40e_aq_str(&pf->hw,
9475 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009476 return -ENOENT;
9477 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009478 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009479 vsi->info.valid_sections = 0;
9480
9481 vsi->seid = ctxt.seid;
9482 vsi->id = ctxt.vsi_number;
9483
9484 enabled_tc = i40e_pf_get_tc_map(pf);
9485
9486 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009487 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9488 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009489 memset(&ctxt, 0, sizeof(ctxt));
9490 ctxt.seid = pf->main_vsi_seid;
9491 ctxt.pf_num = pf->hw.pf_id;
9492 ctxt.vf_num = 0;
9493 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9494 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9495 if (ret) {
9496 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009497 "update vsi failed, err %s aq_err %s\n",
9498 i40e_stat_str(&pf->hw, ret),
9499 i40e_aq_str(&pf->hw,
9500 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009501 ret = -ENOENT;
9502 goto err;
9503 }
9504 /* update the local VSI info queue map */
9505 i40e_vsi_update_queue_map(vsi, &ctxt);
9506 vsi->info.valid_sections = 0;
9507 } else {
9508 /* Default/Main VSI is only enabled for TC0
9509 * reconfigure it to enable all TCs that are
9510 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009511 * For MFP case the iSCSI PF would use this
9512 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009513 */
9514 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9515 if (ret) {
9516 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009517 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9518 enabled_tc,
9519 i40e_stat_str(&pf->hw, ret),
9520 i40e_aq_str(&pf->hw,
9521 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009522 ret = -ENOENT;
9523 }
9524 }
9525 break;
9526
9527 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009528 ctxt.pf_num = hw->pf_id;
9529 ctxt.vf_num = 0;
9530 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009531 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009532 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009533 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9534 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009535 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009536 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009537 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009538 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009539 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009540 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009541 break;
9542
9543 case I40E_VSI_VMDQ2:
9544 ctxt.pf_num = hw->pf_id;
9545 ctxt.vf_num = 0;
9546 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009547 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009548 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9549
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009550 /* This VSI is connected to VEB so the switch_id
9551 * should be set to zero by default.
9552 */
Neerav Parikh51616012015-02-06 08:52:14 +00009553 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9554 ctxt.info.valid_sections |=
9555 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9556 ctxt.info.switch_id =
9557 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9558 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009559
9560 /* Setup the VSI tx/rx queue map for TC0 only for now */
9561 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9562 break;
9563
9564 case I40E_VSI_SRIOV:
9565 ctxt.pf_num = hw->pf_id;
9566 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9567 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009568 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009569 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9570
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009571 /* This VSI is connected to VEB so the switch_id
9572 * should be set to zero by default.
9573 */
Neerav Parikh51616012015-02-06 08:52:14 +00009574 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9575 ctxt.info.valid_sections |=
9576 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9577 ctxt.info.switch_id =
9578 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9579 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009580
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009581 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9582 ctxt.info.valid_sections |=
9583 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9584 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009585 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9586 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009587 }
9588
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009589 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9590 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009591 if (pf->vf[vsi->vf_id].spoofchk) {
9592 ctxt.info.valid_sections |=
9593 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9594 ctxt.info.sec_flags |=
9595 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9596 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9597 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009598 /* Setup the VSI tx/rx queue map for TC0 only for now */
9599 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9600 break;
9601
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009602 case I40E_VSI_IWARP:
9603 /* send down message to iWARP */
9604 break;
9605
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009606 default:
9607 return -ENODEV;
9608 }
9609
9610 if (vsi->type != I40E_VSI_MAIN) {
9611 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9612 if (ret) {
9613 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009614 "add vsi failed, err %s aq_err %s\n",
9615 i40e_stat_str(&pf->hw, ret),
9616 i40e_aq_str(&pf->hw,
9617 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009618 ret = -ENOENT;
9619 goto err;
9620 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009621 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009622 vsi->info.valid_sections = 0;
9623 vsi->seid = ctxt.seid;
9624 vsi->id = ctxt.vsi_number;
9625 }
9626
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009627 vsi->active_filters = 0;
9628 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009629 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009630 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009631 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009632 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009633 f_count++;
9634 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009635 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009636
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009637 if (f_count) {
9638 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9639 pf->flags |= I40E_FLAG_FILTER_SYNC;
9640 }
9641
9642 /* Update VSI BW information */
9643 ret = i40e_vsi_get_bw_info(vsi);
9644 if (ret) {
9645 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009646 "couldn't get vsi bw info, err %s aq_err %s\n",
9647 i40e_stat_str(&pf->hw, ret),
9648 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009649 /* VSI is already added so not tearing that up */
9650 ret = 0;
9651 }
9652
9653err:
9654 return ret;
9655}
9656
9657/**
9658 * i40e_vsi_release - Delete a VSI and free its resources
9659 * @vsi: the VSI being removed
9660 *
9661 * Returns 0 on success or < 0 on error
9662 **/
9663int i40e_vsi_release(struct i40e_vsi *vsi)
9664{
Jacob Keller278e7d02016-10-05 09:30:37 -07009665 struct i40e_mac_filter *f;
9666 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009667 struct i40e_veb *veb = NULL;
9668 struct i40e_pf *pf;
9669 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009670 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009671
9672 pf = vsi->back;
9673
9674 /* release of a VEB-owner or last VSI is not allowed */
9675 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9676 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9677 vsi->seid, vsi->uplink_seid);
9678 return -ENODEV;
9679 }
9680 if (vsi == pf->vsi[pf->lan_vsi] &&
9681 !test_bit(__I40E_DOWN, &pf->state)) {
9682 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9683 return -ENODEV;
9684 }
9685
9686 uplink_seid = vsi->uplink_seid;
9687 if (vsi->type != I40E_VSI_SRIOV) {
9688 if (vsi->netdev_registered) {
9689 vsi->netdev_registered = false;
9690 if (vsi->netdev) {
9691 /* results in a call to i40e_close() */
9692 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009693 }
9694 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009695 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009696 }
9697 i40e_vsi_disable_irq(vsi);
9698 }
9699
Jacob Keller278e7d02016-10-05 09:30:37 -07009700 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009701
9702 /* clear the sync flag on all filters */
9703 if (vsi->netdev) {
9704 __dev_uc_unsync(vsi->netdev, NULL);
9705 __dev_mc_unsync(vsi->netdev, NULL);
9706 }
9707
9708 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009709 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009710 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009711
Jacob Keller278e7d02016-10-05 09:30:37 -07009712 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009713
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009714 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009715
9716 i40e_vsi_delete(vsi);
9717 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009718 if (vsi->netdev) {
9719 free_netdev(vsi->netdev);
9720 vsi->netdev = NULL;
9721 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009722 i40e_vsi_clear_rings(vsi);
9723 i40e_vsi_clear(vsi);
9724
9725 /* If this was the last thing on the VEB, except for the
9726 * controlling VSI, remove the VEB, which puts the controlling
9727 * VSI onto the next level down in the switch.
9728 *
9729 * Well, okay, there's one more exception here: don't remove
9730 * the orphan VEBs yet. We'll wait for an explicit remove request
9731 * from up the network stack.
9732 */
Mitch Williams505682c2014-05-20 08:01:37 +00009733 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009734 if (pf->vsi[i] &&
9735 pf->vsi[i]->uplink_seid == uplink_seid &&
9736 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9737 n++; /* count the VSIs */
9738 }
9739 }
9740 for (i = 0; i < I40E_MAX_VEB; i++) {
9741 if (!pf->veb[i])
9742 continue;
9743 if (pf->veb[i]->uplink_seid == uplink_seid)
9744 n++; /* count the VEBs */
9745 if (pf->veb[i]->seid == uplink_seid)
9746 veb = pf->veb[i];
9747 }
9748 if (n == 0 && veb && veb->uplink_seid != 0)
9749 i40e_veb_release(veb);
9750
9751 return 0;
9752}
9753
9754/**
9755 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9756 * @vsi: ptr to the VSI
9757 *
9758 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9759 * corresponding SW VSI structure and initializes num_queue_pairs for the
9760 * newly allocated VSI.
9761 *
9762 * Returns 0 on success or negative on failure
9763 **/
9764static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9765{
9766 int ret = -ENOENT;
9767 struct i40e_pf *pf = vsi->back;
9768
Alexander Duyck493fb302013-09-28 07:01:44 +00009769 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009770 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9771 vsi->seid);
9772 return -EEXIST;
9773 }
9774
9775 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009776 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009777 vsi->seid, vsi->base_vector);
9778 return -EEXIST;
9779 }
9780
Greg Rose90e04072014-03-06 08:59:57 +00009781 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009782 if (ret) {
9783 dev_info(&pf->pdev->dev,
9784 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9785 vsi->num_q_vectors, vsi->seid, ret);
9786 vsi->num_q_vectors = 0;
9787 goto vector_setup_out;
9788 }
9789
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009790 /* In Legacy mode, we do not have to get any other vector since we
9791 * piggyback on the misc/ICR0 for queue interrupts.
9792 */
9793 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9794 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009795 if (vsi->num_q_vectors)
9796 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9797 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009798 if (vsi->base_vector < 0) {
9799 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009800 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9801 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009802 i40e_vsi_free_q_vectors(vsi);
9803 ret = -ENOENT;
9804 goto vector_setup_out;
9805 }
9806
9807vector_setup_out:
9808 return ret;
9809}
9810
9811/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009812 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9813 * @vsi: pointer to the vsi.
9814 *
9815 * This re-allocates a vsi's queue resources.
9816 *
9817 * Returns pointer to the successfully allocated and configured VSI sw struct
9818 * on success, otherwise returns NULL on failure.
9819 **/
9820static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9821{
John Underwoodf5340392016-02-18 09:19:24 -08009822 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009823 u8 enabled_tc;
9824 int ret;
9825
John Underwoodf5340392016-02-18 09:19:24 -08009826 if (!vsi)
9827 return NULL;
9828
9829 pf = vsi->back;
9830
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009831 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9832 i40e_vsi_clear_rings(vsi);
9833
9834 i40e_vsi_free_arrays(vsi, false);
9835 i40e_set_num_rings_in_vsi(vsi);
9836 ret = i40e_vsi_alloc_arrays(vsi, false);
9837 if (ret)
9838 goto err_vsi;
9839
9840 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9841 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009842 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009843 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009844 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009845 goto err_vsi;
9846 }
9847 vsi->base_queue = ret;
9848
9849 /* Update the FW view of the VSI. Force a reset of TC and queue
9850 * layout configurations.
9851 */
9852 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9853 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9854 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9855 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -08009856 if (vsi->type == I40E_VSI_MAIN)
9857 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009858
9859 /* assign it some queues */
9860 ret = i40e_alloc_rings(vsi);
9861 if (ret)
9862 goto err_rings;
9863
9864 /* map all of the rings to the q_vectors */
9865 i40e_vsi_map_rings_to_vectors(vsi);
9866 return vsi;
9867
9868err_rings:
9869 i40e_vsi_free_q_vectors(vsi);
9870 if (vsi->netdev_registered) {
9871 vsi->netdev_registered = false;
9872 unregister_netdev(vsi->netdev);
9873 free_netdev(vsi->netdev);
9874 vsi->netdev = NULL;
9875 }
9876 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9877err_vsi:
9878 i40e_vsi_clear(vsi);
9879 return NULL;
9880}
9881
9882/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009883 * i40e_vsi_setup - Set up a VSI by a given type
9884 * @pf: board private structure
9885 * @type: VSI type
9886 * @uplink_seid: the switch element to link to
9887 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9888 *
9889 * This allocates the sw VSI structure and its queue resources, then add a VSI
9890 * to the identified VEB.
9891 *
9892 * Returns pointer to the successfully allocated and configure VSI sw struct on
9893 * success, otherwise returns NULL on failure.
9894 **/
9895struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9896 u16 uplink_seid, u32 param1)
9897{
9898 struct i40e_vsi *vsi = NULL;
9899 struct i40e_veb *veb = NULL;
9900 int ret, i;
9901 int v_idx;
9902
9903 /* The requested uplink_seid must be either
9904 * - the PF's port seid
9905 * no VEB is needed because this is the PF
9906 * or this is a Flow Director special case VSI
9907 * - seid of an existing VEB
9908 * - seid of a VSI that owns an existing VEB
9909 * - seid of a VSI that doesn't own a VEB
9910 * a new VEB is created and the VSI becomes the owner
9911 * - seid of the PF VSI, which is what creates the first VEB
9912 * this is a special case of the previous
9913 *
9914 * Find which uplink_seid we were given and create a new VEB if needed
9915 */
9916 for (i = 0; i < I40E_MAX_VEB; i++) {
9917 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9918 veb = pf->veb[i];
9919 break;
9920 }
9921 }
9922
9923 if (!veb && uplink_seid != pf->mac_seid) {
9924
Mitch Williams505682c2014-05-20 08:01:37 +00009925 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009926 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9927 vsi = pf->vsi[i];
9928 break;
9929 }
9930 }
9931 if (!vsi) {
9932 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9933 uplink_seid);
9934 return NULL;
9935 }
9936
9937 if (vsi->uplink_seid == pf->mac_seid)
9938 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9939 vsi->tc_config.enabled_tc);
9940 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9941 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9942 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009943 if (veb) {
9944 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9945 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009946 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009947 return NULL;
9948 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009949 /* We come up by default in VEPA mode if SRIOV is not
9950 * already enabled, in which case we can't force VEPA
9951 * mode.
9952 */
9953 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9954 veb->bridge_mode = BRIDGE_MODE_VEPA;
9955 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9956 }
Neerav Parikh51616012015-02-06 08:52:14 +00009957 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009958 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009959 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9960 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9961 veb = pf->veb[i];
9962 }
9963 if (!veb) {
9964 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9965 return NULL;
9966 }
9967
9968 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9969 uplink_seid = veb->seid;
9970 }
9971
9972 /* get vsi sw struct */
9973 v_idx = i40e_vsi_mem_alloc(pf, type);
9974 if (v_idx < 0)
9975 goto err_alloc;
9976 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009977 if (!vsi)
9978 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009979 vsi->type = type;
9980 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9981
9982 if (type == I40E_VSI_MAIN)
9983 pf->lan_vsi = v_idx;
9984 else if (type == I40E_VSI_SRIOV)
9985 vsi->vf_id = param1;
9986 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009987 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9988 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009989 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009990 dev_info(&pf->pdev->dev,
9991 "failed to get tracking for %d queues for VSI %d err=%d\n",
9992 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009993 goto err_vsi;
9994 }
9995 vsi->base_queue = ret;
9996
9997 /* get a VSI from the hardware */
9998 vsi->uplink_seid = uplink_seid;
9999 ret = i40e_add_vsi(vsi);
10000 if (ret)
10001 goto err_vsi;
10002
10003 switch (vsi->type) {
10004 /* setup the netdev if needed */
10005 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010006 /* Apply relevant filters if a platform-specific mac
10007 * address was selected.
10008 */
10009 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
10010 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
10011 if (ret) {
10012 dev_warn(&pf->pdev->dev,
10013 "could not set up macaddr; err %d\n",
10014 ret);
10015 }
10016 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010017 case I40E_VSI_VMDQ2:
10018 ret = i40e_config_netdev(vsi);
10019 if (ret)
10020 goto err_netdev;
10021 ret = register_netdev(vsi->netdev);
10022 if (ret)
10023 goto err_netdev;
10024 vsi->netdev_registered = true;
10025 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010026#ifdef CONFIG_I40E_DCB
10027 /* Setup DCB netlink interface */
10028 i40e_dcbnl_setup(vsi);
10029#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010030 /* fall through */
10031
10032 case I40E_VSI_FDIR:
10033 /* set up vectors and rings if needed */
10034 ret = i40e_vsi_setup_vectors(vsi);
10035 if (ret)
10036 goto err_msix;
10037
10038 ret = i40e_alloc_rings(vsi);
10039 if (ret)
10040 goto err_rings;
10041
10042 /* map all of the rings to the q_vectors */
10043 i40e_vsi_map_rings_to_vectors(vsi);
10044
10045 i40e_vsi_reset_stats(vsi);
10046 break;
10047
10048 default:
10049 /* no netdev or rings for the other VSI types */
10050 break;
10051 }
10052
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010053 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10054 (vsi->type == I40E_VSI_VMDQ2)) {
10055 ret = i40e_vsi_config_rss(vsi);
10056 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010057 return vsi;
10058
10059err_rings:
10060 i40e_vsi_free_q_vectors(vsi);
10061err_msix:
10062 if (vsi->netdev_registered) {
10063 vsi->netdev_registered = false;
10064 unregister_netdev(vsi->netdev);
10065 free_netdev(vsi->netdev);
10066 vsi->netdev = NULL;
10067 }
10068err_netdev:
10069 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10070err_vsi:
10071 i40e_vsi_clear(vsi);
10072err_alloc:
10073 return NULL;
10074}
10075
10076/**
10077 * i40e_veb_get_bw_info - Query VEB BW information
10078 * @veb: the veb to query
10079 *
10080 * Query the Tx scheduler BW configuration data for given VEB
10081 **/
10082static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10083{
10084 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10085 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10086 struct i40e_pf *pf = veb->pf;
10087 struct i40e_hw *hw = &pf->hw;
10088 u32 tc_bw_max;
10089 int ret = 0;
10090 int i;
10091
10092 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10093 &bw_data, NULL);
10094 if (ret) {
10095 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010096 "query veb bw config failed, err %s aq_err %s\n",
10097 i40e_stat_str(&pf->hw, ret),
10098 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010099 goto out;
10100 }
10101
10102 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10103 &ets_data, NULL);
10104 if (ret) {
10105 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010106 "query veb bw ets config failed, err %s aq_err %s\n",
10107 i40e_stat_str(&pf->hw, ret),
10108 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010109 goto out;
10110 }
10111
10112 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10113 veb->bw_max_quanta = ets_data.tc_bw_max;
10114 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010115 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010116 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10117 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10118 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10119 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10120 veb->bw_tc_limit_credits[i] =
10121 le16_to_cpu(bw_data.tc_bw_limits[i]);
10122 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10123 }
10124
10125out:
10126 return ret;
10127}
10128
10129/**
10130 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10131 * @pf: board private structure
10132 *
10133 * On error: returns error code (negative)
10134 * On success: returns vsi index in PF (positive)
10135 **/
10136static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10137{
10138 int ret = -ENOENT;
10139 struct i40e_veb *veb;
10140 int i;
10141
10142 /* Need to protect the allocation of switch elements at the PF level */
10143 mutex_lock(&pf->switch_mutex);
10144
10145 /* VEB list may be fragmented if VEB creation/destruction has
10146 * been happening. We can afford to do a quick scan to look
10147 * for any free slots in the list.
10148 *
10149 * find next empty veb slot, looping back around if necessary
10150 */
10151 i = 0;
10152 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10153 i++;
10154 if (i >= I40E_MAX_VEB) {
10155 ret = -ENOMEM;
10156 goto err_alloc_veb; /* out of VEB slots! */
10157 }
10158
10159 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10160 if (!veb) {
10161 ret = -ENOMEM;
10162 goto err_alloc_veb;
10163 }
10164 veb->pf = pf;
10165 veb->idx = i;
10166 veb->enabled_tc = 1;
10167
10168 pf->veb[i] = veb;
10169 ret = i;
10170err_alloc_veb:
10171 mutex_unlock(&pf->switch_mutex);
10172 return ret;
10173}
10174
10175/**
10176 * i40e_switch_branch_release - Delete a branch of the switch tree
10177 * @branch: where to start deleting
10178 *
10179 * This uses recursion to find the tips of the branch to be
10180 * removed, deleting until we get back to and can delete this VEB.
10181 **/
10182static void i40e_switch_branch_release(struct i40e_veb *branch)
10183{
10184 struct i40e_pf *pf = branch->pf;
10185 u16 branch_seid = branch->seid;
10186 u16 veb_idx = branch->idx;
10187 int i;
10188
10189 /* release any VEBs on this VEB - RECURSION */
10190 for (i = 0; i < I40E_MAX_VEB; i++) {
10191 if (!pf->veb[i])
10192 continue;
10193 if (pf->veb[i]->uplink_seid == branch->seid)
10194 i40e_switch_branch_release(pf->veb[i]);
10195 }
10196
10197 /* Release the VSIs on this VEB, but not the owner VSI.
10198 *
10199 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10200 * the VEB itself, so don't use (*branch) after this loop.
10201 */
Mitch Williams505682c2014-05-20 08:01:37 +000010202 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010203 if (!pf->vsi[i])
10204 continue;
10205 if (pf->vsi[i]->uplink_seid == branch_seid &&
10206 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10207 i40e_vsi_release(pf->vsi[i]);
10208 }
10209 }
10210
10211 /* There's one corner case where the VEB might not have been
10212 * removed, so double check it here and remove it if needed.
10213 * This case happens if the veb was created from the debugfs
10214 * commands and no VSIs were added to it.
10215 */
10216 if (pf->veb[veb_idx])
10217 i40e_veb_release(pf->veb[veb_idx]);
10218}
10219
10220/**
10221 * i40e_veb_clear - remove veb struct
10222 * @veb: the veb to remove
10223 **/
10224static void i40e_veb_clear(struct i40e_veb *veb)
10225{
10226 if (!veb)
10227 return;
10228
10229 if (veb->pf) {
10230 struct i40e_pf *pf = veb->pf;
10231
10232 mutex_lock(&pf->switch_mutex);
10233 if (pf->veb[veb->idx] == veb)
10234 pf->veb[veb->idx] = NULL;
10235 mutex_unlock(&pf->switch_mutex);
10236 }
10237
10238 kfree(veb);
10239}
10240
10241/**
10242 * i40e_veb_release - Delete a VEB and free its resources
10243 * @veb: the VEB being removed
10244 **/
10245void i40e_veb_release(struct i40e_veb *veb)
10246{
10247 struct i40e_vsi *vsi = NULL;
10248 struct i40e_pf *pf;
10249 int i, n = 0;
10250
10251 pf = veb->pf;
10252
10253 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010254 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010255 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10256 n++;
10257 vsi = pf->vsi[i];
10258 }
10259 }
10260 if (n != 1) {
10261 dev_info(&pf->pdev->dev,
10262 "can't remove VEB %d with %d VSIs left\n",
10263 veb->seid, n);
10264 return;
10265 }
10266
10267 /* move the remaining VSI to uplink veb */
10268 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10269 if (veb->uplink_seid) {
10270 vsi->uplink_seid = veb->uplink_seid;
10271 if (veb->uplink_seid == pf->mac_seid)
10272 vsi->veb_idx = I40E_NO_VEB;
10273 else
10274 vsi->veb_idx = veb->veb_idx;
10275 } else {
10276 /* floating VEB */
10277 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10278 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10279 }
10280
10281 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10282 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010283}
10284
10285/**
10286 * i40e_add_veb - create the VEB in the switch
10287 * @veb: the VEB to be instantiated
10288 * @vsi: the controlling VSI
10289 **/
10290static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10291{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010292 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010293 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010294 int ret;
10295
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010296 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010297 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010298 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010299
10300 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010301 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010302 dev_info(&pf->pdev->dev,
10303 "couldn't add VEB, err %s aq_err %s\n",
10304 i40e_stat_str(&pf->hw, ret),
10305 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010306 return -EPERM;
10307 }
10308
10309 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010310 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010311 &veb->stats_idx, NULL, NULL, NULL);
10312 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010313 dev_info(&pf->pdev->dev,
10314 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10315 i40e_stat_str(&pf->hw, ret),
10316 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010317 return -EPERM;
10318 }
10319 ret = i40e_veb_get_bw_info(veb);
10320 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010321 dev_info(&pf->pdev->dev,
10322 "couldn't get VEB bw info, err %s aq_err %s\n",
10323 i40e_stat_str(&pf->hw, ret),
10324 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10325 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010326 return -ENOENT;
10327 }
10328
10329 vsi->uplink_seid = veb->seid;
10330 vsi->veb_idx = veb->idx;
10331 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10332
10333 return 0;
10334}
10335
10336/**
10337 * i40e_veb_setup - Set up a VEB
10338 * @pf: board private structure
10339 * @flags: VEB setup flags
10340 * @uplink_seid: the switch element to link to
10341 * @vsi_seid: the initial VSI seid
10342 * @enabled_tc: Enabled TC bit-map
10343 *
10344 * This allocates the sw VEB structure and links it into the switch
10345 * It is possible and legal for this to be a duplicate of an already
10346 * existing VEB. It is also possible for both uplink and vsi seids
10347 * to be zero, in order to create a floating VEB.
10348 *
10349 * Returns pointer to the successfully allocated VEB sw struct on
10350 * success, otherwise returns NULL on failure.
10351 **/
10352struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10353 u16 uplink_seid, u16 vsi_seid,
10354 u8 enabled_tc)
10355{
10356 struct i40e_veb *veb, *uplink_veb = NULL;
10357 int vsi_idx, veb_idx;
10358 int ret;
10359
10360 /* if one seid is 0, the other must be 0 to create a floating relay */
10361 if ((uplink_seid == 0 || vsi_seid == 0) &&
10362 (uplink_seid + vsi_seid != 0)) {
10363 dev_info(&pf->pdev->dev,
10364 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10365 uplink_seid, vsi_seid);
10366 return NULL;
10367 }
10368
10369 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010370 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010371 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10372 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010373 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010374 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10375 vsi_seid);
10376 return NULL;
10377 }
10378
10379 if (uplink_seid && uplink_seid != pf->mac_seid) {
10380 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10381 if (pf->veb[veb_idx] &&
10382 pf->veb[veb_idx]->seid == uplink_seid) {
10383 uplink_veb = pf->veb[veb_idx];
10384 break;
10385 }
10386 }
10387 if (!uplink_veb) {
10388 dev_info(&pf->pdev->dev,
10389 "uplink seid %d not found\n", uplink_seid);
10390 return NULL;
10391 }
10392 }
10393
10394 /* get veb sw struct */
10395 veb_idx = i40e_veb_mem_alloc(pf);
10396 if (veb_idx < 0)
10397 goto err_alloc;
10398 veb = pf->veb[veb_idx];
10399 veb->flags = flags;
10400 veb->uplink_seid = uplink_seid;
10401 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10402 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10403
10404 /* create the VEB in the switch */
10405 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10406 if (ret)
10407 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010408 if (vsi_idx == pf->lan_vsi)
10409 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010410
10411 return veb;
10412
10413err_veb:
10414 i40e_veb_clear(veb);
10415err_alloc:
10416 return NULL;
10417}
10418
10419/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010420 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010421 * @pf: board private structure
10422 * @ele: element we are building info from
10423 * @num_reported: total number of elements
10424 * @printconfig: should we print the contents
10425 *
10426 * helper function to assist in extracting a few useful SEID values.
10427 **/
10428static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10429 struct i40e_aqc_switch_config_element_resp *ele,
10430 u16 num_reported, bool printconfig)
10431{
10432 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10433 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10434 u8 element_type = ele->element_type;
10435 u16 seid = le16_to_cpu(ele->seid);
10436
10437 if (printconfig)
10438 dev_info(&pf->pdev->dev,
10439 "type=%d seid=%d uplink=%d downlink=%d\n",
10440 element_type, seid, uplink_seid, downlink_seid);
10441
10442 switch (element_type) {
10443 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10444 pf->mac_seid = seid;
10445 break;
10446 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10447 /* Main VEB? */
10448 if (uplink_seid != pf->mac_seid)
10449 break;
10450 if (pf->lan_veb == I40E_NO_VEB) {
10451 int v;
10452
10453 /* find existing or else empty VEB */
10454 for (v = 0; v < I40E_MAX_VEB; v++) {
10455 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10456 pf->lan_veb = v;
10457 break;
10458 }
10459 }
10460 if (pf->lan_veb == I40E_NO_VEB) {
10461 v = i40e_veb_mem_alloc(pf);
10462 if (v < 0)
10463 break;
10464 pf->lan_veb = v;
10465 }
10466 }
10467
10468 pf->veb[pf->lan_veb]->seid = seid;
10469 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10470 pf->veb[pf->lan_veb]->pf = pf;
10471 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10472 break;
10473 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10474 if (num_reported != 1)
10475 break;
10476 /* This is immediately after a reset so we can assume this is
10477 * the PF's VSI
10478 */
10479 pf->mac_seid = uplink_seid;
10480 pf->pf_seid = downlink_seid;
10481 pf->main_vsi_seid = seid;
10482 if (printconfig)
10483 dev_info(&pf->pdev->dev,
10484 "pf_seid=%d main_vsi_seid=%d\n",
10485 pf->pf_seid, pf->main_vsi_seid);
10486 break;
10487 case I40E_SWITCH_ELEMENT_TYPE_PF:
10488 case I40E_SWITCH_ELEMENT_TYPE_VF:
10489 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10490 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10491 case I40E_SWITCH_ELEMENT_TYPE_PE:
10492 case I40E_SWITCH_ELEMENT_TYPE_PA:
10493 /* ignore these for now */
10494 break;
10495 default:
10496 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10497 element_type, seid);
10498 break;
10499 }
10500}
10501
10502/**
10503 * i40e_fetch_switch_configuration - Get switch config from firmware
10504 * @pf: board private structure
10505 * @printconfig: should we print the contents
10506 *
10507 * Get the current switch configuration from the device and
10508 * extract a few useful SEID values.
10509 **/
10510int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10511{
10512 struct i40e_aqc_get_switch_config_resp *sw_config;
10513 u16 next_seid = 0;
10514 int ret = 0;
10515 u8 *aq_buf;
10516 int i;
10517
10518 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10519 if (!aq_buf)
10520 return -ENOMEM;
10521
10522 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10523 do {
10524 u16 num_reported, num_total;
10525
10526 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10527 I40E_AQ_LARGE_BUF,
10528 &next_seid, NULL);
10529 if (ret) {
10530 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010531 "get switch config failed err %s aq_err %s\n",
10532 i40e_stat_str(&pf->hw, ret),
10533 i40e_aq_str(&pf->hw,
10534 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010535 kfree(aq_buf);
10536 return -ENOENT;
10537 }
10538
10539 num_reported = le16_to_cpu(sw_config->header.num_reported);
10540 num_total = le16_to_cpu(sw_config->header.num_total);
10541
10542 if (printconfig)
10543 dev_info(&pf->pdev->dev,
10544 "header: %d reported %d total\n",
10545 num_reported, num_total);
10546
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010547 for (i = 0; i < num_reported; i++) {
10548 struct i40e_aqc_switch_config_element_resp *ele =
10549 &sw_config->element[i];
10550
10551 i40e_setup_pf_switch_element(pf, ele, num_reported,
10552 printconfig);
10553 }
10554 } while (next_seid != 0);
10555
10556 kfree(aq_buf);
10557 return ret;
10558}
10559
10560/**
10561 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10562 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010563 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010564 *
10565 * Returns 0 on success, negative value on failure
10566 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010567static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010568{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010569 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010570 int ret;
10571
10572 /* find out what's out there already */
10573 ret = i40e_fetch_switch_configuration(pf, false);
10574 if (ret) {
10575 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010576 "couldn't fetch switch config, err %s aq_err %s\n",
10577 i40e_stat_str(&pf->hw, ret),
10578 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010579 return ret;
10580 }
10581 i40e_pf_reset_stats(pf);
10582
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010583 /* set the switch config bit for the whole device to
10584 * support limited promisc or true promisc
10585 * when user requests promisc. The default is limited
10586 * promisc.
10587 */
10588
10589 if ((pf->hw.pf_id == 0) &&
10590 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10591 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10592
10593 if (pf->hw.pf_id == 0) {
10594 u16 valid_flags;
10595
10596 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10597 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10598 NULL);
10599 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10600 dev_info(&pf->pdev->dev,
10601 "couldn't set switch config bits, err %s aq_err %s\n",
10602 i40e_stat_str(&pf->hw, ret),
10603 i40e_aq_str(&pf->hw,
10604 pf->hw.aq.asq_last_status));
10605 /* not a fatal problem, just keep going */
10606 }
10607 }
10608
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010609 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010610 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010611 struct i40e_vsi *vsi = NULL;
10612 u16 uplink_seid;
10613
10614 /* Set up the PF VSI associated with the PF's main VSI
10615 * that is already in the HW switch
10616 */
10617 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10618 uplink_seid = pf->veb[pf->lan_veb]->seid;
10619 else
10620 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010621 if (pf->lan_vsi == I40E_NO_VSI)
10622 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10623 else if (reinit)
10624 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010625 if (!vsi) {
10626 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10627 i40e_fdir_teardown(pf);
10628 return -EAGAIN;
10629 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010630 } else {
10631 /* force a reset of TC and queue layout configurations */
10632 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010633
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010634 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10635 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10636 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10637 }
10638 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10639
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010640 i40e_fdir_sb_setup(pf);
10641
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010642 /* Setup static PF queue filter control settings */
10643 ret = i40e_setup_pf_filter_control(pf);
10644 if (ret) {
10645 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10646 ret);
10647 /* Failure here should not stop continuing other steps */
10648 }
10649
10650 /* enable RSS in the HW, even for only one queue, as the stack can use
10651 * the hash
10652 */
10653 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010654 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010655
10656 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010657 i40e_link_event(pf);
10658
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010659 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010660 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10661 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010662
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010663 i40e_ptp_init(pf);
10664
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010665 return ret;
10666}
10667
10668/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010669 * i40e_determine_queue_usage - Work out queue distribution
10670 * @pf: board private structure
10671 **/
10672static void i40e_determine_queue_usage(struct i40e_pf *pf)
10673{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010674 int queues_left;
10675
10676 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010677
10678 /* Find the max queues to be put into basic use. We'll always be
10679 * using TC0, whether or not DCB is running, and TC0 will get the
10680 * big RSS set.
10681 */
10682 queues_left = pf->hw.func_caps.num_tx_qp;
10683
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010684 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010685 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010686 /* one qp for PF, no queues for anything else */
10687 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010688 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010689
10690 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010691 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010692 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010693 I40E_FLAG_FD_SB_ENABLED |
10694 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010695 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010696 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010697 I40E_FLAG_SRIOV_ENABLED |
10698 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010699 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10700 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010701 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010702 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010703 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010704 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010705 queues_left -= pf->num_lan_qps;
10706
10707 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010708 I40E_FLAG_IWARP_ENABLED |
Frank Zhang9aa7e932014-05-20 08:01:42 +000010709 I40E_FLAG_FD_SB_ENABLED |
10710 I40E_FLAG_FD_ATR_ENABLED |
10711 I40E_FLAG_DCB_ENABLED |
10712 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010713 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010714 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010715 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010716 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010717 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10718 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010719 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10720 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010721 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10722 num_online_cpus());
10723 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10724 pf->hw.func_caps.num_tx_qp);
10725
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010726 queues_left -= pf->num_lan_qps;
10727 }
10728
10729 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10730 if (queues_left > 1) {
10731 queues_left -= 1; /* save 1 queue for FD */
10732 } else {
10733 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10734 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10735 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010736 }
10737
10738 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10739 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010740 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10741 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010742 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10743 }
10744
10745 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10746 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10747 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10748 (queues_left / pf->num_vmdq_qps));
10749 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10750 }
10751
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010752 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010753 dev_dbg(&pf->pdev->dev,
10754 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10755 pf->hw.func_caps.num_tx_qp,
10756 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010757 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10758 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10759 queues_left);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010760}
10761
10762/**
10763 * i40e_setup_pf_filter_control - Setup PF static filter control
10764 * @pf: PF to be setup
10765 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010766 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010767 * settings. If PE/FCoE are enabled then it will also set the per PF
10768 * based filter sizes required for them. It also enables Flow director,
10769 * ethertype and macvlan type filter settings for the pf.
10770 *
10771 * Returns 0 on success, negative on failure
10772 **/
10773static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10774{
10775 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10776
10777 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10778
10779 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010780 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010781 settings->enable_fdir = true;
10782
10783 /* Ethtype and MACVLAN filters enabled for PF */
10784 settings->enable_ethtype = true;
10785 settings->enable_macvlan = true;
10786
10787 if (i40e_set_filter_control(&pf->hw, settings))
10788 return -ENOENT;
10789
10790 return 0;
10791}
10792
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010793#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010794#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010795static void i40e_print_features(struct i40e_pf *pf)
10796{
10797 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010798 char *buf;
10799 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010800
Joe Perches3b195842015-12-03 04:20:57 -080010801 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10802 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010803 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010804
Joe Perches3b195842015-12-03 04:20:57 -080010805 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010806#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010807 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010808#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010809 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010810 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010811 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010812 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010813 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010814 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010815 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010816 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010817 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10818 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010819 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010820 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010821 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010822 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010823 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010824 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010825 i += snprintf(&buf[i], REMAIN(i), " PTP");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010826 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010827 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010828 else
Joe Perches3b195842015-12-03 04:20:57 -080010829 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010830
Joe Perches3b195842015-12-03 04:20:57 -080010831 dev_info(&pf->pdev->dev, "%s\n", buf);
10832 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010833 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010834}
10835
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010836/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010837 * i40e_get_platform_mac_addr - get platform-specific MAC address
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010838 * @pdev: PCI device information struct
10839 * @pf: board private structure
10840 *
Jacob Keller41c4c2b2017-04-05 07:50:57 -040010841 * Look up the MAC address for the device. First we'll try
10842 * eth_platform_get_mac_address, which will check Open Firmware, or arch
10843 * specific fallback. Otherwise, we'll default to the stored value in
10844 * firmware.
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010845 **/
10846static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10847{
Jacob Keller41c4c2b2017-04-05 07:50:57 -040010848 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
10849 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010850}
10851
10852/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010853 * i40e_probe - Device initialization routine
10854 * @pdev: PCI device information struct
10855 * @ent: entry in i40e_pci_tbl
10856 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010857 * i40e_probe initializes a PF identified by a pci_dev structure.
10858 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010859 * and a hardware reset occur.
10860 *
10861 * Returns 0 on success, negative on failure
10862 **/
10863static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10864{
Catherine Sullivane8278452015-02-06 08:52:08 +000010865 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010866 struct i40e_pf *pf;
10867 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010868 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010869 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010870 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010871 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010872 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010873 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010874 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010875
10876 err = pci_enable_device_mem(pdev);
10877 if (err)
10878 return err;
10879
10880 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010881 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010882 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010883 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10884 if (err) {
10885 dev_err(&pdev->dev,
10886 "DMA configuration failed: 0x%x\n", err);
10887 goto err_dma;
10888 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010889 }
10890
10891 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010892 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010893 if (err) {
10894 dev_info(&pdev->dev,
10895 "pci_request_selected_regions failed %d\n", err);
10896 goto err_pci_reg;
10897 }
10898
10899 pci_enable_pcie_error_reporting(pdev);
10900 pci_set_master(pdev);
10901
10902 /* Now that we have a PCI connection, we need to do the
10903 * low level device setup. This is primarily setting up
10904 * the Admin Queue structures and then querying for the
10905 * device's current profile information.
10906 */
10907 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10908 if (!pf) {
10909 err = -ENOMEM;
10910 goto err_pf_alloc;
10911 }
10912 pf->next_vsi = 0;
10913 pf->pdev = pdev;
10914 set_bit(__I40E_DOWN, &pf->state);
10915
10916 hw = &pf->hw;
10917 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010918
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010919 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10920 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010921
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010922 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010923 if (!hw->hw_addr) {
10924 err = -EIO;
10925 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10926 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010927 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010928 goto err_ioremap;
10929 }
10930 hw->vendor_id = pdev->vendor;
10931 hw->device_id = pdev->device;
10932 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10933 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10934 hw->subsystem_device_id = pdev->subsystem_device;
10935 hw->bus.device = PCI_SLOT(pdev->devfn);
10936 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080010937 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010938 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010939
Jacob Keller0e588de2017-02-06 14:38:50 -080010940 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
10941 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
10942
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010943 /* set up the locks for the AQ, do this only once in probe
10944 * and destroy them only once in remove
10945 */
10946 mutex_init(&hw->aq.asq_mutex);
10947 mutex_init(&hw->aq.arq_mutex);
10948
Alexander Duyck5d4ca232016-09-30 08:21:46 -040010949 pf->msg_enable = netif_msg_init(debug,
10950 NETIF_MSG_DRV |
10951 NETIF_MSG_PROBE |
10952 NETIF_MSG_LINK);
10953 if (debug < -1)
10954 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010955
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010956 /* do a special CORER for clearing PXE mode once at init */
10957 if (hw->revision_id == 0 &&
10958 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10959 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10960 i40e_flush(hw);
10961 msleep(200);
10962 pf->corer_count++;
10963
10964 i40e_clear_pxe_mode(hw);
10965 }
10966
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010967 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010968 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010969 err = i40e_pf_reset(hw);
10970 if (err) {
10971 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10972 goto err_pf_reset;
10973 }
10974 pf->pfr_count++;
10975
10976 hw->aq.num_arq_entries = I40E_AQ_LEN;
10977 hw->aq.num_asq_entries = I40E_AQ_LEN;
10978 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10979 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10980 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010981
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010982 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010983 "%s-%s:misc",
10984 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010985
10986 err = i40e_init_shared_code(hw);
10987 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010988 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10989 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010990 goto err_pf_reset;
10991 }
10992
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010993 /* set up a default setting for link flow control */
10994 pf->hw.fc.requested_mode = I40E_FC_NONE;
10995
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010996 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040010997 if (err) {
10998 if (err == I40E_ERR_FIRMWARE_API_VERSION)
10999 dev_info(&pdev->dev,
11000 "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");
11001 else
11002 dev_info(&pdev->dev,
11003 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11004
11005 goto err_pf_reset;
11006 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011007
Shannon Nelson6dec1012015-09-28 14:12:30 -040011008 /* provide nvm, fw, api versions */
11009 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11010 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11011 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11012 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011013
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011014 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
11015 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011016 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011017 "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");
11018 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
11019 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011020 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011021 "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 +000011022
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011023 i40e_verify_eeprom(pf);
11024
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011025 /* Rev 0 hardware was never productized */
11026 if (hw->revision_id < 1)
11027 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");
11028
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011029 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011030 err = i40e_get_capabilities(pf);
11031 if (err)
11032 goto err_adminq_setup;
11033
11034 err = i40e_sw_init(pf);
11035 if (err) {
11036 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11037 goto err_sw_init;
11038 }
11039
11040 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -080011041 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011042 if (err) {
11043 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11044 goto err_init_lan_hmc;
11045 }
11046
11047 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11048 if (err) {
11049 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11050 err = -ENOENT;
11051 goto err_configure_lan_hmc;
11052 }
11053
Neerav Parikhb686ece2014-12-14 01:55:11 +000011054 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11055 * Ignore error return codes because if it was already disabled via
11056 * hardware settings this will fail
11057 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011058 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011059 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11060 i40e_aq_stop_lldp(hw, true, NULL);
11061 }
11062
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011063 /* allow a platform config to override the HW addr */
11064 i40e_get_platform_mac_addr(pdev, pf);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011065
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011066 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011067 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11068 err = -EIO;
11069 goto err_mac_addr;
11070 }
11071 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011072 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011073 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11074 if (is_valid_ether_addr(hw->mac.port_addr))
11075 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011076
11077 pci_set_drvdata(pdev, pf);
11078 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011079#ifdef CONFIG_I40E_DCB
11080 err = i40e_init_pf_dcb(pf);
11081 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011082 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011083 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011084 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011085 }
11086#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011087
11088 /* set up periodic task facility */
11089 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11090 pf->service_timer_period = HZ;
11091
11092 INIT_WORK(&pf->service_task, i40e_service_task);
11093 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011094
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011095 /* NVM bit on means WoL disabled for the port */
11096 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011097 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011098 pf->wol_en = false;
11099 else
11100 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011101 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11102
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011103 /* set up the main switch operations */
11104 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011105 err = i40e_init_interrupt_scheme(pf);
11106 if (err)
11107 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011108
Mitch Williams505682c2014-05-20 08:01:37 +000011109 /* The number of VSIs reported by the FW is the minimum guaranteed
11110 * to us; HW supports far more and we share the remaining pool with
11111 * the other PFs. We allocate space for more than the guarantee with
11112 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011113 */
Mitch Williams505682c2014-05-20 08:01:37 +000011114 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11115 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11116 else
11117 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11118
11119 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011120 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11121 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011122 if (!pf->vsi) {
11123 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011124 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011125 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011126
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011127#ifdef CONFIG_PCI_IOV
11128 /* prep for VF support */
11129 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11130 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11131 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11132 if (pci_num_vf(pdev))
11133 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11134 }
11135#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011136 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011137 if (err) {
11138 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11139 goto err_vsis;
11140 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011141
11142 /* Make sure flow control is set according to current settings */
11143 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11144 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11145 dev_dbg(&pf->pdev->dev,
11146 "Set fc with err %s aq_err %s on get_phy_cap\n",
11147 i40e_stat_str(hw, err),
11148 i40e_aq_str(hw, hw->aq.asq_last_status));
11149 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11150 dev_dbg(&pf->pdev->dev,
11151 "Set fc with err %s aq_err %s on set_phy_config\n",
11152 i40e_stat_str(hw, err),
11153 i40e_aq_str(hw, hw->aq.asq_last_status));
11154 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11155 dev_dbg(&pf->pdev->dev,
11156 "Set fc with err %s aq_err %s on get_link_info\n",
11157 i40e_stat_str(hw, err),
11158 i40e_aq_str(hw, hw->aq.asq_last_status));
11159
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011160 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011161 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011162 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11163 i40e_vsi_open(pf->vsi[i]);
11164 break;
11165 }
11166 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011167
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011168 /* The driver only wants link up/down and module qualification
11169 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011170 */
11171 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011172 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011173 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011174 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011175 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011176 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11177 i40e_stat_str(&pf->hw, err),
11178 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011179
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011180 /* Reconfigure hardware for allowing smaller MSS in the case
11181 * of TSO, so that we avoid the MDD being fired and causing
11182 * a reset in the case of small MSS+TSO.
11183 */
11184 val = rd32(hw, I40E_REG_MSS);
11185 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11186 val &= ~I40E_REG_MSS_MIN_MASK;
11187 val |= I40E_64BYTE_MSS;
11188 wr32(hw, I40E_REG_MSS, val);
11189 }
11190
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011191 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011192 msleep(75);
11193 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11194 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011195 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11196 i40e_stat_str(&pf->hw, err),
11197 i40e_aq_str(&pf->hw,
11198 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011199 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011200 /* The main driver is (mostly) up and happy. We need to set this state
11201 * before setting up the misc vector or we get a race and the vector
11202 * ends up disabled forever.
11203 */
11204 clear_bit(__I40E_DOWN, &pf->state);
11205
11206 /* In case of MSIX we are going to setup the misc vector right here
11207 * to handle admin queue events etc. In case of legacy and MSI
11208 * the misc functionality and queue processing is combined in
11209 * the same vector and that gets setup at open.
11210 */
11211 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11212 err = i40e_setup_misc_vector(pf);
11213 if (err) {
11214 dev_info(&pdev->dev,
11215 "setup of misc vector failed: %d\n", err);
11216 goto err_vsis;
11217 }
11218 }
11219
Greg Rosedf805f62014-04-04 04:43:16 +000011220#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011221 /* prep for VF support */
11222 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011223 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11224 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011225 /* disable link interrupts for VFs */
11226 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11227 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11228 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11229 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011230
11231 if (pci_num_vf(pdev)) {
11232 dev_info(&pdev->dev,
11233 "Active VFs found, allocating resources.\n");
11234 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11235 if (err)
11236 dev_info(&pdev->dev,
11237 "Error %d allocating resources for existing VFs\n",
11238 err);
11239 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011240 }
Greg Rosedf805f62014-04-04 04:43:16 +000011241#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011242
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011243 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11244 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11245 pf->num_iwarp_msix,
11246 I40E_IWARP_IRQ_PILE_ID);
11247 if (pf->iwarp_base_vector < 0) {
11248 dev_info(&pdev->dev,
11249 "failed to get tracking for %d vectors for IWARP err=%d\n",
11250 pf->num_iwarp_msix, pf->iwarp_base_vector);
11251 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11252 }
11253 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011254
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011255 i40e_dbg_pf_init(pf);
11256
11257 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011258 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011259
11260 /* since everything's happy, start the service_task timer */
11261 mod_timer(&pf->service_timer,
11262 round_jiffies(jiffies + pf->service_timer_period));
11263
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011264 /* add this PF to client device list and launch a client service task */
Mitch Williams004eb612017-04-04 12:40:16 -070011265 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11266 err = i40e_lan_add_device(pf);
11267 if (err)
11268 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11269 err);
11270 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011271
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011272#define PCI_SPEED_SIZE 8
11273#define PCI_WIDTH_SIZE 8
11274 /* Devices on the IOSF bus do not have this information
11275 * and will report PCI Gen 1 x 1 by default so don't bother
11276 * checking them.
11277 */
11278 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11279 char speed[PCI_SPEED_SIZE] = "Unknown";
11280 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011281
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011282 /* Get the negotiated link width and speed from PCI config
11283 * space
11284 */
11285 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11286 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011287
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011288 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011289
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011290 switch (hw->bus.speed) {
11291 case i40e_bus_speed_8000:
11292 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11293 case i40e_bus_speed_5000:
11294 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11295 case i40e_bus_speed_2500:
11296 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11297 default:
11298 break;
11299 }
11300 switch (hw->bus.width) {
11301 case i40e_bus_width_pcie_x8:
11302 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11303 case i40e_bus_width_pcie_x4:
11304 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11305 case i40e_bus_width_pcie_x2:
11306 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11307 case i40e_bus_width_pcie_x1:
11308 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11309 default:
11310 break;
11311 }
11312
11313 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11314 speed, width);
11315
11316 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11317 hw->bus.speed < i40e_bus_speed_8000) {
11318 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11319 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11320 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011321 }
11322
Catherine Sullivane8278452015-02-06 08:52:08 +000011323 /* get the requested speeds from the fw */
11324 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11325 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011326 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11327 i40e_stat_str(&pf->hw, err),
11328 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011329 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11330
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011331 /* get the supported phy types from the fw */
11332 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11333 if (err)
11334 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11335 i40e_stat_str(&pf->hw, err),
11336 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011337
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011338 /* Add a filter to drop all Flow control frames from any VSI from being
11339 * transmitted. By doing so we stop a malicious VF from sending out
11340 * PAUSE or PFC frames and potentially controlling traffic for other
11341 * PF/VF VSIs.
11342 * The FW can still send Flow control frames if enabled.
11343 */
11344 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11345 pf->main_vsi_seid);
11346
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011347 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011348 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11349 pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011350 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11351 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011352 /* print a string summarizing features */
11353 i40e_print_features(pf);
11354
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011355 return 0;
11356
11357 /* Unwind what we've done if something failed in the setup */
11358err_vsis:
11359 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011360 i40e_clear_interrupt_scheme(pf);
11361 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011362err_switch_setup:
11363 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011364 del_timer_sync(&pf->service_timer);
11365err_mac_addr:
11366err_configure_lan_hmc:
11367 (void)i40e_shutdown_lan_hmc(hw);
11368err_init_lan_hmc:
11369 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011370err_sw_init:
11371err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011372err_pf_reset:
11373 iounmap(hw->hw_addr);
11374err_ioremap:
11375 kfree(pf);
11376err_pf_alloc:
11377 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011378 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011379err_pci_reg:
11380err_dma:
11381 pci_disable_device(pdev);
11382 return err;
11383}
11384
11385/**
11386 * i40e_remove - Device removal routine
11387 * @pdev: PCI device information struct
11388 *
11389 * i40e_remove is called by the PCI subsystem to alert the driver
11390 * that is should release a PCI device. This could be caused by a
11391 * Hot-Plug event, or because the driver is going to be removed from
11392 * memory.
11393 **/
11394static void i40e_remove(struct pci_dev *pdev)
11395{
11396 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011397 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011398 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011399 int i;
11400
11401 i40e_dbg_pf_exit(pf);
11402
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011403 i40e_ptp_stop(pf);
11404
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011405 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011406 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11407 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011408
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011409 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011410 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011411 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011412 if (pf->service_timer.data)
11413 del_timer_sync(&pf->service_timer);
11414 if (pf->service_task.func)
11415 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011416
Mitch Williams921c4672017-03-30 00:46:08 -070011417 /* Client close must be called explicitly here because the timer
11418 * has been stopped.
11419 */
11420 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
11421
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011422 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11423 i40e_free_vfs(pf);
11424 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11425 }
11426
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011427 i40e_fdir_teardown(pf);
11428
11429 /* If there is a switch structure or any orphans, remove them.
11430 * This will leave only the PF's VSI remaining.
11431 */
11432 for (i = 0; i < I40E_MAX_VEB; i++) {
11433 if (!pf->veb[i])
11434 continue;
11435
11436 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11437 pf->veb[i]->uplink_seid == 0)
11438 i40e_switch_branch_release(pf->veb[i]);
11439 }
11440
11441 /* Now we can shutdown the PF's VSI, just before we kill
11442 * adminq and hmc.
11443 */
11444 if (pf->vsi[pf->lan_vsi])
11445 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11446
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011447 /* remove attached clients */
Mitch Williams004eb612017-04-04 12:40:16 -070011448 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11449 ret_code = i40e_lan_del_device(pf);
11450 if (ret_code)
11451 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11452 ret_code);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011453 }
11454
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011455 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011456 if (hw->hmc.hmc_obj) {
11457 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011458 if (ret_code)
11459 dev_warn(&pdev->dev,
11460 "Failed to destroy the HMC resources: %d\n",
11461 ret_code);
11462 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011463
11464 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011465 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011466
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011467 /* destroy the locks only once, here */
11468 mutex_destroy(&hw->aq.arq_mutex);
11469 mutex_destroy(&hw->aq.asq_mutex);
11470
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011471 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11472 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011473 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011474 if (pf->vsi[i]) {
11475 i40e_vsi_clear_rings(pf->vsi[i]);
11476 i40e_vsi_clear(pf->vsi[i]);
11477 pf->vsi[i] = NULL;
11478 }
11479 }
11480
11481 for (i = 0; i < I40E_MAX_VEB; i++) {
11482 kfree(pf->veb[i]);
11483 pf->veb[i] = NULL;
11484 }
11485
11486 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011487 kfree(pf->vsi);
11488
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011489 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011490 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011491 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011492
11493 pci_disable_pcie_error_reporting(pdev);
11494 pci_disable_device(pdev);
11495}
11496
11497/**
11498 * i40e_pci_error_detected - warning that something funky happened in PCI land
11499 * @pdev: PCI device information struct
11500 *
11501 * Called to warn that something happened and the error handling steps
11502 * are in progress. Allows the driver to quiesce things, be ready for
11503 * remediation.
11504 **/
11505static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11506 enum pci_channel_state error)
11507{
11508 struct i40e_pf *pf = pci_get_drvdata(pdev);
11509
11510 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11511
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011512 if (!pf) {
11513 dev_info(&pdev->dev,
11514 "Cannot recover - error happened during device probe\n");
11515 return PCI_ERS_RESULT_DISCONNECT;
11516 }
11517
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011518 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011519 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11520 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011521 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011522 rtnl_unlock();
11523 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011524
11525 /* Request a slot reset */
11526 return PCI_ERS_RESULT_NEED_RESET;
11527}
11528
11529/**
11530 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11531 * @pdev: PCI device information struct
11532 *
11533 * Called to find if the driver can work with the device now that
11534 * the pci slot has been reset. If a basic connection seems good
11535 * (registers are readable and have sane content) then return a
11536 * happy little PCI_ERS_RESULT_xxx.
11537 **/
11538static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11539{
11540 struct i40e_pf *pf = pci_get_drvdata(pdev);
11541 pci_ers_result_t result;
11542 int err;
11543 u32 reg;
11544
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011545 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011546 if (pci_enable_device_mem(pdev)) {
11547 dev_info(&pdev->dev,
11548 "Cannot re-enable PCI device after reset.\n");
11549 result = PCI_ERS_RESULT_DISCONNECT;
11550 } else {
11551 pci_set_master(pdev);
11552 pci_restore_state(pdev);
11553 pci_save_state(pdev);
11554 pci_wake_from_d3(pdev, false);
11555
11556 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11557 if (reg == 0)
11558 result = PCI_ERS_RESULT_RECOVERED;
11559 else
11560 result = PCI_ERS_RESULT_DISCONNECT;
11561 }
11562
11563 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11564 if (err) {
11565 dev_info(&pdev->dev,
11566 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11567 err);
11568 /* non-fatal, continue */
11569 }
11570
11571 return result;
11572}
11573
11574/**
11575 * i40e_pci_error_resume - restart operations after PCI error recovery
11576 * @pdev: PCI device information struct
11577 *
11578 * Called to allow the driver to bring things back up after PCI error
11579 * and/or reset recovery has finished.
11580 **/
11581static void i40e_pci_error_resume(struct pci_dev *pdev)
11582{
11583 struct i40e_pf *pf = pci_get_drvdata(pdev);
11584
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011585 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011586 if (test_bit(__I40E_SUSPENDED, &pf->state))
11587 return;
11588
11589 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011590 i40e_handle_reset_warning(pf, true);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011591 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011592}
11593
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011594/**
Joshua Hay1d680052016-12-12 15:44:08 -080011595 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
11596 * using the mac_address_write admin q function
11597 * @pf: pointer to i40e_pf struct
11598 **/
11599static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
11600{
11601 struct i40e_hw *hw = &pf->hw;
11602 i40e_status ret;
11603 u8 mac_addr[6];
11604 u16 flags = 0;
11605
11606 /* Get current MAC address in case it's an LAA */
11607 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
11608 ether_addr_copy(mac_addr,
11609 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
11610 } else {
11611 dev_err(&pf->pdev->dev,
11612 "Failed to retrieve MAC address; using default\n");
11613 ether_addr_copy(mac_addr, hw->mac.addr);
11614 }
11615
11616 /* The FW expects the mac address write cmd to first be called with
11617 * one of these flags before calling it again with the multicast
11618 * enable flags.
11619 */
11620 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
11621
11622 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
11623 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
11624
11625 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11626 if (ret) {
11627 dev_err(&pf->pdev->dev,
11628 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
11629 return;
11630 }
11631
11632 flags = I40E_AQC_MC_MAG_EN
11633 | I40E_AQC_WOL_PRESERVE_ON_PFR
11634 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
11635 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11636 if (ret)
11637 dev_err(&pf->pdev->dev,
11638 "Failed to enable Multicast Magic Packet wake up\n");
11639}
11640
11641/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011642 * i40e_shutdown - PCI callback for shutting down
11643 * @pdev: PCI device information struct
11644 **/
11645static void i40e_shutdown(struct pci_dev *pdev)
11646{
11647 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011648 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011649
11650 set_bit(__I40E_SUSPENDED, &pf->state);
11651 set_bit(__I40E_DOWN, &pf->state);
11652 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011653 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011654 rtnl_unlock();
11655
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011656 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11657 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11658
Catherine Sullivan02b42492015-07-10 19:35:59 -040011659 del_timer_sync(&pf->service_timer);
11660 cancel_work_sync(&pf->service_task);
11661 i40e_fdir_teardown(pf);
11662
Mitch Williams921c4672017-03-30 00:46:08 -070011663 /* Client close must be called explicitly here because the timer
11664 * has been stopped.
11665 */
11666 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
11667
Joshua Hay1d680052016-12-12 15:44:08 -080011668 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11669 i40e_enable_mc_magic_wake(pf);
11670
Catherine Sullivan02b42492015-07-10 19:35:59 -040011671 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011672 i40e_prep_for_reset(pf, true);
Catherine Sullivan02b42492015-07-10 19:35:59 -040011673 rtnl_unlock();
11674
11675 wr32(hw, I40E_PFPM_APM,
11676 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11677 wr32(hw, I40E_PFPM_WUFC,
11678 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11679
Shannon Nelsone1477582015-02-21 06:44:33 +000011680 i40e_clear_interrupt_scheme(pf);
11681
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011682 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011683 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011684 pci_set_power_state(pdev, PCI_D3hot);
11685 }
11686}
11687
11688#ifdef CONFIG_PM
11689/**
11690 * i40e_suspend - PCI callback for moving to D3
11691 * @pdev: PCI device information struct
11692 **/
11693static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11694{
11695 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011696 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011697 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011698
11699 set_bit(__I40E_SUSPENDED, &pf->state);
11700 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011701
Joshua Hay1d680052016-12-12 15:44:08 -080011702 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11703 i40e_enable_mc_magic_wake(pf);
11704
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011705 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011706 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011707 rtnl_unlock();
11708
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011709 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11710 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11711
Greg Roseb33d3b72016-05-16 10:26:37 -070011712 i40e_stop_misc_vector(pf);
11713
Greg Rose059ff692016-05-16 10:26:38 -070011714 retval = pci_save_state(pdev);
11715 if (retval)
11716 return retval;
11717
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011718 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011719 pci_set_power_state(pdev, PCI_D3hot);
11720
Greg Rose059ff692016-05-16 10:26:38 -070011721 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011722}
11723
11724/**
11725 * i40e_resume - PCI callback for waking up from D3
11726 * @pdev: PCI device information struct
11727 **/
11728static int i40e_resume(struct pci_dev *pdev)
11729{
11730 struct i40e_pf *pf = pci_get_drvdata(pdev);
11731 u32 err;
11732
11733 pci_set_power_state(pdev, PCI_D0);
11734 pci_restore_state(pdev);
11735 /* pci_restore_state() clears dev->state_saves, so
11736 * call pci_save_state() again to restore it.
11737 */
11738 pci_save_state(pdev);
11739
11740 err = pci_enable_device_mem(pdev);
11741 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011742 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011743 return err;
11744 }
11745 pci_set_master(pdev);
11746
11747 /* no wakeup events while running */
11748 pci_wake_from_d3(pdev, false);
11749
11750 /* handling the reset will rebuild the device state */
11751 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11752 clear_bit(__I40E_DOWN, &pf->state);
11753 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011754 i40e_reset_and_rebuild(pf, false, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011755 rtnl_unlock();
11756 }
11757
11758 return 0;
11759}
11760
11761#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011762static const struct pci_error_handlers i40e_err_handler = {
11763 .error_detected = i40e_pci_error_detected,
11764 .slot_reset = i40e_pci_error_slot_reset,
11765 .resume = i40e_pci_error_resume,
11766};
11767
11768static struct pci_driver i40e_driver = {
11769 .name = i40e_driver_name,
11770 .id_table = i40e_pci_tbl,
11771 .probe = i40e_probe,
11772 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011773#ifdef CONFIG_PM
11774 .suspend = i40e_suspend,
11775 .resume = i40e_resume,
11776#endif
11777 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011778 .err_handler = &i40e_err_handler,
11779 .sriov_configure = i40e_pci_sriov_configure,
11780};
11781
11782/**
11783 * i40e_init_module - Driver registration routine
11784 *
11785 * i40e_init_module is the first routine called when the driver is
11786 * loaded. All it does is register with the PCI subsystem.
11787 **/
11788static int __init i40e_init_module(void)
11789{
11790 pr_info("%s: %s - version %s\n", i40e_driver_name,
11791 i40e_driver_string, i40e_driver_version_str);
11792 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011793
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011794 /* we will see if single thread per module is enough for now,
11795 * it can't be any worse than using the system workqueue which
11796 * was already single threaded
11797 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011798 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11799 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011800 if (!i40e_wq) {
11801 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11802 return -ENOMEM;
11803 }
11804
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011805 i40e_dbg_init();
11806 return pci_register_driver(&i40e_driver);
11807}
11808module_init(i40e_init_module);
11809
11810/**
11811 * i40e_exit_module - Driver exit cleanup routine
11812 *
11813 * i40e_exit_module is called just before the driver is removed
11814 * from memory.
11815 **/
11816static void __exit i40e_exit_module(void)
11817{
11818 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011819 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011820 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011821}
11822module_exit(i40e_exit_module);