blob: 6cf9089531859344390b6019179db1e9ef377499 [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{
1201 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001202 struct hlist_node *h;
1203 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001204
1205 /* Only -1 for all the filters denotes not in vlan mode
1206 * so we have to go through all the list in order to make sure
1207 */
Jacob Keller278e7d02016-10-05 09:30:37 -07001208 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Greg Rosed9b68f82015-07-23 16:54:31 -04001209 if (f->vlan >= 0 || vsi->info.pvid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001210 return true;
1211 }
1212
1213 return false;
1214}
1215
1216/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001217 * i40e_add_filter - Add a mac/vlan filter to the VSI
1218 * @vsi: the VSI to be searched
1219 * @macaddr: the MAC address
1220 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001221 *
1222 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001223 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001224 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001225 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001226 **/
1227struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001228 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001229{
1230 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001231 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001232
1233 if (!vsi || !macaddr)
1234 return NULL;
1235
Kiran Patilf6bd0962016-06-20 09:10:34 -07001236 /* Do not allow broadcast filter to be added since broadcast filter
1237 * is added as part of add VSI for any newly created VSI except
1238 * FDIR VSI
1239 */
1240 if (is_broadcast_ether_addr(macaddr))
1241 return NULL;
1242
Jacob Keller1bc87e82016-10-05 09:30:31 -07001243 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001244 if (!f) {
1245 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1246 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001247 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001248
Greg Rose9a173902014-05-22 06:32:02 +00001249 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001250 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001251 /* If we're in overflow promisc mode, set the state directly
1252 * to failed, so we don't bother to try sending the filter
1253 * to the hardware.
1254 */
1255 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1256 f->state = I40E_FILTER_FAILED;
1257 else
1258 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001259 INIT_HLIST_NODE(&f->hlist);
1260
1261 key = i40e_addr_to_hkey(macaddr);
1262 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001263
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001264 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1265 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1266 }
1267
Jacob Keller1bc87e82016-10-05 09:30:31 -07001268 /* If we're asked to add a filter that has been marked for removal, it
1269 * is safe to simply restore it to active state. __i40e_del_filter
1270 * will have simply deleted any filters which were previously marked
1271 * NEW or FAILED, so if it is currently marked REMOVE it must have
1272 * previously been ACTIVE. Since we haven't yet run the sync filters
1273 * task, just restore this filter to the ACTIVE state so that the
1274 * sync task leaves it in place
1275 */
1276 if (f->state == I40E_FILTER_REMOVE)
1277 f->state = I40E_FILTER_ACTIVE;
1278
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001279 return f;
1280}
1281
1282/**
Jacob Keller290d2552016-10-05 09:30:36 -07001283 * __i40e_del_filter - Remove a specific filter from the VSI
1284 * @vsi: VSI to remove from
1285 * @f: the filter to remove from the list
1286 *
1287 * This function should be called instead of i40e_del_filter only if you know
1288 * the exact filter you will remove already, such as via i40e_find_filter or
1289 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001290 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001291 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001292 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001293 * ANOTHER NOTE: This function MUST be called from within the context of
1294 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1295 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001296 **/
Jacob Keller290d2552016-10-05 09:30:36 -07001297static void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001298{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001299 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001300 return;
1301
Jacob Keller1bc87e82016-10-05 09:30:31 -07001302 if ((f->state == I40E_FILTER_FAILED) ||
1303 (f->state == I40E_FILTER_NEW)) {
1304 /* this one never got added by the FW. Just remove it,
1305 * no need to sync anything.
1306 */
Jacob Keller278e7d02016-10-05 09:30:37 -07001307 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001308 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001309 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001310 f->state = I40E_FILTER_REMOVE;
1311 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1312 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001313 }
1314}
1315
1316/**
Jacob Keller290d2552016-10-05 09:30:36 -07001317 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1318 * @vsi: the VSI to be searched
1319 * @macaddr: the MAC address
1320 * @vlan: the VLAN
1321 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001322 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001323 * being held.
1324 * ANOTHER NOTE: This function MUST be called from within the context of
1325 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1326 * instead of list_for_each_entry().
1327 **/
1328void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1329{
1330 struct i40e_mac_filter *f;
1331
1332 if (!vsi || !macaddr)
1333 return;
1334
1335 f = i40e_find_filter(vsi, macaddr, vlan);
1336 __i40e_del_filter(vsi, f);
1337}
1338
1339/**
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001340 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1341 * @vsi: the VSI to be searched
1342 * @macaddr: the mac address to be filtered
1343 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001344 * Goes through all the macvlan filters and adds a macvlan filter for each
1345 * unique vlan that already exists. If a PVID has been assigned, instead only
1346 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001347 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001348 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001349 **/
1350struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi,
1351 const u8 *macaddr)
1352{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001353 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001354 struct hlist_node *h;
1355 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001356
1357 if (vsi->info.pvid)
1358 return i40e_add_filter(vsi, macaddr,
1359 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001360
Jacob Keller278e7d02016-10-05 09:30:37 -07001361 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001362 if (f->state == I40E_FILTER_REMOVE)
1363 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001364 add = i40e_add_filter(vsi, macaddr, f->vlan);
1365 if (!add)
1366 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001367 }
1368
Jacob Keller5feb3d72016-10-05 09:30:34 -07001369 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001370}
1371
1372/**
1373 * i40e_del_mac_all_vlan - Remove a MAC filter from all VLANS
1374 * @vsi: the VSI to be searched
1375 * @macaddr: the mac address to be removed
1376 *
1377 * Removes a given MAC address from a VSI, regardless of VLAN
1378 *
1379 * Returns 0 for success, or error
1380 **/
1381int i40e_del_mac_all_vlan(struct i40e_vsi *vsi, const u8 *macaddr)
1382{
Jacob Keller278e7d02016-10-05 09:30:37 -07001383 struct i40e_mac_filter *f;
1384 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001385 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001386 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001387
Jacob Keller278e7d02016-10-05 09:30:37 -07001388 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1389 "Missing mac_filter_hash_lock\n");
1390 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001391 if (ether_addr_equal(macaddr, f->macaddr)) {
1392 __i40e_del_filter(vsi, f);
1393 found = true;
1394 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001395 }
Jacob Keller290d2552016-10-05 09:30:36 -07001396
1397 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001398 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001399 else
1400 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001401}
1402
1403/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001404 * i40e_set_mac - NDO callback to set mac address
1405 * @netdev: network interface device structure
1406 * @p: pointer to an address structure
1407 *
1408 * Returns 0 on success, negative on failure
1409 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001410#ifdef I40E_FCOE
1411int i40e_set_mac(struct net_device *netdev, void *p)
1412#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001413static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001414#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001415{
1416 struct i40e_netdev_priv *np = netdev_priv(netdev);
1417 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001418 struct i40e_pf *pf = vsi->back;
1419 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001420 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001421
1422 if (!is_valid_ether_addr(addr->sa_data))
1423 return -EADDRNOTAVAIL;
1424
Shannon Nelson30650cc2014-07-29 04:01:50 +00001425 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1426 netdev_info(netdev, "already using mac address %pM\n",
1427 addr->sa_data);
1428 return 0;
1429 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001430
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001431 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1432 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1433 return -EADDRNOTAVAIL;
1434
Shannon Nelson30650cc2014-07-29 04:01:50 +00001435 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1436 netdev_info(netdev, "returning to hw mac address %pM\n",
1437 hw->mac.addr);
1438 else
1439 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1440
Jacob Keller278e7d02016-10-05 09:30:37 -07001441 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001442 i40e_del_mac_all_vlan(vsi, netdev->dev_addr);
1443 i40e_put_mac_in_vlan(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001444 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001445 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001446 if (vsi->type == I40E_VSI_MAIN) {
1447 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001448
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001449 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001450 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001451 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001452 if (ret)
1453 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1454 i40e_stat_str(hw, ret),
1455 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001456 }
1457
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001458 /* schedule our worker thread which will take care of
1459 * applying the new filter changes
1460 */
1461 i40e_service_event_schedule(vsi->back);
1462 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001463}
1464
1465/**
1466 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1467 * @vsi: the VSI being setup
1468 * @ctxt: VSI context structure
1469 * @enabled_tc: Enabled TCs bitmap
1470 * @is_add: True if called before Add VSI
1471 *
1472 * Setup VSI queue mapping for enabled traffic classes.
1473 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001474#ifdef I40E_FCOE
1475void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1476 struct i40e_vsi_context *ctxt,
1477 u8 enabled_tc,
1478 bool is_add)
1479#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001480static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1481 struct i40e_vsi_context *ctxt,
1482 u8 enabled_tc,
1483 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001484#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001485{
1486 struct i40e_pf *pf = vsi->back;
1487 u16 sections = 0;
1488 u8 netdev_tc = 0;
1489 u16 numtc = 0;
1490 u16 qcount;
1491 u8 offset;
1492 u16 qmap;
1493 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001494 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001495
1496 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1497 offset = 0;
1498
1499 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1500 /* Find numtc from enabled TC bitmap */
1501 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001502 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001503 numtc++;
1504 }
1505 if (!numtc) {
1506 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1507 numtc = 1;
1508 }
1509 } else {
1510 /* At least TC0 is enabled in case of non-DCB case */
1511 numtc = 1;
1512 }
1513
1514 vsi->tc_config.numtc = numtc;
1515 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001516 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001517 qcount = vsi->alloc_queue_pairs;
1518
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001519 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001520 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001521
1522 /* Setup queue offset/count for all TCs for given VSI */
1523 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1524 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001525 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001526 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001527 int pow, num_qps;
1528
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001529 switch (vsi->type) {
1530 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001531 qcount = min_t(int, pf->alloc_rss_size,
1532 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001533 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001534#ifdef I40E_FCOE
1535 case I40E_VSI_FCOE:
1536 qcount = num_tc_qps;
1537 break;
1538#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001539 case I40E_VSI_FDIR:
1540 case I40E_VSI_SRIOV:
1541 case I40E_VSI_VMDQ2:
1542 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001543 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001544 WARN_ON(i != 0);
1545 break;
1546 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001547 vsi->tc_config.tc_info[i].qoffset = offset;
1548 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001549
Shannon Nelson1e200e42015-02-27 09:15:24 +00001550 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001551 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001552 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001553 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001554 pow++;
1555 num_qps >>= 1;
1556 }
1557
1558 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1559 qmap =
1560 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1561 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1562
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001563 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001564 } else {
1565 /* TC is not enabled so set the offset to
1566 * default queue and allocate one queue
1567 * for the given TC.
1568 */
1569 vsi->tc_config.tc_info[i].qoffset = 0;
1570 vsi->tc_config.tc_info[i].qcount = 1;
1571 vsi->tc_config.tc_info[i].netdev_tc = 0;
1572
1573 qmap = 0;
1574 }
1575 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1576 }
1577
1578 /* Set actual Tx/Rx queue pairs */
1579 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001580 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1581 if (vsi->req_queue_pairs > 0)
1582 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001583 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001584 vsi->num_queue_pairs = pf->num_lan_msix;
1585 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001586
1587 /* Scheduler section valid can only be set for ADD VSI */
1588 if (is_add) {
1589 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1590
1591 ctxt->info.up_enable_bits = enabled_tc;
1592 }
1593 if (vsi->type == I40E_VSI_SRIOV) {
1594 ctxt->info.mapping_flags |=
1595 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1596 for (i = 0; i < vsi->num_queue_pairs; i++)
1597 ctxt->info.queue_mapping[i] =
1598 cpu_to_le16(vsi->base_queue + i);
1599 } else {
1600 ctxt->info.mapping_flags |=
1601 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1602 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1603 }
1604 ctxt->info.valid_sections |= cpu_to_le16(sections);
1605}
1606
1607/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001608 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1609 * @netdev: the netdevice
1610 * @addr: address to add
1611 *
1612 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1613 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1614 */
1615static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1616{
1617 struct i40e_netdev_priv *np = netdev_priv(netdev);
1618 struct i40e_vsi *vsi = np->vsi;
1619 struct i40e_mac_filter *f;
1620
1621 if (i40e_is_vsi_in_vlan(vsi))
1622 f = i40e_put_mac_in_vlan(vsi, addr);
1623 else
1624 f = i40e_add_filter(vsi, addr, I40E_VLAN_ANY);
1625
1626 if (f)
1627 return 0;
1628 else
1629 return -ENOMEM;
1630}
1631
1632/**
1633 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1634 * @netdev: the netdevice
1635 * @addr: address to add
1636 *
1637 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1638 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1639 */
1640static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1641{
1642 struct i40e_netdev_priv *np = netdev_priv(netdev);
1643 struct i40e_vsi *vsi = np->vsi;
1644
1645 if (i40e_is_vsi_in_vlan(vsi))
1646 i40e_del_mac_all_vlan(vsi, addr);
1647 else
1648 i40e_del_filter(vsi, addr, I40E_VLAN_ANY);
1649
1650 return 0;
1651}
1652
1653/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001654 * i40e_set_rx_mode - NDO callback to set the netdev filters
1655 * @netdev: network interface device structure
1656 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001657#ifdef I40E_FCOE
1658void i40e_set_rx_mode(struct net_device *netdev)
1659#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001660static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001661#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001662{
1663 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001664 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001665
Jacob Keller278e7d02016-10-05 09:30:37 -07001666 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001667
Jacob Keller6622f5c2016-10-05 09:30:32 -07001668 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1669 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001670
Jacob Keller278e7d02016-10-05 09:30:37 -07001671 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001672
1673 /* check for other flag changes */
1674 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1675 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1676 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1677 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001678
1679 /* schedule our worker thread which will take care of
1680 * applying the new filter changes
1681 */
1682 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001683}
1684
1685/**
Jacob Keller4a2ce272016-10-05 09:30:38 -07001686 * i40e_undo_filter_entries - Undo the changes made to MAC filter entries
1687 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001688 * @from: Pointer to list which contains MAC filter entries - changes to
1689 * those entries needs to be undone.
1690 *
Jacob Keller4a2ce272016-10-05 09:30:38 -07001691 * MAC filter entries from list were slated to be sent to firmware, either for
1692 * addition or deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001693 **/
Jacob Keller4a2ce272016-10-05 09:30:38 -07001694static void i40e_undo_filter_entries(struct i40e_vsi *vsi,
1695 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001696{
Jacob Keller278e7d02016-10-05 09:30:37 -07001697 struct i40e_mac_filter *f;
1698 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001699
Jacob Keller278e7d02016-10-05 09:30:37 -07001700 hlist_for_each_entry_safe(f, h, from, hlist) {
1701 u64 key = i40e_addr_to_hkey(f->macaddr);
1702
Kiran Patil21659032015-09-30 14:09:03 -04001703 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001704 hlist_del(&f->hlist);
1705 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001706 }
1707}
1708
1709/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001710 * i40e_update_filter_state - Update filter state based on return data
1711 * from firmware
1712 * @count: Number of filters added
1713 * @add_list: return data from fw
1714 * @head: pointer to first filter in current batch
1715 * @aq_err: status from fw
Kiran Patil21659032015-09-30 14:09:03 -04001716 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001717 * MAC filter entries from list were slated to be added to device. Returns
1718 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001719 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001720static int
1721i40e_update_filter_state(int count,
1722 struct i40e_aqc_add_macvlan_element_data *add_list,
1723 struct i40e_mac_filter *add_head, int aq_err)
Kiran Patil21659032015-09-30 14:09:03 -04001724{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001725 int retval = 0;
1726 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001727
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001728
1729 if (!aq_err) {
1730 retval = count;
1731 /* Everything's good, mark all filters active. */
1732 for (i = 0; i < count ; i++) {
1733 add_head->state = I40E_FILTER_ACTIVE;
Jacob Keller278e7d02016-10-05 09:30:37 -07001734 add_head = hlist_entry(add_head->hlist.next,
1735 typeof(struct i40e_mac_filter),
1736 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001737 }
1738 } else if (aq_err == I40E_AQ_RC_ENOSPC) {
1739 /* Device ran out of filter space. Check the return value
1740 * for each filter to see which ones are active.
1741 */
1742 for (i = 0; i < count ; i++) {
1743 if (add_list[i].match_method ==
1744 I40E_AQC_MM_ERR_NO_RES) {
1745 add_head->state = I40E_FILTER_FAILED;
1746 } else {
1747 add_head->state = I40E_FILTER_ACTIVE;
1748 retval++;
1749 }
Jacob Keller278e7d02016-10-05 09:30:37 -07001750 add_head = hlist_entry(add_head->hlist.next,
1751 typeof(struct i40e_mac_filter),
1752 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001753 }
1754 } else {
1755 /* Some other horrible thing happened, fail all filters */
1756 retval = 0;
1757 for (i = 0; i < count ; i++) {
1758 add_head->state = I40E_FILTER_FAILED;
Jacob Keller278e7d02016-10-05 09:30:37 -07001759 add_head = hlist_entry(add_head->hlist.next,
1760 typeof(struct i40e_mac_filter),
1761 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001762 }
Kiran Patil21659032015-09-30 14:09:03 -04001763 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001764 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001765}
1766
1767/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001768 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1769 * @vsi: ptr to the VSI
1770 *
1771 * Push any outstanding VSI filter changes through the AdminQ.
1772 *
1773 * Returns 0 or error value
1774 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001775int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001776{
Jacob Keller278e7d02016-10-05 09:30:37 -07001777 struct hlist_head tmp_add_list, tmp_del_list;
Alan Brady84f5ca62016-10-05 09:30:39 -07001778 struct i40e_mac_filter *f, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001779 struct i40e_hw *hw = &vsi->back->hw;
Alan Brady84f5ca62016-10-05 09:30:39 -07001780 unsigned int vlan_any_filters = 0;
1781 unsigned int non_vlan_filters = 0;
1782 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001783 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07001784 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001785 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001786 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001787 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07001788 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001789 struct i40e_pf *pf;
1790 int num_add = 0;
1791 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001792 int retval = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001793 int aq_err = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001794 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001795 int list_size;
1796 int fcnt;
Jacob Keller278e7d02016-10-05 09:30:37 -07001797 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001798
1799 /* empty array typed pointers, kcalloc later */
1800 struct i40e_aqc_add_macvlan_element_data *add_list;
1801 struct i40e_aqc_remove_macvlan_element_data *del_list;
1802
1803 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1804 usleep_range(1000, 2000);
1805 pf = vsi->back;
1806
1807 if (vsi->netdev) {
1808 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1809 vsi->current_netdev_flags = vsi->netdev->flags;
1810 }
1811
Jacob Keller278e7d02016-10-05 09:30:37 -07001812 INIT_HLIST_HEAD(&tmp_add_list);
1813 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04001814
Shannon Nelson2d1de822016-05-16 10:26:44 -07001815 if (vsi->type == I40E_VSI_SRIOV)
1816 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
1817 else if (vsi->type != I40E_VSI_MAIN)
1818 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
1819
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001820 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1821 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1822
Jacob Keller278e7d02016-10-05 09:30:37 -07001823 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001824 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07001825 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001826 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001827 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07001828 hash_del(&f->hlist);
1829 hlist_add_head(&f->hlist, &tmp_del_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001830 vsi->active_filters--;
Alan Brady84f5ca62016-10-05 09:30:39 -07001831
1832 /* Avoid counting removed filters */
1833 continue;
Kiran Patil21659032015-09-30 14:09:03 -04001834 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001835 if (f->state == I40E_FILTER_NEW) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001836 hash_del(&f->hlist);
1837 hlist_add_head(&f->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001838 }
Alan Brady84f5ca62016-10-05 09:30:39 -07001839
1840 /* Count the number of each type of filter we have
1841 * remaining, ignoring any filters we're about to
1842 * delete.
1843 */
1844 if (f->vlan > 0)
1845 vlan_filters++;
1846 else if (!f->vlan)
1847 non_vlan_filters++;
1848 else
1849 vlan_any_filters++;
1850 }
1851
1852 /* We should never have VLAN=-1 filters at the same time as we
1853 * have either VLAN=0 or VLAN>0 filters, so warn about this
1854 * case here to help catch any issues.
1855 */
1856 WARN_ON(vlan_any_filters && (vlan_filters + non_vlan_filters));
1857
1858 /* If we only have VLAN=0 filters remaining, and don't have
1859 * any other VLAN filters, we need to convert these VLAN=0
1860 * filters into VLAN=-1 (I40E_VLAN_ANY) so that we operate
1861 * correctly in non-VLAN mode and receive all traffic tagged
1862 * or untagged.
1863 */
1864 if (non_vlan_filters && !vlan_filters) {
1865 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
1866 hlist) {
1867 /* Only replace VLAN=0 filters */
1868 if (f->vlan)
1869 continue;
1870
1871 /* Allocate a replacement element */
1872 add_head = kzalloc(sizeof(*add_head),
1873 GFP_KERNEL);
1874 if (!add_head)
1875 goto err_no_memory_locked;
1876
1877 /* Copy the filter, with new state and VLAN */
1878 *add_head = *f;
1879 add_head->state = I40E_FILTER_NEW;
1880 add_head->vlan = I40E_VLAN_ANY;
1881
1882 /* Move the replacement to the add list */
1883 INIT_HLIST_NODE(&add_head->hlist);
1884 hlist_add_head(&add_head->hlist,
1885 &tmp_add_list);
1886
1887 /* Move the original to the delete list */
1888 f->state = I40E_FILTER_REMOVE;
1889 hash_del(&f->hlist);
1890 hlist_add_head(&f->hlist, &tmp_del_list);
1891 vsi->active_filters--;
1892 }
1893
1894 /* Also update any filters on the tmp_add list */
1895 hlist_for_each_entry(f, &tmp_add_list, hlist) {
1896 if (!f->vlan)
1897 f->vlan = I40E_VLAN_ANY;
1898 }
1899 add_head = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04001900 }
Jacob Keller278e7d02016-10-05 09:30:37 -07001901 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001902 }
1903
1904 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07001905 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001906 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04001907 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001908 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08001909 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001910 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07001911 if (!del_list)
1912 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04001913
Jacob Keller278e7d02016-10-05 09:30:37 -07001914 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001915 cmd_flags = 0;
1916
1917 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001918 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001919 if (f->vlan == I40E_VLAN_ANY) {
1920 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07001921 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001922 } else {
1923 del_list[num_del].vlan_tag =
1924 cpu_to_le16((u16)(f->vlan));
1925 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001926
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001927 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1928 del_list[num_del].flags = cmd_flags;
1929 num_del++;
1930
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001931 /* flush a full buffer */
1932 if (num_del == filter_list_len) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001933 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid,
1934 del_list,
1935 num_del, NULL);
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001936 aq_err = hw->aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001937 num_del = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001938 memset(del_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001939
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001940 /* Explicitly ignore and do not report when
1941 * firmware returns ENOENT.
1942 */
1943 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
Mitch Williamsea02e902015-11-09 15:35:50 -08001944 retval = -EIO;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001945 dev_info(&pf->pdev->dev,
1946 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
Shannon Nelson2d1de822016-05-16 10:26:44 -07001947 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001948 i40e_stat_str(hw, aq_ret),
1949 i40e_aq_str(hw, aq_err));
Mitch Williamsea02e902015-11-09 15:35:50 -08001950 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001951 }
Kiran Patil21659032015-09-30 14:09:03 -04001952 /* Release memory for MAC filter entries which were
1953 * synced up with HW.
1954 */
Jacob Keller278e7d02016-10-05 09:30:37 -07001955 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04001956 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001957 }
Kiran Patil21659032015-09-30 14:09:03 -04001958
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001959 if (num_del) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001960 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, del_list,
1961 num_del, NULL);
1962 aq_err = hw->aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001963 num_del = 0;
1964
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001965 /* Explicitly ignore and do not report when firmware
1966 * returns ENOENT.
1967 */
1968 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1969 retval = -EIO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001970 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07001971 "ignoring delete macvlan error on %s, err %s aq_err %s\n",
1972 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001973 i40e_stat_str(hw, aq_ret),
1974 i40e_aq_str(hw, aq_err));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001975 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001976 }
1977
1978 kfree(del_list);
1979 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04001980 }
1981
Jacob Keller278e7d02016-10-05 09:30:37 -07001982 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001983 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001984 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08001985 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001986 list_size = filter_list_len *
1987 sizeof(struct i40e_aqc_add_macvlan_element_data);
1988 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07001989 if (!add_list)
1990 goto err_no_memory;
1991
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001992 num_add = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07001993 hlist_for_each_entry(f, &tmp_add_list, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001994 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1995 &vsi->state)) {
1996 f->state = I40E_FILTER_FAILED;
1997 continue;
1998 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001999 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002000 if (num_add == 0)
2001 add_head = f;
2002 cmd_flags = 0;
Greg Rose9a173902014-05-22 06:32:02 +00002003 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002004 if (f->vlan == I40E_VLAN_ANY) {
2005 add_list[num_add].vlan_tag = 0;
2006 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2007 } else {
2008 add_list[num_add].vlan_tag =
2009 cpu_to_le16((u16)(f->vlan));
2010 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002011 add_list[num_add].queue_number = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002012 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002013 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2014 num_add++;
2015
2016 /* flush a full buffer */
2017 if (num_add == filter_list_len) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002018 aq_ret = i40e_aq_add_macvlan(hw, vsi->seid,
Mitch Williamsea02e902015-11-09 15:35:50 -08002019 add_list, num_add,
2020 NULL);
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002021 aq_err = hw->aq.asq_last_status;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002022 fcnt = i40e_update_filter_state(num_add,
2023 add_list,
2024 add_head,
2025 aq_ret);
2026 vsi->active_filters += fcnt;
2027
2028 if (fcnt != num_add) {
2029 promisc_changed = true;
2030 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2031 &vsi->state);
2032 vsi->promisc_threshold =
2033 (vsi->active_filters * 3) / 4;
2034 dev_warn(&pf->pdev->dev,
2035 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2036 i40e_aq_str(hw, aq_err),
2037 vsi_name);
2038 }
2039 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002040 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002041 }
2042 }
2043 if (num_add) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002044 aq_ret = i40e_aq_add_macvlan(hw, vsi->seid,
Mitch Williamsea02e902015-11-09 15:35:50 -08002045 add_list, num_add, NULL);
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002046 aq_err = hw->aq.asq_last_status;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002047 fcnt = i40e_update_filter_state(num_add, add_list,
2048 add_head, aq_ret);
2049 vsi->active_filters += fcnt;
2050 if (fcnt != num_add) {
2051 promisc_changed = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002052 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2053 &vsi->state);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002054 vsi->promisc_threshold =
2055 (vsi->active_filters * 3) / 4;
2056 dev_warn(&pf->pdev->dev,
2057 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2058 i40e_aq_str(hw, aq_err), vsi_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002059 }
2060 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002061 /* Now move all of the filters from the temp add list back to
2062 * the VSI's list.
2063 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002064 spin_lock_bh(&vsi->mac_filter_hash_lock);
2065 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
2066 u64 key = i40e_addr_to_hkey(f->macaddr);
2067
2068 hlist_del(&f->hlist);
2069 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002070 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002071 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002072 kfree(add_list);
2073 add_list = NULL;
2074 }
2075
2076 /* Check to see if we can drop out of overflow promiscuous mode. */
2077 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
2078 (vsi->active_filters < vsi->promisc_threshold)) {
2079 int failed_count = 0;
2080 /* See if we have any failed filters. We can't drop out of
2081 * promiscuous until these have all been deleted.
2082 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002083 spin_lock_bh(&vsi->mac_filter_hash_lock);
2084 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002085 if (f->state == I40E_FILTER_FAILED)
2086 failed_count++;
2087 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002088 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002089 if (!failed_count) {
2090 dev_info(&pf->pdev->dev,
2091 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2092 vsi_name);
2093 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2094 promisc_changed = true;
2095 vsi->promisc_threshold = 0;
2096 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002097 }
2098
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002099 /* if the VF is not trusted do not do promisc */
2100 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2101 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2102 goto out;
2103 }
2104
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002105 /* check for changes in promiscuous modes */
2106 if (changed_flags & IFF_ALLMULTI) {
2107 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002108
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002109 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002110 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2111 vsi->seid,
2112 cur_multipromisc,
2113 NULL);
2114 if (aq_ret) {
2115 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002116 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002117 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002118 "set multi promisc failed on %s, err %s aq_err %s\n",
2119 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002120 i40e_stat_str(hw, aq_ret),
2121 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002122 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002123 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002124 if ((changed_flags & IFF_PROMISC) ||
2125 (promisc_changed &&
2126 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002127 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002128
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002129 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2130 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2131 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002132 if ((vsi->type == I40E_VSI_MAIN) &&
2133 (pf->lan_veb != I40E_NO_VEB) &&
2134 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002135 /* set defport ON for Main VSI instead of true promisc
2136 * this way we will get all unicast/multicast and VLAN
2137 * promisc behavior but will not get VF or VMDq traffic
2138 * replicated on the Main VSI.
2139 */
2140 if (pf->cur_promisc != cur_promisc) {
2141 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002142 if (cur_promisc)
2143 aq_ret =
2144 i40e_aq_set_default_vsi(hw,
2145 vsi->seid,
2146 NULL);
2147 else
2148 aq_ret =
2149 i40e_aq_clear_default_vsi(hw,
2150 vsi->seid,
2151 NULL);
2152 if (aq_ret) {
2153 retval = i40e_aq_rc_to_posix(aq_ret,
2154 hw->aq.asq_last_status);
2155 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002156 "Set default VSI failed on %s, err %s, aq_err %s\n",
2157 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002158 i40e_stat_str(hw, aq_ret),
2159 i40e_aq_str(hw,
2160 hw->aq.asq_last_status));
2161 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002162 }
2163 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002164 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002165 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002166 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002167 cur_promisc, NULL,
2168 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002169 if (aq_ret) {
2170 retval =
2171 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002172 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002173 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002174 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2175 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002176 i40e_stat_str(hw, aq_ret),
2177 i40e_aq_str(hw,
2178 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002179 }
2180 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002181 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002182 vsi->seid,
2183 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002184 if (aq_ret) {
2185 retval =
2186 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002187 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002188 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002189 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2190 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002191 i40e_stat_str(hw, aq_ret),
2192 i40e_aq_str(hw,
2193 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002194 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002195 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002196 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2197 vsi->seid,
2198 cur_promisc, NULL);
2199 if (aq_ret) {
2200 retval = i40e_aq_rc_to_posix(aq_ret,
2201 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002202 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002203 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002204 i40e_stat_str(hw, aq_ret),
2205 i40e_aq_str(hw,
2206 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002207 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002208 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002209out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002210 /* if something went wrong then set the changed flag so we try again */
2211 if (retval)
2212 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2213
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002214 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002215 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002216
2217err_no_memory:
2218 /* Restore elements on the temporary add and delete lists */
2219 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002220err_no_memory_locked:
Jacob Keller4a2ce272016-10-05 09:30:38 -07002221 i40e_undo_filter_entries(vsi, &tmp_del_list);
2222 i40e_undo_filter_entries(vsi, &tmp_add_list);
2223 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2224
2225 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2226 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2227 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002228}
2229
2230/**
2231 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2232 * @pf: board private structure
2233 **/
2234static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2235{
2236 int v;
2237
2238 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2239 return;
2240 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2241
Mitch Williams505682c2014-05-20 08:01:37 +00002242 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002243 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002244 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2245 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2246
2247 if (ret) {
2248 /* come back and try again later */
2249 pf->flags |= I40E_FLAG_FILTER_SYNC;
2250 break;
2251 }
2252 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002253 }
2254}
2255
2256/**
2257 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2258 * @netdev: network interface device structure
2259 * @new_mtu: new value for maximum frame size
2260 *
2261 * Returns 0 on success, negative on failure
2262 **/
2263static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2264{
2265 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002266 struct i40e_vsi *vsi = np->vsi;
2267
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002268 netdev_info(netdev, "changing MTU from %d to %d\n",
2269 netdev->mtu, new_mtu);
2270 netdev->mtu = new_mtu;
2271 if (netif_running(netdev))
2272 i40e_vsi_reinit_locked(vsi);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06002273 i40e_notify_client_of_l2_param_changes(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002274 return 0;
2275}
2276
2277/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002278 * i40e_ioctl - Access the hwtstamp interface
2279 * @netdev: network interface device structure
2280 * @ifr: interface request data
2281 * @cmd: ioctl command
2282 **/
2283int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2284{
2285 struct i40e_netdev_priv *np = netdev_priv(netdev);
2286 struct i40e_pf *pf = np->vsi->back;
2287
2288 switch (cmd) {
2289 case SIOCGHWTSTAMP:
2290 return i40e_ptp_get_ts_config(pf, ifr);
2291 case SIOCSHWTSTAMP:
2292 return i40e_ptp_set_ts_config(pf, ifr);
2293 default:
2294 return -EOPNOTSUPP;
2295 }
2296}
2297
2298/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002299 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2300 * @vsi: the vsi being adjusted
2301 **/
2302void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2303{
2304 struct i40e_vsi_context ctxt;
2305 i40e_status ret;
2306
2307 if ((vsi->info.valid_sections &
2308 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2309 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2310 return; /* already enabled */
2311
2312 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2313 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2314 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2315
2316 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002317 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002318 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2319 if (ret) {
2320 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002321 "update vlan stripping failed, err %s aq_err %s\n",
2322 i40e_stat_str(&vsi->back->hw, ret),
2323 i40e_aq_str(&vsi->back->hw,
2324 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002325 }
2326}
2327
2328/**
2329 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2330 * @vsi: the vsi being adjusted
2331 **/
2332void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2333{
2334 struct i40e_vsi_context ctxt;
2335 i40e_status ret;
2336
2337 if ((vsi->info.valid_sections &
2338 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2339 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2340 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2341 return; /* already disabled */
2342
2343 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2344 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2345 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2346
2347 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002348 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002349 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2350 if (ret) {
2351 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002352 "update vlan stripping failed, err %s aq_err %s\n",
2353 i40e_stat_str(&vsi->back->hw, ret),
2354 i40e_aq_str(&vsi->back->hw,
2355 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002356 }
2357}
2358
2359/**
2360 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2361 * @netdev: network interface to be adjusted
2362 * @features: netdev features to test if VLAN offload is enabled or not
2363 **/
2364static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2365{
2366 struct i40e_netdev_priv *np = netdev_priv(netdev);
2367 struct i40e_vsi *vsi = np->vsi;
2368
2369 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2370 i40e_vlan_stripping_enable(vsi);
2371 else
2372 i40e_vlan_stripping_disable(vsi);
2373}
2374
2375/**
2376 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2377 * @vsi: the vsi being configured
2378 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2379 **/
2380int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2381{
Jacob Keller278e7d02016-10-05 09:30:37 -07002382 struct i40e_mac_filter *f, *add_f, *del_f;
2383 struct hlist_node *h;
2384 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002385
Kiran Patil21659032015-09-30 14:09:03 -04002386 /* Locked once because all functions invoked below iterates list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07002387 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002388
Jacob Keller1bc87e82016-10-05 09:30:31 -07002389 if (vsi->netdev) {
2390 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002391 if (!add_f) {
2392 dev_info(&vsi->back->pdev->dev,
2393 "Could not add vlan filter %d for %pM\n",
2394 vid, vsi->netdev->dev_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002395 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002396 return -ENOMEM;
2397 }
2398 }
2399
Jacob Keller278e7d02016-10-05 09:30:37 -07002400 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002401 if (f->state == I40E_FILTER_REMOVE)
2402 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002403 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002404 if (!add_f) {
2405 dev_info(&vsi->back->pdev->dev,
2406 "Could not add vlan filter %d for %pM\n",
2407 vid, f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002408 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002409 return -ENOMEM;
2410 }
2411 }
2412
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002413 /* Now if we add a vlan tag, make sure to check if it is the first
2414 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2415 * with 0, so we now accept untagged and specified tagged traffic
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002416 * (and not all tags along with untagged)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002417 */
Jacob Keller290d2552016-10-05 09:30:36 -07002418 if (vid > 0 && vsi->netdev) {
2419 del_f = i40e_find_filter(vsi, vsi->netdev->dev_addr,
2420 I40E_VLAN_ANY);
2421 if (del_f) {
2422 __i40e_del_filter(vsi, del_f);
Jacob Keller1bc87e82016-10-05 09:30:31 -07002423 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002424 if (!add_f) {
2425 dev_info(&vsi->back->pdev->dev,
2426 "Could not add filter 0 for %pM\n",
2427 vsi->netdev->dev_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002428 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002429 return -ENOMEM;
2430 }
2431 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002432 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002433
Greg Rose8d82a7c2014-01-13 16:13:04 -08002434 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2435 if (vid > 0 && !vsi->info.pvid) {
Jacob Keller278e7d02016-10-05 09:30:37 -07002436 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002437 if (f->state == I40E_FILTER_REMOVE)
2438 continue;
Jacob Keller290d2552016-10-05 09:30:36 -07002439 del_f = i40e_find_filter(vsi, f->macaddr,
2440 I40E_VLAN_ANY);
2441 if (!del_f)
Kiran Patil21659032015-09-30 14:09:03 -04002442 continue;
Jacob Keller290d2552016-10-05 09:30:36 -07002443 __i40e_del_filter(vsi, del_f);
Jacob Keller1bc87e82016-10-05 09:30:31 -07002444 add_f = i40e_add_filter(vsi, f->macaddr, 0);
Kiran Patil21659032015-09-30 14:09:03 -04002445 if (!add_f) {
2446 dev_info(&vsi->back->pdev->dev,
2447 "Could not add filter 0 for %pM\n",
2448 f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002449 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002450 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002451 }
2452 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002453 }
2454
Jacob Keller278e7d02016-10-05 09:30:37 -07002455 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002456
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002457 /* schedule our worker thread which will take care of
2458 * applying the new filter changes
2459 */
2460 i40e_service_event_schedule(vsi->back);
2461 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002462}
2463
2464/**
2465 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2466 * @vsi: the vsi being configured
2467 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002468 *
2469 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002470 **/
2471int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
2472{
2473 struct net_device *netdev = vsi->netdev;
Alan Brady84f5ca62016-10-05 09:30:39 -07002474 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002475 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002476 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002477
Kiran Patil21659032015-09-30 14:09:03 -04002478 /* Locked once because all functions invoked below iterates list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002479 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002480
Jacob Keller1bc87e82016-10-05 09:30:31 -07002481 if (vsi->netdev)
2482 i40e_del_filter(vsi, netdev->dev_addr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002483
Jacob Keller278e7d02016-10-05 09:30:37 -07002484 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002485 if (f->vlan == vid)
2486 __i40e_del_filter(vsi, f);
2487 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002488
Jacob Keller278e7d02016-10-05 09:30:37 -07002489 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002490
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002491 /* schedule our worker thread which will take care of
2492 * applying the new filter changes
2493 */
2494 i40e_service_event_schedule(vsi->back);
2495 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002496}
2497
2498/**
2499 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2500 * @netdev: network interface to be adjusted
2501 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002502 *
2503 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002504 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002505#ifdef I40E_FCOE
2506int i40e_vlan_rx_add_vid(struct net_device *netdev,
2507 __always_unused __be16 proto, u16 vid)
2508#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002509static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2510 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002511#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002512{
2513 struct i40e_netdev_priv *np = netdev_priv(netdev);
2514 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002515 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002516
2517 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002518 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002519
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002520 /* If the network stack called us with vid = 0 then
2521 * it is asking to receive priority tagged packets with
2522 * vlan id 0. Our HW receives them by default when configured
2523 * to receive untagged packets so there is no need to add an
2524 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002525 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002526 if (vid)
2527 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002528
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002529 if (!ret && (vid < VLAN_N_VID))
2530 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002531
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002532 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002533}
2534
2535/**
2536 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2537 * @netdev: network interface to be adjusted
2538 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002539 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002540 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002541 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002542#ifdef I40E_FCOE
2543int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2544 __always_unused __be16 proto, u16 vid)
2545#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002546static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2547 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002548#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002549{
2550 struct i40e_netdev_priv *np = netdev_priv(netdev);
2551 struct i40e_vsi *vsi = np->vsi;
2552
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002553 /* return code is ignored as there is nothing a user
2554 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002555 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002556 */
2557 i40e_vsi_kill_vlan(vsi, vid);
2558
2559 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002560
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002561 return 0;
2562}
2563
2564/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002565 * i40e_macaddr_init - explicitly write the mac address filters
2566 *
2567 * @vsi: pointer to the vsi
2568 * @macaddr: the MAC address
2569 *
2570 * This is needed when the macaddr has been obtained by other
2571 * means than the default, e.g., from Open Firmware or IDPROM.
2572 * Returns 0 on success, negative on failure
2573 **/
2574static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2575{
2576 int ret;
2577 struct i40e_aqc_add_macvlan_element_data element;
2578
2579 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2580 I40E_AQC_WRITE_TYPE_LAA_WOL,
2581 macaddr, NULL);
2582 if (ret) {
2583 dev_info(&vsi->back->pdev->dev,
2584 "Addr change for VSI failed: %d\n", ret);
2585 return -EADDRNOTAVAIL;
2586 }
2587
2588 memset(&element, 0, sizeof(element));
2589 ether_addr_copy(element.mac_addr, macaddr);
2590 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2591 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2592 if (ret) {
2593 dev_info(&vsi->back->pdev->dev,
2594 "add filter failed err %s aq_err %s\n",
2595 i40e_stat_str(&vsi->back->hw, ret),
2596 i40e_aq_str(&vsi->back->hw,
2597 vsi->back->hw.aq.asq_last_status));
2598 }
2599 return ret;
2600}
2601
2602/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002603 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2604 * @vsi: the vsi being brought back up
2605 **/
2606static void i40e_restore_vlan(struct i40e_vsi *vsi)
2607{
2608 u16 vid;
2609
2610 if (!vsi->netdev)
2611 return;
2612
2613 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2614
2615 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2616 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2617 vid);
2618}
2619
2620/**
2621 * i40e_vsi_add_pvid - Add pvid for the VSI
2622 * @vsi: the vsi being adjusted
2623 * @vid: the vlan id to set as a PVID
2624 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002625int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002626{
2627 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002628 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002629
2630 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2631 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002632 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2633 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002634 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002635
2636 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002637 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002638 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2639 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002640 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002641 "add pvid failed, err %s aq_err %s\n",
2642 i40e_stat_str(&vsi->back->hw, ret),
2643 i40e_aq_str(&vsi->back->hw,
2644 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002645 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002646 }
2647
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002648 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002649}
2650
2651/**
2652 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2653 * @vsi: the vsi being adjusted
2654 *
2655 * Just use the vlan_rx_register() service to put it back to normal
2656 **/
2657void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2658{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002659 i40e_vlan_stripping_disable(vsi);
2660
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002661 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002662}
2663
2664/**
2665 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2666 * @vsi: ptr to the VSI
2667 *
2668 * If this function returns with an error, then it's possible one or
2669 * more of the rings is populated (while the rest are not). It is the
2670 * callers duty to clean those orphaned rings.
2671 *
2672 * Return 0 on success, negative on failure
2673 **/
2674static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2675{
2676 int i, err = 0;
2677
2678 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002679 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002680
2681 return err;
2682}
2683
2684/**
2685 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2686 * @vsi: ptr to the VSI
2687 *
2688 * Free VSI's transmit software resources
2689 **/
2690static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2691{
2692 int i;
2693
Greg Rose8e9dca52013-12-18 13:45:53 +00002694 if (!vsi->tx_rings)
2695 return;
2696
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002697 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002698 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002699 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002700}
2701
2702/**
2703 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2704 * @vsi: ptr to the VSI
2705 *
2706 * If this function returns with an error, then it's possible one or
2707 * more of the rings is populated (while the rest are not). It is the
2708 * callers duty to clean those orphaned rings.
2709 *
2710 * Return 0 on success, negative on failure
2711 **/
2712static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2713{
2714 int i, err = 0;
2715
2716 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002717 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002718#ifdef I40E_FCOE
2719 i40e_fcoe_setup_ddp_resources(vsi);
2720#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002721 return err;
2722}
2723
2724/**
2725 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2726 * @vsi: ptr to the VSI
2727 *
2728 * Free all receive software resources
2729 **/
2730static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2731{
2732 int i;
2733
Greg Rose8e9dca52013-12-18 13:45:53 +00002734 if (!vsi->rx_rings)
2735 return;
2736
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002737 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002738 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002739 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002740#ifdef I40E_FCOE
2741 i40e_fcoe_free_ddp_resources(vsi);
2742#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002743}
2744
2745/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002746 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2747 * @ring: The Tx ring to configure
2748 *
2749 * This enables/disables XPS for a given Tx descriptor ring
2750 * based on the TCs enabled for the VSI that ring belongs to.
2751 **/
2752static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2753{
2754 struct i40e_vsi *vsi = ring->vsi;
2755 cpumask_var_t mask;
2756
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002757 if (!ring->q_vector || !ring->netdev)
2758 return;
2759
2760 /* Single TC mode enable XPS */
2761 if (vsi->tc_config.numtc <= 1) {
2762 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002763 netif_set_xps_queue(ring->netdev,
2764 &ring->q_vector->affinity_mask,
2765 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002766 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2767 /* Disable XPS to allow selection based on TC */
2768 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2769 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2770 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002771 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002772
2773 /* schedule our worker thread which will take care of
2774 * applying the new filter changes
2775 */
2776 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002777}
2778
2779/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002780 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2781 * @ring: The Tx ring to configure
2782 *
2783 * Configure the Tx descriptor ring in the HMC context.
2784 **/
2785static int i40e_configure_tx_ring(struct i40e_ring *ring)
2786{
2787 struct i40e_vsi *vsi = ring->vsi;
2788 u16 pf_q = vsi->base_queue + ring->queue_index;
2789 struct i40e_hw *hw = &vsi->back->hw;
2790 struct i40e_hmc_obj_txq tx_ctx;
2791 i40e_status err = 0;
2792 u32 qtx_ctl = 0;
2793
2794 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002795 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002796 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2797 ring->atr_count = 0;
2798 } else {
2799 ring->atr_sample_rate = 0;
2800 }
2801
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002802 /* configure XPS */
2803 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002804
2805 /* clear the context structure first */
2806 memset(&tx_ctx, 0, sizeof(tx_ctx));
2807
2808 tx_ctx.new_context = 1;
2809 tx_ctx.base = (ring->dma / 128);
2810 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002811 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2812 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002813#ifdef I40E_FCOE
2814 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2815#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002816 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002817 /* FDIR VSI tx ring can still use RS bit and writebacks */
2818 if (vsi->type != I40E_VSI_FDIR)
2819 tx_ctx.head_wb_ena = 1;
2820 tx_ctx.head_wb_addr = ring->dma +
2821 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002822
2823 /* As part of VSI creation/update, FW allocates certain
2824 * Tx arbitration queue sets for each TC enabled for
2825 * the VSI. The FW returns the handles to these queue
2826 * sets as part of the response buffer to Add VSI,
2827 * Update VSI, etc. AQ commands. It is expected that
2828 * these queue set handles be associated with the Tx
2829 * queues by the driver as part of the TX queue context
2830 * initialization. This has to be done regardless of
2831 * DCB as by default everything is mapped to TC0.
2832 */
2833 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2834 tx_ctx.rdylist_act = 0;
2835
2836 /* clear the context in the HMC */
2837 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2838 if (err) {
2839 dev_info(&vsi->back->pdev->dev,
2840 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2841 ring->queue_index, pf_q, err);
2842 return -ENOMEM;
2843 }
2844
2845 /* set the context in the HMC */
2846 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2847 if (err) {
2848 dev_info(&vsi->back->pdev->dev,
2849 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2850 ring->queue_index, pf_q, err);
2851 return -ENOMEM;
2852 }
2853
2854 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002855 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002856 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002857 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2858 I40E_QTX_CTL_VFVM_INDX_MASK;
2859 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002860 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002861 }
2862
Shannon Nelson13fd9772013-09-28 07:14:19 +00002863 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2864 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002865 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2866 i40e_flush(hw);
2867
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002868 /* cache tail off for easier writes later */
2869 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2870
2871 return 0;
2872}
2873
2874/**
2875 * i40e_configure_rx_ring - Configure a receive ring context
2876 * @ring: The Rx ring to configure
2877 *
2878 * Configure the Rx descriptor ring in the HMC context.
2879 **/
2880static int i40e_configure_rx_ring(struct i40e_ring *ring)
2881{
2882 struct i40e_vsi *vsi = ring->vsi;
2883 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2884 u16 pf_q = vsi->base_queue + ring->queue_index;
2885 struct i40e_hw *hw = &vsi->back->hw;
2886 struct i40e_hmc_obj_rxq rx_ctx;
2887 i40e_status err = 0;
2888
2889 ring->state = 0;
2890
2891 /* clear the context structure first */
2892 memset(&rx_ctx, 0, sizeof(rx_ctx));
2893
2894 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002895
2896 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002897
2898 rx_ctx.base = (ring->dma / 128);
2899 rx_ctx.qlen = ring->count;
2900
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07002901 /* use 32 byte descriptors */
2902 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002903
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07002904 /* descriptor type is always zero
2905 * rx_ctx.dtype = 0;
2906 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07002907 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002908
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07002909 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002910 if (hw->revision_id == 0)
2911 rx_ctx.lrxqthresh = 0;
2912 else
2913 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002914 rx_ctx.crcstrip = 1;
2915 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07002916 /* this controls whether VLAN is stripped from inner headers */
2917 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07002918#ifdef I40E_FCOE
2919 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2920#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002921 /* set the prefena field to 1 because the manual says to */
2922 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002923
2924 /* clear the context in the HMC */
2925 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2926 if (err) {
2927 dev_info(&vsi->back->pdev->dev,
2928 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2929 ring->queue_index, pf_q, err);
2930 return -ENOMEM;
2931 }
2932
2933 /* set the context in the HMC */
2934 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2935 if (err) {
2936 dev_info(&vsi->back->pdev->dev,
2937 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2938 ring->queue_index, pf_q, err);
2939 return -ENOMEM;
2940 }
2941
2942 /* cache tail for quicker writes, and clear the reg before use */
2943 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2944 writel(0, ring->tail);
2945
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002946 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002947
2948 return 0;
2949}
2950
2951/**
2952 * i40e_vsi_configure_tx - Configure the VSI for Tx
2953 * @vsi: VSI structure describing this set of rings and resources
2954 *
2955 * Configure the Tx VSI for operation.
2956 **/
2957static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2958{
2959 int err = 0;
2960 u16 i;
2961
Alexander Duyck9f65e152013-09-28 06:00:58 +00002962 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2963 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002964
2965 return err;
2966}
2967
2968/**
2969 * i40e_vsi_configure_rx - Configure the VSI for Rx
2970 * @vsi: the VSI being configured
2971 *
2972 * Configure the Rx VSI for operation.
2973 **/
2974static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2975{
2976 int err = 0;
2977 u16 i;
2978
2979 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2980 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2981 + ETH_FCS_LEN + VLAN_HLEN;
2982 else
2983 vsi->max_frame = I40E_RXBUFFER_2048;
2984
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002985 vsi->rx_buf_len = I40E_RXBUFFER_2048;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002986
Vasu Dev38e00432014-08-01 13:27:03 -07002987#ifdef I40E_FCOE
2988 /* setup rx buffer for FCoE */
2989 if ((vsi->type == I40E_VSI_FCOE) &&
2990 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
Vasu Dev38e00432014-08-01 13:27:03 -07002991 vsi->rx_buf_len = I40E_RXBUFFER_3072;
2992 vsi->max_frame = I40E_RXBUFFER_3072;
Vasu Dev38e00432014-08-01 13:27:03 -07002993 }
2994
2995#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002996 /* round up for the chip's needs */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002997 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002998 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002999
3000 /* set up individual rings */
3001 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003002 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003003
3004 return err;
3005}
3006
3007/**
3008 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3009 * @vsi: ptr to the VSI
3010 **/
3011static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3012{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003013 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003014 u16 qoffset, qcount;
3015 int i, n;
3016
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003017 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3018 /* Reset the TC information */
3019 for (i = 0; i < vsi->num_queue_pairs; i++) {
3020 rx_ring = vsi->rx_rings[i];
3021 tx_ring = vsi->tx_rings[i];
3022 rx_ring->dcb_tc = 0;
3023 tx_ring->dcb_tc = 0;
3024 }
3025 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003026
3027 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003028 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003029 continue;
3030
3031 qoffset = vsi->tc_config.tc_info[n].qoffset;
3032 qcount = vsi->tc_config.tc_info[n].qcount;
3033 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003034 rx_ring = vsi->rx_rings[i];
3035 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003036 rx_ring->dcb_tc = n;
3037 tx_ring->dcb_tc = n;
3038 }
3039 }
3040}
3041
3042/**
3043 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3044 * @vsi: ptr to the VSI
3045 **/
3046static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3047{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003048 struct i40e_pf *pf = vsi->back;
3049 int err;
3050
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003051 if (vsi->netdev)
3052 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003053
3054 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3055 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3056 if (err) {
3057 dev_warn(&pf->pdev->dev,
3058 "could not set up macaddr; err %d\n", err);
3059 }
3060 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003061}
3062
3063/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003064 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3065 * @vsi: Pointer to the targeted VSI
3066 *
3067 * This function replays the hlist on the hw where all the SB Flow Director
3068 * filters were saved.
3069 **/
3070static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3071{
3072 struct i40e_fdir_filter *filter;
3073 struct i40e_pf *pf = vsi->back;
3074 struct hlist_node *node;
3075
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003076 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3077 return;
3078
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003079 hlist_for_each_entry_safe(filter, node,
3080 &pf->fdir_filter_list, fdir_node) {
3081 i40e_add_del_fdir(vsi, filter, true);
3082 }
3083}
3084
3085/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003086 * i40e_vsi_configure - Set up the VSI for action
3087 * @vsi: the VSI being configured
3088 **/
3089static int i40e_vsi_configure(struct i40e_vsi *vsi)
3090{
3091 int err;
3092
3093 i40e_set_vsi_rx_mode(vsi);
3094 i40e_restore_vlan(vsi);
3095 i40e_vsi_config_dcb_rings(vsi);
3096 err = i40e_vsi_configure_tx(vsi);
3097 if (!err)
3098 err = i40e_vsi_configure_rx(vsi);
3099
3100 return err;
3101}
3102
3103/**
3104 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3105 * @vsi: the VSI being configured
3106 **/
3107static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3108{
3109 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003110 struct i40e_hw *hw = &pf->hw;
3111 u16 vector;
3112 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003113 u32 qp;
3114
3115 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3116 * and PFINT_LNKLSTn registers, e.g.:
3117 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3118 */
3119 qp = vsi->base_queue;
3120 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003121 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003122 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3123
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003124 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003125 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003126 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3127 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3128 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003129 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003130 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3131 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3132 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003133 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3134 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003135
3136 /* Linked list for the queuepairs assigned to this vector */
3137 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3138 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003139 u32 val;
3140
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003141 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3142 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3143 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3144 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3145 (I40E_QUEUE_TYPE_TX
3146 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3147
3148 wr32(hw, I40E_QINT_RQCTL(qp), val);
3149
3150 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3151 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3152 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3153 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3154 (I40E_QUEUE_TYPE_RX
3155 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3156
3157 /* Terminate the linked list */
3158 if (q == (q_vector->num_ringpairs - 1))
3159 val |= (I40E_QUEUE_END_OF_LIST
3160 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3161
3162 wr32(hw, I40E_QINT_TQCTL(qp), val);
3163 qp++;
3164 }
3165 }
3166
3167 i40e_flush(hw);
3168}
3169
3170/**
3171 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3172 * @hw: ptr to the hardware info
3173 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003174static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003175{
Jacob Kellerab437b52014-12-14 01:55:08 +00003176 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003177 u32 val;
3178
3179 /* clear things first */
3180 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3181 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3182
3183 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3184 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3185 I40E_PFINT_ICR0_ENA_GRST_MASK |
3186 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3187 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003188 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3189 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3190 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3191
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003192 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3193 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3194
Jacob Kellerab437b52014-12-14 01:55:08 +00003195 if (pf->flags & I40E_FLAG_PTP)
3196 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3197
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003198 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3199
3200 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003201 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3202 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003203
3204 /* OTHER_ITR_IDX = 0 */
3205 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3206}
3207
3208/**
3209 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3210 * @vsi: the VSI being configured
3211 **/
3212static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3213{
Alexander Duyck493fb302013-09-28 07:01:44 +00003214 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003215 struct i40e_pf *pf = vsi->back;
3216 struct i40e_hw *hw = &pf->hw;
3217 u32 val;
3218
3219 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003220 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003221 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003222 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3223 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003224 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003225 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3226 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3227
Jacob Kellerab437b52014-12-14 01:55:08 +00003228 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003229
3230 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3231 wr32(hw, I40E_PFINT_LNKLST0, 0);
3232
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003233 /* Associate the queue pair to the vector and enable the queue int */
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 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3237
3238 wr32(hw, I40E_QINT_RQCTL(0), val);
3239
3240 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3241 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3242 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3243
3244 wr32(hw, I40E_QINT_TQCTL(0), val);
3245 i40e_flush(hw);
3246}
3247
3248/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003249 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3250 * @pf: board private structure
3251 **/
3252void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3253{
3254 struct i40e_hw *hw = &pf->hw;
3255
3256 wr32(hw, I40E_PFINT_DYN_CTL0,
3257 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3258 i40e_flush(hw);
3259}
3260
3261/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003262 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3263 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003264 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003265 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003266void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003267{
3268 struct i40e_hw *hw = &pf->hw;
3269 u32 val;
3270
3271 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003272 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003273 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3274
3275 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3276 i40e_flush(hw);
3277}
3278
3279/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003280 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3281 * @irq: interrupt number
3282 * @data: pointer to a q_vector
3283 **/
3284static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3285{
3286 struct i40e_q_vector *q_vector = data;
3287
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003288 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003289 return IRQ_HANDLED;
3290
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003291 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003292
3293 return IRQ_HANDLED;
3294}
3295
3296/**
Alan Brady96db7762016-09-14 16:24:38 -07003297 * i40e_irq_affinity_notify - Callback for affinity changes
3298 * @notify: context as to what irq was changed
3299 * @mask: the new affinity mask
3300 *
3301 * This is a callback function used by the irq_set_affinity_notifier function
3302 * so that we may register to receive changes to the irq affinity masks.
3303 **/
3304static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3305 const cpumask_t *mask)
3306{
3307 struct i40e_q_vector *q_vector =
3308 container_of(notify, struct i40e_q_vector, affinity_notify);
3309
3310 q_vector->affinity_mask = *mask;
3311}
3312
3313/**
3314 * i40e_irq_affinity_release - Callback for affinity notifier release
3315 * @ref: internal core kernel usage
3316 *
3317 * This is a callback function used by the irq_set_affinity_notifier function
3318 * to inform the current notification subscriber that they will no longer
3319 * receive notifications.
3320 **/
3321static void i40e_irq_affinity_release(struct kref *ref) {}
3322
3323/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003324 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3325 * @vsi: the VSI being configured
3326 * @basename: name for the vector
3327 *
3328 * Allocates MSI-X vectors and requests interrupts from the kernel.
3329 **/
3330static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3331{
3332 int q_vectors = vsi->num_q_vectors;
3333 struct i40e_pf *pf = vsi->back;
3334 int base = vsi->base_vector;
3335 int rx_int_idx = 0;
3336 int tx_int_idx = 0;
3337 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003338 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003339
3340 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003341 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003342
Alan Brady96db7762016-09-14 16:24:38 -07003343 irq_num = pf->msix_entries[base + vector].vector;
3344
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003345 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003346 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3347 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3348 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003349 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003350 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3351 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003352 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003353 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3354 "%s-%s-%d", basename, "tx", tx_int_idx++);
3355 } else {
3356 /* skip this unused q_vector */
3357 continue;
3358 }
Alan Brady96db7762016-09-14 16:24:38 -07003359 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003360 vsi->irq_handler,
3361 0,
3362 q_vector->name,
3363 q_vector);
3364 if (err) {
3365 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003366 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003367 goto free_queue_irqs;
3368 }
Alan Brady96db7762016-09-14 16:24:38 -07003369
3370 /* register for affinity change notifications */
3371 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3372 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3373 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003374 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003375 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003376 }
3377
Shannon Nelson63741842014-04-23 04:50:16 +00003378 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003379 return 0;
3380
3381free_queue_irqs:
3382 while (vector) {
3383 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003384 irq_num = pf->msix_entries[base + vector].vector;
3385 irq_set_affinity_notifier(irq_num, NULL);
3386 irq_set_affinity_hint(irq_num, NULL);
3387 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003388 }
3389 return err;
3390}
3391
3392/**
3393 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3394 * @vsi: the VSI being un-configured
3395 **/
3396static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3397{
3398 struct i40e_pf *pf = vsi->back;
3399 struct i40e_hw *hw = &pf->hw;
3400 int base = vsi->base_vector;
3401 int i;
3402
3403 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003404 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3405 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003406 }
3407
3408 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3409 for (i = vsi->base_vector;
3410 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3411 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3412
3413 i40e_flush(hw);
3414 for (i = 0; i < vsi->num_q_vectors; i++)
3415 synchronize_irq(pf->msix_entries[i + base].vector);
3416 } else {
3417 /* Legacy and MSI mode - this stops all interrupt handling */
3418 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3419 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3420 i40e_flush(hw);
3421 synchronize_irq(pf->pdev->irq);
3422 }
3423}
3424
3425/**
3426 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3427 * @vsi: the VSI being configured
3428 **/
3429static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3430{
3431 struct i40e_pf *pf = vsi->back;
3432 int i;
3433
3434 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003435 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003436 i40e_irq_dynamic_enable(vsi, i);
3437 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003438 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003439 }
3440
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003441 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003442 return 0;
3443}
3444
3445/**
3446 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3447 * @pf: board private structure
3448 **/
3449static void i40e_stop_misc_vector(struct i40e_pf *pf)
3450{
3451 /* Disable ICR 0 */
3452 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3453 i40e_flush(&pf->hw);
3454}
3455
3456/**
3457 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3458 * @irq: interrupt number
3459 * @data: pointer to a q_vector
3460 *
3461 * This is the handler used for all MSI/Legacy interrupts, and deals
3462 * with both queue and non-queue interrupts. This is also used in
3463 * MSIX mode to handle the non-queue interrupts.
3464 **/
3465static irqreturn_t i40e_intr(int irq, void *data)
3466{
3467 struct i40e_pf *pf = (struct i40e_pf *)data;
3468 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003469 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003470 u32 icr0, icr0_remaining;
3471 u32 val, ena_mask;
3472
3473 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003474 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003475
Shannon Nelson116a57d2013-09-28 07:13:59 +00003476 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3477 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003478 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003479
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003480 /* if interrupt but no bits showing, must be SWINT */
3481 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3482 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3483 pf->sw_int_count++;
3484
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003485 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3486 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3487 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3488 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3489 dev_info(&pf->pdev->dev, "cleared PE_CRITERR\n");
3490 }
3491
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003492 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3493 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003494 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3495 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003496
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003497 /* We do not have a way to disarm Queue causes while leaving
3498 * interrupt enabled for all other causes, ideally
3499 * interrupt should be disabled while we are in NAPI but
3500 * this is not a performance path and napi_schedule()
3501 * can deal with rescheduling.
3502 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003503 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003504 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003505 }
3506
3507 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3508 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3509 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003510 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003511 }
3512
3513 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3514 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3515 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3516 }
3517
3518 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3519 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3520 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3521 }
3522
3523 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3524 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3525 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3526 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3527 val = rd32(hw, I40E_GLGEN_RSTAT);
3528 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3529 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003530 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003531 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003532 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003533 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003534 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003535 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003536 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003537 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003538 }
3539
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003540 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3541 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3542 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003543 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3544 rd32(hw, I40E_PFHMC_ERRORINFO),
3545 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003546 }
3547
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003548 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3549 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3550
3551 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003552 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003553 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003554 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003555 }
3556
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003557 /* If a critical error is pending we have no choice but to reset the
3558 * device.
3559 * Report and mask out any remaining unexpected interrupts.
3560 */
3561 icr0_remaining = icr0 & ena_mask;
3562 if (icr0_remaining) {
3563 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3564 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003565 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003566 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003567 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003568 dev_info(&pf->pdev->dev, "device will be reset\n");
3569 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3570 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003571 }
3572 ena_mask &= ~icr0_remaining;
3573 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003574 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003575
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003576enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003577 /* re-enable interrupt causes */
3578 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003579 if (!test_bit(__I40E_DOWN, &pf->state)) {
3580 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003581 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003582 }
3583
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003584 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003585}
3586
3587/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003588 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3589 * @tx_ring: tx ring to clean
3590 * @budget: how many cleans we're allowed
3591 *
3592 * Returns true if there's any budget left (e.g. the clean is finished)
3593 **/
3594static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3595{
3596 struct i40e_vsi *vsi = tx_ring->vsi;
3597 u16 i = tx_ring->next_to_clean;
3598 struct i40e_tx_buffer *tx_buf;
3599 struct i40e_tx_desc *tx_desc;
3600
3601 tx_buf = &tx_ring->tx_bi[i];
3602 tx_desc = I40E_TX_DESC(tx_ring, i);
3603 i -= tx_ring->count;
3604
3605 do {
3606 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3607
3608 /* if next_to_watch is not set then there is no work pending */
3609 if (!eop_desc)
3610 break;
3611
3612 /* prevent any other reads prior to eop_desc */
3613 read_barrier_depends();
3614
3615 /* if the descriptor isn't done, no work yet to do */
3616 if (!(eop_desc->cmd_type_offset_bsz &
3617 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3618 break;
3619
3620 /* clear next_to_watch to prevent false hangs */
3621 tx_buf->next_to_watch = NULL;
3622
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003623 tx_desc->buffer_addr = 0;
3624 tx_desc->cmd_type_offset_bsz = 0;
3625 /* move past filter desc */
3626 tx_buf++;
3627 tx_desc++;
3628 i++;
3629 if (unlikely(!i)) {
3630 i -= tx_ring->count;
3631 tx_buf = tx_ring->tx_bi;
3632 tx_desc = I40E_TX_DESC(tx_ring, 0);
3633 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003634 /* unmap skb header data */
3635 dma_unmap_single(tx_ring->dev,
3636 dma_unmap_addr(tx_buf, dma),
3637 dma_unmap_len(tx_buf, len),
3638 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003639 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3640 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003641
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003642 tx_buf->raw_buf = NULL;
3643 tx_buf->tx_flags = 0;
3644 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003645 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003646 tx_desc->buffer_addr = 0;
3647 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003648
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003649 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003650 tx_buf++;
3651 tx_desc++;
3652 i++;
3653 if (unlikely(!i)) {
3654 i -= tx_ring->count;
3655 tx_buf = tx_ring->tx_bi;
3656 tx_desc = I40E_TX_DESC(tx_ring, 0);
3657 }
3658
3659 /* update budget accounting */
3660 budget--;
3661 } while (likely(budget));
3662
3663 i += tx_ring->count;
3664 tx_ring->next_to_clean = i;
3665
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003666 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003667 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003668
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003669 return budget > 0;
3670}
3671
3672/**
3673 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3674 * @irq: interrupt number
3675 * @data: pointer to a q_vector
3676 **/
3677static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3678{
3679 struct i40e_q_vector *q_vector = data;
3680 struct i40e_vsi *vsi;
3681
3682 if (!q_vector->tx.ring)
3683 return IRQ_HANDLED;
3684
3685 vsi = q_vector->tx.ring->vsi;
3686 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3687
3688 return IRQ_HANDLED;
3689}
3690
3691/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003692 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003693 * @vsi: the VSI being configured
3694 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003695 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003696 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003697static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003698{
Alexander Duyck493fb302013-09-28 07:01:44 +00003699 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003700 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3701 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003702
3703 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003704 tx_ring->next = q_vector->tx.ring;
3705 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003706 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003707
3708 rx_ring->q_vector = q_vector;
3709 rx_ring->next = q_vector->rx.ring;
3710 q_vector->rx.ring = rx_ring;
3711 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003712}
3713
3714/**
3715 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3716 * @vsi: the VSI being configured
3717 *
3718 * This function maps descriptor rings to the queue-specific vectors
3719 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3720 * one vector per queue pair, but on a constrained vector budget, we
3721 * group the queue pairs as "efficiently" as possible.
3722 **/
3723static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3724{
3725 int qp_remaining = vsi->num_queue_pairs;
3726 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003727 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003728 int v_start = 0;
3729 int qp_idx = 0;
3730
3731 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3732 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003733 * It is also important to go through all the vectors available to be
3734 * sure that if we don't use all the vectors, that the remaining vectors
3735 * are cleared. This is especially important when decreasing the
3736 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003737 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003738 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003739 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3740
3741 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3742
3743 q_vector->num_ringpairs = num_ringpairs;
3744
3745 q_vector->rx.count = 0;
3746 q_vector->tx.count = 0;
3747 q_vector->rx.ring = NULL;
3748 q_vector->tx.ring = NULL;
3749
3750 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003751 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003752 qp_idx++;
3753 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003754 }
3755 }
3756}
3757
3758/**
3759 * i40e_vsi_request_irq - Request IRQ from the OS
3760 * @vsi: the VSI being configured
3761 * @basename: name for the vector
3762 **/
3763static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3764{
3765 struct i40e_pf *pf = vsi->back;
3766 int err;
3767
3768 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3769 err = i40e_vsi_request_irq_msix(vsi, basename);
3770 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3771 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003772 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003773 else
3774 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003775 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003776
3777 if (err)
3778 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3779
3780 return err;
3781}
3782
3783#ifdef CONFIG_NET_POLL_CONTROLLER
3784/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003785 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003786 * @netdev: network interface device structure
3787 *
3788 * This is used by netconsole to send skbs without having to re-enable
3789 * interrupts. It's not called while the normal interrupt routine is executing.
3790 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003791#ifdef I40E_FCOE
3792void i40e_netpoll(struct net_device *netdev)
3793#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003794static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003795#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003796{
3797 struct i40e_netdev_priv *np = netdev_priv(netdev);
3798 struct i40e_vsi *vsi = np->vsi;
3799 struct i40e_pf *pf = vsi->back;
3800 int i;
3801
3802 /* if interface is down do nothing */
3803 if (test_bit(__I40E_DOWN, &vsi->state))
3804 return;
3805
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003806 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3807 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003808 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003809 } else {
3810 i40e_intr(pf->pdev->irq, netdev);
3811 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003812}
3813#endif
3814
3815/**
Neerav Parikh23527302014-06-03 23:50:15 +00003816 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3817 * @pf: the PF being configured
3818 * @pf_q: the PF queue
3819 * @enable: enable or disable state of the queue
3820 *
3821 * This routine will wait for the given Tx queue of the PF to reach the
3822 * enabled or disabled state.
3823 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3824 * multiple retries; else will return 0 in case of success.
3825 **/
3826static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3827{
3828 int i;
3829 u32 tx_reg;
3830
3831 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3832 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3833 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3834 break;
3835
Neerav Parikhf98a2002014-09-13 07:40:44 +00003836 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003837 }
3838 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3839 return -ETIMEDOUT;
3840
3841 return 0;
3842}
3843
3844/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003845 * i40e_vsi_control_tx - Start or stop a VSI's rings
3846 * @vsi: the VSI being configured
3847 * @enable: start or stop the rings
3848 **/
3849static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3850{
3851 struct i40e_pf *pf = vsi->back;
3852 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003853 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003854 u32 tx_reg;
3855
3856 pf_q = vsi->base_queue;
3857 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003858
3859 /* warn the TX unit of coming changes */
3860 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3861 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003862 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00003863
Mitch Williams6c5ef622014-02-20 19:29:16 -08003864 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003865 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003866 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3867 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3868 break;
3869 usleep_range(1000, 2000);
3870 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003871 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003872 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003873 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003874
3875 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003876 if (enable) {
3877 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003878 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003879 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003880 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003881 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003882
3883 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003884 /* No waiting for the Tx queue to disable */
3885 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3886 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003887
3888 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003889 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3890 if (ret) {
3891 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003892 "VSI seid %d Tx ring %d %sable timeout\n",
3893 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003894 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003895 }
3896 }
3897
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003898 if (hw->revision_id == 0)
3899 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003900 return ret;
3901}
3902
3903/**
3904 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3905 * @pf: the PF being configured
3906 * @pf_q: the PF queue
3907 * @enable: enable or disable state of the queue
3908 *
3909 * This routine will wait for the given Rx queue of the PF to reach the
3910 * enabled or disabled state.
3911 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3912 * multiple retries; else will return 0 in case of success.
3913 **/
3914static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3915{
3916 int i;
3917 u32 rx_reg;
3918
3919 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3920 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3921 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3922 break;
3923
Neerav Parikhf98a2002014-09-13 07:40:44 +00003924 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003925 }
3926 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3927 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003928
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003929 return 0;
3930}
3931
3932/**
3933 * i40e_vsi_control_rx - Start or stop a VSI's rings
3934 * @vsi: the VSI being configured
3935 * @enable: start or stop the rings
3936 **/
3937static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3938{
3939 struct i40e_pf *pf = vsi->back;
3940 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003941 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003942 u32 rx_reg;
3943
3944 pf_q = vsi->base_queue;
3945 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003946 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003947 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003948 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3949 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3950 break;
3951 usleep_range(1000, 2000);
3952 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003953
Catherine Sullivan7c122002014-03-14 07:32:29 +00003954 /* Skip if the queue is already in the requested state */
3955 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3956 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003957
3958 /* turn on/off the queue */
3959 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003960 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003961 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003962 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003963 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08003964 /* No waiting for the Tx queue to disable */
3965 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3966 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003967
3968 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003969 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3970 if (ret) {
3971 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003972 "VSI seid %d Rx ring %d %sable timeout\n",
3973 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003974 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003975 }
3976 }
3977
Neerav Parikh23527302014-06-03 23:50:15 +00003978 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003979}
3980
3981/**
3982 * i40e_vsi_control_rings - Start or stop a VSI's rings
3983 * @vsi: the VSI being configured
3984 * @enable: start or stop the rings
3985 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003986int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003987{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003988 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003989
3990 /* do rx first for enable and last for disable */
3991 if (request) {
3992 ret = i40e_vsi_control_rx(vsi, request);
3993 if (ret)
3994 return ret;
3995 ret = i40e_vsi_control_tx(vsi, request);
3996 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003997 /* Ignore return value, we need to shutdown whatever we can */
3998 i40e_vsi_control_tx(vsi, request);
3999 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004000 }
4001
4002 return ret;
4003}
4004
4005/**
4006 * i40e_vsi_free_irq - Free the irq association with the OS
4007 * @vsi: the VSI being configured
4008 **/
4009static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4010{
4011 struct i40e_pf *pf = vsi->back;
4012 struct i40e_hw *hw = &pf->hw;
4013 int base = vsi->base_vector;
4014 u32 val, qp;
4015 int i;
4016
4017 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4018 if (!vsi->q_vectors)
4019 return;
4020
Shannon Nelson63741842014-04-23 04:50:16 +00004021 if (!vsi->irqs_ready)
4022 return;
4023
4024 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004025 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004026 int irq_num;
4027 u16 vector;
4028
4029 vector = i + base;
4030 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004031
4032 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004033 if (!vsi->q_vectors[i] ||
4034 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004035 continue;
4036
Alan Brady96db7762016-09-14 16:24:38 -07004037 /* clear the affinity notifier in the IRQ descriptor */
4038 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004039 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004040 irq_set_affinity_hint(irq_num, NULL);
4041 synchronize_irq(irq_num);
4042 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004043
4044 /* Tear down the interrupt queue link list
4045 *
4046 * We know that they come in pairs and always
4047 * the Rx first, then the Tx. To clear the
4048 * link list, stick the EOL value into the
4049 * next_q field of the registers.
4050 */
4051 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4052 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4053 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4054 val |= I40E_QUEUE_END_OF_LIST
4055 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4056 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4057
4058 while (qp != I40E_QUEUE_END_OF_LIST) {
4059 u32 next;
4060
4061 val = rd32(hw, I40E_QINT_RQCTL(qp));
4062
4063 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4064 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4065 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4066 I40E_QINT_RQCTL_INTEVENT_MASK);
4067
4068 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4069 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4070
4071 wr32(hw, I40E_QINT_RQCTL(qp), val);
4072
4073 val = rd32(hw, I40E_QINT_TQCTL(qp));
4074
4075 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4076 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4077
4078 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4079 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4080 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4081 I40E_QINT_TQCTL_INTEVENT_MASK);
4082
4083 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4084 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4085
4086 wr32(hw, I40E_QINT_TQCTL(qp), val);
4087 qp = next;
4088 }
4089 }
4090 } else {
4091 free_irq(pf->pdev->irq, pf);
4092
4093 val = rd32(hw, I40E_PFINT_LNKLST0);
4094 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4095 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4096 val |= I40E_QUEUE_END_OF_LIST
4097 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4098 wr32(hw, I40E_PFINT_LNKLST0, val);
4099
4100 val = rd32(hw, I40E_QINT_RQCTL(qp));
4101 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4102 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4103 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4104 I40E_QINT_RQCTL_INTEVENT_MASK);
4105
4106 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4107 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4108
4109 wr32(hw, I40E_QINT_RQCTL(qp), val);
4110
4111 val = rd32(hw, I40E_QINT_TQCTL(qp));
4112
4113 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4114 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4115 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4116 I40E_QINT_TQCTL_INTEVENT_MASK);
4117
4118 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4119 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4120
4121 wr32(hw, I40E_QINT_TQCTL(qp), val);
4122 }
4123}
4124
4125/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004126 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4127 * @vsi: the VSI being configured
4128 * @v_idx: Index of vector to be freed
4129 *
4130 * This function frees the memory allocated to the q_vector. In addition if
4131 * NAPI is enabled it will delete any references to the NAPI struct prior
4132 * to freeing the q_vector.
4133 **/
4134static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4135{
4136 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004137 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004138
4139 if (!q_vector)
4140 return;
4141
4142 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004143 i40e_for_each_ring(ring, q_vector->tx)
4144 ring->q_vector = NULL;
4145
4146 i40e_for_each_ring(ring, q_vector->rx)
4147 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004148
4149 /* only VSI w/ an associated netdev is set up w/ NAPI */
4150 if (vsi->netdev)
4151 netif_napi_del(&q_vector->napi);
4152
4153 vsi->q_vectors[v_idx] = NULL;
4154
4155 kfree_rcu(q_vector, rcu);
4156}
4157
4158/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004159 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4160 * @vsi: the VSI being un-configured
4161 *
4162 * This frees the memory allocated to the q_vectors and
4163 * deletes references to the NAPI struct.
4164 **/
4165static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4166{
4167 int v_idx;
4168
Alexander Duyck493fb302013-09-28 07:01:44 +00004169 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4170 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004171}
4172
4173/**
4174 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4175 * @pf: board private structure
4176 **/
4177static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4178{
4179 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4180 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4181 pci_disable_msix(pf->pdev);
4182 kfree(pf->msix_entries);
4183 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004184 kfree(pf->irq_pile);
4185 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004186 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4187 pci_disable_msi(pf->pdev);
4188 }
4189 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4190}
4191
4192/**
4193 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4194 * @pf: board private structure
4195 *
4196 * We go through and clear interrupt specific resources and reset the structure
4197 * to pre-load conditions
4198 **/
4199static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4200{
4201 int i;
4202
Shannon Nelsone1477582015-02-21 06:44:33 +00004203 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004204 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004205 synchronize_irq(pf->msix_entries[0].vector);
4206 free_irq(pf->msix_entries[0].vector, pf);
4207 }
4208
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004209 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4210 I40E_IWARP_IRQ_PILE_ID);
4211
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004212 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004213 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004214 if (pf->vsi[i])
4215 i40e_vsi_free_q_vectors(pf->vsi[i]);
4216 i40e_reset_interrupt_capability(pf);
4217}
4218
4219/**
4220 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4221 * @vsi: the VSI being configured
4222 **/
4223static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4224{
4225 int q_idx;
4226
4227 if (!vsi->netdev)
4228 return;
4229
4230 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004231 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004232}
4233
4234/**
4235 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4236 * @vsi: the VSI being configured
4237 **/
4238static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4239{
4240 int q_idx;
4241
4242 if (!vsi->netdev)
4243 return;
4244
4245 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004246 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004247}
4248
4249/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004250 * i40e_vsi_close - Shut down a VSI
4251 * @vsi: the vsi to be quelled
4252 **/
4253static void i40e_vsi_close(struct i40e_vsi *vsi)
4254{
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004255 bool reset = false;
4256
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004257 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4258 i40e_down(vsi);
4259 i40e_vsi_free_irq(vsi);
4260 i40e_vsi_free_tx_resources(vsi);
4261 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004262 vsi->current_netdev_flags = 0;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004263 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4264 reset = true;
4265 i40e_notify_client_of_netdev_close(vsi, reset);
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004266}
4267
4268/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004269 * i40e_quiesce_vsi - Pause a given VSI
4270 * @vsi: the VSI being paused
4271 **/
4272static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4273{
4274 if (test_bit(__I40E_DOWN, &vsi->state))
4275 return;
4276
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004277 /* No need to disable FCoE VSI when Tx suspended */
4278 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4279 vsi->type == I40E_VSI_FCOE) {
4280 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004281 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004282 return;
4283 }
4284
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004285 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004286 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004287 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004288 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004289 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004290}
4291
4292/**
4293 * i40e_unquiesce_vsi - Resume a given VSI
4294 * @vsi: the VSI being resumed
4295 **/
4296static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4297{
4298 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4299 return;
4300
4301 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4302 if (vsi->netdev && netif_running(vsi->netdev))
4303 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4304 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004305 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004306}
4307
4308/**
4309 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4310 * @pf: the PF
4311 **/
4312static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4313{
4314 int v;
4315
Mitch Williams505682c2014-05-20 08:01:37 +00004316 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004317 if (pf->vsi[v])
4318 i40e_quiesce_vsi(pf->vsi[v]);
4319 }
4320}
4321
4322/**
4323 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4324 * @pf: the PF
4325 **/
4326static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4327{
4328 int v;
4329
Mitch Williams505682c2014-05-20 08:01:37 +00004330 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004331 if (pf->vsi[v])
4332 i40e_unquiesce_vsi(pf->vsi[v]);
4333 }
4334}
4335
Neerav Parikh69129dc2014-11-12 00:18:46 +00004336#ifdef CONFIG_I40E_DCB
4337/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004338 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004339 * @vsi: the VSI being configured
4340 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004341 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004342 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004343static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004344{
4345 struct i40e_pf *pf = vsi->back;
4346 int i, pf_q, ret;
4347
4348 pf_q = vsi->base_queue;
4349 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4350 /* Check and wait for the disable status of the queue */
4351 ret = i40e_pf_txq_wait(pf, pf_q, false);
4352 if (ret) {
4353 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004354 "VSI seid %d Tx ring %d disable timeout\n",
4355 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004356 return ret;
4357 }
4358 }
4359
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004360 pf_q = vsi->base_queue;
4361 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4362 /* Check and wait for the disable status of the queue */
4363 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4364 if (ret) {
4365 dev_info(&pf->pdev->dev,
4366 "VSI seid %d Rx ring %d disable timeout\n",
4367 vsi->seid, pf_q);
4368 return ret;
4369 }
4370 }
4371
Neerav Parikh69129dc2014-11-12 00:18:46 +00004372 return 0;
4373}
4374
4375/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004376 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004377 * @pf: the PF
4378 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004379 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004380 * VSIs that are managed by this PF.
4381 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004382static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004383{
4384 int v, ret = 0;
4385
4386 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004387 /* No need to wait for FCoE VSI queues */
4388 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004389 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004390 if (ret)
4391 break;
4392 }
4393 }
4394
4395 return ret;
4396}
4397
4398#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004399
4400/**
4401 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4402 * @q_idx: TX queue number
4403 * @vsi: Pointer to VSI struct
4404 *
4405 * This function checks specified queue for given VSI. Detects hung condition.
4406 * Sets hung bit since it is two step process. Before next run of service task
4407 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4408 * hung condition remain unchanged and during subsequent run, this function
4409 * issues SW interrupt to recover from hung condition.
4410 **/
4411static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4412{
4413 struct i40e_ring *tx_ring = NULL;
4414 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004415 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004416 int i;
4417
4418 pf = vsi->back;
4419
4420 /* now that we have an index, find the tx_ring struct */
4421 for (i = 0; i < vsi->num_queue_pairs; i++) {
4422 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4423 if (q_idx == vsi->tx_rings[i]->queue_index) {
4424 tx_ring = vsi->tx_rings[i];
4425 break;
4426 }
4427 }
4428 }
4429
4430 if (!tx_ring)
4431 return;
4432
4433 /* Read interrupt register */
4434 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4435 val = rd32(&pf->hw,
4436 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4437 tx_ring->vsi->base_vector - 1));
4438 else
4439 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4440
4441 head = i40e_get_head(tx_ring);
4442
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004443 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004444
Kiran Patil9c6c1252015-11-06 15:26:02 -08004445 /* HW is done executing descriptors, updated HEAD write back,
4446 * but SW hasn't processed those descriptors. If interrupt is
4447 * not generated from this point ON, it could result into
4448 * dev_watchdog detecting timeout on those netdev_queue,
4449 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004450 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004451 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004452 /* NAPI Poll didn't run and clear since it was set */
4453 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4454 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004455 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",
4456 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004457 tx_ring->next_to_clean, head,
4458 tx_ring->next_to_use,
4459 readl(tx_ring->tail));
4460 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4461 vsi->seid, q_idx, val);
4462 i40e_force_wb(vsi, tx_ring->q_vector);
4463 } else {
4464 /* First Chance - detected possible hung */
4465 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4466 &tx_ring->q_vector->hung_detected);
4467 }
4468 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004469
4470 /* This is the case where we have interrupts missing,
4471 * so the tx_pending in HW will most likely be 0, but we
4472 * will have tx_pending in SW since the WB happened but the
4473 * interrupt got lost.
4474 */
4475 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4476 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
4477 if (napi_reschedule(&tx_ring->q_vector->napi))
4478 tx_ring->tx_stats.tx_lost_interrupt++;
4479 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004480}
4481
4482/**
4483 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4484 * @pf: pointer to PF struct
4485 *
4486 * LAN VSI has netdev and netdev has TX queues. This function is to check
4487 * each of those TX queues if they are hung, trigger recovery by issuing
4488 * SW interrupt.
4489 **/
4490static void i40e_detect_recover_hung(struct i40e_pf *pf)
4491{
4492 struct net_device *netdev;
4493 struct i40e_vsi *vsi;
4494 int i;
4495
4496 /* Only for LAN VSI */
4497 vsi = pf->vsi[pf->lan_vsi];
4498
4499 if (!vsi)
4500 return;
4501
4502 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4503 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4504 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4505 return;
4506
4507 /* Make sure type is MAIN VSI */
4508 if (vsi->type != I40E_VSI_MAIN)
4509 return;
4510
4511 netdev = vsi->netdev;
4512 if (!netdev)
4513 return;
4514
4515 /* Bail out if netif_carrier is not OK */
4516 if (!netif_carrier_ok(netdev))
4517 return;
4518
4519 /* Go thru' TX queues for netdev */
4520 for (i = 0; i < netdev->num_tx_queues; i++) {
4521 struct netdev_queue *q;
4522
4523 q = netdev_get_tx_queue(netdev, i);
4524 if (q)
4525 i40e_detect_recover_hung_queue(i, vsi);
4526 }
4527}
4528
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004529/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004530 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004531 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004532 *
4533 * Get TC map for ISCSI PF type that will include iSCSI TC
4534 * and LAN TC.
4535 **/
4536static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4537{
4538 struct i40e_dcb_app_priority_table app;
4539 struct i40e_hw *hw = &pf->hw;
4540 u8 enabled_tc = 1; /* TC0 is always enabled */
4541 u8 tc, i;
4542 /* Get the iSCSI APP TLV */
4543 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4544
4545 for (i = 0; i < dcbcfg->numapps; i++) {
4546 app = dcbcfg->app[i];
4547 if (app.selector == I40E_APP_SEL_TCPIP &&
4548 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4549 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004550 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004551 break;
4552 }
4553 }
4554
4555 return enabled_tc;
4556}
4557
4558/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004559 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4560 * @dcbcfg: the corresponding DCBx configuration structure
4561 *
4562 * Return the number of TCs from given DCBx configuration
4563 **/
4564static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4565{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004566 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004567 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004568 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004569
4570 /* Scan the ETS Config Priority Table to find
4571 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004572 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004573 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004574 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4575 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4576
4577 /* Now scan the bitmask to check for
4578 * contiguous TCs starting with TC0
4579 */
4580 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4581 if (num_tc & BIT(i)) {
4582 if (!tc_unused) {
4583 ret++;
4584 } else {
4585 pr_err("Non-contiguous TC - Disabling DCB\n");
4586 return 1;
4587 }
4588 } else {
4589 tc_unused = 1;
4590 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004591 }
4592
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004593 /* There is always at least TC0 */
4594 if (!ret)
4595 ret = 1;
4596
4597 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004598}
4599
4600/**
4601 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4602 * @dcbcfg: the corresponding DCBx configuration structure
4603 *
4604 * Query the current DCB configuration and return the number of
4605 * traffic classes enabled from the given DCBX config
4606 **/
4607static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4608{
4609 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4610 u8 enabled_tc = 1;
4611 u8 i;
4612
4613 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004614 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004615
4616 return enabled_tc;
4617}
4618
4619/**
4620 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4621 * @pf: PF being queried
4622 *
4623 * Return number of traffic classes enabled for the given PF
4624 **/
4625static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4626{
4627 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004628 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004629 u8 num_tc = 0;
4630 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4631
4632 /* If DCB is not enabled then always in single TC */
4633 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4634 return 1;
4635
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004636 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004637 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4638 return i40e_dcb_get_num_tc(dcbcfg);
4639
4640 /* MFP mode return count of enabled TCs for this PF */
4641 if (pf->hw.func_caps.iscsi)
4642 enabled_tc = i40e_get_iscsi_tc_map(pf);
4643 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004644 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004645
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004646 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004647 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004648 num_tc++;
4649 }
4650 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004651}
4652
4653/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004654 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4655 * @pf: PF being queried
4656 *
4657 * Return a bitmap for enabled traffic classes for this PF.
4658 **/
4659static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4660{
4661 /* If DCB is not enabled for this PF then just return default TC */
4662 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004663 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004664
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004665 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004666 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4667 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4668
Neerav Parikhfc51de92015-02-24 06:58:53 +00004669 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004670 if (pf->hw.func_caps.iscsi)
4671 return i40e_get_iscsi_tc_map(pf);
4672 else
David Ertmanea6acb72016-09-20 07:10:50 -07004673 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004674}
4675
4676/**
4677 * i40e_vsi_get_bw_info - Query VSI BW Information
4678 * @vsi: the VSI being queried
4679 *
4680 * Returns 0 on success, negative value on failure
4681 **/
4682static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4683{
4684 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4685 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4686 struct i40e_pf *pf = vsi->back;
4687 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004688 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004689 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004690 int i;
4691
4692 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004693 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4694 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004695 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004696 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4697 i40e_stat_str(&pf->hw, ret),
4698 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004699 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004700 }
4701
4702 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004703 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4704 NULL);
4705 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004706 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004707 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4708 i40e_stat_str(&pf->hw, ret),
4709 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004710 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004711 }
4712
4713 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4714 dev_info(&pf->pdev->dev,
4715 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4716 bw_config.tc_valid_bits,
4717 bw_ets_config.tc_valid_bits);
4718 /* Still continuing */
4719 }
4720
4721 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4722 vsi->bw_max_quanta = bw_config.max_bw;
4723 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4724 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4725 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4726 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4727 vsi->bw_ets_limit_credits[i] =
4728 le16_to_cpu(bw_ets_config.credits[i]);
4729 /* 3 bits out of 4 for each TC */
4730 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4731 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004732
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004733 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004734}
4735
4736/**
4737 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4738 * @vsi: the VSI being configured
4739 * @enabled_tc: TC bitmap
4740 * @bw_credits: BW shared credits per TC
4741 *
4742 * Returns 0 on success, negative value on failure
4743 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004744static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004745 u8 *bw_share)
4746{
4747 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004748 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004749 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004750
4751 bw_data.tc_valid_bits = enabled_tc;
4752 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4753 bw_data.tc_bw_credits[i] = bw_share[i];
4754
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004755 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4756 NULL);
4757 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004758 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004759 "AQ command Config VSI BW allocation per TC failed = %d\n",
4760 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004761 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004762 }
4763
4764 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4765 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4766
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004767 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004768}
4769
4770/**
4771 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4772 * @vsi: the VSI being configured
4773 * @enabled_tc: TC map to be enabled
4774 *
4775 **/
4776static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4777{
4778 struct net_device *netdev = vsi->netdev;
4779 struct i40e_pf *pf = vsi->back;
4780 struct i40e_hw *hw = &pf->hw;
4781 u8 netdev_tc = 0;
4782 int i;
4783 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4784
4785 if (!netdev)
4786 return;
4787
4788 if (!enabled_tc) {
4789 netdev_reset_tc(netdev);
4790 return;
4791 }
4792
4793 /* Set up actual enabled TCs on the VSI */
4794 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4795 return;
4796
4797 /* set per TC queues for the VSI */
4798 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4799 /* Only set TC queues for enabled tcs
4800 *
4801 * e.g. For a VSI that has TC0 and TC3 enabled the
4802 * enabled_tc bitmap would be 0x00001001; the driver
4803 * will set the numtc for netdev as 2 that will be
4804 * referenced by the netdev layer as TC 0 and 1.
4805 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004806 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004807 netdev_set_tc_queue(netdev,
4808 vsi->tc_config.tc_info[i].netdev_tc,
4809 vsi->tc_config.tc_info[i].qcount,
4810 vsi->tc_config.tc_info[i].qoffset);
4811 }
4812
4813 /* Assign UP2TC map for the VSI */
4814 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4815 /* Get the actual TC# for the UP */
4816 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4817 /* Get the mapped netdev TC# for the UP */
4818 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4819 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4820 }
4821}
4822
4823/**
4824 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4825 * @vsi: the VSI being configured
4826 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4827 **/
4828static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4829 struct i40e_vsi_context *ctxt)
4830{
4831 /* copy just the sections touched not the entire info
4832 * since not all sections are valid as returned by
4833 * update vsi params
4834 */
4835 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4836 memcpy(&vsi->info.queue_mapping,
4837 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4838 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4839 sizeof(vsi->info.tc_mapping));
4840}
4841
4842/**
4843 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4844 * @vsi: VSI to be configured
4845 * @enabled_tc: TC bitmap
4846 *
4847 * This configures a particular VSI for TCs that are mapped to the
4848 * given TC bitmap. It uses default bandwidth share for TCs across
4849 * VSIs to configure TC for a particular VSI.
4850 *
4851 * NOTE:
4852 * It is expected that the VSI queues have been quisced before calling
4853 * this function.
4854 **/
4855static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4856{
4857 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4858 struct i40e_vsi_context ctxt;
4859 int ret = 0;
4860 int i;
4861
4862 /* Check if enabled_tc is same as existing or new TCs */
4863 if (vsi->tc_config.enabled_tc == enabled_tc)
4864 return ret;
4865
4866 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4867 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004868 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004869 bw_share[i] = 1;
4870 }
4871
4872 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4873 if (ret) {
4874 dev_info(&vsi->back->pdev->dev,
4875 "Failed configuring TC map %d for VSI %d\n",
4876 enabled_tc, vsi->seid);
4877 goto out;
4878 }
4879
4880 /* Update Queue Pairs Mapping for currently enabled UPs */
4881 ctxt.seid = vsi->seid;
4882 ctxt.pf_num = vsi->back->hw.pf_id;
4883 ctxt.vf_num = 0;
4884 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004885 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004886 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4887
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004888 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4889 ctxt.info.valid_sections |=
4890 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4891 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4892 }
4893
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004894 /* Update the VSI after updating the VSI queue-mapping information */
4895 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4896 if (ret) {
4897 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004898 "Update vsi tc config failed, err %s aq_err %s\n",
4899 i40e_stat_str(&vsi->back->hw, ret),
4900 i40e_aq_str(&vsi->back->hw,
4901 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004902 goto out;
4903 }
4904 /* update the local VSI info with updated queue map */
4905 i40e_vsi_update_queue_map(vsi, &ctxt);
4906 vsi->info.valid_sections = 0;
4907
4908 /* Update current VSI BW information */
4909 ret = i40e_vsi_get_bw_info(vsi);
4910 if (ret) {
4911 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004912 "Failed updating vsi bw info, err %s aq_err %s\n",
4913 i40e_stat_str(&vsi->back->hw, ret),
4914 i40e_aq_str(&vsi->back->hw,
4915 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004916 goto out;
4917 }
4918
4919 /* Update the netdev TC setup */
4920 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4921out:
4922 return ret;
4923}
4924
4925/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004926 * i40e_veb_config_tc - Configure TCs for given VEB
4927 * @veb: given VEB
4928 * @enabled_tc: TC bitmap
4929 *
4930 * Configures given TC bitmap for VEB (switching) element
4931 **/
4932int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4933{
4934 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4935 struct i40e_pf *pf = veb->pf;
4936 int ret = 0;
4937 int i;
4938
4939 /* No TCs or already enabled TCs just return */
4940 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4941 return ret;
4942
4943 bw_data.tc_valid_bits = enabled_tc;
4944 /* bw_data.absolute_credits is not set (relative) */
4945
4946 /* Enable ETS TCs with equal BW Share for now */
4947 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004948 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004949 bw_data.tc_bw_share_credits[i] = 1;
4950 }
4951
4952 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4953 &bw_data, NULL);
4954 if (ret) {
4955 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004956 "VEB bw config failed, err %s aq_err %s\n",
4957 i40e_stat_str(&pf->hw, ret),
4958 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004959 goto out;
4960 }
4961
4962 /* Update the BW information */
4963 ret = i40e_veb_get_bw_info(veb);
4964 if (ret) {
4965 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004966 "Failed getting veb bw config, err %s aq_err %s\n",
4967 i40e_stat_str(&pf->hw, ret),
4968 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004969 }
4970
4971out:
4972 return ret;
4973}
4974
4975#ifdef CONFIG_I40E_DCB
4976/**
4977 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4978 * @pf: PF struct
4979 *
4980 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4981 * the caller would've quiesce all the VSIs before calling
4982 * this function
4983 **/
4984static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4985{
4986 u8 tc_map = 0;
4987 int ret;
4988 u8 v;
4989
4990 /* Enable the TCs available on PF to all VEBs */
4991 tc_map = i40e_pf_get_tc_map(pf);
4992 for (v = 0; v < I40E_MAX_VEB; v++) {
4993 if (!pf->veb[v])
4994 continue;
4995 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4996 if (ret) {
4997 dev_info(&pf->pdev->dev,
4998 "Failed configuring TC for VEB seid=%d\n",
4999 pf->veb[v]->seid);
5000 /* Will try to configure as many components */
5001 }
5002 }
5003
5004 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005005 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005006 if (!pf->vsi[v])
5007 continue;
5008
5009 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07005010#ifdef I40E_FCOE
5011 * - For FCoE VSI only enable the TC configured
5012 * as per the APP TLV
5013#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005014 * - For all others keep them at TC0 for now
5015 */
5016 if (v == pf->lan_vsi)
5017 tc_map = i40e_pf_get_tc_map(pf);
5018 else
David Ertmanea6acb72016-09-20 07:10:50 -07005019 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Vasu Dev38e00432014-08-01 13:27:03 -07005020#ifdef I40E_FCOE
5021 if (pf->vsi[v]->type == I40E_VSI_FCOE)
5022 tc_map = i40e_get_fcoe_tc_map(pf);
5023#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005024
5025 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5026 if (ret) {
5027 dev_info(&pf->pdev->dev,
5028 "Failed configuring TC for VSI seid=%d\n",
5029 pf->vsi[v]->seid);
5030 /* Will try to configure as many components */
5031 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005032 /* Re-configure VSI vectors based on updated TC map */
5033 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005034 if (pf->vsi[v]->netdev)
5035 i40e_dcbnl_set_all(pf->vsi[v]);
5036 }
5037 }
5038}
5039
5040/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005041 * i40e_resume_port_tx - Resume port Tx
5042 * @pf: PF struct
5043 *
5044 * Resume a port's Tx and issue a PF reset in case of failure to
5045 * resume.
5046 **/
5047static int i40e_resume_port_tx(struct i40e_pf *pf)
5048{
5049 struct i40e_hw *hw = &pf->hw;
5050 int ret;
5051
5052 ret = i40e_aq_resume_port_tx(hw, NULL);
5053 if (ret) {
5054 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005055 "Resume Port Tx 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 Parikh2fd75f32014-11-12 00:18:20 +00005058 /* Schedule PF reset to recover */
5059 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5060 i40e_service_event_schedule(pf);
5061 }
5062
5063 return ret;
5064}
5065
5066/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005067 * i40e_init_pf_dcb - Initialize DCB configuration
5068 * @pf: PF being configured
5069 *
5070 * Query the current DCB configuration and cache it
5071 * in the hardware structure
5072 **/
5073static int i40e_init_pf_dcb(struct i40e_pf *pf)
5074{
5075 struct i40e_hw *hw = &pf->hw;
5076 int err = 0;
5077
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005078 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005079 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005080 goto out;
5081
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005082 /* Get the initial DCB configuration */
5083 err = i40e_init_dcb(hw);
5084 if (!err) {
5085 /* Device/Function is not DCBX capable */
5086 if ((!hw->func_caps.dcb) ||
5087 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5088 dev_info(&pf->pdev->dev,
5089 "DCBX offload is not supported or is disabled for this PF.\n");
5090
5091 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5092 goto out;
5093
5094 } else {
5095 /* When status is not DISABLED then DCBX in FW */
5096 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5097 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005098
5099 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005100 /* Enable DCB tagging only when more than one TC
5101 * or explicitly disable if only one TC
5102 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005103 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5104 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005105 else
5106 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005107 dev_dbg(&pf->pdev->dev,
5108 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005109 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005110 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005111 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005112 "Query for DCB configuration failed, err %s aq_err %s\n",
5113 i40e_stat_str(&pf->hw, err),
5114 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005115 }
5116
5117out:
5118 return err;
5119}
5120#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005121#define SPEED_SIZE 14
5122#define FC_SIZE 8
5123/**
5124 * i40e_print_link_message - print link up or down
5125 * @vsi: the VSI for which link needs a message
5126 */
Matt Jaredc156f852015-08-27 11:42:39 -04005127void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005128{
Shannon Nelsona9165492015-09-03 17:19:00 -04005129 char *speed = "Unknown";
5130 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005131
Matt Jaredc156f852015-08-27 11:42:39 -04005132 if (vsi->current_isup == isup)
5133 return;
5134 vsi->current_isup = isup;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005135 if (!isup) {
5136 netdev_info(vsi->netdev, "NIC Link is Down\n");
5137 return;
5138 }
5139
Greg Rose148c2d82014-12-11 07:06:27 +00005140 /* Warn user if link speed on NPAR enabled partition is not at
5141 * least 10GB
5142 */
5143 if (vsi->back->hw.func_caps.npar_enable &&
5144 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5145 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5146 netdev_warn(vsi->netdev,
5147 "The partition detected link speed that is less than 10Gbps\n");
5148
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005149 switch (vsi->back->hw.phy.link_info.link_speed) {
5150 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005151 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005152 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005153 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005154 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005155 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005156 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005157 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005158 break;
5159 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005160 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005161 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005162 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005163 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005164 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005165 default:
5166 break;
5167 }
5168
5169 switch (vsi->back->hw.fc.current_mode) {
5170 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005171 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005172 break;
5173 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005174 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005175 break;
5176 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005177 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005178 break;
5179 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005180 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005181 break;
5182 }
5183
Shannon Nelsona9165492015-09-03 17:19:00 -04005184 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005185 speed, fc);
5186}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005187
5188/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005189 * i40e_up_complete - Finish the last steps of bringing up a connection
5190 * @vsi: the VSI being configured
5191 **/
5192static int i40e_up_complete(struct i40e_vsi *vsi)
5193{
5194 struct i40e_pf *pf = vsi->back;
5195 int err;
5196
5197 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5198 i40e_vsi_configure_msix(vsi);
5199 else
5200 i40e_configure_msi_and_legacy(vsi);
5201
5202 /* start rings */
5203 err = i40e_vsi_control_rings(vsi, true);
5204 if (err)
5205 return err;
5206
5207 clear_bit(__I40E_DOWN, &vsi->state);
5208 i40e_napi_enable_all(vsi);
5209 i40e_vsi_enable_irq(vsi);
5210
5211 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5212 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005213 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005214 netif_tx_start_all_queues(vsi->netdev);
5215 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005216 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005217 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005218 /* need to check for qualified module here*/
5219 if ((pf->hw.phy.link_info.link_info &
5220 I40E_AQ_MEDIA_AVAILABLE) &&
5221 (!(pf->hw.phy.link_info.an_info &
5222 I40E_AQ_QUALIFIED_MODULE)))
5223 netdev_err(vsi->netdev,
5224 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005225 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005226
5227 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005228 if (vsi->type == I40E_VSI_FDIR) {
5229 /* reset fd counters */
5230 pf->fd_add_err = pf->fd_atr_cnt = 0;
5231 if (pf->fd_tcp_rule > 0) {
Jacob Keller234dc4e2016-09-06 18:05:09 -07005232 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005233 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5234 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005235 pf->fd_tcp_rule = 0;
5236 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005237 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005238 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005239
5240 /* On the next run of the service_task, notify any clients of the new
5241 * opened netdev
5242 */
5243 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005244 i40e_service_event_schedule(pf);
5245
5246 return 0;
5247}
5248
5249/**
5250 * i40e_vsi_reinit_locked - Reset the VSI
5251 * @vsi: the VSI being configured
5252 *
5253 * Rebuild the ring structs after some configuration
5254 * has changed, e.g. MTU size.
5255 **/
5256static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5257{
5258 struct i40e_pf *pf = vsi->back;
5259
5260 WARN_ON(in_interrupt());
5261 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5262 usleep_range(1000, 2000);
5263 i40e_down(vsi);
5264
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005265 i40e_up(vsi);
5266 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5267}
5268
5269/**
5270 * i40e_up - Bring the connection back up after being down
5271 * @vsi: the VSI being configured
5272 **/
5273int i40e_up(struct i40e_vsi *vsi)
5274{
5275 int err;
5276
5277 err = i40e_vsi_configure(vsi);
5278 if (!err)
5279 err = i40e_up_complete(vsi);
5280
5281 return err;
5282}
5283
5284/**
5285 * i40e_down - Shutdown the connection processing
5286 * @vsi: the VSI being stopped
5287 **/
5288void i40e_down(struct i40e_vsi *vsi)
5289{
5290 int i;
5291
5292 /* It is assumed that the caller of this function
5293 * sets the vsi->state __I40E_DOWN bit.
5294 */
5295 if (vsi->netdev) {
5296 netif_carrier_off(vsi->netdev);
5297 netif_tx_disable(vsi->netdev);
5298 }
5299 i40e_vsi_disable_irq(vsi);
5300 i40e_vsi_control_rings(vsi, false);
5301 i40e_napi_disable_all(vsi);
5302
5303 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005304 i40e_clean_tx_ring(vsi->tx_rings[i]);
5305 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005306 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005307
5308 i40e_notify_client_of_netdev_close(vsi, false);
5309
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005310}
5311
5312/**
5313 * i40e_setup_tc - configure multiple traffic classes
5314 * @netdev: net device to configure
5315 * @tc: number of traffic classes to enable
5316 **/
5317static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5318{
5319 struct i40e_netdev_priv *np = netdev_priv(netdev);
5320 struct i40e_vsi *vsi = np->vsi;
5321 struct i40e_pf *pf = vsi->back;
5322 u8 enabled_tc = 0;
5323 int ret = -EINVAL;
5324 int i;
5325
5326 /* Check if DCB enabled to continue */
5327 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5328 netdev_info(netdev, "DCB is not enabled for adapter\n");
5329 goto exit;
5330 }
5331
5332 /* Check if MFP enabled */
5333 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5334 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5335 goto exit;
5336 }
5337
5338 /* Check whether tc count is within enabled limit */
5339 if (tc > i40e_pf_get_num_tc(pf)) {
5340 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5341 goto exit;
5342 }
5343
5344 /* Generate TC map for number of tc requested */
5345 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005346 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005347
5348 /* Requesting same TC configuration as already enabled */
5349 if (enabled_tc == vsi->tc_config.enabled_tc)
5350 return 0;
5351
5352 /* Quiesce VSI queues */
5353 i40e_quiesce_vsi(vsi);
5354
5355 /* Configure VSI for enabled TCs */
5356 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5357 if (ret) {
5358 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5359 vsi->seid);
5360 goto exit;
5361 }
5362
5363 /* Unquiesce VSI */
5364 i40e_unquiesce_vsi(vsi);
5365
5366exit:
5367 return ret;
5368}
5369
John Fastabende4c67342016-02-16 21:16:15 -08005370#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005371int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5372 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005373#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005374static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5375 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005376#endif
5377{
John Fastabend16e5cc62016-02-16 21:16:43 -08005378 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005379 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005380 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005381}
5382
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005383/**
5384 * i40e_open - Called when a network interface is made active
5385 * @netdev: network interface device structure
5386 *
5387 * The open entry point is called when a network interface is made
5388 * active by the system (IFF_UP). At this point all resources needed
5389 * for transmit and receive operations are allocated, the interrupt
5390 * handler is registered with the OS, the netdev watchdog subtask is
5391 * enabled, and the stack is notified that the interface is ready.
5392 *
5393 * Returns 0 on success, negative value on failure
5394 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005395int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005396{
5397 struct i40e_netdev_priv *np = netdev_priv(netdev);
5398 struct i40e_vsi *vsi = np->vsi;
5399 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005400 int err;
5401
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005402 /* disallow open during test or if eeprom is broken */
5403 if (test_bit(__I40E_TESTING, &pf->state) ||
5404 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005405 return -EBUSY;
5406
5407 netif_carrier_off(netdev);
5408
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005409 err = i40e_vsi_open(vsi);
5410 if (err)
5411 return err;
5412
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005413 /* configure global TSO hardware offload settings */
5414 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5415 TCP_FLAG_FIN) >> 16);
5416 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5417 TCP_FLAG_FIN |
5418 TCP_FLAG_CWR) >> 16);
5419 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5420
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005421 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005422
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005423 return 0;
5424}
5425
5426/**
5427 * i40e_vsi_open -
5428 * @vsi: the VSI to open
5429 *
5430 * Finish initialization of the VSI.
5431 *
5432 * Returns 0 on success, negative value on failure
5433 **/
5434int i40e_vsi_open(struct i40e_vsi *vsi)
5435{
5436 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005437 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005438 int err;
5439
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005440 /* allocate descriptors */
5441 err = i40e_vsi_setup_tx_resources(vsi);
5442 if (err)
5443 goto err_setup_tx;
5444 err = i40e_vsi_setup_rx_resources(vsi);
5445 if (err)
5446 goto err_setup_rx;
5447
5448 err = i40e_vsi_configure(vsi);
5449 if (err)
5450 goto err_setup_rx;
5451
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005452 if (vsi->netdev) {
5453 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5454 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5455 err = i40e_vsi_request_irq(vsi, int_name);
5456 if (err)
5457 goto err_setup_rx;
5458
5459 /* Notify the stack of the actual queue counts. */
5460 err = netif_set_real_num_tx_queues(vsi->netdev,
5461 vsi->num_queue_pairs);
5462 if (err)
5463 goto err_set_queues;
5464
5465 err = netif_set_real_num_rx_queues(vsi->netdev,
5466 vsi->num_queue_pairs);
5467 if (err)
5468 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005469
5470 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005471 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005472 dev_driver_string(&pf->pdev->dev),
5473 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005474 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005475
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005476 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005477 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005478 goto err_setup_rx;
5479 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005480
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005481 err = i40e_up_complete(vsi);
5482 if (err)
5483 goto err_up_complete;
5484
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005485 return 0;
5486
5487err_up_complete:
5488 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005489err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005490 i40e_vsi_free_irq(vsi);
5491err_setup_rx:
5492 i40e_vsi_free_rx_resources(vsi);
5493err_setup_tx:
5494 i40e_vsi_free_tx_resources(vsi);
5495 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005496 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005497
5498 return err;
5499}
5500
5501/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005502 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005503 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005504 *
5505 * This function destroys the hlist where all the Flow Director
5506 * filters were saved.
5507 **/
5508static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5509{
5510 struct i40e_fdir_filter *filter;
5511 struct hlist_node *node2;
5512
5513 hlist_for_each_entry_safe(filter, node2,
5514 &pf->fdir_filter_list, fdir_node) {
5515 hlist_del(&filter->fdir_node);
5516 kfree(filter);
5517 }
5518 pf->fdir_pf_active_filters = 0;
5519}
5520
5521/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005522 * i40e_close - Disables a network interface
5523 * @netdev: network interface device structure
5524 *
5525 * The close entry point is called when an interface is de-activated
5526 * by the OS. The hardware is still under the driver's control, but
5527 * this netdev interface is disabled.
5528 *
5529 * Returns 0, this is not allowed to fail
5530 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005531int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005532{
5533 struct i40e_netdev_priv *np = netdev_priv(netdev);
5534 struct i40e_vsi *vsi = np->vsi;
5535
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005536 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005537
5538 return 0;
5539}
5540
5541/**
5542 * i40e_do_reset - Start a PF or Core Reset sequence
5543 * @pf: board private structure
5544 * @reset_flags: which reset is requested
5545 *
5546 * The essential difference in resets is that the PF Reset
5547 * doesn't clear the packet buffers, doesn't reset the PE
5548 * firmware, and doesn't bother the other PFs on the chip.
5549 **/
5550void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5551{
5552 u32 val;
5553
5554 WARN_ON(in_interrupt());
5555
Mitch Williams263fc482014-04-23 04:50:11 +00005556
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005557 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005558 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005559
5560 /* Request a Global Reset
5561 *
5562 * This will start the chip's countdown to the actual full
5563 * chip reset event, and a warning interrupt to be sent
5564 * to all PFs, including the requestor. Our handler
5565 * for the warning interrupt will deal with the shutdown
5566 * and recovery of the switch setup.
5567 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005568 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005569 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5570 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5571 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5572
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005573 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005574
5575 /* Request a Core Reset
5576 *
5577 * Same as Global Reset, except does *not* include the MAC/PHY
5578 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005579 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005580 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5581 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5582 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5583 i40e_flush(&pf->hw);
5584
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005585 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005586
5587 /* Request a PF Reset
5588 *
5589 * Resets only the PF-specific registers
5590 *
5591 * This goes directly to the tear-down and rebuild of
5592 * the switch, since we need to do all the recovery as
5593 * for the Core Reset.
5594 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005595 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005596 i40e_handle_reset_warning(pf);
5597
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005598 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005599 int v;
5600
5601 /* Find the VSI(s) that requested a re-init */
5602 dev_info(&pf->pdev->dev,
5603 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005604 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005605 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005606
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005607 if (vsi != NULL &&
5608 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5609 i40e_vsi_reinit_locked(pf->vsi[v]);
5610 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5611 }
5612 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005613 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005614 int v;
5615
5616 /* Find the VSI(s) that needs to be brought down */
5617 dev_info(&pf->pdev->dev, "VSI down requested\n");
5618 for (v = 0; v < pf->num_alloc_vsi; v++) {
5619 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005620
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005621 if (vsi != NULL &&
5622 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5623 set_bit(__I40E_DOWN, &vsi->state);
5624 i40e_down(vsi);
5625 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5626 }
5627 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005628 } else {
5629 dev_info(&pf->pdev->dev,
5630 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005631 }
5632}
5633
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005634#ifdef CONFIG_I40E_DCB
5635/**
5636 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5637 * @pf: board private structure
5638 * @old_cfg: current DCB config
5639 * @new_cfg: new DCB config
5640 **/
5641bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5642 struct i40e_dcbx_config *old_cfg,
5643 struct i40e_dcbx_config *new_cfg)
5644{
5645 bool need_reconfig = false;
5646
5647 /* Check if ETS configuration has changed */
5648 if (memcmp(&new_cfg->etscfg,
5649 &old_cfg->etscfg,
5650 sizeof(new_cfg->etscfg))) {
5651 /* If Priority Table has changed reconfig is needed */
5652 if (memcmp(&new_cfg->etscfg.prioritytable,
5653 &old_cfg->etscfg.prioritytable,
5654 sizeof(new_cfg->etscfg.prioritytable))) {
5655 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005656 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005657 }
5658
5659 if (memcmp(&new_cfg->etscfg.tcbwtable,
5660 &old_cfg->etscfg.tcbwtable,
5661 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005662 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005663
5664 if (memcmp(&new_cfg->etscfg.tsatable,
5665 &old_cfg->etscfg.tsatable,
5666 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005667 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005668 }
5669
5670 /* Check if PFC configuration has changed */
5671 if (memcmp(&new_cfg->pfc,
5672 &old_cfg->pfc,
5673 sizeof(new_cfg->pfc))) {
5674 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005675 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005676 }
5677
5678 /* Check if APP Table has changed */
5679 if (memcmp(&new_cfg->app,
5680 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005681 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005682 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005683 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005684 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005685
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005686 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005687 return need_reconfig;
5688}
5689
5690/**
5691 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5692 * @pf: board private structure
5693 * @e: event info posted on ARQ
5694 **/
5695static int i40e_handle_lldp_event(struct i40e_pf *pf,
5696 struct i40e_arq_event_info *e)
5697{
5698 struct i40e_aqc_lldp_get_mib *mib =
5699 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5700 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005701 struct i40e_dcbx_config tmp_dcbx_cfg;
5702 bool need_reconfig = false;
5703 int ret = 0;
5704 u8 type;
5705
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005706 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005707 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005708 return ret;
5709
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005710 /* Ignore if event is not for Nearest Bridge */
5711 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5712 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005713 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005714 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5715 return ret;
5716
5717 /* Check MIB Type and return if event for Remote MIB update */
5718 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005719 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005720 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005721 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5722 /* Update the remote cached instance and return */
5723 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5724 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5725 &hw->remote_dcbx_config);
5726 goto exit;
5727 }
5728
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005729 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005730 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005731
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005732 /* Reset the old DCBx configuration data */
5733 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005734 /* Get updated DCBX data from firmware */
5735 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005736 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005737 dev_info(&pf->pdev->dev,
5738 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5739 i40e_stat_str(&pf->hw, ret),
5740 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005741 goto exit;
5742 }
5743
5744 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005745 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5746 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005747 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005748 goto exit;
5749 }
5750
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005751 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5752 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005753
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005754 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005755
5756 if (!need_reconfig)
5757 goto exit;
5758
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005759 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005760 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005761 pf->flags |= I40E_FLAG_DCB_ENABLED;
5762 else
5763 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5764
Neerav Parikh69129dc2014-11-12 00:18:46 +00005765 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005766 /* Reconfiguration needed quiesce all VSIs */
5767 i40e_pf_quiesce_all_vsi(pf);
5768
5769 /* Changes in configuration update VEB/VSI */
5770 i40e_dcb_reconfigure(pf);
5771
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005772 ret = i40e_resume_port_tx(pf);
5773
Neerav Parikh69129dc2014-11-12 00:18:46 +00005774 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005775 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005776 if (ret)
5777 goto exit;
5778
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005779 /* Wait for the PF's queues to be disabled */
5780 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005781 if (ret) {
5782 /* Schedule PF reset to recover */
5783 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5784 i40e_service_event_schedule(pf);
5785 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005786 i40e_pf_unquiesce_all_vsi(pf);
Neerav Parikh85a1aab2016-06-07 09:14:55 -07005787 /* Notify the client for the DCB changes */
5788 i40e_notify_client_of_l2_param_changes(pf->vsi[pf->lan_vsi]);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005789 }
5790
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005791exit:
5792 return ret;
5793}
5794#endif /* CONFIG_I40E_DCB */
5795
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005796/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005797 * i40e_do_reset_safe - Protected reset path for userland calls.
5798 * @pf: board private structure
5799 * @reset_flags: which reset is requested
5800 *
5801 **/
5802void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5803{
5804 rtnl_lock();
5805 i40e_do_reset(pf, reset_flags);
5806 rtnl_unlock();
5807}
5808
5809/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005810 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5811 * @pf: board private structure
5812 * @e: event info posted on ARQ
5813 *
5814 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5815 * and VF queues
5816 **/
5817static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5818 struct i40e_arq_event_info *e)
5819{
5820 struct i40e_aqc_lan_overflow *data =
5821 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5822 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5823 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5824 struct i40e_hw *hw = &pf->hw;
5825 struct i40e_vf *vf;
5826 u16 vf_id;
5827
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005828 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5829 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005830
5831 /* Queue belongs to VF, find the VF and issue VF reset */
5832 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5833 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5834 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5835 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5836 vf_id -= hw->func_caps.vf_base_id;
5837 vf = &pf->vf[vf_id];
5838 i40e_vc_notify_vf_reset(vf);
5839 /* Allow VF to process pending reset notification */
5840 msleep(20);
5841 i40e_reset_vf(vf, false);
5842 }
5843}
5844
5845/**
5846 * i40e_service_event_complete - Finish up the service event
5847 * @pf: board private structure
5848 **/
5849static void i40e_service_event_complete(struct i40e_pf *pf)
5850{
Shannon Nelsonb875f992015-10-21 19:47:03 -04005851 WARN_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005852
5853 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005854 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005855 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5856}
5857
5858/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005859 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5860 * @pf: board private structure
5861 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005862u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005863{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005864 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005865
5866 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5867 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5868 return fcnt_prog;
5869}
5870
5871/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005872 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005873 * @pf: board private structure
5874 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005875u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005876{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005877 u32 val, fcnt_prog;
5878
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005879 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5880 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5881 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5882 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5883 return fcnt_prog;
5884}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005885
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005886/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005887 * i40e_get_global_fd_count - Get total FD filters programmed on device
5888 * @pf: board private structure
5889 **/
5890u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5891{
5892 u32 val, fcnt_prog;
5893
5894 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5895 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5896 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5897 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5898 return fcnt_prog;
5899}
5900
5901/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005902 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5903 * @pf: board private structure
5904 **/
5905void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5906{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005907 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005908 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005909 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005910
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005911 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5912 return;
5913
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005914 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5915 * to re-enable
5916 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005917 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005918 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005919 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5920 (pf->fd_add_err == 0) ||
5921 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005922 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5923 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5924 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005925 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5926 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 +00005927 }
5928 }
Jacob Kellera3417d22016-09-06 18:05:10 -07005929
5930 /* Wait for some more space to be available to turn on ATR. We also
5931 * must check that no existing ntuple rules for TCP are in effect
5932 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005933 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5934 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Kellera3417d22016-09-06 18:05:10 -07005935 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED) &&
5936 (pf->fd_tcp_rule == 0)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005937 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005938 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07005939 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 +00005940 }
5941 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005942
5943 /* if hw had a problem adding a filter, delete it */
5944 if (pf->fd_inv > 0) {
5945 hlist_for_each_entry_safe(filter, node,
5946 &pf->fdir_filter_list, fdir_node) {
5947 if (filter->fd_id == pf->fd_inv) {
5948 hlist_del(&filter->fdir_node);
5949 kfree(filter);
5950 pf->fdir_pf_active_filters--;
5951 }
5952 }
5953 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005954}
5955
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005956#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005957#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005958/**
5959 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
5960 * @pf: board private structure
5961 **/
5962static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
5963{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005964 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005965 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005966 bool disable_atr = false;
5967 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005968 int reg;
5969
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005970 if (!time_after(jiffies, pf->fd_flush_timestamp +
5971 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
5972 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005973
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005974 /* If the flush is happening too quick and we have mostly SB rules we
5975 * should not re-enable ATR for some time.
5976 */
5977 min_flush_time = pf->fd_flush_timestamp +
5978 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
5979 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005980
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005981 if (!(time_after(jiffies, min_flush_time)) &&
5982 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
5983 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5984 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
5985 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005986 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005987
5988 pf->fd_flush_timestamp = jiffies;
Jacob Keller234dc4e2016-09-06 18:05:09 -07005989 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005990 /* flush all filters */
5991 wr32(&pf->hw, I40E_PFQF_CTL_1,
5992 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
5993 i40e_flush(&pf->hw);
5994 pf->fd_flush_cnt++;
5995 pf->fd_add_err = 0;
5996 do {
5997 /* Check FD flush status every 5-6msec */
5998 usleep_range(5000, 6000);
5999 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6000 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6001 break;
6002 } while (flush_wait_retry--);
6003 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6004 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6005 } else {
6006 /* replay sideband filters */
6007 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
6008 if (!disable_atr)
Jacob Keller234dc4e2016-09-06 18:05:09 -07006009 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006010 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6011 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6012 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6013 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006014}
6015
6016/**
6017 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6018 * @pf: board private structure
6019 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006020u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006021{
6022 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6023}
6024
6025/* We can see up to 256 filter programming desc in transit if the filters are
6026 * being applied really fast; before we see the first
6027 * filter miss error on Rx queue 0. Accumulating enough error messages before
6028 * reacting will make sure we don't cause flush too often.
6029 */
6030#define I40E_MAX_FD_PROGRAM_ERROR 256
6031
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006032/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006033 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6034 * @pf: board private structure
6035 **/
6036static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6037{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006038
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006039 /* if interface is down do nothing */
6040 if (test_bit(__I40E_DOWN, &pf->state))
6041 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006042
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006043 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006044 i40e_fdir_flush_and_replay(pf);
6045
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006046 i40e_fdir_check_and_reenable(pf);
6047
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006048}
6049
6050/**
6051 * i40e_vsi_link_event - notify VSI of a link event
6052 * @vsi: vsi to be notified
6053 * @link_up: link up or down
6054 **/
6055static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6056{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006057 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006058 return;
6059
6060 switch (vsi->type) {
6061 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006062#ifdef I40E_FCOE
6063 case I40E_VSI_FCOE:
6064#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006065 if (!vsi->netdev || !vsi->netdev_registered)
6066 break;
6067
6068 if (link_up) {
6069 netif_carrier_on(vsi->netdev);
6070 netif_tx_wake_all_queues(vsi->netdev);
6071 } else {
6072 netif_carrier_off(vsi->netdev);
6073 netif_tx_stop_all_queues(vsi->netdev);
6074 }
6075 break;
6076
6077 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006078 case I40E_VSI_VMDQ2:
6079 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006080 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006081 case I40E_VSI_MIRROR:
6082 default:
6083 /* there is no notification for other VSIs */
6084 break;
6085 }
6086}
6087
6088/**
6089 * i40e_veb_link_event - notify elements on the veb of a link event
6090 * @veb: veb to be notified
6091 * @link_up: link up or down
6092 **/
6093static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6094{
6095 struct i40e_pf *pf;
6096 int i;
6097
6098 if (!veb || !veb->pf)
6099 return;
6100 pf = veb->pf;
6101
6102 /* depth first... */
6103 for (i = 0; i < I40E_MAX_VEB; i++)
6104 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6105 i40e_veb_link_event(pf->veb[i], link_up);
6106
6107 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006108 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006109 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6110 i40e_vsi_link_event(pf->vsi[i], link_up);
6111}
6112
6113/**
6114 * i40e_link_event - Update netif_carrier status
6115 * @pf: board private structure
6116 **/
6117static void i40e_link_event(struct i40e_pf *pf)
6118{
Mitch Williams320684c2014-10-17 03:14:43 +00006119 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006120 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006121 i40e_status status;
6122 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006123
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006124 /* save off old link status information */
6125 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6126
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006127 /* set this to force the get_link_status call to refresh state */
6128 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006129
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006130 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006131
6132 status = i40e_get_link_status(&pf->hw, &new_link);
6133 if (status) {
6134 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6135 status);
6136 return;
6137 }
6138
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006139 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6140 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006141
6142 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006143 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006144 (test_bit(__I40E_DOWN, &vsi->state) ||
6145 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006146 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006147
6148 if (!test_bit(__I40E_DOWN, &vsi->state))
6149 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006150
6151 /* Notify the base of the switch tree connected to
6152 * the link. Floating VEBs are not notified.
6153 */
6154 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6155 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6156 else
Mitch Williams320684c2014-10-17 03:14:43 +00006157 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006158
6159 if (pf->vf)
6160 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006161
6162 if (pf->flags & I40E_FLAG_PTP)
6163 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006164}
6165
6166/**
Shannon Nelson21536712014-10-25 10:35:25 +00006167 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006168 * @pf: board private structure
6169 **/
6170static void i40e_watchdog_subtask(struct i40e_pf *pf)
6171{
6172 int i;
6173
6174 /* if interface is down do nothing */
6175 if (test_bit(__I40E_DOWN, &pf->state) ||
6176 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6177 return;
6178
Shannon Nelson21536712014-10-25 10:35:25 +00006179 /* make sure we don't do these things too often */
6180 if (time_before(jiffies, (pf->service_timer_previous +
6181 pf->service_timer_period)))
6182 return;
6183 pf->service_timer_previous = jiffies;
6184
Shannon Nelson9ac77262015-08-27 11:42:40 -04006185 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6186 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006187
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006188 /* Update the stats for active netdevs so the network stack
6189 * can look at updated numbers whenever it cares to
6190 */
Mitch Williams505682c2014-05-20 08:01:37 +00006191 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006192 if (pf->vsi[i] && pf->vsi[i]->netdev)
6193 i40e_update_stats(pf->vsi[i]);
6194
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006195 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6196 /* Update the stats for the active switching components */
6197 for (i = 0; i < I40E_MAX_VEB; i++)
6198 if (pf->veb[i])
6199 i40e_update_veb_stats(pf->veb[i]);
6200 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006201
6202 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006203}
6204
6205/**
6206 * i40e_reset_subtask - Set up for resetting the device and driver
6207 * @pf: board private structure
6208 **/
6209static void i40e_reset_subtask(struct i40e_pf *pf)
6210{
6211 u32 reset_flags = 0;
6212
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006213 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006214 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006215 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006216 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6217 }
6218 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006219 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006220 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6221 }
6222 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006223 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006224 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6225 }
6226 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006227 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006228 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6229 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006230 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006231 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006232 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6233 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006234
6235 /* If there's a recovery already waiting, it takes
6236 * precedence before starting a new reset sequence.
6237 */
6238 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6239 i40e_handle_reset_warning(pf);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006240 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006241 }
6242
6243 /* If we're already down or resetting, just bail */
6244 if (reset_flags &&
6245 !test_bit(__I40E_DOWN, &pf->state) &&
6246 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6247 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006248
6249unlock:
6250 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006251}
6252
6253/**
6254 * i40e_handle_link_event - Handle link event
6255 * @pf: board private structure
6256 * @e: event info posted on ARQ
6257 **/
6258static void i40e_handle_link_event(struct i40e_pf *pf,
6259 struct i40e_arq_event_info *e)
6260{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006261 struct i40e_aqc_get_link_status *status =
6262 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006263
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006264 /* Do a new status request to re-enable LSE reporting
6265 * and load new status information into the hw struct
6266 * This completely ignores any state information
6267 * in the ARQ event info, instead choosing to always
6268 * issue the AQ update link status command.
6269 */
6270 i40e_link_event(pf);
6271
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006272 /* check for unqualified module, if link is down */
6273 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6274 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6275 (!(status->link_info & I40E_AQ_LINK_UP)))
6276 dev_err(&pf->pdev->dev,
6277 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006278}
6279
6280/**
6281 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6282 * @pf: board private structure
6283 **/
6284static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6285{
6286 struct i40e_arq_event_info event;
6287 struct i40e_hw *hw = &pf->hw;
6288 u16 pending, i = 0;
6289 i40e_status ret;
6290 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006291 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006292 u32 val;
6293
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006294 /* Do not run clean AQ when PF reset fails */
6295 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6296 return;
6297
Shannon Nelson86df2422014-05-20 08:01:35 +00006298 /* check for error indications */
6299 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6300 oldval = val;
6301 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006302 if (hw->debug_mask & I40E_DEBUG_AQ)
6303 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006304 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6305 }
6306 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006307 if (hw->debug_mask & I40E_DEBUG_AQ)
6308 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006309 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006310 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006311 }
6312 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006313 if (hw->debug_mask & I40E_DEBUG_AQ)
6314 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006315 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6316 }
6317 if (oldval != val)
6318 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6319
6320 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6321 oldval = val;
6322 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006323 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6324 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006325 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6326 }
6327 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006328 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6329 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006330 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6331 }
6332 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006333 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6334 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006335 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6336 }
6337 if (oldval != val)
6338 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6339
Mitch Williams1001dc32014-11-11 20:02:19 +00006340 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6341 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006342 if (!event.msg_buf)
6343 return;
6344
6345 do {
6346 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006347 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006348 break;
Mitch Williams56497972014-06-04 08:45:18 +00006349 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006350 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6351 break;
6352 }
6353
6354 opcode = le16_to_cpu(event.desc.opcode);
6355 switch (opcode) {
6356
6357 case i40e_aqc_opc_get_link_status:
6358 i40e_handle_link_event(pf, &event);
6359 break;
6360 case i40e_aqc_opc_send_msg_to_pf:
6361 ret = i40e_vc_process_vf_msg(pf,
6362 le16_to_cpu(event.desc.retval),
6363 le32_to_cpu(event.desc.cookie_high),
6364 le32_to_cpu(event.desc.cookie_low),
6365 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006366 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006367 break;
6368 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006369 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006370#ifdef CONFIG_I40E_DCB
6371 rtnl_lock();
6372 ret = i40e_handle_lldp_event(pf, &event);
6373 rtnl_unlock();
6374#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006375 break;
6376 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006377 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006378 i40e_handle_lan_overflow_event(pf, &event);
6379 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006380 case i40e_aqc_opc_send_msg_to_peer:
6381 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6382 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006383 case i40e_aqc_opc_nvm_erase:
6384 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006385 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006386 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6387 "ARQ NVM operation 0x%04x completed\n",
6388 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006389 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006390 default:
6391 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006392 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006393 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006394 break;
6395 }
6396 } while (pending && (i++ < pf->adminq_work_limit));
6397
6398 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6399 /* re-enable Admin queue interrupt cause */
6400 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6401 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6402 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6403 i40e_flush(hw);
6404
6405 kfree(event.msg_buf);
6406}
6407
6408/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006409 * i40e_verify_eeprom - make sure eeprom is good to use
6410 * @pf: board private structure
6411 **/
6412static void i40e_verify_eeprom(struct i40e_pf *pf)
6413{
6414 int err;
6415
6416 err = i40e_diag_eeprom_test(&pf->hw);
6417 if (err) {
6418 /* retry in case of garbage read */
6419 err = i40e_diag_eeprom_test(&pf->hw);
6420 if (err) {
6421 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6422 err);
6423 set_bit(__I40E_BAD_EEPROM, &pf->state);
6424 }
6425 }
6426
6427 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6428 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6429 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6430 }
6431}
6432
6433/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006434 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006435 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006436 *
6437 * enable switch loop back or die - no point in a return value
6438 **/
6439static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6440{
6441 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6442 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006443 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006444
6445 ctxt.seid = pf->main_vsi_seid;
6446 ctxt.pf_num = pf->hw.pf_id;
6447 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006448 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6449 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006450 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006451 "couldn't get PF vsi config, err %s aq_err %s\n",
6452 i40e_stat_str(&pf->hw, ret),
6453 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006454 return;
6455 }
6456 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6457 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6458 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6459
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006460 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6461 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006462 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006463 "update vsi switch failed, err %s aq_err %s\n",
6464 i40e_stat_str(&pf->hw, ret),
6465 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006466 }
6467}
6468
6469/**
6470 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006471 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006472 *
6473 * disable switch loop back or die - no point in a return value
6474 **/
6475static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6476{
6477 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6478 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006479 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006480
6481 ctxt.seid = pf->main_vsi_seid;
6482 ctxt.pf_num = pf->hw.pf_id;
6483 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006484 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6485 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006486 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006487 "couldn't get PF vsi config, err %s aq_err %s\n",
6488 i40e_stat_str(&pf->hw, ret),
6489 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006490 return;
6491 }
6492 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6493 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6494 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6495
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006496 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6497 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006498 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006499 "update vsi switch failed, err %s aq_err %s\n",
6500 i40e_stat_str(&pf->hw, ret),
6501 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006502 }
6503}
6504
6505/**
Neerav Parikh51616012015-02-06 08:52:14 +00006506 * i40e_config_bridge_mode - Configure the HW bridge mode
6507 * @veb: pointer to the bridge instance
6508 *
6509 * Configure the loop back mode for the LAN VSI that is downlink to the
6510 * specified HW bridge instance. It is expected this function is called
6511 * when a new HW bridge is instantiated.
6512 **/
6513static void i40e_config_bridge_mode(struct i40e_veb *veb)
6514{
6515 struct i40e_pf *pf = veb->pf;
6516
Shannon Nelson6dec1012015-09-28 14:12:30 -04006517 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6518 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6519 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006520 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6521 i40e_disable_pf_switch_lb(pf);
6522 else
6523 i40e_enable_pf_switch_lb(pf);
6524}
6525
6526/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006527 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6528 * @veb: pointer to the VEB instance
6529 *
6530 * This is a recursive function that first builds the attached VSIs then
6531 * recurses in to build the next layer of VEB. We track the connections
6532 * through our own index numbers because the seid's from the HW could
6533 * change across the reset.
6534 **/
6535static int i40e_reconstitute_veb(struct i40e_veb *veb)
6536{
6537 struct i40e_vsi *ctl_vsi = NULL;
6538 struct i40e_pf *pf = veb->pf;
6539 int v, veb_idx;
6540 int ret;
6541
6542 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006543 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006544 if (pf->vsi[v] &&
6545 pf->vsi[v]->veb_idx == veb->idx &&
6546 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6547 ctl_vsi = pf->vsi[v];
6548 break;
6549 }
6550 }
6551 if (!ctl_vsi) {
6552 dev_info(&pf->pdev->dev,
6553 "missing owner VSI for veb_idx %d\n", veb->idx);
6554 ret = -ENOENT;
6555 goto end_reconstitute;
6556 }
6557 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6558 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6559 ret = i40e_add_vsi(ctl_vsi);
6560 if (ret) {
6561 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006562 "rebuild of veb_idx %d owner VSI failed: %d\n",
6563 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006564 goto end_reconstitute;
6565 }
6566 i40e_vsi_reset_stats(ctl_vsi);
6567
6568 /* create the VEB in the switch and move the VSI onto the VEB */
6569 ret = i40e_add_veb(veb, ctl_vsi);
6570 if (ret)
6571 goto end_reconstitute;
6572
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006573 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6574 veb->bridge_mode = BRIDGE_MODE_VEB;
6575 else
6576 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006577 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006578
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006579 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006580 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006581 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6582 continue;
6583
6584 if (pf->vsi[v]->veb_idx == veb->idx) {
6585 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006586
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006587 vsi->uplink_seid = veb->seid;
6588 ret = i40e_add_vsi(vsi);
6589 if (ret) {
6590 dev_info(&pf->pdev->dev,
6591 "rebuild of vsi_idx %d failed: %d\n",
6592 v, ret);
6593 goto end_reconstitute;
6594 }
6595 i40e_vsi_reset_stats(vsi);
6596 }
6597 }
6598
6599 /* create any VEBs attached to this VEB - RECURSION */
6600 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6601 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6602 pf->veb[veb_idx]->uplink_seid = veb->seid;
6603 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6604 if (ret)
6605 break;
6606 }
6607 }
6608
6609end_reconstitute:
6610 return ret;
6611}
6612
6613/**
6614 * i40e_get_capabilities - get info about the HW
6615 * @pf: the PF struct
6616 **/
6617static int i40e_get_capabilities(struct i40e_pf *pf)
6618{
6619 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6620 u16 data_size;
6621 int buf_len;
6622 int err;
6623
6624 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6625 do {
6626 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6627 if (!cap_buf)
6628 return -ENOMEM;
6629
6630 /* this loads the data into the hw struct for us */
6631 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6632 &data_size,
6633 i40e_aqc_opc_list_func_capabilities,
6634 NULL);
6635 /* data loaded, buffer no longer needed */
6636 kfree(cap_buf);
6637
6638 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6639 /* retry with a larger buffer */
6640 buf_len = data_size;
6641 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6642 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006643 "capability discovery failed, err %s aq_err %s\n",
6644 i40e_stat_str(&pf->hw, err),
6645 i40e_aq_str(&pf->hw,
6646 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006647 return -ENODEV;
6648 }
6649 } while (err);
6650
6651 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6652 dev_info(&pf->pdev->dev,
6653 "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",
6654 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6655 pf->hw.func_caps.num_msix_vectors,
6656 pf->hw.func_caps.num_msix_vectors_vf,
6657 pf->hw.func_caps.fd_filters_guaranteed,
6658 pf->hw.func_caps.fd_filters_best_effort,
6659 pf->hw.func_caps.num_tx_qp,
6660 pf->hw.func_caps.num_vsis);
6661
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006662#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6663 + pf->hw.func_caps.num_vfs)
6664 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6665 dev_info(&pf->pdev->dev,
6666 "got num_vsis %d, setting num_vsis to %d\n",
6667 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6668 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6669 }
6670
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006671 return 0;
6672}
6673
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006674static int i40e_vsi_clear(struct i40e_vsi *vsi);
6675
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006676/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006677 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006678 * @pf: board private structure
6679 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006680static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006681{
6682 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006683 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006684
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006685 /* quick workaround for an NVM issue that leaves a critical register
6686 * uninitialized
6687 */
6688 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6689 static const u32 hkey[] = {
6690 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6691 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6692 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6693 0x95b3a76d};
6694
6695 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6696 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6697 }
6698
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006699 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006700 return;
6701
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006702 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006703 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00006704 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006705 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006706 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006707 break;
6708 }
6709 }
6710
6711 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006712 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006713 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6714 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006715 if (!vsi) {
6716 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006717 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6718 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006719 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006720 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006721
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006722 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006723}
6724
6725/**
6726 * i40e_fdir_teardown - release the Flow Director resources
6727 * @pf: board private structure
6728 **/
6729static void i40e_fdir_teardown(struct i40e_pf *pf)
6730{
6731 int i;
6732
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006733 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00006734 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006735 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
6736 i40e_vsi_release(pf->vsi[i]);
6737 break;
6738 }
6739 }
6740}
6741
6742/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006743 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006744 * @pf: board private structure
6745 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006746 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006747 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006748static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006749{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006750 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006751 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006752 u32 v;
6753
6754 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6755 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006756 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006757 if (i40e_check_asq_alive(&pf->hw))
6758 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006759
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006760 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006761
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006762 /* quiesce the VSIs and their queues that are not already DOWN */
6763 i40e_pf_quiesce_all_vsi(pf);
6764
Mitch Williams505682c2014-05-20 08:01:37 +00006765 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006766 if (pf->vsi[v])
6767 pf->vsi[v]->seid = 0;
6768 }
6769
6770 i40e_shutdown_adminq(&pf->hw);
6771
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006772 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006773 if (hw->hmc.hmc_obj) {
6774 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006775 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006776 dev_warn(&pf->pdev->dev,
6777 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006778 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006779}
6780
6781/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006782 * i40e_send_version - update firmware with driver version
6783 * @pf: PF struct
6784 */
6785static void i40e_send_version(struct i40e_pf *pf)
6786{
6787 struct i40e_driver_version dv;
6788
6789 dv.major_version = DRV_VERSION_MAJOR;
6790 dv.minor_version = DRV_VERSION_MINOR;
6791 dv.build_version = DRV_VERSION_BUILD;
6792 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006793 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006794 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6795}
6796
6797/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006798 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006799 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006800 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006801 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006802static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006803{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006804 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006805 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006806 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006807 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006808 u32 v;
6809
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006810 /* Now we wait for GRST to settle out.
6811 * We don't have to delete the VEBs or VSIs from the hw switch
6812 * because the reset will make them disappear.
6813 */
6814 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006815 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006816 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006817 set_bit(__I40E_RESET_FAILED, &pf->state);
6818 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006819 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006820 pf->pfr_count++;
6821
6822 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006823 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006824 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006825
6826 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6827 ret = i40e_init_adminq(&pf->hw);
6828 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006829 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6830 i40e_stat_str(&pf->hw, ret),
6831 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006832 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006833 }
6834
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006835 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006836 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006837 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006838
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006839 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006840 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006841 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006842 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006843
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006844 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6845 hw->func_caps.num_rx_qp,
6846 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6847 if (ret) {
6848 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6849 goto end_core_reset;
6850 }
6851 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6852 if (ret) {
6853 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6854 goto end_core_reset;
6855 }
6856
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006857#ifdef CONFIG_I40E_DCB
6858 ret = i40e_init_pf_dcb(pf);
6859 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006860 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6861 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6862 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006863 }
6864#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006865#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006866 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006867
Vasu Dev38e00432014-08-01 13:27:03 -07006868#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006869 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006870 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006871 if (ret)
6872 goto end_core_reset;
6873
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006874 /* The driver only wants link up/down and module qualification
6875 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006876 */
6877 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006878 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07006879 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006880 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006881 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006882 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6883 i40e_stat_str(&pf->hw, ret),
6884 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006885
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006886 /* make sure our flow control settings are restored */
6887 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6888 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04006889 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6890 i40e_stat_str(&pf->hw, ret),
6891 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006892
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006893 /* Rebuild the VSIs and VEBs that existed before reset.
6894 * They are still in our local switch element arrays, so only
6895 * need to rebuild the switch model in the HW.
6896 *
6897 * If there were VEBs but the reconstitution failed, we'll try
6898 * try to recover minimal use by getting the basic PF VSI working.
6899 */
6900 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006901 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006902 /* find the one VEB connected to the MAC, and find orphans */
6903 for (v = 0; v < I40E_MAX_VEB; v++) {
6904 if (!pf->veb[v])
6905 continue;
6906
6907 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6908 pf->veb[v]->uplink_seid == 0) {
6909 ret = i40e_reconstitute_veb(pf->veb[v]);
6910
6911 if (!ret)
6912 continue;
6913
6914 /* If Main VEB failed, we're in deep doodoo,
6915 * so give up rebuilding the switch and set up
6916 * for minimal rebuild of PF VSI.
6917 * If orphan failed, we'll report the error
6918 * but try to keep going.
6919 */
6920 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6921 dev_info(&pf->pdev->dev,
6922 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6923 ret);
6924 pf->vsi[pf->lan_vsi]->uplink_seid
6925 = pf->mac_seid;
6926 break;
6927 } else if (pf->veb[v]->uplink_seid == 0) {
6928 dev_info(&pf->pdev->dev,
6929 "rebuild of orphan VEB failed: %d\n",
6930 ret);
6931 }
6932 }
6933 }
6934 }
6935
6936 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006937 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006938 /* no VEB, so rebuild only the Main VSI */
6939 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6940 if (ret) {
6941 dev_info(&pf->pdev->dev,
6942 "rebuild of Main VSI failed: %d\n", ret);
6943 goto end_core_reset;
6944 }
6945 }
6946
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006947 /* Reconfigure hardware for allowing smaller MSS in the case
6948 * of TSO, so that we avoid the MDD being fired and causing
6949 * a reset in the case of small MSS+TSO.
6950 */
6951#define I40E_REG_MSS 0x000E64DC
6952#define I40E_REG_MSS_MIN_MASK 0x3FF0000
6953#define I40E_64BYTE_MSS 0x400000
6954 val = rd32(hw, I40E_REG_MSS);
6955 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
6956 val &= ~I40E_REG_MSS_MIN_MASK;
6957 val |= I40E_64BYTE_MSS;
6958 wr32(hw, I40E_REG_MSS, val);
6959 }
6960
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08006961 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006962 msleep(75);
6963 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6964 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006965 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
6966 i40e_stat_str(&pf->hw, ret),
6967 i40e_aq_str(&pf->hw,
6968 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006969 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006970 /* reinit the misc interrupt */
6971 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6972 ret = i40e_setup_misc_vector(pf);
6973
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04006974 /* Add a filter to drop all Flow control frames from any VSI from being
6975 * transmitted. By doing so we stop a malicious VF from sending out
6976 * PAUSE or PFC frames and potentially controlling traffic for other
6977 * PF/VF VSIs.
6978 * The FW can still send Flow control frames if enabled.
6979 */
6980 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
6981 pf->main_vsi_seid);
6982
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006983 /* restart the VSIs that were rebuilt and running before the reset */
6984 i40e_pf_unquiesce_all_vsi(pf);
6985
Mitch Williams69f64b22014-02-13 03:48:46 -08006986 if (pf->num_alloc_vfs) {
6987 for (v = 0; v < pf->num_alloc_vfs; v++)
6988 i40e_reset_vf(&pf->vf[v], true);
6989 }
6990
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006991 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006992 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006993
6994end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006995 clear_bit(__I40E_RESET_FAILED, &pf->state);
6996clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006997 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6998}
6999
7000/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007001 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007002 * @pf: board private structure
7003 *
7004 * Close up the VFs and other things in prep for a Core Reset,
7005 * then get ready to rebuild the world.
7006 **/
7007static void i40e_handle_reset_warning(struct i40e_pf *pf)
7008{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007009 i40e_prep_for_reset(pf);
7010 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007011}
7012
7013/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007014 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007015 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007016 *
7017 * Called from the MDD irq handler to identify possibly malicious vfs
7018 **/
7019static void i40e_handle_mdd_event(struct i40e_pf *pf)
7020{
7021 struct i40e_hw *hw = &pf->hw;
7022 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007023 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007024 struct i40e_vf *vf;
7025 u32 reg;
7026 int i;
7027
7028 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7029 return;
7030
7031 /* find what triggered the MDD event */
7032 reg = rd32(hw, I40E_GL_MDET_TX);
7033 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007034 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7035 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007036 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007037 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007038 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007039 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007040 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7041 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7042 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007043 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007044 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 +00007045 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007046 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7047 mdd_detected = true;
7048 }
7049 reg = rd32(hw, I40E_GL_MDET_RX);
7050 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007051 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7052 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007053 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007054 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007055 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7056 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7057 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007058 if (netif_msg_rx_err(pf))
7059 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7060 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007061 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7062 mdd_detected = true;
7063 }
7064
Neerav Parikhdf430b12014-06-04 01:23:15 +00007065 if (mdd_detected) {
7066 reg = rd32(hw, I40E_PF_MDET_TX);
7067 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7068 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007069 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007070 pf_mdd_detected = true;
7071 }
7072 reg = rd32(hw, I40E_PF_MDET_RX);
7073 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7074 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007075 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007076 pf_mdd_detected = true;
7077 }
7078 /* Queue belongs to the PF, initiate a reset */
7079 if (pf_mdd_detected) {
7080 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7081 i40e_service_event_schedule(pf);
7082 }
7083 }
7084
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007085 /* see if one of the VFs needs its hand slapped */
7086 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7087 vf = &(pf->vf[i]);
7088 reg = rd32(hw, I40E_VP_MDET_TX(i));
7089 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7090 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7091 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007092 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7093 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007094 }
7095
7096 reg = rd32(hw, I40E_VP_MDET_RX(i));
7097 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7098 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7099 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007100 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7101 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007102 }
7103
7104 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7105 dev_info(&pf->pdev->dev,
7106 "Too many MDD events on VF %d, disabled\n", i);
7107 dev_info(&pf->pdev->dev,
7108 "Use PF Control I/F to re-enable the VF\n");
7109 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7110 }
7111 }
7112
7113 /* re-enable mdd interrupt cause */
7114 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7115 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7116 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7117 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7118 i40e_flush(hw);
7119}
7120
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007121/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007122 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007123 * @pf: board private structure
7124 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007125static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007126{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007127 struct i40e_hw *hw = &pf->hw;
7128 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007129 __be16 port;
7130 int i;
7131
Singhai, Anjali6a899022015-12-14 12:21:18 -08007132 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007133 return;
7134
Singhai, Anjali6a899022015-12-14 12:21:18 -08007135 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007136
7137 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007138 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7139 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7140 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007141 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007142 ret = i40e_aq_add_udp_tunnel(hw, port,
7143 pf->udp_ports[i].type,
7144 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007145 else
7146 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007147
7148 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007149 dev_dbg(&pf->pdev->dev,
7150 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7151 pf->udp_ports[i].type ? "vxlan" : "geneve",
7152 port ? "add" : "delete",
7153 ntohs(port), i,
7154 i40e_stat_str(&pf->hw, ret),
7155 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007156 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007157 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007158 }
7159 }
7160 }
7161}
7162
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007163/**
7164 * i40e_service_task - Run the driver's async subtasks
7165 * @work: pointer to work_struct containing our data
7166 **/
7167static void i40e_service_task(struct work_struct *work)
7168{
7169 struct i40e_pf *pf = container_of(work,
7170 struct i40e_pf,
7171 service_task);
7172 unsigned long start_time = jiffies;
7173
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007174 /* don't bother with service tasks if a reset is in progress */
7175 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7176 i40e_service_event_complete(pf);
7177 return;
7178 }
7179
Kiran Patilb03a8c12015-09-24 18:13:15 -04007180 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007181 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007182 i40e_reset_subtask(pf);
7183 i40e_handle_mdd_event(pf);
7184 i40e_vc_process_vflr_event(pf);
7185 i40e_watchdog_subtask(pf);
7186 i40e_fdir_reinit_subtask(pf);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007187 i40e_client_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007188 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007189 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007190 i40e_clean_adminq_subtask(pf);
7191
7192 i40e_service_event_complete(pf);
7193
7194 /* If the tasks have taken longer than one timer cycle or there
7195 * is more work to be done, reschedule the service task now
7196 * rather than wait for the timer to tick again.
7197 */
7198 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7199 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7200 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7201 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7202 i40e_service_event_schedule(pf);
7203}
7204
7205/**
7206 * i40e_service_timer - timer callback
7207 * @data: pointer to PF struct
7208 **/
7209static void i40e_service_timer(unsigned long data)
7210{
7211 struct i40e_pf *pf = (struct i40e_pf *)data;
7212
7213 mod_timer(&pf->service_timer,
7214 round_jiffies(jiffies + pf->service_timer_period));
7215 i40e_service_event_schedule(pf);
7216}
7217
7218/**
7219 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7220 * @vsi: the VSI being configured
7221 **/
7222static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7223{
7224 struct i40e_pf *pf = vsi->back;
7225
7226 switch (vsi->type) {
7227 case I40E_VSI_MAIN:
7228 vsi->alloc_queue_pairs = pf->num_lan_qps;
7229 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7230 I40E_REQ_DESCRIPTOR_MULTIPLE);
7231 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7232 vsi->num_q_vectors = pf->num_lan_msix;
7233 else
7234 vsi->num_q_vectors = 1;
7235
7236 break;
7237
7238 case I40E_VSI_FDIR:
7239 vsi->alloc_queue_pairs = 1;
7240 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7241 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007242 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007243 break;
7244
7245 case I40E_VSI_VMDQ2:
7246 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7247 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7248 I40E_REQ_DESCRIPTOR_MULTIPLE);
7249 vsi->num_q_vectors = pf->num_vmdq_msix;
7250 break;
7251
7252 case I40E_VSI_SRIOV:
7253 vsi->alloc_queue_pairs = pf->num_vf_qps;
7254 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7255 I40E_REQ_DESCRIPTOR_MULTIPLE);
7256 break;
7257
Vasu Dev38e00432014-08-01 13:27:03 -07007258#ifdef I40E_FCOE
7259 case I40E_VSI_FCOE:
7260 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7261 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7262 I40E_REQ_DESCRIPTOR_MULTIPLE);
7263 vsi->num_q_vectors = pf->num_fcoe_msix;
7264 break;
7265
7266#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007267 default:
7268 WARN_ON(1);
7269 return -ENODATA;
7270 }
7271
7272 return 0;
7273}
7274
7275/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007276 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7277 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007278 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007279 *
7280 * On error: returns error code (negative)
7281 * On success: returns 0
7282 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007283static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007284{
7285 int size;
7286 int ret = 0;
7287
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007288 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007289 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7290 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7291 if (!vsi->tx_rings)
7292 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007293 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7294
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007295 if (alloc_qvectors) {
7296 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007297 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007298 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7299 if (!vsi->q_vectors) {
7300 ret = -ENOMEM;
7301 goto err_vectors;
7302 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007303 }
7304 return ret;
7305
7306err_vectors:
7307 kfree(vsi->tx_rings);
7308 return ret;
7309}
7310
7311/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007312 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7313 * @pf: board private structure
7314 * @type: type of VSI
7315 *
7316 * On error: returns error code (negative)
7317 * On success: returns vsi index in PF (positive)
7318 **/
7319static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7320{
7321 int ret = -ENODEV;
7322 struct i40e_vsi *vsi;
7323 int vsi_idx;
7324 int i;
7325
7326 /* Need to protect the allocation of the VSIs at the PF level */
7327 mutex_lock(&pf->switch_mutex);
7328
7329 /* VSI list may be fragmented if VSI creation/destruction has
7330 * been happening. We can afford to do a quick scan to look
7331 * for any free VSIs in the list.
7332 *
7333 * find next empty vsi slot, looping back around if necessary
7334 */
7335 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007336 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007337 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007338 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007339 i = 0;
7340 while (i < pf->next_vsi && pf->vsi[i])
7341 i++;
7342 }
7343
Mitch Williams505682c2014-05-20 08:01:37 +00007344 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007345 vsi_idx = i; /* Found one! */
7346 } else {
7347 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007348 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007349 }
7350 pf->next_vsi = ++i;
7351
7352 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7353 if (!vsi) {
7354 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007355 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007356 }
7357 vsi->type = type;
7358 vsi->back = pf;
7359 set_bit(__I40E_DOWN, &vsi->state);
7360 vsi->flags = 0;
7361 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007362 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007363 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7364 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007365 vsi->netdev_registered = false;
7366 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007367 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007368 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007369
Alexander Duyck9f65e152013-09-28 06:00:58 +00007370 ret = i40e_set_num_rings_in_vsi(vsi);
7371 if (ret)
7372 goto err_rings;
7373
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007374 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007375 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007376 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007377
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007378 /* Setup default MSIX irq handler for VSI */
7379 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7380
Kiran Patil21659032015-09-30 14:09:03 -04007381 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007382 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007383 pf->vsi[vsi_idx] = vsi;
7384 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007385 goto unlock_pf;
7386
Alexander Duyck9f65e152013-09-28 06:00:58 +00007387err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007388 pf->next_vsi = i - 1;
7389 kfree(vsi);
7390unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007391 mutex_unlock(&pf->switch_mutex);
7392 return ret;
7393}
7394
7395/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007396 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7397 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007398 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007399 *
7400 * On error: returns error code (negative)
7401 * On success: returns 0
7402 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007403static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007404{
7405 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007406 if (free_qvectors) {
7407 kfree(vsi->q_vectors);
7408 vsi->q_vectors = NULL;
7409 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007410 kfree(vsi->tx_rings);
7411 vsi->tx_rings = NULL;
7412 vsi->rx_rings = NULL;
7413}
7414
7415/**
Helin Zhang28c58692015-10-26 19:44:27 -04007416 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7417 * and lookup table
7418 * @vsi: Pointer to VSI structure
7419 */
7420static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7421{
7422 if (!vsi)
7423 return;
7424
7425 kfree(vsi->rss_hkey_user);
7426 vsi->rss_hkey_user = NULL;
7427
7428 kfree(vsi->rss_lut_user);
7429 vsi->rss_lut_user = NULL;
7430}
7431
7432/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007433 * i40e_vsi_clear - Deallocate the VSI provided
7434 * @vsi: the VSI being un-configured
7435 **/
7436static int i40e_vsi_clear(struct i40e_vsi *vsi)
7437{
7438 struct i40e_pf *pf;
7439
7440 if (!vsi)
7441 return 0;
7442
7443 if (!vsi->back)
7444 goto free_vsi;
7445 pf = vsi->back;
7446
7447 mutex_lock(&pf->switch_mutex);
7448 if (!pf->vsi[vsi->idx]) {
7449 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7450 vsi->idx, vsi->idx, vsi, vsi->type);
7451 goto unlock_vsi;
7452 }
7453
7454 if (pf->vsi[vsi->idx] != vsi) {
7455 dev_err(&pf->pdev->dev,
7456 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7457 pf->vsi[vsi->idx]->idx,
7458 pf->vsi[vsi->idx],
7459 pf->vsi[vsi->idx]->type,
7460 vsi->idx, vsi, vsi->type);
7461 goto unlock_vsi;
7462 }
7463
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007464 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007465 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7466 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7467
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007468 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007469 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007470
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007471 pf->vsi[vsi->idx] = NULL;
7472 if (vsi->idx < pf->next_vsi)
7473 pf->next_vsi = vsi->idx;
7474
7475unlock_vsi:
7476 mutex_unlock(&pf->switch_mutex);
7477free_vsi:
7478 kfree(vsi);
7479
7480 return 0;
7481}
7482
7483/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007484 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7485 * @vsi: the VSI being cleaned
7486 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007487static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007488{
7489 int i;
7490
Greg Rose8e9dca52013-12-18 13:45:53 +00007491 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007492 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007493 kfree_rcu(vsi->tx_rings[i], rcu);
7494 vsi->tx_rings[i] = NULL;
7495 vsi->rx_rings[i] = NULL;
7496 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007497 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007498}
7499
7500/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007501 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7502 * @vsi: the VSI being configured
7503 **/
7504static int i40e_alloc_rings(struct i40e_vsi *vsi)
7505{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007506 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007507 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007508 int i;
7509
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007510 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007511 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007512 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007513 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7514 if (!tx_ring)
7515 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007516
7517 tx_ring->queue_index = i;
7518 tx_ring->reg_idx = vsi->base_queue + i;
7519 tx_ring->ring_active = false;
7520 tx_ring->vsi = vsi;
7521 tx_ring->netdev = vsi->netdev;
7522 tx_ring->dev = &pf->pdev->dev;
7523 tx_ring->count = vsi->num_desc;
7524 tx_ring->size = 0;
7525 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007526 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7527 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007528 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007529 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007530
Alexander Duyck9f65e152013-09-28 06:00:58 +00007531 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007532 rx_ring->queue_index = i;
7533 rx_ring->reg_idx = vsi->base_queue + i;
7534 rx_ring->ring_active = false;
7535 rx_ring->vsi = vsi;
7536 rx_ring->netdev = vsi->netdev;
7537 rx_ring->dev = &pf->pdev->dev;
7538 rx_ring->count = vsi->num_desc;
7539 rx_ring->size = 0;
7540 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007541 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007542 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007543 }
7544
7545 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007546
7547err_out:
7548 i40e_vsi_clear_rings(vsi);
7549 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007550}
7551
7552/**
7553 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7554 * @pf: board private structure
7555 * @vectors: the number of MSI-X vectors to request
7556 *
7557 * Returns the number of vectors reserved, or error
7558 **/
7559static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7560{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007561 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7562 I40E_MIN_MSIX, vectors);
7563 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007564 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007565 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007566 vectors = 0;
7567 }
7568
7569 return vectors;
7570}
7571
7572/**
7573 * i40e_init_msix - Setup the MSIX capability
7574 * @pf: board private structure
7575 *
7576 * Work with the OS to set up the MSIX vectors needed.
7577 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007578 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007579 **/
7580static int i40e_init_msix(struct i40e_pf *pf)
7581{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007582 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007583 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007584 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007585 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007586 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007587
7588 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7589 return -ENODEV;
7590
7591 /* The number of vectors we'll request will be comprised of:
7592 * - Add 1 for "other" cause for Admin Queue events, etc.
7593 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007594 * - Queues being used for RSS.
7595 * We don't need as many as max_rss_size vectors.
7596 * use rss_size instead in the calculation since that
7597 * is governed by number of cpus in the system.
7598 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007599 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007600 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007601#ifdef I40E_FCOE
7602 * - The number of FCOE qps.
7603#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007604 * Once we count this up, try the request.
7605 *
7606 * If we can't get what we want, we'll simplify to nearly nothing
7607 * and try again. If that still fails, we punt.
7608 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007609 vectors_left = hw->func_caps.num_msix_vectors;
7610 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007611
Shannon Nelson1e200e42015-02-27 09:15:24 +00007612 /* reserve one vector for miscellaneous handler */
7613 if (vectors_left) {
7614 v_budget++;
7615 vectors_left--;
7616 }
7617
7618 /* reserve vectors for the main PF traffic queues */
7619 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7620 vectors_left -= pf->num_lan_msix;
7621 v_budget += pf->num_lan_msix;
7622
7623 /* reserve one vector for sideband flow director */
7624 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7625 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007626 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007627 v_budget++;
7628 vectors_left--;
7629 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007630 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007631 }
7632 }
John W Linville83840e42015-01-14 03:06:28 +00007633
Vasu Dev38e00432014-08-01 13:27:03 -07007634#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007635 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007636 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007637 if (!vectors_left)
7638 pf->num_fcoe_msix = 0;
7639 else if (vectors_left >= pf->num_fcoe_qps)
7640 pf->num_fcoe_msix = pf->num_fcoe_qps;
7641 else
7642 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007643 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007644 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007645 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007646
Vasu Dev38e00432014-08-01 13:27:03 -07007647#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007648 /* can we reserve enough for iWARP? */
7649 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007650 iwarp_requested = pf->num_iwarp_msix;
7651
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007652 if (!vectors_left)
7653 pf->num_iwarp_msix = 0;
7654 else if (vectors_left < pf->num_iwarp_msix)
7655 pf->num_iwarp_msix = 1;
7656 v_budget += pf->num_iwarp_msix;
7657 vectors_left -= pf->num_iwarp_msix;
7658 }
7659
Shannon Nelson1e200e42015-02-27 09:15:24 +00007660 /* any vectors left over go for VMDq support */
7661 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7662 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7663 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7664
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007665 if (!vectors_left) {
7666 pf->num_vmdq_msix = 0;
7667 pf->num_vmdq_qps = 0;
7668 } else {
7669 /* if we're short on vectors for what's desired, we limit
7670 * the queues per vmdq. If this is still more than are
7671 * available, the user will need to change the number of
7672 * queues/vectors used by the PF later with the ethtool
7673 * channels command
7674 */
7675 if (vmdq_vecs < vmdq_vecs_wanted)
7676 pf->num_vmdq_qps = 1;
7677 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007678
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007679 v_budget += vmdq_vecs;
7680 vectors_left -= vmdq_vecs;
7681 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007682 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007683
7684 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7685 GFP_KERNEL);
7686 if (!pf->msix_entries)
7687 return -ENOMEM;
7688
7689 for (i = 0; i < v_budget; i++)
7690 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007691 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007692
Shannon Nelson3b444392015-02-26 16:15:57 +00007693 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007694 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7695 kfree(pf->msix_entries);
7696 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007697 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007698 return -ENODEV;
7699
Shannon Nelson3b444392015-02-26 16:15:57 +00007700 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007701 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007702 pf->num_vmdq_vsis = 0;
7703 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007704 pf->num_lan_qps = 1;
7705 pf->num_lan_msix = 1;
7706
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007707 } else if (!vectors_left) {
7708 /* If we have limited resources, we will start with no vectors
7709 * for the special features and then allocate vectors to some
7710 * of these features based on the policy and at the end disable
7711 * the features that did not get any vectors.
7712 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007713 int vec;
7714
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007715 dev_info(&pf->pdev->dev,
7716 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007717 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007718 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007719
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007720 /* Scale vector usage down */
7721 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007722 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007723 pf->num_vmdq_qps = 1;
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007724#ifdef I40E_FCOE
7725 pf->num_fcoe_qps = 0;
7726 pf->num_fcoe_msix = 0;
7727#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007728
7729 /* partition out the remaining vectors */
7730 switch (vec) {
7731 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007732 pf->num_lan_msix = 1;
7733 break;
7734 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007735 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7736 pf->num_lan_msix = 1;
7737 pf->num_iwarp_msix = 1;
7738 } else {
7739 pf->num_lan_msix = 2;
7740 }
Vasu Dev38e00432014-08-01 13:27:03 -07007741#ifdef I40E_FCOE
7742 /* give one vector to FCoE */
7743 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7744 pf->num_lan_msix = 1;
7745 pf->num_fcoe_msix = 1;
7746 }
Vasu Dev38e00432014-08-01 13:27:03 -07007747#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007748 break;
7749 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007750 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7751 pf->num_iwarp_msix = min_t(int, (vec / 3),
7752 iwarp_requested);
7753 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7754 I40E_DEFAULT_NUM_VMDQ_VSI);
7755 } else {
7756 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7757 I40E_DEFAULT_NUM_VMDQ_VSI);
7758 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007759 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7760 pf->num_fdsb_msix = 1;
7761 vec--;
7762 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007763 pf->num_lan_msix = min_t(int,
7764 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7765 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007766 pf->num_lan_qps = pf->num_lan_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007767#ifdef I40E_FCOE
7768 /* give one vector to FCoE */
7769 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7770 pf->num_fcoe_msix = 1;
7771 vec--;
7772 }
7773#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007774 break;
7775 }
7776 }
7777
Stefan Assmannabd97a92016-09-19 13:37:51 +02007778 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7779 (pf->num_fdsb_msix == 0)) {
7780 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7781 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7782 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007783 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7784 (pf->num_vmdq_msix == 0)) {
7785 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7786 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7787 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007788
7789 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7790 (pf->num_iwarp_msix == 0)) {
7791 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7792 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7793 }
Vasu Dev38e00432014-08-01 13:27:03 -07007794#ifdef I40E_FCOE
7795
7796 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7797 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7798 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7799 }
7800#endif
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007801 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7802 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7803 pf->num_lan_msix,
7804 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7805 pf->num_fdsb_msix,
7806 pf->num_iwarp_msix);
7807
Shannon Nelson3b444392015-02-26 16:15:57 +00007808 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007809}
7810
7811/**
Greg Rose90e04072014-03-06 08:59:57 +00007812 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007813 * @vsi: the VSI being configured
7814 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007815 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007816 *
7817 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7818 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007819static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007820{
7821 struct i40e_q_vector *q_vector;
7822
7823 /* allocate q_vector */
7824 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7825 if (!q_vector)
7826 return -ENOMEM;
7827
7828 q_vector->vsi = vsi;
7829 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007830 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7831
Alexander Duyck493fb302013-09-28 07:01:44 +00007832 if (vsi->netdev)
7833 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007834 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007835
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007836 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7837 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7838
Alexander Duyck493fb302013-09-28 07:01:44 +00007839 /* tie q_vector and vsi together */
7840 vsi->q_vectors[v_idx] = q_vector;
7841
7842 return 0;
7843}
7844
7845/**
Greg Rose90e04072014-03-06 08:59:57 +00007846 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007847 * @vsi: the VSI being configured
7848 *
7849 * We allocate one q_vector per queue interrupt. If allocation fails we
7850 * return -ENOMEM.
7851 **/
Greg Rose90e04072014-03-06 08:59:57 +00007852static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007853{
7854 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007855 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007856
7857 /* if not MSIX, give the one vector only to the LAN VSI */
7858 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7859 num_q_vectors = vsi->num_q_vectors;
7860 else if (vsi == pf->vsi[pf->lan_vsi])
7861 num_q_vectors = 1;
7862 else
7863 return -EINVAL;
7864
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007865 current_cpu = cpumask_first(cpu_online_mask);
7866
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007867 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007868 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00007869 if (err)
7870 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007871 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
7872 if (unlikely(current_cpu >= nr_cpu_ids))
7873 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007874 }
7875
7876 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007877
7878err_out:
7879 while (v_idx--)
7880 i40e_free_q_vector(vsi, v_idx);
7881
7882 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007883}
7884
7885/**
7886 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7887 * @pf: board private structure to initialize
7888 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007889static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007890{
Shannon Nelson3b444392015-02-26 16:15:57 +00007891 int vectors = 0;
7892 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007893
7894 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007895 vectors = i40e_init_msix(pf);
7896 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007897 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007898 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007899#ifdef I40E_FCOE
7900 I40E_FLAG_FCOE_ENABLED |
7901#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007902 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007903 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07007904 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007905 I40E_FLAG_SRIOV_ENABLED |
7906 I40E_FLAG_FD_SB_ENABLED |
7907 I40E_FLAG_FD_ATR_ENABLED |
7908 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007909
7910 /* rework the queue expectations without MSIX */
7911 i40e_determine_queue_usage(pf);
7912 }
7913 }
7914
7915 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7916 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007917 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007918 vectors = pci_enable_msi(pf->pdev);
7919 if (vectors < 0) {
7920 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7921 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007922 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7923 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007924 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007925 }
7926
Shannon Nelson958a3e32013-09-28 07:13:28 +00007927 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007928 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007929
Shannon Nelson3b444392015-02-26 16:15:57 +00007930 /* set up vector assignment tracking */
7931 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7932 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007933 if (!pf->irq_pile) {
7934 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7935 return -ENOMEM;
7936 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007937 pf->irq_pile->num_entries = vectors;
7938 pf->irq_pile->search_hint = 0;
7939
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007940 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007941 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007942
7943 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007944}
7945
7946/**
7947 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7948 * @pf: board private structure
7949 *
7950 * This sets up the handler for MSIX 0, which is used to manage the
7951 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7952 * when in MSI or Legacy interrupt mode.
7953 **/
7954static int i40e_setup_misc_vector(struct i40e_pf *pf)
7955{
7956 struct i40e_hw *hw = &pf->hw;
7957 int err = 0;
7958
7959 /* Only request the irq if this is the first time through, and
7960 * not when we're rebuilding after a Reset
7961 */
7962 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7963 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007964 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007965 if (err) {
7966 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007967 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007968 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007969 return -EFAULT;
7970 }
7971 }
7972
Jacob Kellerab437b52014-12-14 01:55:08 +00007973 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007974
7975 /* associate no queues to the misc vector */
7976 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
7977 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
7978
7979 i40e_flush(hw);
7980
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08007981 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007982
7983 return err;
7984}
7985
7986/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007987 * i40e_config_rss_aq - Prepare for RSS using AQ commands
7988 * @vsi: vsi structure
7989 * @seed: RSS hash seed
7990 **/
Helin Zhange69ff812015-10-21 19:56:22 -04007991static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
7992 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007993{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007994 struct i40e_pf *pf = vsi->back;
7995 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07007996 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007997
Jacob Keller776b2e12016-07-19 16:23:30 -07007998 if (seed) {
7999 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8000 (struct i40e_aqc_get_set_rss_key_data *)seed;
8001 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8002 if (ret) {
8003 dev_info(&pf->pdev->dev,
8004 "Cannot set RSS key, err %s aq_err %s\n",
8005 i40e_stat_str(hw, ret),
8006 i40e_aq_str(hw, hw->aq.asq_last_status));
8007 return ret;
8008 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008009 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008010 if (lut) {
8011 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008012
Jacob Keller776b2e12016-07-19 16:23:30 -07008013 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8014 if (ret) {
8015 dev_info(&pf->pdev->dev,
8016 "Cannot set RSS lut, err %s aq_err %s\n",
8017 i40e_stat_str(hw, ret),
8018 i40e_aq_str(hw, hw->aq.asq_last_status));
8019 return ret;
8020 }
8021 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008022 return ret;
8023}
8024
8025/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008026 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8027 * @vsi: Pointer to vsi structure
8028 * @seed: Buffter to store the hash keys
8029 * @lut: Buffer to store the lookup table entries
8030 * @lut_size: Size of buffer to store the lookup table entries
8031 *
8032 * Return 0 on success, negative on failure
8033 */
8034static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8035 u8 *lut, u16 lut_size)
8036{
8037 struct i40e_pf *pf = vsi->back;
8038 struct i40e_hw *hw = &pf->hw;
8039 int ret = 0;
8040
8041 if (seed) {
8042 ret = i40e_aq_get_rss_key(hw, vsi->id,
8043 (struct i40e_aqc_get_set_rss_key_data *)seed);
8044 if (ret) {
8045 dev_info(&pf->pdev->dev,
8046 "Cannot get RSS key, err %s aq_err %s\n",
8047 i40e_stat_str(&pf->hw, ret),
8048 i40e_aq_str(&pf->hw,
8049 pf->hw.aq.asq_last_status));
8050 return ret;
8051 }
8052 }
8053
8054 if (lut) {
8055 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8056
8057 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8058 if (ret) {
8059 dev_info(&pf->pdev->dev,
8060 "Cannot get RSS lut, err %s aq_err %s\n",
8061 i40e_stat_str(&pf->hw, ret),
8062 i40e_aq_str(&pf->hw,
8063 pf->hw.aq.asq_last_status));
8064 return ret;
8065 }
8066 }
8067
8068 return ret;
8069}
8070
8071/**
Jacob Keller0582b962016-07-19 16:23:29 -07008072 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8073 * @vsi: VSI structure
8074 **/
8075static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8076{
8077 u8 seed[I40E_HKEY_ARRAY_SIZE];
8078 struct i40e_pf *pf = vsi->back;
8079 u8 *lut;
8080 int ret;
8081
8082 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8083 return 0;
8084
Jacob Keller552b9962016-07-19 16:23:31 -07008085 if (!vsi->rss_size)
8086 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8087 vsi->num_queue_pairs);
8088 if (!vsi->rss_size)
8089 return -EINVAL;
8090
Jacob Keller0582b962016-07-19 16:23:29 -07008091 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8092 if (!lut)
8093 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008094 /* Use the user configured hash keys and lookup table if there is one,
8095 * otherwise use default
8096 */
8097 if (vsi->rss_lut_user)
8098 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8099 else
8100 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8101 if (vsi->rss_hkey_user)
8102 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8103 else
8104 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008105 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8106 kfree(lut);
8107
8108 return ret;
8109}
8110
8111/**
Helin Zhang043dd652015-10-21 19:56:23 -04008112 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008113 * @vsi: Pointer to vsi structure
8114 * @seed: RSS hash seed
8115 * @lut: Lookup table
8116 * @lut_size: Lookup table size
8117 *
8118 * Returns 0 on success, negative on failure
8119 **/
8120static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8121 const u8 *lut, u16 lut_size)
8122{
8123 struct i40e_pf *pf = vsi->back;
8124 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008125 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008126 u8 i;
8127
8128 /* Fill out hash function seed */
8129 if (seed) {
8130 u32 *seed_dw = (u32 *)seed;
8131
Mitch Williamsc4e18682016-04-12 08:30:40 -07008132 if (vsi->type == I40E_VSI_MAIN) {
8133 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8134 i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
8135 seed_dw[i]);
8136 } else if (vsi->type == I40E_VSI_SRIOV) {
8137 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
8138 i40e_write_rx_ctl(hw,
8139 I40E_VFQF_HKEY1(i, vf_id),
8140 seed_dw[i]);
8141 } else {
8142 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8143 }
Helin Zhange69ff812015-10-21 19:56:22 -04008144 }
8145
8146 if (lut) {
8147 u32 *lut_dw = (u32 *)lut;
8148
Mitch Williamsc4e18682016-04-12 08:30:40 -07008149 if (vsi->type == I40E_VSI_MAIN) {
8150 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8151 return -EINVAL;
8152 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8153 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8154 } else if (vsi->type == I40E_VSI_SRIOV) {
8155 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8156 return -EINVAL;
8157 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8158 i40e_write_rx_ctl(hw,
8159 I40E_VFQF_HLUT1(i, vf_id),
8160 lut_dw[i]);
8161 } else {
8162 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8163 }
Helin Zhange69ff812015-10-21 19:56:22 -04008164 }
8165 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008166
8167 return 0;
8168}
8169
8170/**
Helin Zhang043dd652015-10-21 19:56:23 -04008171 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8172 * @vsi: Pointer to VSI structure
8173 * @seed: Buffer to store the keys
8174 * @lut: Buffer to store the lookup table entries
8175 * @lut_size: Size of buffer to store the lookup table entries
8176 *
8177 * Returns 0 on success, negative on failure
8178 */
8179static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8180 u8 *lut, u16 lut_size)
8181{
8182 struct i40e_pf *pf = vsi->back;
8183 struct i40e_hw *hw = &pf->hw;
8184 u16 i;
8185
8186 if (seed) {
8187 u32 *seed_dw = (u32 *)seed;
8188
8189 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008190 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008191 }
8192 if (lut) {
8193 u32 *lut_dw = (u32 *)lut;
8194
8195 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8196 return -EINVAL;
8197 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8198 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8199 }
8200
8201 return 0;
8202}
8203
8204/**
8205 * i40e_config_rss - Configure RSS keys and lut
8206 * @vsi: Pointer to VSI structure
8207 * @seed: RSS hash seed
8208 * @lut: Lookup table
8209 * @lut_size: Lookup table size
8210 *
8211 * Returns 0 on success, negative on failure
8212 */
8213int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8214{
8215 struct i40e_pf *pf = vsi->back;
8216
8217 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8218 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8219 else
8220 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8221}
8222
8223/**
8224 * i40e_get_rss - Get RSS keys and lut
8225 * @vsi: Pointer to VSI structure
8226 * @seed: Buffer to store the keys
8227 * @lut: Buffer to store the lookup table entries
8228 * lut_size: Size of buffer to store the lookup table entries
8229 *
8230 * Returns 0 on success, negative on failure
8231 */
8232int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8233{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008234 struct i40e_pf *pf = vsi->back;
8235
8236 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8237 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8238 else
8239 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008240}
8241
8242/**
Helin Zhange69ff812015-10-21 19:56:22 -04008243 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8244 * @pf: Pointer to board private structure
8245 * @lut: Lookup table
8246 * @rss_table_size: Lookup table size
8247 * @rss_size: Range of queue number for hashing
8248 */
Alan Bradyf1582352016-08-24 11:33:46 -07008249void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8250 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008251{
Helin Zhange69ff812015-10-21 19:56:22 -04008252 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008253
Helin Zhange69ff812015-10-21 19:56:22 -04008254 for (i = 0; i < rss_table_size; i++)
8255 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008256}
8257
8258/**
Helin Zhang043dd652015-10-21 19:56:23 -04008259 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008260 * @pf: board private structure
8261 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008262static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008263{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008264 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008265 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008266 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008267 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008268 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008269 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008270 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008271
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008272 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008273 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8274 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008275 hena |= i40e_pf_get_default_rss_hena(pf);
8276
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008277 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8278 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008279
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008280 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008281 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008282 reg_val = (pf->rss_table_size == 512) ?
8283 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8284 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008285 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008286
Helin Zhang28c58692015-10-26 19:44:27 -04008287 /* Determine the RSS size of the VSI */
8288 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008289 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8290 vsi->num_queue_pairs);
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008291 if (!vsi->rss_size)
8292 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008293
Helin Zhange69ff812015-10-21 19:56:22 -04008294 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8295 if (!lut)
8296 return -ENOMEM;
8297
Helin Zhang28c58692015-10-26 19:44:27 -04008298 /* Use user configured lut if there is one, otherwise use default */
8299 if (vsi->rss_lut_user)
8300 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8301 else
8302 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008303
Helin Zhang28c58692015-10-26 19:44:27 -04008304 /* Use user configured hash key if there is one, otherwise
8305 * use default.
8306 */
8307 if (vsi->rss_hkey_user)
8308 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8309 else
8310 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008311 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008312 kfree(lut);
8313
8314 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008315}
8316
8317/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008318 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8319 * @pf: board private structure
8320 * @queue_count: the requested queue count for rss.
8321 *
8322 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8323 * count which may be different from the requested queue count.
8324 **/
8325int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8326{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008327 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8328 int new_rss_size;
8329
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008330 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8331 return 0;
8332
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008333 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008334
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008335 if (queue_count != vsi->num_queue_pairs) {
8336 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008337 i40e_prep_for_reset(pf);
8338
Helin Zhangacd65442015-10-26 19:44:28 -04008339 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008340
8341 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008342
8343 /* Discard the user configured hash keys and lut, if less
8344 * queues are enabled.
8345 */
8346 if (queue_count < vsi->rss_size) {
8347 i40e_clear_rss_config_user(vsi);
8348 dev_dbg(&pf->pdev->dev,
8349 "discard user configured hash keys and lut\n");
8350 }
8351
8352 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008353 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8354 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008355
Helin Zhang043dd652015-10-21 19:56:23 -04008356 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008357 }
Lihong Yang12815052016-09-27 11:28:48 -07008358 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8359 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008360 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008361}
8362
8363/**
Greg Rosef4492db2015-02-06 08:52:12 +00008364 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8365 * @pf: board private structure
8366 **/
8367i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8368{
8369 i40e_status status;
8370 bool min_valid, max_valid;
8371 u32 max_bw, min_bw;
8372
8373 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8374 &min_valid, &max_valid);
8375
8376 if (!status) {
8377 if (min_valid)
8378 pf->npar_min_bw = min_bw;
8379 if (max_valid)
8380 pf->npar_max_bw = max_bw;
8381 }
8382
8383 return status;
8384}
8385
8386/**
8387 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8388 * @pf: board private structure
8389 **/
8390i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8391{
8392 struct i40e_aqc_configure_partition_bw_data bw_data;
8393 i40e_status status;
8394
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008395 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008396 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008397 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8398 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8399
8400 /* Set the new bandwidths */
8401 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8402
8403 return status;
8404}
8405
8406/**
8407 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8408 * @pf: board private structure
8409 **/
8410i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8411{
8412 /* Commit temporary BW setting to permanent NVM image */
8413 enum i40e_admin_queue_err last_aq_status;
8414 i40e_status ret;
8415 u16 nvm_word;
8416
8417 if (pf->hw.partition_id != 1) {
8418 dev_info(&pf->pdev->dev,
8419 "Commit BW only works on partition 1! This is partition %d",
8420 pf->hw.partition_id);
8421 ret = I40E_NOT_SUPPORTED;
8422 goto bw_commit_out;
8423 }
8424
8425 /* Acquire NVM for read access */
8426 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8427 last_aq_status = pf->hw.aq.asq_last_status;
8428 if (ret) {
8429 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008430 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8431 i40e_stat_str(&pf->hw, ret),
8432 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008433 goto bw_commit_out;
8434 }
8435
8436 /* Read word 0x10 of NVM - SW compatibility word 1 */
8437 ret = i40e_aq_read_nvm(&pf->hw,
8438 I40E_SR_NVM_CONTROL_WORD,
8439 0x10, sizeof(nvm_word), &nvm_word,
8440 false, NULL);
8441 /* Save off last admin queue command status before releasing
8442 * the NVM
8443 */
8444 last_aq_status = pf->hw.aq.asq_last_status;
8445 i40e_release_nvm(&pf->hw);
8446 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008447 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8448 i40e_stat_str(&pf->hw, ret),
8449 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008450 goto bw_commit_out;
8451 }
8452
8453 /* Wait a bit for NVM release to complete */
8454 msleep(50);
8455
8456 /* Acquire NVM for write access */
8457 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8458 last_aq_status = pf->hw.aq.asq_last_status;
8459 if (ret) {
8460 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008461 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8462 i40e_stat_str(&pf->hw, ret),
8463 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008464 goto bw_commit_out;
8465 }
8466 /* Write it back out unchanged to initiate update NVM,
8467 * which will force a write of the shadow (alt) RAM to
8468 * the NVM - thus storing the bandwidth values permanently.
8469 */
8470 ret = i40e_aq_update_nvm(&pf->hw,
8471 I40E_SR_NVM_CONTROL_WORD,
8472 0x10, sizeof(nvm_word),
8473 &nvm_word, true, NULL);
8474 /* Save off last admin queue command status before releasing
8475 * the NVM
8476 */
8477 last_aq_status = pf->hw.aq.asq_last_status;
8478 i40e_release_nvm(&pf->hw);
8479 if (ret)
8480 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008481 "BW settings NOT SAVED, err %s aq_err %s\n",
8482 i40e_stat_str(&pf->hw, ret),
8483 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008484bw_commit_out:
8485
8486 return ret;
8487}
8488
8489/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008490 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8491 * @pf: board private structure to initialize
8492 *
8493 * i40e_sw_init initializes the Adapter private data structure.
8494 * Fields are initialized based on PCI device information and
8495 * OS network device settings (MTU size).
8496 **/
8497static int i40e_sw_init(struct i40e_pf *pf)
8498{
8499 int err = 0;
8500 int size;
8501
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008502 /* Set default capability flags */
8503 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8504 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008505 I40E_FLAG_MSIX_ENABLED;
8506
Mitch Williamsca99eb92014-04-04 04:43:07 +00008507 /* Set default ITR */
8508 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8509 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8510
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008511 /* Depending on PF configurations, it is possible that the RSS
8512 * maximum might end up larger than the available queues
8513 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008514 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008515 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008516 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008517 pf->rss_size_max = min_t(int, pf->rss_size_max,
8518 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008519 if (pf->hw.func_caps.rss) {
8520 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008521 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8522 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008523 }
8524
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008525 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008526 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008527 pf->flags |= I40E_FLAG_MFP_ENABLED;
8528 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008529 if (i40e_get_npar_bw_setting(pf))
8530 dev_warn(&pf->pdev->dev,
8531 "Could not get NPAR bw settings\n");
8532 else
8533 dev_info(&pf->pdev->dev,
8534 "Min BW = %8.8x, Max BW = %8.8x\n",
8535 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008536 }
8537
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008538 /* FW/NVM is not yet fixed in this regard */
8539 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8540 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8541 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8542 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008543 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8544 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008545 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008546 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008547 else
8548 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008549 pf->fdir_pf_filter_count =
8550 pf->hw.func_caps.fd_filters_guaranteed;
8551 pf->hw.fdir_shared_filter_count =
8552 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008553 }
8554
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008555 if (i40e_is_mac_710(&pf->hw) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008556 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008557 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008558 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008559 /* No DCB support for FW < v4.33 */
8560 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8561 }
8562
8563 /* Disable FW LLDP if FW < v4.3 */
8564 if (i40e_is_mac_710(&pf->hw) &&
8565 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8566 (pf->hw.aq.fw_maj_ver < 4)))
8567 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8568
8569 /* Use the FW Set LLDP MIB API if FW > v4.40 */
8570 if (i40e_is_mac_710(&pf->hw) &&
8571 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8572 (pf->hw.aq.fw_maj_ver >= 5)))
8573 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008574
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008575 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008576 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008577 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008578 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008579 }
8580
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008581 if (pf->hw.func_caps.iwarp) {
8582 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8583 /* IWARP needs one extra vector for CQP just like MISC.*/
8584 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8585 }
8586
Vasu Dev38e00432014-08-01 13:27:03 -07008587#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008588 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008589
8590#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008591#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008592 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008593 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8594 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8595 pf->num_req_vfs = min_t(int,
8596 pf->hw.func_caps.num_vfs,
8597 I40E_MAX_VF_COUNT);
8598 }
8599#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008600 if (pf->hw.mac.type == I40E_MAC_X722) {
8601 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8602 I40E_FLAG_128_QP_RSS_CAPABLE |
8603 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8604 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8605 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008606 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
Jesse Brandeburg8e2cc0e2016-02-29 11:00:24 -08008607 I40E_FLAG_NO_PCI_LINK_CHECK |
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008608 I40E_FLAG_USE_SET_LLDP_MIB |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008609 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008610 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8611 ((pf->hw.aq.api_maj_ver == 1) &&
8612 (pf->hw.aq.api_min_ver > 4))) {
8613 /* Supported in FW API version higher than 1.4 */
8614 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008615 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8616 } else {
8617 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008618 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008619
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008620 pf->eeprom_version = 0xDEAD;
8621 pf->lan_veb = I40E_NO_VEB;
8622 pf->lan_vsi = I40E_NO_VSI;
8623
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008624 /* By default FW has this off for performance reasons */
8625 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8626
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008627 /* set up queue assignment tracking */
8628 size = sizeof(struct i40e_lump_tracking)
8629 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8630 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8631 if (!pf->qp_pile) {
8632 err = -ENOMEM;
8633 goto sw_init_done;
8634 }
8635 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8636 pf->qp_pile->search_hint = 0;
8637
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008638 pf->tx_timeout_recovery_level = 1;
8639
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008640 mutex_init(&pf->switch_mutex);
8641
Greg Rosec668a122015-02-26 16:10:39 +00008642 /* If NPAR is enabled nudge the Tx scheduler */
8643 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8644 i40e_set_npar_bw_setting(pf);
8645
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008646sw_init_done:
8647 return err;
8648}
8649
8650/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008651 * i40e_set_ntuple - set the ntuple feature flag and take action
8652 * @pf: board private structure to initialize
8653 * @features: the feature set that the stack is suggesting
8654 *
8655 * returns a bool to indicate if reset needs to happen
8656 **/
8657bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8658{
8659 bool need_reset = false;
8660
8661 /* Check if Flow Director n-tuple support was enabled or disabled. If
8662 * the state changed, we need to reset.
8663 */
8664 if (features & NETIF_F_NTUPLE) {
8665 /* Enable filters and mark for reset */
8666 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8667 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008668 /* enable FD_SB only if there is MSI-X vector */
8669 if (pf->num_fdsb_msix > 0)
8670 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008671 } else {
8672 /* turn off filters, mark for reset and clear SW filter list */
8673 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8674 need_reset = true;
8675 i40e_fdir_filter_exit(pf);
8676 }
8677 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008678 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008679 /* reset fd counters */
8680 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8681 pf->fdir_pf_active_filters = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008682 /* if ATR was auto disabled it can be re-enabled. */
8683 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller234dc4e2016-09-06 18:05:09 -07008684 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008685 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008686 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8687 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8688 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008689 }
8690 return need_reset;
8691}
8692
8693/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008694 * i40e_clear_rss_lut - clear the rx hash lookup table
8695 * @vsi: the VSI being configured
8696 **/
8697static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8698{
8699 struct i40e_pf *pf = vsi->back;
8700 struct i40e_hw *hw = &pf->hw;
8701 u16 vf_id = vsi->vf_id;
8702 u8 i;
8703
8704 if (vsi->type == I40E_VSI_MAIN) {
8705 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8706 wr32(hw, I40E_PFQF_HLUT(i), 0);
8707 } else if (vsi->type == I40E_VSI_SRIOV) {
8708 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8709 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8710 } else {
8711 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8712 }
8713}
8714
8715/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008716 * i40e_set_features - set the netdev feature flags
8717 * @netdev: ptr to the netdev being adjusted
8718 * @features: the feature set that the stack is suggesting
8719 **/
8720static int i40e_set_features(struct net_device *netdev,
8721 netdev_features_t features)
8722{
8723 struct i40e_netdev_priv *np = netdev_priv(netdev);
8724 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008725 struct i40e_pf *pf = vsi->back;
8726 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008727
Alan Bradyd8ec9862016-07-27 12:02:38 -07008728 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8729 i40e_pf_config_rss(pf);
8730 else if (!(features & NETIF_F_RXHASH) &&
8731 netdev->features & NETIF_F_RXHASH)
8732 i40e_clear_rss_lut(vsi);
8733
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008734 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8735 i40e_vlan_stripping_enable(vsi);
8736 else
8737 i40e_vlan_stripping_disable(vsi);
8738
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008739 need_reset = i40e_set_ntuple(pf, features);
8740
8741 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008742 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008743
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008744 return 0;
8745}
8746
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008747/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008748 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008749 * @pf: board private structure
8750 * @port: The UDP port to look up
8751 *
8752 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8753 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008754static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008755{
8756 u8 i;
8757
8758 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008759 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008760 return i;
8761 }
8762
8763 return i;
8764}
8765
8766/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008767 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008768 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008769 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008770 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008771static void i40e_udp_tunnel_add(struct net_device *netdev,
8772 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008773{
8774 struct i40e_netdev_priv *np = netdev_priv(netdev);
8775 struct i40e_vsi *vsi = np->vsi;
8776 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008777 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008778 u8 next_idx;
8779 u8 idx;
8780
Singhai, Anjali6a899022015-12-14 12:21:18 -08008781 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008782
8783 /* Check if port already exists */
8784 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008785 netdev_info(netdev, "port %d already offloaded\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008786 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008787 return;
8788 }
8789
8790 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008791 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008792
8793 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008794 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008795 ntohs(port));
8796 return;
8797 }
8798
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008799 switch (ti->type) {
8800 case UDP_TUNNEL_TYPE_VXLAN:
8801 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8802 break;
8803 case UDP_TUNNEL_TYPE_GENEVE:
8804 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8805 return;
8806 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8807 break;
8808 default:
8809 return;
8810 }
8811
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008812 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008813 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008814 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8815 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008816}
8817
8818/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008819 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008820 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008821 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008822 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008823static void i40e_udp_tunnel_del(struct net_device *netdev,
8824 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008825{
8826 struct i40e_netdev_priv *np = netdev_priv(netdev);
8827 struct i40e_vsi *vsi = np->vsi;
8828 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008829 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008830 u8 idx;
8831
Singhai, Anjali6a899022015-12-14 12:21:18 -08008832 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008833
8834 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008835 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8836 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008837
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008838 switch (ti->type) {
8839 case UDP_TUNNEL_TYPE_VXLAN:
8840 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8841 goto not_found;
8842 break;
8843 case UDP_TUNNEL_TYPE_GENEVE:
8844 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8845 goto not_found;
8846 break;
8847 default:
8848 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008849 }
8850
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008851 /* if port exists, set it to 0 (mark for deletion)
8852 * and make it pending
8853 */
8854 pf->udp_ports[idx].index = 0;
8855 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008856 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8857
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008858 return;
8859not_found:
8860 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
8861 ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08008862}
8863
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008864static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008865 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008866{
8867 struct i40e_netdev_priv *np = netdev_priv(netdev);
8868 struct i40e_pf *pf = np->vsi->back;
8869 struct i40e_hw *hw = &pf->hw;
8870
8871 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8872 return -EOPNOTSUPP;
8873
8874 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8875 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8876
8877 return 0;
8878}
8879
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008880/**
8881 * i40e_ndo_fdb_add - add an entry to the hardware database
8882 * @ndm: the input from the stack
8883 * @tb: pointer to array of nladdr (unused)
8884 * @dev: the net device pointer
8885 * @addr: the MAC address entry being added
8886 * @flags: instructions from stack about fdb operation
8887 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008888static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8889 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008890 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008891 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008892{
8893 struct i40e_netdev_priv *np = netdev_priv(dev);
8894 struct i40e_pf *pf = np->vsi->back;
8895 int err = 0;
8896
8897 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8898 return -EOPNOTSUPP;
8899
Or Gerlitz65891fe2014-12-14 18:19:05 +02008900 if (vid) {
8901 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8902 return -EINVAL;
8903 }
8904
Greg Rose4ba0dea2014-03-06 08:59:55 +00008905 /* Hardware does not support aging addresses so if a
8906 * ndm_state is given only allow permanent addresses
8907 */
8908 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
8909 netdev_info(dev, "FDB only supports static addresses\n");
8910 return -EINVAL;
8911 }
8912
8913 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
8914 err = dev_uc_add_excl(dev, addr);
8915 else if (is_multicast_ether_addr(addr))
8916 err = dev_mc_add_excl(dev, addr);
8917 else
8918 err = -EINVAL;
8919
8920 /* Only return duplicate errors if NLM_F_EXCL is set */
8921 if (err == -EEXIST && !(flags & NLM_F_EXCL))
8922 err = 0;
8923
8924 return err;
8925}
8926
Neerav Parikh51616012015-02-06 08:52:14 +00008927/**
8928 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
8929 * @dev: the netdev being configured
8930 * @nlh: RTNL message
8931 *
8932 * Inserts a new hardware bridge if not already created and
8933 * enables the bridging mode requested (VEB or VEPA). If the
8934 * hardware bridge has already been inserted and the request
8935 * is to change the mode then that requires a PF reset to
8936 * allow rebuild of the components with required hardware
8937 * bridge mode enabled.
8938 **/
8939static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04008940 struct nlmsghdr *nlh,
8941 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00008942{
8943 struct i40e_netdev_priv *np = netdev_priv(dev);
8944 struct i40e_vsi *vsi = np->vsi;
8945 struct i40e_pf *pf = vsi->back;
8946 struct i40e_veb *veb = NULL;
8947 struct nlattr *attr, *br_spec;
8948 int i, rem;
8949
8950 /* Only for PF VSI for now */
8951 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8952 return -EOPNOTSUPP;
8953
8954 /* Find the HW bridge for PF VSI */
8955 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8956 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8957 veb = pf->veb[i];
8958 }
8959
8960 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8961
8962 nla_for_each_nested(attr, br_spec, rem) {
8963 __u16 mode;
8964
8965 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8966 continue;
8967
8968 mode = nla_get_u16(attr);
8969 if ((mode != BRIDGE_MODE_VEPA) &&
8970 (mode != BRIDGE_MODE_VEB))
8971 return -EINVAL;
8972
8973 /* Insert a new HW bridge */
8974 if (!veb) {
8975 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8976 vsi->tc_config.enabled_tc);
8977 if (veb) {
8978 veb->bridge_mode = mode;
8979 i40e_config_bridge_mode(veb);
8980 } else {
8981 /* No Bridge HW offload available */
8982 return -ENOENT;
8983 }
8984 break;
8985 } else if (mode != veb->bridge_mode) {
8986 /* Existing HW bridge but different mode needs reset */
8987 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008988 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
8989 if (mode == BRIDGE_MODE_VEB)
8990 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
8991 else
8992 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
8993 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00008994 break;
8995 }
8996 }
8997
8998 return 0;
8999}
9000
9001/**
9002 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9003 * @skb: skb buff
9004 * @pid: process id
9005 * @seq: RTNL message seq #
9006 * @dev: the netdev being configured
9007 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009008 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009009 *
9010 * Return the mode in which the hardware bridge is operating in
9011 * i.e VEB or VEPA.
9012 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009013static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9014 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009015 u32 __always_unused filter_mask,
9016 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009017{
9018 struct i40e_netdev_priv *np = netdev_priv(dev);
9019 struct i40e_vsi *vsi = np->vsi;
9020 struct i40e_pf *pf = vsi->back;
9021 struct i40e_veb *veb = NULL;
9022 int i;
9023
9024 /* Only for PF VSI for now */
9025 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9026 return -EOPNOTSUPP;
9027
9028 /* Find the HW bridge for the PF VSI */
9029 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9030 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9031 veb = pf->veb[i];
9032 }
9033
9034 if (!veb)
9035 return 0;
9036
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009037 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009038 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009039}
Neerav Parikh51616012015-02-06 08:52:14 +00009040
Singhai, Anjali6a899022015-12-14 12:21:18 -08009041/* Hardware supports L4 tunnel length of 128B (=2^7) which includes
9042 * inner mac plus all inner ethertypes.
9043 */
9044#define I40E_MAX_TUNNEL_HDR_LEN 128
Joe Stringerf44a75e2015-04-14 17:09:14 -07009045/**
9046 * i40e_features_check - Validate encapsulated packet conforms to limits
9047 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009048 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009049 * @features: Offload features that the stack believes apply
9050 **/
9051static netdev_features_t i40e_features_check(struct sk_buff *skb,
9052 struct net_device *dev,
9053 netdev_features_t features)
9054{
9055 if (skb->encapsulation &&
Singhai, Anjali6a899022015-12-14 12:21:18 -08009056 ((skb_inner_network_header(skb) - skb_transport_header(skb)) >
Joe Stringerf44a75e2015-04-14 17:09:14 -07009057 I40E_MAX_TUNNEL_HDR_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08009058 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009059
9060 return features;
9061}
9062
Shannon Nelson37a29732015-02-27 09:15:19 +00009063static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009064 .ndo_open = i40e_open,
9065 .ndo_stop = i40e_close,
9066 .ndo_start_xmit = i40e_lan_xmit_frame,
9067 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9068 .ndo_set_rx_mode = i40e_set_rx_mode,
9069 .ndo_validate_addr = eth_validate_addr,
9070 .ndo_set_mac_address = i40e_set_mac,
9071 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009072 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009073 .ndo_tx_timeout = i40e_tx_timeout,
9074 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9075 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9076#ifdef CONFIG_NET_POLL_CONTROLLER
9077 .ndo_poll_controller = i40e_netpoll,
9078#endif
John Fastabende4c67342016-02-16 21:16:15 -08009079 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009080#ifdef I40E_FCOE
9081 .ndo_fcoe_enable = i40e_fcoe_enable,
9082 .ndo_fcoe_disable = i40e_fcoe_disable,
9083#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009084 .ndo_set_features = i40e_set_features,
9085 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9086 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009087 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009088 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009089 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009090 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009091 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009092 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9093 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009094 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009095 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009096 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009097 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9098 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009099};
9100
9101/**
9102 * i40e_config_netdev - Setup the netdev flags
9103 * @vsi: the VSI being configured
9104 *
9105 * Returns 0 on success, negative value on failure
9106 **/
9107static int i40e_config_netdev(struct i40e_vsi *vsi)
9108{
9109 struct i40e_pf *pf = vsi->back;
9110 struct i40e_hw *hw = &pf->hw;
9111 struct i40e_netdev_priv *np;
9112 struct net_device *netdev;
9113 u8 mac_addr[ETH_ALEN];
9114 int etherdev_size;
9115
9116 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009117 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009118 if (!netdev)
9119 return -ENOMEM;
9120
9121 vsi->netdev = netdev;
9122 np = netdev_priv(netdev);
9123 np->vsi = vsi;
9124
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009125 netdev->hw_enc_features |= NETIF_F_SG |
9126 NETIF_F_IP_CSUM |
9127 NETIF_F_IPV6_CSUM |
9128 NETIF_F_HIGHDMA |
9129 NETIF_F_SOFT_FEATURES |
9130 NETIF_F_TSO |
9131 NETIF_F_TSO_ECN |
9132 NETIF_F_TSO6 |
9133 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009134 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07009135 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07009136 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009137 NETIF_F_GSO_UDP_TUNNEL |
9138 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009139 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009140 NETIF_F_SCTP_CRC |
9141 NETIF_F_RXHASH |
9142 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009143 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009144
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009145 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009146 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9147
9148 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009149
9150 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009151 netdev->vlan_features |= netdev->hw_enc_features |
9152 NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009153
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009154 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009155 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009156
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009157 netdev->hw_features |= netdev->hw_enc_features |
9158 NETIF_F_HW_VLAN_CTAG_TX |
9159 NETIF_F_HW_VLAN_CTAG_RX;
9160
9161 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009162 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009163
9164 if (vsi->type == I40E_VSI_MAIN) {
9165 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009166 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009167 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009168 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009169 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009170 } else {
9171 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9172 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9173 pf->vsi[pf->lan_vsi]->netdev->name);
9174 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009175
Jacob Keller278e7d02016-10-05 09:30:37 -07009176 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009177 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009178 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009179 }
Kiran Patil21659032015-09-30 14:09:03 -04009180
Greg Rose9a173902014-05-22 06:32:02 +00009181 ether_addr_copy(netdev->dev_addr, mac_addr);
9182 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009183
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009184 netdev->priv_flags |= IFF_UNICAST_FLT;
9185 netdev->priv_flags |= IFF_SUPP_NOFCS;
9186 /* Setup netdev TC information */
9187 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9188
9189 netdev->netdev_ops = &i40e_netdev_ops;
9190 netdev->watchdog_timeo = 5 * HZ;
9191 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009192#ifdef I40E_FCOE
9193 i40e_fcoe_config_netdev(netdev, vsi);
9194#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009195
Jarod Wilson91c527a2016-10-17 15:54:05 -04009196 /* MTU range: 68 - 9706 */
9197 netdev->min_mtu = ETH_MIN_MTU;
9198 netdev->max_mtu = I40E_MAX_RXBUFFER -
9199 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9200
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009201 return 0;
9202}
9203
9204/**
9205 * i40e_vsi_delete - Delete a VSI from the switch
9206 * @vsi: the VSI being removed
9207 *
9208 * Returns 0 on success, negative value on failure
9209 **/
9210static void i40e_vsi_delete(struct i40e_vsi *vsi)
9211{
9212 /* remove default VSI is not allowed */
9213 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9214 return;
9215
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009216 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009217}
9218
9219/**
Neerav Parikh51616012015-02-06 08:52:14 +00009220 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9221 * @vsi: the VSI being queried
9222 *
9223 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9224 **/
9225int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9226{
9227 struct i40e_veb *veb;
9228 struct i40e_pf *pf = vsi->back;
9229
9230 /* Uplink is not a bridge so default to VEB */
9231 if (vsi->veb_idx == I40E_NO_VEB)
9232 return 1;
9233
9234 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009235 if (!veb) {
9236 dev_info(&pf->pdev->dev,
9237 "There is no veb associated with the bridge\n");
9238 return -ENOENT;
9239 }
Neerav Parikh51616012015-02-06 08:52:14 +00009240
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009241 /* Uplink is a bridge in VEPA mode */
9242 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9243 return 0;
9244 } else {
9245 /* Uplink is a bridge in VEB mode */
9246 return 1;
9247 }
9248
9249 /* VEPA is now default bridge, so return 0 */
9250 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009251}
9252
9253/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009254 * i40e_add_vsi - Add a VSI to the switch
9255 * @vsi: the VSI being configured
9256 *
9257 * This initializes a VSI context depending on the VSI type to be added and
9258 * passes it down to the add_vsi aq command.
9259 **/
9260static int i40e_add_vsi(struct i40e_vsi *vsi)
9261{
9262 int ret = -ENODEV;
Kiran Patilf6bd0962016-06-20 09:10:34 -07009263 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009264 struct i40e_pf *pf = vsi->back;
9265 struct i40e_hw *hw = &pf->hw;
9266 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009267 struct i40e_mac_filter *f;
9268 struct hlist_node *h;
9269 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009270
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009271 u8 enabled_tc = 0x1; /* TC0 enabled */
9272 int f_count = 0;
9273
9274 memset(&ctxt, 0, sizeof(ctxt));
9275 switch (vsi->type) {
9276 case I40E_VSI_MAIN:
9277 /* The PF's main VSI is already setup as part of the
9278 * device initialization, so we'll not bother with
9279 * the add_vsi call, but we will retrieve the current
9280 * VSI context.
9281 */
9282 ctxt.seid = pf->main_vsi_seid;
9283 ctxt.pf_num = pf->hw.pf_id;
9284 ctxt.vf_num = 0;
9285 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9286 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9287 if (ret) {
9288 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009289 "couldn't get PF vsi config, err %s aq_err %s\n",
9290 i40e_stat_str(&pf->hw, ret),
9291 i40e_aq_str(&pf->hw,
9292 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009293 return -ENOENT;
9294 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009295 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009296 vsi->info.valid_sections = 0;
9297
9298 vsi->seid = ctxt.seid;
9299 vsi->id = ctxt.vsi_number;
9300
9301 enabled_tc = i40e_pf_get_tc_map(pf);
9302
9303 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009304 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9305 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009306 memset(&ctxt, 0, sizeof(ctxt));
9307 ctxt.seid = pf->main_vsi_seid;
9308 ctxt.pf_num = pf->hw.pf_id;
9309 ctxt.vf_num = 0;
9310 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9311 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9312 if (ret) {
9313 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009314 "update vsi failed, err %s aq_err %s\n",
9315 i40e_stat_str(&pf->hw, ret),
9316 i40e_aq_str(&pf->hw,
9317 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009318 ret = -ENOENT;
9319 goto err;
9320 }
9321 /* update the local VSI info queue map */
9322 i40e_vsi_update_queue_map(vsi, &ctxt);
9323 vsi->info.valid_sections = 0;
9324 } else {
9325 /* Default/Main VSI is only enabled for TC0
9326 * reconfigure it to enable all TCs that are
9327 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009328 * For MFP case the iSCSI PF would use this
9329 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009330 */
9331 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9332 if (ret) {
9333 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009334 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9335 enabled_tc,
9336 i40e_stat_str(&pf->hw, ret),
9337 i40e_aq_str(&pf->hw,
9338 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009339 ret = -ENOENT;
9340 }
9341 }
9342 break;
9343
9344 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009345 ctxt.pf_num = hw->pf_id;
9346 ctxt.vf_num = 0;
9347 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009348 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009349 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009350 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9351 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009352 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009353 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009354 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009355 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009356 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009357 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009358 break;
9359
9360 case I40E_VSI_VMDQ2:
9361 ctxt.pf_num = hw->pf_id;
9362 ctxt.vf_num = 0;
9363 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009364 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009365 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9366
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009367 /* This VSI is connected to VEB so the switch_id
9368 * should be set to zero by default.
9369 */
Neerav Parikh51616012015-02-06 08:52:14 +00009370 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9371 ctxt.info.valid_sections |=
9372 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9373 ctxt.info.switch_id =
9374 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9375 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009376
9377 /* Setup the VSI tx/rx queue map for TC0 only for now */
9378 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9379 break;
9380
9381 case I40E_VSI_SRIOV:
9382 ctxt.pf_num = hw->pf_id;
9383 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9384 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009385 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009386 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9387
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009388 /* This VSI is connected to VEB so the switch_id
9389 * should be set to zero by default.
9390 */
Neerav Parikh51616012015-02-06 08:52:14 +00009391 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9392 ctxt.info.valid_sections |=
9393 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9394 ctxt.info.switch_id =
9395 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9396 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009397
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009398 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9399 ctxt.info.valid_sections |=
9400 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9401 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009402 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9403 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009404 }
9405
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009406 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9407 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009408 if (pf->vf[vsi->vf_id].spoofchk) {
9409 ctxt.info.valid_sections |=
9410 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9411 ctxt.info.sec_flags |=
9412 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9413 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9414 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009415 /* Setup the VSI tx/rx queue map for TC0 only for now */
9416 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9417 break;
9418
Vasu Dev38e00432014-08-01 13:27:03 -07009419#ifdef I40E_FCOE
9420 case I40E_VSI_FCOE:
9421 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9422 if (ret) {
9423 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9424 return ret;
9425 }
9426 break;
9427
9428#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009429 case I40E_VSI_IWARP:
9430 /* send down message to iWARP */
9431 break;
9432
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009433 default:
9434 return -ENODEV;
9435 }
9436
9437 if (vsi->type != I40E_VSI_MAIN) {
9438 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9439 if (ret) {
9440 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009441 "add vsi failed, err %s aq_err %s\n",
9442 i40e_stat_str(&pf->hw, ret),
9443 i40e_aq_str(&pf->hw,
9444 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009445 ret = -ENOENT;
9446 goto err;
9447 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009448 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009449 vsi->info.valid_sections = 0;
9450 vsi->seid = ctxt.seid;
9451 vsi->id = ctxt.vsi_number;
9452 }
Kiran Patilf6bd0962016-06-20 09:10:34 -07009453 /* Except FDIR VSI, for all othet VSI set the broadcast filter */
9454 if (vsi->type != I40E_VSI_FDIR) {
9455 aq_ret = i40e_aq_set_vsi_broadcast(hw, vsi->seid, true, NULL);
9456 if (aq_ret) {
9457 ret = i40e_aq_rc_to_posix(aq_ret,
9458 hw->aq.asq_last_status);
9459 dev_info(&pf->pdev->dev,
9460 "set brdcast promisc failed, err %s, aq_err %s\n",
9461 i40e_stat_str(hw, aq_ret),
9462 i40e_aq_str(hw, hw->aq.asq_last_status));
9463 }
9464 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009465
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009466 vsi->active_filters = 0;
9467 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009468 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009469 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009470 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009471 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009472 f_count++;
9473 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009474 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009475
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009476 if (f_count) {
9477 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9478 pf->flags |= I40E_FLAG_FILTER_SYNC;
9479 }
9480
9481 /* Update VSI BW information */
9482 ret = i40e_vsi_get_bw_info(vsi);
9483 if (ret) {
9484 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009485 "couldn't get vsi bw info, err %s aq_err %s\n",
9486 i40e_stat_str(&pf->hw, ret),
9487 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009488 /* VSI is already added so not tearing that up */
9489 ret = 0;
9490 }
9491
9492err:
9493 return ret;
9494}
9495
9496/**
9497 * i40e_vsi_release - Delete a VSI and free its resources
9498 * @vsi: the VSI being removed
9499 *
9500 * Returns 0 on success or < 0 on error
9501 **/
9502int i40e_vsi_release(struct i40e_vsi *vsi)
9503{
Jacob Keller278e7d02016-10-05 09:30:37 -07009504 struct i40e_mac_filter *f;
9505 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009506 struct i40e_veb *veb = NULL;
9507 struct i40e_pf *pf;
9508 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009509 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009510
9511 pf = vsi->back;
9512
9513 /* release of a VEB-owner or last VSI is not allowed */
9514 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9515 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9516 vsi->seid, vsi->uplink_seid);
9517 return -ENODEV;
9518 }
9519 if (vsi == pf->vsi[pf->lan_vsi] &&
9520 !test_bit(__I40E_DOWN, &pf->state)) {
9521 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9522 return -ENODEV;
9523 }
9524
9525 uplink_seid = vsi->uplink_seid;
9526 if (vsi->type != I40E_VSI_SRIOV) {
9527 if (vsi->netdev_registered) {
9528 vsi->netdev_registered = false;
9529 if (vsi->netdev) {
9530 /* results in a call to i40e_close() */
9531 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009532 }
9533 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009534 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009535 }
9536 i40e_vsi_disable_irq(vsi);
9537 }
9538
Jacob Keller278e7d02016-10-05 09:30:37 -07009539 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009540
9541 /* clear the sync flag on all filters */
9542 if (vsi->netdev) {
9543 __dev_uc_unsync(vsi->netdev, NULL);
9544 __dev_mc_unsync(vsi->netdev, NULL);
9545 }
9546
9547 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009548 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009549 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009550
Jacob Keller278e7d02016-10-05 09:30:37 -07009551 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009552
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009553 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009554
9555 i40e_vsi_delete(vsi);
9556 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009557 if (vsi->netdev) {
9558 free_netdev(vsi->netdev);
9559 vsi->netdev = NULL;
9560 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009561 i40e_vsi_clear_rings(vsi);
9562 i40e_vsi_clear(vsi);
9563
9564 /* If this was the last thing on the VEB, except for the
9565 * controlling VSI, remove the VEB, which puts the controlling
9566 * VSI onto the next level down in the switch.
9567 *
9568 * Well, okay, there's one more exception here: don't remove
9569 * the orphan VEBs yet. We'll wait for an explicit remove request
9570 * from up the network stack.
9571 */
Mitch Williams505682c2014-05-20 08:01:37 +00009572 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009573 if (pf->vsi[i] &&
9574 pf->vsi[i]->uplink_seid == uplink_seid &&
9575 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9576 n++; /* count the VSIs */
9577 }
9578 }
9579 for (i = 0; i < I40E_MAX_VEB; i++) {
9580 if (!pf->veb[i])
9581 continue;
9582 if (pf->veb[i]->uplink_seid == uplink_seid)
9583 n++; /* count the VEBs */
9584 if (pf->veb[i]->seid == uplink_seid)
9585 veb = pf->veb[i];
9586 }
9587 if (n == 0 && veb && veb->uplink_seid != 0)
9588 i40e_veb_release(veb);
9589
9590 return 0;
9591}
9592
9593/**
9594 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9595 * @vsi: ptr to the VSI
9596 *
9597 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9598 * corresponding SW VSI structure and initializes num_queue_pairs for the
9599 * newly allocated VSI.
9600 *
9601 * Returns 0 on success or negative on failure
9602 **/
9603static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9604{
9605 int ret = -ENOENT;
9606 struct i40e_pf *pf = vsi->back;
9607
Alexander Duyck493fb302013-09-28 07:01:44 +00009608 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009609 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9610 vsi->seid);
9611 return -EEXIST;
9612 }
9613
9614 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009615 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009616 vsi->seid, vsi->base_vector);
9617 return -EEXIST;
9618 }
9619
Greg Rose90e04072014-03-06 08:59:57 +00009620 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009621 if (ret) {
9622 dev_info(&pf->pdev->dev,
9623 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9624 vsi->num_q_vectors, vsi->seid, ret);
9625 vsi->num_q_vectors = 0;
9626 goto vector_setup_out;
9627 }
9628
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009629 /* In Legacy mode, we do not have to get any other vector since we
9630 * piggyback on the misc/ICR0 for queue interrupts.
9631 */
9632 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9633 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009634 if (vsi->num_q_vectors)
9635 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9636 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009637 if (vsi->base_vector < 0) {
9638 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009639 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9640 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009641 i40e_vsi_free_q_vectors(vsi);
9642 ret = -ENOENT;
9643 goto vector_setup_out;
9644 }
9645
9646vector_setup_out:
9647 return ret;
9648}
9649
9650/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009651 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9652 * @vsi: pointer to the vsi.
9653 *
9654 * This re-allocates a vsi's queue resources.
9655 *
9656 * Returns pointer to the successfully allocated and configured VSI sw struct
9657 * on success, otherwise returns NULL on failure.
9658 **/
9659static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9660{
John Underwoodf5340392016-02-18 09:19:24 -08009661 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009662 u8 enabled_tc;
9663 int ret;
9664
John Underwoodf5340392016-02-18 09:19:24 -08009665 if (!vsi)
9666 return NULL;
9667
9668 pf = vsi->back;
9669
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009670 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9671 i40e_vsi_clear_rings(vsi);
9672
9673 i40e_vsi_free_arrays(vsi, false);
9674 i40e_set_num_rings_in_vsi(vsi);
9675 ret = i40e_vsi_alloc_arrays(vsi, false);
9676 if (ret)
9677 goto err_vsi;
9678
9679 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9680 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009681 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009682 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009683 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009684 goto err_vsi;
9685 }
9686 vsi->base_queue = ret;
9687
9688 /* Update the FW view of the VSI. Force a reset of TC and queue
9689 * layout configurations.
9690 */
9691 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9692 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9693 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9694 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9695
9696 /* assign it some queues */
9697 ret = i40e_alloc_rings(vsi);
9698 if (ret)
9699 goto err_rings;
9700
9701 /* map all of the rings to the q_vectors */
9702 i40e_vsi_map_rings_to_vectors(vsi);
9703 return vsi;
9704
9705err_rings:
9706 i40e_vsi_free_q_vectors(vsi);
9707 if (vsi->netdev_registered) {
9708 vsi->netdev_registered = false;
9709 unregister_netdev(vsi->netdev);
9710 free_netdev(vsi->netdev);
9711 vsi->netdev = NULL;
9712 }
9713 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9714err_vsi:
9715 i40e_vsi_clear(vsi);
9716 return NULL;
9717}
9718
9719/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009720 * i40e_vsi_setup - Set up a VSI by a given type
9721 * @pf: board private structure
9722 * @type: VSI type
9723 * @uplink_seid: the switch element to link to
9724 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9725 *
9726 * This allocates the sw VSI structure and its queue resources, then add a VSI
9727 * to the identified VEB.
9728 *
9729 * Returns pointer to the successfully allocated and configure VSI sw struct on
9730 * success, otherwise returns NULL on failure.
9731 **/
9732struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9733 u16 uplink_seid, u32 param1)
9734{
9735 struct i40e_vsi *vsi = NULL;
9736 struct i40e_veb *veb = NULL;
9737 int ret, i;
9738 int v_idx;
9739
9740 /* The requested uplink_seid must be either
9741 * - the PF's port seid
9742 * no VEB is needed because this is the PF
9743 * or this is a Flow Director special case VSI
9744 * - seid of an existing VEB
9745 * - seid of a VSI that owns an existing VEB
9746 * - seid of a VSI that doesn't own a VEB
9747 * a new VEB is created and the VSI becomes the owner
9748 * - seid of the PF VSI, which is what creates the first VEB
9749 * this is a special case of the previous
9750 *
9751 * Find which uplink_seid we were given and create a new VEB if needed
9752 */
9753 for (i = 0; i < I40E_MAX_VEB; i++) {
9754 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9755 veb = pf->veb[i];
9756 break;
9757 }
9758 }
9759
9760 if (!veb && uplink_seid != pf->mac_seid) {
9761
Mitch Williams505682c2014-05-20 08:01:37 +00009762 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009763 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9764 vsi = pf->vsi[i];
9765 break;
9766 }
9767 }
9768 if (!vsi) {
9769 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9770 uplink_seid);
9771 return NULL;
9772 }
9773
9774 if (vsi->uplink_seid == pf->mac_seid)
9775 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9776 vsi->tc_config.enabled_tc);
9777 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9778 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9779 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009780 if (veb) {
9781 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9782 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009783 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009784 return NULL;
9785 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009786 /* We come up by default in VEPA mode if SRIOV is not
9787 * already enabled, in which case we can't force VEPA
9788 * mode.
9789 */
9790 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9791 veb->bridge_mode = BRIDGE_MODE_VEPA;
9792 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9793 }
Neerav Parikh51616012015-02-06 08:52:14 +00009794 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009795 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009796 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9797 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9798 veb = pf->veb[i];
9799 }
9800 if (!veb) {
9801 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9802 return NULL;
9803 }
9804
9805 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9806 uplink_seid = veb->seid;
9807 }
9808
9809 /* get vsi sw struct */
9810 v_idx = i40e_vsi_mem_alloc(pf, type);
9811 if (v_idx < 0)
9812 goto err_alloc;
9813 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009814 if (!vsi)
9815 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009816 vsi->type = type;
9817 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9818
9819 if (type == I40E_VSI_MAIN)
9820 pf->lan_vsi = v_idx;
9821 else if (type == I40E_VSI_SRIOV)
9822 vsi->vf_id = param1;
9823 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009824 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9825 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009826 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009827 dev_info(&pf->pdev->dev,
9828 "failed to get tracking for %d queues for VSI %d err=%d\n",
9829 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009830 goto err_vsi;
9831 }
9832 vsi->base_queue = ret;
9833
9834 /* get a VSI from the hardware */
9835 vsi->uplink_seid = uplink_seid;
9836 ret = i40e_add_vsi(vsi);
9837 if (ret)
9838 goto err_vsi;
9839
9840 switch (vsi->type) {
9841 /* setup the netdev if needed */
9842 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009843 /* Apply relevant filters if a platform-specific mac
9844 * address was selected.
9845 */
9846 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9847 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9848 if (ret) {
9849 dev_warn(&pf->pdev->dev,
9850 "could not set up macaddr; err %d\n",
9851 ret);
9852 }
9853 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009854 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07009855 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009856 ret = i40e_config_netdev(vsi);
9857 if (ret)
9858 goto err_netdev;
9859 ret = register_netdev(vsi->netdev);
9860 if (ret)
9861 goto err_netdev;
9862 vsi->netdev_registered = true;
9863 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009864#ifdef CONFIG_I40E_DCB
9865 /* Setup DCB netlink interface */
9866 i40e_dcbnl_setup(vsi);
9867#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009868 /* fall through */
9869
9870 case I40E_VSI_FDIR:
9871 /* set up vectors and rings if needed */
9872 ret = i40e_vsi_setup_vectors(vsi);
9873 if (ret)
9874 goto err_msix;
9875
9876 ret = i40e_alloc_rings(vsi);
9877 if (ret)
9878 goto err_rings;
9879
9880 /* map all of the rings to the q_vectors */
9881 i40e_vsi_map_rings_to_vectors(vsi);
9882
9883 i40e_vsi_reset_stats(vsi);
9884 break;
9885
9886 default:
9887 /* no netdev or rings for the other VSI types */
9888 break;
9889 }
9890
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009891 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
9892 (vsi->type == I40E_VSI_VMDQ2)) {
9893 ret = i40e_vsi_config_rss(vsi);
9894 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009895 return vsi;
9896
9897err_rings:
9898 i40e_vsi_free_q_vectors(vsi);
9899err_msix:
9900 if (vsi->netdev_registered) {
9901 vsi->netdev_registered = false;
9902 unregister_netdev(vsi->netdev);
9903 free_netdev(vsi->netdev);
9904 vsi->netdev = NULL;
9905 }
9906err_netdev:
9907 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9908err_vsi:
9909 i40e_vsi_clear(vsi);
9910err_alloc:
9911 return NULL;
9912}
9913
9914/**
9915 * i40e_veb_get_bw_info - Query VEB BW information
9916 * @veb: the veb to query
9917 *
9918 * Query the Tx scheduler BW configuration data for given VEB
9919 **/
9920static int i40e_veb_get_bw_info(struct i40e_veb *veb)
9921{
9922 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
9923 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
9924 struct i40e_pf *pf = veb->pf;
9925 struct i40e_hw *hw = &pf->hw;
9926 u32 tc_bw_max;
9927 int ret = 0;
9928 int i;
9929
9930 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
9931 &bw_data, NULL);
9932 if (ret) {
9933 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009934 "query veb bw config failed, err %s aq_err %s\n",
9935 i40e_stat_str(&pf->hw, ret),
9936 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009937 goto out;
9938 }
9939
9940 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
9941 &ets_data, NULL);
9942 if (ret) {
9943 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009944 "query veb bw ets config failed, err %s aq_err %s\n",
9945 i40e_stat_str(&pf->hw, ret),
9946 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009947 goto out;
9948 }
9949
9950 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
9951 veb->bw_max_quanta = ets_data.tc_bw_max;
9952 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +00009953 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009954 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
9955 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
9956 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
9957 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
9958 veb->bw_tc_limit_credits[i] =
9959 le16_to_cpu(bw_data.tc_bw_limits[i]);
9960 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
9961 }
9962
9963out:
9964 return ret;
9965}
9966
9967/**
9968 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
9969 * @pf: board private structure
9970 *
9971 * On error: returns error code (negative)
9972 * On success: returns vsi index in PF (positive)
9973 **/
9974static int i40e_veb_mem_alloc(struct i40e_pf *pf)
9975{
9976 int ret = -ENOENT;
9977 struct i40e_veb *veb;
9978 int i;
9979
9980 /* Need to protect the allocation of switch elements at the PF level */
9981 mutex_lock(&pf->switch_mutex);
9982
9983 /* VEB list may be fragmented if VEB creation/destruction has
9984 * been happening. We can afford to do a quick scan to look
9985 * for any free slots in the list.
9986 *
9987 * find next empty veb slot, looping back around if necessary
9988 */
9989 i = 0;
9990 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
9991 i++;
9992 if (i >= I40E_MAX_VEB) {
9993 ret = -ENOMEM;
9994 goto err_alloc_veb; /* out of VEB slots! */
9995 }
9996
9997 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
9998 if (!veb) {
9999 ret = -ENOMEM;
10000 goto err_alloc_veb;
10001 }
10002 veb->pf = pf;
10003 veb->idx = i;
10004 veb->enabled_tc = 1;
10005
10006 pf->veb[i] = veb;
10007 ret = i;
10008err_alloc_veb:
10009 mutex_unlock(&pf->switch_mutex);
10010 return ret;
10011}
10012
10013/**
10014 * i40e_switch_branch_release - Delete a branch of the switch tree
10015 * @branch: where to start deleting
10016 *
10017 * This uses recursion to find the tips of the branch to be
10018 * removed, deleting until we get back to and can delete this VEB.
10019 **/
10020static void i40e_switch_branch_release(struct i40e_veb *branch)
10021{
10022 struct i40e_pf *pf = branch->pf;
10023 u16 branch_seid = branch->seid;
10024 u16 veb_idx = branch->idx;
10025 int i;
10026
10027 /* release any VEBs on this VEB - RECURSION */
10028 for (i = 0; i < I40E_MAX_VEB; i++) {
10029 if (!pf->veb[i])
10030 continue;
10031 if (pf->veb[i]->uplink_seid == branch->seid)
10032 i40e_switch_branch_release(pf->veb[i]);
10033 }
10034
10035 /* Release the VSIs on this VEB, but not the owner VSI.
10036 *
10037 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10038 * the VEB itself, so don't use (*branch) after this loop.
10039 */
Mitch Williams505682c2014-05-20 08:01:37 +000010040 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010041 if (!pf->vsi[i])
10042 continue;
10043 if (pf->vsi[i]->uplink_seid == branch_seid &&
10044 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10045 i40e_vsi_release(pf->vsi[i]);
10046 }
10047 }
10048
10049 /* There's one corner case where the VEB might not have been
10050 * removed, so double check it here and remove it if needed.
10051 * This case happens if the veb was created from the debugfs
10052 * commands and no VSIs were added to it.
10053 */
10054 if (pf->veb[veb_idx])
10055 i40e_veb_release(pf->veb[veb_idx]);
10056}
10057
10058/**
10059 * i40e_veb_clear - remove veb struct
10060 * @veb: the veb to remove
10061 **/
10062static void i40e_veb_clear(struct i40e_veb *veb)
10063{
10064 if (!veb)
10065 return;
10066
10067 if (veb->pf) {
10068 struct i40e_pf *pf = veb->pf;
10069
10070 mutex_lock(&pf->switch_mutex);
10071 if (pf->veb[veb->idx] == veb)
10072 pf->veb[veb->idx] = NULL;
10073 mutex_unlock(&pf->switch_mutex);
10074 }
10075
10076 kfree(veb);
10077}
10078
10079/**
10080 * i40e_veb_release - Delete a VEB and free its resources
10081 * @veb: the VEB being removed
10082 **/
10083void i40e_veb_release(struct i40e_veb *veb)
10084{
10085 struct i40e_vsi *vsi = NULL;
10086 struct i40e_pf *pf;
10087 int i, n = 0;
10088
10089 pf = veb->pf;
10090
10091 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010092 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010093 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10094 n++;
10095 vsi = pf->vsi[i];
10096 }
10097 }
10098 if (n != 1) {
10099 dev_info(&pf->pdev->dev,
10100 "can't remove VEB %d with %d VSIs left\n",
10101 veb->seid, n);
10102 return;
10103 }
10104
10105 /* move the remaining VSI to uplink veb */
10106 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10107 if (veb->uplink_seid) {
10108 vsi->uplink_seid = veb->uplink_seid;
10109 if (veb->uplink_seid == pf->mac_seid)
10110 vsi->veb_idx = I40E_NO_VEB;
10111 else
10112 vsi->veb_idx = veb->veb_idx;
10113 } else {
10114 /* floating VEB */
10115 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10116 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10117 }
10118
10119 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10120 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010121}
10122
10123/**
10124 * i40e_add_veb - create the VEB in the switch
10125 * @veb: the VEB to be instantiated
10126 * @vsi: the controlling VSI
10127 **/
10128static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10129{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010130 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010131 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010132 int ret;
10133
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010134 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010135 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010136 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010137
10138 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010139 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010140 dev_info(&pf->pdev->dev,
10141 "couldn't add VEB, err %s aq_err %s\n",
10142 i40e_stat_str(&pf->hw, ret),
10143 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010144 return -EPERM;
10145 }
10146
10147 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010148 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010149 &veb->stats_idx, NULL, NULL, NULL);
10150 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010151 dev_info(&pf->pdev->dev,
10152 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10153 i40e_stat_str(&pf->hw, ret),
10154 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010155 return -EPERM;
10156 }
10157 ret = i40e_veb_get_bw_info(veb);
10158 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010159 dev_info(&pf->pdev->dev,
10160 "couldn't get VEB bw info, err %s aq_err %s\n",
10161 i40e_stat_str(&pf->hw, ret),
10162 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10163 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010164 return -ENOENT;
10165 }
10166
10167 vsi->uplink_seid = veb->seid;
10168 vsi->veb_idx = veb->idx;
10169 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10170
10171 return 0;
10172}
10173
10174/**
10175 * i40e_veb_setup - Set up a VEB
10176 * @pf: board private structure
10177 * @flags: VEB setup flags
10178 * @uplink_seid: the switch element to link to
10179 * @vsi_seid: the initial VSI seid
10180 * @enabled_tc: Enabled TC bit-map
10181 *
10182 * This allocates the sw VEB structure and links it into the switch
10183 * It is possible and legal for this to be a duplicate of an already
10184 * existing VEB. It is also possible for both uplink and vsi seids
10185 * to be zero, in order to create a floating VEB.
10186 *
10187 * Returns pointer to the successfully allocated VEB sw struct on
10188 * success, otherwise returns NULL on failure.
10189 **/
10190struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10191 u16 uplink_seid, u16 vsi_seid,
10192 u8 enabled_tc)
10193{
10194 struct i40e_veb *veb, *uplink_veb = NULL;
10195 int vsi_idx, veb_idx;
10196 int ret;
10197
10198 /* if one seid is 0, the other must be 0 to create a floating relay */
10199 if ((uplink_seid == 0 || vsi_seid == 0) &&
10200 (uplink_seid + vsi_seid != 0)) {
10201 dev_info(&pf->pdev->dev,
10202 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10203 uplink_seid, vsi_seid);
10204 return NULL;
10205 }
10206
10207 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010208 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010209 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10210 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010211 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010212 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10213 vsi_seid);
10214 return NULL;
10215 }
10216
10217 if (uplink_seid && uplink_seid != pf->mac_seid) {
10218 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10219 if (pf->veb[veb_idx] &&
10220 pf->veb[veb_idx]->seid == uplink_seid) {
10221 uplink_veb = pf->veb[veb_idx];
10222 break;
10223 }
10224 }
10225 if (!uplink_veb) {
10226 dev_info(&pf->pdev->dev,
10227 "uplink seid %d not found\n", uplink_seid);
10228 return NULL;
10229 }
10230 }
10231
10232 /* get veb sw struct */
10233 veb_idx = i40e_veb_mem_alloc(pf);
10234 if (veb_idx < 0)
10235 goto err_alloc;
10236 veb = pf->veb[veb_idx];
10237 veb->flags = flags;
10238 veb->uplink_seid = uplink_seid;
10239 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10240 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10241
10242 /* create the VEB in the switch */
10243 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10244 if (ret)
10245 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010246 if (vsi_idx == pf->lan_vsi)
10247 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010248
10249 return veb;
10250
10251err_veb:
10252 i40e_veb_clear(veb);
10253err_alloc:
10254 return NULL;
10255}
10256
10257/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010258 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010259 * @pf: board private structure
10260 * @ele: element we are building info from
10261 * @num_reported: total number of elements
10262 * @printconfig: should we print the contents
10263 *
10264 * helper function to assist in extracting a few useful SEID values.
10265 **/
10266static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10267 struct i40e_aqc_switch_config_element_resp *ele,
10268 u16 num_reported, bool printconfig)
10269{
10270 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10271 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10272 u8 element_type = ele->element_type;
10273 u16 seid = le16_to_cpu(ele->seid);
10274
10275 if (printconfig)
10276 dev_info(&pf->pdev->dev,
10277 "type=%d seid=%d uplink=%d downlink=%d\n",
10278 element_type, seid, uplink_seid, downlink_seid);
10279
10280 switch (element_type) {
10281 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10282 pf->mac_seid = seid;
10283 break;
10284 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10285 /* Main VEB? */
10286 if (uplink_seid != pf->mac_seid)
10287 break;
10288 if (pf->lan_veb == I40E_NO_VEB) {
10289 int v;
10290
10291 /* find existing or else empty VEB */
10292 for (v = 0; v < I40E_MAX_VEB; v++) {
10293 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10294 pf->lan_veb = v;
10295 break;
10296 }
10297 }
10298 if (pf->lan_veb == I40E_NO_VEB) {
10299 v = i40e_veb_mem_alloc(pf);
10300 if (v < 0)
10301 break;
10302 pf->lan_veb = v;
10303 }
10304 }
10305
10306 pf->veb[pf->lan_veb]->seid = seid;
10307 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10308 pf->veb[pf->lan_veb]->pf = pf;
10309 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10310 break;
10311 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10312 if (num_reported != 1)
10313 break;
10314 /* This is immediately after a reset so we can assume this is
10315 * the PF's VSI
10316 */
10317 pf->mac_seid = uplink_seid;
10318 pf->pf_seid = downlink_seid;
10319 pf->main_vsi_seid = seid;
10320 if (printconfig)
10321 dev_info(&pf->pdev->dev,
10322 "pf_seid=%d main_vsi_seid=%d\n",
10323 pf->pf_seid, pf->main_vsi_seid);
10324 break;
10325 case I40E_SWITCH_ELEMENT_TYPE_PF:
10326 case I40E_SWITCH_ELEMENT_TYPE_VF:
10327 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10328 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10329 case I40E_SWITCH_ELEMENT_TYPE_PE:
10330 case I40E_SWITCH_ELEMENT_TYPE_PA:
10331 /* ignore these for now */
10332 break;
10333 default:
10334 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10335 element_type, seid);
10336 break;
10337 }
10338}
10339
10340/**
10341 * i40e_fetch_switch_configuration - Get switch config from firmware
10342 * @pf: board private structure
10343 * @printconfig: should we print the contents
10344 *
10345 * Get the current switch configuration from the device and
10346 * extract a few useful SEID values.
10347 **/
10348int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10349{
10350 struct i40e_aqc_get_switch_config_resp *sw_config;
10351 u16 next_seid = 0;
10352 int ret = 0;
10353 u8 *aq_buf;
10354 int i;
10355
10356 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10357 if (!aq_buf)
10358 return -ENOMEM;
10359
10360 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10361 do {
10362 u16 num_reported, num_total;
10363
10364 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10365 I40E_AQ_LARGE_BUF,
10366 &next_seid, NULL);
10367 if (ret) {
10368 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010369 "get switch config failed err %s aq_err %s\n",
10370 i40e_stat_str(&pf->hw, ret),
10371 i40e_aq_str(&pf->hw,
10372 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010373 kfree(aq_buf);
10374 return -ENOENT;
10375 }
10376
10377 num_reported = le16_to_cpu(sw_config->header.num_reported);
10378 num_total = le16_to_cpu(sw_config->header.num_total);
10379
10380 if (printconfig)
10381 dev_info(&pf->pdev->dev,
10382 "header: %d reported %d total\n",
10383 num_reported, num_total);
10384
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010385 for (i = 0; i < num_reported; i++) {
10386 struct i40e_aqc_switch_config_element_resp *ele =
10387 &sw_config->element[i];
10388
10389 i40e_setup_pf_switch_element(pf, ele, num_reported,
10390 printconfig);
10391 }
10392 } while (next_seid != 0);
10393
10394 kfree(aq_buf);
10395 return ret;
10396}
10397
10398/**
10399 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10400 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010401 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010402 *
10403 * Returns 0 on success, negative value on failure
10404 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010405static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010406{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010407 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010408 int ret;
10409
10410 /* find out what's out there already */
10411 ret = i40e_fetch_switch_configuration(pf, false);
10412 if (ret) {
10413 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010414 "couldn't fetch switch config, err %s aq_err %s\n",
10415 i40e_stat_str(&pf->hw, ret),
10416 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010417 return ret;
10418 }
10419 i40e_pf_reset_stats(pf);
10420
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010421 /* set the switch config bit for the whole device to
10422 * support limited promisc or true promisc
10423 * when user requests promisc. The default is limited
10424 * promisc.
10425 */
10426
10427 if ((pf->hw.pf_id == 0) &&
10428 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10429 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10430
10431 if (pf->hw.pf_id == 0) {
10432 u16 valid_flags;
10433
10434 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10435 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10436 NULL);
10437 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10438 dev_info(&pf->pdev->dev,
10439 "couldn't set switch config bits, err %s aq_err %s\n",
10440 i40e_stat_str(&pf->hw, ret),
10441 i40e_aq_str(&pf->hw,
10442 pf->hw.aq.asq_last_status));
10443 /* not a fatal problem, just keep going */
10444 }
10445 }
10446
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010447 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010448 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010449 struct i40e_vsi *vsi = NULL;
10450 u16 uplink_seid;
10451
10452 /* Set up the PF VSI associated with the PF's main VSI
10453 * that is already in the HW switch
10454 */
10455 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10456 uplink_seid = pf->veb[pf->lan_veb]->seid;
10457 else
10458 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010459 if (pf->lan_vsi == I40E_NO_VSI)
10460 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10461 else if (reinit)
10462 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010463 if (!vsi) {
10464 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10465 i40e_fdir_teardown(pf);
10466 return -EAGAIN;
10467 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010468 } else {
10469 /* force a reset of TC and queue layout configurations */
10470 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010471
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010472 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10473 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10474 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10475 }
10476 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10477
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010478 i40e_fdir_sb_setup(pf);
10479
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010480 /* Setup static PF queue filter control settings */
10481 ret = i40e_setup_pf_filter_control(pf);
10482 if (ret) {
10483 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10484 ret);
10485 /* Failure here should not stop continuing other steps */
10486 }
10487
10488 /* enable RSS in the HW, even for only one queue, as the stack can use
10489 * the hash
10490 */
10491 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010492 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010493
10494 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010495 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010496 i40e_link_event(pf);
10497
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010498 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010499 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10500 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010501
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010502 i40e_ptp_init(pf);
10503
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010504 return ret;
10505}
10506
10507/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010508 * i40e_determine_queue_usage - Work out queue distribution
10509 * @pf: board private structure
10510 **/
10511static void i40e_determine_queue_usage(struct i40e_pf *pf)
10512{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010513 int queues_left;
10514
10515 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010516#ifdef I40E_FCOE
10517 pf->num_fcoe_qps = 0;
10518#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010519
10520 /* Find the max queues to be put into basic use. We'll always be
10521 * using TC0, whether or not DCB is running, and TC0 will get the
10522 * big RSS set.
10523 */
10524 queues_left = pf->hw.func_caps.num_tx_qp;
10525
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010526 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010527 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010528 /* one qp for PF, no queues for anything else */
10529 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010530 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010531
10532 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010533 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010534 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010535#ifdef I40E_FCOE
10536 I40E_FLAG_FCOE_ENABLED |
10537#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010538 I40E_FLAG_FD_SB_ENABLED |
10539 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010540 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010541 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010542 I40E_FLAG_SRIOV_ENABLED |
10543 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010544 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10545 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010546 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010547 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010548 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010549 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010550 queues_left -= pf->num_lan_qps;
10551
10552 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010553 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010554#ifdef I40E_FCOE
10555 I40E_FLAG_FCOE_ENABLED |
10556#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010557 I40E_FLAG_FD_SB_ENABLED |
10558 I40E_FLAG_FD_ATR_ENABLED |
10559 I40E_FLAG_DCB_ENABLED |
10560 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010561 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010562 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010563 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010564 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010565 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10566 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010567 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10568 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010569 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10570 num_online_cpus());
10571 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10572 pf->hw.func_caps.num_tx_qp);
10573
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010574 queues_left -= pf->num_lan_qps;
10575 }
10576
Vasu Dev38e00432014-08-01 13:27:03 -070010577#ifdef I40E_FCOE
10578 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10579 if (I40E_DEFAULT_FCOE <= queues_left) {
10580 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10581 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10582 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10583 } else {
10584 pf->num_fcoe_qps = 0;
10585 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10586 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10587 }
10588
10589 queues_left -= pf->num_fcoe_qps;
10590 }
10591
10592#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010593 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10594 if (queues_left > 1) {
10595 queues_left -= 1; /* save 1 queue for FD */
10596 } else {
10597 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10598 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10599 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010600 }
10601
10602 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10603 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010604 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10605 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010606 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10607 }
10608
10609 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10610 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10611 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10612 (queues_left / pf->num_vmdq_qps));
10613 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10614 }
10615
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010616 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010617 dev_dbg(&pf->pdev->dev,
10618 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10619 pf->hw.func_caps.num_tx_qp,
10620 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010621 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10622 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10623 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010624#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010625 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010626#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010627}
10628
10629/**
10630 * i40e_setup_pf_filter_control - Setup PF static filter control
10631 * @pf: PF to be setup
10632 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010633 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010634 * settings. If PE/FCoE are enabled then it will also set the per PF
10635 * based filter sizes required for them. It also enables Flow director,
10636 * ethertype and macvlan type filter settings for the pf.
10637 *
10638 * Returns 0 on success, negative on failure
10639 **/
10640static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10641{
10642 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10643
10644 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10645
10646 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010647 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010648 settings->enable_fdir = true;
10649
10650 /* Ethtype and MACVLAN filters enabled for PF */
10651 settings->enable_ethtype = true;
10652 settings->enable_macvlan = true;
10653
10654 if (i40e_set_filter_control(&pf->hw, settings))
10655 return -ENOENT;
10656
10657 return 0;
10658}
10659
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010660#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010661#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010662static void i40e_print_features(struct i40e_pf *pf)
10663{
10664 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010665 char *buf;
10666 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010667
Joe Perches3b195842015-12-03 04:20:57 -080010668 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10669 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010670 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010671
Joe Perches3b195842015-12-03 04:20:57 -080010672 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010673#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010674 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010675#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010676 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010677 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010678 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010679 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010680 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010681 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010682 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010683 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010684 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10685 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010686 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010687 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010688 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010689 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010690 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010691 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010692 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010693#ifdef I40E_FCOE
10694 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010695 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010696#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010697 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010698 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010699 else
Joe Perches3b195842015-12-03 04:20:57 -080010700 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010701
Joe Perches3b195842015-12-03 04:20:57 -080010702 dev_info(&pf->pdev->dev, "%s\n", buf);
10703 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010704 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010705}
10706
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010707/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010708 * i40e_get_platform_mac_addr - get platform-specific MAC address
10709 *
10710 * @pdev: PCI device information struct
10711 * @pf: board private structure
10712 *
10713 * Look up the MAC address in Open Firmware on systems that support it,
10714 * and use IDPROM on SPARC if no OF address is found. On return, the
10715 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10716 * has been selected.
10717 **/
10718static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10719{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010720 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010721 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010722 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010723}
10724
10725/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010726 * i40e_probe - Device initialization routine
10727 * @pdev: PCI device information struct
10728 * @ent: entry in i40e_pci_tbl
10729 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010730 * i40e_probe initializes a PF identified by a pci_dev structure.
10731 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010732 * and a hardware reset occur.
10733 *
10734 * Returns 0 on success, negative on failure
10735 **/
10736static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10737{
Catherine Sullivane8278452015-02-06 08:52:08 +000010738 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010739 struct i40e_pf *pf;
10740 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010741 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010742 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010743 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010744 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010745 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010746 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010747 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010748
10749 err = pci_enable_device_mem(pdev);
10750 if (err)
10751 return err;
10752
10753 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010754 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010755 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010756 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10757 if (err) {
10758 dev_err(&pdev->dev,
10759 "DMA configuration failed: 0x%x\n", err);
10760 goto err_dma;
10761 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010762 }
10763
10764 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010765 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010766 if (err) {
10767 dev_info(&pdev->dev,
10768 "pci_request_selected_regions failed %d\n", err);
10769 goto err_pci_reg;
10770 }
10771
10772 pci_enable_pcie_error_reporting(pdev);
10773 pci_set_master(pdev);
10774
10775 /* Now that we have a PCI connection, we need to do the
10776 * low level device setup. This is primarily setting up
10777 * the Admin Queue structures and then querying for the
10778 * device's current profile information.
10779 */
10780 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10781 if (!pf) {
10782 err = -ENOMEM;
10783 goto err_pf_alloc;
10784 }
10785 pf->next_vsi = 0;
10786 pf->pdev = pdev;
10787 set_bit(__I40E_DOWN, &pf->state);
10788
10789 hw = &pf->hw;
10790 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010791
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010792 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10793 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010794
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010795 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010796 if (!hw->hw_addr) {
10797 err = -EIO;
10798 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10799 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010800 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010801 goto err_ioremap;
10802 }
10803 hw->vendor_id = pdev->vendor;
10804 hw->device_id = pdev->device;
10805 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10806 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10807 hw->subsystem_device_id = pdev->subsystem_device;
10808 hw->bus.device = PCI_SLOT(pdev->devfn);
10809 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010810 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010811
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010812 /* set up the locks for the AQ, do this only once in probe
10813 * and destroy them only once in remove
10814 */
10815 mutex_init(&hw->aq.asq_mutex);
10816 mutex_init(&hw->aq.arq_mutex);
10817
Alexander Duyck5d4ca232016-09-30 08:21:46 -040010818 pf->msg_enable = netif_msg_init(debug,
10819 NETIF_MSG_DRV |
10820 NETIF_MSG_PROBE |
10821 NETIF_MSG_LINK);
10822 if (debug < -1)
10823 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010824
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010825 /* do a special CORER for clearing PXE mode once at init */
10826 if (hw->revision_id == 0 &&
10827 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10828 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10829 i40e_flush(hw);
10830 msleep(200);
10831 pf->corer_count++;
10832
10833 i40e_clear_pxe_mode(hw);
10834 }
10835
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010836 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010837 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010838 err = i40e_pf_reset(hw);
10839 if (err) {
10840 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10841 goto err_pf_reset;
10842 }
10843 pf->pfr_count++;
10844
10845 hw->aq.num_arq_entries = I40E_AQ_LEN;
10846 hw->aq.num_asq_entries = I40E_AQ_LEN;
10847 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10848 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10849 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010850
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010851 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010852 "%s-%s:misc",
10853 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010854
10855 err = i40e_init_shared_code(hw);
10856 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010857 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10858 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010859 goto err_pf_reset;
10860 }
10861
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010862 /* set up a default setting for link flow control */
10863 pf->hw.fc.requested_mode = I40E_FC_NONE;
10864
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010865 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040010866 if (err) {
10867 if (err == I40E_ERR_FIRMWARE_API_VERSION)
10868 dev_info(&pdev->dev,
10869 "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");
10870 else
10871 dev_info(&pdev->dev,
10872 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
10873
10874 goto err_pf_reset;
10875 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010876
Shannon Nelson6dec1012015-09-28 14:12:30 -040010877 /* provide nvm, fw, api versions */
10878 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
10879 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
10880 hw->aq.api_maj_ver, hw->aq.api_min_ver,
10881 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010882
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010883 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10884 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010885 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010886 "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");
10887 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
10888 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000010889 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010890 "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 +000010891
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010892 i40e_verify_eeprom(pf);
10893
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000010894 /* Rev 0 hardware was never productized */
10895 if (hw->revision_id < 1)
10896 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");
10897
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000010898 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010899 err = i40e_get_capabilities(pf);
10900 if (err)
10901 goto err_adminq_setup;
10902
10903 err = i40e_sw_init(pf);
10904 if (err) {
10905 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
10906 goto err_sw_init;
10907 }
10908
10909 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10910 hw->func_caps.num_rx_qp,
10911 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
10912 if (err) {
10913 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
10914 goto err_init_lan_hmc;
10915 }
10916
10917 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10918 if (err) {
10919 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
10920 err = -ENOENT;
10921 goto err_configure_lan_hmc;
10922 }
10923
Neerav Parikhb686ece2014-12-14 01:55:11 +000010924 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
10925 * Ignore error return codes because if it was already disabled via
10926 * hardware settings this will fail
10927 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080010928 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000010929 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
10930 i40e_aq_stop_lldp(hw, true, NULL);
10931 }
10932
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010933 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010934 /* allow a platform config to override the HW addr */
10935 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000010936 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010937 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
10938 err = -EIO;
10939 goto err_mac_addr;
10940 }
10941 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000010942 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010943 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
10944 if (is_valid_ether_addr(hw->mac.port_addr))
10945 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070010946#ifdef I40E_FCOE
10947 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
10948 if (err)
10949 dev_info(&pdev->dev,
10950 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
10951 if (!is_valid_ether_addr(hw->mac.san_addr)) {
10952 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
10953 hw->mac.san_addr);
10954 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
10955 }
10956 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
10957#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010958
10959 pci_set_drvdata(pdev, pf);
10960 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010961#ifdef CONFIG_I40E_DCB
10962 err = i40e_init_pf_dcb(pf);
10963 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000010964 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070010965 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000010966 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010967 }
10968#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010969
10970 /* set up periodic task facility */
10971 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
10972 pf->service_timer_period = HZ;
10973
10974 INIT_WORK(&pf->service_task, i40e_service_task);
10975 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
10976 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010977
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010978 /* NVM bit on means WoL disabled for the port */
10979 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080010980 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010981 pf->wol_en = false;
10982 else
10983 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010984 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
10985
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010986 /* set up the main switch operations */
10987 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010988 err = i40e_init_interrupt_scheme(pf);
10989 if (err)
10990 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010991
Mitch Williams505682c2014-05-20 08:01:37 +000010992 /* The number of VSIs reported by the FW is the minimum guaranteed
10993 * to us; HW supports far more and we share the remaining pool with
10994 * the other PFs. We allocate space for more than the guarantee with
10995 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010996 */
Mitch Williams505682c2014-05-20 08:01:37 +000010997 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
10998 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
10999 else
11000 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11001
11002 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011003 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11004 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011005 if (!pf->vsi) {
11006 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011007 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011008 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011009
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011010#ifdef CONFIG_PCI_IOV
11011 /* prep for VF support */
11012 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11013 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11014 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11015 if (pci_num_vf(pdev))
11016 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11017 }
11018#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011019 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011020 if (err) {
11021 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11022 goto err_vsis;
11023 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011024
11025 /* Make sure flow control is set according to current settings */
11026 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11027 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11028 dev_dbg(&pf->pdev->dev,
11029 "Set fc with err %s aq_err %s on get_phy_cap\n",
11030 i40e_stat_str(hw, err),
11031 i40e_aq_str(hw, hw->aq.asq_last_status));
11032 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11033 dev_dbg(&pf->pdev->dev,
11034 "Set fc with err %s aq_err %s on set_phy_config\n",
11035 i40e_stat_str(hw, err),
11036 i40e_aq_str(hw, hw->aq.asq_last_status));
11037 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11038 dev_dbg(&pf->pdev->dev,
11039 "Set fc with err %s aq_err %s on get_link_info\n",
11040 i40e_stat_str(hw, err),
11041 i40e_aq_str(hw, hw->aq.asq_last_status));
11042
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011043 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011044 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011045 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11046 i40e_vsi_open(pf->vsi[i]);
11047 break;
11048 }
11049 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011050
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011051 /* The driver only wants link up/down and module qualification
11052 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011053 */
11054 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011055 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011056 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011057 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011058 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011059 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11060 i40e_stat_str(&pf->hw, err),
11061 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011062
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011063 /* Reconfigure hardware for allowing smaller MSS in the case
11064 * of TSO, so that we avoid the MDD being fired and causing
11065 * a reset in the case of small MSS+TSO.
11066 */
11067 val = rd32(hw, I40E_REG_MSS);
11068 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11069 val &= ~I40E_REG_MSS_MIN_MASK;
11070 val |= I40E_64BYTE_MSS;
11071 wr32(hw, I40E_REG_MSS, val);
11072 }
11073
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011074 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011075 msleep(75);
11076 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11077 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011078 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11079 i40e_stat_str(&pf->hw, err),
11080 i40e_aq_str(&pf->hw,
11081 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011082 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011083 /* The main driver is (mostly) up and happy. We need to set this state
11084 * before setting up the misc vector or we get a race and the vector
11085 * ends up disabled forever.
11086 */
11087 clear_bit(__I40E_DOWN, &pf->state);
11088
11089 /* In case of MSIX we are going to setup the misc vector right here
11090 * to handle admin queue events etc. In case of legacy and MSI
11091 * the misc functionality and queue processing is combined in
11092 * the same vector and that gets setup at open.
11093 */
11094 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11095 err = i40e_setup_misc_vector(pf);
11096 if (err) {
11097 dev_info(&pdev->dev,
11098 "setup of misc vector failed: %d\n", err);
11099 goto err_vsis;
11100 }
11101 }
11102
Greg Rosedf805f62014-04-04 04:43:16 +000011103#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011104 /* prep for VF support */
11105 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011106 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11107 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011108 /* disable link interrupts for VFs */
11109 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11110 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11111 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11112 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011113
11114 if (pci_num_vf(pdev)) {
11115 dev_info(&pdev->dev,
11116 "Active VFs found, allocating resources.\n");
11117 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11118 if (err)
11119 dev_info(&pdev->dev,
11120 "Error %d allocating resources for existing VFs\n",
11121 err);
11122 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011123 }
Greg Rosedf805f62014-04-04 04:43:16 +000011124#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011125
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011126 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11127 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11128 pf->num_iwarp_msix,
11129 I40E_IWARP_IRQ_PILE_ID);
11130 if (pf->iwarp_base_vector < 0) {
11131 dev_info(&pdev->dev,
11132 "failed to get tracking for %d vectors for IWARP err=%d\n",
11133 pf->num_iwarp_msix, pf->iwarp_base_vector);
11134 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11135 }
11136 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011137
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011138 i40e_dbg_pf_init(pf);
11139
11140 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011141 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011142
11143 /* since everything's happy, start the service_task timer */
11144 mod_timer(&pf->service_timer,
11145 round_jiffies(jiffies + pf->service_timer_period));
11146
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011147 /* add this PF to client device list and launch a client service task */
11148 err = i40e_lan_add_device(pf);
11149 if (err)
11150 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11151 err);
11152
Vasu Dev38e00432014-08-01 13:27:03 -070011153#ifdef I40E_FCOE
11154 /* create FCoE interface */
11155 i40e_fcoe_vsi_setup(pf);
11156
11157#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011158#define PCI_SPEED_SIZE 8
11159#define PCI_WIDTH_SIZE 8
11160 /* Devices on the IOSF bus do not have this information
11161 * and will report PCI Gen 1 x 1 by default so don't bother
11162 * checking them.
11163 */
11164 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11165 char speed[PCI_SPEED_SIZE] = "Unknown";
11166 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011167
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011168 /* Get the negotiated link width and speed from PCI config
11169 * space
11170 */
11171 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11172 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011173
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011174 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011175
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011176 switch (hw->bus.speed) {
11177 case i40e_bus_speed_8000:
11178 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11179 case i40e_bus_speed_5000:
11180 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11181 case i40e_bus_speed_2500:
11182 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11183 default:
11184 break;
11185 }
11186 switch (hw->bus.width) {
11187 case i40e_bus_width_pcie_x8:
11188 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11189 case i40e_bus_width_pcie_x4:
11190 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11191 case i40e_bus_width_pcie_x2:
11192 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11193 case i40e_bus_width_pcie_x1:
11194 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11195 default:
11196 break;
11197 }
11198
11199 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11200 speed, width);
11201
11202 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11203 hw->bus.speed < i40e_bus_speed_8000) {
11204 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11205 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11206 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011207 }
11208
Catherine Sullivane8278452015-02-06 08:52:08 +000011209 /* get the requested speeds from the fw */
11210 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11211 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011212 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11213 i40e_stat_str(&pf->hw, err),
11214 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011215 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11216
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011217 /* get the supported phy types from the fw */
11218 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11219 if (err)
11220 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11221 i40e_stat_str(&pf->hw, err),
11222 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11223 pf->hw.phy.phy_types = le32_to_cpu(abilities.phy_type);
11224
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011225 /* Add a filter to drop all Flow control frames from any VSI from being
11226 * transmitted. By doing so we stop a malicious VF from sending out
11227 * PAUSE or PFC frames and potentially controlling traffic for other
11228 * PF/VF VSIs.
11229 * The FW can still send Flow control frames if enabled.
11230 */
11231 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11232 pf->main_vsi_seid);
11233
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011234 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
11235 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11236 pf->flags |= I40E_FLAG_HAVE_10GBASET_PHY;
11237
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011238 /* print a string summarizing features */
11239 i40e_print_features(pf);
11240
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011241 return 0;
11242
11243 /* Unwind what we've done if something failed in the setup */
11244err_vsis:
11245 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011246 i40e_clear_interrupt_scheme(pf);
11247 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011248err_switch_setup:
11249 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011250 del_timer_sync(&pf->service_timer);
11251err_mac_addr:
11252err_configure_lan_hmc:
11253 (void)i40e_shutdown_lan_hmc(hw);
11254err_init_lan_hmc:
11255 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011256err_sw_init:
11257err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011258err_pf_reset:
11259 iounmap(hw->hw_addr);
11260err_ioremap:
11261 kfree(pf);
11262err_pf_alloc:
11263 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011264 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011265err_pci_reg:
11266err_dma:
11267 pci_disable_device(pdev);
11268 return err;
11269}
11270
11271/**
11272 * i40e_remove - Device removal routine
11273 * @pdev: PCI device information struct
11274 *
11275 * i40e_remove is called by the PCI subsystem to alert the driver
11276 * that is should release a PCI device. This could be caused by a
11277 * Hot-Plug event, or because the driver is going to be removed from
11278 * memory.
11279 **/
11280static void i40e_remove(struct pci_dev *pdev)
11281{
11282 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011283 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011284 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011285 int i;
11286
11287 i40e_dbg_pf_exit(pf);
11288
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011289 i40e_ptp_stop(pf);
11290
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011291 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011292 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11293 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011294
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011295 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011296 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011297 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011298 if (pf->service_timer.data)
11299 del_timer_sync(&pf->service_timer);
11300 if (pf->service_task.func)
11301 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011302
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011303 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11304 i40e_free_vfs(pf);
11305 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11306 }
11307
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011308 i40e_fdir_teardown(pf);
11309
11310 /* If there is a switch structure or any orphans, remove them.
11311 * This will leave only the PF's VSI remaining.
11312 */
11313 for (i = 0; i < I40E_MAX_VEB; i++) {
11314 if (!pf->veb[i])
11315 continue;
11316
11317 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11318 pf->veb[i]->uplink_seid == 0)
11319 i40e_switch_branch_release(pf->veb[i]);
11320 }
11321
11322 /* Now we can shutdown the PF's VSI, just before we kill
11323 * adminq and hmc.
11324 */
11325 if (pf->vsi[pf->lan_vsi])
11326 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11327
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011328 /* remove attached clients */
11329 ret_code = i40e_lan_del_device(pf);
11330 if (ret_code) {
11331 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11332 ret_code);
11333 }
11334
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011335 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011336 if (hw->hmc.hmc_obj) {
11337 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011338 if (ret_code)
11339 dev_warn(&pdev->dev,
11340 "Failed to destroy the HMC resources: %d\n",
11341 ret_code);
11342 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011343
11344 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011345 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011346
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011347 /* destroy the locks only once, here */
11348 mutex_destroy(&hw->aq.arq_mutex);
11349 mutex_destroy(&hw->aq.asq_mutex);
11350
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011351 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11352 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011353 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011354 if (pf->vsi[i]) {
11355 i40e_vsi_clear_rings(pf->vsi[i]);
11356 i40e_vsi_clear(pf->vsi[i]);
11357 pf->vsi[i] = NULL;
11358 }
11359 }
11360
11361 for (i = 0; i < I40E_MAX_VEB; i++) {
11362 kfree(pf->veb[i]);
11363 pf->veb[i] = NULL;
11364 }
11365
11366 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011367 kfree(pf->vsi);
11368
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011369 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011370 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011371 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011372
11373 pci_disable_pcie_error_reporting(pdev);
11374 pci_disable_device(pdev);
11375}
11376
11377/**
11378 * i40e_pci_error_detected - warning that something funky happened in PCI land
11379 * @pdev: PCI device information struct
11380 *
11381 * Called to warn that something happened and the error handling steps
11382 * are in progress. Allows the driver to quiesce things, be ready for
11383 * remediation.
11384 **/
11385static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11386 enum pci_channel_state error)
11387{
11388 struct i40e_pf *pf = pci_get_drvdata(pdev);
11389
11390 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11391
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011392 if (!pf) {
11393 dev_info(&pdev->dev,
11394 "Cannot recover - error happened during device probe\n");
11395 return PCI_ERS_RESULT_DISCONNECT;
11396 }
11397
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011398 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011399 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11400 rtnl_lock();
11401 i40e_prep_for_reset(pf);
11402 rtnl_unlock();
11403 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011404
11405 /* Request a slot reset */
11406 return PCI_ERS_RESULT_NEED_RESET;
11407}
11408
11409/**
11410 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11411 * @pdev: PCI device information struct
11412 *
11413 * Called to find if the driver can work with the device now that
11414 * the pci slot has been reset. If a basic connection seems good
11415 * (registers are readable and have sane content) then return a
11416 * happy little PCI_ERS_RESULT_xxx.
11417 **/
11418static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11419{
11420 struct i40e_pf *pf = pci_get_drvdata(pdev);
11421 pci_ers_result_t result;
11422 int err;
11423 u32 reg;
11424
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011425 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011426 if (pci_enable_device_mem(pdev)) {
11427 dev_info(&pdev->dev,
11428 "Cannot re-enable PCI device after reset.\n");
11429 result = PCI_ERS_RESULT_DISCONNECT;
11430 } else {
11431 pci_set_master(pdev);
11432 pci_restore_state(pdev);
11433 pci_save_state(pdev);
11434 pci_wake_from_d3(pdev, false);
11435
11436 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11437 if (reg == 0)
11438 result = PCI_ERS_RESULT_RECOVERED;
11439 else
11440 result = PCI_ERS_RESULT_DISCONNECT;
11441 }
11442
11443 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11444 if (err) {
11445 dev_info(&pdev->dev,
11446 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11447 err);
11448 /* non-fatal, continue */
11449 }
11450
11451 return result;
11452}
11453
11454/**
11455 * i40e_pci_error_resume - restart operations after PCI error recovery
11456 * @pdev: PCI device information struct
11457 *
11458 * Called to allow the driver to bring things back up after PCI error
11459 * and/or reset recovery has finished.
11460 **/
11461static void i40e_pci_error_resume(struct pci_dev *pdev)
11462{
11463 struct i40e_pf *pf = pci_get_drvdata(pdev);
11464
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011465 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011466 if (test_bit(__I40E_SUSPENDED, &pf->state))
11467 return;
11468
11469 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011470 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011471 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011472}
11473
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011474/**
11475 * i40e_shutdown - PCI callback for shutting down
11476 * @pdev: PCI device information struct
11477 **/
11478static void i40e_shutdown(struct pci_dev *pdev)
11479{
11480 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011481 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011482
11483 set_bit(__I40E_SUSPENDED, &pf->state);
11484 set_bit(__I40E_DOWN, &pf->state);
11485 rtnl_lock();
11486 i40e_prep_for_reset(pf);
11487 rtnl_unlock();
11488
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011489 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11490 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11491
Catherine Sullivan02b42492015-07-10 19:35:59 -040011492 del_timer_sync(&pf->service_timer);
11493 cancel_work_sync(&pf->service_task);
11494 i40e_fdir_teardown(pf);
11495
11496 rtnl_lock();
11497 i40e_prep_for_reset(pf);
11498 rtnl_unlock();
11499
11500 wr32(hw, I40E_PFPM_APM,
11501 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11502 wr32(hw, I40E_PFPM_WUFC,
11503 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11504
Shannon Nelsone1477582015-02-21 06:44:33 +000011505 i40e_clear_interrupt_scheme(pf);
11506
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011507 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011508 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011509 pci_set_power_state(pdev, PCI_D3hot);
11510 }
11511}
11512
11513#ifdef CONFIG_PM
11514/**
11515 * i40e_suspend - PCI callback for moving to D3
11516 * @pdev: PCI device information struct
11517 **/
11518static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11519{
11520 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011521 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011522 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011523
11524 set_bit(__I40E_SUSPENDED, &pf->state);
11525 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011526
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011527 rtnl_lock();
11528 i40e_prep_for_reset(pf);
11529 rtnl_unlock();
11530
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011531 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11532 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11533
Greg Roseb33d3b72016-05-16 10:26:37 -070011534 i40e_stop_misc_vector(pf);
11535
Greg Rose059ff692016-05-16 10:26:38 -070011536 retval = pci_save_state(pdev);
11537 if (retval)
11538 return retval;
11539
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011540 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011541 pci_set_power_state(pdev, PCI_D3hot);
11542
Greg Rose059ff692016-05-16 10:26:38 -070011543 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011544}
11545
11546/**
11547 * i40e_resume - PCI callback for waking up from D3
11548 * @pdev: PCI device information struct
11549 **/
11550static int i40e_resume(struct pci_dev *pdev)
11551{
11552 struct i40e_pf *pf = pci_get_drvdata(pdev);
11553 u32 err;
11554
11555 pci_set_power_state(pdev, PCI_D0);
11556 pci_restore_state(pdev);
11557 /* pci_restore_state() clears dev->state_saves, so
11558 * call pci_save_state() again to restore it.
11559 */
11560 pci_save_state(pdev);
11561
11562 err = pci_enable_device_mem(pdev);
11563 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011564 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011565 return err;
11566 }
11567 pci_set_master(pdev);
11568
11569 /* no wakeup events while running */
11570 pci_wake_from_d3(pdev, false);
11571
11572 /* handling the reset will rebuild the device state */
11573 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11574 clear_bit(__I40E_DOWN, &pf->state);
11575 rtnl_lock();
11576 i40e_reset_and_rebuild(pf, false);
11577 rtnl_unlock();
11578 }
11579
11580 return 0;
11581}
11582
11583#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011584static const struct pci_error_handlers i40e_err_handler = {
11585 .error_detected = i40e_pci_error_detected,
11586 .slot_reset = i40e_pci_error_slot_reset,
11587 .resume = i40e_pci_error_resume,
11588};
11589
11590static struct pci_driver i40e_driver = {
11591 .name = i40e_driver_name,
11592 .id_table = i40e_pci_tbl,
11593 .probe = i40e_probe,
11594 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011595#ifdef CONFIG_PM
11596 .suspend = i40e_suspend,
11597 .resume = i40e_resume,
11598#endif
11599 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011600 .err_handler = &i40e_err_handler,
11601 .sriov_configure = i40e_pci_sriov_configure,
11602};
11603
11604/**
11605 * i40e_init_module - Driver registration routine
11606 *
11607 * i40e_init_module is the first routine called when the driver is
11608 * loaded. All it does is register with the PCI subsystem.
11609 **/
11610static int __init i40e_init_module(void)
11611{
11612 pr_info("%s: %s - version %s\n", i40e_driver_name,
11613 i40e_driver_string, i40e_driver_version_str);
11614 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011615
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011616 /* we will see if single thread per module is enough for now,
11617 * it can't be any worse than using the system workqueue which
11618 * was already single threaded
11619 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011620 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11621 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011622 if (!i40e_wq) {
11623 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11624 return -ENOMEM;
11625 }
11626
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011627 i40e_dbg_init();
11628 return pci_register_driver(&i40e_driver);
11629}
11630module_init(i40e_init_module);
11631
11632/**
11633 * i40e_exit_module - Driver exit cleanup routine
11634 *
11635 * i40e_exit_module is called just before the driver is removed
11636 * from memory.
11637 **/
11638static void __exit i40e_exit_module(void)
11639{
11640 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011641 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011642 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011643}
11644module_exit(i40e_exit_module);