blob: 4534d41fa3407668b8726a1d0f159247041dcada [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
Catherine Sullivane8e724d2014-07-10 07:58:26 +000042#define DRV_VERSION_MAJOR 1
Bimmy Pujari07061952016-05-16 10:26:45 -070043#define DRV_VERSION_MINOR 6
Bimmy Pujaricf465fe2016-09-27 11:28:54 -070044#define DRV_VERSION_BUILD 21
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000045#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
46 __stringify(DRV_VERSION_MINOR) "." \
47 __stringify(DRV_VERSION_BUILD) DRV_KERN
48const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080049static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000050
51/* a bit of forward declarations */
52static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
53static void i40e_handle_reset_warning(struct i40e_pf *pf);
54static int i40e_add_vsi(struct i40e_vsi *vsi);
55static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000056static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000057static int i40e_setup_misc_vector(struct i40e_pf *pf);
58static void i40e_determine_queue_usage(struct i40e_pf *pf);
59static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080060static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080061static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000062
63/* i40e_pci_tbl - PCI Device ID Table
64 *
65 * Last entry must be all 0s
66 *
67 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
68 * Class, Class Mask, private data (not used) }
69 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020070static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080071 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080072 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080073 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
74 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080075 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
76 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
77 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000078 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040079 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Jesse Brandeburgae24b402015-03-27 00:12:09 -070080 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080081 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
82 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040083 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
85 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070086 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040087 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
88 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000089 /* required last entry */
90 {0, }
91};
92MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
93
94#define I40E_MAX_VF_COUNT 128
95static int debug = -1;
Alexander Duyck5d4ca232016-09-30 08:21:46 -040096module_param(debug, uint, 0);
97MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000098
99MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
100MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
101MODULE_LICENSE("GPL");
102MODULE_VERSION(DRV_VERSION);
103
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800104static struct workqueue_struct *i40e_wq;
105
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000106/**
107 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
108 * @hw: pointer to the HW structure
109 * @mem: ptr to mem struct to fill out
110 * @size: size of memory requested
111 * @alignment: what to align the allocation to
112 **/
113int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
114 u64 size, u32 alignment)
115{
116 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
117
118 mem->size = ALIGN(size, alignment);
119 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
120 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000121 if (!mem->va)
122 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000123
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000124 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000125}
126
127/**
128 * i40e_free_dma_mem_d - OS specific memory free for shared code
129 * @hw: pointer to the HW structure
130 * @mem: ptr to mem struct to free
131 **/
132int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
133{
134 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
135
136 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
137 mem->va = NULL;
138 mem->pa = 0;
139 mem->size = 0;
140
141 return 0;
142}
143
144/**
145 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
146 * @hw: pointer to the HW structure
147 * @mem: ptr to mem struct to fill out
148 * @size: size of memory requested
149 **/
150int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
151 u32 size)
152{
153 mem->size = size;
154 mem->va = kzalloc(size, GFP_KERNEL);
155
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000156 if (!mem->va)
157 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000158
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000159 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000160}
161
162/**
163 * i40e_free_virt_mem_d - OS specific memory free for shared code
164 * @hw: pointer to the HW structure
165 * @mem: ptr to mem struct to free
166 **/
167int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
168{
169 /* it's ok to kfree a NULL pointer */
170 kfree(mem->va);
171 mem->va = NULL;
172 mem->size = 0;
173
174 return 0;
175}
176
177/**
178 * i40e_get_lump - find a lump of free generic resource
179 * @pf: board private structure
180 * @pile: the pile of resource to search
181 * @needed: the number of items needed
182 * @id: an owner id to stick on the items assigned
183 *
184 * Returns the base item index of the lump, or negative for error
185 *
186 * The search_hint trick and lack of advanced fit-finding only work
187 * because we're highly likely to have all the same size lump requests.
188 * Linear search time and any fragmentation should be minimal.
189 **/
190static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
191 u16 needed, u16 id)
192{
193 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000194 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000195
196 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
197 dev_info(&pf->pdev->dev,
198 "param err: pile=%p needed=%d id=0x%04x\n",
199 pile, needed, id);
200 return -EINVAL;
201 }
202
203 /* start the linear search with an imperfect hint */
204 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000205 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000206 /* skip already allocated entries */
207 if (pile->list[i] & I40E_PILE_VALID_BIT) {
208 i++;
209 continue;
210 }
211
212 /* do we have enough in this lump? */
213 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
214 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
215 break;
216 }
217
218 if (j == needed) {
219 /* there was enough, so assign it to the requestor */
220 for (j = 0; j < needed; j++)
221 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
222 ret = i;
223 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000224 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000225 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400226
227 /* not enough, so skip over it and continue looking */
228 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000229 }
230
231 return ret;
232}
233
234/**
235 * i40e_put_lump - return a lump of generic resource
236 * @pile: the pile of resource to search
237 * @index: the base item index
238 * @id: the owner id of the items assigned
239 *
240 * Returns the count of items in the lump
241 **/
242static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
243{
244 int valid_id = (id | I40E_PILE_VALID_BIT);
245 int count = 0;
246 int i;
247
248 if (!pile || index >= pile->num_entries)
249 return -EINVAL;
250
251 for (i = index;
252 i < pile->num_entries && pile->list[i] == valid_id;
253 i++) {
254 pile->list[i] = 0;
255 count++;
256 }
257
258 if (count && index < pile->search_hint)
259 pile->search_hint = index;
260
261 return count;
262}
263
264/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700265 * i40e_find_vsi_from_id - searches for the vsi with the given id
266 * @pf - the pf structure to search for the vsi
267 * @id - id of the vsi it is searching for
268 **/
269struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
270{
271 int i;
272
273 for (i = 0; i < pf->num_alloc_vsi; i++)
274 if (pf->vsi[i] && (pf->vsi[i]->id == id))
275 return pf->vsi[i];
276
277 return NULL;
278}
279
280/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000281 * i40e_service_event_schedule - Schedule the service task to wake up
282 * @pf: board private structure
283 *
284 * If not already scheduled, this puts the task into the work queue
285 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600286void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000287{
288 if (!test_bit(__I40E_DOWN, &pf->state) &&
289 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
290 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800291 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000292}
293
294/**
295 * i40e_tx_timeout - Respond to a Tx Hang
296 * @netdev: network interface device structure
297 *
298 * If any port has noticed a Tx timeout, it is likely that the whole
299 * device is munged, not just the one netdev port, so go for the full
300 * reset.
301 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700302#ifdef I40E_FCOE
303void i40e_tx_timeout(struct net_device *netdev)
304#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000305static void i40e_tx_timeout(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -0700306#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000307{
308 struct i40e_netdev_priv *np = netdev_priv(netdev);
309 struct i40e_vsi *vsi = np->vsi;
310 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400311 struct i40e_ring *tx_ring = NULL;
312 unsigned int i, hung_queue = 0;
313 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000314
315 pf->tx_timeout_count++;
316
Kiran Patilb03a8c12015-09-24 18:13:15 -0400317 /* find the stopped queue the same way the stack does */
318 for (i = 0; i < netdev->num_tx_queues; i++) {
319 struct netdev_queue *q;
320 unsigned long trans_start;
321
322 q = netdev_get_tx_queue(netdev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200323 trans_start = q->trans_start;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400324 if (netif_xmit_stopped(q) &&
325 time_after(jiffies,
326 (trans_start + netdev->watchdog_timeo))) {
327 hung_queue = i;
328 break;
329 }
330 }
331
332 if (i == netdev->num_tx_queues) {
333 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
334 } else {
335 /* now that we have an index, find the tx_ring struct */
336 for (i = 0; i < vsi->num_queue_pairs; i++) {
337 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
338 if (hung_queue ==
339 vsi->tx_rings[i]->queue_index) {
340 tx_ring = vsi->tx_rings[i];
341 break;
342 }
343 }
344 }
345 }
346
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000347 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400348 pf->tx_timeout_recovery_level = 1; /* reset after some time */
349 else if (time_before(jiffies,
350 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
351 return; /* don't do any new action before the next timeout */
352
353 if (tx_ring) {
354 head = i40e_get_head(tx_ring);
355 /* Read interrupt register */
356 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
357 val = rd32(&pf->hw,
358 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
359 tx_ring->vsi->base_vector - 1));
360 else
361 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
362
363 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",
364 vsi->seid, hung_queue, tx_ring->next_to_clean,
365 head, tx_ring->next_to_use,
366 readl(tx_ring->tail), val);
367 }
368
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000369 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400370 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
371 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000372
373 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000374 case 1:
375 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
376 break;
377 case 2:
378 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
379 break;
380 case 3:
381 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
382 break;
383 default:
384 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000385 break;
386 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400387
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000388 i40e_service_event_schedule(pf);
389 pf->tx_timeout_recovery_level++;
390}
391
392/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000393 * i40e_get_vsi_stats_struct - Get System Network Statistics
394 * @vsi: the VSI we care about
395 *
396 * Returns the address of the device statistics structure.
397 * The statistics are actually updated from the service task.
398 **/
399struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
400{
401 return &vsi->net_stats;
402}
403
404/**
405 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
406 * @netdev: network interface device structure
407 *
408 * Returns the address of the device statistics structure.
409 * The statistics are actually updated from the service task.
410 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700411#ifdef I40E_FCOE
412struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
413 struct net_device *netdev,
414 struct rtnl_link_stats64 *stats)
415#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000416static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
417 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000418 struct rtnl_link_stats64 *stats)
Vasu Dev38e00432014-08-01 13:27:03 -0700419#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000420{
421 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000422 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000423 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000424 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
425 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000426
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000427 if (test_bit(__I40E_DOWN, &vsi->state))
428 return stats;
429
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800430 if (!vsi->tx_rings)
431 return stats;
432
Alexander Duyck980e9b12013-09-28 06:01:03 +0000433 rcu_read_lock();
434 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000435 u64 bytes, packets;
436 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000437
Alexander Duyck980e9b12013-09-28 06:01:03 +0000438 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
439 if (!tx_ring)
440 continue;
441
442 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700443 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000444 packets = tx_ring->stats.packets;
445 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700446 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000447
448 stats->tx_packets += packets;
449 stats->tx_bytes += bytes;
450 rx_ring = &tx_ring[1];
451
452 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700453 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000454 packets = rx_ring->stats.packets;
455 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700456 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000457
458 stats->rx_packets += packets;
459 stats->rx_bytes += bytes;
460 }
461 rcu_read_unlock();
462
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000463 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000464 stats->multicast = vsi_stats->multicast;
465 stats->tx_errors = vsi_stats->tx_errors;
466 stats->tx_dropped = vsi_stats->tx_dropped;
467 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400468 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000469 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
470 stats->rx_length_errors = vsi_stats->rx_length_errors;
471
472 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000473}
474
475/**
476 * i40e_vsi_reset_stats - Resets all stats of the given vsi
477 * @vsi: the VSI to have its stats reset
478 **/
479void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
480{
481 struct rtnl_link_stats64 *ns;
482 int i;
483
484 if (!vsi)
485 return;
486
487 ns = i40e_get_vsi_stats_struct(vsi);
488 memset(ns, 0, sizeof(*ns));
489 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
490 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
491 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000492 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000493 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400494 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000495 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400496 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000497 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400498 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000499 sizeof(vsi->tx_rings[i]->stats));
500 memset(&vsi->tx_rings[i]->tx_stats, 0,
501 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000502 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000503 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000504 vsi->stat_offsets_loaded = false;
505}
506
507/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000508 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000509 * @pf: the PF to be reset
510 **/
511void i40e_pf_reset_stats(struct i40e_pf *pf)
512{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000513 int i;
514
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000515 memset(&pf->stats, 0, sizeof(pf->stats));
516 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
517 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000518
519 for (i = 0; i < I40E_MAX_VEB; i++) {
520 if (pf->veb[i]) {
521 memset(&pf->veb[i]->stats, 0,
522 sizeof(pf->veb[i]->stats));
523 memset(&pf->veb[i]->stats_offsets, 0,
524 sizeof(pf->veb[i]->stats_offsets));
525 pf->veb[i]->stat_offsets_loaded = false;
526 }
527 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700528 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000529}
530
531/**
532 * i40e_stat_update48 - read and update a 48 bit stat from the chip
533 * @hw: ptr to the hardware info
534 * @hireg: the high 32 bit reg to read
535 * @loreg: the low 32 bit reg to read
536 * @offset_loaded: has the initial offset been loaded yet
537 * @offset: ptr to current offset value
538 * @stat: ptr to the stat
539 *
540 * Since the device stats are not reset at PFReset, they likely will not
541 * be zeroed when the driver starts. We'll save the first values read
542 * and use them as offsets to be subtracted from the raw values in order
543 * to report stats that count from zero. In the process, we also manage
544 * the potential roll-over.
545 **/
546static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
547 bool offset_loaded, u64 *offset, u64 *stat)
548{
549 u64 new_data;
550
Shannon Nelsonab600852014-01-17 15:36:39 -0800551 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000552 new_data = rd32(hw, loreg);
553 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
554 } else {
555 new_data = rd64(hw, loreg);
556 }
557 if (!offset_loaded)
558 *offset = new_data;
559 if (likely(new_data >= *offset))
560 *stat = new_data - *offset;
561 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400562 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000563 *stat &= 0xFFFFFFFFFFFFULL;
564}
565
566/**
567 * i40e_stat_update32 - read and update a 32 bit stat from the chip
568 * @hw: ptr to the hardware info
569 * @reg: the hw reg to read
570 * @offset_loaded: has the initial offset been loaded yet
571 * @offset: ptr to current offset value
572 * @stat: ptr to the stat
573 **/
574static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
575 bool offset_loaded, u64 *offset, u64 *stat)
576{
577 u32 new_data;
578
579 new_data = rd32(hw, reg);
580 if (!offset_loaded)
581 *offset = new_data;
582 if (likely(new_data >= *offset))
583 *stat = (u32)(new_data - *offset);
584 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400585 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000586}
587
588/**
589 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
590 * @vsi: the VSI to be updated
591 **/
592void i40e_update_eth_stats(struct i40e_vsi *vsi)
593{
594 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
595 struct i40e_pf *pf = vsi->back;
596 struct i40e_hw *hw = &pf->hw;
597 struct i40e_eth_stats *oes;
598 struct i40e_eth_stats *es; /* device's eth stats */
599
600 es = &vsi->eth_stats;
601 oes = &vsi->eth_stats_offsets;
602
603 /* Gather up the stats that the hw collects */
604 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
605 vsi->stat_offsets_loaded,
606 &oes->tx_errors, &es->tx_errors);
607 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
608 vsi->stat_offsets_loaded,
609 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000610 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
611 vsi->stat_offsets_loaded,
612 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
613 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
614 vsi->stat_offsets_loaded,
615 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000616
617 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
618 I40E_GLV_GORCL(stat_idx),
619 vsi->stat_offsets_loaded,
620 &oes->rx_bytes, &es->rx_bytes);
621 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
622 I40E_GLV_UPRCL(stat_idx),
623 vsi->stat_offsets_loaded,
624 &oes->rx_unicast, &es->rx_unicast);
625 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
626 I40E_GLV_MPRCL(stat_idx),
627 vsi->stat_offsets_loaded,
628 &oes->rx_multicast, &es->rx_multicast);
629 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
630 I40E_GLV_BPRCL(stat_idx),
631 vsi->stat_offsets_loaded,
632 &oes->rx_broadcast, &es->rx_broadcast);
633
634 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
635 I40E_GLV_GOTCL(stat_idx),
636 vsi->stat_offsets_loaded,
637 &oes->tx_bytes, &es->tx_bytes);
638 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
639 I40E_GLV_UPTCL(stat_idx),
640 vsi->stat_offsets_loaded,
641 &oes->tx_unicast, &es->tx_unicast);
642 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
643 I40E_GLV_MPTCL(stat_idx),
644 vsi->stat_offsets_loaded,
645 &oes->tx_multicast, &es->tx_multicast);
646 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
647 I40E_GLV_BPTCL(stat_idx),
648 vsi->stat_offsets_loaded,
649 &oes->tx_broadcast, &es->tx_broadcast);
650 vsi->stat_offsets_loaded = true;
651}
652
653/**
654 * i40e_update_veb_stats - Update Switch component statistics
655 * @veb: the VEB being updated
656 **/
657static void i40e_update_veb_stats(struct i40e_veb *veb)
658{
659 struct i40e_pf *pf = veb->pf;
660 struct i40e_hw *hw = &pf->hw;
661 struct i40e_eth_stats *oes;
662 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400663 struct i40e_veb_tc_stats *veb_oes;
664 struct i40e_veb_tc_stats *veb_es;
665 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000666
667 idx = veb->stats_idx;
668 es = &veb->stats;
669 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400670 veb_es = &veb->tc_stats;
671 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000672
673 /* Gather up the stats that the hw collects */
674 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
675 veb->stat_offsets_loaded,
676 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000677 if (hw->revision_id > 0)
678 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
679 veb->stat_offsets_loaded,
680 &oes->rx_unknown_protocol,
681 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000682 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
683 veb->stat_offsets_loaded,
684 &oes->rx_bytes, &es->rx_bytes);
685 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
686 veb->stat_offsets_loaded,
687 &oes->rx_unicast, &es->rx_unicast);
688 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
689 veb->stat_offsets_loaded,
690 &oes->rx_multicast, &es->rx_multicast);
691 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
692 veb->stat_offsets_loaded,
693 &oes->rx_broadcast, &es->rx_broadcast);
694
695 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
696 veb->stat_offsets_loaded,
697 &oes->tx_bytes, &es->tx_bytes);
698 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
699 veb->stat_offsets_loaded,
700 &oes->tx_unicast, &es->tx_unicast);
701 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
702 veb->stat_offsets_loaded,
703 &oes->tx_multicast, &es->tx_multicast);
704 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
705 veb->stat_offsets_loaded,
706 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400707 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
708 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
709 I40E_GLVEBTC_RPCL(i, idx),
710 veb->stat_offsets_loaded,
711 &veb_oes->tc_rx_packets[i],
712 &veb_es->tc_rx_packets[i]);
713 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
714 I40E_GLVEBTC_RBCL(i, idx),
715 veb->stat_offsets_loaded,
716 &veb_oes->tc_rx_bytes[i],
717 &veb_es->tc_rx_bytes[i]);
718 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
719 I40E_GLVEBTC_TPCL(i, idx),
720 veb->stat_offsets_loaded,
721 &veb_oes->tc_tx_packets[i],
722 &veb_es->tc_tx_packets[i]);
723 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
724 I40E_GLVEBTC_TBCL(i, idx),
725 veb->stat_offsets_loaded,
726 &veb_oes->tc_tx_bytes[i],
727 &veb_es->tc_tx_bytes[i]);
728 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000729 veb->stat_offsets_loaded = true;
730}
731
Vasu Dev38e00432014-08-01 13:27:03 -0700732#ifdef I40E_FCOE
733/**
734 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
735 * @vsi: the VSI that is capable of doing FCoE
736 **/
737static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
738{
739 struct i40e_pf *pf = vsi->back;
740 struct i40e_hw *hw = &pf->hw;
741 struct i40e_fcoe_stats *ofs;
742 struct i40e_fcoe_stats *fs; /* device's eth stats */
743 int idx;
744
745 if (vsi->type != I40E_VSI_FCOE)
746 return;
747
Kiran Patil4147e2c2016-01-15 14:33:14 -0800748 idx = hw->pf_id + I40E_FCOE_PF_STAT_OFFSET;
Vasu Dev38e00432014-08-01 13:27:03 -0700749 fs = &vsi->fcoe_stats;
750 ofs = &vsi->fcoe_stats_offsets;
751
752 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
753 vsi->fcoe_stat_offsets_loaded,
754 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
755 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
756 vsi->fcoe_stat_offsets_loaded,
757 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
758 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
759 vsi->fcoe_stat_offsets_loaded,
760 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
761 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
762 vsi->fcoe_stat_offsets_loaded,
763 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
764 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
765 vsi->fcoe_stat_offsets_loaded,
766 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
767 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
768 vsi->fcoe_stat_offsets_loaded,
769 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
770 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
771 vsi->fcoe_stat_offsets_loaded,
772 &ofs->fcoe_last_error, &fs->fcoe_last_error);
773 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
774 vsi->fcoe_stat_offsets_loaded,
775 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
776
777 vsi->fcoe_stat_offsets_loaded = true;
778}
779
780#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000781/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000782 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000783 * @vsi: the VSI to be updated
784 *
785 * There are a few instances where we store the same stat in a
786 * couple of different structs. This is partly because we have
787 * the netdev stats that need to be filled out, which is slightly
788 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000789 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000790 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000791static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000792{
793 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000794 struct rtnl_link_stats64 *ons;
795 struct rtnl_link_stats64 *ns; /* netdev stats */
796 struct i40e_eth_stats *oes;
797 struct i40e_eth_stats *es; /* device's eth stats */
798 u32 tx_restart, tx_busy;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800799 u64 tx_lost_interrupt;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000800 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000801 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000802 u64 bytes, packets;
803 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400804 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400805 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000806 u64 rx_p, rx_b;
807 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000808 u16 q;
809
810 if (test_bit(__I40E_DOWN, &vsi->state) ||
811 test_bit(__I40E_CONFIG_BUSY, &pf->state))
812 return;
813
814 ns = i40e_get_vsi_stats_struct(vsi);
815 ons = &vsi->net_stats_offsets;
816 es = &vsi->eth_stats;
817 oes = &vsi->eth_stats_offsets;
818
819 /* Gather up the netdev and vsi stats that the driver collects
820 * on the fly during packet processing
821 */
822 rx_b = rx_p = 0;
823 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400824 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800825 tx_lost_interrupt = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000826 rx_page = 0;
827 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000828 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000829 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000830 /* locate Tx ring */
831 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000832
Alexander Duyck980e9b12013-09-28 06:01:03 +0000833 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700834 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000835 packets = p->stats.packets;
836 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700837 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000838 tx_b += bytes;
839 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000840 tx_restart += p->tx_stats.restart_queue;
841 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400842 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400843 tx_force_wb += p->tx_stats.tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800844 tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000845
846 /* Rx queue is part of the same block as Tx queue */
847 p = &p[1];
848 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700849 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000850 packets = p->stats.packets;
851 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700852 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000853 rx_b += bytes;
854 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000855 rx_buf += p->rx_stats.alloc_buff_failed;
856 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000857 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000858 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000859 vsi->tx_restart = tx_restart;
860 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400861 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400862 vsi->tx_force_wb = tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800863 vsi->tx_lost_interrupt = tx_lost_interrupt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000864 vsi->rx_page_failed = rx_page;
865 vsi->rx_buf_failed = rx_buf;
866
867 ns->rx_packets = rx_p;
868 ns->rx_bytes = rx_b;
869 ns->tx_packets = tx_p;
870 ns->tx_bytes = tx_b;
871
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000872 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000873 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000874 ons->tx_errors = oes->tx_errors;
875 ns->tx_errors = es->tx_errors;
876 ons->multicast = oes->rx_multicast;
877 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000878 ons->rx_dropped = oes->rx_discards;
879 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000880 ons->tx_dropped = oes->tx_discards;
881 ns->tx_dropped = es->tx_discards;
882
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000883 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000884 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000885 ns->rx_crc_errors = pf->stats.crc_errors;
886 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
887 ns->rx_length_errors = pf->stats.rx_length_errors;
888 }
889}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000890
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000891/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000892 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000893 * @pf: the PF to be updated
894 **/
895static void i40e_update_pf_stats(struct i40e_pf *pf)
896{
897 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
898 struct i40e_hw_port_stats *nsd = &pf->stats;
899 struct i40e_hw *hw = &pf->hw;
900 u32 val;
901 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000902
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000903 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
904 I40E_GLPRT_GORCL(hw->port),
905 pf->stat_offsets_loaded,
906 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
907 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
908 I40E_GLPRT_GOTCL(hw->port),
909 pf->stat_offsets_loaded,
910 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
911 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
912 pf->stat_offsets_loaded,
913 &osd->eth.rx_discards,
914 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000915 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
916 I40E_GLPRT_UPRCL(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->eth.rx_unicast,
919 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000920 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
921 I40E_GLPRT_MPRCL(hw->port),
922 pf->stat_offsets_loaded,
923 &osd->eth.rx_multicast,
924 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000925 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
926 I40E_GLPRT_BPRCL(hw->port),
927 pf->stat_offsets_loaded,
928 &osd->eth.rx_broadcast,
929 &nsd->eth.rx_broadcast);
930 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
931 I40E_GLPRT_UPTCL(hw->port),
932 pf->stat_offsets_loaded,
933 &osd->eth.tx_unicast,
934 &nsd->eth.tx_unicast);
935 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
936 I40E_GLPRT_MPTCL(hw->port),
937 pf->stat_offsets_loaded,
938 &osd->eth.tx_multicast,
939 &nsd->eth.tx_multicast);
940 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
941 I40E_GLPRT_BPTCL(hw->port),
942 pf->stat_offsets_loaded,
943 &osd->eth.tx_broadcast,
944 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000945
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000946 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
947 pf->stat_offsets_loaded,
948 &osd->tx_dropped_link_down,
949 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000950
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000951 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
952 pf->stat_offsets_loaded,
953 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000954
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000955 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
956 pf->stat_offsets_loaded,
957 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000958
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000959 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
960 pf->stat_offsets_loaded,
961 &osd->mac_local_faults,
962 &nsd->mac_local_faults);
963 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
964 pf->stat_offsets_loaded,
965 &osd->mac_remote_faults,
966 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000967
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000968 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
969 pf->stat_offsets_loaded,
970 &osd->rx_length_errors,
971 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000972
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000973 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
974 pf->stat_offsets_loaded,
975 &osd->link_xon_rx, &nsd->link_xon_rx);
976 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
977 pf->stat_offsets_loaded,
978 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800979 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
980 pf->stat_offsets_loaded,
981 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000982 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
983 pf->stat_offsets_loaded,
984 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000985
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000986 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800987 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
988 pf->stat_offsets_loaded,
989 &osd->priority_xoff_rx[i],
990 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000991 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000992 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000993 &osd->priority_xon_rx[i],
994 &nsd->priority_xon_rx[i]);
995 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000996 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000997 &osd->priority_xon_tx[i],
998 &nsd->priority_xon_tx[i]);
999 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001000 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001001 &osd->priority_xoff_tx[i],
1002 &nsd->priority_xoff_tx[i]);
1003 i40e_stat_update32(hw,
1004 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001005 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001006 &osd->priority_xon_2_xoff[i],
1007 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001008 }
1009
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001010 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1011 I40E_GLPRT_PRC64L(hw->port),
1012 pf->stat_offsets_loaded,
1013 &osd->rx_size_64, &nsd->rx_size_64);
1014 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1015 I40E_GLPRT_PRC127L(hw->port),
1016 pf->stat_offsets_loaded,
1017 &osd->rx_size_127, &nsd->rx_size_127);
1018 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1019 I40E_GLPRT_PRC255L(hw->port),
1020 pf->stat_offsets_loaded,
1021 &osd->rx_size_255, &nsd->rx_size_255);
1022 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1023 I40E_GLPRT_PRC511L(hw->port),
1024 pf->stat_offsets_loaded,
1025 &osd->rx_size_511, &nsd->rx_size_511);
1026 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1027 I40E_GLPRT_PRC1023L(hw->port),
1028 pf->stat_offsets_loaded,
1029 &osd->rx_size_1023, &nsd->rx_size_1023);
1030 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1031 I40E_GLPRT_PRC1522L(hw->port),
1032 pf->stat_offsets_loaded,
1033 &osd->rx_size_1522, &nsd->rx_size_1522);
1034 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1035 I40E_GLPRT_PRC9522L(hw->port),
1036 pf->stat_offsets_loaded,
1037 &osd->rx_size_big, &nsd->rx_size_big);
1038
1039 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1040 I40E_GLPRT_PTC64L(hw->port),
1041 pf->stat_offsets_loaded,
1042 &osd->tx_size_64, &nsd->tx_size_64);
1043 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1044 I40E_GLPRT_PTC127L(hw->port),
1045 pf->stat_offsets_loaded,
1046 &osd->tx_size_127, &nsd->tx_size_127);
1047 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1048 I40E_GLPRT_PTC255L(hw->port),
1049 pf->stat_offsets_loaded,
1050 &osd->tx_size_255, &nsd->tx_size_255);
1051 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1052 I40E_GLPRT_PTC511L(hw->port),
1053 pf->stat_offsets_loaded,
1054 &osd->tx_size_511, &nsd->tx_size_511);
1055 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1056 I40E_GLPRT_PTC1023L(hw->port),
1057 pf->stat_offsets_loaded,
1058 &osd->tx_size_1023, &nsd->tx_size_1023);
1059 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1060 I40E_GLPRT_PTC1522L(hw->port),
1061 pf->stat_offsets_loaded,
1062 &osd->tx_size_1522, &nsd->tx_size_1522);
1063 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1064 I40E_GLPRT_PTC9522L(hw->port),
1065 pf->stat_offsets_loaded,
1066 &osd->tx_size_big, &nsd->tx_size_big);
1067
1068 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1069 pf->stat_offsets_loaded,
1070 &osd->rx_undersize, &nsd->rx_undersize);
1071 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1072 pf->stat_offsets_loaded,
1073 &osd->rx_fragments, &nsd->rx_fragments);
1074 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1075 pf->stat_offsets_loaded,
1076 &osd->rx_oversize, &nsd->rx_oversize);
1077 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1078 pf->stat_offsets_loaded,
1079 &osd->rx_jabber, &nsd->rx_jabber);
1080
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001081 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001082 i40e_stat_update32(hw,
1083 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001084 pf->stat_offsets_loaded,
1085 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001086 i40e_stat_update32(hw,
1087 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001088 pf->stat_offsets_loaded,
1089 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001090 i40e_stat_update32(hw,
1091 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1092 pf->stat_offsets_loaded,
1093 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001094
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001095 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1096 nsd->tx_lpi_status =
1097 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1098 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1099 nsd->rx_lpi_status =
1100 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1101 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1102 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1103 pf->stat_offsets_loaded,
1104 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1105 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1106 pf->stat_offsets_loaded,
1107 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1108
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001109 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1110 !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
1111 nsd->fd_sb_status = true;
1112 else
1113 nsd->fd_sb_status = false;
1114
1115 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1116 !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1117 nsd->fd_atr_status = true;
1118 else
1119 nsd->fd_atr_status = false;
1120
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001121 pf->stat_offsets_loaded = true;
1122}
1123
1124/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001125 * i40e_update_stats - Update the various statistics counters.
1126 * @vsi: the VSI to be updated
1127 *
1128 * Update the various stats for this VSI and its related entities.
1129 **/
1130void i40e_update_stats(struct i40e_vsi *vsi)
1131{
1132 struct i40e_pf *pf = vsi->back;
1133
1134 if (vsi == pf->vsi[pf->lan_vsi])
1135 i40e_update_pf_stats(pf);
1136
1137 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001138#ifdef I40E_FCOE
1139 i40e_update_fcoe_stats(vsi);
1140#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001141}
1142
1143/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001144 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1145 * @vsi: the VSI to be searched
1146 * @macaddr: the MAC address
1147 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001148 *
1149 * Returns ptr to the filter object or NULL
1150 **/
1151static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001152 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001153{
1154 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001155 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001156
1157 if (!vsi || !macaddr)
1158 return NULL;
1159
Jacob Keller278e7d02016-10-05 09:30:37 -07001160 key = i40e_addr_to_hkey(macaddr);
1161 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001162 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001163 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001164 return f;
1165 }
1166 return NULL;
1167}
1168
1169/**
1170 * i40e_find_mac - Find a mac addr in the macvlan filters list
1171 * @vsi: the VSI to be searched
1172 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001173 *
1174 * Returns the first filter with the provided MAC address or NULL if
1175 * MAC address was not found
1176 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001177struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001178{
1179 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001180 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001181
1182 if (!vsi || !macaddr)
1183 return NULL;
1184
Jacob Keller278e7d02016-10-05 09:30:37 -07001185 key = i40e_addr_to_hkey(macaddr);
1186 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001187 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001188 return f;
1189 }
1190 return NULL;
1191}
1192
1193/**
1194 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1195 * @vsi: the VSI to be searched
1196 *
1197 * Returns true if VSI is in vlan mode or false otherwise
1198 **/
1199bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1200{
Jacob Kellercbebb852016-10-05 09:30:40 -07001201 /* If we have a PVID, always operate in VLAN mode */
1202 if (vsi->info.pvid)
1203 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001204
Jacob Kellercbebb852016-10-05 09:30:40 -07001205 /* We need to operate in VLAN mode whenever we have any filters with
1206 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1207 * time, incurring search cost repeatedly. However, we can notice two
1208 * things:
1209 *
1210 * 1) the only place where we can gain a VLAN filter is in
1211 * i40e_add_filter.
1212 *
1213 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001214 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001215 *
1216 * Thus, we can simply use a boolean value, has_vlan_filters which we
1217 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1218 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001219 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001220 * filters here and can perform the check at the same time. This
1221 * results in avoiding embedding a loop for VLAN mode inside another
1222 * loop over all the filters, and should maintain correctness as noted
1223 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001224 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001225 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001226}
1227
1228/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001229 * i40e_add_filter - Add a mac/vlan filter to the VSI
1230 * @vsi: the VSI to be searched
1231 * @macaddr: the MAC address
1232 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001233 *
1234 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001235 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001236 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001237 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001238 **/
1239struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001240 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001241{
1242 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001243 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001244
1245 if (!vsi || !macaddr)
1246 return NULL;
1247
Jacob Keller1bc87e82016-10-05 09:30:31 -07001248 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001249 if (!f) {
1250 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1251 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001252 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001253
Jacob Kellercbebb852016-10-05 09:30:40 -07001254 /* Update the boolean indicating if we need to function in
1255 * VLAN mode.
1256 */
1257 if (vlan >= 0)
1258 vsi->has_vlan_filter = true;
1259
Greg Rose9a173902014-05-22 06:32:02 +00001260 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001261 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001262 /* If we're in overflow promisc mode, set the state directly
1263 * to failed, so we don't bother to try sending the filter
1264 * to the hardware.
1265 */
1266 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1267 f->state = I40E_FILTER_FAILED;
1268 else
1269 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001270 INIT_HLIST_NODE(&f->hlist);
1271
1272 key = i40e_addr_to_hkey(macaddr);
1273 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001274
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001275 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1276 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1277 }
1278
Jacob Keller1bc87e82016-10-05 09:30:31 -07001279 /* If we're asked to add a filter that has been marked for removal, it
1280 * is safe to simply restore it to active state. __i40e_del_filter
1281 * will have simply deleted any filters which were previously marked
1282 * NEW or FAILED, so if it is currently marked REMOVE it must have
1283 * previously been ACTIVE. Since we haven't yet run the sync filters
1284 * task, just restore this filter to the ACTIVE state so that the
1285 * sync task leaves it in place
1286 */
1287 if (f->state == I40E_FILTER_REMOVE)
1288 f->state = I40E_FILTER_ACTIVE;
1289
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001290 return f;
1291}
1292
1293/**
Jacob Keller290d2552016-10-05 09:30:36 -07001294 * __i40e_del_filter - Remove a specific filter from the VSI
1295 * @vsi: VSI to remove from
1296 * @f: the filter to remove from the list
1297 *
1298 * This function should be called instead of i40e_del_filter only if you know
1299 * the exact filter you will remove already, such as via i40e_find_filter or
1300 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001301 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001302 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001303 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001304 * ANOTHER NOTE: This function MUST be called from within the context of
1305 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1306 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001307 **/
Jacob Keller290d2552016-10-05 09:30:36 -07001308static void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001309{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001310 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001311 return;
1312
Jacob Keller1bc87e82016-10-05 09:30:31 -07001313 if ((f->state == I40E_FILTER_FAILED) ||
1314 (f->state == I40E_FILTER_NEW)) {
1315 /* this one never got added by the FW. Just remove it,
1316 * no need to sync anything.
1317 */
Jacob Keller278e7d02016-10-05 09:30:37 -07001318 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001319 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001320 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001321 f->state = I40E_FILTER_REMOVE;
1322 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1323 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001324 }
1325}
1326
1327/**
Jacob Keller290d2552016-10-05 09:30:36 -07001328 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1329 * @vsi: the VSI to be searched
1330 * @macaddr: the MAC address
1331 * @vlan: the VLAN
1332 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001333 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001334 * being held.
1335 * ANOTHER NOTE: This function MUST be called from within the context of
1336 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1337 * instead of list_for_each_entry().
1338 **/
1339void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1340{
1341 struct i40e_mac_filter *f;
1342
1343 if (!vsi || !macaddr)
1344 return;
1345
1346 f = i40e_find_filter(vsi, macaddr, vlan);
1347 __i40e_del_filter(vsi, f);
1348}
1349
1350/**
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001351 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1352 * @vsi: the VSI to be searched
1353 * @macaddr: the mac address to be filtered
1354 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001355 * Goes through all the macvlan filters and adds a macvlan filter for each
1356 * unique vlan that already exists. If a PVID has been assigned, instead only
1357 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001358 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001359 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001360 **/
1361struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi,
1362 const u8 *macaddr)
1363{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001364 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001365 struct hlist_node *h;
1366 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001367
1368 if (vsi->info.pvid)
1369 return i40e_add_filter(vsi, macaddr,
1370 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001371
Jacob Keller278e7d02016-10-05 09:30:37 -07001372 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001373 if (f->state == I40E_FILTER_REMOVE)
1374 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001375 add = i40e_add_filter(vsi, macaddr, f->vlan);
1376 if (!add)
1377 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001378 }
1379
Jacob Keller5feb3d72016-10-05 09:30:34 -07001380 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001381}
1382
1383/**
1384 * i40e_del_mac_all_vlan - Remove a MAC filter from all VLANS
1385 * @vsi: the VSI to be searched
1386 * @macaddr: the mac address to be removed
1387 *
1388 * Removes a given MAC address from a VSI, regardless of VLAN
1389 *
1390 * Returns 0 for success, or error
1391 **/
1392int i40e_del_mac_all_vlan(struct i40e_vsi *vsi, const u8 *macaddr)
1393{
Jacob Keller278e7d02016-10-05 09:30:37 -07001394 struct i40e_mac_filter *f;
1395 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001396 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001397 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001398
Jacob Keller278e7d02016-10-05 09:30:37 -07001399 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1400 "Missing mac_filter_hash_lock\n");
1401 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001402 if (ether_addr_equal(macaddr, f->macaddr)) {
1403 __i40e_del_filter(vsi, f);
1404 found = true;
1405 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001406 }
Jacob Keller290d2552016-10-05 09:30:36 -07001407
1408 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001409 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001410 else
1411 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001412}
1413
1414/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001415 * i40e_set_mac - NDO callback to set mac address
1416 * @netdev: network interface device structure
1417 * @p: pointer to an address structure
1418 *
1419 * Returns 0 on success, negative on failure
1420 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001421#ifdef I40E_FCOE
1422int i40e_set_mac(struct net_device *netdev, void *p)
1423#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001424static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001425#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001426{
1427 struct i40e_netdev_priv *np = netdev_priv(netdev);
1428 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001429 struct i40e_pf *pf = vsi->back;
1430 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001431 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001432
1433 if (!is_valid_ether_addr(addr->sa_data))
1434 return -EADDRNOTAVAIL;
1435
Shannon Nelson30650cc2014-07-29 04:01:50 +00001436 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1437 netdev_info(netdev, "already using mac address %pM\n",
1438 addr->sa_data);
1439 return 0;
1440 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001441
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001442 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1443 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1444 return -EADDRNOTAVAIL;
1445
Shannon Nelson30650cc2014-07-29 04:01:50 +00001446 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1447 netdev_info(netdev, "returning to hw mac address %pM\n",
1448 hw->mac.addr);
1449 else
1450 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1451
Jacob Keller278e7d02016-10-05 09:30:37 -07001452 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001453 i40e_del_mac_all_vlan(vsi, netdev->dev_addr);
1454 i40e_put_mac_in_vlan(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001455 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001456 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001457 if (vsi->type == I40E_VSI_MAIN) {
1458 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001459
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001460 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001461 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001462 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001463 if (ret)
1464 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1465 i40e_stat_str(hw, ret),
1466 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001467 }
1468
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001469 /* schedule our worker thread which will take care of
1470 * applying the new filter changes
1471 */
1472 i40e_service_event_schedule(vsi->back);
1473 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001474}
1475
1476/**
1477 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1478 * @vsi: the VSI being setup
1479 * @ctxt: VSI context structure
1480 * @enabled_tc: Enabled TCs bitmap
1481 * @is_add: True if called before Add VSI
1482 *
1483 * Setup VSI queue mapping for enabled traffic classes.
1484 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001485#ifdef I40E_FCOE
1486void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1487 struct i40e_vsi_context *ctxt,
1488 u8 enabled_tc,
1489 bool is_add)
1490#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001491static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1492 struct i40e_vsi_context *ctxt,
1493 u8 enabled_tc,
1494 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001495#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001496{
1497 struct i40e_pf *pf = vsi->back;
1498 u16 sections = 0;
1499 u8 netdev_tc = 0;
1500 u16 numtc = 0;
1501 u16 qcount;
1502 u8 offset;
1503 u16 qmap;
1504 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001505 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001506
1507 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1508 offset = 0;
1509
1510 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1511 /* Find numtc from enabled TC bitmap */
1512 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001513 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001514 numtc++;
1515 }
1516 if (!numtc) {
1517 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1518 numtc = 1;
1519 }
1520 } else {
1521 /* At least TC0 is enabled in case of non-DCB case */
1522 numtc = 1;
1523 }
1524
1525 vsi->tc_config.numtc = numtc;
1526 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001527 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001528 qcount = vsi->alloc_queue_pairs;
1529
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001530 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001531 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001532
1533 /* Setup queue offset/count for all TCs for given VSI */
1534 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1535 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001536 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001537 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001538 int pow, num_qps;
1539
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001540 switch (vsi->type) {
1541 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001542 qcount = min_t(int, pf->alloc_rss_size,
1543 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001544 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001545#ifdef I40E_FCOE
1546 case I40E_VSI_FCOE:
1547 qcount = num_tc_qps;
1548 break;
1549#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001550 case I40E_VSI_FDIR:
1551 case I40E_VSI_SRIOV:
1552 case I40E_VSI_VMDQ2:
1553 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001554 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001555 WARN_ON(i != 0);
1556 break;
1557 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001558 vsi->tc_config.tc_info[i].qoffset = offset;
1559 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001560
Shannon Nelson1e200e42015-02-27 09:15:24 +00001561 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001562 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001563 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001564 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001565 pow++;
1566 num_qps >>= 1;
1567 }
1568
1569 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1570 qmap =
1571 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1572 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1573
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001574 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001575 } else {
1576 /* TC is not enabled so set the offset to
1577 * default queue and allocate one queue
1578 * for the given TC.
1579 */
1580 vsi->tc_config.tc_info[i].qoffset = 0;
1581 vsi->tc_config.tc_info[i].qcount = 1;
1582 vsi->tc_config.tc_info[i].netdev_tc = 0;
1583
1584 qmap = 0;
1585 }
1586 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1587 }
1588
1589 /* Set actual Tx/Rx queue pairs */
1590 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001591 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1592 if (vsi->req_queue_pairs > 0)
1593 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001594 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001595 vsi->num_queue_pairs = pf->num_lan_msix;
1596 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001597
1598 /* Scheduler section valid can only be set for ADD VSI */
1599 if (is_add) {
1600 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1601
1602 ctxt->info.up_enable_bits = enabled_tc;
1603 }
1604 if (vsi->type == I40E_VSI_SRIOV) {
1605 ctxt->info.mapping_flags |=
1606 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1607 for (i = 0; i < vsi->num_queue_pairs; i++)
1608 ctxt->info.queue_mapping[i] =
1609 cpu_to_le16(vsi->base_queue + i);
1610 } else {
1611 ctxt->info.mapping_flags |=
1612 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1613 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1614 }
1615 ctxt->info.valid_sections |= cpu_to_le16(sections);
1616}
1617
1618/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001619 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1620 * @netdev: the netdevice
1621 * @addr: address to add
1622 *
1623 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1624 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1625 */
1626static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1627{
1628 struct i40e_netdev_priv *np = netdev_priv(netdev);
1629 struct i40e_vsi *vsi = np->vsi;
1630 struct i40e_mac_filter *f;
1631
1632 if (i40e_is_vsi_in_vlan(vsi))
1633 f = i40e_put_mac_in_vlan(vsi, addr);
1634 else
1635 f = i40e_add_filter(vsi, addr, I40E_VLAN_ANY);
1636
1637 if (f)
1638 return 0;
1639 else
1640 return -ENOMEM;
1641}
1642
1643/**
1644 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1645 * @netdev: the netdevice
1646 * @addr: address to add
1647 *
1648 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1649 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1650 */
1651static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1652{
1653 struct i40e_netdev_priv *np = netdev_priv(netdev);
1654 struct i40e_vsi *vsi = np->vsi;
1655
1656 if (i40e_is_vsi_in_vlan(vsi))
1657 i40e_del_mac_all_vlan(vsi, addr);
1658 else
1659 i40e_del_filter(vsi, addr, I40E_VLAN_ANY);
1660
1661 return 0;
1662}
1663
1664/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001665 * i40e_set_rx_mode - NDO callback to set the netdev filters
1666 * @netdev: network interface device structure
1667 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001668#ifdef I40E_FCOE
1669void i40e_set_rx_mode(struct net_device *netdev)
1670#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001671static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001672#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001673{
1674 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001675 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001676
Jacob Keller278e7d02016-10-05 09:30:37 -07001677 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001678
Jacob Keller6622f5c2016-10-05 09:30:32 -07001679 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1680 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001681
Jacob Keller278e7d02016-10-05 09:30:37 -07001682 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001683
1684 /* check for other flag changes */
1685 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1686 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1687 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1688 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001689
1690 /* schedule our worker thread which will take care of
1691 * applying the new filter changes
1692 */
1693 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001694}
1695
1696/**
Jacob Keller4a2ce272016-10-05 09:30:38 -07001697 * i40e_undo_filter_entries - Undo the changes made to MAC filter entries
1698 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001699 * @from: Pointer to list which contains MAC filter entries - changes to
1700 * those entries needs to be undone.
1701 *
Jacob Keller4a2ce272016-10-05 09:30:38 -07001702 * MAC filter entries from list were slated to be sent to firmware, either for
1703 * addition or deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001704 **/
Jacob Keller4a2ce272016-10-05 09:30:38 -07001705static void i40e_undo_filter_entries(struct i40e_vsi *vsi,
1706 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001707{
Jacob Keller278e7d02016-10-05 09:30:37 -07001708 struct i40e_mac_filter *f;
1709 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001710
Jacob Keller278e7d02016-10-05 09:30:37 -07001711 hlist_for_each_entry_safe(f, h, from, hlist) {
1712 u64 key = i40e_addr_to_hkey(f->macaddr);
1713
Kiran Patil21659032015-09-30 14:09:03 -04001714 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001715 hlist_del(&f->hlist);
1716 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001717 }
1718}
1719
1720/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001721 * i40e_update_filter_state - Update filter state based on return data
1722 * from firmware
1723 * @count: Number of filters added
1724 * @add_list: return data from fw
1725 * @head: pointer to first filter in current batch
1726 * @aq_err: status from fw
Kiran Patil21659032015-09-30 14:09:03 -04001727 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001728 * MAC filter entries from list were slated to be added to device. Returns
1729 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001730 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001731static int
1732i40e_update_filter_state(int count,
1733 struct i40e_aqc_add_macvlan_element_data *add_list,
1734 struct i40e_mac_filter *add_head, int aq_err)
Kiran Patil21659032015-09-30 14:09:03 -04001735{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001736 int retval = 0;
1737 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001738
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001739
1740 if (!aq_err) {
1741 retval = count;
1742 /* Everything's good, mark all filters active. */
1743 for (i = 0; i < count ; i++) {
1744 add_head->state = I40E_FILTER_ACTIVE;
Jacob Keller278e7d02016-10-05 09:30:37 -07001745 add_head = hlist_entry(add_head->hlist.next,
1746 typeof(struct i40e_mac_filter),
1747 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001748 }
1749 } else if (aq_err == I40E_AQ_RC_ENOSPC) {
1750 /* Device ran out of filter space. Check the return value
1751 * for each filter to see which ones are active.
1752 */
1753 for (i = 0; i < count ; i++) {
1754 if (add_list[i].match_method ==
1755 I40E_AQC_MM_ERR_NO_RES) {
1756 add_head->state = I40E_FILTER_FAILED;
1757 } else {
1758 add_head->state = I40E_FILTER_ACTIVE;
1759 retval++;
1760 }
Jacob Keller278e7d02016-10-05 09:30:37 -07001761 add_head = hlist_entry(add_head->hlist.next,
1762 typeof(struct i40e_mac_filter),
1763 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001764 }
1765 } else {
1766 /* Some other horrible thing happened, fail all filters */
1767 retval = 0;
1768 for (i = 0; i < count ; i++) {
1769 add_head->state = I40E_FILTER_FAILED;
Jacob Keller278e7d02016-10-05 09:30:37 -07001770 add_head = hlist_entry(add_head->hlist.next,
1771 typeof(struct i40e_mac_filter),
1772 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001773 }
Kiran Patil21659032015-09-30 14:09:03 -04001774 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001775 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001776}
1777
1778/**
Jacob Keller00936312016-10-05 09:30:41 -07001779 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1780 * @vsi: ptr to the VSI
1781 * @vsi_name: name to display in messages
1782 * @list: the list of filters to send to firmware
1783 * @num_del: the number of filters to delete
1784 * @retval: Set to -EIO on failure to delete
1785 *
1786 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1787 * *retval instead of a return value so that success does not force ret_val to
1788 * be set to 0. This ensures that a sequence of calls to this function
1789 * preserve the previous value of *retval on successful delete.
1790 */
1791static
1792void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1793 struct i40e_aqc_remove_macvlan_element_data *list,
1794 int num_del, int *retval)
1795{
1796 struct i40e_hw *hw = &vsi->back->hw;
1797 i40e_status aq_ret;
1798 int aq_err;
1799
1800 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1801 aq_err = hw->aq.asq_last_status;
1802
1803 /* Explicitly ignore and do not report when firmware returns ENOENT */
1804 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1805 *retval = -EIO;
1806 dev_info(&vsi->back->pdev->dev,
1807 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1808 vsi_name, i40e_stat_str(hw, aq_ret),
1809 i40e_aq_str(hw, aq_err));
1810 }
1811}
1812
1813/**
1814 * i40e_aqc_add_filters - Request firmware to add a set of filters
1815 * @vsi: ptr to the VSI
1816 * @vsi_name: name to display in messages
1817 * @list: the list of filters to send to firmware
1818 * @add_head: Position in the add hlist
1819 * @num_add: the number of filters to add
1820 * @promisc_change: set to true on exit if promiscuous mode was forced on
1821 *
1822 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1823 * promisc_changed to true if the firmware has run out of space for more
1824 * filters.
1825 */
1826static
1827void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1828 struct i40e_aqc_add_macvlan_element_data *list,
1829 struct i40e_mac_filter *add_head,
1830 int num_add, bool *promisc_changed)
1831{
1832 struct i40e_hw *hw = &vsi->back->hw;
1833 i40e_status aq_ret;
1834 int aq_err, fcnt;
1835
1836 aq_ret = i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
1837 aq_err = hw->aq.asq_last_status;
1838 fcnt = i40e_update_filter_state(num_add, list, add_head, aq_ret);
1839 vsi->active_filters += fcnt;
1840
1841 if (fcnt != num_add) {
1842 *promisc_changed = true;
1843 set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
1844 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
1845 dev_warn(&vsi->back->pdev->dev,
1846 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1847 i40e_aq_str(hw, aq_err),
1848 vsi_name);
1849 }
1850}
1851
1852/**
Jacob Keller435c0842016-11-08 13:05:10 -08001853 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
1854 * @vsi: pointer to the VSI
1855 * @f: filter data
1856 *
1857 * This function sets or clears the promiscuous broadcast flags for VLAN
1858 * filters in order to properly receive broadcast frames. Assumes that only
1859 * broadcast filters are passed.
1860 **/
1861static
1862void i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
1863 struct i40e_mac_filter *f)
1864{
1865 bool enable = f->state == I40E_FILTER_NEW;
1866 struct i40e_hw *hw = &vsi->back->hw;
1867 i40e_status aq_ret;
1868
1869 if (f->vlan == I40E_VLAN_ANY) {
1870 aq_ret = i40e_aq_set_vsi_broadcast(hw,
1871 vsi->seid,
1872 enable,
1873 NULL);
1874 } else {
1875 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
1876 vsi->seid,
1877 enable,
1878 f->vlan,
1879 NULL);
1880 }
1881
1882 if (aq_ret) {
1883 dev_warn(&vsi->back->pdev->dev,
1884 "Error %s setting broadcast promiscuous mode on %s\n",
1885 i40e_aq_str(hw, hw->aq.asq_last_status),
1886 vsi_name);
1887 f->state = I40E_FILTER_FAILED;
1888 } else if (enable) {
1889 f->state = I40E_FILTER_ACTIVE;
1890 }
1891}
1892
1893/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001894 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1895 * @vsi: ptr to the VSI
1896 *
1897 * Push any outstanding VSI filter changes through the AdminQ.
1898 *
1899 * Returns 0 or error value
1900 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001901int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001902{
Jacob Keller278e7d02016-10-05 09:30:37 -07001903 struct hlist_head tmp_add_list, tmp_del_list;
Alan Brady84f5ca62016-10-05 09:30:39 -07001904 struct i40e_mac_filter *f, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001905 struct i40e_hw *hw = &vsi->back->hw;
Alan Brady84f5ca62016-10-05 09:30:39 -07001906 unsigned int vlan_any_filters = 0;
1907 unsigned int non_vlan_filters = 0;
1908 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001909 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07001910 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001911 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001912 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001913 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07001914 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001915 struct i40e_pf *pf;
1916 int num_add = 0;
1917 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001918 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001919 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001920 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07001921 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001922
1923 /* empty array typed pointers, kcalloc later */
1924 struct i40e_aqc_add_macvlan_element_data *add_list;
1925 struct i40e_aqc_remove_macvlan_element_data *del_list;
1926
1927 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1928 usleep_range(1000, 2000);
1929 pf = vsi->back;
1930
1931 if (vsi->netdev) {
1932 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1933 vsi->current_netdev_flags = vsi->netdev->flags;
1934 }
1935
Jacob Keller278e7d02016-10-05 09:30:37 -07001936 INIT_HLIST_HEAD(&tmp_add_list);
1937 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04001938
Shannon Nelson2d1de822016-05-16 10:26:44 -07001939 if (vsi->type == I40E_VSI_SRIOV)
1940 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
1941 else if (vsi->type != I40E_VSI_MAIN)
1942 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
1943
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001944 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1945 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1946
Jacob Keller278e7d02016-10-05 09:30:37 -07001947 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001948 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07001949 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001950 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001951 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07001952 hash_del(&f->hlist);
1953 hlist_add_head(&f->hlist, &tmp_del_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001954 vsi->active_filters--;
Alan Brady84f5ca62016-10-05 09:30:39 -07001955
1956 /* Avoid counting removed filters */
1957 continue;
Kiran Patil21659032015-09-30 14:09:03 -04001958 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001959 if (f->state == I40E_FILTER_NEW) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001960 hash_del(&f->hlist);
1961 hlist_add_head(&f->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001962 }
Alan Brady84f5ca62016-10-05 09:30:39 -07001963
1964 /* Count the number of each type of filter we have
1965 * remaining, ignoring any filters we're about to
1966 * delete.
1967 */
1968 if (f->vlan > 0)
1969 vlan_filters++;
1970 else if (!f->vlan)
1971 non_vlan_filters++;
1972 else
1973 vlan_any_filters++;
1974 }
1975
1976 /* We should never have VLAN=-1 filters at the same time as we
1977 * have either VLAN=0 or VLAN>0 filters, so warn about this
1978 * case here to help catch any issues.
1979 */
1980 WARN_ON(vlan_any_filters && (vlan_filters + non_vlan_filters));
1981
1982 /* If we only have VLAN=0 filters remaining, and don't have
1983 * any other VLAN filters, we need to convert these VLAN=0
1984 * filters into VLAN=-1 (I40E_VLAN_ANY) so that we operate
1985 * correctly in non-VLAN mode and receive all traffic tagged
1986 * or untagged.
1987 */
1988 if (non_vlan_filters && !vlan_filters) {
1989 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
1990 hlist) {
1991 /* Only replace VLAN=0 filters */
1992 if (f->vlan)
1993 continue;
1994
1995 /* Allocate a replacement element */
1996 add_head = kzalloc(sizeof(*add_head),
1997 GFP_KERNEL);
1998 if (!add_head)
1999 goto err_no_memory_locked;
2000
2001 /* Copy the filter, with new state and VLAN */
2002 *add_head = *f;
2003 add_head->state = I40E_FILTER_NEW;
2004 add_head->vlan = I40E_VLAN_ANY;
2005
2006 /* Move the replacement to the add list */
2007 INIT_HLIST_NODE(&add_head->hlist);
2008 hlist_add_head(&add_head->hlist,
2009 &tmp_add_list);
2010
2011 /* Move the original to the delete list */
2012 f->state = I40E_FILTER_REMOVE;
2013 hash_del(&f->hlist);
2014 hlist_add_head(&f->hlist, &tmp_del_list);
2015 vsi->active_filters--;
2016 }
2017
2018 /* Also update any filters on the tmp_add list */
2019 hlist_for_each_entry(f, &tmp_add_list, hlist) {
2020 if (!f->vlan)
2021 f->vlan = I40E_VLAN_ANY;
2022 }
2023 add_head = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002024 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002025 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002026 }
2027
2028 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002029 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002030 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002031 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002032 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002033 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002034 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002035 if (!del_list)
2036 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002037
Jacob Keller278e7d02016-10-05 09:30:37 -07002038 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002039 cmd_flags = 0;
2040
Jacob Keller435c0842016-11-08 13:05:10 -08002041 /* handle broadcast filters by updating the broadcast
2042 * promiscuous flag instead of deleting a MAC filter.
2043 */
2044 if (is_broadcast_ether_addr(f->macaddr)) {
2045 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2046
2047 hlist_del(&f->hlist);
2048 kfree(f);
2049 continue;
2050 }
2051
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002052 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002053 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002054 if (f->vlan == I40E_VLAN_ANY) {
2055 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002056 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002057 } else {
2058 del_list[num_del].vlan_tag =
2059 cpu_to_le16((u16)(f->vlan));
2060 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002061
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002062 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2063 del_list[num_del].flags = cmd_flags;
2064 num_del++;
2065
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002066 /* flush a full buffer */
2067 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002068 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2069 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002070 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002071 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002072 }
Kiran Patil21659032015-09-30 14:09:03 -04002073 /* Release memory for MAC filter entries which were
2074 * synced up with HW.
2075 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002076 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002077 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002078 }
Kiran Patil21659032015-09-30 14:09:03 -04002079
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002080 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002081 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2082 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002083 }
2084
2085 kfree(del_list);
2086 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002087 }
2088
Jacob Kellercbebb852016-10-05 09:30:40 -07002089 /* After finishing notifying firmware of the deleted filters, update
2090 * the cached value of vsi->has_vlan_filter. Note that we are safe to
2091 * use just !!vlan_filters here because if we only have VLAN=0 (that
2092 * is, non_vlan_filters) these will all be converted to VLAN=-1 in the
2093 * logic above already so this value would still be correct.
2094 */
2095 vsi->has_vlan_filter = !!vlan_filters;
2096
Jacob Keller278e7d02016-10-05 09:30:37 -07002097 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002098 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002099 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002100 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002101 list_size = filter_list_len *
2102 sizeof(struct i40e_aqc_add_macvlan_element_data);
2103 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002104 if (!add_list)
2105 goto err_no_memory;
2106
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002107 num_add = 0;
Jacob Keller435c0842016-11-08 13:05:10 -08002108 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002109 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2110 &vsi->state)) {
2111 f->state = I40E_FILTER_FAILED;
2112 continue;
2113 }
Jacob Keller435c0842016-11-08 13:05:10 -08002114
2115 /* handle broadcast filters by updating the broadcast
2116 * promiscuous flag instead of adding a MAC filter.
2117 */
2118 if (is_broadcast_ether_addr(f->macaddr)) {
2119 u64 key = i40e_addr_to_hkey(f->macaddr);
2120 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2121
2122 hlist_del(&f->hlist);
2123 hash_add(vsi->mac_filter_hash, &f->hlist, key);
2124 continue;
2125 }
2126
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002127 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002128 if (num_add == 0)
2129 add_head = f;
2130 cmd_flags = 0;
Greg Rose9a173902014-05-22 06:32:02 +00002131 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002132 if (f->vlan == I40E_VLAN_ANY) {
2133 add_list[num_add].vlan_tag = 0;
2134 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2135 } else {
2136 add_list[num_add].vlan_tag =
2137 cpu_to_le16((u16)(f->vlan));
2138 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002139 add_list[num_add].queue_number = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002140 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002141 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2142 num_add++;
2143
2144 /* flush a full buffer */
2145 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002146 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2147 add_head, num_add,
2148 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002149 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002150 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002151 }
2152 }
2153 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002154 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2155 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002156 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002157 /* Now move all of the filters from the temp add list back to
2158 * the VSI's list.
2159 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002160 spin_lock_bh(&vsi->mac_filter_hash_lock);
2161 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
2162 u64 key = i40e_addr_to_hkey(f->macaddr);
2163
2164 hlist_del(&f->hlist);
2165 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002166 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002167 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002168 kfree(add_list);
2169 add_list = NULL;
2170 }
2171
2172 /* Check to see if we can drop out of overflow promiscuous mode. */
2173 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
2174 (vsi->active_filters < vsi->promisc_threshold)) {
2175 int failed_count = 0;
2176 /* See if we have any failed filters. We can't drop out of
2177 * promiscuous until these have all been deleted.
2178 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002179 spin_lock_bh(&vsi->mac_filter_hash_lock);
2180 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002181 if (f->state == I40E_FILTER_FAILED)
2182 failed_count++;
2183 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002184 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002185 if (!failed_count) {
2186 dev_info(&pf->pdev->dev,
2187 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2188 vsi_name);
2189 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2190 promisc_changed = true;
2191 vsi->promisc_threshold = 0;
2192 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002193 }
2194
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002195 /* if the VF is not trusted do not do promisc */
2196 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2197 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2198 goto out;
2199 }
2200
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002201 /* check for changes in promiscuous modes */
2202 if (changed_flags & IFF_ALLMULTI) {
2203 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002204
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002205 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002206 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2207 vsi->seid,
2208 cur_multipromisc,
2209 NULL);
2210 if (aq_ret) {
2211 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002212 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002213 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002214 "set multi promisc failed on %s, err %s aq_err %s\n",
2215 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002216 i40e_stat_str(hw, aq_ret),
2217 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002218 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002219 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002220 if ((changed_flags & IFF_PROMISC) ||
2221 (promisc_changed &&
2222 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002223 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002224
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002225 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2226 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2227 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002228 if ((vsi->type == I40E_VSI_MAIN) &&
2229 (pf->lan_veb != I40E_NO_VEB) &&
2230 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002231 /* set defport ON for Main VSI instead of true promisc
2232 * this way we will get all unicast/multicast and VLAN
2233 * promisc behavior but will not get VF or VMDq traffic
2234 * replicated on the Main VSI.
2235 */
2236 if (pf->cur_promisc != cur_promisc) {
2237 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002238 if (cur_promisc)
2239 aq_ret =
2240 i40e_aq_set_default_vsi(hw,
2241 vsi->seid,
2242 NULL);
2243 else
2244 aq_ret =
2245 i40e_aq_clear_default_vsi(hw,
2246 vsi->seid,
2247 NULL);
2248 if (aq_ret) {
2249 retval = i40e_aq_rc_to_posix(aq_ret,
2250 hw->aq.asq_last_status);
2251 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002252 "Set default VSI failed on %s, err %s, aq_err %s\n",
2253 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002254 i40e_stat_str(hw, aq_ret),
2255 i40e_aq_str(hw,
2256 hw->aq.asq_last_status));
2257 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002258 }
2259 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002260 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002261 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002262 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002263 cur_promisc, NULL,
2264 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002265 if (aq_ret) {
2266 retval =
2267 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002268 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002269 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002270 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2271 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002272 i40e_stat_str(hw, aq_ret),
2273 i40e_aq_str(hw,
2274 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002275 }
2276 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002277 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002278 vsi->seid,
2279 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002280 if (aq_ret) {
2281 retval =
2282 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002283 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002284 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002285 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2286 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002287 i40e_stat_str(hw, aq_ret),
2288 i40e_aq_str(hw,
2289 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002290 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002291 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002292 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2293 vsi->seid,
2294 cur_promisc, NULL);
2295 if (aq_ret) {
2296 retval = i40e_aq_rc_to_posix(aq_ret,
2297 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002298 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002299 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002300 i40e_stat_str(hw, aq_ret),
2301 i40e_aq_str(hw,
2302 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002303 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002304 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002305out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002306 /* if something went wrong then set the changed flag so we try again */
2307 if (retval)
2308 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2309
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002310 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002311 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002312
2313err_no_memory:
2314 /* Restore elements on the temporary add and delete lists */
2315 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002316err_no_memory_locked:
Jacob Keller4a2ce272016-10-05 09:30:38 -07002317 i40e_undo_filter_entries(vsi, &tmp_del_list);
2318 i40e_undo_filter_entries(vsi, &tmp_add_list);
2319 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2320
2321 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2322 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2323 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002324}
2325
2326/**
2327 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2328 * @pf: board private structure
2329 **/
2330static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2331{
2332 int v;
2333
2334 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2335 return;
2336 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2337
Mitch Williams505682c2014-05-20 08:01:37 +00002338 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002339 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002340 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2341 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2342
2343 if (ret) {
2344 /* come back and try again later */
2345 pf->flags |= I40E_FLAG_FILTER_SYNC;
2346 break;
2347 }
2348 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002349 }
2350}
2351
2352/**
2353 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2354 * @netdev: network interface device structure
2355 * @new_mtu: new value for maximum frame size
2356 *
2357 * Returns 0 on success, negative on failure
2358 **/
2359static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2360{
2361 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002362 struct i40e_vsi *vsi = np->vsi;
2363
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002364 netdev_info(netdev, "changing MTU from %d to %d\n",
2365 netdev->mtu, new_mtu);
2366 netdev->mtu = new_mtu;
2367 if (netif_running(netdev))
2368 i40e_vsi_reinit_locked(vsi);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06002369 i40e_notify_client_of_l2_param_changes(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002370 return 0;
2371}
2372
2373/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002374 * i40e_ioctl - Access the hwtstamp interface
2375 * @netdev: network interface device structure
2376 * @ifr: interface request data
2377 * @cmd: ioctl command
2378 **/
2379int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2380{
2381 struct i40e_netdev_priv *np = netdev_priv(netdev);
2382 struct i40e_pf *pf = np->vsi->back;
2383
2384 switch (cmd) {
2385 case SIOCGHWTSTAMP:
2386 return i40e_ptp_get_ts_config(pf, ifr);
2387 case SIOCSHWTSTAMP:
2388 return i40e_ptp_set_ts_config(pf, ifr);
2389 default:
2390 return -EOPNOTSUPP;
2391 }
2392}
2393
2394/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002395 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2396 * @vsi: the vsi being adjusted
2397 **/
2398void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2399{
2400 struct i40e_vsi_context ctxt;
2401 i40e_status ret;
2402
2403 if ((vsi->info.valid_sections &
2404 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2405 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2406 return; /* already enabled */
2407
2408 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2409 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2410 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2411
2412 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002413 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002414 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2415 if (ret) {
2416 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002417 "update vlan stripping failed, err %s aq_err %s\n",
2418 i40e_stat_str(&vsi->back->hw, ret),
2419 i40e_aq_str(&vsi->back->hw,
2420 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002421 }
2422}
2423
2424/**
2425 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2426 * @vsi: the vsi being adjusted
2427 **/
2428void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2429{
2430 struct i40e_vsi_context ctxt;
2431 i40e_status ret;
2432
2433 if ((vsi->info.valid_sections &
2434 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2435 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2436 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2437 return; /* already disabled */
2438
2439 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2440 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2441 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2442
2443 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002444 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002445 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2446 if (ret) {
2447 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002448 "update vlan stripping failed, err %s aq_err %s\n",
2449 i40e_stat_str(&vsi->back->hw, ret),
2450 i40e_aq_str(&vsi->back->hw,
2451 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002452 }
2453}
2454
2455/**
2456 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2457 * @netdev: network interface to be adjusted
2458 * @features: netdev features to test if VLAN offload is enabled or not
2459 **/
2460static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2461{
2462 struct i40e_netdev_priv *np = netdev_priv(netdev);
2463 struct i40e_vsi *vsi = np->vsi;
2464
2465 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2466 i40e_vlan_stripping_enable(vsi);
2467 else
2468 i40e_vlan_stripping_disable(vsi);
2469}
2470
2471/**
2472 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2473 * @vsi: the vsi being configured
2474 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2475 **/
2476int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2477{
Jacob Keller278e7d02016-10-05 09:30:37 -07002478 struct i40e_mac_filter *f, *add_f, *del_f;
2479 struct hlist_node *h;
2480 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002481
Kiran Patil21659032015-09-30 14:09:03 -04002482 /* Locked once because all functions invoked below iterates list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07002483 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002484
Jacob Keller1bc87e82016-10-05 09:30:31 -07002485 if (vsi->netdev) {
2486 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002487 if (!add_f) {
2488 dev_info(&vsi->back->pdev->dev,
2489 "Could not add vlan filter %d for %pM\n",
2490 vid, vsi->netdev->dev_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002491 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002492 return -ENOMEM;
2493 }
2494 }
2495
Jacob Keller278e7d02016-10-05 09:30:37 -07002496 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002497 if (f->state == I40E_FILTER_REMOVE)
2498 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002499 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002500 if (!add_f) {
2501 dev_info(&vsi->back->pdev->dev,
2502 "Could not add vlan filter %d for %pM\n",
2503 vid, f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002504 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002505 return -ENOMEM;
2506 }
2507 }
2508
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002509 /* Now if we add a vlan tag, make sure to check if it is the first
2510 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2511 * with 0, so we now accept untagged and specified tagged traffic
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002512 * (and not all tags along with untagged)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002513 */
Jacob Keller290d2552016-10-05 09:30:36 -07002514 if (vid > 0 && vsi->netdev) {
2515 del_f = i40e_find_filter(vsi, vsi->netdev->dev_addr,
2516 I40E_VLAN_ANY);
2517 if (del_f) {
2518 __i40e_del_filter(vsi, del_f);
Jacob Keller1bc87e82016-10-05 09:30:31 -07002519 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002520 if (!add_f) {
2521 dev_info(&vsi->back->pdev->dev,
2522 "Could not add filter 0 for %pM\n",
2523 vsi->netdev->dev_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002524 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002525 return -ENOMEM;
2526 }
2527 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002528 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002529
Greg Rose8d82a7c2014-01-13 16:13:04 -08002530 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2531 if (vid > 0 && !vsi->info.pvid) {
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 Keller290d2552016-10-05 09:30:36 -07002535 del_f = i40e_find_filter(vsi, f->macaddr,
2536 I40E_VLAN_ANY);
2537 if (!del_f)
Kiran Patil21659032015-09-30 14:09:03 -04002538 continue;
Jacob Keller290d2552016-10-05 09:30:36 -07002539 __i40e_del_filter(vsi, del_f);
Jacob Keller1bc87e82016-10-05 09:30:31 -07002540 add_f = i40e_add_filter(vsi, f->macaddr, 0);
Kiran Patil21659032015-09-30 14:09:03 -04002541 if (!add_f) {
2542 dev_info(&vsi->back->pdev->dev,
2543 "Could not add filter 0 for %pM\n",
2544 f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002545 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002546 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002547 }
2548 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002549 }
2550
Jacob Keller278e7d02016-10-05 09:30:37 -07002551 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002552
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002553 /* schedule our worker thread which will take care of
2554 * applying the new filter changes
2555 */
2556 i40e_service_event_schedule(vsi->back);
2557 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002558}
2559
2560/**
2561 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2562 * @vsi: the vsi being configured
2563 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2564 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07002565void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002566{
2567 struct net_device *netdev = vsi->netdev;
Alan Brady84f5ca62016-10-05 09:30:39 -07002568 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002569 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002570 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002571
Kiran Patil21659032015-09-30 14:09:03 -04002572 /* Locked once because all functions invoked below iterates list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002573 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002574
Jacob Keller1bc87e82016-10-05 09:30:31 -07002575 if (vsi->netdev)
2576 i40e_del_filter(vsi, netdev->dev_addr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002577
Jacob Keller278e7d02016-10-05 09:30:37 -07002578 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002579 if (f->vlan == vid)
2580 __i40e_del_filter(vsi, f);
2581 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002582
Jacob Keller278e7d02016-10-05 09:30:37 -07002583 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002584
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002585 /* schedule our worker thread which will take care of
2586 * applying the new filter changes
2587 */
2588 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002589}
2590
2591/**
2592 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2593 * @netdev: network interface to be adjusted
2594 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002595 *
2596 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002597 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002598#ifdef I40E_FCOE
2599int i40e_vlan_rx_add_vid(struct net_device *netdev,
2600 __always_unused __be16 proto, u16 vid)
2601#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002602static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2603 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002604#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002605{
2606 struct i40e_netdev_priv *np = netdev_priv(netdev);
2607 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002608 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002609
Jacob Keller6a1127852016-10-25 16:08:49 -07002610 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002611 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002612
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002613 /* If the network stack called us with vid = 0 then
2614 * it is asking to receive priority tagged packets with
2615 * vlan id 0. Our HW receives them by default when configured
2616 * to receive untagged packets so there is no need to add an
2617 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002618 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002619 if (vid)
2620 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002621
Jacob Keller6a1127852016-10-25 16:08:49 -07002622 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002623 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002624
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002625 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002626}
2627
2628/**
2629 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2630 * @netdev: network interface to be adjusted
2631 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002632 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002633 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002634 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002635#ifdef I40E_FCOE
2636int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2637 __always_unused __be16 proto, u16 vid)
2638#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002639static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2640 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002641#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002642{
2643 struct i40e_netdev_priv *np = netdev_priv(netdev);
2644 struct i40e_vsi *vsi = np->vsi;
2645
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002646 /* return code is ignored as there is nothing a user
2647 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002648 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002649 */
2650 i40e_vsi_kill_vlan(vsi, vid);
2651
2652 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002653
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002654 return 0;
2655}
2656
2657/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002658 * i40e_macaddr_init - explicitly write the mac address filters
2659 *
2660 * @vsi: pointer to the vsi
2661 * @macaddr: the MAC address
2662 *
2663 * This is needed when the macaddr has been obtained by other
2664 * means than the default, e.g., from Open Firmware or IDPROM.
2665 * Returns 0 on success, negative on failure
2666 **/
2667static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2668{
2669 int ret;
2670 struct i40e_aqc_add_macvlan_element_data element;
2671
2672 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2673 I40E_AQC_WRITE_TYPE_LAA_WOL,
2674 macaddr, NULL);
2675 if (ret) {
2676 dev_info(&vsi->back->pdev->dev,
2677 "Addr change for VSI failed: %d\n", ret);
2678 return -EADDRNOTAVAIL;
2679 }
2680
2681 memset(&element, 0, sizeof(element));
2682 ether_addr_copy(element.mac_addr, macaddr);
2683 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2684 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2685 if (ret) {
2686 dev_info(&vsi->back->pdev->dev,
2687 "add filter failed err %s aq_err %s\n",
2688 i40e_stat_str(&vsi->back->hw, ret),
2689 i40e_aq_str(&vsi->back->hw,
2690 vsi->back->hw.aq.asq_last_status));
2691 }
2692 return ret;
2693}
2694
2695/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002696 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2697 * @vsi: the vsi being brought back up
2698 **/
2699static void i40e_restore_vlan(struct i40e_vsi *vsi)
2700{
2701 u16 vid;
2702
2703 if (!vsi->netdev)
2704 return;
2705
2706 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2707
2708 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2709 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2710 vid);
2711}
2712
2713/**
2714 * i40e_vsi_add_pvid - Add pvid for the VSI
2715 * @vsi: the vsi being adjusted
2716 * @vid: the vlan id to set as a PVID
2717 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002718int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002719{
2720 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002721 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002722
2723 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2724 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002725 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2726 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002727 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002728
2729 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002730 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002731 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2732 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002733 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002734 "add pvid failed, err %s aq_err %s\n",
2735 i40e_stat_str(&vsi->back->hw, ret),
2736 i40e_aq_str(&vsi->back->hw,
2737 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002738 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002739 }
2740
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002741 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002742}
2743
2744/**
2745 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2746 * @vsi: the vsi being adjusted
2747 *
2748 * Just use the vlan_rx_register() service to put it back to normal
2749 **/
2750void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2751{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002752 i40e_vlan_stripping_disable(vsi);
2753
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002754 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002755}
2756
2757/**
2758 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2759 * @vsi: ptr to the VSI
2760 *
2761 * If this function returns with an error, then it's possible one or
2762 * more of the rings is populated (while the rest are not). It is the
2763 * callers duty to clean those orphaned rings.
2764 *
2765 * Return 0 on success, negative on failure
2766 **/
2767static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2768{
2769 int i, err = 0;
2770
2771 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002772 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002773
2774 return err;
2775}
2776
2777/**
2778 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2779 * @vsi: ptr to the VSI
2780 *
2781 * Free VSI's transmit software resources
2782 **/
2783static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2784{
2785 int i;
2786
Greg Rose8e9dca52013-12-18 13:45:53 +00002787 if (!vsi->tx_rings)
2788 return;
2789
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002790 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002791 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002792 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002793}
2794
2795/**
2796 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2797 * @vsi: ptr to the VSI
2798 *
2799 * If this function returns with an error, then it's possible one or
2800 * more of the rings is populated (while the rest are not). It is the
2801 * callers duty to clean those orphaned rings.
2802 *
2803 * Return 0 on success, negative on failure
2804 **/
2805static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2806{
2807 int i, err = 0;
2808
2809 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002810 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002811#ifdef I40E_FCOE
2812 i40e_fcoe_setup_ddp_resources(vsi);
2813#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002814 return err;
2815}
2816
2817/**
2818 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2819 * @vsi: ptr to the VSI
2820 *
2821 * Free all receive software resources
2822 **/
2823static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2824{
2825 int i;
2826
Greg Rose8e9dca52013-12-18 13:45:53 +00002827 if (!vsi->rx_rings)
2828 return;
2829
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002830 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002831 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002832 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002833#ifdef I40E_FCOE
2834 i40e_fcoe_free_ddp_resources(vsi);
2835#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002836}
2837
2838/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002839 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2840 * @ring: The Tx ring to configure
2841 *
2842 * This enables/disables XPS for a given Tx descriptor ring
2843 * based on the TCs enabled for the VSI that ring belongs to.
2844 **/
2845static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2846{
2847 struct i40e_vsi *vsi = ring->vsi;
2848 cpumask_var_t mask;
2849
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002850 if (!ring->q_vector || !ring->netdev)
2851 return;
2852
2853 /* Single TC mode enable XPS */
2854 if (vsi->tc_config.numtc <= 1) {
2855 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002856 netif_set_xps_queue(ring->netdev,
2857 &ring->q_vector->affinity_mask,
2858 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002859 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2860 /* Disable XPS to allow selection based on TC */
2861 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2862 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2863 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002864 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002865
2866 /* schedule our worker thread which will take care of
2867 * applying the new filter changes
2868 */
2869 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002870}
2871
2872/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002873 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2874 * @ring: The Tx ring to configure
2875 *
2876 * Configure the Tx descriptor ring in the HMC context.
2877 **/
2878static int i40e_configure_tx_ring(struct i40e_ring *ring)
2879{
2880 struct i40e_vsi *vsi = ring->vsi;
2881 u16 pf_q = vsi->base_queue + ring->queue_index;
2882 struct i40e_hw *hw = &vsi->back->hw;
2883 struct i40e_hmc_obj_txq tx_ctx;
2884 i40e_status err = 0;
2885 u32 qtx_ctl = 0;
2886
2887 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002888 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002889 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2890 ring->atr_count = 0;
2891 } else {
2892 ring->atr_sample_rate = 0;
2893 }
2894
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002895 /* configure XPS */
2896 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002897
2898 /* clear the context structure first */
2899 memset(&tx_ctx, 0, sizeof(tx_ctx));
2900
2901 tx_ctx.new_context = 1;
2902 tx_ctx.base = (ring->dma / 128);
2903 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002904 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2905 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002906#ifdef I40E_FCOE
2907 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2908#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002909 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002910 /* FDIR VSI tx ring can still use RS bit and writebacks */
2911 if (vsi->type != I40E_VSI_FDIR)
2912 tx_ctx.head_wb_ena = 1;
2913 tx_ctx.head_wb_addr = ring->dma +
2914 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002915
2916 /* As part of VSI creation/update, FW allocates certain
2917 * Tx arbitration queue sets for each TC enabled for
2918 * the VSI. The FW returns the handles to these queue
2919 * sets as part of the response buffer to Add VSI,
2920 * Update VSI, etc. AQ commands. It is expected that
2921 * these queue set handles be associated with the Tx
2922 * queues by the driver as part of the TX queue context
2923 * initialization. This has to be done regardless of
2924 * DCB as by default everything is mapped to TC0.
2925 */
2926 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2927 tx_ctx.rdylist_act = 0;
2928
2929 /* clear the context in the HMC */
2930 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2931 if (err) {
2932 dev_info(&vsi->back->pdev->dev,
2933 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2934 ring->queue_index, pf_q, err);
2935 return -ENOMEM;
2936 }
2937
2938 /* set the context in the HMC */
2939 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2940 if (err) {
2941 dev_info(&vsi->back->pdev->dev,
2942 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2943 ring->queue_index, pf_q, err);
2944 return -ENOMEM;
2945 }
2946
2947 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002948 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002949 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002950 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2951 I40E_QTX_CTL_VFVM_INDX_MASK;
2952 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002953 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002954 }
2955
Shannon Nelson13fd9772013-09-28 07:14:19 +00002956 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2957 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002958 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2959 i40e_flush(hw);
2960
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002961 /* cache tail off for easier writes later */
2962 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2963
2964 return 0;
2965}
2966
2967/**
2968 * i40e_configure_rx_ring - Configure a receive ring context
2969 * @ring: The Rx ring to configure
2970 *
2971 * Configure the Rx descriptor ring in the HMC context.
2972 **/
2973static int i40e_configure_rx_ring(struct i40e_ring *ring)
2974{
2975 struct i40e_vsi *vsi = ring->vsi;
2976 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2977 u16 pf_q = vsi->base_queue + ring->queue_index;
2978 struct i40e_hw *hw = &vsi->back->hw;
2979 struct i40e_hmc_obj_rxq rx_ctx;
2980 i40e_status err = 0;
2981
2982 ring->state = 0;
2983
2984 /* clear the context structure first */
2985 memset(&rx_ctx, 0, sizeof(rx_ctx));
2986
2987 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002988
2989 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002990
2991 rx_ctx.base = (ring->dma / 128);
2992 rx_ctx.qlen = ring->count;
2993
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07002994 /* use 32 byte descriptors */
2995 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002996
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07002997 /* descriptor type is always zero
2998 * rx_ctx.dtype = 0;
2999 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003000 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003001
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003002 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003003 if (hw->revision_id == 0)
3004 rx_ctx.lrxqthresh = 0;
3005 else
3006 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003007 rx_ctx.crcstrip = 1;
3008 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003009 /* this controls whether VLAN is stripped from inner headers */
3010 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07003011#ifdef I40E_FCOE
3012 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
3013#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00003014 /* set the prefena field to 1 because the manual says to */
3015 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003016
3017 /* clear the context in the HMC */
3018 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3019 if (err) {
3020 dev_info(&vsi->back->pdev->dev,
3021 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3022 ring->queue_index, pf_q, err);
3023 return -ENOMEM;
3024 }
3025
3026 /* set the context in the HMC */
3027 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3028 if (err) {
3029 dev_info(&vsi->back->pdev->dev,
3030 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3031 ring->queue_index, pf_q, err);
3032 return -ENOMEM;
3033 }
3034
3035 /* cache tail for quicker writes, and clear the reg before use */
3036 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3037 writel(0, ring->tail);
3038
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003039 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003040
3041 return 0;
3042}
3043
3044/**
3045 * i40e_vsi_configure_tx - Configure the VSI for Tx
3046 * @vsi: VSI structure describing this set of rings and resources
3047 *
3048 * Configure the Tx VSI for operation.
3049 **/
3050static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3051{
3052 int err = 0;
3053 u16 i;
3054
Alexander Duyck9f65e152013-09-28 06:00:58 +00003055 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3056 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003057
3058 return err;
3059}
3060
3061/**
3062 * i40e_vsi_configure_rx - Configure the VSI for Rx
3063 * @vsi: the VSI being configured
3064 *
3065 * Configure the Rx VSI for operation.
3066 **/
3067static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3068{
3069 int err = 0;
3070 u16 i;
3071
3072 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
3073 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
3074 + ETH_FCS_LEN + VLAN_HLEN;
3075 else
3076 vsi->max_frame = I40E_RXBUFFER_2048;
3077
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003078 vsi->rx_buf_len = I40E_RXBUFFER_2048;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003079
Vasu Dev38e00432014-08-01 13:27:03 -07003080#ifdef I40E_FCOE
3081 /* setup rx buffer for FCoE */
3082 if ((vsi->type == I40E_VSI_FCOE) &&
3083 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
Vasu Dev38e00432014-08-01 13:27:03 -07003084 vsi->rx_buf_len = I40E_RXBUFFER_3072;
3085 vsi->max_frame = I40E_RXBUFFER_3072;
Vasu Dev38e00432014-08-01 13:27:03 -07003086 }
3087
3088#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003089 /* round up for the chip's needs */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003090 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003091 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003092
3093 /* set up individual rings */
3094 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003095 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003096
3097 return err;
3098}
3099
3100/**
3101 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3102 * @vsi: ptr to the VSI
3103 **/
3104static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3105{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003106 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003107 u16 qoffset, qcount;
3108 int i, n;
3109
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003110 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3111 /* Reset the TC information */
3112 for (i = 0; i < vsi->num_queue_pairs; i++) {
3113 rx_ring = vsi->rx_rings[i];
3114 tx_ring = vsi->tx_rings[i];
3115 rx_ring->dcb_tc = 0;
3116 tx_ring->dcb_tc = 0;
3117 }
3118 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003119
3120 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003121 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003122 continue;
3123
3124 qoffset = vsi->tc_config.tc_info[n].qoffset;
3125 qcount = vsi->tc_config.tc_info[n].qcount;
3126 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003127 rx_ring = vsi->rx_rings[i];
3128 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003129 rx_ring->dcb_tc = n;
3130 tx_ring->dcb_tc = n;
3131 }
3132 }
3133}
3134
3135/**
3136 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3137 * @vsi: ptr to the VSI
3138 **/
3139static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3140{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003141 struct i40e_pf *pf = vsi->back;
3142 int err;
3143
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003144 if (vsi->netdev)
3145 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003146
3147 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3148 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3149 if (err) {
3150 dev_warn(&pf->pdev->dev,
3151 "could not set up macaddr; err %d\n", err);
3152 }
3153 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003154}
3155
3156/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003157 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3158 * @vsi: Pointer to the targeted VSI
3159 *
3160 * This function replays the hlist on the hw where all the SB Flow Director
3161 * filters were saved.
3162 **/
3163static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3164{
3165 struct i40e_fdir_filter *filter;
3166 struct i40e_pf *pf = vsi->back;
3167 struct hlist_node *node;
3168
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003169 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3170 return;
3171
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003172 hlist_for_each_entry_safe(filter, node,
3173 &pf->fdir_filter_list, fdir_node) {
3174 i40e_add_del_fdir(vsi, filter, true);
3175 }
3176}
3177
3178/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003179 * i40e_vsi_configure - Set up the VSI for action
3180 * @vsi: the VSI being configured
3181 **/
3182static int i40e_vsi_configure(struct i40e_vsi *vsi)
3183{
3184 int err;
3185
3186 i40e_set_vsi_rx_mode(vsi);
3187 i40e_restore_vlan(vsi);
3188 i40e_vsi_config_dcb_rings(vsi);
3189 err = i40e_vsi_configure_tx(vsi);
3190 if (!err)
3191 err = i40e_vsi_configure_rx(vsi);
3192
3193 return err;
3194}
3195
3196/**
3197 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3198 * @vsi: the VSI being configured
3199 **/
3200static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3201{
3202 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003203 struct i40e_hw *hw = &pf->hw;
3204 u16 vector;
3205 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003206 u32 qp;
3207
3208 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3209 * and PFINT_LNKLSTn registers, e.g.:
3210 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3211 */
3212 qp = vsi->base_queue;
3213 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003214 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003215 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3216
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003217 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003218 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003219 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3220 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3221 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003222 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003223 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3224 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3225 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003226 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3227 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003228
3229 /* Linked list for the queuepairs assigned to this vector */
3230 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3231 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003232 u32 val;
3233
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003234 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3235 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3236 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3237 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3238 (I40E_QUEUE_TYPE_TX
3239 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3240
3241 wr32(hw, I40E_QINT_RQCTL(qp), val);
3242
3243 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3244 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3245 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3246 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3247 (I40E_QUEUE_TYPE_RX
3248 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3249
3250 /* Terminate the linked list */
3251 if (q == (q_vector->num_ringpairs - 1))
3252 val |= (I40E_QUEUE_END_OF_LIST
3253 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3254
3255 wr32(hw, I40E_QINT_TQCTL(qp), val);
3256 qp++;
3257 }
3258 }
3259
3260 i40e_flush(hw);
3261}
3262
3263/**
3264 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3265 * @hw: ptr to the hardware info
3266 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003267static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003268{
Jacob Kellerab437b52014-12-14 01:55:08 +00003269 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003270 u32 val;
3271
3272 /* clear things first */
3273 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3274 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3275
3276 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3277 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3278 I40E_PFINT_ICR0_ENA_GRST_MASK |
3279 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3280 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003281 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3282 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3283 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3284
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003285 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3286 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3287
Jacob Kellerab437b52014-12-14 01:55:08 +00003288 if (pf->flags & I40E_FLAG_PTP)
3289 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3290
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003291 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3292
3293 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003294 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3295 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003296
3297 /* OTHER_ITR_IDX = 0 */
3298 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3299}
3300
3301/**
3302 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3303 * @vsi: the VSI being configured
3304 **/
3305static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3306{
Alexander Duyck493fb302013-09-28 07:01:44 +00003307 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003308 struct i40e_pf *pf = vsi->back;
3309 struct i40e_hw *hw = &pf->hw;
3310 u32 val;
3311
3312 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003313 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003314 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003315 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3316 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003317 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003318 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3319 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3320
Jacob Kellerab437b52014-12-14 01:55:08 +00003321 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003322
3323 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3324 wr32(hw, I40E_PFINT_LNKLST0, 0);
3325
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003326 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003327 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3328 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3329 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3330
3331 wr32(hw, I40E_QINT_RQCTL(0), val);
3332
3333 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3334 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3335 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3336
3337 wr32(hw, I40E_QINT_TQCTL(0), val);
3338 i40e_flush(hw);
3339}
3340
3341/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003342 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3343 * @pf: board private structure
3344 **/
3345void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3346{
3347 struct i40e_hw *hw = &pf->hw;
3348
3349 wr32(hw, I40E_PFINT_DYN_CTL0,
3350 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3351 i40e_flush(hw);
3352}
3353
3354/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003355 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3356 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003357 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003358 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003359void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003360{
3361 struct i40e_hw *hw = &pf->hw;
3362 u32 val;
3363
3364 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003365 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003366 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3367
3368 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3369 i40e_flush(hw);
3370}
3371
3372/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003373 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3374 * @irq: interrupt number
3375 * @data: pointer to a q_vector
3376 **/
3377static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3378{
3379 struct i40e_q_vector *q_vector = data;
3380
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003381 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003382 return IRQ_HANDLED;
3383
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003384 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003385
3386 return IRQ_HANDLED;
3387}
3388
3389/**
Alan Brady96db7762016-09-14 16:24:38 -07003390 * i40e_irq_affinity_notify - Callback for affinity changes
3391 * @notify: context as to what irq was changed
3392 * @mask: the new affinity mask
3393 *
3394 * This is a callback function used by the irq_set_affinity_notifier function
3395 * so that we may register to receive changes to the irq affinity masks.
3396 **/
3397static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3398 const cpumask_t *mask)
3399{
3400 struct i40e_q_vector *q_vector =
3401 container_of(notify, struct i40e_q_vector, affinity_notify);
3402
3403 q_vector->affinity_mask = *mask;
3404}
3405
3406/**
3407 * i40e_irq_affinity_release - Callback for affinity notifier release
3408 * @ref: internal core kernel usage
3409 *
3410 * This is a callback function used by the irq_set_affinity_notifier function
3411 * to inform the current notification subscriber that they will no longer
3412 * receive notifications.
3413 **/
3414static void i40e_irq_affinity_release(struct kref *ref) {}
3415
3416/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003417 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3418 * @vsi: the VSI being configured
3419 * @basename: name for the vector
3420 *
3421 * Allocates MSI-X vectors and requests interrupts from the kernel.
3422 **/
3423static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3424{
3425 int q_vectors = vsi->num_q_vectors;
3426 struct i40e_pf *pf = vsi->back;
3427 int base = vsi->base_vector;
3428 int rx_int_idx = 0;
3429 int tx_int_idx = 0;
3430 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003431 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003432
3433 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003434 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003435
Alan Brady96db7762016-09-14 16:24:38 -07003436 irq_num = pf->msix_entries[base + vector].vector;
3437
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003438 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003439 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3440 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3441 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003442 } else if (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, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003445 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003446 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3447 "%s-%s-%d", basename, "tx", tx_int_idx++);
3448 } else {
3449 /* skip this unused q_vector */
3450 continue;
3451 }
Alan Brady96db7762016-09-14 16:24:38 -07003452 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003453 vsi->irq_handler,
3454 0,
3455 q_vector->name,
3456 q_vector);
3457 if (err) {
3458 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003459 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003460 goto free_queue_irqs;
3461 }
Alan Brady96db7762016-09-14 16:24:38 -07003462
3463 /* register for affinity change notifications */
3464 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3465 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3466 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003467 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003468 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003469 }
3470
Shannon Nelson63741842014-04-23 04:50:16 +00003471 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003472 return 0;
3473
3474free_queue_irqs:
3475 while (vector) {
3476 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003477 irq_num = pf->msix_entries[base + vector].vector;
3478 irq_set_affinity_notifier(irq_num, NULL);
3479 irq_set_affinity_hint(irq_num, NULL);
3480 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003481 }
3482 return err;
3483}
3484
3485/**
3486 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3487 * @vsi: the VSI being un-configured
3488 **/
3489static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3490{
3491 struct i40e_pf *pf = vsi->back;
3492 struct i40e_hw *hw = &pf->hw;
3493 int base = vsi->base_vector;
3494 int i;
3495
3496 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003497 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3498 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003499 }
3500
3501 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3502 for (i = vsi->base_vector;
3503 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3504 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3505
3506 i40e_flush(hw);
3507 for (i = 0; i < vsi->num_q_vectors; i++)
3508 synchronize_irq(pf->msix_entries[i + base].vector);
3509 } else {
3510 /* Legacy and MSI mode - this stops all interrupt handling */
3511 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3512 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3513 i40e_flush(hw);
3514 synchronize_irq(pf->pdev->irq);
3515 }
3516}
3517
3518/**
3519 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3520 * @vsi: the VSI being configured
3521 **/
3522static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3523{
3524 struct i40e_pf *pf = vsi->back;
3525 int i;
3526
3527 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003528 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003529 i40e_irq_dynamic_enable(vsi, i);
3530 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003531 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003532 }
3533
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003534 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003535 return 0;
3536}
3537
3538/**
3539 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3540 * @pf: board private structure
3541 **/
3542static void i40e_stop_misc_vector(struct i40e_pf *pf)
3543{
3544 /* Disable ICR 0 */
3545 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3546 i40e_flush(&pf->hw);
3547}
3548
3549/**
3550 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3551 * @irq: interrupt number
3552 * @data: pointer to a q_vector
3553 *
3554 * This is the handler used for all MSI/Legacy interrupts, and deals
3555 * with both queue and non-queue interrupts. This is also used in
3556 * MSIX mode to handle the non-queue interrupts.
3557 **/
3558static irqreturn_t i40e_intr(int irq, void *data)
3559{
3560 struct i40e_pf *pf = (struct i40e_pf *)data;
3561 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003562 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003563 u32 icr0, icr0_remaining;
3564 u32 val, ena_mask;
3565
3566 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003567 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003568
Shannon Nelson116a57d2013-09-28 07:13:59 +00003569 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3570 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003571 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003572
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003573 /* if interrupt but no bits showing, must be SWINT */
3574 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3575 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3576 pf->sw_int_count++;
3577
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003578 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3579 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3580 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3581 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003582 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003583 }
3584
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003585 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3586 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003587 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3588 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003589
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003590 /* We do not have a way to disarm Queue causes while leaving
3591 * interrupt enabled for all other causes, ideally
3592 * interrupt should be disabled while we are in NAPI but
3593 * this is not a performance path and napi_schedule()
3594 * can deal with rescheduling.
3595 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003596 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003597 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003598 }
3599
3600 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3601 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3602 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003603 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003604 }
3605
3606 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3607 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3608 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3609 }
3610
3611 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3612 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3613 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3614 }
3615
3616 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3617 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3618 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3619 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3620 val = rd32(hw, I40E_GLGEN_RSTAT);
3621 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3622 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003623 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003624 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003625 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003626 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003627 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003628 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003629 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003630 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003631 }
3632
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003633 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3634 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3635 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003636 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3637 rd32(hw, I40E_PFHMC_ERRORINFO),
3638 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003639 }
3640
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003641 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3642 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3643
3644 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003645 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003646 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003647 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003648 }
3649
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003650 /* If a critical error is pending we have no choice but to reset the
3651 * device.
3652 * Report and mask out any remaining unexpected interrupts.
3653 */
3654 icr0_remaining = icr0 & ena_mask;
3655 if (icr0_remaining) {
3656 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3657 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003658 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003659 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003660 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003661 dev_info(&pf->pdev->dev, "device will be reset\n");
3662 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3663 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003664 }
3665 ena_mask &= ~icr0_remaining;
3666 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003667 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003668
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003669enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003670 /* re-enable interrupt causes */
3671 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003672 if (!test_bit(__I40E_DOWN, &pf->state)) {
3673 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003674 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003675 }
3676
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003677 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003678}
3679
3680/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003681 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3682 * @tx_ring: tx ring to clean
3683 * @budget: how many cleans we're allowed
3684 *
3685 * Returns true if there's any budget left (e.g. the clean is finished)
3686 **/
3687static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3688{
3689 struct i40e_vsi *vsi = tx_ring->vsi;
3690 u16 i = tx_ring->next_to_clean;
3691 struct i40e_tx_buffer *tx_buf;
3692 struct i40e_tx_desc *tx_desc;
3693
3694 tx_buf = &tx_ring->tx_bi[i];
3695 tx_desc = I40E_TX_DESC(tx_ring, i);
3696 i -= tx_ring->count;
3697
3698 do {
3699 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3700
3701 /* if next_to_watch is not set then there is no work pending */
3702 if (!eop_desc)
3703 break;
3704
3705 /* prevent any other reads prior to eop_desc */
3706 read_barrier_depends();
3707
3708 /* if the descriptor isn't done, no work yet to do */
3709 if (!(eop_desc->cmd_type_offset_bsz &
3710 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3711 break;
3712
3713 /* clear next_to_watch to prevent false hangs */
3714 tx_buf->next_to_watch = NULL;
3715
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003716 tx_desc->buffer_addr = 0;
3717 tx_desc->cmd_type_offset_bsz = 0;
3718 /* move past filter desc */
3719 tx_buf++;
3720 tx_desc++;
3721 i++;
3722 if (unlikely(!i)) {
3723 i -= tx_ring->count;
3724 tx_buf = tx_ring->tx_bi;
3725 tx_desc = I40E_TX_DESC(tx_ring, 0);
3726 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003727 /* unmap skb header data */
3728 dma_unmap_single(tx_ring->dev,
3729 dma_unmap_addr(tx_buf, dma),
3730 dma_unmap_len(tx_buf, len),
3731 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003732 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3733 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003734
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003735 tx_buf->raw_buf = NULL;
3736 tx_buf->tx_flags = 0;
3737 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003738 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003739 tx_desc->buffer_addr = 0;
3740 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003741
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003742 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003743 tx_buf++;
3744 tx_desc++;
3745 i++;
3746 if (unlikely(!i)) {
3747 i -= tx_ring->count;
3748 tx_buf = tx_ring->tx_bi;
3749 tx_desc = I40E_TX_DESC(tx_ring, 0);
3750 }
3751
3752 /* update budget accounting */
3753 budget--;
3754 } while (likely(budget));
3755
3756 i += tx_ring->count;
3757 tx_ring->next_to_clean = i;
3758
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003759 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003760 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003761
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003762 return budget > 0;
3763}
3764
3765/**
3766 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3767 * @irq: interrupt number
3768 * @data: pointer to a q_vector
3769 **/
3770static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3771{
3772 struct i40e_q_vector *q_vector = data;
3773 struct i40e_vsi *vsi;
3774
3775 if (!q_vector->tx.ring)
3776 return IRQ_HANDLED;
3777
3778 vsi = q_vector->tx.ring->vsi;
3779 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3780
3781 return IRQ_HANDLED;
3782}
3783
3784/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003785 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003786 * @vsi: the VSI being configured
3787 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003788 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003789 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003790static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003791{
Alexander Duyck493fb302013-09-28 07:01:44 +00003792 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003793 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3794 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003795
3796 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003797 tx_ring->next = q_vector->tx.ring;
3798 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003799 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003800
3801 rx_ring->q_vector = q_vector;
3802 rx_ring->next = q_vector->rx.ring;
3803 q_vector->rx.ring = rx_ring;
3804 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003805}
3806
3807/**
3808 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3809 * @vsi: the VSI being configured
3810 *
3811 * This function maps descriptor rings to the queue-specific vectors
3812 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3813 * one vector per queue pair, but on a constrained vector budget, we
3814 * group the queue pairs as "efficiently" as possible.
3815 **/
3816static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3817{
3818 int qp_remaining = vsi->num_queue_pairs;
3819 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003820 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003821 int v_start = 0;
3822 int qp_idx = 0;
3823
3824 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3825 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003826 * It is also important to go through all the vectors available to be
3827 * sure that if we don't use all the vectors, that the remaining vectors
3828 * are cleared. This is especially important when decreasing the
3829 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003830 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003831 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003832 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3833
3834 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3835
3836 q_vector->num_ringpairs = num_ringpairs;
3837
3838 q_vector->rx.count = 0;
3839 q_vector->tx.count = 0;
3840 q_vector->rx.ring = NULL;
3841 q_vector->tx.ring = NULL;
3842
3843 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003844 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003845 qp_idx++;
3846 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003847 }
3848 }
3849}
3850
3851/**
3852 * i40e_vsi_request_irq - Request IRQ from the OS
3853 * @vsi: the VSI being configured
3854 * @basename: name for the vector
3855 **/
3856static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3857{
3858 struct i40e_pf *pf = vsi->back;
3859 int err;
3860
3861 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3862 err = i40e_vsi_request_irq_msix(vsi, basename);
3863 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3864 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003865 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003866 else
3867 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003868 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003869
3870 if (err)
3871 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3872
3873 return err;
3874}
3875
3876#ifdef CONFIG_NET_POLL_CONTROLLER
3877/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003878 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003879 * @netdev: network interface device structure
3880 *
3881 * This is used by netconsole to send skbs without having to re-enable
3882 * interrupts. It's not called while the normal interrupt routine is executing.
3883 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003884#ifdef I40E_FCOE
3885void i40e_netpoll(struct net_device *netdev)
3886#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003887static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003888#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003889{
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
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003991 if (hw->revision_id == 0)
3992 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003993 return ret;
3994}
3995
3996/**
3997 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3998 * @pf: the PF being configured
3999 * @pf_q: the PF queue
4000 * @enable: enable or disable state of the queue
4001 *
4002 * This routine will wait for the given Rx queue of the PF to reach the
4003 * enabled or disabled state.
4004 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4005 * multiple retries; else will return 0 in case of success.
4006 **/
4007static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4008{
4009 int i;
4010 u32 rx_reg;
4011
4012 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4013 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4014 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4015 break;
4016
Neerav Parikhf98a2002014-09-13 07:40:44 +00004017 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004018 }
4019 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4020 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004021
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004022 return 0;
4023}
4024
4025/**
4026 * i40e_vsi_control_rx - Start or stop a VSI's rings
4027 * @vsi: the VSI being configured
4028 * @enable: start or stop the rings
4029 **/
4030static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4031{
4032 struct i40e_pf *pf = vsi->back;
4033 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00004034 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004035 u32 rx_reg;
4036
4037 pf_q = vsi->base_queue;
4038 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08004039 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004040 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004041 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4042 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4043 break;
4044 usleep_range(1000, 2000);
4045 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004046
Catherine Sullivan7c122002014-03-14 07:32:29 +00004047 /* Skip if the queue is already in the requested state */
4048 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4049 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004050
4051 /* turn on/off the queue */
4052 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08004053 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004054 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08004055 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004056 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004057 /* No waiting for the Tx queue to disable */
4058 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4059 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004060
4061 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004062 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4063 if (ret) {
4064 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004065 "VSI seid %d Rx ring %d %sable timeout\n",
4066 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004067 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004068 }
4069 }
4070
Neerav Parikh23527302014-06-03 23:50:15 +00004071 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004072}
4073
4074/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004075 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004076 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004077 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004078int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004079{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004080 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004081
4082 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004083 ret = i40e_vsi_control_rx(vsi, true);
4084 if (ret)
4085 return ret;
4086 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004087
4088 return ret;
4089}
4090
4091/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004092 * i40e_vsi_stop_rings - Stop a VSI's rings
4093 * @vsi: the VSI being configured
4094 **/
4095void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4096{
4097 /* do rx first for enable and last for disable
4098 * Ignore return value, we need to shutdown whatever we can
4099 */
4100 i40e_vsi_control_tx(vsi, false);
4101 i40e_vsi_control_rx(vsi, false);
4102}
4103
4104/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004105 * i40e_vsi_free_irq - Free the irq association with the OS
4106 * @vsi: the VSI being configured
4107 **/
4108static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4109{
4110 struct i40e_pf *pf = vsi->back;
4111 struct i40e_hw *hw = &pf->hw;
4112 int base = vsi->base_vector;
4113 u32 val, qp;
4114 int i;
4115
4116 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4117 if (!vsi->q_vectors)
4118 return;
4119
Shannon Nelson63741842014-04-23 04:50:16 +00004120 if (!vsi->irqs_ready)
4121 return;
4122
4123 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004124 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004125 int irq_num;
4126 u16 vector;
4127
4128 vector = i + base;
4129 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004130
4131 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004132 if (!vsi->q_vectors[i] ||
4133 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004134 continue;
4135
Alan Brady96db7762016-09-14 16:24:38 -07004136 /* clear the affinity notifier in the IRQ descriptor */
4137 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004138 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004139 irq_set_affinity_hint(irq_num, NULL);
4140 synchronize_irq(irq_num);
4141 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004142
4143 /* Tear down the interrupt queue link list
4144 *
4145 * We know that they come in pairs and always
4146 * the Rx first, then the Tx. To clear the
4147 * link list, stick the EOL value into the
4148 * next_q field of the registers.
4149 */
4150 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4151 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4152 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4153 val |= I40E_QUEUE_END_OF_LIST
4154 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4155 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4156
4157 while (qp != I40E_QUEUE_END_OF_LIST) {
4158 u32 next;
4159
4160 val = rd32(hw, I40E_QINT_RQCTL(qp));
4161
4162 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4163 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4164 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4165 I40E_QINT_RQCTL_INTEVENT_MASK);
4166
4167 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4168 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4169
4170 wr32(hw, I40E_QINT_RQCTL(qp), val);
4171
4172 val = rd32(hw, I40E_QINT_TQCTL(qp));
4173
4174 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4175 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4176
4177 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4178 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4179 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4180 I40E_QINT_TQCTL_INTEVENT_MASK);
4181
4182 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4183 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4184
4185 wr32(hw, I40E_QINT_TQCTL(qp), val);
4186 qp = next;
4187 }
4188 }
4189 } else {
4190 free_irq(pf->pdev->irq, pf);
4191
4192 val = rd32(hw, I40E_PFINT_LNKLST0);
4193 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4194 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4195 val |= I40E_QUEUE_END_OF_LIST
4196 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4197 wr32(hw, I40E_PFINT_LNKLST0, val);
4198
4199 val = rd32(hw, I40E_QINT_RQCTL(qp));
4200 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4201 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4202 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4203 I40E_QINT_RQCTL_INTEVENT_MASK);
4204
4205 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4206 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4207
4208 wr32(hw, I40E_QINT_RQCTL(qp), val);
4209
4210 val = rd32(hw, I40E_QINT_TQCTL(qp));
4211
4212 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4213 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4214 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4215 I40E_QINT_TQCTL_INTEVENT_MASK);
4216
4217 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4218 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4219
4220 wr32(hw, I40E_QINT_TQCTL(qp), val);
4221 }
4222}
4223
4224/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004225 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4226 * @vsi: the VSI being configured
4227 * @v_idx: Index of vector to be freed
4228 *
4229 * This function frees the memory allocated to the q_vector. In addition if
4230 * NAPI is enabled it will delete any references to the NAPI struct prior
4231 * to freeing the q_vector.
4232 **/
4233static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4234{
4235 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004236 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004237
4238 if (!q_vector)
4239 return;
4240
4241 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004242 i40e_for_each_ring(ring, q_vector->tx)
4243 ring->q_vector = NULL;
4244
4245 i40e_for_each_ring(ring, q_vector->rx)
4246 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004247
4248 /* only VSI w/ an associated netdev is set up w/ NAPI */
4249 if (vsi->netdev)
4250 netif_napi_del(&q_vector->napi);
4251
4252 vsi->q_vectors[v_idx] = NULL;
4253
4254 kfree_rcu(q_vector, rcu);
4255}
4256
4257/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004258 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4259 * @vsi: the VSI being un-configured
4260 *
4261 * This frees the memory allocated to the q_vectors and
4262 * deletes references to the NAPI struct.
4263 **/
4264static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4265{
4266 int v_idx;
4267
Alexander Duyck493fb302013-09-28 07:01:44 +00004268 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4269 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004270}
4271
4272/**
4273 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4274 * @pf: board private structure
4275 **/
4276static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4277{
4278 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4279 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4280 pci_disable_msix(pf->pdev);
4281 kfree(pf->msix_entries);
4282 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004283 kfree(pf->irq_pile);
4284 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004285 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4286 pci_disable_msi(pf->pdev);
4287 }
4288 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4289}
4290
4291/**
4292 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4293 * @pf: board private structure
4294 *
4295 * We go through and clear interrupt specific resources and reset the structure
4296 * to pre-load conditions
4297 **/
4298static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4299{
4300 int i;
4301
Shannon Nelsone1477582015-02-21 06:44:33 +00004302 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004303 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004304 synchronize_irq(pf->msix_entries[0].vector);
4305 free_irq(pf->msix_entries[0].vector, pf);
4306 }
4307
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004308 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4309 I40E_IWARP_IRQ_PILE_ID);
4310
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004311 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004312 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004313 if (pf->vsi[i])
4314 i40e_vsi_free_q_vectors(pf->vsi[i]);
4315 i40e_reset_interrupt_capability(pf);
4316}
4317
4318/**
4319 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4320 * @vsi: the VSI being configured
4321 **/
4322static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4323{
4324 int q_idx;
4325
4326 if (!vsi->netdev)
4327 return;
4328
4329 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004330 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004331}
4332
4333/**
4334 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4335 * @vsi: the VSI being configured
4336 **/
4337static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4338{
4339 int q_idx;
4340
4341 if (!vsi->netdev)
4342 return;
4343
4344 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004345 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004346}
4347
4348/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004349 * i40e_vsi_close - Shut down a VSI
4350 * @vsi: the vsi to be quelled
4351 **/
4352static void i40e_vsi_close(struct i40e_vsi *vsi)
4353{
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004354 bool reset = false;
4355
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004356 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4357 i40e_down(vsi);
4358 i40e_vsi_free_irq(vsi);
4359 i40e_vsi_free_tx_resources(vsi);
4360 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004361 vsi->current_netdev_flags = 0;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004362 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4363 reset = true;
4364 i40e_notify_client_of_netdev_close(vsi, reset);
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004365}
4366
4367/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004368 * i40e_quiesce_vsi - Pause a given VSI
4369 * @vsi: the VSI being paused
4370 **/
4371static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4372{
4373 if (test_bit(__I40E_DOWN, &vsi->state))
4374 return;
4375
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004376 /* No need to disable FCoE VSI when Tx suspended */
4377 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4378 vsi->type == I40E_VSI_FCOE) {
4379 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004380 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004381 return;
4382 }
4383
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004384 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004385 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004386 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004387 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004388 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004389}
4390
4391/**
4392 * i40e_unquiesce_vsi - Resume a given VSI
4393 * @vsi: the VSI being resumed
4394 **/
4395static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4396{
4397 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4398 return;
4399
4400 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4401 if (vsi->netdev && netif_running(vsi->netdev))
4402 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4403 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004404 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004405}
4406
4407/**
4408 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4409 * @pf: the PF
4410 **/
4411static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4412{
4413 int v;
4414
Mitch Williams505682c2014-05-20 08:01:37 +00004415 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004416 if (pf->vsi[v])
4417 i40e_quiesce_vsi(pf->vsi[v]);
4418 }
4419}
4420
4421/**
4422 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4423 * @pf: the PF
4424 **/
4425static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4426{
4427 int v;
4428
Mitch Williams505682c2014-05-20 08:01:37 +00004429 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004430 if (pf->vsi[v])
4431 i40e_unquiesce_vsi(pf->vsi[v]);
4432 }
4433}
4434
Neerav Parikh69129dc2014-11-12 00:18:46 +00004435#ifdef CONFIG_I40E_DCB
4436/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004437 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004438 * @vsi: the VSI being configured
4439 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004440 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004441 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004442static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004443{
4444 struct i40e_pf *pf = vsi->back;
4445 int i, pf_q, ret;
4446
4447 pf_q = vsi->base_queue;
4448 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4449 /* Check and wait for the disable status of the queue */
4450 ret = i40e_pf_txq_wait(pf, pf_q, false);
4451 if (ret) {
4452 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004453 "VSI seid %d Tx ring %d disable timeout\n",
4454 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004455 return ret;
4456 }
4457 }
4458
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004459 pf_q = vsi->base_queue;
4460 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4461 /* Check and wait for the disable status of the queue */
4462 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4463 if (ret) {
4464 dev_info(&pf->pdev->dev,
4465 "VSI seid %d Rx ring %d disable timeout\n",
4466 vsi->seid, pf_q);
4467 return ret;
4468 }
4469 }
4470
Neerav Parikh69129dc2014-11-12 00:18:46 +00004471 return 0;
4472}
4473
4474/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004475 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004476 * @pf: the PF
4477 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004478 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004479 * VSIs that are managed by this PF.
4480 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004481static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004482{
4483 int v, ret = 0;
4484
4485 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004486 /* No need to wait for FCoE VSI queues */
4487 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004488 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004489 if (ret)
4490 break;
4491 }
4492 }
4493
4494 return ret;
4495}
4496
4497#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004498
4499/**
4500 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4501 * @q_idx: TX queue number
4502 * @vsi: Pointer to VSI struct
4503 *
4504 * This function checks specified queue for given VSI. Detects hung condition.
4505 * Sets hung bit since it is two step process. Before next run of service task
4506 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4507 * hung condition remain unchanged and during subsequent run, this function
4508 * issues SW interrupt to recover from hung condition.
4509 **/
4510static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4511{
4512 struct i40e_ring *tx_ring = NULL;
4513 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004514 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004515 int i;
4516
4517 pf = vsi->back;
4518
4519 /* now that we have an index, find the tx_ring struct */
4520 for (i = 0; i < vsi->num_queue_pairs; i++) {
4521 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4522 if (q_idx == vsi->tx_rings[i]->queue_index) {
4523 tx_ring = vsi->tx_rings[i];
4524 break;
4525 }
4526 }
4527 }
4528
4529 if (!tx_ring)
4530 return;
4531
4532 /* Read interrupt register */
4533 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4534 val = rd32(&pf->hw,
4535 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4536 tx_ring->vsi->base_vector - 1));
4537 else
4538 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4539
4540 head = i40e_get_head(tx_ring);
4541
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004542 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004543
Kiran Patil9c6c1252015-11-06 15:26:02 -08004544 /* HW is done executing descriptors, updated HEAD write back,
4545 * but SW hasn't processed those descriptors. If interrupt is
4546 * not generated from this point ON, it could result into
4547 * dev_watchdog detecting timeout on those netdev_queue,
4548 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004549 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004550 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004551 /* NAPI Poll didn't run and clear since it was set */
4552 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4553 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004554 netdev_info(vsi->netdev, "VSI_seid %d, Hung TX queue %d, tx_pending_hw: %d, NTC:0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x\n",
4555 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004556 tx_ring->next_to_clean, head,
4557 tx_ring->next_to_use,
4558 readl(tx_ring->tail));
4559 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4560 vsi->seid, q_idx, val);
4561 i40e_force_wb(vsi, tx_ring->q_vector);
4562 } else {
4563 /* First Chance - detected possible hung */
4564 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4565 &tx_ring->q_vector->hung_detected);
4566 }
4567 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004568
4569 /* This is the case where we have interrupts missing,
4570 * so the tx_pending in HW will most likely be 0, but we
4571 * will have tx_pending in SW since the WB happened but the
4572 * interrupt got lost.
4573 */
4574 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4575 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
4576 if (napi_reschedule(&tx_ring->q_vector->napi))
4577 tx_ring->tx_stats.tx_lost_interrupt++;
4578 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004579}
4580
4581/**
4582 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4583 * @pf: pointer to PF struct
4584 *
4585 * LAN VSI has netdev and netdev has TX queues. This function is to check
4586 * each of those TX queues if they are hung, trigger recovery by issuing
4587 * SW interrupt.
4588 **/
4589static void i40e_detect_recover_hung(struct i40e_pf *pf)
4590{
4591 struct net_device *netdev;
4592 struct i40e_vsi *vsi;
4593 int i;
4594
4595 /* Only for LAN VSI */
4596 vsi = pf->vsi[pf->lan_vsi];
4597
4598 if (!vsi)
4599 return;
4600
4601 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4602 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4603 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4604 return;
4605
4606 /* Make sure type is MAIN VSI */
4607 if (vsi->type != I40E_VSI_MAIN)
4608 return;
4609
4610 netdev = vsi->netdev;
4611 if (!netdev)
4612 return;
4613
4614 /* Bail out if netif_carrier is not OK */
4615 if (!netif_carrier_ok(netdev))
4616 return;
4617
4618 /* Go thru' TX queues for netdev */
4619 for (i = 0; i < netdev->num_tx_queues; i++) {
4620 struct netdev_queue *q;
4621
4622 q = netdev_get_tx_queue(netdev, i);
4623 if (q)
4624 i40e_detect_recover_hung_queue(i, vsi);
4625 }
4626}
4627
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004628/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004629 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004630 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004631 *
4632 * Get TC map for ISCSI PF type that will include iSCSI TC
4633 * and LAN TC.
4634 **/
4635static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4636{
4637 struct i40e_dcb_app_priority_table app;
4638 struct i40e_hw *hw = &pf->hw;
4639 u8 enabled_tc = 1; /* TC0 is always enabled */
4640 u8 tc, i;
4641 /* Get the iSCSI APP TLV */
4642 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4643
4644 for (i = 0; i < dcbcfg->numapps; i++) {
4645 app = dcbcfg->app[i];
4646 if (app.selector == I40E_APP_SEL_TCPIP &&
4647 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4648 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004649 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004650 break;
4651 }
4652 }
4653
4654 return enabled_tc;
4655}
4656
4657/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004658 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4659 * @dcbcfg: the corresponding DCBx configuration structure
4660 *
4661 * Return the number of TCs from given DCBx configuration
4662 **/
4663static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4664{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004665 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004666 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004667 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004668
4669 /* Scan the ETS Config Priority Table to find
4670 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004671 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004672 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004673 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4674 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4675
4676 /* Now scan the bitmask to check for
4677 * contiguous TCs starting with TC0
4678 */
4679 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4680 if (num_tc & BIT(i)) {
4681 if (!tc_unused) {
4682 ret++;
4683 } else {
4684 pr_err("Non-contiguous TC - Disabling DCB\n");
4685 return 1;
4686 }
4687 } else {
4688 tc_unused = 1;
4689 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004690 }
4691
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004692 /* There is always at least TC0 */
4693 if (!ret)
4694 ret = 1;
4695
4696 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004697}
4698
4699/**
4700 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4701 * @dcbcfg: the corresponding DCBx configuration structure
4702 *
4703 * Query the current DCB configuration and return the number of
4704 * traffic classes enabled from the given DCBX config
4705 **/
4706static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4707{
4708 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4709 u8 enabled_tc = 1;
4710 u8 i;
4711
4712 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004713 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004714
4715 return enabled_tc;
4716}
4717
4718/**
4719 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4720 * @pf: PF being queried
4721 *
4722 * Return number of traffic classes enabled for the given PF
4723 **/
4724static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4725{
4726 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004727 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004728 u8 num_tc = 0;
4729 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4730
4731 /* If DCB is not enabled then always in single TC */
4732 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4733 return 1;
4734
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004735 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004736 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4737 return i40e_dcb_get_num_tc(dcbcfg);
4738
4739 /* MFP mode return count of enabled TCs for this PF */
4740 if (pf->hw.func_caps.iscsi)
4741 enabled_tc = i40e_get_iscsi_tc_map(pf);
4742 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004743 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004744
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004745 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004746 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004747 num_tc++;
4748 }
4749 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004750}
4751
4752/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004753 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4754 * @pf: PF being queried
4755 *
4756 * Return a bitmap for enabled traffic classes for this PF.
4757 **/
4758static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4759{
4760 /* If DCB is not enabled for this PF then just return default TC */
4761 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004762 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004763
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004764 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004765 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4766 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4767
Neerav Parikhfc51de92015-02-24 06:58:53 +00004768 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004769 if (pf->hw.func_caps.iscsi)
4770 return i40e_get_iscsi_tc_map(pf);
4771 else
David Ertmanea6acb72016-09-20 07:10:50 -07004772 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004773}
4774
4775/**
4776 * i40e_vsi_get_bw_info - Query VSI BW Information
4777 * @vsi: the VSI being queried
4778 *
4779 * Returns 0 on success, negative value on failure
4780 **/
4781static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4782{
4783 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4784 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4785 struct i40e_pf *pf = vsi->back;
4786 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004787 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004788 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004789 int i;
4790
4791 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004792 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4793 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004794 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004795 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4796 i40e_stat_str(&pf->hw, ret),
4797 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004798 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004799 }
4800
4801 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004802 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4803 NULL);
4804 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004805 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004806 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4807 i40e_stat_str(&pf->hw, ret),
4808 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004809 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004810 }
4811
4812 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4813 dev_info(&pf->pdev->dev,
4814 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4815 bw_config.tc_valid_bits,
4816 bw_ets_config.tc_valid_bits);
4817 /* Still continuing */
4818 }
4819
4820 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4821 vsi->bw_max_quanta = bw_config.max_bw;
4822 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4823 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4824 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4825 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4826 vsi->bw_ets_limit_credits[i] =
4827 le16_to_cpu(bw_ets_config.credits[i]);
4828 /* 3 bits out of 4 for each TC */
4829 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4830 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004831
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004832 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004833}
4834
4835/**
4836 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4837 * @vsi: the VSI being configured
4838 * @enabled_tc: TC bitmap
4839 * @bw_credits: BW shared credits per TC
4840 *
4841 * Returns 0 on success, negative value on failure
4842 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004843static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004844 u8 *bw_share)
4845{
4846 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004847 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004848 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004849
4850 bw_data.tc_valid_bits = enabled_tc;
4851 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4852 bw_data.tc_bw_credits[i] = bw_share[i];
4853
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004854 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4855 NULL);
4856 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004857 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004858 "AQ command Config VSI BW allocation per TC failed = %d\n",
4859 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004860 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004861 }
4862
4863 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4864 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4865
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004866 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004867}
4868
4869/**
4870 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4871 * @vsi: the VSI being configured
4872 * @enabled_tc: TC map to be enabled
4873 *
4874 **/
4875static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4876{
4877 struct net_device *netdev = vsi->netdev;
4878 struct i40e_pf *pf = vsi->back;
4879 struct i40e_hw *hw = &pf->hw;
4880 u8 netdev_tc = 0;
4881 int i;
4882 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4883
4884 if (!netdev)
4885 return;
4886
4887 if (!enabled_tc) {
4888 netdev_reset_tc(netdev);
4889 return;
4890 }
4891
4892 /* Set up actual enabled TCs on the VSI */
4893 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4894 return;
4895
4896 /* set per TC queues for the VSI */
4897 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4898 /* Only set TC queues for enabled tcs
4899 *
4900 * e.g. For a VSI that has TC0 and TC3 enabled the
4901 * enabled_tc bitmap would be 0x00001001; the driver
4902 * will set the numtc for netdev as 2 that will be
4903 * referenced by the netdev layer as TC 0 and 1.
4904 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004905 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004906 netdev_set_tc_queue(netdev,
4907 vsi->tc_config.tc_info[i].netdev_tc,
4908 vsi->tc_config.tc_info[i].qcount,
4909 vsi->tc_config.tc_info[i].qoffset);
4910 }
4911
4912 /* Assign UP2TC map for the VSI */
4913 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4914 /* Get the actual TC# for the UP */
4915 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4916 /* Get the mapped netdev TC# for the UP */
4917 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4918 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4919 }
4920}
4921
4922/**
4923 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4924 * @vsi: the VSI being configured
4925 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4926 **/
4927static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4928 struct i40e_vsi_context *ctxt)
4929{
4930 /* copy just the sections touched not the entire info
4931 * since not all sections are valid as returned by
4932 * update vsi params
4933 */
4934 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4935 memcpy(&vsi->info.queue_mapping,
4936 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4937 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4938 sizeof(vsi->info.tc_mapping));
4939}
4940
4941/**
4942 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4943 * @vsi: VSI to be configured
4944 * @enabled_tc: TC bitmap
4945 *
4946 * This configures a particular VSI for TCs that are mapped to the
4947 * given TC bitmap. It uses default bandwidth share for TCs across
4948 * VSIs to configure TC for a particular VSI.
4949 *
4950 * NOTE:
4951 * It is expected that the VSI queues have been quisced before calling
4952 * this function.
4953 **/
4954static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4955{
4956 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4957 struct i40e_vsi_context ctxt;
4958 int ret = 0;
4959 int i;
4960
4961 /* Check if enabled_tc is same as existing or new TCs */
4962 if (vsi->tc_config.enabled_tc == enabled_tc)
4963 return ret;
4964
4965 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4966 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004967 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004968 bw_share[i] = 1;
4969 }
4970
4971 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4972 if (ret) {
4973 dev_info(&vsi->back->pdev->dev,
4974 "Failed configuring TC map %d for VSI %d\n",
4975 enabled_tc, vsi->seid);
4976 goto out;
4977 }
4978
4979 /* Update Queue Pairs Mapping for currently enabled UPs */
4980 ctxt.seid = vsi->seid;
4981 ctxt.pf_num = vsi->back->hw.pf_id;
4982 ctxt.vf_num = 0;
4983 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004984 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004985 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4986
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004987 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4988 ctxt.info.valid_sections |=
4989 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4990 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4991 }
4992
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004993 /* Update the VSI after updating the VSI queue-mapping information */
4994 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4995 if (ret) {
4996 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004997 "Update vsi tc config failed, err %s aq_err %s\n",
4998 i40e_stat_str(&vsi->back->hw, ret),
4999 i40e_aq_str(&vsi->back->hw,
5000 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005001 goto out;
5002 }
5003 /* update the local VSI info with updated queue map */
5004 i40e_vsi_update_queue_map(vsi, &ctxt);
5005 vsi->info.valid_sections = 0;
5006
5007 /* Update current VSI BW information */
5008 ret = i40e_vsi_get_bw_info(vsi);
5009 if (ret) {
5010 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005011 "Failed updating vsi bw info, err %s aq_err %s\n",
5012 i40e_stat_str(&vsi->back->hw, ret),
5013 i40e_aq_str(&vsi->back->hw,
5014 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005015 goto out;
5016 }
5017
5018 /* Update the netdev TC setup */
5019 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5020out:
5021 return ret;
5022}
5023
5024/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005025 * i40e_veb_config_tc - Configure TCs for given VEB
5026 * @veb: given VEB
5027 * @enabled_tc: TC bitmap
5028 *
5029 * Configures given TC bitmap for VEB (switching) element
5030 **/
5031int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5032{
5033 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5034 struct i40e_pf *pf = veb->pf;
5035 int ret = 0;
5036 int i;
5037
5038 /* No TCs or already enabled TCs just return */
5039 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5040 return ret;
5041
5042 bw_data.tc_valid_bits = enabled_tc;
5043 /* bw_data.absolute_credits is not set (relative) */
5044
5045 /* Enable ETS TCs with equal BW Share for now */
5046 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005047 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005048 bw_data.tc_bw_share_credits[i] = 1;
5049 }
5050
5051 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5052 &bw_data, NULL);
5053 if (ret) {
5054 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005055 "VEB bw config failed, err %s aq_err %s\n",
5056 i40e_stat_str(&pf->hw, ret),
5057 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005058 goto out;
5059 }
5060
5061 /* Update the BW information */
5062 ret = i40e_veb_get_bw_info(veb);
5063 if (ret) {
5064 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005065 "Failed getting veb bw config, err %s aq_err %s\n",
5066 i40e_stat_str(&pf->hw, ret),
5067 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005068 }
5069
5070out:
5071 return ret;
5072}
5073
5074#ifdef CONFIG_I40E_DCB
5075/**
5076 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5077 * @pf: PF struct
5078 *
5079 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5080 * the caller would've quiesce all the VSIs before calling
5081 * this function
5082 **/
5083static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5084{
5085 u8 tc_map = 0;
5086 int ret;
5087 u8 v;
5088
5089 /* Enable the TCs available on PF to all VEBs */
5090 tc_map = i40e_pf_get_tc_map(pf);
5091 for (v = 0; v < I40E_MAX_VEB; v++) {
5092 if (!pf->veb[v])
5093 continue;
5094 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5095 if (ret) {
5096 dev_info(&pf->pdev->dev,
5097 "Failed configuring TC for VEB seid=%d\n",
5098 pf->veb[v]->seid);
5099 /* Will try to configure as many components */
5100 }
5101 }
5102
5103 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005104 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005105 if (!pf->vsi[v])
5106 continue;
5107
5108 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07005109#ifdef I40E_FCOE
5110 * - For FCoE VSI only enable the TC configured
5111 * as per the APP TLV
5112#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005113 * - For all others keep them at TC0 for now
5114 */
5115 if (v == pf->lan_vsi)
5116 tc_map = i40e_pf_get_tc_map(pf);
5117 else
David Ertmanea6acb72016-09-20 07:10:50 -07005118 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Vasu Dev38e00432014-08-01 13:27:03 -07005119#ifdef I40E_FCOE
5120 if (pf->vsi[v]->type == I40E_VSI_FCOE)
5121 tc_map = i40e_get_fcoe_tc_map(pf);
5122#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005123
5124 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5125 if (ret) {
5126 dev_info(&pf->pdev->dev,
5127 "Failed configuring TC for VSI seid=%d\n",
5128 pf->vsi[v]->seid);
5129 /* Will try to configure as many components */
5130 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005131 /* Re-configure VSI vectors based on updated TC map */
5132 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005133 if (pf->vsi[v]->netdev)
5134 i40e_dcbnl_set_all(pf->vsi[v]);
5135 }
5136 }
5137}
5138
5139/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005140 * i40e_resume_port_tx - Resume port Tx
5141 * @pf: PF struct
5142 *
5143 * Resume a port's Tx and issue a PF reset in case of failure to
5144 * resume.
5145 **/
5146static int i40e_resume_port_tx(struct i40e_pf *pf)
5147{
5148 struct i40e_hw *hw = &pf->hw;
5149 int ret;
5150
5151 ret = i40e_aq_resume_port_tx(hw, NULL);
5152 if (ret) {
5153 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005154 "Resume Port Tx failed, err %s aq_err %s\n",
5155 i40e_stat_str(&pf->hw, ret),
5156 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005157 /* Schedule PF reset to recover */
5158 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5159 i40e_service_event_schedule(pf);
5160 }
5161
5162 return ret;
5163}
5164
5165/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005166 * i40e_init_pf_dcb - Initialize DCB configuration
5167 * @pf: PF being configured
5168 *
5169 * Query the current DCB configuration and cache it
5170 * in the hardware structure
5171 **/
5172static int i40e_init_pf_dcb(struct i40e_pf *pf)
5173{
5174 struct i40e_hw *hw = &pf->hw;
5175 int err = 0;
5176
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005177 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005178 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005179 goto out;
5180
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005181 /* Get the initial DCB configuration */
5182 err = i40e_init_dcb(hw);
5183 if (!err) {
5184 /* Device/Function is not DCBX capable */
5185 if ((!hw->func_caps.dcb) ||
5186 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5187 dev_info(&pf->pdev->dev,
5188 "DCBX offload is not supported or is disabled for this PF.\n");
5189
5190 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5191 goto out;
5192
5193 } else {
5194 /* When status is not DISABLED then DCBX in FW */
5195 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5196 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005197
5198 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005199 /* Enable DCB tagging only when more than one TC
5200 * or explicitly disable if only one TC
5201 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005202 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5203 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005204 else
5205 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005206 dev_dbg(&pf->pdev->dev,
5207 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005208 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005209 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005210 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005211 "Query for DCB configuration failed, err %s aq_err %s\n",
5212 i40e_stat_str(&pf->hw, err),
5213 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005214 }
5215
5216out:
5217 return err;
5218}
5219#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005220#define SPEED_SIZE 14
5221#define FC_SIZE 8
5222/**
5223 * i40e_print_link_message - print link up or down
5224 * @vsi: the VSI for which link needs a message
5225 */
Matt Jaredc156f852015-08-27 11:42:39 -04005226void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005227{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005228 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005229 char *speed = "Unknown";
5230 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005231
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005232 new_speed = vsi->back->hw.phy.link_info.link_speed;
5233
5234 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005235 return;
5236 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005237 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005238 if (!isup) {
5239 netdev_info(vsi->netdev, "NIC Link is Down\n");
5240 return;
5241 }
5242
Greg Rose148c2d82014-12-11 07:06:27 +00005243 /* Warn user if link speed on NPAR enabled partition is not at
5244 * least 10GB
5245 */
5246 if (vsi->back->hw.func_caps.npar_enable &&
5247 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5248 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5249 netdev_warn(vsi->netdev,
5250 "The partition detected link speed that is less than 10Gbps\n");
5251
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005252 switch (vsi->back->hw.phy.link_info.link_speed) {
5253 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005254 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005255 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005256 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005257 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005258 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005259 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005260 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005261 break;
5262 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005263 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005264 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005265 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005266 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005267 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005268 default:
5269 break;
5270 }
5271
5272 switch (vsi->back->hw.fc.current_mode) {
5273 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005274 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005275 break;
5276 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005277 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005278 break;
5279 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005280 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005281 break;
5282 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005283 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005284 break;
5285 }
5286
Shannon Nelsona9165492015-09-03 17:19:00 -04005287 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005288 speed, fc);
5289}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005290
5291/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005292 * i40e_up_complete - Finish the last steps of bringing up a connection
5293 * @vsi: the VSI being configured
5294 **/
5295static int i40e_up_complete(struct i40e_vsi *vsi)
5296{
5297 struct i40e_pf *pf = vsi->back;
5298 int err;
5299
5300 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5301 i40e_vsi_configure_msix(vsi);
5302 else
5303 i40e_configure_msi_and_legacy(vsi);
5304
5305 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005306 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005307 if (err)
5308 return err;
5309
5310 clear_bit(__I40E_DOWN, &vsi->state);
5311 i40e_napi_enable_all(vsi);
5312 i40e_vsi_enable_irq(vsi);
5313
5314 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5315 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005316 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005317 netif_tx_start_all_queues(vsi->netdev);
5318 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005319 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005320 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005321 /* need to check for qualified module here*/
5322 if ((pf->hw.phy.link_info.link_info &
5323 I40E_AQ_MEDIA_AVAILABLE) &&
5324 (!(pf->hw.phy.link_info.an_info &
5325 I40E_AQ_QUALIFIED_MODULE)))
5326 netdev_err(vsi->netdev,
5327 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005328 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005329
5330 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005331 if (vsi->type == I40E_VSI_FDIR) {
5332 /* reset fd counters */
5333 pf->fd_add_err = pf->fd_atr_cnt = 0;
5334 if (pf->fd_tcp_rule > 0) {
Jacob Keller234dc4e2016-09-06 18:05:09 -07005335 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005336 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5337 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005338 pf->fd_tcp_rule = 0;
5339 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005340 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005341 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005342
5343 /* On the next run of the service_task, notify any clients of the new
5344 * opened netdev
5345 */
5346 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005347 i40e_service_event_schedule(pf);
5348
5349 return 0;
5350}
5351
5352/**
5353 * i40e_vsi_reinit_locked - Reset the VSI
5354 * @vsi: the VSI being configured
5355 *
5356 * Rebuild the ring structs after some configuration
5357 * has changed, e.g. MTU size.
5358 **/
5359static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5360{
5361 struct i40e_pf *pf = vsi->back;
5362
5363 WARN_ON(in_interrupt());
5364 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5365 usleep_range(1000, 2000);
5366 i40e_down(vsi);
5367
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005368 i40e_up(vsi);
5369 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5370}
5371
5372/**
5373 * i40e_up - Bring the connection back up after being down
5374 * @vsi: the VSI being configured
5375 **/
5376int i40e_up(struct i40e_vsi *vsi)
5377{
5378 int err;
5379
5380 err = i40e_vsi_configure(vsi);
5381 if (!err)
5382 err = i40e_up_complete(vsi);
5383
5384 return err;
5385}
5386
5387/**
5388 * i40e_down - Shutdown the connection processing
5389 * @vsi: the VSI being stopped
5390 **/
5391void i40e_down(struct i40e_vsi *vsi)
5392{
5393 int i;
5394
5395 /* It is assumed that the caller of this function
5396 * sets the vsi->state __I40E_DOWN bit.
5397 */
5398 if (vsi->netdev) {
5399 netif_carrier_off(vsi->netdev);
5400 netif_tx_disable(vsi->netdev);
5401 }
5402 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005403 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005404 i40e_napi_disable_all(vsi);
5405
5406 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005407 i40e_clean_tx_ring(vsi->tx_rings[i]);
5408 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005409 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005410
5411 i40e_notify_client_of_netdev_close(vsi, false);
5412
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005413}
5414
5415/**
5416 * i40e_setup_tc - configure multiple traffic classes
5417 * @netdev: net device to configure
5418 * @tc: number of traffic classes to enable
5419 **/
5420static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5421{
5422 struct i40e_netdev_priv *np = netdev_priv(netdev);
5423 struct i40e_vsi *vsi = np->vsi;
5424 struct i40e_pf *pf = vsi->back;
5425 u8 enabled_tc = 0;
5426 int ret = -EINVAL;
5427 int i;
5428
5429 /* Check if DCB enabled to continue */
5430 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5431 netdev_info(netdev, "DCB is not enabled for adapter\n");
5432 goto exit;
5433 }
5434
5435 /* Check if MFP enabled */
5436 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5437 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5438 goto exit;
5439 }
5440
5441 /* Check whether tc count is within enabled limit */
5442 if (tc > i40e_pf_get_num_tc(pf)) {
5443 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5444 goto exit;
5445 }
5446
5447 /* Generate TC map for number of tc requested */
5448 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005449 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005450
5451 /* Requesting same TC configuration as already enabled */
5452 if (enabled_tc == vsi->tc_config.enabled_tc)
5453 return 0;
5454
5455 /* Quiesce VSI queues */
5456 i40e_quiesce_vsi(vsi);
5457
5458 /* Configure VSI for enabled TCs */
5459 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5460 if (ret) {
5461 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5462 vsi->seid);
5463 goto exit;
5464 }
5465
5466 /* Unquiesce VSI */
5467 i40e_unquiesce_vsi(vsi);
5468
5469exit:
5470 return ret;
5471}
5472
John Fastabende4c67342016-02-16 21:16:15 -08005473#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005474int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5475 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005476#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005477static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5478 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005479#endif
5480{
John Fastabend16e5cc62016-02-16 21:16:43 -08005481 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005482 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005483 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005484}
5485
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005486/**
5487 * i40e_open - Called when a network interface is made active
5488 * @netdev: network interface device structure
5489 *
5490 * The open entry point is called when a network interface is made
5491 * active by the system (IFF_UP). At this point all resources needed
5492 * for transmit and receive operations are allocated, the interrupt
5493 * handler is registered with the OS, the netdev watchdog subtask is
5494 * enabled, and the stack is notified that the interface is ready.
5495 *
5496 * Returns 0 on success, negative value on failure
5497 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005498int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005499{
5500 struct i40e_netdev_priv *np = netdev_priv(netdev);
5501 struct i40e_vsi *vsi = np->vsi;
5502 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005503 int err;
5504
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005505 /* disallow open during test or if eeprom is broken */
5506 if (test_bit(__I40E_TESTING, &pf->state) ||
5507 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005508 return -EBUSY;
5509
5510 netif_carrier_off(netdev);
5511
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005512 err = i40e_vsi_open(vsi);
5513 if (err)
5514 return err;
5515
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005516 /* configure global TSO hardware offload settings */
5517 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5518 TCP_FLAG_FIN) >> 16);
5519 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5520 TCP_FLAG_FIN |
5521 TCP_FLAG_CWR) >> 16);
5522 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5523
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005524 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005525
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005526 return 0;
5527}
5528
5529/**
5530 * i40e_vsi_open -
5531 * @vsi: the VSI to open
5532 *
5533 * Finish initialization of the VSI.
5534 *
5535 * Returns 0 on success, negative value on failure
5536 **/
5537int i40e_vsi_open(struct i40e_vsi *vsi)
5538{
5539 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005540 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005541 int err;
5542
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005543 /* allocate descriptors */
5544 err = i40e_vsi_setup_tx_resources(vsi);
5545 if (err)
5546 goto err_setup_tx;
5547 err = i40e_vsi_setup_rx_resources(vsi);
5548 if (err)
5549 goto err_setup_rx;
5550
5551 err = i40e_vsi_configure(vsi);
5552 if (err)
5553 goto err_setup_rx;
5554
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005555 if (vsi->netdev) {
5556 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5557 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5558 err = i40e_vsi_request_irq(vsi, int_name);
5559 if (err)
5560 goto err_setup_rx;
5561
5562 /* Notify the stack of the actual queue counts. */
5563 err = netif_set_real_num_tx_queues(vsi->netdev,
5564 vsi->num_queue_pairs);
5565 if (err)
5566 goto err_set_queues;
5567
5568 err = netif_set_real_num_rx_queues(vsi->netdev,
5569 vsi->num_queue_pairs);
5570 if (err)
5571 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005572
5573 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005574 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005575 dev_driver_string(&pf->pdev->dev),
5576 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005577 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005578
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005579 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005580 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005581 goto err_setup_rx;
5582 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005583
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005584 err = i40e_up_complete(vsi);
5585 if (err)
5586 goto err_up_complete;
5587
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005588 return 0;
5589
5590err_up_complete:
5591 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005592err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005593 i40e_vsi_free_irq(vsi);
5594err_setup_rx:
5595 i40e_vsi_free_rx_resources(vsi);
5596err_setup_tx:
5597 i40e_vsi_free_tx_resources(vsi);
5598 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005599 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005600
5601 return err;
5602}
5603
5604/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005605 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005606 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005607 *
5608 * This function destroys the hlist where all the Flow Director
5609 * filters were saved.
5610 **/
5611static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5612{
5613 struct i40e_fdir_filter *filter;
5614 struct hlist_node *node2;
5615
5616 hlist_for_each_entry_safe(filter, node2,
5617 &pf->fdir_filter_list, fdir_node) {
5618 hlist_del(&filter->fdir_node);
5619 kfree(filter);
5620 }
5621 pf->fdir_pf_active_filters = 0;
5622}
5623
5624/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005625 * i40e_close - Disables a network interface
5626 * @netdev: network interface device structure
5627 *
5628 * The close entry point is called when an interface is de-activated
5629 * by the OS. The hardware is still under the driver's control, but
5630 * this netdev interface is disabled.
5631 *
5632 * Returns 0, this is not allowed to fail
5633 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005634int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005635{
5636 struct i40e_netdev_priv *np = netdev_priv(netdev);
5637 struct i40e_vsi *vsi = np->vsi;
5638
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005639 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005640
5641 return 0;
5642}
5643
5644/**
5645 * i40e_do_reset - Start a PF or Core Reset sequence
5646 * @pf: board private structure
5647 * @reset_flags: which reset is requested
5648 *
5649 * The essential difference in resets is that the PF Reset
5650 * doesn't clear the packet buffers, doesn't reset the PE
5651 * firmware, and doesn't bother the other PFs on the chip.
5652 **/
5653void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5654{
5655 u32 val;
5656
5657 WARN_ON(in_interrupt());
5658
Mitch Williams263fc482014-04-23 04:50:11 +00005659
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005660 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005661 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005662
5663 /* Request a Global Reset
5664 *
5665 * This will start the chip's countdown to the actual full
5666 * chip reset event, and a warning interrupt to be sent
5667 * to all PFs, including the requestor. Our handler
5668 * for the warning interrupt will deal with the shutdown
5669 * and recovery of the switch setup.
5670 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005671 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005672 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5673 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5674 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5675
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005676 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005677
5678 /* Request a Core Reset
5679 *
5680 * Same as Global Reset, except does *not* include the MAC/PHY
5681 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005682 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005683 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5684 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5685 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5686 i40e_flush(&pf->hw);
5687
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005688 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005689
5690 /* Request a PF Reset
5691 *
5692 * Resets only the PF-specific registers
5693 *
5694 * This goes directly to the tear-down and rebuild of
5695 * the switch, since we need to do all the recovery as
5696 * for the Core Reset.
5697 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005698 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005699 i40e_handle_reset_warning(pf);
5700
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005701 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005702 int v;
5703
5704 /* Find the VSI(s) that requested a re-init */
5705 dev_info(&pf->pdev->dev,
5706 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005707 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005708 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005709
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005710 if (vsi != NULL &&
5711 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5712 i40e_vsi_reinit_locked(pf->vsi[v]);
5713 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5714 }
5715 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005716 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005717 int v;
5718
5719 /* Find the VSI(s) that needs to be brought down */
5720 dev_info(&pf->pdev->dev, "VSI down requested\n");
5721 for (v = 0; v < pf->num_alloc_vsi; v++) {
5722 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005723
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005724 if (vsi != NULL &&
5725 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5726 set_bit(__I40E_DOWN, &vsi->state);
5727 i40e_down(vsi);
5728 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5729 }
5730 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005731 } else {
5732 dev_info(&pf->pdev->dev,
5733 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005734 }
5735}
5736
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005737#ifdef CONFIG_I40E_DCB
5738/**
5739 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5740 * @pf: board private structure
5741 * @old_cfg: current DCB config
5742 * @new_cfg: new DCB config
5743 **/
5744bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5745 struct i40e_dcbx_config *old_cfg,
5746 struct i40e_dcbx_config *new_cfg)
5747{
5748 bool need_reconfig = false;
5749
5750 /* Check if ETS configuration has changed */
5751 if (memcmp(&new_cfg->etscfg,
5752 &old_cfg->etscfg,
5753 sizeof(new_cfg->etscfg))) {
5754 /* If Priority Table has changed reconfig is needed */
5755 if (memcmp(&new_cfg->etscfg.prioritytable,
5756 &old_cfg->etscfg.prioritytable,
5757 sizeof(new_cfg->etscfg.prioritytable))) {
5758 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005759 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005760 }
5761
5762 if (memcmp(&new_cfg->etscfg.tcbwtable,
5763 &old_cfg->etscfg.tcbwtable,
5764 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005765 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005766
5767 if (memcmp(&new_cfg->etscfg.tsatable,
5768 &old_cfg->etscfg.tsatable,
5769 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005770 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005771 }
5772
5773 /* Check if PFC configuration has changed */
5774 if (memcmp(&new_cfg->pfc,
5775 &old_cfg->pfc,
5776 sizeof(new_cfg->pfc))) {
5777 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005778 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005779 }
5780
5781 /* Check if APP Table has changed */
5782 if (memcmp(&new_cfg->app,
5783 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005784 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005785 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005786 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005787 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005788
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005789 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005790 return need_reconfig;
5791}
5792
5793/**
5794 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5795 * @pf: board private structure
5796 * @e: event info posted on ARQ
5797 **/
5798static int i40e_handle_lldp_event(struct i40e_pf *pf,
5799 struct i40e_arq_event_info *e)
5800{
5801 struct i40e_aqc_lldp_get_mib *mib =
5802 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5803 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005804 struct i40e_dcbx_config tmp_dcbx_cfg;
5805 bool need_reconfig = false;
5806 int ret = 0;
5807 u8 type;
5808
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005809 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005810 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005811 return ret;
5812
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005813 /* Ignore if event is not for Nearest Bridge */
5814 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5815 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005816 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005817 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5818 return ret;
5819
5820 /* Check MIB Type and return if event for Remote MIB update */
5821 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005822 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005823 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005824 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5825 /* Update the remote cached instance and return */
5826 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5827 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5828 &hw->remote_dcbx_config);
5829 goto exit;
5830 }
5831
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005832 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005833 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005834
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005835 /* Reset the old DCBx configuration data */
5836 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005837 /* Get updated DCBX data from firmware */
5838 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005839 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005840 dev_info(&pf->pdev->dev,
5841 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5842 i40e_stat_str(&pf->hw, ret),
5843 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005844 goto exit;
5845 }
5846
5847 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005848 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5849 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005850 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005851 goto exit;
5852 }
5853
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005854 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5855 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005856
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005857 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005858
5859 if (!need_reconfig)
5860 goto exit;
5861
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005862 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005863 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005864 pf->flags |= I40E_FLAG_DCB_ENABLED;
5865 else
5866 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5867
Neerav Parikh69129dc2014-11-12 00:18:46 +00005868 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005869 /* Reconfiguration needed quiesce all VSIs */
5870 i40e_pf_quiesce_all_vsi(pf);
5871
5872 /* Changes in configuration update VEB/VSI */
5873 i40e_dcb_reconfigure(pf);
5874
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005875 ret = i40e_resume_port_tx(pf);
5876
Neerav Parikh69129dc2014-11-12 00:18:46 +00005877 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005878 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005879 if (ret)
5880 goto exit;
5881
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005882 /* Wait for the PF's queues to be disabled */
5883 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005884 if (ret) {
5885 /* Schedule PF reset to recover */
5886 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5887 i40e_service_event_schedule(pf);
5888 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005889 i40e_pf_unquiesce_all_vsi(pf);
Neerav Parikh85a1aab2016-06-07 09:14:55 -07005890 /* Notify the client for the DCB changes */
5891 i40e_notify_client_of_l2_param_changes(pf->vsi[pf->lan_vsi]);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005892 }
5893
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005894exit:
5895 return ret;
5896}
5897#endif /* CONFIG_I40E_DCB */
5898
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005899/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005900 * i40e_do_reset_safe - Protected reset path for userland calls.
5901 * @pf: board private structure
5902 * @reset_flags: which reset is requested
5903 *
5904 **/
5905void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5906{
5907 rtnl_lock();
5908 i40e_do_reset(pf, reset_flags);
5909 rtnl_unlock();
5910}
5911
5912/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005913 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5914 * @pf: board private structure
5915 * @e: event info posted on ARQ
5916 *
5917 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5918 * and VF queues
5919 **/
5920static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5921 struct i40e_arq_event_info *e)
5922{
5923 struct i40e_aqc_lan_overflow *data =
5924 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5925 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5926 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5927 struct i40e_hw *hw = &pf->hw;
5928 struct i40e_vf *vf;
5929 u16 vf_id;
5930
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005931 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5932 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005933
5934 /* Queue belongs to VF, find the VF and issue VF reset */
5935 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5936 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5937 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5938 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5939 vf_id -= hw->func_caps.vf_base_id;
5940 vf = &pf->vf[vf_id];
5941 i40e_vc_notify_vf_reset(vf);
5942 /* Allow VF to process pending reset notification */
5943 msleep(20);
5944 i40e_reset_vf(vf, false);
5945 }
5946}
5947
5948/**
5949 * i40e_service_event_complete - Finish up the service event
5950 * @pf: board private structure
5951 **/
5952static void i40e_service_event_complete(struct i40e_pf *pf)
5953{
Shannon Nelsonb875f992015-10-21 19:47:03 -04005954 WARN_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005955
5956 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005957 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005958 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5959}
5960
5961/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005962 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5963 * @pf: board private structure
5964 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005965u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005966{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005967 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005968
5969 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5970 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5971 return fcnt_prog;
5972}
5973
5974/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005975 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005976 * @pf: board private structure
5977 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005978u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005979{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005980 u32 val, fcnt_prog;
5981
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005982 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5983 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5984 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5985 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5986 return fcnt_prog;
5987}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005988
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005989/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005990 * i40e_get_global_fd_count - Get total FD filters programmed on device
5991 * @pf: board private structure
5992 **/
5993u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5994{
5995 u32 val, fcnt_prog;
5996
5997 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5998 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5999 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6000 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6001 return fcnt_prog;
6002}
6003
6004/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006005 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6006 * @pf: board private structure
6007 **/
6008void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6009{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006010 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006011 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006012 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006013
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006014 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
6015 return;
6016
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006017 /* Check if, FD SB or ATR was auto disabled and if there is enough room
6018 * to re-enable
6019 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006020 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006021 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006022 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6023 (pf->fd_add_err == 0) ||
6024 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006025 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
6026 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
6027 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006028 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6029 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 +00006030 }
6031 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006032
6033 /* Wait for some more space to be available to turn on ATR. We also
6034 * must check that no existing ntuple rules for TCP are in effect
6035 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006036 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
6037 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Kellera3417d22016-09-06 18:05:10 -07006038 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED) &&
6039 (pf->fd_tcp_rule == 0)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006040 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006041 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07006042 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 +00006043 }
6044 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006045
6046 /* if hw had a problem adding a filter, delete it */
6047 if (pf->fd_inv > 0) {
6048 hlist_for_each_entry_safe(filter, node,
6049 &pf->fdir_filter_list, fdir_node) {
6050 if (filter->fd_id == pf->fd_inv) {
6051 hlist_del(&filter->fdir_node);
6052 kfree(filter);
6053 pf->fdir_pf_active_filters--;
6054 }
6055 }
6056 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006057}
6058
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006059#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006060#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006061/**
6062 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6063 * @pf: board private structure
6064 **/
6065static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6066{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006067 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006068 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006069 bool disable_atr = false;
6070 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006071 int reg;
6072
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006073 if (!time_after(jiffies, pf->fd_flush_timestamp +
6074 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6075 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006076
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006077 /* If the flush is happening too quick and we have mostly SB rules we
6078 * should not re-enable ATR for some time.
6079 */
6080 min_flush_time = pf->fd_flush_timestamp +
6081 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6082 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006083
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006084 if (!(time_after(jiffies, min_flush_time)) &&
6085 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6086 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6087 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6088 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006089 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006090
6091 pf->fd_flush_timestamp = jiffies;
Jacob Keller234dc4e2016-09-06 18:05:09 -07006092 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006093 /* flush all filters */
6094 wr32(&pf->hw, I40E_PFQF_CTL_1,
6095 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6096 i40e_flush(&pf->hw);
6097 pf->fd_flush_cnt++;
6098 pf->fd_add_err = 0;
6099 do {
6100 /* Check FD flush status every 5-6msec */
6101 usleep_range(5000, 6000);
6102 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6103 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6104 break;
6105 } while (flush_wait_retry--);
6106 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6107 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6108 } else {
6109 /* replay sideband filters */
6110 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
6111 if (!disable_atr)
Jacob Keller234dc4e2016-09-06 18:05:09 -07006112 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006113 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6114 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6115 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6116 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006117}
6118
6119/**
6120 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6121 * @pf: board private structure
6122 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006123u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006124{
6125 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6126}
6127
6128/* We can see up to 256 filter programming desc in transit if the filters are
6129 * being applied really fast; before we see the first
6130 * filter miss error on Rx queue 0. Accumulating enough error messages before
6131 * reacting will make sure we don't cause flush too often.
6132 */
6133#define I40E_MAX_FD_PROGRAM_ERROR 256
6134
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006135/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006136 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6137 * @pf: board private structure
6138 **/
6139static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6140{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006141
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006142 /* if interface is down do nothing */
6143 if (test_bit(__I40E_DOWN, &pf->state))
6144 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006145
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006146 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006147 i40e_fdir_flush_and_replay(pf);
6148
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006149 i40e_fdir_check_and_reenable(pf);
6150
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006151}
6152
6153/**
6154 * i40e_vsi_link_event - notify VSI of a link event
6155 * @vsi: vsi to be notified
6156 * @link_up: link up or down
6157 **/
6158static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6159{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006160 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006161 return;
6162
6163 switch (vsi->type) {
6164 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006165#ifdef I40E_FCOE
6166 case I40E_VSI_FCOE:
6167#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006168 if (!vsi->netdev || !vsi->netdev_registered)
6169 break;
6170
6171 if (link_up) {
6172 netif_carrier_on(vsi->netdev);
6173 netif_tx_wake_all_queues(vsi->netdev);
6174 } else {
6175 netif_carrier_off(vsi->netdev);
6176 netif_tx_stop_all_queues(vsi->netdev);
6177 }
6178 break;
6179
6180 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006181 case I40E_VSI_VMDQ2:
6182 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006183 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006184 case I40E_VSI_MIRROR:
6185 default:
6186 /* there is no notification for other VSIs */
6187 break;
6188 }
6189}
6190
6191/**
6192 * i40e_veb_link_event - notify elements on the veb of a link event
6193 * @veb: veb to be notified
6194 * @link_up: link up or down
6195 **/
6196static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6197{
6198 struct i40e_pf *pf;
6199 int i;
6200
6201 if (!veb || !veb->pf)
6202 return;
6203 pf = veb->pf;
6204
6205 /* depth first... */
6206 for (i = 0; i < I40E_MAX_VEB; i++)
6207 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6208 i40e_veb_link_event(pf->veb[i], link_up);
6209
6210 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006211 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006212 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6213 i40e_vsi_link_event(pf->vsi[i], link_up);
6214}
6215
6216/**
6217 * i40e_link_event - Update netif_carrier status
6218 * @pf: board private structure
6219 **/
6220static void i40e_link_event(struct i40e_pf *pf)
6221{
Mitch Williams320684c2014-10-17 03:14:43 +00006222 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006223 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006224 i40e_status status;
6225 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006226
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006227 /* save off old link status information */
6228 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6229
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006230 /* set this to force the get_link_status call to refresh state */
6231 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006232
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006233 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006234
6235 status = i40e_get_link_status(&pf->hw, &new_link);
6236 if (status) {
6237 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
Shannon Nelson9ac77262015-08-27 11:42:40 -04006288 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6289 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006290
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006291 /* Update the stats for active netdevs so the network stack
6292 * can look at updated numbers whenever it cares to
6293 */
Mitch Williams505682c2014-05-20 08:01:37 +00006294 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006295 if (pf->vsi[i] && pf->vsi[i]->netdev)
6296 i40e_update_stats(pf->vsi[i]);
6297
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006298 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6299 /* Update the stats for the active switching components */
6300 for (i = 0; i < I40E_MAX_VEB; i++)
6301 if (pf->veb[i])
6302 i40e_update_veb_stats(pf->veb[i]);
6303 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006304
6305 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006306}
6307
6308/**
6309 * i40e_reset_subtask - Set up for resetting the device and driver
6310 * @pf: board private structure
6311 **/
6312static void i40e_reset_subtask(struct i40e_pf *pf)
6313{
6314 u32 reset_flags = 0;
6315
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006316 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006317 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)) {
6342 i40e_handle_reset_warning(pf);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006343 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006344 }
6345
6346 /* If we're already down or resetting, just bail */
6347 if (reset_flags &&
6348 !test_bit(__I40E_DOWN, &pf->state) &&
6349 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6350 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006351
6352unlock:
6353 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006354}
6355
6356/**
6357 * i40e_handle_link_event - Handle link event
6358 * @pf: board private structure
6359 * @e: event info posted on ARQ
6360 **/
6361static void i40e_handle_link_event(struct i40e_pf *pf,
6362 struct i40e_arq_event_info *e)
6363{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006364 struct i40e_aqc_get_link_status *status =
6365 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006366
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006367 /* Do a new status request to re-enable LSE reporting
6368 * and load new status information into the hw struct
6369 * This completely ignores any state information
6370 * in the ARQ event info, instead choosing to always
6371 * issue the AQ update link status command.
6372 */
6373 i40e_link_event(pf);
6374
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006375 /* check for unqualified module, if link is down */
6376 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6377 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6378 (!(status->link_info & I40E_AQ_LINK_UP)))
6379 dev_err(&pf->pdev->dev,
6380 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006381}
6382
6383/**
6384 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6385 * @pf: board private structure
6386 **/
6387static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6388{
6389 struct i40e_arq_event_info event;
6390 struct i40e_hw *hw = &pf->hw;
6391 u16 pending, i = 0;
6392 i40e_status ret;
6393 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006394 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006395 u32 val;
6396
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006397 /* Do not run clean AQ when PF reset fails */
6398 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6399 return;
6400
Shannon Nelson86df2422014-05-20 08:01:35 +00006401 /* check for error indications */
6402 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6403 oldval = val;
6404 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006405 if (hw->debug_mask & I40E_DEBUG_AQ)
6406 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006407 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6408 }
6409 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006410 if (hw->debug_mask & I40E_DEBUG_AQ)
6411 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006412 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006413 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006414 }
6415 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006416 if (hw->debug_mask & I40E_DEBUG_AQ)
6417 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006418 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6419 }
6420 if (oldval != val)
6421 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6422
6423 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6424 oldval = val;
6425 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006426 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6427 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006428 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6429 }
6430 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006431 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6432 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006433 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6434 }
6435 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006436 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6437 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006438 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6439 }
6440 if (oldval != val)
6441 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6442
Mitch Williams1001dc32014-11-11 20:02:19 +00006443 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6444 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006445 if (!event.msg_buf)
6446 return;
6447
6448 do {
6449 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006450 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006451 break;
Mitch Williams56497972014-06-04 08:45:18 +00006452 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006453 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6454 break;
6455 }
6456
6457 opcode = le16_to_cpu(event.desc.opcode);
6458 switch (opcode) {
6459
6460 case i40e_aqc_opc_get_link_status:
6461 i40e_handle_link_event(pf, &event);
6462 break;
6463 case i40e_aqc_opc_send_msg_to_pf:
6464 ret = i40e_vc_process_vf_msg(pf,
6465 le16_to_cpu(event.desc.retval),
6466 le32_to_cpu(event.desc.cookie_high),
6467 le32_to_cpu(event.desc.cookie_low),
6468 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006469 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006470 break;
6471 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006472 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006473#ifdef CONFIG_I40E_DCB
6474 rtnl_lock();
6475 ret = i40e_handle_lldp_event(pf, &event);
6476 rtnl_unlock();
6477#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006478 break;
6479 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006480 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006481 i40e_handle_lan_overflow_event(pf, &event);
6482 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006483 case i40e_aqc_opc_send_msg_to_peer:
6484 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6485 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006486 case i40e_aqc_opc_nvm_erase:
6487 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006488 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006489 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6490 "ARQ NVM operation 0x%04x completed\n",
6491 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006492 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006493 default:
6494 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006495 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006496 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006497 break;
6498 }
6499 } while (pending && (i++ < pf->adminq_work_limit));
6500
6501 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6502 /* re-enable Admin queue interrupt cause */
6503 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6504 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6505 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6506 i40e_flush(hw);
6507
6508 kfree(event.msg_buf);
6509}
6510
6511/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006512 * i40e_verify_eeprom - make sure eeprom is good to use
6513 * @pf: board private structure
6514 **/
6515static void i40e_verify_eeprom(struct i40e_pf *pf)
6516{
6517 int err;
6518
6519 err = i40e_diag_eeprom_test(&pf->hw);
6520 if (err) {
6521 /* retry in case of garbage read */
6522 err = i40e_diag_eeprom_test(&pf->hw);
6523 if (err) {
6524 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6525 err);
6526 set_bit(__I40E_BAD_EEPROM, &pf->state);
6527 }
6528 }
6529
6530 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6531 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6532 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6533 }
6534}
6535
6536/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006537 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006538 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006539 *
6540 * enable switch loop back or die - no point in a return value
6541 **/
6542static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6543{
6544 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6545 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006546 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006547
6548 ctxt.seid = pf->main_vsi_seid;
6549 ctxt.pf_num = pf->hw.pf_id;
6550 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006551 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6552 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006553 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006554 "couldn't get PF vsi config, err %s aq_err %s\n",
6555 i40e_stat_str(&pf->hw, ret),
6556 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006557 return;
6558 }
6559 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6560 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6561 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6562
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006563 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6564 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006565 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006566 "update vsi switch failed, err %s aq_err %s\n",
6567 i40e_stat_str(&pf->hw, ret),
6568 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006569 }
6570}
6571
6572/**
6573 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006574 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006575 *
6576 * disable switch loop back or die - no point in a return value
6577 **/
6578static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6579{
6580 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6581 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006582 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006583
6584 ctxt.seid = pf->main_vsi_seid;
6585 ctxt.pf_num = pf->hw.pf_id;
6586 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006587 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6588 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006589 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006590 "couldn't get PF vsi config, err %s aq_err %s\n",
6591 i40e_stat_str(&pf->hw, ret),
6592 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006593 return;
6594 }
6595 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6596 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6597 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6598
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006599 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6600 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006601 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006602 "update vsi switch failed, err %s aq_err %s\n",
6603 i40e_stat_str(&pf->hw, ret),
6604 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006605 }
6606}
6607
6608/**
Neerav Parikh51616012015-02-06 08:52:14 +00006609 * i40e_config_bridge_mode - Configure the HW bridge mode
6610 * @veb: pointer to the bridge instance
6611 *
6612 * Configure the loop back mode for the LAN VSI that is downlink to the
6613 * specified HW bridge instance. It is expected this function is called
6614 * when a new HW bridge is instantiated.
6615 **/
6616static void i40e_config_bridge_mode(struct i40e_veb *veb)
6617{
6618 struct i40e_pf *pf = veb->pf;
6619
Shannon Nelson6dec1012015-09-28 14:12:30 -04006620 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6621 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6622 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006623 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6624 i40e_disable_pf_switch_lb(pf);
6625 else
6626 i40e_enable_pf_switch_lb(pf);
6627}
6628
6629/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006630 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6631 * @veb: pointer to the VEB instance
6632 *
6633 * This is a recursive function that first builds the attached VSIs then
6634 * recurses in to build the next layer of VEB. We track the connections
6635 * through our own index numbers because the seid's from the HW could
6636 * change across the reset.
6637 **/
6638static int i40e_reconstitute_veb(struct i40e_veb *veb)
6639{
6640 struct i40e_vsi *ctl_vsi = NULL;
6641 struct i40e_pf *pf = veb->pf;
6642 int v, veb_idx;
6643 int ret;
6644
6645 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006646 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006647 if (pf->vsi[v] &&
6648 pf->vsi[v]->veb_idx == veb->idx &&
6649 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6650 ctl_vsi = pf->vsi[v];
6651 break;
6652 }
6653 }
6654 if (!ctl_vsi) {
6655 dev_info(&pf->pdev->dev,
6656 "missing owner VSI for veb_idx %d\n", veb->idx);
6657 ret = -ENOENT;
6658 goto end_reconstitute;
6659 }
6660 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6661 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6662 ret = i40e_add_vsi(ctl_vsi);
6663 if (ret) {
6664 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006665 "rebuild of veb_idx %d owner VSI failed: %d\n",
6666 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006667 goto end_reconstitute;
6668 }
6669 i40e_vsi_reset_stats(ctl_vsi);
6670
6671 /* create the VEB in the switch and move the VSI onto the VEB */
6672 ret = i40e_add_veb(veb, ctl_vsi);
6673 if (ret)
6674 goto end_reconstitute;
6675
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006676 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6677 veb->bridge_mode = BRIDGE_MODE_VEB;
6678 else
6679 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006680 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006681
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006682 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006683 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006684 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6685 continue;
6686
6687 if (pf->vsi[v]->veb_idx == veb->idx) {
6688 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006689
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006690 vsi->uplink_seid = veb->seid;
6691 ret = i40e_add_vsi(vsi);
6692 if (ret) {
6693 dev_info(&pf->pdev->dev,
6694 "rebuild of vsi_idx %d failed: %d\n",
6695 v, ret);
6696 goto end_reconstitute;
6697 }
6698 i40e_vsi_reset_stats(vsi);
6699 }
6700 }
6701
6702 /* create any VEBs attached to this VEB - RECURSION */
6703 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6704 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6705 pf->veb[veb_idx]->uplink_seid = veb->seid;
6706 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6707 if (ret)
6708 break;
6709 }
6710 }
6711
6712end_reconstitute:
6713 return ret;
6714}
6715
6716/**
6717 * i40e_get_capabilities - get info about the HW
6718 * @pf: the PF struct
6719 **/
6720static int i40e_get_capabilities(struct i40e_pf *pf)
6721{
6722 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6723 u16 data_size;
6724 int buf_len;
6725 int err;
6726
6727 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6728 do {
6729 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6730 if (!cap_buf)
6731 return -ENOMEM;
6732
6733 /* this loads the data into the hw struct for us */
6734 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6735 &data_size,
6736 i40e_aqc_opc_list_func_capabilities,
6737 NULL);
6738 /* data loaded, buffer no longer needed */
6739 kfree(cap_buf);
6740
6741 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6742 /* retry with a larger buffer */
6743 buf_len = data_size;
6744 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6745 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006746 "capability discovery failed, err %s aq_err %s\n",
6747 i40e_stat_str(&pf->hw, err),
6748 i40e_aq_str(&pf->hw,
6749 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006750 return -ENODEV;
6751 }
6752 } while (err);
6753
6754 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6755 dev_info(&pf->pdev->dev,
6756 "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",
6757 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6758 pf->hw.func_caps.num_msix_vectors,
6759 pf->hw.func_caps.num_msix_vectors_vf,
6760 pf->hw.func_caps.fd_filters_guaranteed,
6761 pf->hw.func_caps.fd_filters_best_effort,
6762 pf->hw.func_caps.num_tx_qp,
6763 pf->hw.func_caps.num_vsis);
6764
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006765#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6766 + pf->hw.func_caps.num_vfs)
6767 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6768 dev_info(&pf->pdev->dev,
6769 "got num_vsis %d, setting num_vsis to %d\n",
6770 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6771 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6772 }
6773
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006774 return 0;
6775}
6776
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006777static int i40e_vsi_clear(struct i40e_vsi *vsi);
6778
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006779/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006780 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006781 * @pf: board private structure
6782 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006783static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006784{
6785 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006786
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006787 /* quick workaround for an NVM issue that leaves a critical register
6788 * uninitialized
6789 */
6790 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6791 static const u32 hkey[] = {
6792 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6793 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6794 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6795 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006796 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006797
6798 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6799 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6800 }
6801
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006802 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006803 return;
6804
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006805 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006806 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006807
6808 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006809 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006810 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6811 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006812 if (!vsi) {
6813 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006814 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6815 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006816 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006817 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006818
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006819 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006820}
6821
6822/**
6823 * i40e_fdir_teardown - release the Flow Director resources
6824 * @pf: board private structure
6825 **/
6826static void i40e_fdir_teardown(struct i40e_pf *pf)
6827{
Alexander Duyck4b816442016-10-11 15:26:53 -07006828 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006829
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006830 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07006831 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6832 if (vsi)
6833 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006834}
6835
6836/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006837 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006838 * @pf: board private structure
6839 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006840 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006841 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006842static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006843{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006844 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006845 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006846 u32 v;
6847
6848 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6849 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006850 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006851 if (i40e_check_asq_alive(&pf->hw))
6852 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006853
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006854 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006855
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006856 /* quiesce the VSIs and their queues that are not already DOWN */
6857 i40e_pf_quiesce_all_vsi(pf);
6858
Mitch Williams505682c2014-05-20 08:01:37 +00006859 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006860 if (pf->vsi[v])
6861 pf->vsi[v]->seid = 0;
6862 }
6863
6864 i40e_shutdown_adminq(&pf->hw);
6865
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006866 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006867 if (hw->hmc.hmc_obj) {
6868 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006869 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006870 dev_warn(&pf->pdev->dev,
6871 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006872 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006873}
6874
6875/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006876 * i40e_send_version - update firmware with driver version
6877 * @pf: PF struct
6878 */
6879static void i40e_send_version(struct i40e_pf *pf)
6880{
6881 struct i40e_driver_version dv;
6882
6883 dv.major_version = DRV_VERSION_MAJOR;
6884 dv.minor_version = DRV_VERSION_MINOR;
6885 dv.build_version = DRV_VERSION_BUILD;
6886 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006887 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006888 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6889}
6890
6891/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006892 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006893 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006894 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006895 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006896static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006897{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006898 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006899 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006900 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006901 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006902 u32 v;
6903
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006904 /* Now we wait for GRST to settle out.
6905 * We don't have to delete the VEBs or VSIs from the hw switch
6906 * because the reset will make them disappear.
6907 */
6908 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006909 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006910 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006911 set_bit(__I40E_RESET_FAILED, &pf->state);
6912 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006913 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006914 pf->pfr_count++;
6915
6916 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006917 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006918 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006919
6920 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6921 ret = i40e_init_adminq(&pf->hw);
6922 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006923 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6924 i40e_stat_str(&pf->hw, ret),
6925 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006926 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006927 }
6928
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006929 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006930 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006931 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006932
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006933 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006934 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006935 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006936 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006937
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006938 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6939 hw->func_caps.num_rx_qp,
6940 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6941 if (ret) {
6942 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6943 goto end_core_reset;
6944 }
6945 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6946 if (ret) {
6947 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6948 goto end_core_reset;
6949 }
6950
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006951#ifdef CONFIG_I40E_DCB
6952 ret = i40e_init_pf_dcb(pf);
6953 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006954 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6955 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6956 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006957 }
6958#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006959#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006960 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006961
Vasu Dev38e00432014-08-01 13:27:03 -07006962#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006963 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006964 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006965 if (ret)
6966 goto end_core_reset;
6967
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006968 /* The driver only wants link up/down and module qualification
6969 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006970 */
6971 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006972 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07006973 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006974 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006975 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006976 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6977 i40e_stat_str(&pf->hw, ret),
6978 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006979
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006980 /* make sure our flow control settings are restored */
6981 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6982 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04006983 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6984 i40e_stat_str(&pf->hw, ret),
6985 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006986
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006987 /* Rebuild the VSIs and VEBs that existed before reset.
6988 * They are still in our local switch element arrays, so only
6989 * need to rebuild the switch model in the HW.
6990 *
6991 * If there were VEBs but the reconstitution failed, we'll try
6992 * try to recover minimal use by getting the basic PF VSI working.
6993 */
6994 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006995 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006996 /* find the one VEB connected to the MAC, and find orphans */
6997 for (v = 0; v < I40E_MAX_VEB; v++) {
6998 if (!pf->veb[v])
6999 continue;
7000
7001 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7002 pf->veb[v]->uplink_seid == 0) {
7003 ret = i40e_reconstitute_veb(pf->veb[v]);
7004
7005 if (!ret)
7006 continue;
7007
7008 /* If Main VEB failed, we're in deep doodoo,
7009 * so give up rebuilding the switch and set up
7010 * for minimal rebuild of PF VSI.
7011 * If orphan failed, we'll report the error
7012 * but try to keep going.
7013 */
7014 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7015 dev_info(&pf->pdev->dev,
7016 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7017 ret);
7018 pf->vsi[pf->lan_vsi]->uplink_seid
7019 = pf->mac_seid;
7020 break;
7021 } else if (pf->veb[v]->uplink_seid == 0) {
7022 dev_info(&pf->pdev->dev,
7023 "rebuild of orphan VEB failed: %d\n",
7024 ret);
7025 }
7026 }
7027 }
7028 }
7029
7030 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007031 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007032 /* no VEB, so rebuild only the Main VSI */
7033 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7034 if (ret) {
7035 dev_info(&pf->pdev->dev,
7036 "rebuild of Main VSI failed: %d\n", ret);
7037 goto end_core_reset;
7038 }
7039 }
7040
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007041 /* Reconfigure hardware for allowing smaller MSS in the case
7042 * of TSO, so that we avoid the MDD being fired and causing
7043 * a reset in the case of small MSS+TSO.
7044 */
7045#define I40E_REG_MSS 0x000E64DC
7046#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7047#define I40E_64BYTE_MSS 0x400000
7048 val = rd32(hw, I40E_REG_MSS);
7049 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7050 val &= ~I40E_REG_MSS_MIN_MASK;
7051 val |= I40E_64BYTE_MSS;
7052 wr32(hw, I40E_REG_MSS, val);
7053 }
7054
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08007055 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007056 msleep(75);
7057 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7058 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007059 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7060 i40e_stat_str(&pf->hw, ret),
7061 i40e_aq_str(&pf->hw,
7062 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007063 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007064 /* reinit the misc interrupt */
7065 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7066 ret = i40e_setup_misc_vector(pf);
7067
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007068 /* Add a filter to drop all Flow control frames from any VSI from being
7069 * transmitted. By doing so we stop a malicious VF from sending out
7070 * PAUSE or PFC frames and potentially controlling traffic for other
7071 * PF/VF VSIs.
7072 * The FW can still send Flow control frames if enabled.
7073 */
7074 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7075 pf->main_vsi_seid);
7076
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007077 /* restart the VSIs that were rebuilt and running before the reset */
7078 i40e_pf_unquiesce_all_vsi(pf);
7079
Mitch Williams69f64b22014-02-13 03:48:46 -08007080 if (pf->num_alloc_vfs) {
7081 for (v = 0; v < pf->num_alloc_vfs; v++)
7082 i40e_reset_vf(&pf->vf[v], true);
7083 }
7084
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007085 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007086 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007087
7088end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007089 clear_bit(__I40E_RESET_FAILED, &pf->state);
7090clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007091 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7092}
7093
7094/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007095 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007096 * @pf: board private structure
7097 *
7098 * Close up the VFs and other things in prep for a Core Reset,
7099 * then get ready to rebuild the world.
7100 **/
7101static void i40e_handle_reset_warning(struct i40e_pf *pf)
7102{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007103 i40e_prep_for_reset(pf);
7104 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007105}
7106
7107/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007108 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007109 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007110 *
7111 * Called from the MDD irq handler to identify possibly malicious vfs
7112 **/
7113static void i40e_handle_mdd_event(struct i40e_pf *pf)
7114{
7115 struct i40e_hw *hw = &pf->hw;
7116 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007117 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007118 struct i40e_vf *vf;
7119 u32 reg;
7120 int i;
7121
7122 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7123 return;
7124
7125 /* find what triggered the MDD event */
7126 reg = rd32(hw, I40E_GL_MDET_TX);
7127 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007128 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7129 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007130 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007131 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007132 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007133 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007134 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7135 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7136 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007137 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007138 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 +00007139 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007140 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7141 mdd_detected = true;
7142 }
7143 reg = rd32(hw, I40E_GL_MDET_RX);
7144 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007145 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7146 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007147 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007148 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007149 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7150 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7151 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007152 if (netif_msg_rx_err(pf))
7153 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7154 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007155 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7156 mdd_detected = true;
7157 }
7158
Neerav Parikhdf430b12014-06-04 01:23:15 +00007159 if (mdd_detected) {
7160 reg = rd32(hw, I40E_PF_MDET_TX);
7161 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7162 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007163 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007164 pf_mdd_detected = true;
7165 }
7166 reg = rd32(hw, I40E_PF_MDET_RX);
7167 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7168 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007169 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007170 pf_mdd_detected = true;
7171 }
7172 /* Queue belongs to the PF, initiate a reset */
7173 if (pf_mdd_detected) {
7174 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7175 i40e_service_event_schedule(pf);
7176 }
7177 }
7178
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007179 /* see if one of the VFs needs its hand slapped */
7180 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7181 vf = &(pf->vf[i]);
7182 reg = rd32(hw, I40E_VP_MDET_TX(i));
7183 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7184 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7185 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007186 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7187 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007188 }
7189
7190 reg = rd32(hw, I40E_VP_MDET_RX(i));
7191 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7192 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7193 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007194 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7195 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007196 }
7197
7198 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7199 dev_info(&pf->pdev->dev,
7200 "Too many MDD events on VF %d, disabled\n", i);
7201 dev_info(&pf->pdev->dev,
7202 "Use PF Control I/F to re-enable the VF\n");
7203 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7204 }
7205 }
7206
7207 /* re-enable mdd interrupt cause */
7208 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7209 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7210 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7211 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7212 i40e_flush(hw);
7213}
7214
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007215/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007216 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007217 * @pf: board private structure
7218 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007219static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007220{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007221 struct i40e_hw *hw = &pf->hw;
7222 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007223 __be16 port;
7224 int i;
7225
Singhai, Anjali6a899022015-12-14 12:21:18 -08007226 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007227 return;
7228
Singhai, Anjali6a899022015-12-14 12:21:18 -08007229 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007230
7231 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007232 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7233 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7234 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007235 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007236 ret = i40e_aq_add_udp_tunnel(hw, port,
7237 pf->udp_ports[i].type,
7238 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007239 else
7240 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007241
7242 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007243 dev_dbg(&pf->pdev->dev,
7244 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7245 pf->udp_ports[i].type ? "vxlan" : "geneve",
7246 port ? "add" : "delete",
7247 ntohs(port), i,
7248 i40e_stat_str(&pf->hw, ret),
7249 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007250 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007251 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007252 }
7253 }
7254 }
7255}
7256
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007257/**
7258 * i40e_service_task - Run the driver's async subtasks
7259 * @work: pointer to work_struct containing our data
7260 **/
7261static void i40e_service_task(struct work_struct *work)
7262{
7263 struct i40e_pf *pf = container_of(work,
7264 struct i40e_pf,
7265 service_task);
7266 unsigned long start_time = jiffies;
7267
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007268 /* don't bother with service tasks if a reset is in progress */
7269 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7270 i40e_service_event_complete(pf);
7271 return;
7272 }
7273
Kiran Patilb03a8c12015-09-24 18:13:15 -04007274 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007275 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007276 i40e_reset_subtask(pf);
7277 i40e_handle_mdd_event(pf);
7278 i40e_vc_process_vflr_event(pf);
7279 i40e_watchdog_subtask(pf);
7280 i40e_fdir_reinit_subtask(pf);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007281 i40e_client_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007282 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007283 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007284 i40e_clean_adminq_subtask(pf);
7285
7286 i40e_service_event_complete(pf);
7287
7288 /* If the tasks have taken longer than one timer cycle or there
7289 * is more work to be done, reschedule the service task now
7290 * rather than wait for the timer to tick again.
7291 */
7292 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7293 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7294 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7295 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7296 i40e_service_event_schedule(pf);
7297}
7298
7299/**
7300 * i40e_service_timer - timer callback
7301 * @data: pointer to PF struct
7302 **/
7303static void i40e_service_timer(unsigned long data)
7304{
7305 struct i40e_pf *pf = (struct i40e_pf *)data;
7306
7307 mod_timer(&pf->service_timer,
7308 round_jiffies(jiffies + pf->service_timer_period));
7309 i40e_service_event_schedule(pf);
7310}
7311
7312/**
7313 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7314 * @vsi: the VSI being configured
7315 **/
7316static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7317{
7318 struct i40e_pf *pf = vsi->back;
7319
7320 switch (vsi->type) {
7321 case I40E_VSI_MAIN:
7322 vsi->alloc_queue_pairs = pf->num_lan_qps;
7323 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7324 I40E_REQ_DESCRIPTOR_MULTIPLE);
7325 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7326 vsi->num_q_vectors = pf->num_lan_msix;
7327 else
7328 vsi->num_q_vectors = 1;
7329
7330 break;
7331
7332 case I40E_VSI_FDIR:
7333 vsi->alloc_queue_pairs = 1;
7334 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7335 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007336 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007337 break;
7338
7339 case I40E_VSI_VMDQ2:
7340 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7341 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7342 I40E_REQ_DESCRIPTOR_MULTIPLE);
7343 vsi->num_q_vectors = pf->num_vmdq_msix;
7344 break;
7345
7346 case I40E_VSI_SRIOV:
7347 vsi->alloc_queue_pairs = pf->num_vf_qps;
7348 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7349 I40E_REQ_DESCRIPTOR_MULTIPLE);
7350 break;
7351
Vasu Dev38e00432014-08-01 13:27:03 -07007352#ifdef I40E_FCOE
7353 case I40E_VSI_FCOE:
7354 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7355 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7356 I40E_REQ_DESCRIPTOR_MULTIPLE);
7357 vsi->num_q_vectors = pf->num_fcoe_msix;
7358 break;
7359
7360#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007361 default:
7362 WARN_ON(1);
7363 return -ENODATA;
7364 }
7365
7366 return 0;
7367}
7368
7369/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007370 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7371 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007372 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007373 *
7374 * On error: returns error code (negative)
7375 * On success: returns 0
7376 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007377static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007378{
7379 int size;
7380 int ret = 0;
7381
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007382 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007383 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7384 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7385 if (!vsi->tx_rings)
7386 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007387 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7388
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007389 if (alloc_qvectors) {
7390 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007391 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007392 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7393 if (!vsi->q_vectors) {
7394 ret = -ENOMEM;
7395 goto err_vectors;
7396 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007397 }
7398 return ret;
7399
7400err_vectors:
7401 kfree(vsi->tx_rings);
7402 return ret;
7403}
7404
7405/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007406 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7407 * @pf: board private structure
7408 * @type: type of VSI
7409 *
7410 * On error: returns error code (negative)
7411 * On success: returns vsi index in PF (positive)
7412 **/
7413static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7414{
7415 int ret = -ENODEV;
7416 struct i40e_vsi *vsi;
7417 int vsi_idx;
7418 int i;
7419
7420 /* Need to protect the allocation of the VSIs at the PF level */
7421 mutex_lock(&pf->switch_mutex);
7422
7423 /* VSI list may be fragmented if VSI creation/destruction has
7424 * been happening. We can afford to do a quick scan to look
7425 * for any free VSIs in the list.
7426 *
7427 * find next empty vsi slot, looping back around if necessary
7428 */
7429 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007430 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007431 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007432 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007433 i = 0;
7434 while (i < pf->next_vsi && pf->vsi[i])
7435 i++;
7436 }
7437
Mitch Williams505682c2014-05-20 08:01:37 +00007438 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007439 vsi_idx = i; /* Found one! */
7440 } else {
7441 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007442 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007443 }
7444 pf->next_vsi = ++i;
7445
7446 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7447 if (!vsi) {
7448 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007449 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007450 }
7451 vsi->type = type;
7452 vsi->back = pf;
7453 set_bit(__I40E_DOWN, &vsi->state);
7454 vsi->flags = 0;
7455 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007456 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007457 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7458 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007459 vsi->netdev_registered = false;
7460 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007461 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007462 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007463
Alexander Duyck9f65e152013-09-28 06:00:58 +00007464 ret = i40e_set_num_rings_in_vsi(vsi);
7465 if (ret)
7466 goto err_rings;
7467
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007468 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007469 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007470 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007471
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007472 /* Setup default MSIX irq handler for VSI */
7473 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7474
Kiran Patil21659032015-09-30 14:09:03 -04007475 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007476 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007477 pf->vsi[vsi_idx] = vsi;
7478 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007479 goto unlock_pf;
7480
Alexander Duyck9f65e152013-09-28 06:00:58 +00007481err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007482 pf->next_vsi = i - 1;
7483 kfree(vsi);
7484unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007485 mutex_unlock(&pf->switch_mutex);
7486 return ret;
7487}
7488
7489/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007490 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7491 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007492 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007493 *
7494 * On error: returns error code (negative)
7495 * On success: returns 0
7496 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007497static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007498{
7499 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007500 if (free_qvectors) {
7501 kfree(vsi->q_vectors);
7502 vsi->q_vectors = NULL;
7503 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007504 kfree(vsi->tx_rings);
7505 vsi->tx_rings = NULL;
7506 vsi->rx_rings = NULL;
7507}
7508
7509/**
Helin Zhang28c58692015-10-26 19:44:27 -04007510 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7511 * and lookup table
7512 * @vsi: Pointer to VSI structure
7513 */
7514static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7515{
7516 if (!vsi)
7517 return;
7518
7519 kfree(vsi->rss_hkey_user);
7520 vsi->rss_hkey_user = NULL;
7521
7522 kfree(vsi->rss_lut_user);
7523 vsi->rss_lut_user = NULL;
7524}
7525
7526/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007527 * i40e_vsi_clear - Deallocate the VSI provided
7528 * @vsi: the VSI being un-configured
7529 **/
7530static int i40e_vsi_clear(struct i40e_vsi *vsi)
7531{
7532 struct i40e_pf *pf;
7533
7534 if (!vsi)
7535 return 0;
7536
7537 if (!vsi->back)
7538 goto free_vsi;
7539 pf = vsi->back;
7540
7541 mutex_lock(&pf->switch_mutex);
7542 if (!pf->vsi[vsi->idx]) {
7543 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7544 vsi->idx, vsi->idx, vsi, vsi->type);
7545 goto unlock_vsi;
7546 }
7547
7548 if (pf->vsi[vsi->idx] != vsi) {
7549 dev_err(&pf->pdev->dev,
7550 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7551 pf->vsi[vsi->idx]->idx,
7552 pf->vsi[vsi->idx],
7553 pf->vsi[vsi->idx]->type,
7554 vsi->idx, vsi, vsi->type);
7555 goto unlock_vsi;
7556 }
7557
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007558 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007559 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7560 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7561
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007562 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007563 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007564
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007565 pf->vsi[vsi->idx] = NULL;
7566 if (vsi->idx < pf->next_vsi)
7567 pf->next_vsi = vsi->idx;
7568
7569unlock_vsi:
7570 mutex_unlock(&pf->switch_mutex);
7571free_vsi:
7572 kfree(vsi);
7573
7574 return 0;
7575}
7576
7577/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007578 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7579 * @vsi: the VSI being cleaned
7580 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007581static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007582{
7583 int i;
7584
Greg Rose8e9dca52013-12-18 13:45:53 +00007585 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007586 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007587 kfree_rcu(vsi->tx_rings[i], rcu);
7588 vsi->tx_rings[i] = NULL;
7589 vsi->rx_rings[i] = NULL;
7590 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007591 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007592}
7593
7594/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007595 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7596 * @vsi: the VSI being configured
7597 **/
7598static int i40e_alloc_rings(struct i40e_vsi *vsi)
7599{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007600 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007601 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007602 int i;
7603
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007604 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007605 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007606 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007607 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7608 if (!tx_ring)
7609 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007610
7611 tx_ring->queue_index = i;
7612 tx_ring->reg_idx = vsi->base_queue + i;
7613 tx_ring->ring_active = false;
7614 tx_ring->vsi = vsi;
7615 tx_ring->netdev = vsi->netdev;
7616 tx_ring->dev = &pf->pdev->dev;
7617 tx_ring->count = vsi->num_desc;
7618 tx_ring->size = 0;
7619 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007620 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7621 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007622 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007623 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007624
Alexander Duyck9f65e152013-09-28 06:00:58 +00007625 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007626 rx_ring->queue_index = i;
7627 rx_ring->reg_idx = vsi->base_queue + i;
7628 rx_ring->ring_active = false;
7629 rx_ring->vsi = vsi;
7630 rx_ring->netdev = vsi->netdev;
7631 rx_ring->dev = &pf->pdev->dev;
7632 rx_ring->count = vsi->num_desc;
7633 rx_ring->size = 0;
7634 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007635 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007636 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007637 }
7638
7639 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007640
7641err_out:
7642 i40e_vsi_clear_rings(vsi);
7643 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007644}
7645
7646/**
7647 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7648 * @pf: board private structure
7649 * @vectors: the number of MSI-X vectors to request
7650 *
7651 * Returns the number of vectors reserved, or error
7652 **/
7653static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7654{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007655 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7656 I40E_MIN_MSIX, vectors);
7657 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007658 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007659 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007660 vectors = 0;
7661 }
7662
7663 return vectors;
7664}
7665
7666/**
7667 * i40e_init_msix - Setup the MSIX capability
7668 * @pf: board private structure
7669 *
7670 * Work with the OS to set up the MSIX vectors needed.
7671 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007672 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007673 **/
7674static int i40e_init_msix(struct i40e_pf *pf)
7675{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007676 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007677 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007678 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007679 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007680 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007681
7682 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7683 return -ENODEV;
7684
7685 /* The number of vectors we'll request will be comprised of:
7686 * - Add 1 for "other" cause for Admin Queue events, etc.
7687 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007688 * - Queues being used for RSS.
7689 * We don't need as many as max_rss_size vectors.
7690 * use rss_size instead in the calculation since that
7691 * is governed by number of cpus in the system.
7692 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007693 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007694 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007695#ifdef I40E_FCOE
7696 * - The number of FCOE qps.
7697#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007698 * Once we count this up, try the request.
7699 *
7700 * If we can't get what we want, we'll simplify to nearly nothing
7701 * and try again. If that still fails, we punt.
7702 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007703 vectors_left = hw->func_caps.num_msix_vectors;
7704 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007705
Shannon Nelson1e200e42015-02-27 09:15:24 +00007706 /* reserve one vector for miscellaneous handler */
7707 if (vectors_left) {
7708 v_budget++;
7709 vectors_left--;
7710 }
7711
7712 /* reserve vectors for the main PF traffic queues */
7713 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7714 vectors_left -= pf->num_lan_msix;
7715 v_budget += pf->num_lan_msix;
7716
7717 /* reserve one vector for sideband flow director */
7718 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7719 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007720 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007721 v_budget++;
7722 vectors_left--;
7723 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007724 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007725 }
7726 }
John W Linville83840e42015-01-14 03:06:28 +00007727
Vasu Dev38e00432014-08-01 13:27:03 -07007728#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007729 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007730 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007731 if (!vectors_left)
7732 pf->num_fcoe_msix = 0;
7733 else if (vectors_left >= pf->num_fcoe_qps)
7734 pf->num_fcoe_msix = pf->num_fcoe_qps;
7735 else
7736 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007737 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007738 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007739 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007740
Vasu Dev38e00432014-08-01 13:27:03 -07007741#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007742 /* can we reserve enough for iWARP? */
7743 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007744 iwarp_requested = pf->num_iwarp_msix;
7745
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007746 if (!vectors_left)
7747 pf->num_iwarp_msix = 0;
7748 else if (vectors_left < pf->num_iwarp_msix)
7749 pf->num_iwarp_msix = 1;
7750 v_budget += pf->num_iwarp_msix;
7751 vectors_left -= pf->num_iwarp_msix;
7752 }
7753
Shannon Nelson1e200e42015-02-27 09:15:24 +00007754 /* any vectors left over go for VMDq support */
7755 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7756 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7757 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7758
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007759 if (!vectors_left) {
7760 pf->num_vmdq_msix = 0;
7761 pf->num_vmdq_qps = 0;
7762 } else {
7763 /* if we're short on vectors for what's desired, we limit
7764 * the queues per vmdq. If this is still more than are
7765 * available, the user will need to change the number of
7766 * queues/vectors used by the PF later with the ethtool
7767 * channels command
7768 */
7769 if (vmdq_vecs < vmdq_vecs_wanted)
7770 pf->num_vmdq_qps = 1;
7771 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007772
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007773 v_budget += vmdq_vecs;
7774 vectors_left -= vmdq_vecs;
7775 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007776 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007777
7778 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7779 GFP_KERNEL);
7780 if (!pf->msix_entries)
7781 return -ENOMEM;
7782
7783 for (i = 0; i < v_budget; i++)
7784 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007785 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007786
Shannon Nelson3b444392015-02-26 16:15:57 +00007787 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007788 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7789 kfree(pf->msix_entries);
7790 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007791 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007792 return -ENODEV;
7793
Shannon Nelson3b444392015-02-26 16:15:57 +00007794 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007795 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007796 pf->num_vmdq_vsis = 0;
7797 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007798 pf->num_lan_qps = 1;
7799 pf->num_lan_msix = 1;
7800
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007801 } else if (!vectors_left) {
7802 /* If we have limited resources, we will start with no vectors
7803 * for the special features and then allocate vectors to some
7804 * of these features based on the policy and at the end disable
7805 * the features that did not get any vectors.
7806 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007807 int vec;
7808
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007809 dev_info(&pf->pdev->dev,
7810 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007811 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007812 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007813
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007814 /* Scale vector usage down */
7815 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007816 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007817 pf->num_vmdq_qps = 1;
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007818#ifdef I40E_FCOE
7819 pf->num_fcoe_qps = 0;
7820 pf->num_fcoe_msix = 0;
7821#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007822
7823 /* partition out the remaining vectors */
7824 switch (vec) {
7825 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007826 pf->num_lan_msix = 1;
7827 break;
7828 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007829 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7830 pf->num_lan_msix = 1;
7831 pf->num_iwarp_msix = 1;
7832 } else {
7833 pf->num_lan_msix = 2;
7834 }
Vasu Dev38e00432014-08-01 13:27:03 -07007835#ifdef I40E_FCOE
7836 /* give one vector to FCoE */
7837 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7838 pf->num_lan_msix = 1;
7839 pf->num_fcoe_msix = 1;
7840 }
Vasu Dev38e00432014-08-01 13:27:03 -07007841#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007842 break;
7843 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007844 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7845 pf->num_iwarp_msix = min_t(int, (vec / 3),
7846 iwarp_requested);
7847 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7848 I40E_DEFAULT_NUM_VMDQ_VSI);
7849 } else {
7850 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7851 I40E_DEFAULT_NUM_VMDQ_VSI);
7852 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007853 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7854 pf->num_fdsb_msix = 1;
7855 vec--;
7856 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007857 pf->num_lan_msix = min_t(int,
7858 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7859 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007860 pf->num_lan_qps = pf->num_lan_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007861#ifdef I40E_FCOE
7862 /* give one vector to FCoE */
7863 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7864 pf->num_fcoe_msix = 1;
7865 vec--;
7866 }
7867#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007868 break;
7869 }
7870 }
7871
Stefan Assmannabd97a92016-09-19 13:37:51 +02007872 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7873 (pf->num_fdsb_msix == 0)) {
7874 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7875 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7876 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007877 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7878 (pf->num_vmdq_msix == 0)) {
7879 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7880 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7881 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007882
7883 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7884 (pf->num_iwarp_msix == 0)) {
7885 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7886 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7887 }
Vasu Dev38e00432014-08-01 13:27:03 -07007888#ifdef I40E_FCOE
7889
7890 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7891 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7892 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7893 }
7894#endif
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007895 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7896 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7897 pf->num_lan_msix,
7898 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7899 pf->num_fdsb_msix,
7900 pf->num_iwarp_msix);
7901
Shannon Nelson3b444392015-02-26 16:15:57 +00007902 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007903}
7904
7905/**
Greg Rose90e04072014-03-06 08:59:57 +00007906 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007907 * @vsi: the VSI being configured
7908 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007909 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007910 *
7911 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7912 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007913static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007914{
7915 struct i40e_q_vector *q_vector;
7916
7917 /* allocate q_vector */
7918 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7919 if (!q_vector)
7920 return -ENOMEM;
7921
7922 q_vector->vsi = vsi;
7923 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007924 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7925
Alexander Duyck493fb302013-09-28 07:01:44 +00007926 if (vsi->netdev)
7927 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007928 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007929
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007930 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7931 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7932
Alexander Duyck493fb302013-09-28 07:01:44 +00007933 /* tie q_vector and vsi together */
7934 vsi->q_vectors[v_idx] = q_vector;
7935
7936 return 0;
7937}
7938
7939/**
Greg Rose90e04072014-03-06 08:59:57 +00007940 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007941 * @vsi: the VSI being configured
7942 *
7943 * We allocate one q_vector per queue interrupt. If allocation fails we
7944 * return -ENOMEM.
7945 **/
Greg Rose90e04072014-03-06 08:59:57 +00007946static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007947{
7948 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007949 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007950
7951 /* if not MSIX, give the one vector only to the LAN VSI */
7952 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7953 num_q_vectors = vsi->num_q_vectors;
7954 else if (vsi == pf->vsi[pf->lan_vsi])
7955 num_q_vectors = 1;
7956 else
7957 return -EINVAL;
7958
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007959 current_cpu = cpumask_first(cpu_online_mask);
7960
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007961 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007962 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00007963 if (err)
7964 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007965 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
7966 if (unlikely(current_cpu >= nr_cpu_ids))
7967 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007968 }
7969
7970 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007971
7972err_out:
7973 while (v_idx--)
7974 i40e_free_q_vector(vsi, v_idx);
7975
7976 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007977}
7978
7979/**
7980 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7981 * @pf: board private structure to initialize
7982 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007983static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007984{
Shannon Nelson3b444392015-02-26 16:15:57 +00007985 int vectors = 0;
7986 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007987
7988 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007989 vectors = i40e_init_msix(pf);
7990 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007991 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007992 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007993#ifdef I40E_FCOE
7994 I40E_FLAG_FCOE_ENABLED |
7995#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007996 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007997 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07007998 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007999 I40E_FLAG_SRIOV_ENABLED |
8000 I40E_FLAG_FD_SB_ENABLED |
8001 I40E_FLAG_FD_ATR_ENABLED |
8002 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008003
8004 /* rework the queue expectations without MSIX */
8005 i40e_determine_queue_usage(pf);
8006 }
8007 }
8008
8009 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8010 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008011 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008012 vectors = pci_enable_msi(pf->pdev);
8013 if (vectors < 0) {
8014 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8015 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008016 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8017 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008018 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008019 }
8020
Shannon Nelson958a3e32013-09-28 07:13:28 +00008021 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008022 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008023
Shannon Nelson3b444392015-02-26 16:15:57 +00008024 /* set up vector assignment tracking */
8025 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8026 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008027 if (!pf->irq_pile) {
8028 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8029 return -ENOMEM;
8030 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008031 pf->irq_pile->num_entries = vectors;
8032 pf->irq_pile->search_hint = 0;
8033
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008034 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008035 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008036
8037 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008038}
8039
8040/**
8041 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8042 * @pf: board private structure
8043 *
8044 * This sets up the handler for MSIX 0, which is used to manage the
8045 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8046 * when in MSI or Legacy interrupt mode.
8047 **/
8048static int i40e_setup_misc_vector(struct i40e_pf *pf)
8049{
8050 struct i40e_hw *hw = &pf->hw;
8051 int err = 0;
8052
8053 /* Only request the irq if this is the first time through, and
8054 * not when we're rebuilding after a Reset
8055 */
8056 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
8057 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008058 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008059 if (err) {
8060 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008061 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008062 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008063 return -EFAULT;
8064 }
8065 }
8066
Jacob Kellerab437b52014-12-14 01:55:08 +00008067 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008068
8069 /* associate no queues to the misc vector */
8070 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8071 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8072
8073 i40e_flush(hw);
8074
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008075 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008076
8077 return err;
8078}
8079
8080/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008081 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8082 * @vsi: vsi structure
8083 * @seed: RSS hash seed
8084 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008085static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8086 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008087{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008088 struct i40e_pf *pf = vsi->back;
8089 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008090 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008091
Jacob Keller776b2e12016-07-19 16:23:30 -07008092 if (seed) {
8093 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8094 (struct i40e_aqc_get_set_rss_key_data *)seed;
8095 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8096 if (ret) {
8097 dev_info(&pf->pdev->dev,
8098 "Cannot set RSS key, err %s aq_err %s\n",
8099 i40e_stat_str(hw, ret),
8100 i40e_aq_str(hw, hw->aq.asq_last_status));
8101 return ret;
8102 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008103 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008104 if (lut) {
8105 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008106
Jacob Keller776b2e12016-07-19 16:23:30 -07008107 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8108 if (ret) {
8109 dev_info(&pf->pdev->dev,
8110 "Cannot set RSS lut, err %s aq_err %s\n",
8111 i40e_stat_str(hw, ret),
8112 i40e_aq_str(hw, hw->aq.asq_last_status));
8113 return ret;
8114 }
8115 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008116 return ret;
8117}
8118
8119/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008120 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8121 * @vsi: Pointer to vsi structure
8122 * @seed: Buffter to store the hash keys
8123 * @lut: Buffer to store the lookup table entries
8124 * @lut_size: Size of buffer to store the lookup table entries
8125 *
8126 * Return 0 on success, negative on failure
8127 */
8128static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8129 u8 *lut, u16 lut_size)
8130{
8131 struct i40e_pf *pf = vsi->back;
8132 struct i40e_hw *hw = &pf->hw;
8133 int ret = 0;
8134
8135 if (seed) {
8136 ret = i40e_aq_get_rss_key(hw, vsi->id,
8137 (struct i40e_aqc_get_set_rss_key_data *)seed);
8138 if (ret) {
8139 dev_info(&pf->pdev->dev,
8140 "Cannot get RSS key, err %s aq_err %s\n",
8141 i40e_stat_str(&pf->hw, ret),
8142 i40e_aq_str(&pf->hw,
8143 pf->hw.aq.asq_last_status));
8144 return ret;
8145 }
8146 }
8147
8148 if (lut) {
8149 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8150
8151 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8152 if (ret) {
8153 dev_info(&pf->pdev->dev,
8154 "Cannot get RSS lut, err %s aq_err %s\n",
8155 i40e_stat_str(&pf->hw, ret),
8156 i40e_aq_str(&pf->hw,
8157 pf->hw.aq.asq_last_status));
8158 return ret;
8159 }
8160 }
8161
8162 return ret;
8163}
8164
8165/**
Jacob Keller0582b962016-07-19 16:23:29 -07008166 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8167 * @vsi: VSI structure
8168 **/
8169static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8170{
8171 u8 seed[I40E_HKEY_ARRAY_SIZE];
8172 struct i40e_pf *pf = vsi->back;
8173 u8 *lut;
8174 int ret;
8175
8176 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8177 return 0;
8178
Jacob Keller552b9962016-07-19 16:23:31 -07008179 if (!vsi->rss_size)
8180 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8181 vsi->num_queue_pairs);
8182 if (!vsi->rss_size)
8183 return -EINVAL;
8184
Jacob Keller0582b962016-07-19 16:23:29 -07008185 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8186 if (!lut)
8187 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008188 /* Use the user configured hash keys and lookup table if there is one,
8189 * otherwise use default
8190 */
8191 if (vsi->rss_lut_user)
8192 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8193 else
8194 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8195 if (vsi->rss_hkey_user)
8196 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8197 else
8198 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008199 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8200 kfree(lut);
8201
8202 return ret;
8203}
8204
8205/**
Helin Zhang043dd652015-10-21 19:56:23 -04008206 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008207 * @vsi: Pointer to vsi structure
8208 * @seed: RSS hash seed
8209 * @lut: Lookup table
8210 * @lut_size: Lookup table size
8211 *
8212 * Returns 0 on success, negative on failure
8213 **/
8214static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8215 const u8 *lut, u16 lut_size)
8216{
8217 struct i40e_pf *pf = vsi->back;
8218 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008219 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008220 u8 i;
8221
8222 /* Fill out hash function seed */
8223 if (seed) {
8224 u32 *seed_dw = (u32 *)seed;
8225
Mitch Williamsc4e18682016-04-12 08:30:40 -07008226 if (vsi->type == I40E_VSI_MAIN) {
8227 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8228 i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
8229 seed_dw[i]);
8230 } else if (vsi->type == I40E_VSI_SRIOV) {
8231 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
8232 i40e_write_rx_ctl(hw,
8233 I40E_VFQF_HKEY1(i, vf_id),
8234 seed_dw[i]);
8235 } else {
8236 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8237 }
Helin Zhange69ff812015-10-21 19:56:22 -04008238 }
8239
8240 if (lut) {
8241 u32 *lut_dw = (u32 *)lut;
8242
Mitch Williamsc4e18682016-04-12 08:30:40 -07008243 if (vsi->type == I40E_VSI_MAIN) {
8244 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8245 return -EINVAL;
8246 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8247 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8248 } else if (vsi->type == I40E_VSI_SRIOV) {
8249 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8250 return -EINVAL;
8251 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8252 i40e_write_rx_ctl(hw,
8253 I40E_VFQF_HLUT1(i, vf_id),
8254 lut_dw[i]);
8255 } else {
8256 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8257 }
Helin Zhange69ff812015-10-21 19:56:22 -04008258 }
8259 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008260
8261 return 0;
8262}
8263
8264/**
Helin Zhang043dd652015-10-21 19:56:23 -04008265 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8266 * @vsi: Pointer to VSI structure
8267 * @seed: Buffer to store the keys
8268 * @lut: Buffer to store the lookup table entries
8269 * @lut_size: Size of buffer to store the lookup table entries
8270 *
8271 * Returns 0 on success, negative on failure
8272 */
8273static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8274 u8 *lut, u16 lut_size)
8275{
8276 struct i40e_pf *pf = vsi->back;
8277 struct i40e_hw *hw = &pf->hw;
8278 u16 i;
8279
8280 if (seed) {
8281 u32 *seed_dw = (u32 *)seed;
8282
8283 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008284 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008285 }
8286 if (lut) {
8287 u32 *lut_dw = (u32 *)lut;
8288
8289 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8290 return -EINVAL;
8291 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8292 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8293 }
8294
8295 return 0;
8296}
8297
8298/**
8299 * i40e_config_rss - Configure RSS keys and lut
8300 * @vsi: Pointer to VSI structure
8301 * @seed: RSS hash seed
8302 * @lut: Lookup table
8303 * @lut_size: Lookup table size
8304 *
8305 * Returns 0 on success, negative on failure
8306 */
8307int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8308{
8309 struct i40e_pf *pf = vsi->back;
8310
8311 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8312 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8313 else
8314 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8315}
8316
8317/**
8318 * i40e_get_rss - Get RSS keys and lut
8319 * @vsi: Pointer to VSI structure
8320 * @seed: Buffer to store the keys
8321 * @lut: Buffer to store the lookup table entries
8322 * lut_size: Size of buffer to store the lookup table entries
8323 *
8324 * Returns 0 on success, negative on failure
8325 */
8326int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8327{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008328 struct i40e_pf *pf = vsi->back;
8329
8330 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8331 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8332 else
8333 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008334}
8335
8336/**
Helin Zhange69ff812015-10-21 19:56:22 -04008337 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8338 * @pf: Pointer to board private structure
8339 * @lut: Lookup table
8340 * @rss_table_size: Lookup table size
8341 * @rss_size: Range of queue number for hashing
8342 */
Alan Bradyf1582352016-08-24 11:33:46 -07008343void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8344 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008345{
Helin Zhange69ff812015-10-21 19:56:22 -04008346 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008347
Helin Zhange69ff812015-10-21 19:56:22 -04008348 for (i = 0; i < rss_table_size; i++)
8349 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008350}
8351
8352/**
Helin Zhang043dd652015-10-21 19:56:23 -04008353 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008354 * @pf: board private structure
8355 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008356static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008357{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008358 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008359 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008360 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008361 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008362 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008363 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008364 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008365
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008366 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008367 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8368 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008369 hena |= i40e_pf_get_default_rss_hena(pf);
8370
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008371 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8372 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008373
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008374 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008375 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008376 reg_val = (pf->rss_table_size == 512) ?
8377 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8378 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008379 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008380
Helin Zhang28c58692015-10-26 19:44:27 -04008381 /* Determine the RSS size of the VSI */
8382 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008383 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8384 vsi->num_queue_pairs);
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008385 if (!vsi->rss_size)
8386 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008387
Helin Zhange69ff812015-10-21 19:56:22 -04008388 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8389 if (!lut)
8390 return -ENOMEM;
8391
Helin Zhang28c58692015-10-26 19:44:27 -04008392 /* Use user configured lut if there is one, otherwise use default */
8393 if (vsi->rss_lut_user)
8394 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8395 else
8396 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008397
Helin Zhang28c58692015-10-26 19:44:27 -04008398 /* Use user configured hash key if there is one, otherwise
8399 * use default.
8400 */
8401 if (vsi->rss_hkey_user)
8402 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8403 else
8404 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008405 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008406 kfree(lut);
8407
8408 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008409}
8410
8411/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008412 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8413 * @pf: board private structure
8414 * @queue_count: the requested queue count for rss.
8415 *
8416 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8417 * count which may be different from the requested queue count.
8418 **/
8419int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8420{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008421 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8422 int new_rss_size;
8423
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008424 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8425 return 0;
8426
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008427 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008428
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008429 if (queue_count != vsi->num_queue_pairs) {
8430 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008431 i40e_prep_for_reset(pf);
8432
Helin Zhangacd65442015-10-26 19:44:28 -04008433 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008434
8435 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008436
8437 /* Discard the user configured hash keys and lut, if less
8438 * queues are enabled.
8439 */
8440 if (queue_count < vsi->rss_size) {
8441 i40e_clear_rss_config_user(vsi);
8442 dev_dbg(&pf->pdev->dev,
8443 "discard user configured hash keys and lut\n");
8444 }
8445
8446 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008447 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8448 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008449
Helin Zhang043dd652015-10-21 19:56:23 -04008450 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008451 }
Lihong Yang12815052016-09-27 11:28:48 -07008452 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8453 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008454 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008455}
8456
8457/**
Greg Rosef4492db2015-02-06 08:52:12 +00008458 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8459 * @pf: board private structure
8460 **/
8461i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8462{
8463 i40e_status status;
8464 bool min_valid, max_valid;
8465 u32 max_bw, min_bw;
8466
8467 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8468 &min_valid, &max_valid);
8469
8470 if (!status) {
8471 if (min_valid)
8472 pf->npar_min_bw = min_bw;
8473 if (max_valid)
8474 pf->npar_max_bw = max_bw;
8475 }
8476
8477 return status;
8478}
8479
8480/**
8481 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8482 * @pf: board private structure
8483 **/
8484i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8485{
8486 struct i40e_aqc_configure_partition_bw_data bw_data;
8487 i40e_status status;
8488
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008489 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008490 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008491 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8492 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8493
8494 /* Set the new bandwidths */
8495 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8496
8497 return status;
8498}
8499
8500/**
8501 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8502 * @pf: board private structure
8503 **/
8504i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8505{
8506 /* Commit temporary BW setting to permanent NVM image */
8507 enum i40e_admin_queue_err last_aq_status;
8508 i40e_status ret;
8509 u16 nvm_word;
8510
8511 if (pf->hw.partition_id != 1) {
8512 dev_info(&pf->pdev->dev,
8513 "Commit BW only works on partition 1! This is partition %d",
8514 pf->hw.partition_id);
8515 ret = I40E_NOT_SUPPORTED;
8516 goto bw_commit_out;
8517 }
8518
8519 /* Acquire NVM for read access */
8520 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8521 last_aq_status = pf->hw.aq.asq_last_status;
8522 if (ret) {
8523 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008524 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8525 i40e_stat_str(&pf->hw, ret),
8526 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008527 goto bw_commit_out;
8528 }
8529
8530 /* Read word 0x10 of NVM - SW compatibility word 1 */
8531 ret = i40e_aq_read_nvm(&pf->hw,
8532 I40E_SR_NVM_CONTROL_WORD,
8533 0x10, sizeof(nvm_word), &nvm_word,
8534 false, NULL);
8535 /* Save off last admin queue command status before releasing
8536 * the NVM
8537 */
8538 last_aq_status = pf->hw.aq.asq_last_status;
8539 i40e_release_nvm(&pf->hw);
8540 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008541 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8542 i40e_stat_str(&pf->hw, ret),
8543 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008544 goto bw_commit_out;
8545 }
8546
8547 /* Wait a bit for NVM release to complete */
8548 msleep(50);
8549
8550 /* Acquire NVM for write access */
8551 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8552 last_aq_status = pf->hw.aq.asq_last_status;
8553 if (ret) {
8554 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008555 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8556 i40e_stat_str(&pf->hw, ret),
8557 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008558 goto bw_commit_out;
8559 }
8560 /* Write it back out unchanged to initiate update NVM,
8561 * which will force a write of the shadow (alt) RAM to
8562 * the NVM - thus storing the bandwidth values permanently.
8563 */
8564 ret = i40e_aq_update_nvm(&pf->hw,
8565 I40E_SR_NVM_CONTROL_WORD,
8566 0x10, sizeof(nvm_word),
8567 &nvm_word, true, NULL);
8568 /* Save off last admin queue command status before releasing
8569 * the NVM
8570 */
8571 last_aq_status = pf->hw.aq.asq_last_status;
8572 i40e_release_nvm(&pf->hw);
8573 if (ret)
8574 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008575 "BW settings NOT SAVED, err %s aq_err %s\n",
8576 i40e_stat_str(&pf->hw, ret),
8577 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008578bw_commit_out:
8579
8580 return ret;
8581}
8582
8583/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008584 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8585 * @pf: board private structure to initialize
8586 *
8587 * i40e_sw_init initializes the Adapter private data structure.
8588 * Fields are initialized based on PCI device information and
8589 * OS network device settings (MTU size).
8590 **/
8591static int i40e_sw_init(struct i40e_pf *pf)
8592{
8593 int err = 0;
8594 int size;
8595
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008596 /* Set default capability flags */
8597 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8598 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008599 I40E_FLAG_MSIX_ENABLED;
8600
Mitch Williamsca99eb92014-04-04 04:43:07 +00008601 /* Set default ITR */
8602 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8603 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8604
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008605 /* Depending on PF configurations, it is possible that the RSS
8606 * maximum might end up larger than the available queues
8607 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008608 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008609 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008610 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008611 pf->rss_size_max = min_t(int, pf->rss_size_max,
8612 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008613 if (pf->hw.func_caps.rss) {
8614 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008615 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8616 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008617 }
8618
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008619 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008620 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008621 pf->flags |= I40E_FLAG_MFP_ENABLED;
8622 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008623 if (i40e_get_npar_bw_setting(pf))
8624 dev_warn(&pf->pdev->dev,
8625 "Could not get NPAR bw settings\n");
8626 else
8627 dev_info(&pf->pdev->dev,
8628 "Min BW = %8.8x, Max BW = %8.8x\n",
8629 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008630 }
8631
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008632 /* FW/NVM is not yet fixed in this regard */
8633 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8634 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8635 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8636 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008637 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8638 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008639 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008640 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008641 else
8642 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008643 pf->fdir_pf_filter_count =
8644 pf->hw.func_caps.fd_filters_guaranteed;
8645 pf->hw.fdir_shared_filter_count =
8646 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008647 }
8648
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008649 if (i40e_is_mac_710(&pf->hw) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008650 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008651 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008652 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008653 /* No DCB support for FW < v4.33 */
8654 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8655 }
8656
8657 /* Disable FW LLDP if FW < v4.3 */
8658 if (i40e_is_mac_710(&pf->hw) &&
8659 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8660 (pf->hw.aq.fw_maj_ver < 4)))
8661 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8662
8663 /* Use the FW Set LLDP MIB API if FW > v4.40 */
8664 if (i40e_is_mac_710(&pf->hw) &&
8665 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8666 (pf->hw.aq.fw_maj_ver >= 5)))
8667 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008668
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008669 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008670 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008671 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008672 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008673 }
8674
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008675 if (pf->hw.func_caps.iwarp) {
8676 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8677 /* IWARP needs one extra vector for CQP just like MISC.*/
8678 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8679 }
8680
Vasu Dev38e00432014-08-01 13:27:03 -07008681#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008682 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008683
8684#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008685#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008686 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008687 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8688 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8689 pf->num_req_vfs = min_t(int,
8690 pf->hw.func_caps.num_vfs,
8691 I40E_MAX_VF_COUNT);
8692 }
8693#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008694 if (pf->hw.mac.type == I40E_MAC_X722) {
8695 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8696 I40E_FLAG_128_QP_RSS_CAPABLE |
8697 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8698 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8699 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008700 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
Jesse Brandeburg8e2cc0e2016-02-29 11:00:24 -08008701 I40E_FLAG_NO_PCI_LINK_CHECK |
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008702 I40E_FLAG_USE_SET_LLDP_MIB |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008703 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008704 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8705 ((pf->hw.aq.api_maj_ver == 1) &&
8706 (pf->hw.aq.api_min_ver > 4))) {
8707 /* Supported in FW API version higher than 1.4 */
8708 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008709 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8710 } else {
8711 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008712 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008713
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008714 pf->eeprom_version = 0xDEAD;
8715 pf->lan_veb = I40E_NO_VEB;
8716 pf->lan_vsi = I40E_NO_VSI;
8717
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008718 /* By default FW has this off for performance reasons */
8719 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8720
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008721 /* set up queue assignment tracking */
8722 size = sizeof(struct i40e_lump_tracking)
8723 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8724 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8725 if (!pf->qp_pile) {
8726 err = -ENOMEM;
8727 goto sw_init_done;
8728 }
8729 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8730 pf->qp_pile->search_hint = 0;
8731
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008732 pf->tx_timeout_recovery_level = 1;
8733
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008734 mutex_init(&pf->switch_mutex);
8735
Greg Rosec668a122015-02-26 16:10:39 +00008736 /* If NPAR is enabled nudge the Tx scheduler */
8737 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8738 i40e_set_npar_bw_setting(pf);
8739
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008740sw_init_done:
8741 return err;
8742}
8743
8744/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008745 * i40e_set_ntuple - set the ntuple feature flag and take action
8746 * @pf: board private structure to initialize
8747 * @features: the feature set that the stack is suggesting
8748 *
8749 * returns a bool to indicate if reset needs to happen
8750 **/
8751bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8752{
8753 bool need_reset = false;
8754
8755 /* Check if Flow Director n-tuple support was enabled or disabled. If
8756 * the state changed, we need to reset.
8757 */
8758 if (features & NETIF_F_NTUPLE) {
8759 /* Enable filters and mark for reset */
8760 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8761 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008762 /* enable FD_SB only if there is MSI-X vector */
8763 if (pf->num_fdsb_msix > 0)
8764 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008765 } else {
8766 /* turn off filters, mark for reset and clear SW filter list */
8767 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8768 need_reset = true;
8769 i40e_fdir_filter_exit(pf);
8770 }
8771 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008772 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008773 /* reset fd counters */
8774 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8775 pf->fdir_pf_active_filters = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008776 /* if ATR was auto disabled it can be re-enabled. */
8777 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller234dc4e2016-09-06 18:05:09 -07008778 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008779 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008780 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8781 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8782 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008783 }
8784 return need_reset;
8785}
8786
8787/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008788 * i40e_clear_rss_lut - clear the rx hash lookup table
8789 * @vsi: the VSI being configured
8790 **/
8791static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8792{
8793 struct i40e_pf *pf = vsi->back;
8794 struct i40e_hw *hw = &pf->hw;
8795 u16 vf_id = vsi->vf_id;
8796 u8 i;
8797
8798 if (vsi->type == I40E_VSI_MAIN) {
8799 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8800 wr32(hw, I40E_PFQF_HLUT(i), 0);
8801 } else if (vsi->type == I40E_VSI_SRIOV) {
8802 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8803 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8804 } else {
8805 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8806 }
8807}
8808
8809/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008810 * i40e_set_features - set the netdev feature flags
8811 * @netdev: ptr to the netdev being adjusted
8812 * @features: the feature set that the stack is suggesting
8813 **/
8814static int i40e_set_features(struct net_device *netdev,
8815 netdev_features_t features)
8816{
8817 struct i40e_netdev_priv *np = netdev_priv(netdev);
8818 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008819 struct i40e_pf *pf = vsi->back;
8820 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008821
Alan Bradyd8ec9862016-07-27 12:02:38 -07008822 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8823 i40e_pf_config_rss(pf);
8824 else if (!(features & NETIF_F_RXHASH) &&
8825 netdev->features & NETIF_F_RXHASH)
8826 i40e_clear_rss_lut(vsi);
8827
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008828 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8829 i40e_vlan_stripping_enable(vsi);
8830 else
8831 i40e_vlan_stripping_disable(vsi);
8832
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008833 need_reset = i40e_set_ntuple(pf, features);
8834
8835 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008836 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008837
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008838 return 0;
8839}
8840
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008841/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008842 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008843 * @pf: board private structure
8844 * @port: The UDP port to look up
8845 *
8846 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8847 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008848static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008849{
8850 u8 i;
8851
8852 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008853 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008854 return i;
8855 }
8856
8857 return i;
8858}
8859
8860/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008861 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008862 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008863 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008864 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008865static void i40e_udp_tunnel_add(struct net_device *netdev,
8866 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008867{
8868 struct i40e_netdev_priv *np = netdev_priv(netdev);
8869 struct i40e_vsi *vsi = np->vsi;
8870 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008871 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008872 u8 next_idx;
8873 u8 idx;
8874
Singhai, Anjali6a899022015-12-14 12:21:18 -08008875 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008876
8877 /* Check if port already exists */
8878 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008879 netdev_info(netdev, "port %d already offloaded\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008880 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008881 return;
8882 }
8883
8884 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008885 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008886
8887 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008888 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008889 ntohs(port));
8890 return;
8891 }
8892
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008893 switch (ti->type) {
8894 case UDP_TUNNEL_TYPE_VXLAN:
8895 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8896 break;
8897 case UDP_TUNNEL_TYPE_GENEVE:
8898 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8899 return;
8900 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8901 break;
8902 default:
8903 return;
8904 }
8905
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008906 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008907 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008908 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8909 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008910}
8911
8912/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008913 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008914 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008915 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008916 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008917static void i40e_udp_tunnel_del(struct net_device *netdev,
8918 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008919{
8920 struct i40e_netdev_priv *np = netdev_priv(netdev);
8921 struct i40e_vsi *vsi = np->vsi;
8922 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008923 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008924 u8 idx;
8925
Singhai, Anjali6a899022015-12-14 12:21:18 -08008926 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008927
8928 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008929 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8930 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008931
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008932 switch (ti->type) {
8933 case UDP_TUNNEL_TYPE_VXLAN:
8934 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8935 goto not_found;
8936 break;
8937 case UDP_TUNNEL_TYPE_GENEVE:
8938 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8939 goto not_found;
8940 break;
8941 default:
8942 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008943 }
8944
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008945 /* if port exists, set it to 0 (mark for deletion)
8946 * and make it pending
8947 */
8948 pf->udp_ports[idx].index = 0;
8949 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008950 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8951
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008952 return;
8953not_found:
8954 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
8955 ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08008956}
8957
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008958static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008959 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008960{
8961 struct i40e_netdev_priv *np = netdev_priv(netdev);
8962 struct i40e_pf *pf = np->vsi->back;
8963 struct i40e_hw *hw = &pf->hw;
8964
8965 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8966 return -EOPNOTSUPP;
8967
8968 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8969 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8970
8971 return 0;
8972}
8973
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008974/**
8975 * i40e_ndo_fdb_add - add an entry to the hardware database
8976 * @ndm: the input from the stack
8977 * @tb: pointer to array of nladdr (unused)
8978 * @dev: the net device pointer
8979 * @addr: the MAC address entry being added
8980 * @flags: instructions from stack about fdb operation
8981 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008982static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8983 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008984 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008985 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008986{
8987 struct i40e_netdev_priv *np = netdev_priv(dev);
8988 struct i40e_pf *pf = np->vsi->back;
8989 int err = 0;
8990
8991 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8992 return -EOPNOTSUPP;
8993
Or Gerlitz65891fe2014-12-14 18:19:05 +02008994 if (vid) {
8995 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8996 return -EINVAL;
8997 }
8998
Greg Rose4ba0dea2014-03-06 08:59:55 +00008999 /* Hardware does not support aging addresses so if a
9000 * ndm_state is given only allow permanent addresses
9001 */
9002 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9003 netdev_info(dev, "FDB only supports static addresses\n");
9004 return -EINVAL;
9005 }
9006
9007 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9008 err = dev_uc_add_excl(dev, addr);
9009 else if (is_multicast_ether_addr(addr))
9010 err = dev_mc_add_excl(dev, addr);
9011 else
9012 err = -EINVAL;
9013
9014 /* Only return duplicate errors if NLM_F_EXCL is set */
9015 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9016 err = 0;
9017
9018 return err;
9019}
9020
Neerav Parikh51616012015-02-06 08:52:14 +00009021/**
9022 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9023 * @dev: the netdev being configured
9024 * @nlh: RTNL message
9025 *
9026 * Inserts a new hardware bridge if not already created and
9027 * enables the bridging mode requested (VEB or VEPA). If the
9028 * hardware bridge has already been inserted and the request
9029 * is to change the mode then that requires a PF reset to
9030 * allow rebuild of the components with required hardware
9031 * bridge mode enabled.
9032 **/
9033static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009034 struct nlmsghdr *nlh,
9035 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009036{
9037 struct i40e_netdev_priv *np = netdev_priv(dev);
9038 struct i40e_vsi *vsi = np->vsi;
9039 struct i40e_pf *pf = vsi->back;
9040 struct i40e_veb *veb = NULL;
9041 struct nlattr *attr, *br_spec;
9042 int i, rem;
9043
9044 /* Only for PF VSI for now */
9045 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9046 return -EOPNOTSUPP;
9047
9048 /* Find the HW bridge for PF VSI */
9049 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9050 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9051 veb = pf->veb[i];
9052 }
9053
9054 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9055
9056 nla_for_each_nested(attr, br_spec, rem) {
9057 __u16 mode;
9058
9059 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9060 continue;
9061
9062 mode = nla_get_u16(attr);
9063 if ((mode != BRIDGE_MODE_VEPA) &&
9064 (mode != BRIDGE_MODE_VEB))
9065 return -EINVAL;
9066
9067 /* Insert a new HW bridge */
9068 if (!veb) {
9069 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9070 vsi->tc_config.enabled_tc);
9071 if (veb) {
9072 veb->bridge_mode = mode;
9073 i40e_config_bridge_mode(veb);
9074 } else {
9075 /* No Bridge HW offload available */
9076 return -ENOENT;
9077 }
9078 break;
9079 } else if (mode != veb->bridge_mode) {
9080 /* Existing HW bridge but different mode needs reset */
9081 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009082 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9083 if (mode == BRIDGE_MODE_VEB)
9084 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9085 else
9086 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9087 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00009088 break;
9089 }
9090 }
9091
9092 return 0;
9093}
9094
9095/**
9096 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9097 * @skb: skb buff
9098 * @pid: process id
9099 * @seq: RTNL message seq #
9100 * @dev: the netdev being configured
9101 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009102 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009103 *
9104 * Return the mode in which the hardware bridge is operating in
9105 * i.e VEB or VEPA.
9106 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009107static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9108 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009109 u32 __always_unused filter_mask,
9110 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009111{
9112 struct i40e_netdev_priv *np = netdev_priv(dev);
9113 struct i40e_vsi *vsi = np->vsi;
9114 struct i40e_pf *pf = vsi->back;
9115 struct i40e_veb *veb = NULL;
9116 int i;
9117
9118 /* Only for PF VSI for now */
9119 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9120 return -EOPNOTSUPP;
9121
9122 /* Find the HW bridge for the PF VSI */
9123 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9124 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9125 veb = pf->veb[i];
9126 }
9127
9128 if (!veb)
9129 return 0;
9130
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009131 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009132 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009133}
Neerav Parikh51616012015-02-06 08:52:14 +00009134
Joe Stringerf44a75e2015-04-14 17:09:14 -07009135/**
9136 * i40e_features_check - Validate encapsulated packet conforms to limits
9137 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009138 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009139 * @features: Offload features that the stack believes apply
9140 **/
9141static netdev_features_t i40e_features_check(struct sk_buff *skb,
9142 struct net_device *dev,
9143 netdev_features_t features)
9144{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009145 size_t len;
9146
9147 /* No point in doing any of this if neither checksum nor GSO are
9148 * being requested for this frame. We can rule out both by just
9149 * checking for CHECKSUM_PARTIAL
9150 */
9151 if (skb->ip_summed != CHECKSUM_PARTIAL)
9152 return features;
9153
9154 /* We cannot support GSO if the MSS is going to be less than
9155 * 64 bytes. If it is then we need to drop support for GSO.
9156 */
9157 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9158 features &= ~NETIF_F_GSO_MASK;
9159
9160 /* MACLEN can support at most 63 words */
9161 len = skb_network_header(skb) - skb->data;
9162 if (len & ~(63 * 2))
9163 goto out_err;
9164
9165 /* IPLEN and EIPLEN can support at most 127 dwords */
9166 len = skb_transport_header(skb) - skb_network_header(skb);
9167 if (len & ~(127 * 4))
9168 goto out_err;
9169
9170 if (skb->encapsulation) {
9171 /* L4TUNLEN can support 127 words */
9172 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9173 if (len & ~(127 * 2))
9174 goto out_err;
9175
9176 /* IPLEN can support at most 127 dwords */
9177 len = skb_inner_transport_header(skb) -
9178 skb_inner_network_header(skb);
9179 if (len & ~(127 * 4))
9180 goto out_err;
9181 }
9182
9183 /* No need to validate L4LEN as TCP is the only protocol with a
9184 * a flexible value and we support all possible values supported
9185 * by TCP, which is at most 15 dwords
9186 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009187
9188 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009189out_err:
9190 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009191}
9192
Shannon Nelson37a29732015-02-27 09:15:19 +00009193static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009194 .ndo_open = i40e_open,
9195 .ndo_stop = i40e_close,
9196 .ndo_start_xmit = i40e_lan_xmit_frame,
9197 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9198 .ndo_set_rx_mode = i40e_set_rx_mode,
9199 .ndo_validate_addr = eth_validate_addr,
9200 .ndo_set_mac_address = i40e_set_mac,
9201 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009202 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009203 .ndo_tx_timeout = i40e_tx_timeout,
9204 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9205 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9206#ifdef CONFIG_NET_POLL_CONTROLLER
9207 .ndo_poll_controller = i40e_netpoll,
9208#endif
John Fastabende4c67342016-02-16 21:16:15 -08009209 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009210#ifdef I40E_FCOE
9211 .ndo_fcoe_enable = i40e_fcoe_enable,
9212 .ndo_fcoe_disable = i40e_fcoe_disable,
9213#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009214 .ndo_set_features = i40e_set_features,
9215 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9216 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009217 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009218 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009219 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009220 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009221 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009222 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9223 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009224 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009225 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009226 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009227 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9228 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009229};
9230
9231/**
9232 * i40e_config_netdev - Setup the netdev flags
9233 * @vsi: the VSI being configured
9234 *
9235 * Returns 0 on success, negative value on failure
9236 **/
9237static int i40e_config_netdev(struct i40e_vsi *vsi)
9238{
9239 struct i40e_pf *pf = vsi->back;
9240 struct i40e_hw *hw = &pf->hw;
9241 struct i40e_netdev_priv *np;
9242 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009243 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009244 u8 mac_addr[ETH_ALEN];
9245 int etherdev_size;
9246
9247 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009248 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009249 if (!netdev)
9250 return -ENOMEM;
9251
9252 vsi->netdev = netdev;
9253 np = netdev_priv(netdev);
9254 np->vsi = vsi;
9255
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009256 netdev->hw_enc_features |= NETIF_F_SG |
9257 NETIF_F_IP_CSUM |
9258 NETIF_F_IPV6_CSUM |
9259 NETIF_F_HIGHDMA |
9260 NETIF_F_SOFT_FEATURES |
9261 NETIF_F_TSO |
9262 NETIF_F_TSO_ECN |
9263 NETIF_F_TSO6 |
9264 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009265 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07009266 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07009267 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009268 NETIF_F_GSO_UDP_TUNNEL |
9269 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009270 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009271 NETIF_F_SCTP_CRC |
9272 NETIF_F_RXHASH |
9273 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009274 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009275
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009276 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009277 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9278
9279 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009280
9281 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009282 netdev->vlan_features |= netdev->hw_enc_features |
9283 NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009284
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009285 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009286 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009287
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009288 netdev->hw_features |= netdev->hw_enc_features |
9289 NETIF_F_HW_VLAN_CTAG_TX |
9290 NETIF_F_HW_VLAN_CTAG_RX;
9291
9292 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009293 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009294
9295 if (vsi->type == I40E_VSI_MAIN) {
9296 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009297 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009298 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009299 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009300 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009301 } else {
9302 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9303 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9304 pf->vsi[pf->lan_vsi]->netdev->name);
9305 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009306
Jacob Keller278e7d02016-10-05 09:30:37 -07009307 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009308 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009309 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009310 }
Kiran Patil21659032015-09-30 14:09:03 -04009311
Jacob Keller435c0842016-11-08 13:05:10 -08009312 /* Add the broadcast filter so that we initially will receive
9313 * broadcast packets. Note that when a new VLAN is first added the
9314 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9315 * specific filters as part of transitioning into "vlan" operation.
9316 * When more VLANs are added, the driver will copy each existing MAC
9317 * filter and add it for the new VLAN.
9318 *
9319 * Broadcast filters are handled specially by
9320 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9321 * promiscuous bit instead of adding this directly as a MAC/VLAN
9322 * filter. The subtask will update the correct broadcast promiscuous
9323 * bits as VLANs become active or inactive.
9324 */
9325 eth_broadcast_addr(broadcast);
9326 spin_lock_bh(&vsi->mac_filter_hash_lock);
9327 i40e_add_filter(vsi, broadcast, I40E_VLAN_ANY);
9328 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9329
Greg Rose9a173902014-05-22 06:32:02 +00009330 ether_addr_copy(netdev->dev_addr, mac_addr);
9331 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009332
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009333 netdev->priv_flags |= IFF_UNICAST_FLT;
9334 netdev->priv_flags |= IFF_SUPP_NOFCS;
9335 /* Setup netdev TC information */
9336 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9337
9338 netdev->netdev_ops = &i40e_netdev_ops;
9339 netdev->watchdog_timeo = 5 * HZ;
9340 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009341#ifdef I40E_FCOE
9342 i40e_fcoe_config_netdev(netdev, vsi);
9343#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009344
Jarod Wilson91c527a2016-10-17 15:54:05 -04009345 /* MTU range: 68 - 9706 */
9346 netdev->min_mtu = ETH_MIN_MTU;
9347 netdev->max_mtu = I40E_MAX_RXBUFFER -
9348 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9349
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009350 return 0;
9351}
9352
9353/**
9354 * i40e_vsi_delete - Delete a VSI from the switch
9355 * @vsi: the VSI being removed
9356 *
9357 * Returns 0 on success, negative value on failure
9358 **/
9359static void i40e_vsi_delete(struct i40e_vsi *vsi)
9360{
9361 /* remove default VSI is not allowed */
9362 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9363 return;
9364
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009365 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009366}
9367
9368/**
Neerav Parikh51616012015-02-06 08:52:14 +00009369 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9370 * @vsi: the VSI being queried
9371 *
9372 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9373 **/
9374int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9375{
9376 struct i40e_veb *veb;
9377 struct i40e_pf *pf = vsi->back;
9378
9379 /* Uplink is not a bridge so default to VEB */
9380 if (vsi->veb_idx == I40E_NO_VEB)
9381 return 1;
9382
9383 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009384 if (!veb) {
9385 dev_info(&pf->pdev->dev,
9386 "There is no veb associated with the bridge\n");
9387 return -ENOENT;
9388 }
Neerav Parikh51616012015-02-06 08:52:14 +00009389
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009390 /* Uplink is a bridge in VEPA mode */
9391 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9392 return 0;
9393 } else {
9394 /* Uplink is a bridge in VEB mode */
9395 return 1;
9396 }
9397
9398 /* VEPA is now default bridge, so return 0 */
9399 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009400}
9401
9402/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009403 * i40e_add_vsi - Add a VSI to the switch
9404 * @vsi: the VSI being configured
9405 *
9406 * This initializes a VSI context depending on the VSI type to be added and
9407 * passes it down to the add_vsi aq command.
9408 **/
9409static int i40e_add_vsi(struct i40e_vsi *vsi)
9410{
9411 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009412 struct i40e_pf *pf = vsi->back;
9413 struct i40e_hw *hw = &pf->hw;
9414 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009415 struct i40e_mac_filter *f;
9416 struct hlist_node *h;
9417 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009418
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009419 u8 enabled_tc = 0x1; /* TC0 enabled */
9420 int f_count = 0;
9421
9422 memset(&ctxt, 0, sizeof(ctxt));
9423 switch (vsi->type) {
9424 case I40E_VSI_MAIN:
9425 /* The PF's main VSI is already setup as part of the
9426 * device initialization, so we'll not bother with
9427 * the add_vsi call, but we will retrieve the current
9428 * VSI context.
9429 */
9430 ctxt.seid = pf->main_vsi_seid;
9431 ctxt.pf_num = pf->hw.pf_id;
9432 ctxt.vf_num = 0;
9433 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9434 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9435 if (ret) {
9436 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009437 "couldn't get PF vsi config, err %s aq_err %s\n",
9438 i40e_stat_str(&pf->hw, ret),
9439 i40e_aq_str(&pf->hw,
9440 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009441 return -ENOENT;
9442 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009443 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009444 vsi->info.valid_sections = 0;
9445
9446 vsi->seid = ctxt.seid;
9447 vsi->id = ctxt.vsi_number;
9448
9449 enabled_tc = i40e_pf_get_tc_map(pf);
9450
9451 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009452 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9453 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009454 memset(&ctxt, 0, sizeof(ctxt));
9455 ctxt.seid = pf->main_vsi_seid;
9456 ctxt.pf_num = pf->hw.pf_id;
9457 ctxt.vf_num = 0;
9458 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9459 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9460 if (ret) {
9461 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009462 "update vsi failed, err %s aq_err %s\n",
9463 i40e_stat_str(&pf->hw, ret),
9464 i40e_aq_str(&pf->hw,
9465 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009466 ret = -ENOENT;
9467 goto err;
9468 }
9469 /* update the local VSI info queue map */
9470 i40e_vsi_update_queue_map(vsi, &ctxt);
9471 vsi->info.valid_sections = 0;
9472 } else {
9473 /* Default/Main VSI is only enabled for TC0
9474 * reconfigure it to enable all TCs that are
9475 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009476 * For MFP case the iSCSI PF would use this
9477 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009478 */
9479 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9480 if (ret) {
9481 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009482 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9483 enabled_tc,
9484 i40e_stat_str(&pf->hw, ret),
9485 i40e_aq_str(&pf->hw,
9486 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009487 ret = -ENOENT;
9488 }
9489 }
9490 break;
9491
9492 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009493 ctxt.pf_num = hw->pf_id;
9494 ctxt.vf_num = 0;
9495 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009496 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009497 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009498 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9499 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009500 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009501 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009502 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009503 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009504 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009505 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009506 break;
9507
9508 case I40E_VSI_VMDQ2:
9509 ctxt.pf_num = hw->pf_id;
9510 ctxt.vf_num = 0;
9511 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009512 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009513 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9514
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009515 /* This VSI is connected to VEB so the switch_id
9516 * should be set to zero by default.
9517 */
Neerav Parikh51616012015-02-06 08:52:14 +00009518 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9519 ctxt.info.valid_sections |=
9520 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9521 ctxt.info.switch_id =
9522 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9523 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009524
9525 /* Setup the VSI tx/rx queue map for TC0 only for now */
9526 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9527 break;
9528
9529 case I40E_VSI_SRIOV:
9530 ctxt.pf_num = hw->pf_id;
9531 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9532 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009533 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009534 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9535
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009536 /* This VSI is connected to VEB so the switch_id
9537 * should be set to zero by default.
9538 */
Neerav Parikh51616012015-02-06 08:52:14 +00009539 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9540 ctxt.info.valid_sections |=
9541 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9542 ctxt.info.switch_id =
9543 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9544 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009545
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009546 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9547 ctxt.info.valid_sections |=
9548 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9549 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009550 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9551 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009552 }
9553
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009554 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9555 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009556 if (pf->vf[vsi->vf_id].spoofchk) {
9557 ctxt.info.valid_sections |=
9558 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9559 ctxt.info.sec_flags |=
9560 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9561 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9562 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009563 /* Setup the VSI tx/rx queue map for TC0 only for now */
9564 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9565 break;
9566
Vasu Dev38e00432014-08-01 13:27:03 -07009567#ifdef I40E_FCOE
9568 case I40E_VSI_FCOE:
9569 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9570 if (ret) {
9571 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9572 return ret;
9573 }
9574 break;
9575
9576#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009577 case I40E_VSI_IWARP:
9578 /* send down message to iWARP */
9579 break;
9580
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009581 default:
9582 return -ENODEV;
9583 }
9584
9585 if (vsi->type != I40E_VSI_MAIN) {
9586 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9587 if (ret) {
9588 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009589 "add vsi failed, err %s aq_err %s\n",
9590 i40e_stat_str(&pf->hw, ret),
9591 i40e_aq_str(&pf->hw,
9592 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009593 ret = -ENOENT;
9594 goto err;
9595 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009596 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009597 vsi->info.valid_sections = 0;
9598 vsi->seid = ctxt.seid;
9599 vsi->id = ctxt.vsi_number;
9600 }
9601
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009602 vsi->active_filters = 0;
9603 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009604 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009605 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009606 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009607 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009608 f_count++;
9609 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009610 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009611
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009612 if (f_count) {
9613 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9614 pf->flags |= I40E_FLAG_FILTER_SYNC;
9615 }
9616
9617 /* Update VSI BW information */
9618 ret = i40e_vsi_get_bw_info(vsi);
9619 if (ret) {
9620 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009621 "couldn't get vsi bw info, err %s aq_err %s\n",
9622 i40e_stat_str(&pf->hw, ret),
9623 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009624 /* VSI is already added so not tearing that up */
9625 ret = 0;
9626 }
9627
9628err:
9629 return ret;
9630}
9631
9632/**
9633 * i40e_vsi_release - Delete a VSI and free its resources
9634 * @vsi: the VSI being removed
9635 *
9636 * Returns 0 on success or < 0 on error
9637 **/
9638int i40e_vsi_release(struct i40e_vsi *vsi)
9639{
Jacob Keller278e7d02016-10-05 09:30:37 -07009640 struct i40e_mac_filter *f;
9641 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009642 struct i40e_veb *veb = NULL;
9643 struct i40e_pf *pf;
9644 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009645 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009646
9647 pf = vsi->back;
9648
9649 /* release of a VEB-owner or last VSI is not allowed */
9650 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9651 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9652 vsi->seid, vsi->uplink_seid);
9653 return -ENODEV;
9654 }
9655 if (vsi == pf->vsi[pf->lan_vsi] &&
9656 !test_bit(__I40E_DOWN, &pf->state)) {
9657 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9658 return -ENODEV;
9659 }
9660
9661 uplink_seid = vsi->uplink_seid;
9662 if (vsi->type != I40E_VSI_SRIOV) {
9663 if (vsi->netdev_registered) {
9664 vsi->netdev_registered = false;
9665 if (vsi->netdev) {
9666 /* results in a call to i40e_close() */
9667 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009668 }
9669 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009670 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009671 }
9672 i40e_vsi_disable_irq(vsi);
9673 }
9674
Jacob Keller278e7d02016-10-05 09:30:37 -07009675 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009676
9677 /* clear the sync flag on all filters */
9678 if (vsi->netdev) {
9679 __dev_uc_unsync(vsi->netdev, NULL);
9680 __dev_mc_unsync(vsi->netdev, NULL);
9681 }
9682
9683 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009684 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009685 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009686
Jacob Keller278e7d02016-10-05 09:30:37 -07009687 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009688
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009689 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009690
9691 i40e_vsi_delete(vsi);
9692 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009693 if (vsi->netdev) {
9694 free_netdev(vsi->netdev);
9695 vsi->netdev = NULL;
9696 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009697 i40e_vsi_clear_rings(vsi);
9698 i40e_vsi_clear(vsi);
9699
9700 /* If this was the last thing on the VEB, except for the
9701 * controlling VSI, remove the VEB, which puts the controlling
9702 * VSI onto the next level down in the switch.
9703 *
9704 * Well, okay, there's one more exception here: don't remove
9705 * the orphan VEBs yet. We'll wait for an explicit remove request
9706 * from up the network stack.
9707 */
Mitch Williams505682c2014-05-20 08:01:37 +00009708 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009709 if (pf->vsi[i] &&
9710 pf->vsi[i]->uplink_seid == uplink_seid &&
9711 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9712 n++; /* count the VSIs */
9713 }
9714 }
9715 for (i = 0; i < I40E_MAX_VEB; i++) {
9716 if (!pf->veb[i])
9717 continue;
9718 if (pf->veb[i]->uplink_seid == uplink_seid)
9719 n++; /* count the VEBs */
9720 if (pf->veb[i]->seid == uplink_seid)
9721 veb = pf->veb[i];
9722 }
9723 if (n == 0 && veb && veb->uplink_seid != 0)
9724 i40e_veb_release(veb);
9725
9726 return 0;
9727}
9728
9729/**
9730 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9731 * @vsi: ptr to the VSI
9732 *
9733 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9734 * corresponding SW VSI structure and initializes num_queue_pairs for the
9735 * newly allocated VSI.
9736 *
9737 * Returns 0 on success or negative on failure
9738 **/
9739static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9740{
9741 int ret = -ENOENT;
9742 struct i40e_pf *pf = vsi->back;
9743
Alexander Duyck493fb302013-09-28 07:01:44 +00009744 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009745 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9746 vsi->seid);
9747 return -EEXIST;
9748 }
9749
9750 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009751 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009752 vsi->seid, vsi->base_vector);
9753 return -EEXIST;
9754 }
9755
Greg Rose90e04072014-03-06 08:59:57 +00009756 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009757 if (ret) {
9758 dev_info(&pf->pdev->dev,
9759 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9760 vsi->num_q_vectors, vsi->seid, ret);
9761 vsi->num_q_vectors = 0;
9762 goto vector_setup_out;
9763 }
9764
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009765 /* In Legacy mode, we do not have to get any other vector since we
9766 * piggyback on the misc/ICR0 for queue interrupts.
9767 */
9768 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9769 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009770 if (vsi->num_q_vectors)
9771 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9772 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009773 if (vsi->base_vector < 0) {
9774 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009775 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9776 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009777 i40e_vsi_free_q_vectors(vsi);
9778 ret = -ENOENT;
9779 goto vector_setup_out;
9780 }
9781
9782vector_setup_out:
9783 return ret;
9784}
9785
9786/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009787 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9788 * @vsi: pointer to the vsi.
9789 *
9790 * This re-allocates a vsi's queue resources.
9791 *
9792 * Returns pointer to the successfully allocated and configured VSI sw struct
9793 * on success, otherwise returns NULL on failure.
9794 **/
9795static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9796{
John Underwoodf5340392016-02-18 09:19:24 -08009797 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009798 u8 enabled_tc;
9799 int ret;
9800
John Underwoodf5340392016-02-18 09:19:24 -08009801 if (!vsi)
9802 return NULL;
9803
9804 pf = vsi->back;
9805
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009806 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9807 i40e_vsi_clear_rings(vsi);
9808
9809 i40e_vsi_free_arrays(vsi, false);
9810 i40e_set_num_rings_in_vsi(vsi);
9811 ret = i40e_vsi_alloc_arrays(vsi, false);
9812 if (ret)
9813 goto err_vsi;
9814
9815 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9816 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009817 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009818 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009819 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009820 goto err_vsi;
9821 }
9822 vsi->base_queue = ret;
9823
9824 /* Update the FW view of the VSI. Force a reset of TC and queue
9825 * layout configurations.
9826 */
9827 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9828 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9829 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9830 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9831
9832 /* assign it some queues */
9833 ret = i40e_alloc_rings(vsi);
9834 if (ret)
9835 goto err_rings;
9836
9837 /* map all of the rings to the q_vectors */
9838 i40e_vsi_map_rings_to_vectors(vsi);
9839 return vsi;
9840
9841err_rings:
9842 i40e_vsi_free_q_vectors(vsi);
9843 if (vsi->netdev_registered) {
9844 vsi->netdev_registered = false;
9845 unregister_netdev(vsi->netdev);
9846 free_netdev(vsi->netdev);
9847 vsi->netdev = NULL;
9848 }
9849 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9850err_vsi:
9851 i40e_vsi_clear(vsi);
9852 return NULL;
9853}
9854
9855/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009856 * i40e_vsi_setup - Set up a VSI by a given type
9857 * @pf: board private structure
9858 * @type: VSI type
9859 * @uplink_seid: the switch element to link to
9860 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9861 *
9862 * This allocates the sw VSI structure and its queue resources, then add a VSI
9863 * to the identified VEB.
9864 *
9865 * Returns pointer to the successfully allocated and configure VSI sw struct on
9866 * success, otherwise returns NULL on failure.
9867 **/
9868struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9869 u16 uplink_seid, u32 param1)
9870{
9871 struct i40e_vsi *vsi = NULL;
9872 struct i40e_veb *veb = NULL;
9873 int ret, i;
9874 int v_idx;
9875
9876 /* The requested uplink_seid must be either
9877 * - the PF's port seid
9878 * no VEB is needed because this is the PF
9879 * or this is a Flow Director special case VSI
9880 * - seid of an existing VEB
9881 * - seid of a VSI that owns an existing VEB
9882 * - seid of a VSI that doesn't own a VEB
9883 * a new VEB is created and the VSI becomes the owner
9884 * - seid of the PF VSI, which is what creates the first VEB
9885 * this is a special case of the previous
9886 *
9887 * Find which uplink_seid we were given and create a new VEB if needed
9888 */
9889 for (i = 0; i < I40E_MAX_VEB; i++) {
9890 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9891 veb = pf->veb[i];
9892 break;
9893 }
9894 }
9895
9896 if (!veb && uplink_seid != pf->mac_seid) {
9897
Mitch Williams505682c2014-05-20 08:01:37 +00009898 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009899 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9900 vsi = pf->vsi[i];
9901 break;
9902 }
9903 }
9904 if (!vsi) {
9905 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9906 uplink_seid);
9907 return NULL;
9908 }
9909
9910 if (vsi->uplink_seid == pf->mac_seid)
9911 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9912 vsi->tc_config.enabled_tc);
9913 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9914 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9915 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009916 if (veb) {
9917 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9918 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009919 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009920 return NULL;
9921 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009922 /* We come up by default in VEPA mode if SRIOV is not
9923 * already enabled, in which case we can't force VEPA
9924 * mode.
9925 */
9926 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9927 veb->bridge_mode = BRIDGE_MODE_VEPA;
9928 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9929 }
Neerav Parikh51616012015-02-06 08:52:14 +00009930 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009931 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009932 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9933 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9934 veb = pf->veb[i];
9935 }
9936 if (!veb) {
9937 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9938 return NULL;
9939 }
9940
9941 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9942 uplink_seid = veb->seid;
9943 }
9944
9945 /* get vsi sw struct */
9946 v_idx = i40e_vsi_mem_alloc(pf, type);
9947 if (v_idx < 0)
9948 goto err_alloc;
9949 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009950 if (!vsi)
9951 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009952 vsi->type = type;
9953 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9954
9955 if (type == I40E_VSI_MAIN)
9956 pf->lan_vsi = v_idx;
9957 else if (type == I40E_VSI_SRIOV)
9958 vsi->vf_id = param1;
9959 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009960 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9961 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009962 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009963 dev_info(&pf->pdev->dev,
9964 "failed to get tracking for %d queues for VSI %d err=%d\n",
9965 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009966 goto err_vsi;
9967 }
9968 vsi->base_queue = ret;
9969
9970 /* get a VSI from the hardware */
9971 vsi->uplink_seid = uplink_seid;
9972 ret = i40e_add_vsi(vsi);
9973 if (ret)
9974 goto err_vsi;
9975
9976 switch (vsi->type) {
9977 /* setup the netdev if needed */
9978 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009979 /* Apply relevant filters if a platform-specific mac
9980 * address was selected.
9981 */
9982 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9983 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9984 if (ret) {
9985 dev_warn(&pf->pdev->dev,
9986 "could not set up macaddr; err %d\n",
9987 ret);
9988 }
9989 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009990 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07009991 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009992 ret = i40e_config_netdev(vsi);
9993 if (ret)
9994 goto err_netdev;
9995 ret = register_netdev(vsi->netdev);
9996 if (ret)
9997 goto err_netdev;
9998 vsi->netdev_registered = true;
9999 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010000#ifdef CONFIG_I40E_DCB
10001 /* Setup DCB netlink interface */
10002 i40e_dcbnl_setup(vsi);
10003#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010004 /* fall through */
10005
10006 case I40E_VSI_FDIR:
10007 /* set up vectors and rings if needed */
10008 ret = i40e_vsi_setup_vectors(vsi);
10009 if (ret)
10010 goto err_msix;
10011
10012 ret = i40e_alloc_rings(vsi);
10013 if (ret)
10014 goto err_rings;
10015
10016 /* map all of the rings to the q_vectors */
10017 i40e_vsi_map_rings_to_vectors(vsi);
10018
10019 i40e_vsi_reset_stats(vsi);
10020 break;
10021
10022 default:
10023 /* no netdev or rings for the other VSI types */
10024 break;
10025 }
10026
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010027 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10028 (vsi->type == I40E_VSI_VMDQ2)) {
10029 ret = i40e_vsi_config_rss(vsi);
10030 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010031 return vsi;
10032
10033err_rings:
10034 i40e_vsi_free_q_vectors(vsi);
10035err_msix:
10036 if (vsi->netdev_registered) {
10037 vsi->netdev_registered = false;
10038 unregister_netdev(vsi->netdev);
10039 free_netdev(vsi->netdev);
10040 vsi->netdev = NULL;
10041 }
10042err_netdev:
10043 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10044err_vsi:
10045 i40e_vsi_clear(vsi);
10046err_alloc:
10047 return NULL;
10048}
10049
10050/**
10051 * i40e_veb_get_bw_info - Query VEB BW information
10052 * @veb: the veb to query
10053 *
10054 * Query the Tx scheduler BW configuration data for given VEB
10055 **/
10056static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10057{
10058 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10059 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10060 struct i40e_pf *pf = veb->pf;
10061 struct i40e_hw *hw = &pf->hw;
10062 u32 tc_bw_max;
10063 int ret = 0;
10064 int i;
10065
10066 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10067 &bw_data, NULL);
10068 if (ret) {
10069 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010070 "query veb bw config failed, err %s aq_err %s\n",
10071 i40e_stat_str(&pf->hw, ret),
10072 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010073 goto out;
10074 }
10075
10076 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10077 &ets_data, NULL);
10078 if (ret) {
10079 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010080 "query veb bw ets config failed, err %s aq_err %s\n",
10081 i40e_stat_str(&pf->hw, ret),
10082 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010083 goto out;
10084 }
10085
10086 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10087 veb->bw_max_quanta = ets_data.tc_bw_max;
10088 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010089 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010090 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10091 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10092 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10093 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10094 veb->bw_tc_limit_credits[i] =
10095 le16_to_cpu(bw_data.tc_bw_limits[i]);
10096 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10097 }
10098
10099out:
10100 return ret;
10101}
10102
10103/**
10104 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10105 * @pf: board private structure
10106 *
10107 * On error: returns error code (negative)
10108 * On success: returns vsi index in PF (positive)
10109 **/
10110static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10111{
10112 int ret = -ENOENT;
10113 struct i40e_veb *veb;
10114 int i;
10115
10116 /* Need to protect the allocation of switch elements at the PF level */
10117 mutex_lock(&pf->switch_mutex);
10118
10119 /* VEB list may be fragmented if VEB creation/destruction has
10120 * been happening. We can afford to do a quick scan to look
10121 * for any free slots in the list.
10122 *
10123 * find next empty veb slot, looping back around if necessary
10124 */
10125 i = 0;
10126 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10127 i++;
10128 if (i >= I40E_MAX_VEB) {
10129 ret = -ENOMEM;
10130 goto err_alloc_veb; /* out of VEB slots! */
10131 }
10132
10133 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10134 if (!veb) {
10135 ret = -ENOMEM;
10136 goto err_alloc_veb;
10137 }
10138 veb->pf = pf;
10139 veb->idx = i;
10140 veb->enabled_tc = 1;
10141
10142 pf->veb[i] = veb;
10143 ret = i;
10144err_alloc_veb:
10145 mutex_unlock(&pf->switch_mutex);
10146 return ret;
10147}
10148
10149/**
10150 * i40e_switch_branch_release - Delete a branch of the switch tree
10151 * @branch: where to start deleting
10152 *
10153 * This uses recursion to find the tips of the branch to be
10154 * removed, deleting until we get back to and can delete this VEB.
10155 **/
10156static void i40e_switch_branch_release(struct i40e_veb *branch)
10157{
10158 struct i40e_pf *pf = branch->pf;
10159 u16 branch_seid = branch->seid;
10160 u16 veb_idx = branch->idx;
10161 int i;
10162
10163 /* release any VEBs on this VEB - RECURSION */
10164 for (i = 0; i < I40E_MAX_VEB; i++) {
10165 if (!pf->veb[i])
10166 continue;
10167 if (pf->veb[i]->uplink_seid == branch->seid)
10168 i40e_switch_branch_release(pf->veb[i]);
10169 }
10170
10171 /* Release the VSIs on this VEB, but not the owner VSI.
10172 *
10173 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10174 * the VEB itself, so don't use (*branch) after this loop.
10175 */
Mitch Williams505682c2014-05-20 08:01:37 +000010176 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010177 if (!pf->vsi[i])
10178 continue;
10179 if (pf->vsi[i]->uplink_seid == branch_seid &&
10180 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10181 i40e_vsi_release(pf->vsi[i]);
10182 }
10183 }
10184
10185 /* There's one corner case where the VEB might not have been
10186 * removed, so double check it here and remove it if needed.
10187 * This case happens if the veb was created from the debugfs
10188 * commands and no VSIs were added to it.
10189 */
10190 if (pf->veb[veb_idx])
10191 i40e_veb_release(pf->veb[veb_idx]);
10192}
10193
10194/**
10195 * i40e_veb_clear - remove veb struct
10196 * @veb: the veb to remove
10197 **/
10198static void i40e_veb_clear(struct i40e_veb *veb)
10199{
10200 if (!veb)
10201 return;
10202
10203 if (veb->pf) {
10204 struct i40e_pf *pf = veb->pf;
10205
10206 mutex_lock(&pf->switch_mutex);
10207 if (pf->veb[veb->idx] == veb)
10208 pf->veb[veb->idx] = NULL;
10209 mutex_unlock(&pf->switch_mutex);
10210 }
10211
10212 kfree(veb);
10213}
10214
10215/**
10216 * i40e_veb_release - Delete a VEB and free its resources
10217 * @veb: the VEB being removed
10218 **/
10219void i40e_veb_release(struct i40e_veb *veb)
10220{
10221 struct i40e_vsi *vsi = NULL;
10222 struct i40e_pf *pf;
10223 int i, n = 0;
10224
10225 pf = veb->pf;
10226
10227 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010228 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010229 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10230 n++;
10231 vsi = pf->vsi[i];
10232 }
10233 }
10234 if (n != 1) {
10235 dev_info(&pf->pdev->dev,
10236 "can't remove VEB %d with %d VSIs left\n",
10237 veb->seid, n);
10238 return;
10239 }
10240
10241 /* move the remaining VSI to uplink veb */
10242 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10243 if (veb->uplink_seid) {
10244 vsi->uplink_seid = veb->uplink_seid;
10245 if (veb->uplink_seid == pf->mac_seid)
10246 vsi->veb_idx = I40E_NO_VEB;
10247 else
10248 vsi->veb_idx = veb->veb_idx;
10249 } else {
10250 /* floating VEB */
10251 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10252 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10253 }
10254
10255 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10256 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010257}
10258
10259/**
10260 * i40e_add_veb - create the VEB in the switch
10261 * @veb: the VEB to be instantiated
10262 * @vsi: the controlling VSI
10263 **/
10264static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10265{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010266 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010267 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010268 int ret;
10269
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010270 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010271 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010272 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010273
10274 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010275 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010276 dev_info(&pf->pdev->dev,
10277 "couldn't add VEB, err %s aq_err %s\n",
10278 i40e_stat_str(&pf->hw, ret),
10279 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010280 return -EPERM;
10281 }
10282
10283 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010284 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010285 &veb->stats_idx, NULL, NULL, NULL);
10286 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010287 dev_info(&pf->pdev->dev,
10288 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10289 i40e_stat_str(&pf->hw, ret),
10290 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010291 return -EPERM;
10292 }
10293 ret = i40e_veb_get_bw_info(veb);
10294 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010295 dev_info(&pf->pdev->dev,
10296 "couldn't get VEB bw info, err %s aq_err %s\n",
10297 i40e_stat_str(&pf->hw, ret),
10298 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10299 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010300 return -ENOENT;
10301 }
10302
10303 vsi->uplink_seid = veb->seid;
10304 vsi->veb_idx = veb->idx;
10305 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10306
10307 return 0;
10308}
10309
10310/**
10311 * i40e_veb_setup - Set up a VEB
10312 * @pf: board private structure
10313 * @flags: VEB setup flags
10314 * @uplink_seid: the switch element to link to
10315 * @vsi_seid: the initial VSI seid
10316 * @enabled_tc: Enabled TC bit-map
10317 *
10318 * This allocates the sw VEB structure and links it into the switch
10319 * It is possible and legal for this to be a duplicate of an already
10320 * existing VEB. It is also possible for both uplink and vsi seids
10321 * to be zero, in order to create a floating VEB.
10322 *
10323 * Returns pointer to the successfully allocated VEB sw struct on
10324 * success, otherwise returns NULL on failure.
10325 **/
10326struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10327 u16 uplink_seid, u16 vsi_seid,
10328 u8 enabled_tc)
10329{
10330 struct i40e_veb *veb, *uplink_veb = NULL;
10331 int vsi_idx, veb_idx;
10332 int ret;
10333
10334 /* if one seid is 0, the other must be 0 to create a floating relay */
10335 if ((uplink_seid == 0 || vsi_seid == 0) &&
10336 (uplink_seid + vsi_seid != 0)) {
10337 dev_info(&pf->pdev->dev,
10338 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10339 uplink_seid, vsi_seid);
10340 return NULL;
10341 }
10342
10343 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010344 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010345 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10346 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010347 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010348 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10349 vsi_seid);
10350 return NULL;
10351 }
10352
10353 if (uplink_seid && uplink_seid != pf->mac_seid) {
10354 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10355 if (pf->veb[veb_idx] &&
10356 pf->veb[veb_idx]->seid == uplink_seid) {
10357 uplink_veb = pf->veb[veb_idx];
10358 break;
10359 }
10360 }
10361 if (!uplink_veb) {
10362 dev_info(&pf->pdev->dev,
10363 "uplink seid %d not found\n", uplink_seid);
10364 return NULL;
10365 }
10366 }
10367
10368 /* get veb sw struct */
10369 veb_idx = i40e_veb_mem_alloc(pf);
10370 if (veb_idx < 0)
10371 goto err_alloc;
10372 veb = pf->veb[veb_idx];
10373 veb->flags = flags;
10374 veb->uplink_seid = uplink_seid;
10375 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10376 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10377
10378 /* create the VEB in the switch */
10379 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10380 if (ret)
10381 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010382 if (vsi_idx == pf->lan_vsi)
10383 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010384
10385 return veb;
10386
10387err_veb:
10388 i40e_veb_clear(veb);
10389err_alloc:
10390 return NULL;
10391}
10392
10393/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010394 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010395 * @pf: board private structure
10396 * @ele: element we are building info from
10397 * @num_reported: total number of elements
10398 * @printconfig: should we print the contents
10399 *
10400 * helper function to assist in extracting a few useful SEID values.
10401 **/
10402static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10403 struct i40e_aqc_switch_config_element_resp *ele,
10404 u16 num_reported, bool printconfig)
10405{
10406 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10407 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10408 u8 element_type = ele->element_type;
10409 u16 seid = le16_to_cpu(ele->seid);
10410
10411 if (printconfig)
10412 dev_info(&pf->pdev->dev,
10413 "type=%d seid=%d uplink=%d downlink=%d\n",
10414 element_type, seid, uplink_seid, downlink_seid);
10415
10416 switch (element_type) {
10417 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10418 pf->mac_seid = seid;
10419 break;
10420 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10421 /* Main VEB? */
10422 if (uplink_seid != pf->mac_seid)
10423 break;
10424 if (pf->lan_veb == I40E_NO_VEB) {
10425 int v;
10426
10427 /* find existing or else empty VEB */
10428 for (v = 0; v < I40E_MAX_VEB; v++) {
10429 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10430 pf->lan_veb = v;
10431 break;
10432 }
10433 }
10434 if (pf->lan_veb == I40E_NO_VEB) {
10435 v = i40e_veb_mem_alloc(pf);
10436 if (v < 0)
10437 break;
10438 pf->lan_veb = v;
10439 }
10440 }
10441
10442 pf->veb[pf->lan_veb]->seid = seid;
10443 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10444 pf->veb[pf->lan_veb]->pf = pf;
10445 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10446 break;
10447 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10448 if (num_reported != 1)
10449 break;
10450 /* This is immediately after a reset so we can assume this is
10451 * the PF's VSI
10452 */
10453 pf->mac_seid = uplink_seid;
10454 pf->pf_seid = downlink_seid;
10455 pf->main_vsi_seid = seid;
10456 if (printconfig)
10457 dev_info(&pf->pdev->dev,
10458 "pf_seid=%d main_vsi_seid=%d\n",
10459 pf->pf_seid, pf->main_vsi_seid);
10460 break;
10461 case I40E_SWITCH_ELEMENT_TYPE_PF:
10462 case I40E_SWITCH_ELEMENT_TYPE_VF:
10463 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10464 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10465 case I40E_SWITCH_ELEMENT_TYPE_PE:
10466 case I40E_SWITCH_ELEMENT_TYPE_PA:
10467 /* ignore these for now */
10468 break;
10469 default:
10470 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10471 element_type, seid);
10472 break;
10473 }
10474}
10475
10476/**
10477 * i40e_fetch_switch_configuration - Get switch config from firmware
10478 * @pf: board private structure
10479 * @printconfig: should we print the contents
10480 *
10481 * Get the current switch configuration from the device and
10482 * extract a few useful SEID values.
10483 **/
10484int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10485{
10486 struct i40e_aqc_get_switch_config_resp *sw_config;
10487 u16 next_seid = 0;
10488 int ret = 0;
10489 u8 *aq_buf;
10490 int i;
10491
10492 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10493 if (!aq_buf)
10494 return -ENOMEM;
10495
10496 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10497 do {
10498 u16 num_reported, num_total;
10499
10500 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10501 I40E_AQ_LARGE_BUF,
10502 &next_seid, NULL);
10503 if (ret) {
10504 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010505 "get switch config failed err %s aq_err %s\n",
10506 i40e_stat_str(&pf->hw, ret),
10507 i40e_aq_str(&pf->hw,
10508 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010509 kfree(aq_buf);
10510 return -ENOENT;
10511 }
10512
10513 num_reported = le16_to_cpu(sw_config->header.num_reported);
10514 num_total = le16_to_cpu(sw_config->header.num_total);
10515
10516 if (printconfig)
10517 dev_info(&pf->pdev->dev,
10518 "header: %d reported %d total\n",
10519 num_reported, num_total);
10520
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010521 for (i = 0; i < num_reported; i++) {
10522 struct i40e_aqc_switch_config_element_resp *ele =
10523 &sw_config->element[i];
10524
10525 i40e_setup_pf_switch_element(pf, ele, num_reported,
10526 printconfig);
10527 }
10528 } while (next_seid != 0);
10529
10530 kfree(aq_buf);
10531 return ret;
10532}
10533
10534/**
10535 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10536 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010537 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010538 *
10539 * Returns 0 on success, negative value on failure
10540 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010541static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010542{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010543 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010544 int ret;
10545
10546 /* find out what's out there already */
10547 ret = i40e_fetch_switch_configuration(pf, false);
10548 if (ret) {
10549 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010550 "couldn't fetch switch config, err %s aq_err %s\n",
10551 i40e_stat_str(&pf->hw, ret),
10552 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010553 return ret;
10554 }
10555 i40e_pf_reset_stats(pf);
10556
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010557 /* set the switch config bit for the whole device to
10558 * support limited promisc or true promisc
10559 * when user requests promisc. The default is limited
10560 * promisc.
10561 */
10562
10563 if ((pf->hw.pf_id == 0) &&
10564 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10565 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10566
10567 if (pf->hw.pf_id == 0) {
10568 u16 valid_flags;
10569
10570 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10571 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10572 NULL);
10573 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10574 dev_info(&pf->pdev->dev,
10575 "couldn't set switch config bits, err %s aq_err %s\n",
10576 i40e_stat_str(&pf->hw, ret),
10577 i40e_aq_str(&pf->hw,
10578 pf->hw.aq.asq_last_status));
10579 /* not a fatal problem, just keep going */
10580 }
10581 }
10582
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010583 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010584 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010585 struct i40e_vsi *vsi = NULL;
10586 u16 uplink_seid;
10587
10588 /* Set up the PF VSI associated with the PF's main VSI
10589 * that is already in the HW switch
10590 */
10591 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10592 uplink_seid = pf->veb[pf->lan_veb]->seid;
10593 else
10594 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010595 if (pf->lan_vsi == I40E_NO_VSI)
10596 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10597 else if (reinit)
10598 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010599 if (!vsi) {
10600 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10601 i40e_fdir_teardown(pf);
10602 return -EAGAIN;
10603 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010604 } else {
10605 /* force a reset of TC and queue layout configurations */
10606 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010607
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010608 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10609 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10610 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10611 }
10612 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10613
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010614 i40e_fdir_sb_setup(pf);
10615
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010616 /* Setup static PF queue filter control settings */
10617 ret = i40e_setup_pf_filter_control(pf);
10618 if (ret) {
10619 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10620 ret);
10621 /* Failure here should not stop continuing other steps */
10622 }
10623
10624 /* enable RSS in the HW, even for only one queue, as the stack can use
10625 * the hash
10626 */
10627 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010628 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010629
10630 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010631 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010632 i40e_link_event(pf);
10633
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010634 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010635 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10636 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010637
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010638 i40e_ptp_init(pf);
10639
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010640 return ret;
10641}
10642
10643/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010644 * i40e_determine_queue_usage - Work out queue distribution
10645 * @pf: board private structure
10646 **/
10647static void i40e_determine_queue_usage(struct i40e_pf *pf)
10648{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010649 int queues_left;
10650
10651 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010652#ifdef I40E_FCOE
10653 pf->num_fcoe_qps = 0;
10654#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010655
10656 /* Find the max queues to be put into basic use. We'll always be
10657 * using TC0, whether or not DCB is running, and TC0 will get the
10658 * big RSS set.
10659 */
10660 queues_left = pf->hw.func_caps.num_tx_qp;
10661
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010662 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010663 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010664 /* one qp for PF, no queues for anything else */
10665 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010666 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010667
10668 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010669 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010670 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010671#ifdef I40E_FCOE
10672 I40E_FLAG_FCOE_ENABLED |
10673#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010674 I40E_FLAG_FD_SB_ENABLED |
10675 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010676 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010677 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010678 I40E_FLAG_SRIOV_ENABLED |
10679 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010680 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10681 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010682 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010683 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010684 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010685 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010686 queues_left -= pf->num_lan_qps;
10687
10688 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010689 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010690#ifdef I40E_FCOE
10691 I40E_FLAG_FCOE_ENABLED |
10692#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010693 I40E_FLAG_FD_SB_ENABLED |
10694 I40E_FLAG_FD_ATR_ENABLED |
10695 I40E_FLAG_DCB_ENABLED |
10696 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010697 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010698 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010699 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010700 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010701 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10702 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010703 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10704 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010705 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10706 num_online_cpus());
10707 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10708 pf->hw.func_caps.num_tx_qp);
10709
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010710 queues_left -= pf->num_lan_qps;
10711 }
10712
Vasu Dev38e00432014-08-01 13:27:03 -070010713#ifdef I40E_FCOE
10714 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10715 if (I40E_DEFAULT_FCOE <= queues_left) {
10716 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10717 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10718 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10719 } else {
10720 pf->num_fcoe_qps = 0;
10721 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10722 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10723 }
10724
10725 queues_left -= pf->num_fcoe_qps;
10726 }
10727
10728#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010729 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);
Vasu Dev38e00432014-08-01 13:27:03 -070010760#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010761 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010762#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010763}
10764
10765/**
10766 * i40e_setup_pf_filter_control - Setup PF static filter control
10767 * @pf: PF to be setup
10768 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010769 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010770 * settings. If PE/FCoE are enabled then it will also set the per PF
10771 * based filter sizes required for them. It also enables Flow director,
10772 * ethertype and macvlan type filter settings for the pf.
10773 *
10774 * Returns 0 on success, negative on failure
10775 **/
10776static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10777{
10778 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10779
10780 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10781
10782 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010783 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010784 settings->enable_fdir = true;
10785
10786 /* Ethtype and MACVLAN filters enabled for PF */
10787 settings->enable_ethtype = true;
10788 settings->enable_macvlan = true;
10789
10790 if (i40e_set_filter_control(&pf->hw, settings))
10791 return -ENOENT;
10792
10793 return 0;
10794}
10795
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010796#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010797#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010798static void i40e_print_features(struct i40e_pf *pf)
10799{
10800 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010801 char *buf;
10802 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010803
Joe Perches3b195842015-12-03 04:20:57 -080010804 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10805 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010806 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010807
Joe Perches3b195842015-12-03 04:20:57 -080010808 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010809#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010810 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010811#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010812 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010813 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010814 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010815 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010816 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010817 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010818 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010819 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010820 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10821 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010822 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010823 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010824 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010825 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010826 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010827 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010828 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010829#ifdef I40E_FCOE
10830 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010831 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010832#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010833 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010834 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010835 else
Joe Perches3b195842015-12-03 04:20:57 -080010836 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010837
Joe Perches3b195842015-12-03 04:20:57 -080010838 dev_info(&pf->pdev->dev, "%s\n", buf);
10839 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010840 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010841}
10842
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010843/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010844 * i40e_get_platform_mac_addr - get platform-specific MAC address
10845 *
10846 * @pdev: PCI device information struct
10847 * @pf: board private structure
10848 *
10849 * Look up the MAC address in Open Firmware on systems that support it,
10850 * and use IDPROM on SPARC if no OF address is found. On return, the
10851 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10852 * has been selected.
10853 **/
10854static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10855{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010856 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010857 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010858 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010859}
10860
10861/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010862 * i40e_probe - Device initialization routine
10863 * @pdev: PCI device information struct
10864 * @ent: entry in i40e_pci_tbl
10865 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010866 * i40e_probe initializes a PF identified by a pci_dev structure.
10867 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010868 * and a hardware reset occur.
10869 *
10870 * Returns 0 on success, negative on failure
10871 **/
10872static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10873{
Catherine Sullivane8278452015-02-06 08:52:08 +000010874 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010875 struct i40e_pf *pf;
10876 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010877 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010878 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010879 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010880 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010881 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010882 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010883 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010884
10885 err = pci_enable_device_mem(pdev);
10886 if (err)
10887 return err;
10888
10889 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010890 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010891 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010892 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10893 if (err) {
10894 dev_err(&pdev->dev,
10895 "DMA configuration failed: 0x%x\n", err);
10896 goto err_dma;
10897 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010898 }
10899
10900 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010901 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010902 if (err) {
10903 dev_info(&pdev->dev,
10904 "pci_request_selected_regions failed %d\n", err);
10905 goto err_pci_reg;
10906 }
10907
10908 pci_enable_pcie_error_reporting(pdev);
10909 pci_set_master(pdev);
10910
10911 /* Now that we have a PCI connection, we need to do the
10912 * low level device setup. This is primarily setting up
10913 * the Admin Queue structures and then querying for the
10914 * device's current profile information.
10915 */
10916 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10917 if (!pf) {
10918 err = -ENOMEM;
10919 goto err_pf_alloc;
10920 }
10921 pf->next_vsi = 0;
10922 pf->pdev = pdev;
10923 set_bit(__I40E_DOWN, &pf->state);
10924
10925 hw = &pf->hw;
10926 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010927
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010928 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10929 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010930
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010931 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010932 if (!hw->hw_addr) {
10933 err = -EIO;
10934 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10935 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010936 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010937 goto err_ioremap;
10938 }
10939 hw->vendor_id = pdev->vendor;
10940 hw->device_id = pdev->device;
10941 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10942 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10943 hw->subsystem_device_id = pdev->subsystem_device;
10944 hw->bus.device = PCI_SLOT(pdev->devfn);
10945 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010946 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010947
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010948 /* set up the locks for the AQ, do this only once in probe
10949 * and destroy them only once in remove
10950 */
10951 mutex_init(&hw->aq.asq_mutex);
10952 mutex_init(&hw->aq.arq_mutex);
10953
Alexander Duyck5d4ca232016-09-30 08:21:46 -040010954 pf->msg_enable = netif_msg_init(debug,
10955 NETIF_MSG_DRV |
10956 NETIF_MSG_PROBE |
10957 NETIF_MSG_LINK);
10958 if (debug < -1)
10959 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010960
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010961 /* do a special CORER for clearing PXE mode once at init */
10962 if (hw->revision_id == 0 &&
10963 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10964 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10965 i40e_flush(hw);
10966 msleep(200);
10967 pf->corer_count++;
10968
10969 i40e_clear_pxe_mode(hw);
10970 }
10971
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010972 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010973 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010974 err = i40e_pf_reset(hw);
10975 if (err) {
10976 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10977 goto err_pf_reset;
10978 }
10979 pf->pfr_count++;
10980
10981 hw->aq.num_arq_entries = I40E_AQ_LEN;
10982 hw->aq.num_asq_entries = I40E_AQ_LEN;
10983 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10984 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10985 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010986
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010987 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010988 "%s-%s:misc",
10989 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010990
10991 err = i40e_init_shared_code(hw);
10992 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010993 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10994 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010995 goto err_pf_reset;
10996 }
10997
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010998 /* set up a default setting for link flow control */
10999 pf->hw.fc.requested_mode = I40E_FC_NONE;
11000
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011001 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040011002 if (err) {
11003 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11004 dev_info(&pdev->dev,
11005 "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");
11006 else
11007 dev_info(&pdev->dev,
11008 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11009
11010 goto err_pf_reset;
11011 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011012
Shannon Nelson6dec1012015-09-28 14:12:30 -040011013 /* provide nvm, fw, api versions */
11014 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11015 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11016 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11017 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011018
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011019 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
11020 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011021 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011022 "The driver for the device detected a newer version of the NVM image than expected. Please install the most recent version of the network driver.\n");
11023 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
11024 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011025 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011026 "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 +000011027
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011028 i40e_verify_eeprom(pf);
11029
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011030 /* Rev 0 hardware was never productized */
11031 if (hw->revision_id < 1)
11032 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");
11033
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011034 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011035 err = i40e_get_capabilities(pf);
11036 if (err)
11037 goto err_adminq_setup;
11038
11039 err = i40e_sw_init(pf);
11040 if (err) {
11041 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11042 goto err_sw_init;
11043 }
11044
11045 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
11046 hw->func_caps.num_rx_qp,
11047 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
11048 if (err) {
11049 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11050 goto err_init_lan_hmc;
11051 }
11052
11053 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11054 if (err) {
11055 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11056 err = -ENOENT;
11057 goto err_configure_lan_hmc;
11058 }
11059
Neerav Parikhb686ece2014-12-14 01:55:11 +000011060 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11061 * Ignore error return codes because if it was already disabled via
11062 * hardware settings this will fail
11063 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011064 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011065 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11066 i40e_aq_stop_lldp(hw, true, NULL);
11067 }
11068
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011069 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011070 /* allow a platform config to override the HW addr */
11071 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011072 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011073 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11074 err = -EIO;
11075 goto err_mac_addr;
11076 }
11077 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011078 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011079 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11080 if (is_valid_ether_addr(hw->mac.port_addr))
11081 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070011082#ifdef I40E_FCOE
11083 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
11084 if (err)
11085 dev_info(&pdev->dev,
11086 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
11087 if (!is_valid_ether_addr(hw->mac.san_addr)) {
11088 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
11089 hw->mac.san_addr);
11090 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
11091 }
11092 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
11093#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011094
11095 pci_set_drvdata(pdev, pf);
11096 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011097#ifdef CONFIG_I40E_DCB
11098 err = i40e_init_pf_dcb(pf);
11099 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011100 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011101 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011102 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011103 }
11104#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011105
11106 /* set up periodic task facility */
11107 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11108 pf->service_timer_period = HZ;
11109
11110 INIT_WORK(&pf->service_task, i40e_service_task);
11111 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
11112 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011113
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011114 /* NVM bit on means WoL disabled for the port */
11115 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011116 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011117 pf->wol_en = false;
11118 else
11119 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011120 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11121
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011122 /* set up the main switch operations */
11123 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011124 err = i40e_init_interrupt_scheme(pf);
11125 if (err)
11126 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011127
Mitch Williams505682c2014-05-20 08:01:37 +000011128 /* The number of VSIs reported by the FW is the minimum guaranteed
11129 * to us; HW supports far more and we share the remaining pool with
11130 * the other PFs. We allocate space for more than the guarantee with
11131 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011132 */
Mitch Williams505682c2014-05-20 08:01:37 +000011133 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11134 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11135 else
11136 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11137
11138 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011139 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11140 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011141 if (!pf->vsi) {
11142 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011143 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011144 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011145
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011146#ifdef CONFIG_PCI_IOV
11147 /* prep for VF support */
11148 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11149 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11150 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11151 if (pci_num_vf(pdev))
11152 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11153 }
11154#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011155 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011156 if (err) {
11157 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11158 goto err_vsis;
11159 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011160
11161 /* Make sure flow control is set according to current settings */
11162 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11163 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11164 dev_dbg(&pf->pdev->dev,
11165 "Set fc with err %s aq_err %s on get_phy_cap\n",
11166 i40e_stat_str(hw, err),
11167 i40e_aq_str(hw, hw->aq.asq_last_status));
11168 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11169 dev_dbg(&pf->pdev->dev,
11170 "Set fc with err %s aq_err %s on set_phy_config\n",
11171 i40e_stat_str(hw, err),
11172 i40e_aq_str(hw, hw->aq.asq_last_status));
11173 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11174 dev_dbg(&pf->pdev->dev,
11175 "Set fc with err %s aq_err %s on get_link_info\n",
11176 i40e_stat_str(hw, err),
11177 i40e_aq_str(hw, hw->aq.asq_last_status));
11178
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011179 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011180 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011181 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11182 i40e_vsi_open(pf->vsi[i]);
11183 break;
11184 }
11185 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011186
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011187 /* The driver only wants link up/down and module qualification
11188 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011189 */
11190 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011191 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011192 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011193 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011194 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011195 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11196 i40e_stat_str(&pf->hw, err),
11197 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011198
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011199 /* Reconfigure hardware for allowing smaller MSS in the case
11200 * of TSO, so that we avoid the MDD being fired and causing
11201 * a reset in the case of small MSS+TSO.
11202 */
11203 val = rd32(hw, I40E_REG_MSS);
11204 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11205 val &= ~I40E_REG_MSS_MIN_MASK;
11206 val |= I40E_64BYTE_MSS;
11207 wr32(hw, I40E_REG_MSS, val);
11208 }
11209
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011210 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011211 msleep(75);
11212 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11213 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011214 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11215 i40e_stat_str(&pf->hw, err),
11216 i40e_aq_str(&pf->hw,
11217 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011218 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011219 /* The main driver is (mostly) up and happy. We need to set this state
11220 * before setting up the misc vector or we get a race and the vector
11221 * ends up disabled forever.
11222 */
11223 clear_bit(__I40E_DOWN, &pf->state);
11224
11225 /* In case of MSIX we are going to setup the misc vector right here
11226 * to handle admin queue events etc. In case of legacy and MSI
11227 * the misc functionality and queue processing is combined in
11228 * the same vector and that gets setup at open.
11229 */
11230 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11231 err = i40e_setup_misc_vector(pf);
11232 if (err) {
11233 dev_info(&pdev->dev,
11234 "setup of misc vector failed: %d\n", err);
11235 goto err_vsis;
11236 }
11237 }
11238
Greg Rosedf805f62014-04-04 04:43:16 +000011239#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011240 /* prep for VF support */
11241 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011242 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11243 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011244 /* disable link interrupts for VFs */
11245 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11246 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11247 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11248 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011249
11250 if (pci_num_vf(pdev)) {
11251 dev_info(&pdev->dev,
11252 "Active VFs found, allocating resources.\n");
11253 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11254 if (err)
11255 dev_info(&pdev->dev,
11256 "Error %d allocating resources for existing VFs\n",
11257 err);
11258 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011259 }
Greg Rosedf805f62014-04-04 04:43:16 +000011260#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011261
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011262 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11263 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11264 pf->num_iwarp_msix,
11265 I40E_IWARP_IRQ_PILE_ID);
11266 if (pf->iwarp_base_vector < 0) {
11267 dev_info(&pdev->dev,
11268 "failed to get tracking for %d vectors for IWARP err=%d\n",
11269 pf->num_iwarp_msix, pf->iwarp_base_vector);
11270 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11271 }
11272 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011273
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011274 i40e_dbg_pf_init(pf);
11275
11276 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011277 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011278
11279 /* since everything's happy, start the service_task timer */
11280 mod_timer(&pf->service_timer,
11281 round_jiffies(jiffies + pf->service_timer_period));
11282
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011283 /* add this PF to client device list and launch a client service task */
11284 err = i40e_lan_add_device(pf);
11285 if (err)
11286 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11287 err);
11288
Vasu Dev38e00432014-08-01 13:27:03 -070011289#ifdef I40E_FCOE
11290 /* create FCoE interface */
11291 i40e_fcoe_vsi_setup(pf);
11292
11293#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011294#define PCI_SPEED_SIZE 8
11295#define PCI_WIDTH_SIZE 8
11296 /* Devices on the IOSF bus do not have this information
11297 * and will report PCI Gen 1 x 1 by default so don't bother
11298 * checking them.
11299 */
11300 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11301 char speed[PCI_SPEED_SIZE] = "Unknown";
11302 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011303
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011304 /* Get the negotiated link width and speed from PCI config
11305 * space
11306 */
11307 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11308 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011309
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011310 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011311
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011312 switch (hw->bus.speed) {
11313 case i40e_bus_speed_8000:
11314 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11315 case i40e_bus_speed_5000:
11316 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11317 case i40e_bus_speed_2500:
11318 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11319 default:
11320 break;
11321 }
11322 switch (hw->bus.width) {
11323 case i40e_bus_width_pcie_x8:
11324 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11325 case i40e_bus_width_pcie_x4:
11326 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11327 case i40e_bus_width_pcie_x2:
11328 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11329 case i40e_bus_width_pcie_x1:
11330 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11331 default:
11332 break;
11333 }
11334
11335 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11336 speed, width);
11337
11338 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11339 hw->bus.speed < i40e_bus_speed_8000) {
11340 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11341 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11342 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011343 }
11344
Catherine Sullivane8278452015-02-06 08:52:08 +000011345 /* get the requested speeds from the fw */
11346 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11347 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011348 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11349 i40e_stat_str(&pf->hw, err),
11350 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011351 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11352
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011353 /* get the supported phy types from the fw */
11354 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11355 if (err)
11356 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11357 i40e_stat_str(&pf->hw, err),
11358 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11359 pf->hw.phy.phy_types = le32_to_cpu(abilities.phy_type);
11360
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011361 /* Add a filter to drop all Flow control frames from any VSI from being
11362 * transmitted. By doing so we stop a malicious VF from sending out
11363 * PAUSE or PFC frames and potentially controlling traffic for other
11364 * PF/VF VSIs.
11365 * The FW can still send Flow control frames if enabled.
11366 */
11367 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11368 pf->main_vsi_seid);
11369
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011370 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
11371 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11372 pf->flags |= I40E_FLAG_HAVE_10GBASET_PHY;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011373 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11374 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011375 /* print a string summarizing features */
11376 i40e_print_features(pf);
11377
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011378 return 0;
11379
11380 /* Unwind what we've done if something failed in the setup */
11381err_vsis:
11382 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011383 i40e_clear_interrupt_scheme(pf);
11384 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011385err_switch_setup:
11386 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011387 del_timer_sync(&pf->service_timer);
11388err_mac_addr:
11389err_configure_lan_hmc:
11390 (void)i40e_shutdown_lan_hmc(hw);
11391err_init_lan_hmc:
11392 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011393err_sw_init:
11394err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011395err_pf_reset:
11396 iounmap(hw->hw_addr);
11397err_ioremap:
11398 kfree(pf);
11399err_pf_alloc:
11400 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011401 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011402err_pci_reg:
11403err_dma:
11404 pci_disable_device(pdev);
11405 return err;
11406}
11407
11408/**
11409 * i40e_remove - Device removal routine
11410 * @pdev: PCI device information struct
11411 *
11412 * i40e_remove is called by the PCI subsystem to alert the driver
11413 * that is should release a PCI device. This could be caused by a
11414 * Hot-Plug event, or because the driver is going to be removed from
11415 * memory.
11416 **/
11417static void i40e_remove(struct pci_dev *pdev)
11418{
11419 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011420 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011421 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011422 int i;
11423
11424 i40e_dbg_pf_exit(pf);
11425
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011426 i40e_ptp_stop(pf);
11427
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011428 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011429 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11430 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011431
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011432 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011433 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011434 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011435 if (pf->service_timer.data)
11436 del_timer_sync(&pf->service_timer);
11437 if (pf->service_task.func)
11438 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011439
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011440 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11441 i40e_free_vfs(pf);
11442 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11443 }
11444
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011445 i40e_fdir_teardown(pf);
11446
11447 /* If there is a switch structure or any orphans, remove them.
11448 * This will leave only the PF's VSI remaining.
11449 */
11450 for (i = 0; i < I40E_MAX_VEB; i++) {
11451 if (!pf->veb[i])
11452 continue;
11453
11454 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11455 pf->veb[i]->uplink_seid == 0)
11456 i40e_switch_branch_release(pf->veb[i]);
11457 }
11458
11459 /* Now we can shutdown the PF's VSI, just before we kill
11460 * adminq and hmc.
11461 */
11462 if (pf->vsi[pf->lan_vsi])
11463 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11464
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011465 /* remove attached clients */
11466 ret_code = i40e_lan_del_device(pf);
11467 if (ret_code) {
11468 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11469 ret_code);
11470 }
11471
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011472 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011473 if (hw->hmc.hmc_obj) {
11474 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011475 if (ret_code)
11476 dev_warn(&pdev->dev,
11477 "Failed to destroy the HMC resources: %d\n",
11478 ret_code);
11479 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011480
11481 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011482 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011483
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011484 /* destroy the locks only once, here */
11485 mutex_destroy(&hw->aq.arq_mutex);
11486 mutex_destroy(&hw->aq.asq_mutex);
11487
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011488 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11489 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011490 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011491 if (pf->vsi[i]) {
11492 i40e_vsi_clear_rings(pf->vsi[i]);
11493 i40e_vsi_clear(pf->vsi[i]);
11494 pf->vsi[i] = NULL;
11495 }
11496 }
11497
11498 for (i = 0; i < I40E_MAX_VEB; i++) {
11499 kfree(pf->veb[i]);
11500 pf->veb[i] = NULL;
11501 }
11502
11503 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011504 kfree(pf->vsi);
11505
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011506 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011507 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011508 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011509
11510 pci_disable_pcie_error_reporting(pdev);
11511 pci_disable_device(pdev);
11512}
11513
11514/**
11515 * i40e_pci_error_detected - warning that something funky happened in PCI land
11516 * @pdev: PCI device information struct
11517 *
11518 * Called to warn that something happened and the error handling steps
11519 * are in progress. Allows the driver to quiesce things, be ready for
11520 * remediation.
11521 **/
11522static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11523 enum pci_channel_state error)
11524{
11525 struct i40e_pf *pf = pci_get_drvdata(pdev);
11526
11527 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11528
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011529 if (!pf) {
11530 dev_info(&pdev->dev,
11531 "Cannot recover - error happened during device probe\n");
11532 return PCI_ERS_RESULT_DISCONNECT;
11533 }
11534
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011535 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011536 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11537 rtnl_lock();
11538 i40e_prep_for_reset(pf);
11539 rtnl_unlock();
11540 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011541
11542 /* Request a slot reset */
11543 return PCI_ERS_RESULT_NEED_RESET;
11544}
11545
11546/**
11547 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11548 * @pdev: PCI device information struct
11549 *
11550 * Called to find if the driver can work with the device now that
11551 * the pci slot has been reset. If a basic connection seems good
11552 * (registers are readable and have sane content) then return a
11553 * happy little PCI_ERS_RESULT_xxx.
11554 **/
11555static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11556{
11557 struct i40e_pf *pf = pci_get_drvdata(pdev);
11558 pci_ers_result_t result;
11559 int err;
11560 u32 reg;
11561
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011562 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011563 if (pci_enable_device_mem(pdev)) {
11564 dev_info(&pdev->dev,
11565 "Cannot re-enable PCI device after reset.\n");
11566 result = PCI_ERS_RESULT_DISCONNECT;
11567 } else {
11568 pci_set_master(pdev);
11569 pci_restore_state(pdev);
11570 pci_save_state(pdev);
11571 pci_wake_from_d3(pdev, false);
11572
11573 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11574 if (reg == 0)
11575 result = PCI_ERS_RESULT_RECOVERED;
11576 else
11577 result = PCI_ERS_RESULT_DISCONNECT;
11578 }
11579
11580 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11581 if (err) {
11582 dev_info(&pdev->dev,
11583 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11584 err);
11585 /* non-fatal, continue */
11586 }
11587
11588 return result;
11589}
11590
11591/**
11592 * i40e_pci_error_resume - restart operations after PCI error recovery
11593 * @pdev: PCI device information struct
11594 *
11595 * Called to allow the driver to bring things back up after PCI error
11596 * and/or reset recovery has finished.
11597 **/
11598static void i40e_pci_error_resume(struct pci_dev *pdev)
11599{
11600 struct i40e_pf *pf = pci_get_drvdata(pdev);
11601
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011602 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011603 if (test_bit(__I40E_SUSPENDED, &pf->state))
11604 return;
11605
11606 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011607 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011608 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011609}
11610
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011611/**
11612 * i40e_shutdown - PCI callback for shutting down
11613 * @pdev: PCI device information struct
11614 **/
11615static void i40e_shutdown(struct pci_dev *pdev)
11616{
11617 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011618 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011619
11620 set_bit(__I40E_SUSPENDED, &pf->state);
11621 set_bit(__I40E_DOWN, &pf->state);
11622 rtnl_lock();
11623 i40e_prep_for_reset(pf);
11624 rtnl_unlock();
11625
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011626 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11627 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11628
Catherine Sullivan02b42492015-07-10 19:35:59 -040011629 del_timer_sync(&pf->service_timer);
11630 cancel_work_sync(&pf->service_task);
11631 i40e_fdir_teardown(pf);
11632
11633 rtnl_lock();
11634 i40e_prep_for_reset(pf);
11635 rtnl_unlock();
11636
11637 wr32(hw, I40E_PFPM_APM,
11638 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11639 wr32(hw, I40E_PFPM_WUFC,
11640 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11641
Shannon Nelsone1477582015-02-21 06:44:33 +000011642 i40e_clear_interrupt_scheme(pf);
11643
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011644 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011645 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011646 pci_set_power_state(pdev, PCI_D3hot);
11647 }
11648}
11649
11650#ifdef CONFIG_PM
11651/**
11652 * i40e_suspend - PCI callback for moving to D3
11653 * @pdev: PCI device information struct
11654 **/
11655static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11656{
11657 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011658 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011659 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011660
11661 set_bit(__I40E_SUSPENDED, &pf->state);
11662 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011663
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011664 rtnl_lock();
11665 i40e_prep_for_reset(pf);
11666 rtnl_unlock();
11667
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011668 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11669 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11670
Greg Roseb33d3b72016-05-16 10:26:37 -070011671 i40e_stop_misc_vector(pf);
11672
Greg Rose059ff692016-05-16 10:26:38 -070011673 retval = pci_save_state(pdev);
11674 if (retval)
11675 return retval;
11676
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011677 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011678 pci_set_power_state(pdev, PCI_D3hot);
11679
Greg Rose059ff692016-05-16 10:26:38 -070011680 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011681}
11682
11683/**
11684 * i40e_resume - PCI callback for waking up from D3
11685 * @pdev: PCI device information struct
11686 **/
11687static int i40e_resume(struct pci_dev *pdev)
11688{
11689 struct i40e_pf *pf = pci_get_drvdata(pdev);
11690 u32 err;
11691
11692 pci_set_power_state(pdev, PCI_D0);
11693 pci_restore_state(pdev);
11694 /* pci_restore_state() clears dev->state_saves, so
11695 * call pci_save_state() again to restore it.
11696 */
11697 pci_save_state(pdev);
11698
11699 err = pci_enable_device_mem(pdev);
11700 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011701 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011702 return err;
11703 }
11704 pci_set_master(pdev);
11705
11706 /* no wakeup events while running */
11707 pci_wake_from_d3(pdev, false);
11708
11709 /* handling the reset will rebuild the device state */
11710 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11711 clear_bit(__I40E_DOWN, &pf->state);
11712 rtnl_lock();
11713 i40e_reset_and_rebuild(pf, false);
11714 rtnl_unlock();
11715 }
11716
11717 return 0;
11718}
11719
11720#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011721static const struct pci_error_handlers i40e_err_handler = {
11722 .error_detected = i40e_pci_error_detected,
11723 .slot_reset = i40e_pci_error_slot_reset,
11724 .resume = i40e_pci_error_resume,
11725};
11726
11727static struct pci_driver i40e_driver = {
11728 .name = i40e_driver_name,
11729 .id_table = i40e_pci_tbl,
11730 .probe = i40e_probe,
11731 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011732#ifdef CONFIG_PM
11733 .suspend = i40e_suspend,
11734 .resume = i40e_resume,
11735#endif
11736 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011737 .err_handler = &i40e_err_handler,
11738 .sriov_configure = i40e_pci_sriov_configure,
11739};
11740
11741/**
11742 * i40e_init_module - Driver registration routine
11743 *
11744 * i40e_init_module is the first routine called when the driver is
11745 * loaded. All it does is register with the PCI subsystem.
11746 **/
11747static int __init i40e_init_module(void)
11748{
11749 pr_info("%s: %s - version %s\n", i40e_driver_name,
11750 i40e_driver_string, i40e_driver_version_str);
11751 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011752
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011753 /* we will see if single thread per module is enough for now,
11754 * it can't be any worse than using the system workqueue which
11755 * was already single threaded
11756 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011757 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11758 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011759 if (!i40e_wq) {
11760 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11761 return -ENOMEM;
11762 }
11763
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011764 i40e_dbg_init();
11765 return pci_register_driver(&i40e_driver);
11766}
11767module_init(i40e_init_module);
11768
11769/**
11770 * i40e_exit_module - Driver exit cleanup routine
11771 *
11772 * i40e_exit_module is called just before the driver is removed
11773 * from memory.
11774 **/
11775static void __exit i40e_exit_module(void)
11776{
11777 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011778 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011779 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011780}
11781module_exit(i40e_exit_module);