blob: 10662f691312f83cb74cc7bd1f7cdde386cdcc10 [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Catherine Sullivane8278452015-02-06 08:52:08 +00004 * Copyright(c) 2013 - 2015 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
27/* Local includes */
28#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000029#include "i40e_diag.h"
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000030#ifdef CONFIG_I40E_VXLAN
31#include <net/vxlan.h>
32#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000033
34const char i40e_driver_name[] = "i40e";
35static const char i40e_driver_string[] =
36 "Intel(R) Ethernet Connection XL710 Network Driver";
37
38#define DRV_KERN "-k"
39
Catherine Sullivane8e724d2014-07-10 07:58:26 +000040#define DRV_VERSION_MAJOR 1
Catherine Sullivan42d255c2015-03-31 00:45:06 -070041#define DRV_VERSION_MINOR 3
Catherine Sullivan06a24dd2015-08-31 19:54:55 -040042#define DRV_VERSION_BUILD 25
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000043#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
44 __stringify(DRV_VERSION_MINOR) "." \
45 __stringify(DRV_VERSION_BUILD) DRV_KERN
46const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080047static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000048
49/* a bit of forward declarations */
50static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
51static void i40e_handle_reset_warning(struct i40e_pf *pf);
52static int i40e_add_vsi(struct i40e_vsi *vsi);
53static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000054static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000055static int i40e_setup_misc_vector(struct i40e_pf *pf);
56static void i40e_determine_queue_usage(struct i40e_pf *pf);
57static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080058static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080059static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000060
61/* i40e_pci_tbl - PCI Device ID Table
62 *
63 * Last entry must be all 0s
64 *
65 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
66 * Class, Class Mask, private data (not used) }
67 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020068static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080069 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080070 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
71 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_A), 0},
72 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
73 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080074 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
75 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
76 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000077 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040078 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Jesse Brandeburgae24b402015-03-27 00:12:09 -070079 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040080 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
81 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
82 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040083 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000085 /* required last entry */
86 {0, }
87};
88MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
89
90#define I40E_MAX_VF_COUNT 128
91static int debug = -1;
92module_param(debug, int, 0);
93MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
94
95MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
96MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
97MODULE_LICENSE("GPL");
98MODULE_VERSION(DRV_VERSION);
99
100/**
101 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
102 * @hw: pointer to the HW structure
103 * @mem: ptr to mem struct to fill out
104 * @size: size of memory requested
105 * @alignment: what to align the allocation to
106 **/
107int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
108 u64 size, u32 alignment)
109{
110 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
111
112 mem->size = ALIGN(size, alignment);
113 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
114 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000115 if (!mem->va)
116 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000117
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000118 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000119}
120
121/**
122 * i40e_free_dma_mem_d - OS specific memory free for shared code
123 * @hw: pointer to the HW structure
124 * @mem: ptr to mem struct to free
125 **/
126int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
127{
128 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
129
130 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
131 mem->va = NULL;
132 mem->pa = 0;
133 mem->size = 0;
134
135 return 0;
136}
137
138/**
139 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
140 * @hw: pointer to the HW structure
141 * @mem: ptr to mem struct to fill out
142 * @size: size of memory requested
143 **/
144int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
145 u32 size)
146{
147 mem->size = size;
148 mem->va = kzalloc(size, GFP_KERNEL);
149
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000150 if (!mem->va)
151 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000152
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000153 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000154}
155
156/**
157 * i40e_free_virt_mem_d - OS specific memory free for shared code
158 * @hw: pointer to the HW structure
159 * @mem: ptr to mem struct to free
160 **/
161int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
162{
163 /* it's ok to kfree a NULL pointer */
164 kfree(mem->va);
165 mem->va = NULL;
166 mem->size = 0;
167
168 return 0;
169}
170
171/**
172 * i40e_get_lump - find a lump of free generic resource
173 * @pf: board private structure
174 * @pile: the pile of resource to search
175 * @needed: the number of items needed
176 * @id: an owner id to stick on the items assigned
177 *
178 * Returns the base item index of the lump, or negative for error
179 *
180 * The search_hint trick and lack of advanced fit-finding only work
181 * because we're highly likely to have all the same size lump requests.
182 * Linear search time and any fragmentation should be minimal.
183 **/
184static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
185 u16 needed, u16 id)
186{
187 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000188 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000189
190 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
191 dev_info(&pf->pdev->dev,
192 "param err: pile=%p needed=%d id=0x%04x\n",
193 pile, needed, id);
194 return -EINVAL;
195 }
196
197 /* start the linear search with an imperfect hint */
198 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000199 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000200 /* skip already allocated entries */
201 if (pile->list[i] & I40E_PILE_VALID_BIT) {
202 i++;
203 continue;
204 }
205
206 /* do we have enough in this lump? */
207 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
208 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
209 break;
210 }
211
212 if (j == needed) {
213 /* there was enough, so assign it to the requestor */
214 for (j = 0; j < needed; j++)
215 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
216 ret = i;
217 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000218 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000219 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400220
221 /* not enough, so skip over it and continue looking */
222 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000223 }
224
225 return ret;
226}
227
228/**
229 * i40e_put_lump - return a lump of generic resource
230 * @pile: the pile of resource to search
231 * @index: the base item index
232 * @id: the owner id of the items assigned
233 *
234 * Returns the count of items in the lump
235 **/
236static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
237{
238 int valid_id = (id | I40E_PILE_VALID_BIT);
239 int count = 0;
240 int i;
241
242 if (!pile || index >= pile->num_entries)
243 return -EINVAL;
244
245 for (i = index;
246 i < pile->num_entries && pile->list[i] == valid_id;
247 i++) {
248 pile->list[i] = 0;
249 count++;
250 }
251
252 if (count && index < pile->search_hint)
253 pile->search_hint = index;
254
255 return count;
256}
257
258/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700259 * i40e_find_vsi_from_id - searches for the vsi with the given id
260 * @pf - the pf structure to search for the vsi
261 * @id - id of the vsi it is searching for
262 **/
263struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
264{
265 int i;
266
267 for (i = 0; i < pf->num_alloc_vsi; i++)
268 if (pf->vsi[i] && (pf->vsi[i]->id == id))
269 return pf->vsi[i];
270
271 return NULL;
272}
273
274/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000275 * i40e_service_event_schedule - Schedule the service task to wake up
276 * @pf: board private structure
277 *
278 * If not already scheduled, this puts the task into the work queue
279 **/
280static void i40e_service_event_schedule(struct i40e_pf *pf)
281{
282 if (!test_bit(__I40E_DOWN, &pf->state) &&
283 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
284 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
285 schedule_work(&pf->service_task);
286}
287
288/**
289 * i40e_tx_timeout - Respond to a Tx Hang
290 * @netdev: network interface device structure
291 *
292 * If any port has noticed a Tx timeout, it is likely that the whole
293 * device is munged, not just the one netdev port, so go for the full
294 * reset.
295 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700296#ifdef I40E_FCOE
297void i40e_tx_timeout(struct net_device *netdev)
298#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000299static void i40e_tx_timeout(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -0700300#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000301{
302 struct i40e_netdev_priv *np = netdev_priv(netdev);
303 struct i40e_vsi *vsi = np->vsi;
304 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400305 struct i40e_ring *tx_ring = NULL;
306 unsigned int i, hung_queue = 0;
307 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000308
309 pf->tx_timeout_count++;
310
Kiran Patilb03a8c12015-09-24 18:13:15 -0400311 /* find the stopped queue the same way the stack does */
312 for (i = 0; i < netdev->num_tx_queues; i++) {
313 struct netdev_queue *q;
314 unsigned long trans_start;
315
316 q = netdev_get_tx_queue(netdev, i);
317 trans_start = q->trans_start ? : netdev->trans_start;
318 if (netif_xmit_stopped(q) &&
319 time_after(jiffies,
320 (trans_start + netdev->watchdog_timeo))) {
321 hung_queue = i;
322 break;
323 }
324 }
325
326 if (i == netdev->num_tx_queues) {
327 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
328 } else {
329 /* now that we have an index, find the tx_ring struct */
330 for (i = 0; i < vsi->num_queue_pairs; i++) {
331 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
332 if (hung_queue ==
333 vsi->tx_rings[i]->queue_index) {
334 tx_ring = vsi->tx_rings[i];
335 break;
336 }
337 }
338 }
339 }
340
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000341 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400342 pf->tx_timeout_recovery_level = 1; /* reset after some time */
343 else if (time_before(jiffies,
344 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
345 return; /* don't do any new action before the next timeout */
346
347 if (tx_ring) {
348 head = i40e_get_head(tx_ring);
349 /* Read interrupt register */
350 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
351 val = rd32(&pf->hw,
352 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
353 tx_ring->vsi->base_vector - 1));
354 else
355 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
356
357 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",
358 vsi->seid, hung_queue, tx_ring->next_to_clean,
359 head, tx_ring->next_to_use,
360 readl(tx_ring->tail), val);
361 }
362
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000363 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400364 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
365 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000366
367 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000368 case 1:
369 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
370 break;
371 case 2:
372 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
373 break;
374 case 3:
375 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
376 break;
377 default:
378 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000379 break;
380 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400381
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000382 i40e_service_event_schedule(pf);
383 pf->tx_timeout_recovery_level++;
384}
385
386/**
387 * i40e_release_rx_desc - Store the new tail and head values
388 * @rx_ring: ring to bump
389 * @val: new head index
390 **/
391static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
392{
393 rx_ring->next_to_use = val;
394
395 /* Force memory writes to complete before letting h/w
396 * know there are new descriptors to fetch. (Only
397 * applicable for weak-ordered memory model archs,
398 * such as IA-64).
399 */
400 wmb();
401 writel(val, rx_ring->tail);
402}
403
404/**
405 * i40e_get_vsi_stats_struct - Get System Network Statistics
406 * @vsi: the VSI we care about
407 *
408 * Returns the address of the device statistics structure.
409 * The statistics are actually updated from the service task.
410 **/
411struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
412{
413 return &vsi->net_stats;
414}
415
416/**
417 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
418 * @netdev: network interface device structure
419 *
420 * Returns the address of the device statistics structure.
421 * The statistics are actually updated from the service task.
422 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700423#ifdef I40E_FCOE
424struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
425 struct net_device *netdev,
426 struct rtnl_link_stats64 *stats)
427#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000428static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
429 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000430 struct rtnl_link_stats64 *stats)
Vasu Dev38e00432014-08-01 13:27:03 -0700431#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000432{
433 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000434 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000435 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000436 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
437 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000438
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000439 if (test_bit(__I40E_DOWN, &vsi->state))
440 return stats;
441
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800442 if (!vsi->tx_rings)
443 return stats;
444
Alexander Duyck980e9b12013-09-28 06:01:03 +0000445 rcu_read_lock();
446 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000447 u64 bytes, packets;
448 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000449
Alexander Duyck980e9b12013-09-28 06:01:03 +0000450 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
451 if (!tx_ring)
452 continue;
453
454 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700455 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000456 packets = tx_ring->stats.packets;
457 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700458 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000459
460 stats->tx_packets += packets;
461 stats->tx_bytes += bytes;
462 rx_ring = &tx_ring[1];
463
464 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700465 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000466 packets = rx_ring->stats.packets;
467 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700468 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000469
470 stats->rx_packets += packets;
471 stats->rx_bytes += bytes;
472 }
473 rcu_read_unlock();
474
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000475 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000476 stats->multicast = vsi_stats->multicast;
477 stats->tx_errors = vsi_stats->tx_errors;
478 stats->tx_dropped = vsi_stats->tx_dropped;
479 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400480 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000481 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
482 stats->rx_length_errors = vsi_stats->rx_length_errors;
483
484 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000485}
486
487/**
488 * i40e_vsi_reset_stats - Resets all stats of the given vsi
489 * @vsi: the VSI to have its stats reset
490 **/
491void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
492{
493 struct rtnl_link_stats64 *ns;
494 int i;
495
496 if (!vsi)
497 return;
498
499 ns = i40e_get_vsi_stats_struct(vsi);
500 memset(ns, 0, sizeof(*ns));
501 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
502 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
503 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000504 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000505 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400506 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000507 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400508 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000509 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400510 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000511 sizeof(vsi->tx_rings[i]->stats));
512 memset(&vsi->tx_rings[i]->tx_stats, 0,
513 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000514 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000515 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000516 vsi->stat_offsets_loaded = false;
517}
518
519/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000520 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000521 * @pf: the PF to be reset
522 **/
523void i40e_pf_reset_stats(struct i40e_pf *pf)
524{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000525 int i;
526
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000527 memset(&pf->stats, 0, sizeof(pf->stats));
528 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
529 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000530
531 for (i = 0; i < I40E_MAX_VEB; i++) {
532 if (pf->veb[i]) {
533 memset(&pf->veb[i]->stats, 0,
534 sizeof(pf->veb[i]->stats));
535 memset(&pf->veb[i]->stats_offsets, 0,
536 sizeof(pf->veb[i]->stats_offsets));
537 pf->veb[i]->stat_offsets_loaded = false;
538 }
539 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000540}
541
542/**
543 * i40e_stat_update48 - read and update a 48 bit stat from the chip
544 * @hw: ptr to the hardware info
545 * @hireg: the high 32 bit reg to read
546 * @loreg: the low 32 bit reg to read
547 * @offset_loaded: has the initial offset been loaded yet
548 * @offset: ptr to current offset value
549 * @stat: ptr to the stat
550 *
551 * Since the device stats are not reset at PFReset, they likely will not
552 * be zeroed when the driver starts. We'll save the first values read
553 * and use them as offsets to be subtracted from the raw values in order
554 * to report stats that count from zero. In the process, we also manage
555 * the potential roll-over.
556 **/
557static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
558 bool offset_loaded, u64 *offset, u64 *stat)
559{
560 u64 new_data;
561
Shannon Nelsonab600852014-01-17 15:36:39 -0800562 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000563 new_data = rd32(hw, loreg);
564 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
565 } else {
566 new_data = rd64(hw, loreg);
567 }
568 if (!offset_loaded)
569 *offset = new_data;
570 if (likely(new_data >= *offset))
571 *stat = new_data - *offset;
572 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400573 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000574 *stat &= 0xFFFFFFFFFFFFULL;
575}
576
577/**
578 * i40e_stat_update32 - read and update a 32 bit stat from the chip
579 * @hw: ptr to the hardware info
580 * @reg: the hw reg to read
581 * @offset_loaded: has the initial offset been loaded yet
582 * @offset: ptr to current offset value
583 * @stat: ptr to the stat
584 **/
585static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
586 bool offset_loaded, u64 *offset, u64 *stat)
587{
588 u32 new_data;
589
590 new_data = rd32(hw, reg);
591 if (!offset_loaded)
592 *offset = new_data;
593 if (likely(new_data >= *offset))
594 *stat = (u32)(new_data - *offset);
595 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400596 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000597}
598
599/**
600 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
601 * @vsi: the VSI to be updated
602 **/
603void i40e_update_eth_stats(struct i40e_vsi *vsi)
604{
605 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
606 struct i40e_pf *pf = vsi->back;
607 struct i40e_hw *hw = &pf->hw;
608 struct i40e_eth_stats *oes;
609 struct i40e_eth_stats *es; /* device's eth stats */
610
611 es = &vsi->eth_stats;
612 oes = &vsi->eth_stats_offsets;
613
614 /* Gather up the stats that the hw collects */
615 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
616 vsi->stat_offsets_loaded,
617 &oes->tx_errors, &es->tx_errors);
618 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
619 vsi->stat_offsets_loaded,
620 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000621 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
622 vsi->stat_offsets_loaded,
623 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
624 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
625 vsi->stat_offsets_loaded,
626 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000627
628 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
629 I40E_GLV_GORCL(stat_idx),
630 vsi->stat_offsets_loaded,
631 &oes->rx_bytes, &es->rx_bytes);
632 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
633 I40E_GLV_UPRCL(stat_idx),
634 vsi->stat_offsets_loaded,
635 &oes->rx_unicast, &es->rx_unicast);
636 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
637 I40E_GLV_MPRCL(stat_idx),
638 vsi->stat_offsets_loaded,
639 &oes->rx_multicast, &es->rx_multicast);
640 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
641 I40E_GLV_BPRCL(stat_idx),
642 vsi->stat_offsets_loaded,
643 &oes->rx_broadcast, &es->rx_broadcast);
644
645 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
646 I40E_GLV_GOTCL(stat_idx),
647 vsi->stat_offsets_loaded,
648 &oes->tx_bytes, &es->tx_bytes);
649 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
650 I40E_GLV_UPTCL(stat_idx),
651 vsi->stat_offsets_loaded,
652 &oes->tx_unicast, &es->tx_unicast);
653 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
654 I40E_GLV_MPTCL(stat_idx),
655 vsi->stat_offsets_loaded,
656 &oes->tx_multicast, &es->tx_multicast);
657 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
658 I40E_GLV_BPTCL(stat_idx),
659 vsi->stat_offsets_loaded,
660 &oes->tx_broadcast, &es->tx_broadcast);
661 vsi->stat_offsets_loaded = true;
662}
663
664/**
665 * i40e_update_veb_stats - Update Switch component statistics
666 * @veb: the VEB being updated
667 **/
668static void i40e_update_veb_stats(struct i40e_veb *veb)
669{
670 struct i40e_pf *pf = veb->pf;
671 struct i40e_hw *hw = &pf->hw;
672 struct i40e_eth_stats *oes;
673 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400674 struct i40e_veb_tc_stats *veb_oes;
675 struct i40e_veb_tc_stats *veb_es;
676 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000677
678 idx = veb->stats_idx;
679 es = &veb->stats;
680 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400681 veb_es = &veb->tc_stats;
682 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000683
684 /* Gather up the stats that the hw collects */
685 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
686 veb->stat_offsets_loaded,
687 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000688 if (hw->revision_id > 0)
689 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
690 veb->stat_offsets_loaded,
691 &oes->rx_unknown_protocol,
692 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000693 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
694 veb->stat_offsets_loaded,
695 &oes->rx_bytes, &es->rx_bytes);
696 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
697 veb->stat_offsets_loaded,
698 &oes->rx_unicast, &es->rx_unicast);
699 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
700 veb->stat_offsets_loaded,
701 &oes->rx_multicast, &es->rx_multicast);
702 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
703 veb->stat_offsets_loaded,
704 &oes->rx_broadcast, &es->rx_broadcast);
705
706 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
707 veb->stat_offsets_loaded,
708 &oes->tx_bytes, &es->tx_bytes);
709 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
710 veb->stat_offsets_loaded,
711 &oes->tx_unicast, &es->tx_unicast);
712 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
713 veb->stat_offsets_loaded,
714 &oes->tx_multicast, &es->tx_multicast);
715 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
716 veb->stat_offsets_loaded,
717 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400718 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
719 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
720 I40E_GLVEBTC_RPCL(i, idx),
721 veb->stat_offsets_loaded,
722 &veb_oes->tc_rx_packets[i],
723 &veb_es->tc_rx_packets[i]);
724 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
725 I40E_GLVEBTC_RBCL(i, idx),
726 veb->stat_offsets_loaded,
727 &veb_oes->tc_rx_bytes[i],
728 &veb_es->tc_rx_bytes[i]);
729 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
730 I40E_GLVEBTC_TPCL(i, idx),
731 veb->stat_offsets_loaded,
732 &veb_oes->tc_tx_packets[i],
733 &veb_es->tc_tx_packets[i]);
734 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
735 I40E_GLVEBTC_TBCL(i, idx),
736 veb->stat_offsets_loaded,
737 &veb_oes->tc_tx_bytes[i],
738 &veb_es->tc_tx_bytes[i]);
739 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000740 veb->stat_offsets_loaded = true;
741}
742
Vasu Dev38e00432014-08-01 13:27:03 -0700743#ifdef I40E_FCOE
744/**
745 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
746 * @vsi: the VSI that is capable of doing FCoE
747 **/
748static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
749{
750 struct i40e_pf *pf = vsi->back;
751 struct i40e_hw *hw = &pf->hw;
752 struct i40e_fcoe_stats *ofs;
753 struct i40e_fcoe_stats *fs; /* device's eth stats */
754 int idx;
755
756 if (vsi->type != I40E_VSI_FCOE)
757 return;
758
759 idx = (pf->pf_seid - I40E_BASE_PF_SEID) + I40E_FCOE_PF_STAT_OFFSET;
760 fs = &vsi->fcoe_stats;
761 ofs = &vsi->fcoe_stats_offsets;
762
763 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
764 vsi->fcoe_stat_offsets_loaded,
765 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
766 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
767 vsi->fcoe_stat_offsets_loaded,
768 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
769 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
770 vsi->fcoe_stat_offsets_loaded,
771 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
772 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
773 vsi->fcoe_stat_offsets_loaded,
774 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
775 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
776 vsi->fcoe_stat_offsets_loaded,
777 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
778 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
779 vsi->fcoe_stat_offsets_loaded,
780 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
781 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
782 vsi->fcoe_stat_offsets_loaded,
783 &ofs->fcoe_last_error, &fs->fcoe_last_error);
784 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
785 vsi->fcoe_stat_offsets_loaded,
786 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
787
788 vsi->fcoe_stat_offsets_loaded = true;
789}
790
791#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000792/**
793 * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
794 * @pf: the corresponding PF
795 *
796 * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
797 **/
798static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
799{
800 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
801 struct i40e_hw_port_stats *nsd = &pf->stats;
802 struct i40e_hw *hw = &pf->hw;
803 u64 xoff = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000804
805 if ((hw->fc.current_mode != I40E_FC_FULL) &&
806 (hw->fc.current_mode != I40E_FC_RX_PAUSE))
807 return;
808
809 xoff = nsd->link_xoff_rx;
810 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
811 pf->stat_offsets_loaded,
812 &osd->link_xoff_rx, &nsd->link_xoff_rx);
813
814 /* No new LFC xoff rx */
815 if (!(nsd->link_xoff_rx - xoff))
816 return;
817
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000818}
819
820/**
821 * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
822 * @pf: the corresponding PF
823 *
824 * Update the Rx XOFF counter (PAUSE frames) in PFC mode
825 **/
826static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
827{
828 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
829 struct i40e_hw_port_stats *nsd = &pf->stats;
830 bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
831 struct i40e_dcbx_config *dcb_cfg;
832 struct i40e_hw *hw = &pf->hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400833 u16 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000834 u8 tc;
835
836 dcb_cfg = &hw->local_dcbx_config;
837
Neerav Parikhe1208142015-04-16 20:05:58 -0400838 /* Collect Link XOFF stats when PFC is disabled */
839 if (!dcb_cfg->pfc.pfcenable) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000840 i40e_update_link_xoff_rx(pf);
841 return;
842 }
843
844 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
845 u64 prio_xoff = nsd->priority_xoff_rx[i];
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400846
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000847 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
848 pf->stat_offsets_loaded,
849 &osd->priority_xoff_rx[i],
850 &nsd->priority_xoff_rx[i]);
851
852 /* No new PFC xoff rx */
853 if (!(nsd->priority_xoff_rx[i] - prio_xoff))
854 continue;
855 /* Get the TC for given priority */
856 tc = dcb_cfg->etscfg.prioritytable[i];
857 xoff[tc] = true;
858 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000859}
860
861/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000862 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000863 * @vsi: the VSI to be updated
864 *
865 * There are a few instances where we store the same stat in a
866 * couple of different structs. This is partly because we have
867 * the netdev stats that need to be filled out, which is slightly
868 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000869 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000870 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000871static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000872{
873 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000874 struct rtnl_link_stats64 *ons;
875 struct rtnl_link_stats64 *ns; /* netdev stats */
876 struct i40e_eth_stats *oes;
877 struct i40e_eth_stats *es; /* device's eth stats */
878 u32 tx_restart, tx_busy;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000879 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000880 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000881 u64 bytes, packets;
882 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400883 u64 tx_linearize;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000884 u64 rx_p, rx_b;
885 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000886 u16 q;
887
888 if (test_bit(__I40E_DOWN, &vsi->state) ||
889 test_bit(__I40E_CONFIG_BUSY, &pf->state))
890 return;
891
892 ns = i40e_get_vsi_stats_struct(vsi);
893 ons = &vsi->net_stats_offsets;
894 es = &vsi->eth_stats;
895 oes = &vsi->eth_stats_offsets;
896
897 /* Gather up the netdev and vsi stats that the driver collects
898 * on the fly during packet processing
899 */
900 rx_b = rx_p = 0;
901 tx_b = tx_p = 0;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400902 tx_restart = tx_busy = tx_linearize = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000903 rx_page = 0;
904 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000905 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000906 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000907 /* locate Tx ring */
908 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000909
Alexander Duyck980e9b12013-09-28 06:01:03 +0000910 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700911 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000912 packets = p->stats.packets;
913 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700914 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000915 tx_b += bytes;
916 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000917 tx_restart += p->tx_stats.restart_queue;
918 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400919 tx_linearize += p->tx_stats.tx_linearize;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000920
921 /* Rx queue is part of the same block as Tx queue */
922 p = &p[1];
923 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700924 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000925 packets = p->stats.packets;
926 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700927 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000928 rx_b += bytes;
929 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000930 rx_buf += p->rx_stats.alloc_buff_failed;
931 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000932 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000933 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000934 vsi->tx_restart = tx_restart;
935 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400936 vsi->tx_linearize = tx_linearize;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000937 vsi->rx_page_failed = rx_page;
938 vsi->rx_buf_failed = rx_buf;
939
940 ns->rx_packets = rx_p;
941 ns->rx_bytes = rx_b;
942 ns->tx_packets = tx_p;
943 ns->tx_bytes = tx_b;
944
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000945 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000946 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000947 ons->tx_errors = oes->tx_errors;
948 ns->tx_errors = es->tx_errors;
949 ons->multicast = oes->rx_multicast;
950 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000951 ons->rx_dropped = oes->rx_discards;
952 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000953 ons->tx_dropped = oes->tx_discards;
954 ns->tx_dropped = es->tx_discards;
955
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000956 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000957 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000958 ns->rx_crc_errors = pf->stats.crc_errors;
959 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
960 ns->rx_length_errors = pf->stats.rx_length_errors;
961 }
962}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000963
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000964/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000965 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000966 * @pf: the PF to be updated
967 **/
968static void i40e_update_pf_stats(struct i40e_pf *pf)
969{
970 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
971 struct i40e_hw_port_stats *nsd = &pf->stats;
972 struct i40e_hw *hw = &pf->hw;
973 u32 val;
974 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000975
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000976 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
977 I40E_GLPRT_GORCL(hw->port),
978 pf->stat_offsets_loaded,
979 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
980 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
981 I40E_GLPRT_GOTCL(hw->port),
982 pf->stat_offsets_loaded,
983 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
984 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
985 pf->stat_offsets_loaded,
986 &osd->eth.rx_discards,
987 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000988 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
989 I40E_GLPRT_UPRCL(hw->port),
990 pf->stat_offsets_loaded,
991 &osd->eth.rx_unicast,
992 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000993 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
994 I40E_GLPRT_MPRCL(hw->port),
995 pf->stat_offsets_loaded,
996 &osd->eth.rx_multicast,
997 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000998 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
999 I40E_GLPRT_BPRCL(hw->port),
1000 pf->stat_offsets_loaded,
1001 &osd->eth.rx_broadcast,
1002 &nsd->eth.rx_broadcast);
1003 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1004 I40E_GLPRT_UPTCL(hw->port),
1005 pf->stat_offsets_loaded,
1006 &osd->eth.tx_unicast,
1007 &nsd->eth.tx_unicast);
1008 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1009 I40E_GLPRT_MPTCL(hw->port),
1010 pf->stat_offsets_loaded,
1011 &osd->eth.tx_multicast,
1012 &nsd->eth.tx_multicast);
1013 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1014 I40E_GLPRT_BPTCL(hw->port),
1015 pf->stat_offsets_loaded,
1016 &osd->eth.tx_broadcast,
1017 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001018
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001019 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1020 pf->stat_offsets_loaded,
1021 &osd->tx_dropped_link_down,
1022 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001023
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001024 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1025 pf->stat_offsets_loaded,
1026 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001027
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001028 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1029 pf->stat_offsets_loaded,
1030 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001031
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001032 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1033 pf->stat_offsets_loaded,
1034 &osd->mac_local_faults,
1035 &nsd->mac_local_faults);
1036 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1037 pf->stat_offsets_loaded,
1038 &osd->mac_remote_faults,
1039 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001040
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001041 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1042 pf->stat_offsets_loaded,
1043 &osd->rx_length_errors,
1044 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001045
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001046 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1047 pf->stat_offsets_loaded,
1048 &osd->link_xon_rx, &nsd->link_xon_rx);
1049 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1050 pf->stat_offsets_loaded,
1051 &osd->link_xon_tx, &nsd->link_xon_tx);
1052 i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
1053 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1054 pf->stat_offsets_loaded,
1055 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001056
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001057 for (i = 0; i < 8; i++) {
1058 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001059 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001060 &osd->priority_xon_rx[i],
1061 &nsd->priority_xon_rx[i]);
1062 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001063 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001064 &osd->priority_xon_tx[i],
1065 &nsd->priority_xon_tx[i]);
1066 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001067 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001068 &osd->priority_xoff_tx[i],
1069 &nsd->priority_xoff_tx[i]);
1070 i40e_stat_update32(hw,
1071 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001072 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001073 &osd->priority_xon_2_xoff[i],
1074 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001075 }
1076
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001077 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1078 I40E_GLPRT_PRC64L(hw->port),
1079 pf->stat_offsets_loaded,
1080 &osd->rx_size_64, &nsd->rx_size_64);
1081 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1082 I40E_GLPRT_PRC127L(hw->port),
1083 pf->stat_offsets_loaded,
1084 &osd->rx_size_127, &nsd->rx_size_127);
1085 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1086 I40E_GLPRT_PRC255L(hw->port),
1087 pf->stat_offsets_loaded,
1088 &osd->rx_size_255, &nsd->rx_size_255);
1089 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1090 I40E_GLPRT_PRC511L(hw->port),
1091 pf->stat_offsets_loaded,
1092 &osd->rx_size_511, &nsd->rx_size_511);
1093 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1094 I40E_GLPRT_PRC1023L(hw->port),
1095 pf->stat_offsets_loaded,
1096 &osd->rx_size_1023, &nsd->rx_size_1023);
1097 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1098 I40E_GLPRT_PRC1522L(hw->port),
1099 pf->stat_offsets_loaded,
1100 &osd->rx_size_1522, &nsd->rx_size_1522);
1101 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1102 I40E_GLPRT_PRC9522L(hw->port),
1103 pf->stat_offsets_loaded,
1104 &osd->rx_size_big, &nsd->rx_size_big);
1105
1106 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1107 I40E_GLPRT_PTC64L(hw->port),
1108 pf->stat_offsets_loaded,
1109 &osd->tx_size_64, &nsd->tx_size_64);
1110 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1111 I40E_GLPRT_PTC127L(hw->port),
1112 pf->stat_offsets_loaded,
1113 &osd->tx_size_127, &nsd->tx_size_127);
1114 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1115 I40E_GLPRT_PTC255L(hw->port),
1116 pf->stat_offsets_loaded,
1117 &osd->tx_size_255, &nsd->tx_size_255);
1118 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1119 I40E_GLPRT_PTC511L(hw->port),
1120 pf->stat_offsets_loaded,
1121 &osd->tx_size_511, &nsd->tx_size_511);
1122 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1123 I40E_GLPRT_PTC1023L(hw->port),
1124 pf->stat_offsets_loaded,
1125 &osd->tx_size_1023, &nsd->tx_size_1023);
1126 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1127 I40E_GLPRT_PTC1522L(hw->port),
1128 pf->stat_offsets_loaded,
1129 &osd->tx_size_1522, &nsd->tx_size_1522);
1130 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1131 I40E_GLPRT_PTC9522L(hw->port),
1132 pf->stat_offsets_loaded,
1133 &osd->tx_size_big, &nsd->tx_size_big);
1134
1135 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1136 pf->stat_offsets_loaded,
1137 &osd->rx_undersize, &nsd->rx_undersize);
1138 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1139 pf->stat_offsets_loaded,
1140 &osd->rx_fragments, &nsd->rx_fragments);
1141 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1142 pf->stat_offsets_loaded,
1143 &osd->rx_oversize, &nsd->rx_oversize);
1144 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1145 pf->stat_offsets_loaded,
1146 &osd->rx_jabber, &nsd->rx_jabber);
1147
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001148 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001149 i40e_stat_update32(hw,
1150 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001151 pf->stat_offsets_loaded,
1152 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001153 i40e_stat_update32(hw,
1154 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001155 pf->stat_offsets_loaded,
1156 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001157 i40e_stat_update32(hw,
1158 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1159 pf->stat_offsets_loaded,
1160 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001161
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001162 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1163 nsd->tx_lpi_status =
1164 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1165 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1166 nsd->rx_lpi_status =
1167 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1168 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1169 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1170 pf->stat_offsets_loaded,
1171 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1172 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1173 pf->stat_offsets_loaded,
1174 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1175
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001176 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1177 !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
1178 nsd->fd_sb_status = true;
1179 else
1180 nsd->fd_sb_status = false;
1181
1182 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1183 !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1184 nsd->fd_atr_status = true;
1185 else
1186 nsd->fd_atr_status = false;
1187
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001188 pf->stat_offsets_loaded = true;
1189}
1190
1191/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001192 * i40e_update_stats - Update the various statistics counters.
1193 * @vsi: the VSI to be updated
1194 *
1195 * Update the various stats for this VSI and its related entities.
1196 **/
1197void i40e_update_stats(struct i40e_vsi *vsi)
1198{
1199 struct i40e_pf *pf = vsi->back;
1200
1201 if (vsi == pf->vsi[pf->lan_vsi])
1202 i40e_update_pf_stats(pf);
1203
1204 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001205#ifdef I40E_FCOE
1206 i40e_update_fcoe_stats(vsi);
1207#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001208}
1209
1210/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001211 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1212 * @vsi: the VSI to be searched
1213 * @macaddr: the MAC address
1214 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001215 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001216 * @is_netdev: make sure its a netdev filter, else doesn't matter
1217 *
1218 * Returns ptr to the filter object or NULL
1219 **/
1220static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1221 u8 *macaddr, s16 vlan,
1222 bool is_vf, bool is_netdev)
1223{
1224 struct i40e_mac_filter *f;
1225
1226 if (!vsi || !macaddr)
1227 return NULL;
1228
1229 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1230 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1231 (vlan == f->vlan) &&
1232 (!is_vf || f->is_vf) &&
1233 (!is_netdev || f->is_netdev))
1234 return f;
1235 }
1236 return NULL;
1237}
1238
1239/**
1240 * i40e_find_mac - Find a mac addr in the macvlan filters list
1241 * @vsi: the VSI to be searched
1242 * @macaddr: the MAC address we are searching for
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001243 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001244 * @is_netdev: make sure its a netdev filter, else doesn't matter
1245 *
1246 * Returns the first filter with the provided MAC address or NULL if
1247 * MAC address was not found
1248 **/
1249struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1250 bool is_vf, bool is_netdev)
1251{
1252 struct i40e_mac_filter *f;
1253
1254 if (!vsi || !macaddr)
1255 return NULL;
1256
1257 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1258 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1259 (!is_vf || f->is_vf) &&
1260 (!is_netdev || f->is_netdev))
1261 return f;
1262 }
1263 return NULL;
1264}
1265
1266/**
1267 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1268 * @vsi: the VSI to be searched
1269 *
1270 * Returns true if VSI is in vlan mode or false otherwise
1271 **/
1272bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1273{
1274 struct i40e_mac_filter *f;
1275
1276 /* Only -1 for all the filters denotes not in vlan mode
1277 * so we have to go through all the list in order to make sure
1278 */
1279 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Greg Rosed9b68f82015-07-23 16:54:31 -04001280 if (f->vlan >= 0 || vsi->info.pvid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001281 return true;
1282 }
1283
1284 return false;
1285}
1286
1287/**
1288 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1289 * @vsi: the VSI to be searched
1290 * @macaddr: the mac address to be filtered
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001291 * @is_vf: true if it is a VF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001292 * @is_netdev: true if it is a netdev
1293 *
1294 * Goes through all the macvlan filters and adds a
1295 * macvlan filter for each unique vlan that already exists
1296 *
1297 * Returns first filter found on success, else NULL
1298 **/
1299struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1300 bool is_vf, bool is_netdev)
1301{
1302 struct i40e_mac_filter *f;
1303
1304 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Mitch Williamsecbb44e2015-07-10 19:35:56 -04001305 if (vsi->info.pvid)
1306 f->vlan = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001307 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1308 is_vf, is_netdev)) {
1309 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001310 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001311 return NULL;
1312 }
1313 }
1314
1315 return list_first_entry_or_null(&vsi->mac_filter_list,
1316 struct i40e_mac_filter, list);
1317}
1318
1319/**
Greg Rose8c27d422014-05-22 06:31:56 +00001320 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1321 * @vsi: the PF Main VSI - inappropriate for any other VSI
1322 * @macaddr: the MAC address
Shannon Nelson30650cc2014-07-29 04:01:50 +00001323 *
1324 * Some older firmware configurations set up a default promiscuous VLAN
1325 * filter that needs to be removed.
Greg Rose8c27d422014-05-22 06:31:56 +00001326 **/
Shannon Nelson30650cc2014-07-29 04:01:50 +00001327static int i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
Greg Rose8c27d422014-05-22 06:31:56 +00001328{
1329 struct i40e_aqc_remove_macvlan_element_data element;
1330 struct i40e_pf *pf = vsi->back;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001331 i40e_status ret;
Greg Rose8c27d422014-05-22 06:31:56 +00001332
1333 /* Only appropriate for the PF main VSI */
1334 if (vsi->type != I40E_VSI_MAIN)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001335 return -EINVAL;
Greg Rose8c27d422014-05-22 06:31:56 +00001336
Shannon Nelson30650cc2014-07-29 04:01:50 +00001337 memset(&element, 0, sizeof(element));
Greg Rose8c27d422014-05-22 06:31:56 +00001338 ether_addr_copy(element.mac_addr, macaddr);
1339 element.vlan_tag = 0;
1340 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1341 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001342 ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1343 if (ret)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001344 return -ENOENT;
1345
1346 return 0;
Greg Rose8c27d422014-05-22 06:31:56 +00001347}
1348
1349/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001350 * i40e_add_filter - Add a mac/vlan filter to the VSI
1351 * @vsi: the VSI to be searched
1352 * @macaddr: the MAC address
1353 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001354 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001355 * @is_netdev: make sure its a netdev filter, else doesn't matter
1356 *
1357 * Returns ptr to the filter object or NULL when no memory available.
1358 **/
1359struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1360 u8 *macaddr, s16 vlan,
1361 bool is_vf, bool is_netdev)
1362{
1363 struct i40e_mac_filter *f;
1364
1365 if (!vsi || !macaddr)
1366 return NULL;
1367
1368 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1369 if (!f) {
1370 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1371 if (!f)
1372 goto add_filter_out;
1373
Greg Rose9a173902014-05-22 06:32:02 +00001374 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001375 f->vlan = vlan;
1376 f->changed = true;
1377
1378 INIT_LIST_HEAD(&f->list);
1379 list_add(&f->list, &vsi->mac_filter_list);
1380 }
1381
1382 /* increment counter and add a new flag if needed */
1383 if (is_vf) {
1384 if (!f->is_vf) {
1385 f->is_vf = true;
1386 f->counter++;
1387 }
1388 } else if (is_netdev) {
1389 if (!f->is_netdev) {
1390 f->is_netdev = true;
1391 f->counter++;
1392 }
1393 } else {
1394 f->counter++;
1395 }
1396
1397 /* changed tells sync_filters_subtask to
1398 * push the filter down to the firmware
1399 */
1400 if (f->changed) {
1401 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1402 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1403 }
1404
1405add_filter_out:
1406 return f;
1407}
1408
1409/**
1410 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1411 * @vsi: the VSI to be searched
1412 * @macaddr: the MAC address
1413 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001414 * @is_vf: make sure it's a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001415 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1416 **/
1417void i40e_del_filter(struct i40e_vsi *vsi,
1418 u8 *macaddr, s16 vlan,
1419 bool is_vf, bool is_netdev)
1420{
1421 struct i40e_mac_filter *f;
1422
1423 if (!vsi || !macaddr)
1424 return;
1425
1426 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1427 if (!f || f->counter == 0)
1428 return;
1429
1430 if (is_vf) {
1431 if (f->is_vf) {
1432 f->is_vf = false;
1433 f->counter--;
1434 }
1435 } else if (is_netdev) {
1436 if (f->is_netdev) {
1437 f->is_netdev = false;
1438 f->counter--;
1439 }
1440 } else {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001441 /* make sure we don't remove a filter in use by VF or netdev */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001442 int min_f = 0;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001443
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001444 min_f += (f->is_vf ? 1 : 0);
1445 min_f += (f->is_netdev ? 1 : 0);
1446
1447 if (f->counter > min_f)
1448 f->counter--;
1449 }
1450
1451 /* counter == 0 tells sync_filters_subtask to
1452 * remove the filter from the firmware's list
1453 */
1454 if (f->counter == 0) {
1455 f->changed = true;
1456 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1457 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1458 }
1459}
1460
1461/**
1462 * i40e_set_mac - NDO callback to set mac address
1463 * @netdev: network interface device structure
1464 * @p: pointer to an address structure
1465 *
1466 * Returns 0 on success, negative on failure
1467 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001468#ifdef I40E_FCOE
1469int i40e_set_mac(struct net_device *netdev, void *p)
1470#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001471static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001472#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001473{
1474 struct i40e_netdev_priv *np = netdev_priv(netdev);
1475 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001476 struct i40e_pf *pf = vsi->back;
1477 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001478 struct sockaddr *addr = p;
1479 struct i40e_mac_filter *f;
1480
1481 if (!is_valid_ether_addr(addr->sa_data))
1482 return -EADDRNOTAVAIL;
1483
Shannon Nelson30650cc2014-07-29 04:01:50 +00001484 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1485 netdev_info(netdev, "already using mac address %pM\n",
1486 addr->sa_data);
1487 return 0;
1488 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001489
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001490 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1491 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1492 return -EADDRNOTAVAIL;
1493
Shannon Nelson30650cc2014-07-29 04:01:50 +00001494 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1495 netdev_info(netdev, "returning to hw mac address %pM\n",
1496 hw->mac.addr);
1497 else
1498 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1499
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001500 if (vsi->type == I40E_VSI_MAIN) {
1501 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001502
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001503 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001504 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001505 addr->sa_data, NULL);
1506 if (ret) {
1507 netdev_info(netdev,
1508 "Addr change for Main VSI failed: %d\n",
1509 ret);
1510 return -EADDRNOTAVAIL;
1511 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001512 }
1513
Shannon Nelson30650cc2014-07-29 04:01:50 +00001514 if (ether_addr_equal(netdev->dev_addr, hw->mac.addr)) {
1515 struct i40e_aqc_remove_macvlan_element_data element;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001516
Shannon Nelson30650cc2014-07-29 04:01:50 +00001517 memset(&element, 0, sizeof(element));
1518 ether_addr_copy(element.mac_addr, netdev->dev_addr);
1519 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1520 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1521 } else {
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001522 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1523 false, false);
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001524 }
1525
Shannon Nelson30650cc2014-07-29 04:01:50 +00001526 if (ether_addr_equal(addr->sa_data, hw->mac.addr)) {
1527 struct i40e_aqc_add_macvlan_element_data element;
1528
1529 memset(&element, 0, sizeof(element));
1530 ether_addr_copy(element.mac_addr, hw->mac.addr);
1531 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
1532 i40e_aq_add_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1533 } else {
1534 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY,
1535 false, false);
1536 if (f)
1537 f->is_laa = true;
1538 }
1539
Anjali Singhai30e25612015-09-28 13:37:12 -07001540 i40e_sync_vsi_filters(vsi, false);
Shannon Nelson30650cc2014-07-29 04:01:50 +00001541 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001542
1543 return 0;
1544}
1545
1546/**
1547 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1548 * @vsi: the VSI being setup
1549 * @ctxt: VSI context structure
1550 * @enabled_tc: Enabled TCs bitmap
1551 * @is_add: True if called before Add VSI
1552 *
1553 * Setup VSI queue mapping for enabled traffic classes.
1554 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001555#ifdef I40E_FCOE
1556void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1557 struct i40e_vsi_context *ctxt,
1558 u8 enabled_tc,
1559 bool is_add)
1560#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001561static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1562 struct i40e_vsi_context *ctxt,
1563 u8 enabled_tc,
1564 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001565#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001566{
1567 struct i40e_pf *pf = vsi->back;
1568 u16 sections = 0;
1569 u8 netdev_tc = 0;
1570 u16 numtc = 0;
1571 u16 qcount;
1572 u8 offset;
1573 u16 qmap;
1574 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001575 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001576
1577 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1578 offset = 0;
1579
1580 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1581 /* Find numtc from enabled TC bitmap */
1582 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001583 if (enabled_tc & BIT_ULL(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001584 numtc++;
1585 }
1586 if (!numtc) {
1587 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1588 numtc = 1;
1589 }
1590 } else {
1591 /* At least TC0 is enabled in case of non-DCB case */
1592 numtc = 1;
1593 }
1594
1595 vsi->tc_config.numtc = numtc;
1596 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001597 /* Number of queues per enabled TC */
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001598 /* In MFP case we can have a much lower count of MSIx
1599 * vectors available and so we need to lower the used
1600 * q count.
1601 */
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001602 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1603 qcount = min_t(int, vsi->alloc_queue_pairs, pf->num_lan_msix);
1604 else
1605 qcount = vsi->alloc_queue_pairs;
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001606 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001607 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001608
1609 /* Setup queue offset/count for all TCs for given VSI */
1610 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1611 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001612 if (vsi->tc_config.enabled_tc & BIT_ULL(i)) {
1613 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001614 int pow, num_qps;
1615
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001616 switch (vsi->type) {
1617 case I40E_VSI_MAIN:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001618 qcount = min_t(int, pf->rss_size, num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001619 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001620#ifdef I40E_FCOE
1621 case I40E_VSI_FCOE:
1622 qcount = num_tc_qps;
1623 break;
1624#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001625 case I40E_VSI_FDIR:
1626 case I40E_VSI_SRIOV:
1627 case I40E_VSI_VMDQ2:
1628 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001629 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001630 WARN_ON(i != 0);
1631 break;
1632 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001633 vsi->tc_config.tc_info[i].qoffset = offset;
1634 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001635
Shannon Nelson1e200e42015-02-27 09:15:24 +00001636 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001637 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001638 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001639 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001640 pow++;
1641 num_qps >>= 1;
1642 }
1643
1644 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1645 qmap =
1646 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1647 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1648
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001649 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001650 } else {
1651 /* TC is not enabled so set the offset to
1652 * default queue and allocate one queue
1653 * for the given TC.
1654 */
1655 vsi->tc_config.tc_info[i].qoffset = 0;
1656 vsi->tc_config.tc_info[i].qcount = 1;
1657 vsi->tc_config.tc_info[i].netdev_tc = 0;
1658
1659 qmap = 0;
1660 }
1661 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1662 }
1663
1664 /* Set actual Tx/Rx queue pairs */
1665 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001666 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1667 if (vsi->req_queue_pairs > 0)
1668 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001669 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001670 vsi->num_queue_pairs = pf->num_lan_msix;
1671 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001672
1673 /* Scheduler section valid can only be set for ADD VSI */
1674 if (is_add) {
1675 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1676
1677 ctxt->info.up_enable_bits = enabled_tc;
1678 }
1679 if (vsi->type == I40E_VSI_SRIOV) {
1680 ctxt->info.mapping_flags |=
1681 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1682 for (i = 0; i < vsi->num_queue_pairs; i++)
1683 ctxt->info.queue_mapping[i] =
1684 cpu_to_le16(vsi->base_queue + i);
1685 } else {
1686 ctxt->info.mapping_flags |=
1687 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1688 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1689 }
1690 ctxt->info.valid_sections |= cpu_to_le16(sections);
1691}
1692
1693/**
1694 * i40e_set_rx_mode - NDO callback to set the netdev filters
1695 * @netdev: network interface device structure
1696 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001697#ifdef I40E_FCOE
1698void i40e_set_rx_mode(struct net_device *netdev)
1699#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001700static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001701#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001702{
1703 struct i40e_netdev_priv *np = netdev_priv(netdev);
1704 struct i40e_mac_filter *f, *ftmp;
1705 struct i40e_vsi *vsi = np->vsi;
1706 struct netdev_hw_addr *uca;
1707 struct netdev_hw_addr *mca;
1708 struct netdev_hw_addr *ha;
1709
1710 /* add addr if not already in the filter list */
1711 netdev_for_each_uc_addr(uca, netdev) {
1712 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1713 if (i40e_is_vsi_in_vlan(vsi))
1714 i40e_put_mac_in_vlan(vsi, uca->addr,
1715 false, true);
1716 else
1717 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1718 false, true);
1719 }
1720 }
1721
1722 netdev_for_each_mc_addr(mca, netdev) {
1723 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1724 if (i40e_is_vsi_in_vlan(vsi))
1725 i40e_put_mac_in_vlan(vsi, mca->addr,
1726 false, true);
1727 else
1728 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1729 false, true);
1730 }
1731 }
1732
1733 /* remove filter if not in netdev list */
1734 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001735
1736 if (!f->is_netdev)
1737 continue;
1738
Shannon Nelson2f41f332015-08-26 15:14:20 -04001739 netdev_for_each_mc_addr(mca, netdev)
1740 if (ether_addr_equal(mca->addr, f->macaddr))
1741 goto bottom_of_search_loop;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001742
Shannon Nelson2f41f332015-08-26 15:14:20 -04001743 netdev_for_each_uc_addr(uca, netdev)
1744 if (ether_addr_equal(uca->addr, f->macaddr))
1745 goto bottom_of_search_loop;
1746
1747 for_each_dev_addr(netdev, ha)
1748 if (ether_addr_equal(ha->addr, f->macaddr))
1749 goto bottom_of_search_loop;
1750
1751 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
1752 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1753
1754bottom_of_search_loop:
1755 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001756 }
1757
1758 /* check for other flag changes */
1759 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1760 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1761 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1762 }
1763}
1764
1765/**
1766 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1767 * @vsi: ptr to the VSI
Anjali Singhai30e25612015-09-28 13:37:12 -07001768 * @grab_rtnl: whether RTNL needs to be grabbed
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001769 *
1770 * Push any outstanding VSI filter changes through the AdminQ.
1771 *
1772 * Returns 0 or error value
1773 **/
Anjali Singhai30e25612015-09-28 13:37:12 -07001774int i40e_sync_vsi_filters(struct i40e_vsi *vsi, bool grab_rtnl)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001775{
1776 struct i40e_mac_filter *f, *ftmp;
1777 bool promisc_forced_on = false;
1778 bool add_happened = false;
1779 int filter_list_len = 0;
1780 u32 changed_flags = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001781 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001782 struct i40e_pf *pf;
1783 int num_add = 0;
1784 int num_del = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001785 int aq_err = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001786 u16 cmd_flags;
1787
1788 /* empty array typed pointers, kcalloc later */
1789 struct i40e_aqc_add_macvlan_element_data *add_list;
1790 struct i40e_aqc_remove_macvlan_element_data *del_list;
1791
1792 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1793 usleep_range(1000, 2000);
1794 pf = vsi->back;
1795
1796 if (vsi->netdev) {
1797 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1798 vsi->current_netdev_flags = vsi->netdev->flags;
1799 }
1800
1801 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1802 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1803
1804 filter_list_len = pf->hw.aq.asq_buf_size /
1805 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1806 del_list = kcalloc(filter_list_len,
1807 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1808 GFP_KERNEL);
1809 if (!del_list)
1810 return -ENOMEM;
1811
1812 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1813 if (!f->changed)
1814 continue;
1815
1816 if (f->counter != 0)
1817 continue;
1818 f->changed = false;
1819 cmd_flags = 0;
1820
1821 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001822 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001823 del_list[num_del].vlan_tag =
1824 cpu_to_le16((u16)(f->vlan ==
1825 I40E_VLAN_ANY ? 0 : f->vlan));
1826
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001827 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1828 del_list[num_del].flags = cmd_flags;
1829 num_del++;
1830
1831 /* unlink from filter list */
1832 list_del(&f->list);
1833 kfree(f);
1834
1835 /* flush a full buffer */
1836 if (num_del == filter_list_len) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001837 ret = i40e_aq_remove_macvlan(&pf->hw,
1838 vsi->seid, del_list, num_del,
1839 NULL);
1840 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001841 num_del = 0;
1842 memset(del_list, 0, sizeof(*del_list));
1843
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001844 if (ret && aq_err != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001845 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001846 "ignoring delete macvlan error, err %s, aq_err %s while flushing a full buffer\n",
1847 i40e_stat_str(&pf->hw, ret),
1848 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001849 }
1850 }
1851 if (num_del) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001852 ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001853 del_list, num_del, NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001854 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001855 num_del = 0;
1856
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001857 if (ret && aq_err != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001858 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001859 "ignoring delete macvlan error, err %s aq_err %s\n",
1860 i40e_stat_str(&pf->hw, ret),
1861 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001862 }
1863
1864 kfree(del_list);
1865 del_list = NULL;
1866
1867 /* do all the adds now */
1868 filter_list_len = pf->hw.aq.asq_buf_size /
1869 sizeof(struct i40e_aqc_add_macvlan_element_data),
1870 add_list = kcalloc(filter_list_len,
1871 sizeof(struct i40e_aqc_add_macvlan_element_data),
1872 GFP_KERNEL);
1873 if (!add_list)
1874 return -ENOMEM;
1875
1876 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1877 if (!f->changed)
1878 continue;
1879
1880 if (f->counter == 0)
1881 continue;
1882 f->changed = false;
1883 add_happened = true;
1884 cmd_flags = 0;
1885
1886 /* add to add array */
Greg Rose9a173902014-05-22 06:32:02 +00001887 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001888 add_list[num_add].vlan_tag =
1889 cpu_to_le16(
1890 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1891 add_list[num_add].queue_number = 0;
1892
1893 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001894 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1895 num_add++;
1896
1897 /* flush a full buffer */
1898 if (num_add == filter_list_len) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001899 ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1900 add_list, num_add,
1901 NULL);
1902 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001903 num_add = 0;
1904
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001905 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001906 break;
1907 memset(add_list, 0, sizeof(*add_list));
1908 }
1909 }
1910 if (num_add) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001911 ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1912 add_list, num_add, NULL);
1913 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001914 num_add = 0;
1915 }
1916 kfree(add_list);
1917 add_list = NULL;
1918
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001919 if (add_happened && ret && aq_err != I40E_AQ_RC_EINVAL) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001920 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001921 "add filter failed, err %s aq_err %s\n",
1922 i40e_stat_str(&pf->hw, ret),
1923 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001924 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1925 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1926 &vsi->state)) {
1927 promisc_forced_on = true;
1928 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1929 &vsi->state);
1930 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1931 }
1932 }
1933 }
1934
1935 /* check for changes in promiscuous modes */
1936 if (changed_flags & IFF_ALLMULTI) {
1937 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001938
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001939 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001940 ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1941 vsi->seid,
1942 cur_multipromisc,
1943 NULL);
1944 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001945 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001946 "set multi promisc failed, err %s aq_err %s\n",
1947 i40e_stat_str(&pf->hw, ret),
1948 i40e_aq_str(&pf->hw,
1949 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001950 }
1951 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1952 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001953
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001954 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1955 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1956 &vsi->state));
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04001957 if (vsi->type == I40E_VSI_MAIN && pf->lan_veb != I40E_NO_VEB) {
1958 /* set defport ON for Main VSI instead of true promisc
1959 * this way we will get all unicast/multicast and VLAN
1960 * promisc behavior but will not get VF or VMDq traffic
1961 * replicated on the Main VSI.
1962 */
1963 if (pf->cur_promisc != cur_promisc) {
1964 pf->cur_promisc = cur_promisc;
Anjali Singhai30e25612015-09-28 13:37:12 -07001965 if (grab_rtnl)
1966 i40e_do_reset_safe(pf,
1967 BIT(__I40E_PF_RESET_REQUESTED));
1968 else
1969 i40e_do_reset(pf,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04001970 BIT(__I40E_PF_RESET_REQUESTED));
1971 }
1972 } else {
1973 ret = i40e_aq_set_vsi_unicast_promiscuous(
1974 &vsi->back->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001975 vsi->seid,
1976 cur_promisc, NULL);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04001977 if (ret)
1978 dev_info(&pf->pdev->dev,
1979 "set unicast promisc failed, err %d, aq_err %d\n",
1980 ret, pf->hw.aq.asq_last_status);
1981 ret = i40e_aq_set_vsi_multicast_promiscuous(
1982 &vsi->back->hw,
1983 vsi->seid,
1984 cur_promisc, NULL);
1985 if (ret)
1986 dev_info(&pf->pdev->dev,
1987 "set multicast promisc failed, err %d, aq_err %d\n",
1988 ret, pf->hw.aq.asq_last_status);
1989 }
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001990 ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1991 vsi->seid,
1992 cur_promisc, NULL);
1993 if (ret)
Greg Rose1a103702013-11-28 06:42:39 +00001994 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001995 "set brdcast promisc failed, err %s, aq_err %s\n",
1996 i40e_stat_str(&pf->hw, ret),
1997 i40e_aq_str(&pf->hw,
1998 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001999 }
2000
2001 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2002 return 0;
2003}
2004
2005/**
2006 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2007 * @pf: board private structure
2008 **/
2009static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2010{
2011 int v;
2012
2013 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2014 return;
2015 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2016
Mitch Williams505682c2014-05-20 08:01:37 +00002017 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002018 if (pf->vsi[v] &&
2019 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
Anjali Singhai30e25612015-09-28 13:37:12 -07002020 i40e_sync_vsi_filters(pf->vsi[v], true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002021 }
2022}
2023
2024/**
2025 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2026 * @netdev: network interface device structure
2027 * @new_mtu: new value for maximum frame size
2028 *
2029 * Returns 0 on success, negative on failure
2030 **/
2031static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2032{
2033 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00002034 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002035 struct i40e_vsi *vsi = np->vsi;
2036
2037 /* MTU < 68 is an error and causes problems on some kernels */
2038 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2039 return -EINVAL;
2040
2041 netdev_info(netdev, "changing MTU from %d to %d\n",
2042 netdev->mtu, new_mtu);
2043 netdev->mtu = new_mtu;
2044 if (netif_running(netdev))
2045 i40e_vsi_reinit_locked(vsi);
2046
2047 return 0;
2048}
2049
2050/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002051 * i40e_ioctl - Access the hwtstamp interface
2052 * @netdev: network interface device structure
2053 * @ifr: interface request data
2054 * @cmd: ioctl command
2055 **/
2056int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2057{
2058 struct i40e_netdev_priv *np = netdev_priv(netdev);
2059 struct i40e_pf *pf = np->vsi->back;
2060
2061 switch (cmd) {
2062 case SIOCGHWTSTAMP:
2063 return i40e_ptp_get_ts_config(pf, ifr);
2064 case SIOCSHWTSTAMP:
2065 return i40e_ptp_set_ts_config(pf, ifr);
2066 default:
2067 return -EOPNOTSUPP;
2068 }
2069}
2070
2071/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002072 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2073 * @vsi: the vsi being adjusted
2074 **/
2075void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2076{
2077 struct i40e_vsi_context ctxt;
2078 i40e_status ret;
2079
2080 if ((vsi->info.valid_sections &
2081 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2082 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2083 return; /* already enabled */
2084
2085 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2086 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2087 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2088
2089 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002090 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002091 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2092 if (ret) {
2093 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002094 "update vlan stripping failed, err %s aq_err %s\n",
2095 i40e_stat_str(&vsi->back->hw, ret),
2096 i40e_aq_str(&vsi->back->hw,
2097 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002098 }
2099}
2100
2101/**
2102 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2103 * @vsi: the vsi being adjusted
2104 **/
2105void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2106{
2107 struct i40e_vsi_context ctxt;
2108 i40e_status ret;
2109
2110 if ((vsi->info.valid_sections &
2111 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2112 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2113 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2114 return; /* already disabled */
2115
2116 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2117 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2118 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2119
2120 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002121 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002122 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2123 if (ret) {
2124 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002125 "update vlan stripping failed, err %s aq_err %s\n",
2126 i40e_stat_str(&vsi->back->hw, ret),
2127 i40e_aq_str(&vsi->back->hw,
2128 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002129 }
2130}
2131
2132/**
2133 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2134 * @netdev: network interface to be adjusted
2135 * @features: netdev features to test if VLAN offload is enabled or not
2136 **/
2137static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2138{
2139 struct i40e_netdev_priv *np = netdev_priv(netdev);
2140 struct i40e_vsi *vsi = np->vsi;
2141
2142 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2143 i40e_vlan_stripping_enable(vsi);
2144 else
2145 i40e_vlan_stripping_disable(vsi);
2146}
2147
2148/**
2149 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2150 * @vsi: the vsi being configured
2151 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2152 **/
2153int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2154{
2155 struct i40e_mac_filter *f, *add_f;
2156 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002157
2158 is_vf = (vsi->type == I40E_VSI_SRIOV);
2159 is_netdev = !!(vsi->netdev);
2160
2161 if (is_netdev) {
2162 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
2163 is_vf, is_netdev);
2164 if (!add_f) {
2165 dev_info(&vsi->back->pdev->dev,
2166 "Could not add vlan filter %d for %pM\n",
2167 vid, vsi->netdev->dev_addr);
2168 return -ENOMEM;
2169 }
2170 }
2171
2172 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2173 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2174 if (!add_f) {
2175 dev_info(&vsi->back->pdev->dev,
2176 "Could not add vlan filter %d for %pM\n",
2177 vid, f->macaddr);
2178 return -ENOMEM;
2179 }
2180 }
2181
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002182 /* Now if we add a vlan tag, make sure to check if it is the first
2183 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2184 * with 0, so we now accept untagged and specified tagged traffic
2185 * (and not any taged and untagged)
2186 */
2187 if (vid > 0) {
2188 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
2189 I40E_VLAN_ANY,
2190 is_vf, is_netdev)) {
2191 i40e_del_filter(vsi, vsi->netdev->dev_addr,
2192 I40E_VLAN_ANY, is_vf, is_netdev);
2193 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
2194 is_vf, is_netdev);
2195 if (!add_f) {
2196 dev_info(&vsi->back->pdev->dev,
2197 "Could not add filter 0 for %pM\n",
2198 vsi->netdev->dev_addr);
2199 return -ENOMEM;
2200 }
2201 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002202 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002203
Greg Rose8d82a7c2014-01-13 16:13:04 -08002204 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2205 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002206 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2207 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2208 is_vf, is_netdev)) {
2209 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2210 is_vf, is_netdev);
2211 add_f = i40e_add_filter(vsi, f->macaddr,
2212 0, is_vf, is_netdev);
2213 if (!add_f) {
2214 dev_info(&vsi->back->pdev->dev,
2215 "Could not add filter 0 for %pM\n",
2216 f->macaddr);
2217 return -ENOMEM;
2218 }
2219 }
2220 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002221 }
2222
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002223 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2224 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2225 return 0;
2226
Anjali Singhai30e25612015-09-28 13:37:12 -07002227 return i40e_sync_vsi_filters(vsi, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002228}
2229
2230/**
2231 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2232 * @vsi: the vsi being configured
2233 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002234 *
2235 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002236 **/
2237int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
2238{
2239 struct net_device *netdev = vsi->netdev;
2240 struct i40e_mac_filter *f, *add_f;
2241 bool is_vf, is_netdev;
2242 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002243
2244 is_vf = (vsi->type == I40E_VSI_SRIOV);
2245 is_netdev = !!(netdev);
2246
2247 if (is_netdev)
2248 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
2249
2250 list_for_each_entry(f, &vsi->mac_filter_list, list)
2251 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2252
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002253 /* go through all the filters for this VSI and if there is only
2254 * vid == 0 it means there are no other filters, so vid 0 must
2255 * be replaced with -1. This signifies that we should from now
2256 * on accept any traffic (with any tag present, or untagged)
2257 */
2258 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2259 if (is_netdev) {
2260 if (f->vlan &&
2261 ether_addr_equal(netdev->dev_addr, f->macaddr))
2262 filter_count++;
2263 }
2264
2265 if (f->vlan)
2266 filter_count++;
2267 }
2268
2269 if (!filter_count && is_netdev) {
2270 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2271 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2272 is_vf, is_netdev);
2273 if (!f) {
2274 dev_info(&vsi->back->pdev->dev,
2275 "Could not add filter %d for %pM\n",
2276 I40E_VLAN_ANY, netdev->dev_addr);
2277 return -ENOMEM;
2278 }
2279 }
2280
2281 if (!filter_count) {
2282 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2283 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2284 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2285 is_vf, is_netdev);
2286 if (!add_f) {
2287 dev_info(&vsi->back->pdev->dev,
2288 "Could not add filter %d for %pM\n",
2289 I40E_VLAN_ANY, f->macaddr);
2290 return -ENOMEM;
2291 }
2292 }
2293 }
2294
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002295 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2296 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2297 return 0;
2298
Anjali Singhai30e25612015-09-28 13:37:12 -07002299 return i40e_sync_vsi_filters(vsi, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002300}
2301
2302/**
2303 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2304 * @netdev: network interface to be adjusted
2305 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002306 *
2307 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002308 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002309#ifdef I40E_FCOE
2310int i40e_vlan_rx_add_vid(struct net_device *netdev,
2311 __always_unused __be16 proto, u16 vid)
2312#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002313static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2314 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002315#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002316{
2317 struct i40e_netdev_priv *np = netdev_priv(netdev);
2318 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002319 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002320
2321 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002322 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002323
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002324 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2325
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002326 /* If the network stack called us with vid = 0 then
2327 * it is asking to receive priority tagged packets with
2328 * vlan id 0. Our HW receives them by default when configured
2329 * to receive untagged packets so there is no need to add an
2330 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002331 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002332 if (vid)
2333 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002334
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002335 if (!ret && (vid < VLAN_N_VID))
2336 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002337
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002338 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002339}
2340
2341/**
2342 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2343 * @netdev: network interface to be adjusted
2344 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002345 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002346 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002347 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002348#ifdef I40E_FCOE
2349int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2350 __always_unused __be16 proto, u16 vid)
2351#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002352static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2353 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002354#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002355{
2356 struct i40e_netdev_priv *np = netdev_priv(netdev);
2357 struct i40e_vsi *vsi = np->vsi;
2358
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002359 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2360
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002361 /* return code is ignored as there is nothing a user
2362 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002363 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002364 */
2365 i40e_vsi_kill_vlan(vsi, vid);
2366
2367 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002368
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002369 return 0;
2370}
2371
2372/**
2373 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2374 * @vsi: the vsi being brought back up
2375 **/
2376static void i40e_restore_vlan(struct i40e_vsi *vsi)
2377{
2378 u16 vid;
2379
2380 if (!vsi->netdev)
2381 return;
2382
2383 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2384
2385 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2386 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2387 vid);
2388}
2389
2390/**
2391 * i40e_vsi_add_pvid - Add pvid for the VSI
2392 * @vsi: the vsi being adjusted
2393 * @vid: the vlan id to set as a PVID
2394 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002395int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002396{
2397 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002398 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002399
2400 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2401 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002402 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2403 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002404 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002405
2406 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002407 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002408 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2409 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002410 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002411 "add pvid failed, err %s aq_err %s\n",
2412 i40e_stat_str(&vsi->back->hw, ret),
2413 i40e_aq_str(&vsi->back->hw,
2414 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002415 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002416 }
2417
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002418 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002419}
2420
2421/**
2422 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2423 * @vsi: the vsi being adjusted
2424 *
2425 * Just use the vlan_rx_register() service to put it back to normal
2426 **/
2427void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2428{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002429 i40e_vlan_stripping_disable(vsi);
2430
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002431 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002432}
2433
2434/**
2435 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2436 * @vsi: ptr to the VSI
2437 *
2438 * If this function returns with an error, then it's possible one or
2439 * more of the rings is populated (while the rest are not). It is the
2440 * callers duty to clean those orphaned rings.
2441 *
2442 * Return 0 on success, negative on failure
2443 **/
2444static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2445{
2446 int i, err = 0;
2447
2448 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002449 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002450
2451 return err;
2452}
2453
2454/**
2455 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2456 * @vsi: ptr to the VSI
2457 *
2458 * Free VSI's transmit software resources
2459 **/
2460static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2461{
2462 int i;
2463
Greg Rose8e9dca52013-12-18 13:45:53 +00002464 if (!vsi->tx_rings)
2465 return;
2466
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002467 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002468 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002469 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002470}
2471
2472/**
2473 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2474 * @vsi: ptr to the VSI
2475 *
2476 * If this function returns with an error, then it's possible one or
2477 * more of the rings is populated (while the rest are not). It is the
2478 * callers duty to clean those orphaned rings.
2479 *
2480 * Return 0 on success, negative on failure
2481 **/
2482static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2483{
2484 int i, err = 0;
2485
2486 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002487 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002488#ifdef I40E_FCOE
2489 i40e_fcoe_setup_ddp_resources(vsi);
2490#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002491 return err;
2492}
2493
2494/**
2495 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2496 * @vsi: ptr to the VSI
2497 *
2498 * Free all receive software resources
2499 **/
2500static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2501{
2502 int i;
2503
Greg Rose8e9dca52013-12-18 13:45:53 +00002504 if (!vsi->rx_rings)
2505 return;
2506
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002507 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002508 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002509 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002510#ifdef I40E_FCOE
2511 i40e_fcoe_free_ddp_resources(vsi);
2512#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002513}
2514
2515/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002516 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2517 * @ring: The Tx ring to configure
2518 *
2519 * This enables/disables XPS for a given Tx descriptor ring
2520 * based on the TCs enabled for the VSI that ring belongs to.
2521 **/
2522static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2523{
2524 struct i40e_vsi *vsi = ring->vsi;
2525 cpumask_var_t mask;
2526
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002527 if (!ring->q_vector || !ring->netdev)
2528 return;
2529
2530 /* Single TC mode enable XPS */
2531 if (vsi->tc_config.numtc <= 1) {
2532 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002533 netif_set_xps_queue(ring->netdev,
2534 &ring->q_vector->affinity_mask,
2535 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002536 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2537 /* Disable XPS to allow selection based on TC */
2538 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2539 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2540 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002541 }
2542}
2543
2544/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002545 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2546 * @ring: The Tx ring to configure
2547 *
2548 * Configure the Tx descriptor ring in the HMC context.
2549 **/
2550static int i40e_configure_tx_ring(struct i40e_ring *ring)
2551{
2552 struct i40e_vsi *vsi = ring->vsi;
2553 u16 pf_q = vsi->base_queue + ring->queue_index;
2554 struct i40e_hw *hw = &vsi->back->hw;
2555 struct i40e_hmc_obj_txq tx_ctx;
2556 i40e_status err = 0;
2557 u32 qtx_ctl = 0;
2558
2559 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002560 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002561 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2562 ring->atr_count = 0;
2563 } else {
2564 ring->atr_sample_rate = 0;
2565 }
2566
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002567 /* configure XPS */
2568 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002569
2570 /* clear the context structure first */
2571 memset(&tx_ctx, 0, sizeof(tx_ctx));
2572
2573 tx_ctx.new_context = 1;
2574 tx_ctx.base = (ring->dma / 128);
2575 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002576 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2577 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002578#ifdef I40E_FCOE
2579 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2580#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002581 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002582 /* FDIR VSI tx ring can still use RS bit and writebacks */
2583 if (vsi->type != I40E_VSI_FDIR)
2584 tx_ctx.head_wb_ena = 1;
2585 tx_ctx.head_wb_addr = ring->dma +
2586 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002587
2588 /* As part of VSI creation/update, FW allocates certain
2589 * Tx arbitration queue sets for each TC enabled for
2590 * the VSI. The FW returns the handles to these queue
2591 * sets as part of the response buffer to Add VSI,
2592 * Update VSI, etc. AQ commands. It is expected that
2593 * these queue set handles be associated with the Tx
2594 * queues by the driver as part of the TX queue context
2595 * initialization. This has to be done regardless of
2596 * DCB as by default everything is mapped to TC0.
2597 */
2598 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2599 tx_ctx.rdylist_act = 0;
2600
2601 /* clear the context in the HMC */
2602 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2603 if (err) {
2604 dev_info(&vsi->back->pdev->dev,
2605 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2606 ring->queue_index, pf_q, err);
2607 return -ENOMEM;
2608 }
2609
2610 /* set the context in the HMC */
2611 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2612 if (err) {
2613 dev_info(&vsi->back->pdev->dev,
2614 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2615 ring->queue_index, pf_q, err);
2616 return -ENOMEM;
2617 }
2618
2619 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002620 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002621 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002622 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2623 I40E_QTX_CTL_VFVM_INDX_MASK;
2624 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002625 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002626 }
2627
Shannon Nelson13fd9772013-09-28 07:14:19 +00002628 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2629 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002630 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2631 i40e_flush(hw);
2632
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002633 /* cache tail off for easier writes later */
2634 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2635
2636 return 0;
2637}
2638
2639/**
2640 * i40e_configure_rx_ring - Configure a receive ring context
2641 * @ring: The Rx ring to configure
2642 *
2643 * Configure the Rx descriptor ring in the HMC context.
2644 **/
2645static int i40e_configure_rx_ring(struct i40e_ring *ring)
2646{
2647 struct i40e_vsi *vsi = ring->vsi;
2648 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2649 u16 pf_q = vsi->base_queue + ring->queue_index;
2650 struct i40e_hw *hw = &vsi->back->hw;
2651 struct i40e_hmc_obj_rxq rx_ctx;
2652 i40e_status err = 0;
2653
2654 ring->state = 0;
2655
2656 /* clear the context structure first */
2657 memset(&rx_ctx, 0, sizeof(rx_ctx));
2658
2659 ring->rx_buf_len = vsi->rx_buf_len;
2660 ring->rx_hdr_len = vsi->rx_hdr_len;
2661
2662 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2663 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2664
2665 rx_ctx.base = (ring->dma / 128);
2666 rx_ctx.qlen = ring->count;
2667
2668 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2669 set_ring_16byte_desc_enabled(ring);
2670 rx_ctx.dsize = 0;
2671 } else {
2672 rx_ctx.dsize = 1;
2673 }
2674
2675 rx_ctx.dtype = vsi->dtype;
2676 if (vsi->dtype) {
2677 set_ring_ps_enabled(ring);
2678 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2679 I40E_RX_SPLIT_IP |
2680 I40E_RX_SPLIT_TCP_UDP |
2681 I40E_RX_SPLIT_SCTP;
2682 } else {
2683 rx_ctx.hsplit_0 = 0;
2684 }
2685
2686 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2687 (chain_len * ring->rx_buf_len));
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002688 if (hw->revision_id == 0)
2689 rx_ctx.lrxqthresh = 0;
2690 else
2691 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002692 rx_ctx.crcstrip = 1;
2693 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07002694 /* this controls whether VLAN is stripped from inner headers */
2695 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07002696#ifdef I40E_FCOE
2697 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2698#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002699 /* set the prefena field to 1 because the manual says to */
2700 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002701
2702 /* clear the context in the HMC */
2703 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2704 if (err) {
2705 dev_info(&vsi->back->pdev->dev,
2706 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2707 ring->queue_index, pf_q, err);
2708 return -ENOMEM;
2709 }
2710
2711 /* set the context in the HMC */
2712 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2713 if (err) {
2714 dev_info(&vsi->back->pdev->dev,
2715 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2716 ring->queue_index, pf_q, err);
2717 return -ENOMEM;
2718 }
2719
2720 /* cache tail for quicker writes, and clear the reg before use */
2721 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2722 writel(0, ring->tail);
2723
Mitch Williamsa132af22015-01-24 09:58:35 +00002724 if (ring_is_ps_enabled(ring)) {
2725 i40e_alloc_rx_headers(ring);
2726 i40e_alloc_rx_buffers_ps(ring, I40E_DESC_UNUSED(ring));
2727 } else {
2728 i40e_alloc_rx_buffers_1buf(ring, I40E_DESC_UNUSED(ring));
2729 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002730
2731 return 0;
2732}
2733
2734/**
2735 * i40e_vsi_configure_tx - Configure the VSI for Tx
2736 * @vsi: VSI structure describing this set of rings and resources
2737 *
2738 * Configure the Tx VSI for operation.
2739 **/
2740static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2741{
2742 int err = 0;
2743 u16 i;
2744
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002745 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2746 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002747
2748 return err;
2749}
2750
2751/**
2752 * i40e_vsi_configure_rx - Configure the VSI for Rx
2753 * @vsi: the VSI being configured
2754 *
2755 * Configure the Rx VSI for operation.
2756 **/
2757static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2758{
2759 int err = 0;
2760 u16 i;
2761
2762 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2763 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2764 + ETH_FCS_LEN + VLAN_HLEN;
2765 else
2766 vsi->max_frame = I40E_RXBUFFER_2048;
2767
2768 /* figure out correct receive buffer length */
2769 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2770 I40E_FLAG_RX_PS_ENABLED)) {
2771 case I40E_FLAG_RX_1BUF_ENABLED:
2772 vsi->rx_hdr_len = 0;
2773 vsi->rx_buf_len = vsi->max_frame;
2774 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2775 break;
2776 case I40E_FLAG_RX_PS_ENABLED:
2777 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2778 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2779 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2780 break;
2781 default:
2782 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2783 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2784 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2785 break;
2786 }
2787
Vasu Dev38e00432014-08-01 13:27:03 -07002788#ifdef I40E_FCOE
2789 /* setup rx buffer for FCoE */
2790 if ((vsi->type == I40E_VSI_FCOE) &&
2791 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
2792 vsi->rx_hdr_len = 0;
2793 vsi->rx_buf_len = I40E_RXBUFFER_3072;
2794 vsi->max_frame = I40E_RXBUFFER_3072;
2795 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2796 }
2797
2798#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002799 /* round up for the chip's needs */
2800 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002801 BIT_ULL(I40E_RXQ_CTX_HBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002802 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002803 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002804
2805 /* set up individual rings */
2806 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002807 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002808
2809 return err;
2810}
2811
2812/**
2813 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2814 * @vsi: ptr to the VSI
2815 **/
2816static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2817{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002818 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002819 u16 qoffset, qcount;
2820 int i, n;
2821
Parikh, Neeravcd238a32015-02-21 06:43:37 +00002822 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2823 /* Reset the TC information */
2824 for (i = 0; i < vsi->num_queue_pairs; i++) {
2825 rx_ring = vsi->rx_rings[i];
2826 tx_ring = vsi->tx_rings[i];
2827 rx_ring->dcb_tc = 0;
2828 tx_ring->dcb_tc = 0;
2829 }
2830 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002831
2832 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002833 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002834 continue;
2835
2836 qoffset = vsi->tc_config.tc_info[n].qoffset;
2837 qcount = vsi->tc_config.tc_info[n].qcount;
2838 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002839 rx_ring = vsi->rx_rings[i];
2840 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002841 rx_ring->dcb_tc = n;
2842 tx_ring->dcb_tc = n;
2843 }
2844 }
2845}
2846
2847/**
2848 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2849 * @vsi: ptr to the VSI
2850 **/
2851static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2852{
2853 if (vsi->netdev)
2854 i40e_set_rx_mode(vsi->netdev);
2855}
2856
2857/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002858 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
2859 * @vsi: Pointer to the targeted VSI
2860 *
2861 * This function replays the hlist on the hw where all the SB Flow Director
2862 * filters were saved.
2863 **/
2864static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
2865{
2866 struct i40e_fdir_filter *filter;
2867 struct i40e_pf *pf = vsi->back;
2868 struct hlist_node *node;
2869
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002870 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2871 return;
2872
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002873 hlist_for_each_entry_safe(filter, node,
2874 &pf->fdir_filter_list, fdir_node) {
2875 i40e_add_del_fdir(vsi, filter, true);
2876 }
2877}
2878
2879/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002880 * i40e_vsi_configure - Set up the VSI for action
2881 * @vsi: the VSI being configured
2882 **/
2883static int i40e_vsi_configure(struct i40e_vsi *vsi)
2884{
2885 int err;
2886
2887 i40e_set_vsi_rx_mode(vsi);
2888 i40e_restore_vlan(vsi);
2889 i40e_vsi_config_dcb_rings(vsi);
2890 err = i40e_vsi_configure_tx(vsi);
2891 if (!err)
2892 err = i40e_vsi_configure_rx(vsi);
2893
2894 return err;
2895}
2896
2897/**
2898 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2899 * @vsi: the VSI being configured
2900 **/
2901static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2902{
2903 struct i40e_pf *pf = vsi->back;
2904 struct i40e_q_vector *q_vector;
2905 struct i40e_hw *hw = &pf->hw;
2906 u16 vector;
2907 int i, q;
2908 u32 val;
2909 u32 qp;
2910
2911 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2912 * and PFINT_LNKLSTn registers, e.g.:
2913 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2914 */
2915 qp = vsi->base_queue;
2916 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002917 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2918 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002919 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2920 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2921 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2922 q_vector->rx.itr);
2923 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2924 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2925 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2926 q_vector->tx.itr);
2927
2928 /* Linked list for the queuepairs assigned to this vector */
2929 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2930 for (q = 0; q < q_vector->num_ringpairs; q++) {
2931 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2932 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2933 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2934 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2935 (I40E_QUEUE_TYPE_TX
2936 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2937
2938 wr32(hw, I40E_QINT_RQCTL(qp), val);
2939
2940 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2941 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2942 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2943 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2944 (I40E_QUEUE_TYPE_RX
2945 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2946
2947 /* Terminate the linked list */
2948 if (q == (q_vector->num_ringpairs - 1))
2949 val |= (I40E_QUEUE_END_OF_LIST
2950 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2951
2952 wr32(hw, I40E_QINT_TQCTL(qp), val);
2953 qp++;
2954 }
2955 }
2956
2957 i40e_flush(hw);
2958}
2959
2960/**
2961 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2962 * @hw: ptr to the hardware info
2963 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00002964static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002965{
Jacob Kellerab437b52014-12-14 01:55:08 +00002966 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002967 u32 val;
2968
2969 /* clear things first */
2970 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2971 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2972
2973 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2974 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2975 I40E_PFINT_ICR0_ENA_GRST_MASK |
2976 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2977 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002978 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2979 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2980 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2981
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04002982 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
2983 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
2984
Jacob Kellerab437b52014-12-14 01:55:08 +00002985 if (pf->flags & I40E_FLAG_PTP)
2986 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
2987
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002988 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2989
2990 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002991 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2992 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002993
2994 /* OTHER_ITR_IDX = 0 */
2995 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2996}
2997
2998/**
2999 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3000 * @vsi: the VSI being configured
3001 **/
3002static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3003{
Alexander Duyck493fb302013-09-28 07:01:44 +00003004 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003005 struct i40e_pf *pf = vsi->back;
3006 struct i40e_hw *hw = &pf->hw;
3007 u32 val;
3008
3009 /* set the ITR configuration */
3010 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
3011 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3012 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
3013 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
3014 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3015 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3016
Jacob Kellerab437b52014-12-14 01:55:08 +00003017 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003018
3019 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3020 wr32(hw, I40E_PFINT_LNKLST0, 0);
3021
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003022 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003023 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3024 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3025 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3026
3027 wr32(hw, I40E_QINT_RQCTL(0), val);
3028
3029 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3030 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3031 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3032
3033 wr32(hw, I40E_QINT_TQCTL(0), val);
3034 i40e_flush(hw);
3035}
3036
3037/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003038 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3039 * @pf: board private structure
3040 **/
3041void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3042{
3043 struct i40e_hw *hw = &pf->hw;
3044
3045 wr32(hw, I40E_PFINT_DYN_CTL0,
3046 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3047 i40e_flush(hw);
3048}
3049
3050/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003051 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3052 * @pf: board private structure
3053 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00003054void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003055{
3056 struct i40e_hw *hw = &pf->hw;
3057 u32 val;
3058
3059 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
3060 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3061 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3062
3063 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3064 i40e_flush(hw);
3065}
3066
3067/**
Carolyn Wyborny5c2cebd2014-06-04 01:23:18 +00003068 * i40e_irq_dynamic_disable - Disable default interrupt generation settings
3069 * @vsi: pointer to a vsi
Greg Rose03147772015-01-24 09:58:29 +00003070 * @vector: disable a particular Hw Interrupt vector
Carolyn Wyborny5c2cebd2014-06-04 01:23:18 +00003071 **/
3072void i40e_irq_dynamic_disable(struct i40e_vsi *vsi, int vector)
3073{
3074 struct i40e_pf *pf = vsi->back;
3075 struct i40e_hw *hw = &pf->hw;
3076 u32 val;
3077
3078 val = I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT;
3079 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
3080 i40e_flush(hw);
3081}
3082
3083/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003084 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3085 * @irq: interrupt number
3086 * @data: pointer to a q_vector
3087 **/
3088static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3089{
3090 struct i40e_q_vector *q_vector = data;
3091
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003092 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003093 return IRQ_HANDLED;
3094
3095 napi_schedule(&q_vector->napi);
3096
3097 return IRQ_HANDLED;
3098}
3099
3100/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003101 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3102 * @vsi: the VSI being configured
3103 * @basename: name for the vector
3104 *
3105 * Allocates MSI-X vectors and requests interrupts from the kernel.
3106 **/
3107static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3108{
3109 int q_vectors = vsi->num_q_vectors;
3110 struct i40e_pf *pf = vsi->back;
3111 int base = vsi->base_vector;
3112 int rx_int_idx = 0;
3113 int tx_int_idx = 0;
3114 int vector, err;
3115
3116 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003117 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003118
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003119 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003120 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3121 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3122 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003123 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003124 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3125 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003126 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003127 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3128 "%s-%s-%d", basename, "tx", tx_int_idx++);
3129 } else {
3130 /* skip this unused q_vector */
3131 continue;
3132 }
3133 err = request_irq(pf->msix_entries[base + vector].vector,
3134 vsi->irq_handler,
3135 0,
3136 q_vector->name,
3137 q_vector);
3138 if (err) {
3139 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003140 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003141 goto free_queue_irqs;
3142 }
3143 /* assign the mask for this irq */
3144 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3145 &q_vector->affinity_mask);
3146 }
3147
Shannon Nelson63741842014-04-23 04:50:16 +00003148 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003149 return 0;
3150
3151free_queue_irqs:
3152 while (vector) {
3153 vector--;
3154 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3155 NULL);
3156 free_irq(pf->msix_entries[base + vector].vector,
3157 &(vsi->q_vectors[vector]));
3158 }
3159 return err;
3160}
3161
3162/**
3163 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3164 * @vsi: the VSI being un-configured
3165 **/
3166static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3167{
3168 struct i40e_pf *pf = vsi->back;
3169 struct i40e_hw *hw = &pf->hw;
3170 int base = vsi->base_vector;
3171 int i;
3172
3173 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003174 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3175 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003176 }
3177
3178 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3179 for (i = vsi->base_vector;
3180 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3181 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3182
3183 i40e_flush(hw);
3184 for (i = 0; i < vsi->num_q_vectors; i++)
3185 synchronize_irq(pf->msix_entries[i + base].vector);
3186 } else {
3187 /* Legacy and MSI mode - this stops all interrupt handling */
3188 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3189 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3190 i40e_flush(hw);
3191 synchronize_irq(pf->pdev->irq);
3192 }
3193}
3194
3195/**
3196 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3197 * @vsi: the VSI being configured
3198 **/
3199static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3200{
3201 struct i40e_pf *pf = vsi->back;
3202 int i;
3203
3204 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003205 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003206 i40e_irq_dynamic_enable(vsi, i);
3207 } else {
3208 i40e_irq_dynamic_enable_icr0(pf);
3209 }
3210
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003211 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003212 return 0;
3213}
3214
3215/**
3216 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3217 * @pf: board private structure
3218 **/
3219static void i40e_stop_misc_vector(struct i40e_pf *pf)
3220{
3221 /* Disable ICR 0 */
3222 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3223 i40e_flush(&pf->hw);
3224}
3225
3226/**
3227 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3228 * @irq: interrupt number
3229 * @data: pointer to a q_vector
3230 *
3231 * This is the handler used for all MSI/Legacy interrupts, and deals
3232 * with both queue and non-queue interrupts. This is also used in
3233 * MSIX mode to handle the non-queue interrupts.
3234 **/
3235static irqreturn_t i40e_intr(int irq, void *data)
3236{
3237 struct i40e_pf *pf = (struct i40e_pf *)data;
3238 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003239 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003240 u32 icr0, icr0_remaining;
3241 u32 val, ena_mask;
3242
3243 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003244 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003245
Shannon Nelson116a57d2013-09-28 07:13:59 +00003246 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3247 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003248 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003249
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003250 /* if interrupt but no bits showing, must be SWINT */
3251 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3252 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3253 pf->sw_int_count++;
3254
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003255 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3256 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3257 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3258 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3259 dev_info(&pf->pdev->dev, "cleared PE_CRITERR\n");
3260 }
3261
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003262 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3263 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
3264
3265 /* temporarily disable queue cause for NAPI processing */
3266 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003267
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003268 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3269 wr32(hw, I40E_QINT_RQCTL(0), qval);
3270
3271 qval = rd32(hw, I40E_QINT_TQCTL(0));
3272 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3273 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003274
3275 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00003276 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003277 }
3278
3279 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3280 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3281 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
3282 }
3283
3284 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3285 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3286 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3287 }
3288
3289 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3290 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3291 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3292 }
3293
3294 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3295 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3296 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3297 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3298 val = rd32(hw, I40E_GLGEN_RSTAT);
3299 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3300 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003301 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003302 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003303 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003304 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003305 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003306 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003307 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003308 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003309 }
3310
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003311 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3312 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3313 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003314 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3315 rd32(hw, I40E_PFHMC_ERRORINFO),
3316 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003317 }
3318
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003319 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3320 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3321
3322 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003323 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003324 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003325 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003326 }
3327
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003328 /* If a critical error is pending we have no choice but to reset the
3329 * device.
3330 * Report and mask out any remaining unexpected interrupts.
3331 */
3332 icr0_remaining = icr0 & ena_mask;
3333 if (icr0_remaining) {
3334 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3335 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003336 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003337 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003338 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003339 dev_info(&pf->pdev->dev, "device will be reset\n");
3340 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3341 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003342 }
3343 ena_mask &= ~icr0_remaining;
3344 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003345 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003346
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003347enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003348 /* re-enable interrupt causes */
3349 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003350 if (!test_bit(__I40E_DOWN, &pf->state)) {
3351 i40e_service_event_schedule(pf);
3352 i40e_irq_dynamic_enable_icr0(pf);
3353 }
3354
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003355 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003356}
3357
3358/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003359 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3360 * @tx_ring: tx ring to clean
3361 * @budget: how many cleans we're allowed
3362 *
3363 * Returns true if there's any budget left (e.g. the clean is finished)
3364 **/
3365static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3366{
3367 struct i40e_vsi *vsi = tx_ring->vsi;
3368 u16 i = tx_ring->next_to_clean;
3369 struct i40e_tx_buffer *tx_buf;
3370 struct i40e_tx_desc *tx_desc;
3371
3372 tx_buf = &tx_ring->tx_bi[i];
3373 tx_desc = I40E_TX_DESC(tx_ring, i);
3374 i -= tx_ring->count;
3375
3376 do {
3377 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3378
3379 /* if next_to_watch is not set then there is no work pending */
3380 if (!eop_desc)
3381 break;
3382
3383 /* prevent any other reads prior to eop_desc */
3384 read_barrier_depends();
3385
3386 /* if the descriptor isn't done, no work yet to do */
3387 if (!(eop_desc->cmd_type_offset_bsz &
3388 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3389 break;
3390
3391 /* clear next_to_watch to prevent false hangs */
3392 tx_buf->next_to_watch = NULL;
3393
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003394 tx_desc->buffer_addr = 0;
3395 tx_desc->cmd_type_offset_bsz = 0;
3396 /* move past filter desc */
3397 tx_buf++;
3398 tx_desc++;
3399 i++;
3400 if (unlikely(!i)) {
3401 i -= tx_ring->count;
3402 tx_buf = tx_ring->tx_bi;
3403 tx_desc = I40E_TX_DESC(tx_ring, 0);
3404 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003405 /* unmap skb header data */
3406 dma_unmap_single(tx_ring->dev,
3407 dma_unmap_addr(tx_buf, dma),
3408 dma_unmap_len(tx_buf, len),
3409 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003410 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3411 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003412
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003413 tx_buf->raw_buf = NULL;
3414 tx_buf->tx_flags = 0;
3415 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003416 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003417 tx_desc->buffer_addr = 0;
3418 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003419
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003420 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003421 tx_buf++;
3422 tx_desc++;
3423 i++;
3424 if (unlikely(!i)) {
3425 i -= tx_ring->count;
3426 tx_buf = tx_ring->tx_bi;
3427 tx_desc = I40E_TX_DESC(tx_ring, 0);
3428 }
3429
3430 /* update budget accounting */
3431 budget--;
3432 } while (likely(budget));
3433
3434 i += tx_ring->count;
3435 tx_ring->next_to_clean = i;
3436
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003437 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003438 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003439
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003440 return budget > 0;
3441}
3442
3443/**
3444 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3445 * @irq: interrupt number
3446 * @data: pointer to a q_vector
3447 **/
3448static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3449{
3450 struct i40e_q_vector *q_vector = data;
3451 struct i40e_vsi *vsi;
3452
3453 if (!q_vector->tx.ring)
3454 return IRQ_HANDLED;
3455
3456 vsi = q_vector->tx.ring->vsi;
3457 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3458
3459 return IRQ_HANDLED;
3460}
3461
3462/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003463 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003464 * @vsi: the VSI being configured
3465 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003466 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003467 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003468static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003469{
Alexander Duyck493fb302013-09-28 07:01:44 +00003470 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003471 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3472 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003473
3474 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003475 tx_ring->next = q_vector->tx.ring;
3476 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003477 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003478
3479 rx_ring->q_vector = q_vector;
3480 rx_ring->next = q_vector->rx.ring;
3481 q_vector->rx.ring = rx_ring;
3482 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003483}
3484
3485/**
3486 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3487 * @vsi: the VSI being configured
3488 *
3489 * This function maps descriptor rings to the queue-specific vectors
3490 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3491 * one vector per queue pair, but on a constrained vector budget, we
3492 * group the queue pairs as "efficiently" as possible.
3493 **/
3494static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3495{
3496 int qp_remaining = vsi->num_queue_pairs;
3497 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003498 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003499 int v_start = 0;
3500 int qp_idx = 0;
3501
3502 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3503 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003504 * It is also important to go through all the vectors available to be
3505 * sure that if we don't use all the vectors, that the remaining vectors
3506 * are cleared. This is especially important when decreasing the
3507 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003508 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003509 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003510 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3511
3512 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3513
3514 q_vector->num_ringpairs = num_ringpairs;
3515
3516 q_vector->rx.count = 0;
3517 q_vector->tx.count = 0;
3518 q_vector->rx.ring = NULL;
3519 q_vector->tx.ring = NULL;
3520
3521 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003522 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003523 qp_idx++;
3524 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003525 }
3526 }
3527}
3528
3529/**
3530 * i40e_vsi_request_irq - Request IRQ from the OS
3531 * @vsi: the VSI being configured
3532 * @basename: name for the vector
3533 **/
3534static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3535{
3536 struct i40e_pf *pf = vsi->back;
3537 int err;
3538
3539 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3540 err = i40e_vsi_request_irq_msix(vsi, basename);
3541 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3542 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003543 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003544 else
3545 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003546 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003547
3548 if (err)
3549 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3550
3551 return err;
3552}
3553
3554#ifdef CONFIG_NET_POLL_CONTROLLER
3555/**
3556 * i40e_netpoll - A Polling 'interrupt'handler
3557 * @netdev: network interface device structure
3558 *
3559 * This is used by netconsole to send skbs without having to re-enable
3560 * interrupts. It's not called while the normal interrupt routine is executing.
3561 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003562#ifdef I40E_FCOE
3563void i40e_netpoll(struct net_device *netdev)
3564#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003565static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003566#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003567{
3568 struct i40e_netdev_priv *np = netdev_priv(netdev);
3569 struct i40e_vsi *vsi = np->vsi;
3570 struct i40e_pf *pf = vsi->back;
3571 int i;
3572
3573 /* if interface is down do nothing */
3574 if (test_bit(__I40E_DOWN, &vsi->state))
3575 return;
3576
3577 pf->flags |= I40E_FLAG_IN_NETPOLL;
3578 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3579 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003580 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003581 } else {
3582 i40e_intr(pf->pdev->irq, netdev);
3583 }
3584 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3585}
3586#endif
3587
3588/**
Neerav Parikh23527302014-06-03 23:50:15 +00003589 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3590 * @pf: the PF being configured
3591 * @pf_q: the PF queue
3592 * @enable: enable or disable state of the queue
3593 *
3594 * This routine will wait for the given Tx queue of the PF to reach the
3595 * enabled or disabled state.
3596 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3597 * multiple retries; else will return 0 in case of success.
3598 **/
3599static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3600{
3601 int i;
3602 u32 tx_reg;
3603
3604 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3605 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3606 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3607 break;
3608
Neerav Parikhf98a2002014-09-13 07:40:44 +00003609 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003610 }
3611 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3612 return -ETIMEDOUT;
3613
3614 return 0;
3615}
3616
3617/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003618 * i40e_vsi_control_tx - Start or stop a VSI's rings
3619 * @vsi: the VSI being configured
3620 * @enable: start or stop the rings
3621 **/
3622static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3623{
3624 struct i40e_pf *pf = vsi->back;
3625 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003626 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003627 u32 tx_reg;
3628
3629 pf_q = vsi->base_queue;
3630 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499a2014-04-23 04:50:03 +00003631
3632 /* warn the TX unit of coming changes */
3633 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3634 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003635 usleep_range(10, 20);
Matt Jared351499a2014-04-23 04:50:03 +00003636
Mitch Williams6c5ef622014-02-20 19:29:16 -08003637 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003638 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003639 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3640 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3641 break;
3642 usleep_range(1000, 2000);
3643 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003644 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003645 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003646 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003647
3648 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003649 if (enable) {
3650 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003651 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003652 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003653 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003654 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003655
3656 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003657 /* No waiting for the Tx queue to disable */
3658 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3659 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003660
3661 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003662 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3663 if (ret) {
3664 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003665 "VSI seid %d Tx ring %d %sable timeout\n",
3666 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003667 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003668 }
3669 }
3670
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003671 if (hw->revision_id == 0)
3672 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003673 return ret;
3674}
3675
3676/**
3677 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3678 * @pf: the PF being configured
3679 * @pf_q: the PF queue
3680 * @enable: enable or disable state of the queue
3681 *
3682 * This routine will wait for the given Rx queue of the PF to reach the
3683 * enabled or disabled state.
3684 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3685 * multiple retries; else will return 0 in case of success.
3686 **/
3687static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3688{
3689 int i;
3690 u32 rx_reg;
3691
3692 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3693 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3694 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3695 break;
3696
Neerav Parikhf98a2002014-09-13 07:40:44 +00003697 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003698 }
3699 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3700 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003701
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003702 return 0;
3703}
3704
3705/**
3706 * i40e_vsi_control_rx - Start or stop a VSI's rings
3707 * @vsi: the VSI being configured
3708 * @enable: start or stop the rings
3709 **/
3710static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3711{
3712 struct i40e_pf *pf = vsi->back;
3713 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003714 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003715 u32 rx_reg;
3716
3717 pf_q = vsi->base_queue;
3718 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003719 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003720 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003721 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3722 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3723 break;
3724 usleep_range(1000, 2000);
3725 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003726
Catherine Sullivan7c122002014-03-14 07:32:29 +00003727 /* Skip if the queue is already in the requested state */
3728 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3729 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003730
3731 /* turn on/off the queue */
3732 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003733 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003734 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003735 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003736 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3737
3738 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003739 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3740 if (ret) {
3741 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003742 "VSI seid %d Rx ring %d %sable timeout\n",
3743 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003744 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003745 }
3746 }
3747
Neerav Parikh23527302014-06-03 23:50:15 +00003748 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003749}
3750
3751/**
3752 * i40e_vsi_control_rings - Start or stop a VSI's rings
3753 * @vsi: the VSI being configured
3754 * @enable: start or stop the rings
3755 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003756int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003757{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003758 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003759
3760 /* do rx first for enable and last for disable */
3761 if (request) {
3762 ret = i40e_vsi_control_rx(vsi, request);
3763 if (ret)
3764 return ret;
3765 ret = i40e_vsi_control_tx(vsi, request);
3766 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003767 /* Ignore return value, we need to shutdown whatever we can */
3768 i40e_vsi_control_tx(vsi, request);
3769 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003770 }
3771
3772 return ret;
3773}
3774
3775/**
3776 * i40e_vsi_free_irq - Free the irq association with the OS
3777 * @vsi: the VSI being configured
3778 **/
3779static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3780{
3781 struct i40e_pf *pf = vsi->back;
3782 struct i40e_hw *hw = &pf->hw;
3783 int base = vsi->base_vector;
3784 u32 val, qp;
3785 int i;
3786
3787 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3788 if (!vsi->q_vectors)
3789 return;
3790
Shannon Nelson63741842014-04-23 04:50:16 +00003791 if (!vsi->irqs_ready)
3792 return;
3793
3794 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003795 for (i = 0; i < vsi->num_q_vectors; i++) {
3796 u16 vector = i + base;
3797
3798 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003799 if (!vsi->q_vectors[i] ||
3800 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003801 continue;
3802
3803 /* clear the affinity_mask in the IRQ descriptor */
3804 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3805 NULL);
3806 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003807 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003808
3809 /* Tear down the interrupt queue link list
3810 *
3811 * We know that they come in pairs and always
3812 * the Rx first, then the Tx. To clear the
3813 * link list, stick the EOL value into the
3814 * next_q field of the registers.
3815 */
3816 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3817 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3818 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3819 val |= I40E_QUEUE_END_OF_LIST
3820 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3821 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3822
3823 while (qp != I40E_QUEUE_END_OF_LIST) {
3824 u32 next;
3825
3826 val = rd32(hw, I40E_QINT_RQCTL(qp));
3827
3828 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3829 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3830 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3831 I40E_QINT_RQCTL_INTEVENT_MASK);
3832
3833 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3834 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3835
3836 wr32(hw, I40E_QINT_RQCTL(qp), val);
3837
3838 val = rd32(hw, I40E_QINT_TQCTL(qp));
3839
3840 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3841 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3842
3843 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3844 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3845 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3846 I40E_QINT_TQCTL_INTEVENT_MASK);
3847
3848 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3849 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3850
3851 wr32(hw, I40E_QINT_TQCTL(qp), val);
3852 qp = next;
3853 }
3854 }
3855 } else {
3856 free_irq(pf->pdev->irq, pf);
3857
3858 val = rd32(hw, I40E_PFINT_LNKLST0);
3859 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3860 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3861 val |= I40E_QUEUE_END_OF_LIST
3862 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3863 wr32(hw, I40E_PFINT_LNKLST0, val);
3864
3865 val = rd32(hw, I40E_QINT_RQCTL(qp));
3866 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3867 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3868 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3869 I40E_QINT_RQCTL_INTEVENT_MASK);
3870
3871 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3872 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3873
3874 wr32(hw, I40E_QINT_RQCTL(qp), val);
3875
3876 val = rd32(hw, I40E_QINT_TQCTL(qp));
3877
3878 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3879 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3880 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3881 I40E_QINT_TQCTL_INTEVENT_MASK);
3882
3883 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3884 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3885
3886 wr32(hw, I40E_QINT_TQCTL(qp), val);
3887 }
3888}
3889
3890/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003891 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3892 * @vsi: the VSI being configured
3893 * @v_idx: Index of vector to be freed
3894 *
3895 * This function frees the memory allocated to the q_vector. In addition if
3896 * NAPI is enabled it will delete any references to the NAPI struct prior
3897 * to freeing the q_vector.
3898 **/
3899static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3900{
3901 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003902 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003903
3904 if (!q_vector)
3905 return;
3906
3907 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003908 i40e_for_each_ring(ring, q_vector->tx)
3909 ring->q_vector = NULL;
3910
3911 i40e_for_each_ring(ring, q_vector->rx)
3912 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003913
3914 /* only VSI w/ an associated netdev is set up w/ NAPI */
3915 if (vsi->netdev)
3916 netif_napi_del(&q_vector->napi);
3917
3918 vsi->q_vectors[v_idx] = NULL;
3919
3920 kfree_rcu(q_vector, rcu);
3921}
3922
3923/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003924 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3925 * @vsi: the VSI being un-configured
3926 *
3927 * This frees the memory allocated to the q_vectors and
3928 * deletes references to the NAPI struct.
3929 **/
3930static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3931{
3932 int v_idx;
3933
Alexander Duyck493fb302013-09-28 07:01:44 +00003934 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3935 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003936}
3937
3938/**
3939 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3940 * @pf: board private structure
3941 **/
3942static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3943{
3944 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3945 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3946 pci_disable_msix(pf->pdev);
3947 kfree(pf->msix_entries);
3948 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00003949 kfree(pf->irq_pile);
3950 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003951 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3952 pci_disable_msi(pf->pdev);
3953 }
3954 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3955}
3956
3957/**
3958 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3959 * @pf: board private structure
3960 *
3961 * We go through and clear interrupt specific resources and reset the structure
3962 * to pre-load conditions
3963 **/
3964static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3965{
3966 int i;
3967
Shannon Nelsone1477582015-02-21 06:44:33 +00003968 i40e_stop_misc_vector(pf);
3969 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3970 synchronize_irq(pf->msix_entries[0].vector);
3971 free_irq(pf->msix_entries[0].vector, pf);
3972 }
3973
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003974 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00003975 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003976 if (pf->vsi[i])
3977 i40e_vsi_free_q_vectors(pf->vsi[i]);
3978 i40e_reset_interrupt_capability(pf);
3979}
3980
3981/**
3982 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3983 * @vsi: the VSI being configured
3984 **/
3985static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3986{
3987 int q_idx;
3988
3989 if (!vsi->netdev)
3990 return;
3991
3992 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003993 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003994}
3995
3996/**
3997 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3998 * @vsi: the VSI being configured
3999 **/
4000static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4001{
4002 int q_idx;
4003
4004 if (!vsi->netdev)
4005 return;
4006
4007 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004008 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004009}
4010
4011/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004012 * i40e_vsi_close - Shut down a VSI
4013 * @vsi: the vsi to be quelled
4014 **/
4015static void i40e_vsi_close(struct i40e_vsi *vsi)
4016{
4017 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4018 i40e_down(vsi);
4019 i40e_vsi_free_irq(vsi);
4020 i40e_vsi_free_tx_resources(vsi);
4021 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004022 vsi->current_netdev_flags = 0;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004023}
4024
4025/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004026 * i40e_quiesce_vsi - Pause a given VSI
4027 * @vsi: the VSI being paused
4028 **/
4029static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4030{
4031 if (test_bit(__I40E_DOWN, &vsi->state))
4032 return;
4033
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004034 /* No need to disable FCoE VSI when Tx suspended */
4035 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4036 vsi->type == I40E_VSI_FCOE) {
4037 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004038 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004039 return;
4040 }
4041
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004042 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004043 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004044 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004045 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004046 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004047}
4048
4049/**
4050 * i40e_unquiesce_vsi - Resume a given VSI
4051 * @vsi: the VSI being resumed
4052 **/
4053static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4054{
4055 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4056 return;
4057
4058 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4059 if (vsi->netdev && netif_running(vsi->netdev))
4060 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4061 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004062 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004063}
4064
4065/**
4066 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4067 * @pf: the PF
4068 **/
4069static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4070{
4071 int v;
4072
Mitch Williams505682c2014-05-20 08:01:37 +00004073 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004074 if (pf->vsi[v])
4075 i40e_quiesce_vsi(pf->vsi[v]);
4076 }
4077}
4078
4079/**
4080 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4081 * @pf: the PF
4082 **/
4083static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4084{
4085 int v;
4086
Mitch Williams505682c2014-05-20 08:01:37 +00004087 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004088 if (pf->vsi[v])
4089 i40e_unquiesce_vsi(pf->vsi[v]);
4090 }
4091}
4092
Neerav Parikh69129dc2014-11-12 00:18:46 +00004093#ifdef CONFIG_I40E_DCB
4094/**
4095 * i40e_vsi_wait_txq_disabled - Wait for VSI's queues to be disabled
4096 * @vsi: the VSI being configured
4097 *
4098 * This function waits for the given VSI's Tx queues to be disabled.
4099 **/
4100static int i40e_vsi_wait_txq_disabled(struct i40e_vsi *vsi)
4101{
4102 struct i40e_pf *pf = vsi->back;
4103 int i, pf_q, ret;
4104
4105 pf_q = vsi->base_queue;
4106 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4107 /* Check and wait for the disable status of the queue */
4108 ret = i40e_pf_txq_wait(pf, pf_q, false);
4109 if (ret) {
4110 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004111 "VSI seid %d Tx ring %d disable timeout\n",
4112 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004113 return ret;
4114 }
4115 }
4116
4117 return 0;
4118}
4119
4120/**
4121 * i40e_pf_wait_txq_disabled - Wait for all queues of PF VSIs to be disabled
4122 * @pf: the PF
4123 *
4124 * This function waits for the Tx queues to be in disabled state for all the
4125 * VSIs that are managed by this PF.
4126 **/
4127static int i40e_pf_wait_txq_disabled(struct i40e_pf *pf)
4128{
4129 int v, ret = 0;
4130
4131 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004132 /* No need to wait for FCoE VSI queues */
4133 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh69129dc2014-11-12 00:18:46 +00004134 ret = i40e_vsi_wait_txq_disabled(pf->vsi[v]);
4135 if (ret)
4136 break;
4137 }
4138 }
4139
4140 return ret;
4141}
4142
4143#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004144
4145/**
4146 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4147 * @q_idx: TX queue number
4148 * @vsi: Pointer to VSI struct
4149 *
4150 * This function checks specified queue for given VSI. Detects hung condition.
4151 * Sets hung bit since it is two step process. Before next run of service task
4152 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4153 * hung condition remain unchanged and during subsequent run, this function
4154 * issues SW interrupt to recover from hung condition.
4155 **/
4156static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4157{
4158 struct i40e_ring *tx_ring = NULL;
4159 struct i40e_pf *pf;
4160 u32 head, val, tx_pending;
4161 int i;
4162
4163 pf = vsi->back;
4164
4165 /* now that we have an index, find the tx_ring struct */
4166 for (i = 0; i < vsi->num_queue_pairs; i++) {
4167 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4168 if (q_idx == vsi->tx_rings[i]->queue_index) {
4169 tx_ring = vsi->tx_rings[i];
4170 break;
4171 }
4172 }
4173 }
4174
4175 if (!tx_ring)
4176 return;
4177
4178 /* Read interrupt register */
4179 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4180 val = rd32(&pf->hw,
4181 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4182 tx_ring->vsi->base_vector - 1));
4183 else
4184 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4185
4186 head = i40e_get_head(tx_ring);
4187
4188 tx_pending = i40e_get_tx_pending(tx_ring);
4189
4190 /* Interrupts are disabled and TX pending is non-zero,
4191 * trigger the SW interrupt (don't wait). Worst case
4192 * there will be one extra interrupt which may result
4193 * into not cleaning any queues because queues are cleaned.
4194 */
4195 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4196 i40e_force_wb(vsi, tx_ring->q_vector);
4197}
4198
4199/**
4200 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4201 * @pf: pointer to PF struct
4202 *
4203 * LAN VSI has netdev and netdev has TX queues. This function is to check
4204 * each of those TX queues if they are hung, trigger recovery by issuing
4205 * SW interrupt.
4206 **/
4207static void i40e_detect_recover_hung(struct i40e_pf *pf)
4208{
4209 struct net_device *netdev;
4210 struct i40e_vsi *vsi;
4211 int i;
4212
4213 /* Only for LAN VSI */
4214 vsi = pf->vsi[pf->lan_vsi];
4215
4216 if (!vsi)
4217 return;
4218
4219 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4220 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4221 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4222 return;
4223
4224 /* Make sure type is MAIN VSI */
4225 if (vsi->type != I40E_VSI_MAIN)
4226 return;
4227
4228 netdev = vsi->netdev;
4229 if (!netdev)
4230 return;
4231
4232 /* Bail out if netif_carrier is not OK */
4233 if (!netif_carrier_ok(netdev))
4234 return;
4235
4236 /* Go thru' TX queues for netdev */
4237 for (i = 0; i < netdev->num_tx_queues; i++) {
4238 struct netdev_queue *q;
4239
4240 q = netdev_get_tx_queue(netdev, i);
4241 if (q)
4242 i40e_detect_recover_hung_queue(i, vsi);
4243 }
4244}
4245
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004246/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004247 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004248 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004249 *
4250 * Get TC map for ISCSI PF type that will include iSCSI TC
4251 * and LAN TC.
4252 **/
4253static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4254{
4255 struct i40e_dcb_app_priority_table app;
4256 struct i40e_hw *hw = &pf->hw;
4257 u8 enabled_tc = 1; /* TC0 is always enabled */
4258 u8 tc, i;
4259 /* Get the iSCSI APP TLV */
4260 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4261
4262 for (i = 0; i < dcbcfg->numapps; i++) {
4263 app = dcbcfg->app[i];
4264 if (app.selector == I40E_APP_SEL_TCPIP &&
4265 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4266 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004267 enabled_tc |= BIT_ULL(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004268 break;
4269 }
4270 }
4271
4272 return enabled_tc;
4273}
4274
4275/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004276 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4277 * @dcbcfg: the corresponding DCBx configuration structure
4278 *
4279 * Return the number of TCs from given DCBx configuration
4280 **/
4281static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4282{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004283 u8 num_tc = 0;
4284 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004285
4286 /* Scan the ETS Config Priority Table to find
4287 * traffic class enabled for a given priority
4288 * and use the traffic class index to get the
4289 * number of traffic classes enabled
4290 */
4291 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4292 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
4293 num_tc = dcbcfg->etscfg.prioritytable[i];
4294 }
4295
4296 /* Traffic class index starts from zero so
4297 * increment to return the actual count
4298 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004299 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004300}
4301
4302/**
4303 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4304 * @dcbcfg: the corresponding DCBx configuration structure
4305 *
4306 * Query the current DCB configuration and return the number of
4307 * traffic classes enabled from the given DCBX config
4308 **/
4309static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4310{
4311 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4312 u8 enabled_tc = 1;
4313 u8 i;
4314
4315 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004316 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004317
4318 return enabled_tc;
4319}
4320
4321/**
4322 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4323 * @pf: PF being queried
4324 *
4325 * Return number of traffic classes enabled for the given PF
4326 **/
4327static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4328{
4329 struct i40e_hw *hw = &pf->hw;
4330 u8 i, enabled_tc;
4331 u8 num_tc = 0;
4332 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4333
4334 /* If DCB is not enabled then always in single TC */
4335 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4336 return 1;
4337
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004338 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004339 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4340 return i40e_dcb_get_num_tc(dcbcfg);
4341
4342 /* MFP mode return count of enabled TCs for this PF */
4343 if (pf->hw.func_caps.iscsi)
4344 enabled_tc = i40e_get_iscsi_tc_map(pf);
4345 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004346 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004347
4348 /* At least have TC0 */
4349 enabled_tc = (enabled_tc ? enabled_tc : 0x1);
4350 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004351 if (enabled_tc & BIT_ULL(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004352 num_tc++;
4353 }
4354 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004355}
4356
4357/**
4358 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
4359 * @pf: PF being queried
4360 *
4361 * Return a bitmap for first enabled traffic class for this PF.
4362 **/
4363static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
4364{
4365 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
4366 u8 i = 0;
4367
4368 if (!enabled_tc)
4369 return 0x1; /* TC0 */
4370
4371 /* Find the first enabled TC */
4372 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004373 if (enabled_tc & BIT_ULL(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004374 break;
4375 }
4376
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004377 return BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004378}
4379
4380/**
4381 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4382 * @pf: PF being queried
4383 *
4384 * Return a bitmap for enabled traffic classes for this PF.
4385 **/
4386static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4387{
4388 /* If DCB is not enabled for this PF then just return default TC */
4389 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4390 return i40e_pf_get_default_tc(pf);
4391
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004392 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004393 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4394 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4395
Neerav Parikhfc51de92015-02-24 06:58:53 +00004396 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004397 if (pf->hw.func_caps.iscsi)
4398 return i40e_get_iscsi_tc_map(pf);
4399 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004400 return i40e_pf_get_default_tc(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004401}
4402
4403/**
4404 * i40e_vsi_get_bw_info - Query VSI BW Information
4405 * @vsi: the VSI being queried
4406 *
4407 * Returns 0 on success, negative value on failure
4408 **/
4409static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4410{
4411 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4412 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4413 struct i40e_pf *pf = vsi->back;
4414 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004415 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004416 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004417 int i;
4418
4419 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004420 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4421 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004422 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004423 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4424 i40e_stat_str(&pf->hw, ret),
4425 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004426 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004427 }
4428
4429 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004430 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4431 NULL);
4432 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004433 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004434 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4435 i40e_stat_str(&pf->hw, ret),
4436 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004437 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004438 }
4439
4440 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4441 dev_info(&pf->pdev->dev,
4442 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4443 bw_config.tc_valid_bits,
4444 bw_ets_config.tc_valid_bits);
4445 /* Still continuing */
4446 }
4447
4448 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4449 vsi->bw_max_quanta = bw_config.max_bw;
4450 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4451 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4452 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4453 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4454 vsi->bw_ets_limit_credits[i] =
4455 le16_to_cpu(bw_ets_config.credits[i]);
4456 /* 3 bits out of 4 for each TC */
4457 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4458 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004459
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004460 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004461}
4462
4463/**
4464 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4465 * @vsi: the VSI being configured
4466 * @enabled_tc: TC bitmap
4467 * @bw_credits: BW shared credits per TC
4468 *
4469 * Returns 0 on success, negative value on failure
4470 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004471static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004472 u8 *bw_share)
4473{
4474 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004475 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004476 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004477
4478 bw_data.tc_valid_bits = enabled_tc;
4479 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4480 bw_data.tc_bw_credits[i] = bw_share[i];
4481
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004482 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4483 NULL);
4484 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004485 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004486 "AQ command Config VSI BW allocation per TC failed = %d\n",
4487 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004488 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004489 }
4490
4491 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4492 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4493
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004494 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004495}
4496
4497/**
4498 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4499 * @vsi: the VSI being configured
4500 * @enabled_tc: TC map to be enabled
4501 *
4502 **/
4503static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4504{
4505 struct net_device *netdev = vsi->netdev;
4506 struct i40e_pf *pf = vsi->back;
4507 struct i40e_hw *hw = &pf->hw;
4508 u8 netdev_tc = 0;
4509 int i;
4510 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4511
4512 if (!netdev)
4513 return;
4514
4515 if (!enabled_tc) {
4516 netdev_reset_tc(netdev);
4517 return;
4518 }
4519
4520 /* Set up actual enabled TCs on the VSI */
4521 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4522 return;
4523
4524 /* set per TC queues for the VSI */
4525 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4526 /* Only set TC queues for enabled tcs
4527 *
4528 * e.g. For a VSI that has TC0 and TC3 enabled the
4529 * enabled_tc bitmap would be 0x00001001; the driver
4530 * will set the numtc for netdev as 2 that will be
4531 * referenced by the netdev layer as TC 0 and 1.
4532 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004533 if (vsi->tc_config.enabled_tc & BIT_ULL(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004534 netdev_set_tc_queue(netdev,
4535 vsi->tc_config.tc_info[i].netdev_tc,
4536 vsi->tc_config.tc_info[i].qcount,
4537 vsi->tc_config.tc_info[i].qoffset);
4538 }
4539
4540 /* Assign UP2TC map for the VSI */
4541 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4542 /* Get the actual TC# for the UP */
4543 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4544 /* Get the mapped netdev TC# for the UP */
4545 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4546 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4547 }
4548}
4549
4550/**
4551 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4552 * @vsi: the VSI being configured
4553 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4554 **/
4555static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4556 struct i40e_vsi_context *ctxt)
4557{
4558 /* copy just the sections touched not the entire info
4559 * since not all sections are valid as returned by
4560 * update vsi params
4561 */
4562 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4563 memcpy(&vsi->info.queue_mapping,
4564 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4565 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4566 sizeof(vsi->info.tc_mapping));
4567}
4568
4569/**
4570 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4571 * @vsi: VSI to be configured
4572 * @enabled_tc: TC bitmap
4573 *
4574 * This configures a particular VSI for TCs that are mapped to the
4575 * given TC bitmap. It uses default bandwidth share for TCs across
4576 * VSIs to configure TC for a particular VSI.
4577 *
4578 * NOTE:
4579 * It is expected that the VSI queues have been quisced before calling
4580 * this function.
4581 **/
4582static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4583{
4584 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4585 struct i40e_vsi_context ctxt;
4586 int ret = 0;
4587 int i;
4588
4589 /* Check if enabled_tc is same as existing or new TCs */
4590 if (vsi->tc_config.enabled_tc == enabled_tc)
4591 return ret;
4592
4593 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4594 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004595 if (enabled_tc & BIT_ULL(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004596 bw_share[i] = 1;
4597 }
4598
4599 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4600 if (ret) {
4601 dev_info(&vsi->back->pdev->dev,
4602 "Failed configuring TC map %d for VSI %d\n",
4603 enabled_tc, vsi->seid);
4604 goto out;
4605 }
4606
4607 /* Update Queue Pairs Mapping for currently enabled UPs */
4608 ctxt.seid = vsi->seid;
4609 ctxt.pf_num = vsi->back->hw.pf_id;
4610 ctxt.vf_num = 0;
4611 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004612 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004613 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4614
4615 /* Update the VSI after updating the VSI queue-mapping information */
4616 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4617 if (ret) {
4618 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004619 "Update vsi tc config failed, err %s aq_err %s\n",
4620 i40e_stat_str(&vsi->back->hw, ret),
4621 i40e_aq_str(&vsi->back->hw,
4622 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004623 goto out;
4624 }
4625 /* update the local VSI info with updated queue map */
4626 i40e_vsi_update_queue_map(vsi, &ctxt);
4627 vsi->info.valid_sections = 0;
4628
4629 /* Update current VSI BW information */
4630 ret = i40e_vsi_get_bw_info(vsi);
4631 if (ret) {
4632 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004633 "Failed updating vsi bw info, err %s aq_err %s\n",
4634 i40e_stat_str(&vsi->back->hw, ret),
4635 i40e_aq_str(&vsi->back->hw,
4636 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004637 goto out;
4638 }
4639
4640 /* Update the netdev TC setup */
4641 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4642out:
4643 return ret;
4644}
4645
4646/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004647 * i40e_veb_config_tc - Configure TCs for given VEB
4648 * @veb: given VEB
4649 * @enabled_tc: TC bitmap
4650 *
4651 * Configures given TC bitmap for VEB (switching) element
4652 **/
4653int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4654{
4655 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4656 struct i40e_pf *pf = veb->pf;
4657 int ret = 0;
4658 int i;
4659
4660 /* No TCs or already enabled TCs just return */
4661 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4662 return ret;
4663
4664 bw_data.tc_valid_bits = enabled_tc;
4665 /* bw_data.absolute_credits is not set (relative) */
4666
4667 /* Enable ETS TCs with equal BW Share for now */
4668 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004669 if (enabled_tc & BIT_ULL(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004670 bw_data.tc_bw_share_credits[i] = 1;
4671 }
4672
4673 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4674 &bw_data, NULL);
4675 if (ret) {
4676 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004677 "VEB bw config failed, err %s aq_err %s\n",
4678 i40e_stat_str(&pf->hw, ret),
4679 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004680 goto out;
4681 }
4682
4683 /* Update the BW information */
4684 ret = i40e_veb_get_bw_info(veb);
4685 if (ret) {
4686 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004687 "Failed getting veb bw config, err %s aq_err %s\n",
4688 i40e_stat_str(&pf->hw, ret),
4689 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004690 }
4691
4692out:
4693 return ret;
4694}
4695
4696#ifdef CONFIG_I40E_DCB
4697/**
4698 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4699 * @pf: PF struct
4700 *
4701 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4702 * the caller would've quiesce all the VSIs before calling
4703 * this function
4704 **/
4705static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4706{
4707 u8 tc_map = 0;
4708 int ret;
4709 u8 v;
4710
4711 /* Enable the TCs available on PF to all VEBs */
4712 tc_map = i40e_pf_get_tc_map(pf);
4713 for (v = 0; v < I40E_MAX_VEB; v++) {
4714 if (!pf->veb[v])
4715 continue;
4716 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4717 if (ret) {
4718 dev_info(&pf->pdev->dev,
4719 "Failed configuring TC for VEB seid=%d\n",
4720 pf->veb[v]->seid);
4721 /* Will try to configure as many components */
4722 }
4723 }
4724
4725 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004726 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004727 if (!pf->vsi[v])
4728 continue;
4729
4730 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07004731#ifdef I40E_FCOE
4732 * - For FCoE VSI only enable the TC configured
4733 * as per the APP TLV
4734#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004735 * - For all others keep them at TC0 for now
4736 */
4737 if (v == pf->lan_vsi)
4738 tc_map = i40e_pf_get_tc_map(pf);
4739 else
4740 tc_map = i40e_pf_get_default_tc(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07004741#ifdef I40E_FCOE
4742 if (pf->vsi[v]->type == I40E_VSI_FCOE)
4743 tc_map = i40e_get_fcoe_tc_map(pf);
4744#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004745
4746 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4747 if (ret) {
4748 dev_info(&pf->pdev->dev,
4749 "Failed configuring TC for VSI seid=%d\n",
4750 pf->vsi[v]->seid);
4751 /* Will try to configure as many components */
4752 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004753 /* Re-configure VSI vectors based on updated TC map */
4754 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004755 if (pf->vsi[v]->netdev)
4756 i40e_dcbnl_set_all(pf->vsi[v]);
4757 }
4758 }
4759}
4760
4761/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00004762 * i40e_resume_port_tx - Resume port Tx
4763 * @pf: PF struct
4764 *
4765 * Resume a port's Tx and issue a PF reset in case of failure to
4766 * resume.
4767 **/
4768static int i40e_resume_port_tx(struct i40e_pf *pf)
4769{
4770 struct i40e_hw *hw = &pf->hw;
4771 int ret;
4772
4773 ret = i40e_aq_resume_port_tx(hw, NULL);
4774 if (ret) {
4775 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004776 "Resume Port Tx failed, err %s aq_err %s\n",
4777 i40e_stat_str(&pf->hw, ret),
4778 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00004779 /* Schedule PF reset to recover */
4780 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4781 i40e_service_event_schedule(pf);
4782 }
4783
4784 return ret;
4785}
4786
4787/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004788 * i40e_init_pf_dcb - Initialize DCB configuration
4789 * @pf: PF being configured
4790 *
4791 * Query the current DCB configuration and cache it
4792 * in the hardware structure
4793 **/
4794static int i40e_init_pf_dcb(struct i40e_pf *pf)
4795{
4796 struct i40e_hw *hw = &pf->hw;
4797 int err = 0;
4798
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00004799 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
4800 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
4801 (pf->hw.aq.fw_maj_ver < 4))
4802 goto out;
4803
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004804 /* Get the initial DCB configuration */
4805 err = i40e_init_dcb(hw);
4806 if (!err) {
4807 /* Device/Function is not DCBX capable */
4808 if ((!hw->func_caps.dcb) ||
4809 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4810 dev_info(&pf->pdev->dev,
4811 "DCBX offload is not supported or is disabled for this PF.\n");
4812
4813 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4814 goto out;
4815
4816 } else {
4817 /* When status is not DISABLED then DCBX in FW */
4818 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4819 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004820
4821 pf->flags |= I40E_FLAG_DCB_CAPABLE;
4822 /* Enable DCB tagging only when more than one TC */
4823 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
4824 pf->flags |= I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00004825 dev_dbg(&pf->pdev->dev,
4826 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004827 }
Neerav Parikh014269f2014-04-01 07:11:48 +00004828 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00004829 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004830 "Query for DCB configuration failed, err %s aq_err %s\n",
4831 i40e_stat_str(&pf->hw, err),
4832 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004833 }
4834
4835out:
4836 return err;
4837}
4838#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004839#define SPEED_SIZE 14
4840#define FC_SIZE 8
4841/**
4842 * i40e_print_link_message - print link up or down
4843 * @vsi: the VSI for which link needs a message
4844 */
Matt Jaredc156f852015-08-27 11:42:39 -04004845void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004846{
4847 char speed[SPEED_SIZE] = "Unknown";
4848 char fc[FC_SIZE] = "RX/TX";
4849
Matt Jaredc156f852015-08-27 11:42:39 -04004850 if (vsi->current_isup == isup)
4851 return;
4852 vsi->current_isup = isup;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004853 if (!isup) {
4854 netdev_info(vsi->netdev, "NIC Link is Down\n");
4855 return;
4856 }
4857
Greg Rose148c2d82014-12-11 07:06:27 +00004858 /* Warn user if link speed on NPAR enabled partition is not at
4859 * least 10GB
4860 */
4861 if (vsi->back->hw.func_caps.npar_enable &&
4862 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
4863 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
4864 netdev_warn(vsi->netdev,
4865 "The partition detected link speed that is less than 10Gbps\n");
4866
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004867 switch (vsi->back->hw.phy.link_info.link_speed) {
4868 case I40E_LINK_SPEED_40GB:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004869 strlcpy(speed, "40 Gbps", SPEED_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004870 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07004871 case I40E_LINK_SPEED_20GB:
4872 strncpy(speed, "20 Gbps", SPEED_SIZE);
4873 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004874 case I40E_LINK_SPEED_10GB:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004875 strlcpy(speed, "10 Gbps", SPEED_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004876 break;
4877 case I40E_LINK_SPEED_1GB:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004878 strlcpy(speed, "1000 Mbps", SPEED_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004879 break;
Mitch Williams5960d332014-09-13 07:40:47 +00004880 case I40E_LINK_SPEED_100MB:
4881 strncpy(speed, "100 Mbps", SPEED_SIZE);
4882 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004883 default:
4884 break;
4885 }
4886
4887 switch (vsi->back->hw.fc.current_mode) {
4888 case I40E_FC_FULL:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004889 strlcpy(fc, "RX/TX", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004890 break;
4891 case I40E_FC_TX_PAUSE:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004892 strlcpy(fc, "TX", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004893 break;
4894 case I40E_FC_RX_PAUSE:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004895 strlcpy(fc, "RX", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004896 break;
4897 default:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004898 strlcpy(fc, "None", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004899 break;
4900 }
4901
4902 netdev_info(vsi->netdev, "NIC Link is Up %s Full Duplex, Flow Control: %s\n",
4903 speed, fc);
4904}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004905
4906/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004907 * i40e_up_complete - Finish the last steps of bringing up a connection
4908 * @vsi: the VSI being configured
4909 **/
4910static int i40e_up_complete(struct i40e_vsi *vsi)
4911{
4912 struct i40e_pf *pf = vsi->back;
4913 int err;
4914
4915 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4916 i40e_vsi_configure_msix(vsi);
4917 else
4918 i40e_configure_msi_and_legacy(vsi);
4919
4920 /* start rings */
4921 err = i40e_vsi_control_rings(vsi, true);
4922 if (err)
4923 return err;
4924
4925 clear_bit(__I40E_DOWN, &vsi->state);
4926 i40e_napi_enable_all(vsi);
4927 i40e_vsi_enable_irq(vsi);
4928
4929 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4930 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004931 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004932 netif_tx_start_all_queues(vsi->netdev);
4933 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004934 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004935 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00004936 /* need to check for qualified module here*/
4937 if ((pf->hw.phy.link_info.link_info &
4938 I40E_AQ_MEDIA_AVAILABLE) &&
4939 (!(pf->hw.phy.link_info.an_info &
4940 I40E_AQ_QUALIFIED_MODULE)))
4941 netdev_err(vsi->netdev,
4942 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004943 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004944
4945 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00004946 if (vsi->type == I40E_VSI_FDIR) {
4947 /* reset fd counters */
4948 pf->fd_add_err = pf->fd_atr_cnt = 0;
4949 if (pf->fd_tcp_rule > 0) {
4950 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04004951 if (I40E_DEBUG_FD & pf->hw.debug_mask)
4952 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00004953 pf->fd_tcp_rule = 0;
4954 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004955 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00004956 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004957 i40e_service_event_schedule(pf);
4958
4959 return 0;
4960}
4961
4962/**
4963 * i40e_vsi_reinit_locked - Reset the VSI
4964 * @vsi: the VSI being configured
4965 *
4966 * Rebuild the ring structs after some configuration
4967 * has changed, e.g. MTU size.
4968 **/
4969static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4970{
4971 struct i40e_pf *pf = vsi->back;
4972
4973 WARN_ON(in_interrupt());
4974 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4975 usleep_range(1000, 2000);
4976 i40e_down(vsi);
4977
4978 /* Give a VF some time to respond to the reset. The
4979 * two second wait is based upon the watchdog cycle in
4980 * the VF driver.
4981 */
4982 if (vsi->type == I40E_VSI_SRIOV)
4983 msleep(2000);
4984 i40e_up(vsi);
4985 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
4986}
4987
4988/**
4989 * i40e_up - Bring the connection back up after being down
4990 * @vsi: the VSI being configured
4991 **/
4992int i40e_up(struct i40e_vsi *vsi)
4993{
4994 int err;
4995
4996 err = i40e_vsi_configure(vsi);
4997 if (!err)
4998 err = i40e_up_complete(vsi);
4999
5000 return err;
5001}
5002
5003/**
5004 * i40e_down - Shutdown the connection processing
5005 * @vsi: the VSI being stopped
5006 **/
5007void i40e_down(struct i40e_vsi *vsi)
5008{
5009 int i;
5010
5011 /* It is assumed that the caller of this function
5012 * sets the vsi->state __I40E_DOWN bit.
5013 */
5014 if (vsi->netdev) {
5015 netif_carrier_off(vsi->netdev);
5016 netif_tx_disable(vsi->netdev);
5017 }
5018 i40e_vsi_disable_irq(vsi);
5019 i40e_vsi_control_rings(vsi, false);
5020 i40e_napi_disable_all(vsi);
5021
5022 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005023 i40e_clean_tx_ring(vsi->tx_rings[i]);
5024 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005025 }
5026}
5027
5028/**
5029 * i40e_setup_tc - configure multiple traffic classes
5030 * @netdev: net device to configure
5031 * @tc: number of traffic classes to enable
5032 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005033#ifdef I40E_FCOE
5034int i40e_setup_tc(struct net_device *netdev, u8 tc)
5035#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005036static int i40e_setup_tc(struct net_device *netdev, u8 tc)
Vasu Dev38e00432014-08-01 13:27:03 -07005037#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005038{
5039 struct i40e_netdev_priv *np = netdev_priv(netdev);
5040 struct i40e_vsi *vsi = np->vsi;
5041 struct i40e_pf *pf = vsi->back;
5042 u8 enabled_tc = 0;
5043 int ret = -EINVAL;
5044 int i;
5045
5046 /* Check if DCB enabled to continue */
5047 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5048 netdev_info(netdev, "DCB is not enabled for adapter\n");
5049 goto exit;
5050 }
5051
5052 /* Check if MFP enabled */
5053 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5054 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5055 goto exit;
5056 }
5057
5058 /* Check whether tc count is within enabled limit */
5059 if (tc > i40e_pf_get_num_tc(pf)) {
5060 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5061 goto exit;
5062 }
5063
5064 /* Generate TC map for number of tc requested */
5065 for (i = 0; i < tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005066 enabled_tc |= BIT_ULL(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005067
5068 /* Requesting same TC configuration as already enabled */
5069 if (enabled_tc == vsi->tc_config.enabled_tc)
5070 return 0;
5071
5072 /* Quiesce VSI queues */
5073 i40e_quiesce_vsi(vsi);
5074
5075 /* Configure VSI for enabled TCs */
5076 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5077 if (ret) {
5078 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5079 vsi->seid);
5080 goto exit;
5081 }
5082
5083 /* Unquiesce VSI */
5084 i40e_unquiesce_vsi(vsi);
5085
5086exit:
5087 return ret;
5088}
5089
5090/**
5091 * i40e_open - Called when a network interface is made active
5092 * @netdev: network interface device structure
5093 *
5094 * The open entry point is called when a network interface is made
5095 * active by the system (IFF_UP). At this point all resources needed
5096 * for transmit and receive operations are allocated, the interrupt
5097 * handler is registered with the OS, the netdev watchdog subtask is
5098 * enabled, and the stack is notified that the interface is ready.
5099 *
5100 * Returns 0 on success, negative value on failure
5101 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005102int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005103{
5104 struct i40e_netdev_priv *np = netdev_priv(netdev);
5105 struct i40e_vsi *vsi = np->vsi;
5106 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005107 int err;
5108
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005109 /* disallow open during test or if eeprom is broken */
5110 if (test_bit(__I40E_TESTING, &pf->state) ||
5111 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005112 return -EBUSY;
5113
5114 netif_carrier_off(netdev);
5115
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005116 err = i40e_vsi_open(vsi);
5117 if (err)
5118 return err;
5119
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005120 /* configure global TSO hardware offload settings */
5121 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5122 TCP_FLAG_FIN) >> 16);
5123 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5124 TCP_FLAG_FIN |
5125 TCP_FLAG_CWR) >> 16);
5126 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5127
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005128#ifdef CONFIG_I40E_VXLAN
5129 vxlan_get_rx_port(netdev);
5130#endif
5131
5132 return 0;
5133}
5134
5135/**
5136 * i40e_vsi_open -
5137 * @vsi: the VSI to open
5138 *
5139 * Finish initialization of the VSI.
5140 *
5141 * Returns 0 on success, negative value on failure
5142 **/
5143int i40e_vsi_open(struct i40e_vsi *vsi)
5144{
5145 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005146 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005147 int err;
5148
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005149 /* allocate descriptors */
5150 err = i40e_vsi_setup_tx_resources(vsi);
5151 if (err)
5152 goto err_setup_tx;
5153 err = i40e_vsi_setup_rx_resources(vsi);
5154 if (err)
5155 goto err_setup_rx;
5156
5157 err = i40e_vsi_configure(vsi);
5158 if (err)
5159 goto err_setup_rx;
5160
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005161 if (vsi->netdev) {
5162 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5163 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5164 err = i40e_vsi_request_irq(vsi, int_name);
5165 if (err)
5166 goto err_setup_rx;
5167
5168 /* Notify the stack of the actual queue counts. */
5169 err = netif_set_real_num_tx_queues(vsi->netdev,
5170 vsi->num_queue_pairs);
5171 if (err)
5172 goto err_set_queues;
5173
5174 err = netif_set_real_num_rx_queues(vsi->netdev,
5175 vsi->num_queue_pairs);
5176 if (err)
5177 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005178
5179 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005180 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005181 dev_driver_string(&pf->pdev->dev),
5182 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005183 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005184
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005185 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005186 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005187 goto err_setup_rx;
5188 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005189
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005190 err = i40e_up_complete(vsi);
5191 if (err)
5192 goto err_up_complete;
5193
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005194 return 0;
5195
5196err_up_complete:
5197 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005198err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005199 i40e_vsi_free_irq(vsi);
5200err_setup_rx:
5201 i40e_vsi_free_rx_resources(vsi);
5202err_setup_tx:
5203 i40e_vsi_free_tx_resources(vsi);
5204 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005205 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005206
5207 return err;
5208}
5209
5210/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005211 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005212 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005213 *
5214 * This function destroys the hlist where all the Flow Director
5215 * filters were saved.
5216 **/
5217static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5218{
5219 struct i40e_fdir_filter *filter;
5220 struct hlist_node *node2;
5221
5222 hlist_for_each_entry_safe(filter, node2,
5223 &pf->fdir_filter_list, fdir_node) {
5224 hlist_del(&filter->fdir_node);
5225 kfree(filter);
5226 }
5227 pf->fdir_pf_active_filters = 0;
5228}
5229
5230/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005231 * i40e_close - Disables a network interface
5232 * @netdev: network interface device structure
5233 *
5234 * The close entry point is called when an interface is de-activated
5235 * by the OS. The hardware is still under the driver's control, but
5236 * this netdev interface is disabled.
5237 *
5238 * Returns 0, this is not allowed to fail
5239 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005240#ifdef I40E_FCOE
5241int i40e_close(struct net_device *netdev)
5242#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005243static int i40e_close(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07005244#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005245{
5246 struct i40e_netdev_priv *np = netdev_priv(netdev);
5247 struct i40e_vsi *vsi = np->vsi;
5248
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005249 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005250
5251 return 0;
5252}
5253
5254/**
5255 * i40e_do_reset - Start a PF or Core Reset sequence
5256 * @pf: board private structure
5257 * @reset_flags: which reset is requested
5258 *
5259 * The essential difference in resets is that the PF Reset
5260 * doesn't clear the packet buffers, doesn't reset the PE
5261 * firmware, and doesn't bother the other PFs on the chip.
5262 **/
5263void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5264{
5265 u32 val;
5266
5267 WARN_ON(in_interrupt());
5268
Mitch Williams263fc482014-04-23 04:50:11 +00005269 if (i40e_check_asq_alive(&pf->hw))
5270 i40e_vc_notify_reset(pf);
5271
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005272 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005273 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005274
5275 /* Request a Global Reset
5276 *
5277 * This will start the chip's countdown to the actual full
5278 * chip reset event, and a warning interrupt to be sent
5279 * to all PFs, including the requestor. Our handler
5280 * for the warning interrupt will deal with the shutdown
5281 * and recovery of the switch setup.
5282 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005283 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005284 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5285 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5286 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5287
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005288 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005289
5290 /* Request a Core Reset
5291 *
5292 * Same as Global Reset, except does *not* include the MAC/PHY
5293 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005294 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005295 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5296 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5297 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5298 i40e_flush(&pf->hw);
5299
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005300 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005301
5302 /* Request a PF Reset
5303 *
5304 * Resets only the PF-specific registers
5305 *
5306 * This goes directly to the tear-down and rebuild of
5307 * the switch, since we need to do all the recovery as
5308 * for the Core Reset.
5309 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005310 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005311 i40e_handle_reset_warning(pf);
5312
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005313 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005314 int v;
5315
5316 /* Find the VSI(s) that requested a re-init */
5317 dev_info(&pf->pdev->dev,
5318 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005319 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005320 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005321
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005322 if (vsi != NULL &&
5323 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5324 i40e_vsi_reinit_locked(pf->vsi[v]);
5325 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5326 }
5327 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005328 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005329 int v;
5330
5331 /* Find the VSI(s) that needs to be brought down */
5332 dev_info(&pf->pdev->dev, "VSI down requested\n");
5333 for (v = 0; v < pf->num_alloc_vsi; v++) {
5334 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005335
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005336 if (vsi != NULL &&
5337 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5338 set_bit(__I40E_DOWN, &vsi->state);
5339 i40e_down(vsi);
5340 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5341 }
5342 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005343 } else {
5344 dev_info(&pf->pdev->dev,
5345 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005346 }
5347}
5348
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005349#ifdef CONFIG_I40E_DCB
5350/**
5351 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5352 * @pf: board private structure
5353 * @old_cfg: current DCB config
5354 * @new_cfg: new DCB config
5355 **/
5356bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5357 struct i40e_dcbx_config *old_cfg,
5358 struct i40e_dcbx_config *new_cfg)
5359{
5360 bool need_reconfig = false;
5361
5362 /* Check if ETS configuration has changed */
5363 if (memcmp(&new_cfg->etscfg,
5364 &old_cfg->etscfg,
5365 sizeof(new_cfg->etscfg))) {
5366 /* If Priority Table has changed reconfig is needed */
5367 if (memcmp(&new_cfg->etscfg.prioritytable,
5368 &old_cfg->etscfg.prioritytable,
5369 sizeof(new_cfg->etscfg.prioritytable))) {
5370 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005371 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005372 }
5373
5374 if (memcmp(&new_cfg->etscfg.tcbwtable,
5375 &old_cfg->etscfg.tcbwtable,
5376 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005377 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005378
5379 if (memcmp(&new_cfg->etscfg.tsatable,
5380 &old_cfg->etscfg.tsatable,
5381 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005382 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005383 }
5384
5385 /* Check if PFC configuration has changed */
5386 if (memcmp(&new_cfg->pfc,
5387 &old_cfg->pfc,
5388 sizeof(new_cfg->pfc))) {
5389 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005390 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005391 }
5392
5393 /* Check if APP Table has changed */
5394 if (memcmp(&new_cfg->app,
5395 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005396 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005397 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005398 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005399 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005400
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005401 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005402 return need_reconfig;
5403}
5404
5405/**
5406 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5407 * @pf: board private structure
5408 * @e: event info posted on ARQ
5409 **/
5410static int i40e_handle_lldp_event(struct i40e_pf *pf,
5411 struct i40e_arq_event_info *e)
5412{
5413 struct i40e_aqc_lldp_get_mib *mib =
5414 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5415 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005416 struct i40e_dcbx_config tmp_dcbx_cfg;
5417 bool need_reconfig = false;
5418 int ret = 0;
5419 u8 type;
5420
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005421 /* Not DCB capable or capability disabled */
5422 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
5423 return ret;
5424
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005425 /* Ignore if event is not for Nearest Bridge */
5426 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5427 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005428 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005429 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5430 return ret;
5431
5432 /* Check MIB Type and return if event for Remote MIB update */
5433 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005434 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005435 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005436 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5437 /* Update the remote cached instance and return */
5438 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5439 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5440 &hw->remote_dcbx_config);
5441 goto exit;
5442 }
5443
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005444 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005445 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005446
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005447 /* Reset the old DCBx configuration data */
5448 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005449 /* Get updated DCBX data from firmware */
5450 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005451 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005452 dev_info(&pf->pdev->dev,
5453 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5454 i40e_stat_str(&pf->hw, ret),
5455 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005456 goto exit;
5457 }
5458
5459 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005460 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5461 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005462 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005463 goto exit;
5464 }
5465
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005466 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5467 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005468
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005469 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005470
5471 if (!need_reconfig)
5472 goto exit;
5473
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005474 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005475 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005476 pf->flags |= I40E_FLAG_DCB_ENABLED;
5477 else
5478 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5479
Neerav Parikh69129dc2014-11-12 00:18:46 +00005480 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005481 /* Reconfiguration needed quiesce all VSIs */
5482 i40e_pf_quiesce_all_vsi(pf);
5483
5484 /* Changes in configuration update VEB/VSI */
5485 i40e_dcb_reconfigure(pf);
5486
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005487 ret = i40e_resume_port_tx(pf);
5488
Neerav Parikh69129dc2014-11-12 00:18:46 +00005489 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005490 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005491 if (ret)
5492 goto exit;
5493
5494 /* Wait for the PF's Tx queues to be disabled */
5495 ret = i40e_pf_wait_txq_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005496 if (ret) {
5497 /* Schedule PF reset to recover */
5498 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5499 i40e_service_event_schedule(pf);
5500 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005501 i40e_pf_unquiesce_all_vsi(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005502 }
5503
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005504exit:
5505 return ret;
5506}
5507#endif /* CONFIG_I40E_DCB */
5508
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005509/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005510 * i40e_do_reset_safe - Protected reset path for userland calls.
5511 * @pf: board private structure
5512 * @reset_flags: which reset is requested
5513 *
5514 **/
5515void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5516{
5517 rtnl_lock();
5518 i40e_do_reset(pf, reset_flags);
5519 rtnl_unlock();
5520}
5521
5522/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005523 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5524 * @pf: board private structure
5525 * @e: event info posted on ARQ
5526 *
5527 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5528 * and VF queues
5529 **/
5530static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5531 struct i40e_arq_event_info *e)
5532{
5533 struct i40e_aqc_lan_overflow *data =
5534 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5535 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5536 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5537 struct i40e_hw *hw = &pf->hw;
5538 struct i40e_vf *vf;
5539 u16 vf_id;
5540
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005541 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5542 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005543
5544 /* Queue belongs to VF, find the VF and issue VF reset */
5545 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5546 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5547 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5548 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5549 vf_id -= hw->func_caps.vf_base_id;
5550 vf = &pf->vf[vf_id];
5551 i40e_vc_notify_vf_reset(vf);
5552 /* Allow VF to process pending reset notification */
5553 msleep(20);
5554 i40e_reset_vf(vf, false);
5555 }
5556}
5557
5558/**
5559 * i40e_service_event_complete - Finish up the service event
5560 * @pf: board private structure
5561 **/
5562static void i40e_service_event_complete(struct i40e_pf *pf)
5563{
5564 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
5565
5566 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005567 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005568 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5569}
5570
5571/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005572 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5573 * @pf: board private structure
5574 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005575u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005576{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005577 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005578
5579 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5580 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5581 return fcnt_prog;
5582}
5583
5584/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005585 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005586 * @pf: board private structure
5587 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005588u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005589{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005590 u32 val, fcnt_prog;
5591
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005592 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5593 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5594 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5595 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5596 return fcnt_prog;
5597}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005598
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005599/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005600 * i40e_get_global_fd_count - Get total FD filters programmed on device
5601 * @pf: board private structure
5602 **/
5603u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5604{
5605 u32 val, fcnt_prog;
5606
5607 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5608 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5609 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5610 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5611 return fcnt_prog;
5612}
5613
5614/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005615 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5616 * @pf: board private structure
5617 **/
5618void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5619{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005620 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005621 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005622 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005623
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005624 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5625 return;
5626
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005627 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5628 * to re-enable
5629 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005630 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005631 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005632 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5633 (pf->fd_add_err == 0) ||
5634 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005635 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5636 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5637 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005638 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5639 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 +00005640 }
5641 }
5642 /* Wait for some more space to be available to turn on ATR */
5643 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5644 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
5645 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
5646 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005647 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5648 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005649 }
5650 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005651
5652 /* if hw had a problem adding a filter, delete it */
5653 if (pf->fd_inv > 0) {
5654 hlist_for_each_entry_safe(filter, node,
5655 &pf->fdir_filter_list, fdir_node) {
5656 if (filter->fd_id == pf->fd_inv) {
5657 hlist_del(&filter->fdir_node);
5658 kfree(filter);
5659 pf->fdir_pf_active_filters--;
5660 }
5661 }
5662 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005663}
5664
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005665#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005666#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005667/**
5668 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
5669 * @pf: board private structure
5670 **/
5671static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
5672{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005673 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005674 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005675 bool disable_atr = false;
5676 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005677 int reg;
5678
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005679 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5680 return;
5681
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005682 if (!time_after(jiffies, pf->fd_flush_timestamp +
5683 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
5684 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005685
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005686 /* If the flush is happening too quick and we have mostly SB rules we
5687 * should not re-enable ATR for some time.
5688 */
5689 min_flush_time = pf->fd_flush_timestamp +
5690 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
5691 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005692
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005693 if (!(time_after(jiffies, min_flush_time)) &&
5694 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
5695 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5696 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
5697 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005698 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005699
5700 pf->fd_flush_timestamp = jiffies;
5701 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
5702 /* flush all filters */
5703 wr32(&pf->hw, I40E_PFQF_CTL_1,
5704 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
5705 i40e_flush(&pf->hw);
5706 pf->fd_flush_cnt++;
5707 pf->fd_add_err = 0;
5708 do {
5709 /* Check FD flush status every 5-6msec */
5710 usleep_range(5000, 6000);
5711 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
5712 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
5713 break;
5714 } while (flush_wait_retry--);
5715 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
5716 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
5717 } else {
5718 /* replay sideband filters */
5719 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
5720 if (!disable_atr)
5721 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
5722 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
5723 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5724 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
5725 }
5726
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005727}
5728
5729/**
5730 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
5731 * @pf: board private structure
5732 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005733u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005734{
5735 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
5736}
5737
5738/* We can see up to 256 filter programming desc in transit if the filters are
5739 * being applied really fast; before we see the first
5740 * filter miss error on Rx queue 0. Accumulating enough error messages before
5741 * reacting will make sure we don't cause flush too often.
5742 */
5743#define I40E_MAX_FD_PROGRAM_ERROR 256
5744
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005745/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005746 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
5747 * @pf: board private structure
5748 **/
5749static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
5750{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005751
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005752 /* if interface is down do nothing */
5753 if (test_bit(__I40E_DOWN, &pf->state))
5754 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005755
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005756 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5757 return;
5758
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005759 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005760 i40e_fdir_flush_and_replay(pf);
5761
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005762 i40e_fdir_check_and_reenable(pf);
5763
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005764}
5765
5766/**
5767 * i40e_vsi_link_event - notify VSI of a link event
5768 * @vsi: vsi to be notified
5769 * @link_up: link up or down
5770 **/
5771static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
5772{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00005773 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005774 return;
5775
5776 switch (vsi->type) {
5777 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07005778#ifdef I40E_FCOE
5779 case I40E_VSI_FCOE:
5780#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005781 if (!vsi->netdev || !vsi->netdev_registered)
5782 break;
5783
5784 if (link_up) {
5785 netif_carrier_on(vsi->netdev);
5786 netif_tx_wake_all_queues(vsi->netdev);
5787 } else {
5788 netif_carrier_off(vsi->netdev);
5789 netif_tx_stop_all_queues(vsi->netdev);
5790 }
5791 break;
5792
5793 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005794 case I40E_VSI_VMDQ2:
5795 case I40E_VSI_CTRL:
5796 case I40E_VSI_MIRROR:
5797 default:
5798 /* there is no notification for other VSIs */
5799 break;
5800 }
5801}
5802
5803/**
5804 * i40e_veb_link_event - notify elements on the veb of a link event
5805 * @veb: veb to be notified
5806 * @link_up: link up or down
5807 **/
5808static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
5809{
5810 struct i40e_pf *pf;
5811 int i;
5812
5813 if (!veb || !veb->pf)
5814 return;
5815 pf = veb->pf;
5816
5817 /* depth first... */
5818 for (i = 0; i < I40E_MAX_VEB; i++)
5819 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
5820 i40e_veb_link_event(pf->veb[i], link_up);
5821
5822 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00005823 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005824 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
5825 i40e_vsi_link_event(pf->vsi[i], link_up);
5826}
5827
5828/**
5829 * i40e_link_event - Update netif_carrier status
5830 * @pf: board private structure
5831 **/
5832static void i40e_link_event(struct i40e_pf *pf)
5833{
Mitch Williams320684c2014-10-17 03:14:43 +00005834 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00005835 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04005836 i40e_status status;
5837 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005838
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005839 /* set this to force the get_link_status call to refresh state */
5840 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005841
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005842 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04005843
5844 status = i40e_get_link_status(&pf->hw, &new_link);
5845 if (status) {
5846 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
5847 status);
5848 return;
5849 }
5850
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00005851 old_link_speed = pf->hw.phy.link_info_old.link_speed;
5852 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005853
5854 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00005855 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00005856 (test_bit(__I40E_DOWN, &vsi->state) ||
5857 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005858 return;
Mitch Williams320684c2014-10-17 03:14:43 +00005859
5860 if (!test_bit(__I40E_DOWN, &vsi->state))
5861 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005862
5863 /* Notify the base of the switch tree connected to
5864 * the link. Floating VEBs are not notified.
5865 */
5866 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
5867 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
5868 else
Mitch Williams320684c2014-10-17 03:14:43 +00005869 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005870
5871 if (pf->vf)
5872 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005873
5874 if (pf->flags & I40E_FLAG_PTP)
5875 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005876}
5877
5878/**
Shannon Nelson21536712014-10-25 10:35:25 +00005879 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005880 * @pf: board private structure
5881 **/
5882static void i40e_watchdog_subtask(struct i40e_pf *pf)
5883{
5884 int i;
5885
5886 /* if interface is down do nothing */
5887 if (test_bit(__I40E_DOWN, &pf->state) ||
5888 test_bit(__I40E_CONFIG_BUSY, &pf->state))
5889 return;
5890
Shannon Nelson21536712014-10-25 10:35:25 +00005891 /* make sure we don't do these things too often */
5892 if (time_before(jiffies, (pf->service_timer_previous +
5893 pf->service_timer_period)))
5894 return;
5895 pf->service_timer_previous = jiffies;
5896
Shannon Nelson9ac77262015-08-27 11:42:40 -04005897 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
5898 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00005899
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005900 /* Update the stats for active netdevs so the network stack
5901 * can look at updated numbers whenever it cares to
5902 */
Mitch Williams505682c2014-05-20 08:01:37 +00005903 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005904 if (pf->vsi[i] && pf->vsi[i]->netdev)
5905 i40e_update_stats(pf->vsi[i]);
5906
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04005907 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
5908 /* Update the stats for the active switching components */
5909 for (i = 0; i < I40E_MAX_VEB; i++)
5910 if (pf->veb[i])
5911 i40e_update_veb_stats(pf->veb[i]);
5912 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005913
5914 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005915}
5916
5917/**
5918 * i40e_reset_subtask - Set up for resetting the device and driver
5919 * @pf: board private structure
5920 **/
5921static void i40e_reset_subtask(struct i40e_pf *pf)
5922{
5923 u32 reset_flags = 0;
5924
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005925 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005926 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005927 reset_flags |= BIT_ULL(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005928 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
5929 }
5930 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005931 reset_flags |= BIT_ULL(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005932 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5933 }
5934 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005935 reset_flags |= BIT_ULL(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005936 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
5937 }
5938 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005939 reset_flags |= BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005940 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
5941 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005942 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005943 reset_flags |= BIT_ULL(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005944 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
5945 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005946
5947 /* If there's a recovery already waiting, it takes
5948 * precedence before starting a new reset sequence.
5949 */
5950 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
5951 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005952 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005953 }
5954
5955 /* If we're already down or resetting, just bail */
5956 if (reset_flags &&
5957 !test_bit(__I40E_DOWN, &pf->state) &&
5958 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
5959 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005960
5961unlock:
5962 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005963}
5964
5965/**
5966 * i40e_handle_link_event - Handle link event
5967 * @pf: board private structure
5968 * @e: event info posted on ARQ
5969 **/
5970static void i40e_handle_link_event(struct i40e_pf *pf,
5971 struct i40e_arq_event_info *e)
5972{
5973 struct i40e_hw *hw = &pf->hw;
5974 struct i40e_aqc_get_link_status *status =
5975 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005976
5977 /* save off old link status information */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005978 hw->phy.link_info_old = hw->phy.link_info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005979
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005980 /* Do a new status request to re-enable LSE reporting
5981 * and load new status information into the hw struct
5982 * This completely ignores any state information
5983 * in the ARQ event info, instead choosing to always
5984 * issue the AQ update link status command.
5985 */
5986 i40e_link_event(pf);
5987
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005988 /* check for unqualified module, if link is down */
5989 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
5990 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
5991 (!(status->link_info & I40E_AQ_LINK_UP)))
5992 dev_err(&pf->pdev->dev,
5993 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005994}
5995
5996/**
5997 * i40e_clean_adminq_subtask - Clean the AdminQ rings
5998 * @pf: board private structure
5999 **/
6000static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6001{
6002 struct i40e_arq_event_info event;
6003 struct i40e_hw *hw = &pf->hw;
6004 u16 pending, i = 0;
6005 i40e_status ret;
6006 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006007 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006008 u32 val;
6009
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006010 /* Do not run clean AQ when PF reset fails */
6011 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6012 return;
6013
Shannon Nelson86df2422014-05-20 08:01:35 +00006014 /* check for error indications */
6015 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6016 oldval = val;
6017 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
6018 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
6019 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6020 }
6021 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
6022 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
6023 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
6024 }
6025 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
6026 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
6027 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6028 }
6029 if (oldval != val)
6030 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6031
6032 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6033 oldval = val;
6034 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
6035 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
6036 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6037 }
6038 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
6039 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
6040 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6041 }
6042 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
6043 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
6044 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6045 }
6046 if (oldval != val)
6047 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6048
Mitch Williams1001dc32014-11-11 20:02:19 +00006049 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6050 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006051 if (!event.msg_buf)
6052 return;
6053
6054 do {
6055 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006056 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006057 break;
Mitch Williams56497972014-06-04 08:45:18 +00006058 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006059 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6060 break;
6061 }
6062
6063 opcode = le16_to_cpu(event.desc.opcode);
6064 switch (opcode) {
6065
6066 case i40e_aqc_opc_get_link_status:
6067 i40e_handle_link_event(pf, &event);
6068 break;
6069 case i40e_aqc_opc_send_msg_to_pf:
6070 ret = i40e_vc_process_vf_msg(pf,
6071 le16_to_cpu(event.desc.retval),
6072 le32_to_cpu(event.desc.cookie_high),
6073 le32_to_cpu(event.desc.cookie_low),
6074 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006075 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006076 break;
6077 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006078 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006079#ifdef CONFIG_I40E_DCB
6080 rtnl_lock();
6081 ret = i40e_handle_lldp_event(pf, &event);
6082 rtnl_unlock();
6083#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006084 break;
6085 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006086 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006087 i40e_handle_lan_overflow_event(pf, &event);
6088 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006089 case i40e_aqc_opc_send_msg_to_peer:
6090 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6091 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006092 case i40e_aqc_opc_nvm_erase:
6093 case i40e_aqc_opc_nvm_update:
6094 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "ARQ NVM operation completed\n");
6095 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006096 default:
6097 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00006098 "ARQ Error: Unknown event 0x%04x received\n",
6099 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006100 break;
6101 }
6102 } while (pending && (i++ < pf->adminq_work_limit));
6103
6104 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6105 /* re-enable Admin queue interrupt cause */
6106 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6107 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6108 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6109 i40e_flush(hw);
6110
6111 kfree(event.msg_buf);
6112}
6113
6114/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006115 * i40e_verify_eeprom - make sure eeprom is good to use
6116 * @pf: board private structure
6117 **/
6118static void i40e_verify_eeprom(struct i40e_pf *pf)
6119{
6120 int err;
6121
6122 err = i40e_diag_eeprom_test(&pf->hw);
6123 if (err) {
6124 /* retry in case of garbage read */
6125 err = i40e_diag_eeprom_test(&pf->hw);
6126 if (err) {
6127 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6128 err);
6129 set_bit(__I40E_BAD_EEPROM, &pf->state);
6130 }
6131 }
6132
6133 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6134 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6135 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6136 }
6137}
6138
6139/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006140 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006141 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006142 *
6143 * enable switch loop back or die - no point in a return value
6144 **/
6145static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6146{
6147 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6148 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006149 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006150
6151 ctxt.seid = pf->main_vsi_seid;
6152 ctxt.pf_num = pf->hw.pf_id;
6153 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006154 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6155 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006156 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006157 "couldn't get PF vsi config, err %s aq_err %s\n",
6158 i40e_stat_str(&pf->hw, ret),
6159 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006160 return;
6161 }
6162 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6163 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6164 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6165
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006166 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6167 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006168 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006169 "update vsi switch failed, err %s aq_err %s\n",
6170 i40e_stat_str(&pf->hw, ret),
6171 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006172 }
6173}
6174
6175/**
6176 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006177 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006178 *
6179 * disable switch loop back or die - no point in a return value
6180 **/
6181static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6182{
6183 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6184 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006185 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006186
6187 ctxt.seid = pf->main_vsi_seid;
6188 ctxt.pf_num = pf->hw.pf_id;
6189 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006190 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6191 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006192 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006193 "couldn't get PF vsi config, err %s aq_err %s\n",
6194 i40e_stat_str(&pf->hw, ret),
6195 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006196 return;
6197 }
6198 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6199 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6200 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6201
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006202 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6203 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006204 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006205 "update vsi switch failed, err %s aq_err %s\n",
6206 i40e_stat_str(&pf->hw, ret),
6207 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006208 }
6209}
6210
6211/**
Neerav Parikh51616012015-02-06 08:52:14 +00006212 * i40e_config_bridge_mode - Configure the HW bridge mode
6213 * @veb: pointer to the bridge instance
6214 *
6215 * Configure the loop back mode for the LAN VSI that is downlink to the
6216 * specified HW bridge instance. It is expected this function is called
6217 * when a new HW bridge is instantiated.
6218 **/
6219static void i40e_config_bridge_mode(struct i40e_veb *veb)
6220{
6221 struct i40e_pf *pf = veb->pf;
6222
6223 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6224 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
6225 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6226 i40e_disable_pf_switch_lb(pf);
6227 else
6228 i40e_enable_pf_switch_lb(pf);
6229}
6230
6231/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006232 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6233 * @veb: pointer to the VEB instance
6234 *
6235 * This is a recursive function that first builds the attached VSIs then
6236 * recurses in to build the next layer of VEB. We track the connections
6237 * through our own index numbers because the seid's from the HW could
6238 * change across the reset.
6239 **/
6240static int i40e_reconstitute_veb(struct i40e_veb *veb)
6241{
6242 struct i40e_vsi *ctl_vsi = NULL;
6243 struct i40e_pf *pf = veb->pf;
6244 int v, veb_idx;
6245 int ret;
6246
6247 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006248 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006249 if (pf->vsi[v] &&
6250 pf->vsi[v]->veb_idx == veb->idx &&
6251 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6252 ctl_vsi = pf->vsi[v];
6253 break;
6254 }
6255 }
6256 if (!ctl_vsi) {
6257 dev_info(&pf->pdev->dev,
6258 "missing owner VSI for veb_idx %d\n", veb->idx);
6259 ret = -ENOENT;
6260 goto end_reconstitute;
6261 }
6262 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6263 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6264 ret = i40e_add_vsi(ctl_vsi);
6265 if (ret) {
6266 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006267 "rebuild of veb_idx %d owner VSI failed: %d\n",
6268 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006269 goto end_reconstitute;
6270 }
6271 i40e_vsi_reset_stats(ctl_vsi);
6272
6273 /* create the VEB in the switch and move the VSI onto the VEB */
6274 ret = i40e_add_veb(veb, ctl_vsi);
6275 if (ret)
6276 goto end_reconstitute;
6277
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006278 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6279 veb->bridge_mode = BRIDGE_MODE_VEB;
6280 else
6281 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006282 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006283
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006284 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006285 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006286 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6287 continue;
6288
6289 if (pf->vsi[v]->veb_idx == veb->idx) {
6290 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006291
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006292 vsi->uplink_seid = veb->seid;
6293 ret = i40e_add_vsi(vsi);
6294 if (ret) {
6295 dev_info(&pf->pdev->dev,
6296 "rebuild of vsi_idx %d failed: %d\n",
6297 v, ret);
6298 goto end_reconstitute;
6299 }
6300 i40e_vsi_reset_stats(vsi);
6301 }
6302 }
6303
6304 /* create any VEBs attached to this VEB - RECURSION */
6305 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6306 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6307 pf->veb[veb_idx]->uplink_seid = veb->seid;
6308 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6309 if (ret)
6310 break;
6311 }
6312 }
6313
6314end_reconstitute:
6315 return ret;
6316}
6317
6318/**
6319 * i40e_get_capabilities - get info about the HW
6320 * @pf: the PF struct
6321 **/
6322static int i40e_get_capabilities(struct i40e_pf *pf)
6323{
6324 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6325 u16 data_size;
6326 int buf_len;
6327 int err;
6328
6329 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6330 do {
6331 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6332 if (!cap_buf)
6333 return -ENOMEM;
6334
6335 /* this loads the data into the hw struct for us */
6336 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6337 &data_size,
6338 i40e_aqc_opc_list_func_capabilities,
6339 NULL);
6340 /* data loaded, buffer no longer needed */
6341 kfree(cap_buf);
6342
6343 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6344 /* retry with a larger buffer */
6345 buf_len = data_size;
6346 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6347 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006348 "capability discovery failed, err %s aq_err %s\n",
6349 i40e_stat_str(&pf->hw, err),
6350 i40e_aq_str(&pf->hw,
6351 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006352 return -ENODEV;
6353 }
6354 } while (err);
6355
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00006356 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
6357 (pf->hw.aq.fw_maj_ver < 2)) {
6358 pf->hw.func_caps.num_msix_vectors++;
6359 pf->hw.func_caps.num_msix_vectors_vf++;
6360 }
6361
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006362 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6363 dev_info(&pf->pdev->dev,
6364 "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",
6365 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6366 pf->hw.func_caps.num_msix_vectors,
6367 pf->hw.func_caps.num_msix_vectors_vf,
6368 pf->hw.func_caps.fd_filters_guaranteed,
6369 pf->hw.func_caps.fd_filters_best_effort,
6370 pf->hw.func_caps.num_tx_qp,
6371 pf->hw.func_caps.num_vsis);
6372
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006373#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6374 + pf->hw.func_caps.num_vfs)
6375 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6376 dev_info(&pf->pdev->dev,
6377 "got num_vsis %d, setting num_vsis to %d\n",
6378 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6379 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6380 }
6381
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006382 return 0;
6383}
6384
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006385static int i40e_vsi_clear(struct i40e_vsi *vsi);
6386
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006387/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006388 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006389 * @pf: board private structure
6390 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006391static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006392{
6393 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006394 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006395
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006396 /* quick workaround for an NVM issue that leaves a critical register
6397 * uninitialized
6398 */
6399 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6400 static const u32 hkey[] = {
6401 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6402 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6403 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6404 0x95b3a76d};
6405
6406 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6407 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6408 }
6409
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006410 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006411 return;
6412
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006413 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006414 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00006415 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006416 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006417 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006418 break;
6419 }
6420 }
6421
6422 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006423 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006424 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6425 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006426 if (!vsi) {
6427 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006428 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6429 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006430 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006431 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006432
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006433 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006434}
6435
6436/**
6437 * i40e_fdir_teardown - release the Flow Director resources
6438 * @pf: board private structure
6439 **/
6440static void i40e_fdir_teardown(struct i40e_pf *pf)
6441{
6442 int i;
6443
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006444 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00006445 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006446 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
6447 i40e_vsi_release(pf->vsi[i]);
6448 break;
6449 }
6450 }
6451}
6452
6453/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006454 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006455 * @pf: board private structure
6456 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006457 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006458 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006459static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006460{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006461 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006462 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006463 u32 v;
6464
6465 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6466 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006467 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006468
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006469 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006470
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006471 /* quiesce the VSIs and their queues that are not already DOWN */
6472 i40e_pf_quiesce_all_vsi(pf);
6473
Mitch Williams505682c2014-05-20 08:01:37 +00006474 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006475 if (pf->vsi[v])
6476 pf->vsi[v]->seid = 0;
6477 }
6478
6479 i40e_shutdown_adminq(&pf->hw);
6480
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006481 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006482 if (hw->hmc.hmc_obj) {
6483 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006484 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006485 dev_warn(&pf->pdev->dev,
6486 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006487 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006488}
6489
6490/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006491 * i40e_send_version - update firmware with driver version
6492 * @pf: PF struct
6493 */
6494static void i40e_send_version(struct i40e_pf *pf)
6495{
6496 struct i40e_driver_version dv;
6497
6498 dv.major_version = DRV_VERSION_MAJOR;
6499 dv.minor_version = DRV_VERSION_MINOR;
6500 dv.build_version = DRV_VERSION_BUILD;
6501 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006502 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006503 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6504}
6505
6506/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006507 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006508 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006509 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006510 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006511static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006512{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006513 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006514 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006515 i40e_status ret;
6516 u32 v;
6517
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006518 /* Now we wait for GRST to settle out.
6519 * We don't have to delete the VEBs or VSIs from the hw switch
6520 * because the reset will make them disappear.
6521 */
6522 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006523 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006524 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006525 set_bit(__I40E_RESET_FAILED, &pf->state);
6526 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006527 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006528 pf->pfr_count++;
6529
6530 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006531 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006532 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006533
6534 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6535 ret = i40e_init_adminq(&pf->hw);
6536 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006537 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6538 i40e_stat_str(&pf->hw, ret),
6539 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006540 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006541 }
6542
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006543 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006544 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006545 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006546
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006547 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006548 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006549 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006550 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006551
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006552 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6553 hw->func_caps.num_rx_qp,
6554 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6555 if (ret) {
6556 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6557 goto end_core_reset;
6558 }
6559 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6560 if (ret) {
6561 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6562 goto end_core_reset;
6563 }
6564
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006565#ifdef CONFIG_I40E_DCB
6566 ret = i40e_init_pf_dcb(pf);
6567 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006568 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6569 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6570 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006571 }
6572#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006573#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006574 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006575
Vasu Dev38e00432014-08-01 13:27:03 -07006576#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006577 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006578 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006579 if (ret)
6580 goto end_core_reset;
6581
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006582 /* driver is only interested in link up/down and module qualification
6583 * reports from firmware
6584 */
6585 ret = i40e_aq_set_phy_int_mask(&pf->hw,
6586 I40E_AQ_EVENT_LINK_UPDOWN |
6587 I40E_AQ_EVENT_MODULE_QUAL_FAIL, NULL);
6588 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006589 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6590 i40e_stat_str(&pf->hw, ret),
6591 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006592
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006593 /* make sure our flow control settings are restored */
6594 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6595 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006596 dev_info(&pf->pdev->dev, "set fc fail, err %s aq_err %s\n",
6597 i40e_stat_str(&pf->hw, ret),
6598 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006599
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006600 /* Rebuild the VSIs and VEBs that existed before reset.
6601 * They are still in our local switch element arrays, so only
6602 * need to rebuild the switch model in the HW.
6603 *
6604 * If there were VEBs but the reconstitution failed, we'll try
6605 * try to recover minimal use by getting the basic PF VSI working.
6606 */
6607 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006608 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006609 /* find the one VEB connected to the MAC, and find orphans */
6610 for (v = 0; v < I40E_MAX_VEB; v++) {
6611 if (!pf->veb[v])
6612 continue;
6613
6614 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6615 pf->veb[v]->uplink_seid == 0) {
6616 ret = i40e_reconstitute_veb(pf->veb[v]);
6617
6618 if (!ret)
6619 continue;
6620
6621 /* If Main VEB failed, we're in deep doodoo,
6622 * so give up rebuilding the switch and set up
6623 * for minimal rebuild of PF VSI.
6624 * If orphan failed, we'll report the error
6625 * but try to keep going.
6626 */
6627 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6628 dev_info(&pf->pdev->dev,
6629 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6630 ret);
6631 pf->vsi[pf->lan_vsi]->uplink_seid
6632 = pf->mac_seid;
6633 break;
6634 } else if (pf->veb[v]->uplink_seid == 0) {
6635 dev_info(&pf->pdev->dev,
6636 "rebuild of orphan VEB failed: %d\n",
6637 ret);
6638 }
6639 }
6640 }
6641 }
6642
6643 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006644 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006645 /* no VEB, so rebuild only the Main VSI */
6646 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6647 if (ret) {
6648 dev_info(&pf->pdev->dev,
6649 "rebuild of Main VSI failed: %d\n", ret);
6650 goto end_core_reset;
6651 }
6652 }
6653
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006654 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
6655 (pf->hw.aq.fw_maj_ver < 4)) {
6656 msleep(75);
6657 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6658 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006659 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
6660 i40e_stat_str(&pf->hw, ret),
6661 i40e_aq_str(&pf->hw,
6662 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006663 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006664 /* reinit the misc interrupt */
6665 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6666 ret = i40e_setup_misc_vector(pf);
6667
6668 /* restart the VSIs that were rebuilt and running before the reset */
6669 i40e_pf_unquiesce_all_vsi(pf);
6670
Mitch Williams69f64b22014-02-13 03:48:46 -08006671 if (pf->num_alloc_vfs) {
6672 for (v = 0; v < pf->num_alloc_vfs; v++)
6673 i40e_reset_vf(&pf->vf[v], true);
6674 }
6675
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006676 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006677 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006678
6679end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006680 clear_bit(__I40E_RESET_FAILED, &pf->state);
6681clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006682 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6683}
6684
6685/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006686 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006687 * @pf: board private structure
6688 *
6689 * Close up the VFs and other things in prep for a Core Reset,
6690 * then get ready to rebuild the world.
6691 **/
6692static void i40e_handle_reset_warning(struct i40e_pf *pf)
6693{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006694 i40e_prep_for_reset(pf);
6695 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006696}
6697
6698/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006699 * i40e_handle_mdd_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006700 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006701 *
6702 * Called from the MDD irq handler to identify possibly malicious vfs
6703 **/
6704static void i40e_handle_mdd_event(struct i40e_pf *pf)
6705{
6706 struct i40e_hw *hw = &pf->hw;
6707 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00006708 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006709 struct i40e_vf *vf;
6710 u32 reg;
6711 int i;
6712
6713 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
6714 return;
6715
6716 /* find what triggered the MDD event */
6717 reg = rd32(hw, I40E_GL_MDET_TX);
6718 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006719 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
6720 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006721 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006722 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006723 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006724 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006725 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
6726 I40E_GL_MDET_TX_QUEUE_SHIFT) -
6727 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006728 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006729 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 +00006730 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006731 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
6732 mdd_detected = true;
6733 }
6734 reg = rd32(hw, I40E_GL_MDET_RX);
6735 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006736 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
6737 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006738 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006739 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006740 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
6741 I40E_GL_MDET_RX_QUEUE_SHIFT) -
6742 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006743 if (netif_msg_rx_err(pf))
6744 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
6745 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006746 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
6747 mdd_detected = true;
6748 }
6749
Neerav Parikhdf430b12014-06-04 01:23:15 +00006750 if (mdd_detected) {
6751 reg = rd32(hw, I40E_PF_MDET_TX);
6752 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
6753 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006754 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00006755 pf_mdd_detected = true;
6756 }
6757 reg = rd32(hw, I40E_PF_MDET_RX);
6758 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
6759 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006760 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00006761 pf_mdd_detected = true;
6762 }
6763 /* Queue belongs to the PF, initiate a reset */
6764 if (pf_mdd_detected) {
6765 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6766 i40e_service_event_schedule(pf);
6767 }
6768 }
6769
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006770 /* see if one of the VFs needs its hand slapped */
6771 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
6772 vf = &(pf->vf[i]);
6773 reg = rd32(hw, I40E_VP_MDET_TX(i));
6774 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
6775 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
6776 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006777 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
6778 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006779 }
6780
6781 reg = rd32(hw, I40E_VP_MDET_RX(i));
6782 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
6783 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
6784 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006785 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
6786 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006787 }
6788
6789 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
6790 dev_info(&pf->pdev->dev,
6791 "Too many MDD events on VF %d, disabled\n", i);
6792 dev_info(&pf->pdev->dev,
6793 "Use PF Control I/F to re-enable the VF\n");
6794 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
6795 }
6796 }
6797
6798 /* re-enable mdd interrupt cause */
6799 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
6800 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
6801 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
6802 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
6803 i40e_flush(hw);
6804}
6805
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006806#ifdef CONFIG_I40E_VXLAN
6807/**
6808 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
6809 * @pf: board private structure
6810 **/
6811static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
6812{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006813 struct i40e_hw *hw = &pf->hw;
6814 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006815 __be16 port;
6816 int i;
6817
6818 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
6819 return;
6820
6821 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
6822
6823 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04006824 if (pf->pending_vxlan_bitmap & BIT_ULL(i)) {
6825 pf->pending_vxlan_bitmap &= ~BIT_ULL(i);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006826 port = pf->vxlan_ports[i];
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07006827 if (port)
6828 ret = i40e_aq_add_udp_tunnel(hw, ntohs(port),
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006829 I40E_AQC_TUNNEL_TYPE_VXLAN,
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07006830 NULL, NULL);
6831 else
6832 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006833
6834 if (ret) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07006835 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006836 "%s vxlan port %d, index %d failed, err %s aq_err %s\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07006837 port ? "add" : "delete",
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006838 ntohs(port), i,
6839 i40e_stat_str(&pf->hw, ret),
6840 i40e_aq_str(&pf->hw,
6841 pf->hw.aq.asq_last_status));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006842 pf->vxlan_ports[i] = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006843 }
6844 }
6845 }
6846}
6847
6848#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006849/**
6850 * i40e_service_task - Run the driver's async subtasks
6851 * @work: pointer to work_struct containing our data
6852 **/
6853static void i40e_service_task(struct work_struct *work)
6854{
6855 struct i40e_pf *pf = container_of(work,
6856 struct i40e_pf,
6857 service_task);
6858 unsigned long start_time = jiffies;
6859
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00006860 /* don't bother with service tasks if a reset is in progress */
6861 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6862 i40e_service_event_complete(pf);
6863 return;
6864 }
6865
Kiran Patilb03a8c12015-09-24 18:13:15 -04006866 i40e_detect_recover_hung(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006867 i40e_reset_subtask(pf);
6868 i40e_handle_mdd_event(pf);
6869 i40e_vc_process_vflr_event(pf);
6870 i40e_watchdog_subtask(pf);
6871 i40e_fdir_reinit_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006872 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006873#ifdef CONFIG_I40E_VXLAN
6874 i40e_sync_vxlan_filters_subtask(pf);
6875#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006876 i40e_clean_adminq_subtask(pf);
6877
6878 i40e_service_event_complete(pf);
6879
6880 /* If the tasks have taken longer than one timer cycle or there
6881 * is more work to be done, reschedule the service task now
6882 * rather than wait for the timer to tick again.
6883 */
6884 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
6885 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
6886 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
6887 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
6888 i40e_service_event_schedule(pf);
6889}
6890
6891/**
6892 * i40e_service_timer - timer callback
6893 * @data: pointer to PF struct
6894 **/
6895static void i40e_service_timer(unsigned long data)
6896{
6897 struct i40e_pf *pf = (struct i40e_pf *)data;
6898
6899 mod_timer(&pf->service_timer,
6900 round_jiffies(jiffies + pf->service_timer_period));
6901 i40e_service_event_schedule(pf);
6902}
6903
6904/**
6905 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
6906 * @vsi: the VSI being configured
6907 **/
6908static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
6909{
6910 struct i40e_pf *pf = vsi->back;
6911
6912 switch (vsi->type) {
6913 case I40E_VSI_MAIN:
6914 vsi->alloc_queue_pairs = pf->num_lan_qps;
6915 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6916 I40E_REQ_DESCRIPTOR_MULTIPLE);
6917 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6918 vsi->num_q_vectors = pf->num_lan_msix;
6919 else
6920 vsi->num_q_vectors = 1;
6921
6922 break;
6923
6924 case I40E_VSI_FDIR:
6925 vsi->alloc_queue_pairs = 1;
6926 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
6927 I40E_REQ_DESCRIPTOR_MULTIPLE);
6928 vsi->num_q_vectors = 1;
6929 break;
6930
6931 case I40E_VSI_VMDQ2:
6932 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
6933 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6934 I40E_REQ_DESCRIPTOR_MULTIPLE);
6935 vsi->num_q_vectors = pf->num_vmdq_msix;
6936 break;
6937
6938 case I40E_VSI_SRIOV:
6939 vsi->alloc_queue_pairs = pf->num_vf_qps;
6940 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6941 I40E_REQ_DESCRIPTOR_MULTIPLE);
6942 break;
6943
Vasu Dev38e00432014-08-01 13:27:03 -07006944#ifdef I40E_FCOE
6945 case I40E_VSI_FCOE:
6946 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
6947 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6948 I40E_REQ_DESCRIPTOR_MULTIPLE);
6949 vsi->num_q_vectors = pf->num_fcoe_msix;
6950 break;
6951
6952#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006953 default:
6954 WARN_ON(1);
6955 return -ENODATA;
6956 }
6957
6958 return 0;
6959}
6960
6961/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006962 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
6963 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006964 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006965 *
6966 * On error: returns error code (negative)
6967 * On success: returns 0
6968 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006969static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006970{
6971 int size;
6972 int ret = 0;
6973
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00006974 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006975 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
6976 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
6977 if (!vsi->tx_rings)
6978 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006979 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
6980
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006981 if (alloc_qvectors) {
6982 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00006983 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006984 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
6985 if (!vsi->q_vectors) {
6986 ret = -ENOMEM;
6987 goto err_vectors;
6988 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006989 }
6990 return ret;
6991
6992err_vectors:
6993 kfree(vsi->tx_rings);
6994 return ret;
6995}
6996
6997/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006998 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
6999 * @pf: board private structure
7000 * @type: type of VSI
7001 *
7002 * On error: returns error code (negative)
7003 * On success: returns vsi index in PF (positive)
7004 **/
7005static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7006{
7007 int ret = -ENODEV;
7008 struct i40e_vsi *vsi;
7009 int vsi_idx;
7010 int i;
7011
7012 /* Need to protect the allocation of the VSIs at the PF level */
7013 mutex_lock(&pf->switch_mutex);
7014
7015 /* VSI list may be fragmented if VSI creation/destruction has
7016 * been happening. We can afford to do a quick scan to look
7017 * for any free VSIs in the list.
7018 *
7019 * find next empty vsi slot, looping back around if necessary
7020 */
7021 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007022 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007023 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007024 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007025 i = 0;
7026 while (i < pf->next_vsi && pf->vsi[i])
7027 i++;
7028 }
7029
Mitch Williams505682c2014-05-20 08:01:37 +00007030 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007031 vsi_idx = i; /* Found one! */
7032 } else {
7033 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007034 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007035 }
7036 pf->next_vsi = ++i;
7037
7038 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7039 if (!vsi) {
7040 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007041 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007042 }
7043 vsi->type = type;
7044 vsi->back = pf;
7045 set_bit(__I40E_DOWN, &vsi->state);
7046 vsi->flags = 0;
7047 vsi->idx = vsi_idx;
7048 vsi->rx_itr_setting = pf->rx_itr_default;
7049 vsi->tx_itr_setting = pf->tx_itr_default;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007050 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7051 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007052 vsi->netdev_registered = false;
7053 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
7054 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00007055 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007056
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007057 ret = i40e_set_num_rings_in_vsi(vsi);
7058 if (ret)
7059 goto err_rings;
7060
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007061 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007062 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007063 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007064
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007065 /* Setup default MSIX irq handler for VSI */
7066 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7067
7068 pf->vsi[vsi_idx] = vsi;
7069 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007070 goto unlock_pf;
7071
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007072err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007073 pf->next_vsi = i - 1;
7074 kfree(vsi);
7075unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007076 mutex_unlock(&pf->switch_mutex);
7077 return ret;
7078}
7079
7080/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007081 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7082 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007083 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007084 *
7085 * On error: returns error code (negative)
7086 * On success: returns 0
7087 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007088static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007089{
7090 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007091 if (free_qvectors) {
7092 kfree(vsi->q_vectors);
7093 vsi->q_vectors = NULL;
7094 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007095 kfree(vsi->tx_rings);
7096 vsi->tx_rings = NULL;
7097 vsi->rx_rings = NULL;
7098}
7099
7100/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007101 * i40e_vsi_clear - Deallocate the VSI provided
7102 * @vsi: the VSI being un-configured
7103 **/
7104static int i40e_vsi_clear(struct i40e_vsi *vsi)
7105{
7106 struct i40e_pf *pf;
7107
7108 if (!vsi)
7109 return 0;
7110
7111 if (!vsi->back)
7112 goto free_vsi;
7113 pf = vsi->back;
7114
7115 mutex_lock(&pf->switch_mutex);
7116 if (!pf->vsi[vsi->idx]) {
7117 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7118 vsi->idx, vsi->idx, vsi, vsi->type);
7119 goto unlock_vsi;
7120 }
7121
7122 if (pf->vsi[vsi->idx] != vsi) {
7123 dev_err(&pf->pdev->dev,
7124 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7125 pf->vsi[vsi->idx]->idx,
7126 pf->vsi[vsi->idx],
7127 pf->vsi[vsi->idx]->type,
7128 vsi->idx, vsi, vsi->type);
7129 goto unlock_vsi;
7130 }
7131
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007132 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007133 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7134 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7135
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007136 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00007137
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007138 pf->vsi[vsi->idx] = NULL;
7139 if (vsi->idx < pf->next_vsi)
7140 pf->next_vsi = vsi->idx;
7141
7142unlock_vsi:
7143 mutex_unlock(&pf->switch_mutex);
7144free_vsi:
7145 kfree(vsi);
7146
7147 return 0;
7148}
7149
7150/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007151 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7152 * @vsi: the VSI being cleaned
7153 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007154static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007155{
7156 int i;
7157
Greg Rose8e9dca52013-12-18 13:45:53 +00007158 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007159 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007160 kfree_rcu(vsi->tx_rings[i], rcu);
7161 vsi->tx_rings[i] = NULL;
7162 vsi->rx_rings[i] = NULL;
7163 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007164 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007165}
7166
7167/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007168 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7169 * @vsi: the VSI being configured
7170 **/
7171static int i40e_alloc_rings(struct i40e_vsi *vsi)
7172{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007173 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007174 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007175 int i;
7176
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007177 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007178 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007179 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007180 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7181 if (!tx_ring)
7182 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007183
7184 tx_ring->queue_index = i;
7185 tx_ring->reg_idx = vsi->base_queue + i;
7186 tx_ring->ring_active = false;
7187 tx_ring->vsi = vsi;
7188 tx_ring->netdev = vsi->netdev;
7189 tx_ring->dev = &pf->pdev->dev;
7190 tx_ring->count = vsi->num_desc;
7191 tx_ring->size = 0;
7192 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007193 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7194 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Anjali Singhai Jain527274c2015-06-05 12:20:31 -04007195 if (vsi->back->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE)
7196 tx_ring->flags |= I40E_TXR_FLAGS_OUTER_UDP_CSUM;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007197 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007198
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007199 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007200 rx_ring->queue_index = i;
7201 rx_ring->reg_idx = vsi->base_queue + i;
7202 rx_ring->ring_active = false;
7203 rx_ring->vsi = vsi;
7204 rx_ring->netdev = vsi->netdev;
7205 rx_ring->dev = &pf->pdev->dev;
7206 rx_ring->count = vsi->num_desc;
7207 rx_ring->size = 0;
7208 rx_ring->dcb_tc = 0;
7209 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
7210 set_ring_16byte_desc_enabled(rx_ring);
7211 else
7212 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007213 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007214 }
7215
7216 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007217
7218err_out:
7219 i40e_vsi_clear_rings(vsi);
7220 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007221}
7222
7223/**
7224 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7225 * @pf: board private structure
7226 * @vectors: the number of MSI-X vectors to request
7227 *
7228 * Returns the number of vectors reserved, or error
7229 **/
7230static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7231{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007232 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7233 I40E_MIN_MSIX, vectors);
7234 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007235 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007236 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007237 vectors = 0;
7238 }
7239
7240 return vectors;
7241}
7242
7243/**
7244 * i40e_init_msix - Setup the MSIX capability
7245 * @pf: board private structure
7246 *
7247 * Work with the OS to set up the MSIX vectors needed.
7248 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007249 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007250 **/
7251static int i40e_init_msix(struct i40e_pf *pf)
7252{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007253 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007254 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007255 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007256 int v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007257
7258 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7259 return -ENODEV;
7260
7261 /* The number of vectors we'll request will be comprised of:
7262 * - Add 1 for "other" cause for Admin Queue events, etc.
7263 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007264 * - Queues being used for RSS.
7265 * We don't need as many as max_rss_size vectors.
7266 * use rss_size instead in the calculation since that
7267 * is governed by number of cpus in the system.
7268 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007269 * - The number of VMDq pairs
Vasu Dev38e00432014-08-01 13:27:03 -07007270#ifdef I40E_FCOE
7271 * - The number of FCOE qps.
7272#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007273 * Once we count this up, try the request.
7274 *
7275 * If we can't get what we want, we'll simplify to nearly nothing
7276 * and try again. If that still fails, we punt.
7277 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007278 vectors_left = hw->func_caps.num_msix_vectors;
7279 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007280
Shannon Nelson1e200e42015-02-27 09:15:24 +00007281 /* reserve one vector for miscellaneous handler */
7282 if (vectors_left) {
7283 v_budget++;
7284 vectors_left--;
7285 }
7286
7287 /* reserve vectors for the main PF traffic queues */
7288 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7289 vectors_left -= pf->num_lan_msix;
7290 v_budget += pf->num_lan_msix;
7291
7292 /* reserve one vector for sideband flow director */
7293 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7294 if (vectors_left) {
7295 v_budget++;
7296 vectors_left--;
7297 } else {
7298 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7299 }
7300 }
John W Linville83840e42015-01-14 03:06:28 +00007301
Vasu Dev38e00432014-08-01 13:27:03 -07007302#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007303 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007304 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007305 if (!vectors_left)
7306 pf->num_fcoe_msix = 0;
7307 else if (vectors_left >= pf->num_fcoe_qps)
7308 pf->num_fcoe_msix = pf->num_fcoe_qps;
7309 else
7310 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007311 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007312 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007313 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007314
Vasu Dev38e00432014-08-01 13:27:03 -07007315#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007316 /* any vectors left over go for VMDq support */
7317 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7318 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7319 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7320
7321 /* if we're short on vectors for what's desired, we limit
7322 * the queues per vmdq. If this is still more than are
7323 * available, the user will need to change the number of
7324 * queues/vectors used by the PF later with the ethtool
7325 * channels command
7326 */
7327 if (vmdq_vecs < vmdq_vecs_wanted)
7328 pf->num_vmdq_qps = 1;
7329 pf->num_vmdq_msix = pf->num_vmdq_qps;
7330
7331 v_budget += vmdq_vecs;
7332 vectors_left -= vmdq_vecs;
7333 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007334
7335 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7336 GFP_KERNEL);
7337 if (!pf->msix_entries)
7338 return -ENOMEM;
7339
7340 for (i = 0; i < v_budget; i++)
7341 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007342 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007343
Shannon Nelson3b444392015-02-26 16:15:57 +00007344 if (v_actual != v_budget) {
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007345 /* If we have limited resources, we will start with no vectors
7346 * for the special features and then allocate vectors to some
7347 * of these features based on the policy and at the end disable
7348 * the features that did not get any vectors.
7349 */
Vasu Dev38e00432014-08-01 13:27:03 -07007350#ifdef I40E_FCOE
7351 pf->num_fcoe_qps = 0;
7352 pf->num_fcoe_msix = 0;
7353#endif
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007354 pf->num_vmdq_msix = 0;
7355 }
7356
Shannon Nelson3b444392015-02-26 16:15:57 +00007357 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007358 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7359 kfree(pf->msix_entries);
7360 pf->msix_entries = NULL;
7361 return -ENODEV;
7362
Shannon Nelson3b444392015-02-26 16:15:57 +00007363 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007364 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007365 pf->num_vmdq_vsis = 0;
7366 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007367 pf->num_lan_qps = 1;
7368 pf->num_lan_msix = 1;
7369
Shannon Nelson3b444392015-02-26 16:15:57 +00007370 } else if (v_actual != v_budget) {
7371 int vec;
7372
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007373 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007374 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007375
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007376 /* Scale vector usage down */
7377 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007378 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007379 pf->num_vmdq_qps = 1;
7380 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007381
7382 /* partition out the remaining vectors */
7383 switch (vec) {
7384 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007385 pf->num_lan_msix = 1;
7386 break;
7387 case 3:
Vasu Dev38e00432014-08-01 13:27:03 -07007388#ifdef I40E_FCOE
7389 /* give one vector to FCoE */
7390 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7391 pf->num_lan_msix = 1;
7392 pf->num_fcoe_msix = 1;
7393 }
7394#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007395 pf->num_lan_msix = 2;
Vasu Dev38e00432014-08-01 13:27:03 -07007396#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007397 break;
7398 default:
Vasu Dev38e00432014-08-01 13:27:03 -07007399#ifdef I40E_FCOE
7400 /* give one vector to FCoE */
7401 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7402 pf->num_fcoe_msix = 1;
7403 vec--;
7404 }
7405#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007406 /* give the rest to the PF */
7407 pf->num_lan_msix = min_t(int, vec, pf->num_lan_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007408 break;
7409 }
7410 }
7411
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007412 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7413 (pf->num_vmdq_msix == 0)) {
7414 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7415 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7416 }
Vasu Dev38e00432014-08-01 13:27:03 -07007417#ifdef I40E_FCOE
7418
7419 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7420 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7421 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7422 }
7423#endif
Shannon Nelson3b444392015-02-26 16:15:57 +00007424 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007425}
7426
7427/**
Greg Rose90e04072014-03-06 08:59:57 +00007428 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007429 * @vsi: the VSI being configured
7430 * @v_idx: index of the vector in the vsi struct
7431 *
7432 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7433 **/
Greg Rose90e04072014-03-06 08:59:57 +00007434static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00007435{
7436 struct i40e_q_vector *q_vector;
7437
7438 /* allocate q_vector */
7439 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7440 if (!q_vector)
7441 return -ENOMEM;
7442
7443 q_vector->vsi = vsi;
7444 q_vector->v_idx = v_idx;
7445 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
7446 if (vsi->netdev)
7447 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007448 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007449
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007450 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7451 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7452
Alexander Duyck493fb302013-09-28 07:01:44 +00007453 /* tie q_vector and vsi together */
7454 vsi->q_vectors[v_idx] = q_vector;
7455
7456 return 0;
7457}
7458
7459/**
Greg Rose90e04072014-03-06 08:59:57 +00007460 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007461 * @vsi: the VSI being configured
7462 *
7463 * We allocate one q_vector per queue interrupt. If allocation fails we
7464 * return -ENOMEM.
7465 **/
Greg Rose90e04072014-03-06 08:59:57 +00007466static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007467{
7468 struct i40e_pf *pf = vsi->back;
7469 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00007470 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007471
7472 /* if not MSIX, give the one vector only to the LAN VSI */
7473 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7474 num_q_vectors = vsi->num_q_vectors;
7475 else if (vsi == pf->vsi[pf->lan_vsi])
7476 num_q_vectors = 1;
7477 else
7478 return -EINVAL;
7479
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007480 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00007481 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00007482 if (err)
7483 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007484 }
7485
7486 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007487
7488err_out:
7489 while (v_idx--)
7490 i40e_free_q_vector(vsi, v_idx);
7491
7492 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007493}
7494
7495/**
7496 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7497 * @pf: board private structure to initialize
7498 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007499static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007500{
Shannon Nelson3b444392015-02-26 16:15:57 +00007501 int vectors = 0;
7502 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007503
7504 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007505 vectors = i40e_init_msix(pf);
7506 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007507 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007508#ifdef I40E_FCOE
7509 I40E_FLAG_FCOE_ENABLED |
7510#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007511 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007512 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007513 I40E_FLAG_SRIOV_ENABLED |
7514 I40E_FLAG_FD_SB_ENABLED |
7515 I40E_FLAG_FD_ATR_ENABLED |
7516 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007517
7518 /* rework the queue expectations without MSIX */
7519 i40e_determine_queue_usage(pf);
7520 }
7521 }
7522
7523 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7524 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007525 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007526 vectors = pci_enable_msi(pf->pdev);
7527 if (vectors < 0) {
7528 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7529 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007530 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7531 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007532 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007533 }
7534
Shannon Nelson958a3e32013-09-28 07:13:28 +00007535 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007536 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007537
Shannon Nelson3b444392015-02-26 16:15:57 +00007538 /* set up vector assignment tracking */
7539 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7540 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007541 if (!pf->irq_pile) {
7542 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7543 return -ENOMEM;
7544 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007545 pf->irq_pile->num_entries = vectors;
7546 pf->irq_pile->search_hint = 0;
7547
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007548 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007549 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007550
7551 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007552}
7553
7554/**
7555 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7556 * @pf: board private structure
7557 *
7558 * This sets up the handler for MSIX 0, which is used to manage the
7559 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7560 * when in MSI or Legacy interrupt mode.
7561 **/
7562static int i40e_setup_misc_vector(struct i40e_pf *pf)
7563{
7564 struct i40e_hw *hw = &pf->hw;
7565 int err = 0;
7566
7567 /* Only request the irq if this is the first time through, and
7568 * not when we're rebuilding after a Reset
7569 */
7570 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7571 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007572 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007573 if (err) {
7574 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007575 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007576 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007577 return -EFAULT;
7578 }
7579 }
7580
Jacob Kellerab437b52014-12-14 01:55:08 +00007581 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007582
7583 /* associate no queues to the misc vector */
7584 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
7585 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
7586
7587 i40e_flush(hw);
7588
7589 i40e_irq_dynamic_enable_icr0(pf);
7590
7591 return err;
7592}
7593
7594/**
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007595 * i40e_config_rss_aq - Prepare for RSS using AQ commands
7596 * @vsi: vsi structure
7597 * @seed: RSS hash seed
7598 **/
7599static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed)
7600{
7601 struct i40e_aqc_get_set_rss_key_data rss_key;
7602 struct i40e_pf *pf = vsi->back;
7603 struct i40e_hw *hw = &pf->hw;
7604 bool pf_lut = false;
7605 u8 *rss_lut;
7606 int ret, i;
7607
7608 memset(&rss_key, 0, sizeof(rss_key));
7609 memcpy(&rss_key, seed, sizeof(rss_key));
7610
7611 rss_lut = kzalloc(pf->rss_table_size, GFP_KERNEL);
7612 if (!rss_lut)
7613 return -ENOMEM;
7614
7615 /* Populate the LUT with max no. of queues in round robin fashion */
7616 for (i = 0; i < vsi->rss_table_size; i++)
7617 rss_lut[i] = i % vsi->rss_size;
7618
7619 ret = i40e_aq_set_rss_key(hw, vsi->id, &rss_key);
7620 if (ret) {
7621 dev_info(&pf->pdev->dev,
7622 "Cannot set RSS key, err %s aq_err %s\n",
7623 i40e_stat_str(&pf->hw, ret),
7624 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai126b63d2015-08-24 13:26:53 -07007625 goto config_rss_aq_out;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007626 }
7627
7628 if (vsi->type == I40E_VSI_MAIN)
7629 pf_lut = true;
7630
7631 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, rss_lut,
7632 vsi->rss_table_size);
7633 if (ret)
7634 dev_info(&pf->pdev->dev,
7635 "Cannot set RSS lut, err %s aq_err %s\n",
7636 i40e_stat_str(&pf->hw, ret),
7637 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7638
Anjali Singhai126b63d2015-08-24 13:26:53 -07007639config_rss_aq_out:
7640 kfree(rss_lut);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007641 return ret;
7642}
7643
7644/**
7645 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
7646 * @vsi: VSI structure
7647 **/
7648static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
7649{
7650 u8 seed[I40E_HKEY_ARRAY_SIZE];
7651 struct i40e_pf *pf = vsi->back;
7652
7653 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
7654 vsi->rss_size = min_t(int, pf->rss_size, vsi->num_queue_pairs);
7655
7656 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
7657 return i40e_config_rss_aq(vsi, seed);
7658
7659 return 0;
7660}
7661
7662/**
7663 * i40e_config_rss_reg - Prepare for RSS if used
7664 * @pf: board private structure
7665 * @seed: RSS hash seed
7666 **/
7667static int i40e_config_rss_reg(struct i40e_pf *pf, const u8 *seed)
7668{
7669 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
7670 struct i40e_hw *hw = &pf->hw;
7671 u32 *seed_dw = (u32 *)seed;
7672 u32 current_queue = 0;
7673 u32 lut = 0;
7674 int i, j;
7675
7676 /* Fill out hash function seed */
7677 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
7678 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
7679
7680 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++) {
7681 lut = 0;
7682 for (j = 0; j < 4; j++) {
7683 if (current_queue == vsi->rss_size)
7684 current_queue = 0;
7685 lut |= ((current_queue) << (8 * j));
7686 current_queue++;
7687 }
7688 wr32(&pf->hw, I40E_PFQF_HLUT(i), lut);
7689 }
7690 i40e_flush(hw);
7691
7692 return 0;
7693}
7694
7695/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007696 * i40e_config_rss - Prepare for RSS if used
7697 * @pf: board private structure
7698 **/
7699static int i40e_config_rss(struct i40e_pf *pf)
7700{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00007701 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007702 u8 seed[I40E_HKEY_ARRAY_SIZE];
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00007703 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007704 u32 reg_val;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007705 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007706
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007707 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007708
7709 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
7710 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
7711 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007712 hena |= i40e_pf_get_default_rss_hena(pf);
7713
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007714 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
7715 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
7716
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00007717 vsi->rss_size = min_t(int, pf->rss_size, vsi->num_queue_pairs);
7718
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007719 /* Determine the RSS table size based on the hardware capabilities */
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007720 reg_val = rd32(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007721 reg_val = (pf->rss_table_size == 512) ?
7722 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
7723 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007724 wr32(hw, I40E_PFQF_CTL_0, reg_val);
7725
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007726 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
7727 return i40e_config_rss_aq(pf->vsi[pf->lan_vsi], seed);
7728 else
7729 return i40e_config_rss_reg(pf, seed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007730}
7731
7732/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007733 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
7734 * @pf: board private structure
7735 * @queue_count: the requested queue count for rss.
7736 *
7737 * returns 0 if rss is not enabled, if enabled returns the final rss queue
7738 * count which may be different from the requested queue count.
7739 **/
7740int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
7741{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007742 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
7743 int new_rss_size;
7744
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007745 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
7746 return 0;
7747
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007748 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007749
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007750 if (queue_count != vsi->num_queue_pairs) {
7751 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007752 i40e_prep_for_reset(pf);
7753
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007754 pf->rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007755
7756 i40e_reset_and_rebuild(pf, true);
7757 i40e_config_rss(pf);
7758 }
7759 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
7760 return pf->rss_size;
7761}
7762
7763/**
Greg Rosef4492db2015-02-06 08:52:12 +00007764 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
7765 * @pf: board private structure
7766 **/
7767i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
7768{
7769 i40e_status status;
7770 bool min_valid, max_valid;
7771 u32 max_bw, min_bw;
7772
7773 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
7774 &min_valid, &max_valid);
7775
7776 if (!status) {
7777 if (min_valid)
7778 pf->npar_min_bw = min_bw;
7779 if (max_valid)
7780 pf->npar_max_bw = max_bw;
7781 }
7782
7783 return status;
7784}
7785
7786/**
7787 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
7788 * @pf: board private structure
7789 **/
7790i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
7791{
7792 struct i40e_aqc_configure_partition_bw_data bw_data;
7793 i40e_status status;
7794
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007795 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007796 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00007797 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
7798 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
7799
7800 /* Set the new bandwidths */
7801 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
7802
7803 return status;
7804}
7805
7806/**
7807 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
7808 * @pf: board private structure
7809 **/
7810i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
7811{
7812 /* Commit temporary BW setting to permanent NVM image */
7813 enum i40e_admin_queue_err last_aq_status;
7814 i40e_status ret;
7815 u16 nvm_word;
7816
7817 if (pf->hw.partition_id != 1) {
7818 dev_info(&pf->pdev->dev,
7819 "Commit BW only works on partition 1! This is partition %d",
7820 pf->hw.partition_id);
7821 ret = I40E_NOT_SUPPORTED;
7822 goto bw_commit_out;
7823 }
7824
7825 /* Acquire NVM for read access */
7826 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
7827 last_aq_status = pf->hw.aq.asq_last_status;
7828 if (ret) {
7829 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007830 "Cannot acquire NVM for read access, err %s aq_err %s\n",
7831 i40e_stat_str(&pf->hw, ret),
7832 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00007833 goto bw_commit_out;
7834 }
7835
7836 /* Read word 0x10 of NVM - SW compatibility word 1 */
7837 ret = i40e_aq_read_nvm(&pf->hw,
7838 I40E_SR_NVM_CONTROL_WORD,
7839 0x10, sizeof(nvm_word), &nvm_word,
7840 false, NULL);
7841 /* Save off last admin queue command status before releasing
7842 * the NVM
7843 */
7844 last_aq_status = pf->hw.aq.asq_last_status;
7845 i40e_release_nvm(&pf->hw);
7846 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007847 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
7848 i40e_stat_str(&pf->hw, ret),
7849 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00007850 goto bw_commit_out;
7851 }
7852
7853 /* Wait a bit for NVM release to complete */
7854 msleep(50);
7855
7856 /* Acquire NVM for write access */
7857 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
7858 last_aq_status = pf->hw.aq.asq_last_status;
7859 if (ret) {
7860 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007861 "Cannot acquire NVM for write access, err %s aq_err %s\n",
7862 i40e_stat_str(&pf->hw, ret),
7863 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00007864 goto bw_commit_out;
7865 }
7866 /* Write it back out unchanged to initiate update NVM,
7867 * which will force a write of the shadow (alt) RAM to
7868 * the NVM - thus storing the bandwidth values permanently.
7869 */
7870 ret = i40e_aq_update_nvm(&pf->hw,
7871 I40E_SR_NVM_CONTROL_WORD,
7872 0x10, sizeof(nvm_word),
7873 &nvm_word, true, NULL);
7874 /* Save off last admin queue command status before releasing
7875 * the NVM
7876 */
7877 last_aq_status = pf->hw.aq.asq_last_status;
7878 i40e_release_nvm(&pf->hw);
7879 if (ret)
7880 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007881 "BW settings NOT SAVED, err %s aq_err %s\n",
7882 i40e_stat_str(&pf->hw, ret),
7883 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00007884bw_commit_out:
7885
7886 return ret;
7887}
7888
7889/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007890 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
7891 * @pf: board private structure to initialize
7892 *
7893 * i40e_sw_init initializes the Adapter private data structure.
7894 * Fields are initialized based on PCI device information and
7895 * OS network device settings (MTU size).
7896 **/
7897static int i40e_sw_init(struct i40e_pf *pf)
7898{
7899 int err = 0;
7900 int size;
7901
7902 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
7903 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00007904 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007905 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
7906 if (I40E_DEBUG_USER & debug)
7907 pf->hw.debug_mask = debug;
7908 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
7909 I40E_DEFAULT_MSG_ENABLE);
7910 }
7911
7912 /* Set default capability flags */
7913 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
7914 I40E_FLAG_MSI_ENABLED |
Shannon Nelson9ac77262015-08-27 11:42:40 -04007915 I40E_FLAG_LINK_POLLING_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00007916 I40E_FLAG_MSIX_ENABLED;
7917
7918 if (iommu_present(&pci_bus_type))
7919 pf->flags |= I40E_FLAG_RX_PS_ENABLED;
7920 else
7921 pf->flags |= I40E_FLAG_RX_1BUF_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007922
Mitch Williamsca99eb92014-04-04 04:43:07 +00007923 /* Set default ITR */
7924 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
7925 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
7926
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00007927 /* Depending on PF configurations, it is possible that the RSS
7928 * maximum might end up larger than the available queues
7929 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007930 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Paul M Stillwell Jrec9a7db2014-07-09 07:46:10 +00007931 pf->rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007932 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00007933 pf->rss_size_max = min_t(int, pf->rss_size_max,
7934 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007935 if (pf->hw.func_caps.rss) {
7936 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00007937 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007938 }
7939
Catherine Sullivan2050bc62013-12-18 13:46:03 +00007940 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04007941 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00007942 pf->flags |= I40E_FLAG_MFP_ENABLED;
7943 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00007944 if (i40e_get_npar_bw_setting(pf))
7945 dev_warn(&pf->pdev->dev,
7946 "Could not get NPAR bw settings\n");
7947 else
7948 dev_info(&pf->pdev->dev,
7949 "Min BW = %8.8x, Max BW = %8.8x\n",
7950 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00007951 }
7952
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007953 /* FW/NVM is not yet fixed in this regard */
7954 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
7955 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
7956 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
7957 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007958 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007959 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007960 } else {
7961 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00007962 "Flow Director Sideband mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007963 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007964 pf->fdir_pf_filter_count =
7965 pf->hw.func_caps.fd_filters_guaranteed;
7966 pf->hw.fdir_shared_filter_count =
7967 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007968 }
7969
7970 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007971 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007972 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007973 }
7974
Vasu Dev38e00432014-08-01 13:27:03 -07007975#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04007976 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07007977
7978#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007979#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f12014-12-11 07:06:34 +00007980 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007981 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
7982 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
7983 pf->num_req_vfs = min_t(int,
7984 pf->hw.func_caps.num_vfs,
7985 I40E_MAX_VF_COUNT);
7986 }
7987#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04007988 if (pf->hw.mac.type == I40E_MAC_X722) {
7989 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
7990 I40E_FLAG_128_QP_RSS_CAPABLE |
7991 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
7992 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
7993 I40E_FLAG_WB_ON_ITR_CAPABLE |
7994 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE;
7995 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007996 pf->eeprom_version = 0xDEAD;
7997 pf->lan_veb = I40E_NO_VEB;
7998 pf->lan_vsi = I40E_NO_VSI;
7999
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008000 /* By default FW has this off for performance reasons */
8001 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8002
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008003 /* set up queue assignment tracking */
8004 size = sizeof(struct i40e_lump_tracking)
8005 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8006 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8007 if (!pf->qp_pile) {
8008 err = -ENOMEM;
8009 goto sw_init_done;
8010 }
8011 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8012 pf->qp_pile->search_hint = 0;
8013
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008014 pf->tx_timeout_recovery_level = 1;
8015
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008016 mutex_init(&pf->switch_mutex);
8017
Greg Rosec668a122015-02-26 16:10:39 +00008018 /* If NPAR is enabled nudge the Tx scheduler */
8019 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8020 i40e_set_npar_bw_setting(pf);
8021
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008022sw_init_done:
8023 return err;
8024}
8025
8026/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008027 * i40e_set_ntuple - set the ntuple feature flag and take action
8028 * @pf: board private structure to initialize
8029 * @features: the feature set that the stack is suggesting
8030 *
8031 * returns a bool to indicate if reset needs to happen
8032 **/
8033bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8034{
8035 bool need_reset = false;
8036
8037 /* Check if Flow Director n-tuple support was enabled or disabled. If
8038 * the state changed, we need to reset.
8039 */
8040 if (features & NETIF_F_NTUPLE) {
8041 /* Enable filters and mark for reset */
8042 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8043 need_reset = true;
8044 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8045 } else {
8046 /* turn off filters, mark for reset and clear SW filter list */
8047 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8048 need_reset = true;
8049 i40e_fdir_filter_exit(pf);
8050 }
8051 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008052 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008053 /* reset fd counters */
8054 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8055 pf->fdir_pf_active_filters = 0;
8056 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04008057 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8058 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008059 /* if ATR was auto disabled it can be re-enabled. */
8060 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8061 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
8062 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008063 }
8064 return need_reset;
8065}
8066
8067/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008068 * i40e_set_features - set the netdev feature flags
8069 * @netdev: ptr to the netdev being adjusted
8070 * @features: the feature set that the stack is suggesting
8071 **/
8072static int i40e_set_features(struct net_device *netdev,
8073 netdev_features_t features)
8074{
8075 struct i40e_netdev_priv *np = netdev_priv(netdev);
8076 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008077 struct i40e_pf *pf = vsi->back;
8078 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008079
8080 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8081 i40e_vlan_stripping_enable(vsi);
8082 else
8083 i40e_vlan_stripping_disable(vsi);
8084
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008085 need_reset = i40e_set_ntuple(pf, features);
8086
8087 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008088 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008089
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008090 return 0;
8091}
8092
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008093#ifdef CONFIG_I40E_VXLAN
8094/**
8095 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
8096 * @pf: board private structure
8097 * @port: The UDP port to look up
8098 *
8099 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8100 **/
8101static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
8102{
8103 u8 i;
8104
8105 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
8106 if (pf->vxlan_ports[i] == port)
8107 return i;
8108 }
8109
8110 return i;
8111}
8112
8113/**
8114 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
8115 * @netdev: This physical port's netdev
8116 * @sa_family: Socket Family that VXLAN is notifying us about
8117 * @port: New UDP port number that VXLAN started listening to
8118 **/
8119static void i40e_add_vxlan_port(struct net_device *netdev,
8120 sa_family_t sa_family, __be16 port)
8121{
8122 struct i40e_netdev_priv *np = netdev_priv(netdev);
8123 struct i40e_vsi *vsi = np->vsi;
8124 struct i40e_pf *pf = vsi->back;
8125 u8 next_idx;
8126 u8 idx;
8127
8128 if (sa_family == AF_INET6)
8129 return;
8130
8131 idx = i40e_get_vxlan_port_idx(pf, port);
8132
8133 /* Check if port already exists */
8134 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008135 netdev_info(netdev, "vxlan port %d already offloaded\n",
8136 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008137 return;
8138 }
8139
8140 /* Now check if there is space to add the new port */
8141 next_idx = i40e_get_vxlan_port_idx(pf, 0);
8142
8143 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008144 netdev_info(netdev, "maximum number of vxlan UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008145 ntohs(port));
8146 return;
8147 }
8148
8149 /* New port: add it and mark its index in the bitmap */
8150 pf->vxlan_ports[next_idx] = port;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008151 pf->pending_vxlan_bitmap |= BIT_ULL(next_idx);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008152 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
8153}
8154
8155/**
8156 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
8157 * @netdev: This physical port's netdev
8158 * @sa_family: Socket Family that VXLAN is notifying us about
8159 * @port: UDP port number that VXLAN stopped listening to
8160 **/
8161static void i40e_del_vxlan_port(struct net_device *netdev,
8162 sa_family_t sa_family, __be16 port)
8163{
8164 struct i40e_netdev_priv *np = netdev_priv(netdev);
8165 struct i40e_vsi *vsi = np->vsi;
8166 struct i40e_pf *pf = vsi->back;
8167 u8 idx;
8168
8169 if (sa_family == AF_INET6)
8170 return;
8171
8172 idx = i40e_get_vxlan_port_idx(pf, port);
8173
8174 /* Check if port already exists */
8175 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8176 /* if port exists, set it to 0 (mark for deletion)
8177 * and make it pending
8178 */
8179 pf->vxlan_ports[idx] = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008180 pf->pending_vxlan_bitmap |= BIT_ULL(idx);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008181 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
8182 } else {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008183 netdev_warn(netdev, "vxlan port %d was not found, not deleting\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008184 ntohs(port));
8185 }
8186}
8187
8188#endif
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008189static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008190 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008191{
8192 struct i40e_netdev_priv *np = netdev_priv(netdev);
8193 struct i40e_pf *pf = np->vsi->back;
8194 struct i40e_hw *hw = &pf->hw;
8195
8196 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8197 return -EOPNOTSUPP;
8198
8199 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8200 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8201
8202 return 0;
8203}
8204
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008205/**
8206 * i40e_ndo_fdb_add - add an entry to the hardware database
8207 * @ndm: the input from the stack
8208 * @tb: pointer to array of nladdr (unused)
8209 * @dev: the net device pointer
8210 * @addr: the MAC address entry being added
8211 * @flags: instructions from stack about fdb operation
8212 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008213static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8214 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008215 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008216 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008217{
8218 struct i40e_netdev_priv *np = netdev_priv(dev);
8219 struct i40e_pf *pf = np->vsi->back;
8220 int err = 0;
8221
8222 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8223 return -EOPNOTSUPP;
8224
Or Gerlitz65891fe2014-12-14 18:19:05 +02008225 if (vid) {
8226 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8227 return -EINVAL;
8228 }
8229
Greg Rose4ba0dea2014-03-06 08:59:55 +00008230 /* Hardware does not support aging addresses so if a
8231 * ndm_state is given only allow permanent addresses
8232 */
8233 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
8234 netdev_info(dev, "FDB only supports static addresses\n");
8235 return -EINVAL;
8236 }
8237
8238 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
8239 err = dev_uc_add_excl(dev, addr);
8240 else if (is_multicast_ether_addr(addr))
8241 err = dev_mc_add_excl(dev, addr);
8242 else
8243 err = -EINVAL;
8244
8245 /* Only return duplicate errors if NLM_F_EXCL is set */
8246 if (err == -EEXIST && !(flags & NLM_F_EXCL))
8247 err = 0;
8248
8249 return err;
8250}
8251
Neerav Parikh51616012015-02-06 08:52:14 +00008252/**
8253 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
8254 * @dev: the netdev being configured
8255 * @nlh: RTNL message
8256 *
8257 * Inserts a new hardware bridge if not already created and
8258 * enables the bridging mode requested (VEB or VEPA). If the
8259 * hardware bridge has already been inserted and the request
8260 * is to change the mode then that requires a PF reset to
8261 * allow rebuild of the components with required hardware
8262 * bridge mode enabled.
8263 **/
8264static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04008265 struct nlmsghdr *nlh,
8266 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00008267{
8268 struct i40e_netdev_priv *np = netdev_priv(dev);
8269 struct i40e_vsi *vsi = np->vsi;
8270 struct i40e_pf *pf = vsi->back;
8271 struct i40e_veb *veb = NULL;
8272 struct nlattr *attr, *br_spec;
8273 int i, rem;
8274
8275 /* Only for PF VSI for now */
8276 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8277 return -EOPNOTSUPP;
8278
8279 /* Find the HW bridge for PF VSI */
8280 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8281 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8282 veb = pf->veb[i];
8283 }
8284
8285 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8286
8287 nla_for_each_nested(attr, br_spec, rem) {
8288 __u16 mode;
8289
8290 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8291 continue;
8292
8293 mode = nla_get_u16(attr);
8294 if ((mode != BRIDGE_MODE_VEPA) &&
8295 (mode != BRIDGE_MODE_VEB))
8296 return -EINVAL;
8297
8298 /* Insert a new HW bridge */
8299 if (!veb) {
8300 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8301 vsi->tc_config.enabled_tc);
8302 if (veb) {
8303 veb->bridge_mode = mode;
8304 i40e_config_bridge_mode(veb);
8305 } else {
8306 /* No Bridge HW offload available */
8307 return -ENOENT;
8308 }
8309 break;
8310 } else if (mode != veb->bridge_mode) {
8311 /* Existing HW bridge but different mode needs reset */
8312 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008313 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
8314 if (mode == BRIDGE_MODE_VEB)
8315 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
8316 else
8317 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
8318 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00008319 break;
8320 }
8321 }
8322
8323 return 0;
8324}
8325
8326/**
8327 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
8328 * @skb: skb buff
8329 * @pid: process id
8330 * @seq: RTNL message seq #
8331 * @dev: the netdev being configured
8332 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04008333 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00008334 *
8335 * Return the mode in which the hardware bridge is operating in
8336 * i.e VEB or VEPA.
8337 **/
Neerav Parikh51616012015-02-06 08:52:14 +00008338static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8339 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04008340 u32 __always_unused filter_mask,
8341 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00008342{
8343 struct i40e_netdev_priv *np = netdev_priv(dev);
8344 struct i40e_vsi *vsi = np->vsi;
8345 struct i40e_pf *pf = vsi->back;
8346 struct i40e_veb *veb = NULL;
8347 int i;
8348
8349 /* Only for PF VSI for now */
8350 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8351 return -EOPNOTSUPP;
8352
8353 /* Find the HW bridge for the PF VSI */
8354 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8355 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8356 veb = pf->veb[i];
8357 }
8358
8359 if (!veb)
8360 return 0;
8361
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02008362 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07008363 nlflags, 0, 0, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00008364}
Neerav Parikh51616012015-02-06 08:52:14 +00008365
Joe Stringerf44a75e2015-04-14 17:09:14 -07008366#define I40E_MAX_TUNNEL_HDR_LEN 80
8367/**
8368 * i40e_features_check - Validate encapsulated packet conforms to limits
8369 * @skb: skb buff
8370 * @netdev: This physical port's netdev
8371 * @features: Offload features that the stack believes apply
8372 **/
8373static netdev_features_t i40e_features_check(struct sk_buff *skb,
8374 struct net_device *dev,
8375 netdev_features_t features)
8376{
8377 if (skb->encapsulation &&
8378 (skb_inner_mac_header(skb) - skb_transport_header(skb) >
8379 I40E_MAX_TUNNEL_HDR_LEN))
8380 return features & ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
8381
8382 return features;
8383}
8384
Shannon Nelson37a29732015-02-27 09:15:19 +00008385static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008386 .ndo_open = i40e_open,
8387 .ndo_stop = i40e_close,
8388 .ndo_start_xmit = i40e_lan_xmit_frame,
8389 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
8390 .ndo_set_rx_mode = i40e_set_rx_mode,
8391 .ndo_validate_addr = eth_validate_addr,
8392 .ndo_set_mac_address = i40e_set_mac,
8393 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008394 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008395 .ndo_tx_timeout = i40e_tx_timeout,
8396 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
8397 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
8398#ifdef CONFIG_NET_POLL_CONTROLLER
8399 .ndo_poll_controller = i40e_netpoll,
8400#endif
8401 .ndo_setup_tc = i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07008402#ifdef I40E_FCOE
8403 .ndo_fcoe_enable = i40e_fcoe_enable,
8404 .ndo_fcoe_disable = i40e_fcoe_disable,
8405#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008406 .ndo_set_features = i40e_set_features,
8407 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
8408 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04008409 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008410 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00008411 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00008412 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008413#ifdef CONFIG_I40E_VXLAN
8414 .ndo_add_vxlan_port = i40e_add_vxlan_port,
8415 .ndo_del_vxlan_port = i40e_del_vxlan_port,
8416#endif
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008417 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008418 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07008419 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00008420 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
8421 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008422};
8423
8424/**
8425 * i40e_config_netdev - Setup the netdev flags
8426 * @vsi: the VSI being configured
8427 *
8428 * Returns 0 on success, negative value on failure
8429 **/
8430static int i40e_config_netdev(struct i40e_vsi *vsi)
8431{
Greg Rose1a103702013-11-28 06:42:39 +00008432 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008433 struct i40e_pf *pf = vsi->back;
8434 struct i40e_hw *hw = &pf->hw;
8435 struct i40e_netdev_priv *np;
8436 struct net_device *netdev;
8437 u8 mac_addr[ETH_ALEN];
8438 int etherdev_size;
8439
8440 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008441 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008442 if (!netdev)
8443 return -ENOMEM;
8444
8445 vsi->netdev = netdev;
8446 np = netdev_priv(netdev);
8447 np->vsi = vsi;
8448
Or Gerlitzd70e9412014-03-18 10:36:45 +02008449 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008450 NETIF_F_GSO_UDP_TUNNEL |
Or Gerlitzd70e9412014-03-18 10:36:45 +02008451 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008452
8453 netdev->features = NETIF_F_SG |
8454 NETIF_F_IP_CSUM |
8455 NETIF_F_SCTP_CSUM |
8456 NETIF_F_HIGHDMA |
8457 NETIF_F_GSO_UDP_TUNNEL |
8458 NETIF_F_HW_VLAN_CTAG_TX |
8459 NETIF_F_HW_VLAN_CTAG_RX |
8460 NETIF_F_HW_VLAN_CTAG_FILTER |
8461 NETIF_F_IPV6_CSUM |
8462 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00008463 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008464 NETIF_F_TSO6 |
8465 NETIF_F_RXCSUM |
8466 NETIF_F_RXHASH |
8467 0;
8468
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00008469 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
8470 netdev->features |= NETIF_F_NTUPLE;
8471
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008472 /* copy netdev features into list of user selectable features */
8473 netdev->hw_features |= netdev->features;
8474
8475 if (vsi->type == I40E_VSI_MAIN) {
8476 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00008477 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Shannon Nelson30650cc2014-07-29 04:01:50 +00008478 /* The following steps are necessary to prevent reception
8479 * of tagged packets - some older NVM configurations load a
8480 * default a MAC-VLAN filter that accepts any tagged packet
8481 * which must be replaced by a normal filter.
Greg Rose8c27d422014-05-22 06:31:56 +00008482 */
Shannon Nelson30650cc2014-07-29 04:01:50 +00008483 if (!i40e_rm_default_mac_filter(vsi, mac_addr))
8484 i40e_add_filter(vsi, mac_addr,
8485 I40E_VLAN_ANY, false, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008486 } else {
8487 /* relate the VSI_VMDQ name to the VSI_MAIN name */
8488 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
8489 pf->vsi[pf->lan_vsi]->netdev->name);
8490 random_ether_addr(mac_addr);
8491 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
8492 }
Greg Rose1a103702013-11-28 06:42:39 +00008493 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008494
Greg Rose9a173902014-05-22 06:32:02 +00008495 ether_addr_copy(netdev->dev_addr, mac_addr);
8496 ether_addr_copy(netdev->perm_addr, mac_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008497 /* vlan gets same features (except vlan offload)
8498 * after any tweaks for specific VSI types
8499 */
8500 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
8501 NETIF_F_HW_VLAN_CTAG_RX |
8502 NETIF_F_HW_VLAN_CTAG_FILTER);
8503 netdev->priv_flags |= IFF_UNICAST_FLT;
8504 netdev->priv_flags |= IFF_SUPP_NOFCS;
8505 /* Setup netdev TC information */
8506 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
8507
8508 netdev->netdev_ops = &i40e_netdev_ops;
8509 netdev->watchdog_timeo = 5 * HZ;
8510 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07008511#ifdef I40E_FCOE
8512 i40e_fcoe_config_netdev(netdev, vsi);
8513#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008514
8515 return 0;
8516}
8517
8518/**
8519 * i40e_vsi_delete - Delete a VSI from the switch
8520 * @vsi: the VSI being removed
8521 *
8522 * Returns 0 on success, negative value on failure
8523 **/
8524static void i40e_vsi_delete(struct i40e_vsi *vsi)
8525{
8526 /* remove default VSI is not allowed */
8527 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
8528 return;
8529
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008530 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008531}
8532
8533/**
Neerav Parikh51616012015-02-06 08:52:14 +00008534 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
8535 * @vsi: the VSI being queried
8536 *
8537 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
8538 **/
8539int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
8540{
8541 struct i40e_veb *veb;
8542 struct i40e_pf *pf = vsi->back;
8543
8544 /* Uplink is not a bridge so default to VEB */
8545 if (vsi->veb_idx == I40E_NO_VEB)
8546 return 1;
8547
8548 veb = pf->veb[vsi->veb_idx];
8549 /* Uplink is a bridge in VEPA mode */
8550 if (veb && (veb->bridge_mode & BRIDGE_MODE_VEPA))
8551 return 0;
8552
8553 /* Uplink is a bridge in VEB mode */
8554 return 1;
8555}
8556
8557/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008558 * i40e_add_vsi - Add a VSI to the switch
8559 * @vsi: the VSI being configured
8560 *
8561 * This initializes a VSI context depending on the VSI type to be added and
8562 * passes it down to the add_vsi aq command.
8563 **/
8564static int i40e_add_vsi(struct i40e_vsi *vsi)
8565{
8566 int ret = -ENODEV;
8567 struct i40e_mac_filter *f, *ftmp;
8568 struct i40e_pf *pf = vsi->back;
8569 struct i40e_hw *hw = &pf->hw;
8570 struct i40e_vsi_context ctxt;
8571 u8 enabled_tc = 0x1; /* TC0 enabled */
8572 int f_count = 0;
8573
8574 memset(&ctxt, 0, sizeof(ctxt));
8575 switch (vsi->type) {
8576 case I40E_VSI_MAIN:
8577 /* The PF's main VSI is already setup as part of the
8578 * device initialization, so we'll not bother with
8579 * the add_vsi call, but we will retrieve the current
8580 * VSI context.
8581 */
8582 ctxt.seid = pf->main_vsi_seid;
8583 ctxt.pf_num = pf->hw.pf_id;
8584 ctxt.vf_num = 0;
8585 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8586 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8587 if (ret) {
8588 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008589 "couldn't get PF vsi config, err %s aq_err %s\n",
8590 i40e_stat_str(&pf->hw, ret),
8591 i40e_aq_str(&pf->hw,
8592 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008593 return -ENOENT;
8594 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07008595 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008596 vsi->info.valid_sections = 0;
8597
8598 vsi->seid = ctxt.seid;
8599 vsi->id = ctxt.vsi_number;
8600
8601 enabled_tc = i40e_pf_get_tc_map(pf);
8602
8603 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00008604 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
8605 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008606 memset(&ctxt, 0, sizeof(ctxt));
8607 ctxt.seid = pf->main_vsi_seid;
8608 ctxt.pf_num = pf->hw.pf_id;
8609 ctxt.vf_num = 0;
8610 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
8611 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
8612 if (ret) {
8613 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008614 "update vsi failed, err %s aq_err %s\n",
8615 i40e_stat_str(&pf->hw, ret),
8616 i40e_aq_str(&pf->hw,
8617 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008618 ret = -ENOENT;
8619 goto err;
8620 }
8621 /* update the local VSI info queue map */
8622 i40e_vsi_update_queue_map(vsi, &ctxt);
8623 vsi->info.valid_sections = 0;
8624 } else {
8625 /* Default/Main VSI is only enabled for TC0
8626 * reconfigure it to enable all TCs that are
8627 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00008628 * For MFP case the iSCSI PF would use this
8629 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008630 */
8631 ret = i40e_vsi_config_tc(vsi, enabled_tc);
8632 if (ret) {
8633 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008634 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
8635 enabled_tc,
8636 i40e_stat_str(&pf->hw, ret),
8637 i40e_aq_str(&pf->hw,
8638 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008639 ret = -ENOENT;
8640 }
8641 }
8642 break;
8643
8644 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008645 ctxt.pf_num = hw->pf_id;
8646 ctxt.vf_num = 0;
8647 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00008648 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008649 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008650 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
8651 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00008652 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008653 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00008654 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008655 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00008656 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008657 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008658 break;
8659
8660 case I40E_VSI_VMDQ2:
8661 ctxt.pf_num = hw->pf_id;
8662 ctxt.vf_num = 0;
8663 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00008664 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008665 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
8666
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008667 /* This VSI is connected to VEB so the switch_id
8668 * should be set to zero by default.
8669 */
Neerav Parikh51616012015-02-06 08:52:14 +00008670 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
8671 ctxt.info.valid_sections |=
8672 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8673 ctxt.info.switch_id =
8674 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8675 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008676
8677 /* Setup the VSI tx/rx queue map for TC0 only for now */
8678 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
8679 break;
8680
8681 case I40E_VSI_SRIOV:
8682 ctxt.pf_num = hw->pf_id;
8683 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
8684 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00008685 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008686 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
8687
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008688 /* This VSI is connected to VEB so the switch_id
8689 * should be set to zero by default.
8690 */
Neerav Parikh51616012015-02-06 08:52:14 +00008691 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
8692 ctxt.info.valid_sections |=
8693 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8694 ctxt.info.switch_id =
8695 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8696 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008697
8698 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
8699 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00008700 if (pf->vf[vsi->vf_id].spoofchk) {
8701 ctxt.info.valid_sections |=
8702 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
8703 ctxt.info.sec_flags |=
8704 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
8705 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
8706 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008707 /* Setup the VSI tx/rx queue map for TC0 only for now */
8708 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
8709 break;
8710
Vasu Dev38e00432014-08-01 13:27:03 -07008711#ifdef I40E_FCOE
8712 case I40E_VSI_FCOE:
8713 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
8714 if (ret) {
8715 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
8716 return ret;
8717 }
8718 break;
8719
8720#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008721 default:
8722 return -ENODEV;
8723 }
8724
8725 if (vsi->type != I40E_VSI_MAIN) {
8726 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
8727 if (ret) {
8728 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008729 "add vsi failed, err %s aq_err %s\n",
8730 i40e_stat_str(&pf->hw, ret),
8731 i40e_aq_str(&pf->hw,
8732 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008733 ret = -ENOENT;
8734 goto err;
8735 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07008736 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008737 vsi->info.valid_sections = 0;
8738 vsi->seid = ctxt.seid;
8739 vsi->id = ctxt.vsi_number;
8740 }
8741
8742 /* If macvlan filters already exist, force them to get loaded */
8743 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
8744 f->changed = true;
8745 f_count++;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00008746
8747 if (f->is_laa && vsi->type == I40E_VSI_MAIN) {
Shannon Nelson30650cc2014-07-29 04:01:50 +00008748 struct i40e_aqc_remove_macvlan_element_data element;
8749
8750 memset(&element, 0, sizeof(element));
8751 ether_addr_copy(element.mac_addr, f->macaddr);
8752 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
8753 ret = i40e_aq_remove_macvlan(hw, vsi->seid,
8754 &element, 1, NULL);
8755 if (ret) {
8756 /* some older FW has a different default */
8757 element.flags |=
8758 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
8759 i40e_aq_remove_macvlan(hw, vsi->seid,
8760 &element, 1, NULL);
8761 }
8762
8763 i40e_aq_mac_address_write(hw,
Shannon Nelson6252c7e2014-06-04 01:23:23 +00008764 I40E_AQC_WRITE_TYPE_LAA_WOL,
8765 f->macaddr, NULL);
8766 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008767 }
8768 if (f_count) {
8769 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
8770 pf->flags |= I40E_FLAG_FILTER_SYNC;
8771 }
8772
8773 /* Update VSI BW information */
8774 ret = i40e_vsi_get_bw_info(vsi);
8775 if (ret) {
8776 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008777 "couldn't get vsi bw info, err %s aq_err %s\n",
8778 i40e_stat_str(&pf->hw, ret),
8779 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008780 /* VSI is already added so not tearing that up */
8781 ret = 0;
8782 }
8783
8784err:
8785 return ret;
8786}
8787
8788/**
8789 * i40e_vsi_release - Delete a VSI and free its resources
8790 * @vsi: the VSI being removed
8791 *
8792 * Returns 0 on success or < 0 on error
8793 **/
8794int i40e_vsi_release(struct i40e_vsi *vsi)
8795{
8796 struct i40e_mac_filter *f, *ftmp;
8797 struct i40e_veb *veb = NULL;
8798 struct i40e_pf *pf;
8799 u16 uplink_seid;
8800 int i, n;
8801
8802 pf = vsi->back;
8803
8804 /* release of a VEB-owner or last VSI is not allowed */
8805 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
8806 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
8807 vsi->seid, vsi->uplink_seid);
8808 return -ENODEV;
8809 }
8810 if (vsi == pf->vsi[pf->lan_vsi] &&
8811 !test_bit(__I40E_DOWN, &pf->state)) {
8812 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
8813 return -ENODEV;
8814 }
8815
8816 uplink_seid = vsi->uplink_seid;
8817 if (vsi->type != I40E_VSI_SRIOV) {
8818 if (vsi->netdev_registered) {
8819 vsi->netdev_registered = false;
8820 if (vsi->netdev) {
8821 /* results in a call to i40e_close() */
8822 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008823 }
8824 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00008825 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008826 }
8827 i40e_vsi_disable_irq(vsi);
8828 }
8829
8830 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
8831 i40e_del_filter(vsi, f->macaddr, f->vlan,
8832 f->is_vf, f->is_netdev);
Anjali Singhai30e25612015-09-28 13:37:12 -07008833 i40e_sync_vsi_filters(vsi, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008834
8835 i40e_vsi_delete(vsi);
8836 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00008837 if (vsi->netdev) {
8838 free_netdev(vsi->netdev);
8839 vsi->netdev = NULL;
8840 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008841 i40e_vsi_clear_rings(vsi);
8842 i40e_vsi_clear(vsi);
8843
8844 /* If this was the last thing on the VEB, except for the
8845 * controlling VSI, remove the VEB, which puts the controlling
8846 * VSI onto the next level down in the switch.
8847 *
8848 * Well, okay, there's one more exception here: don't remove
8849 * the orphan VEBs yet. We'll wait for an explicit remove request
8850 * from up the network stack.
8851 */
Mitch Williams505682c2014-05-20 08:01:37 +00008852 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008853 if (pf->vsi[i] &&
8854 pf->vsi[i]->uplink_seid == uplink_seid &&
8855 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
8856 n++; /* count the VSIs */
8857 }
8858 }
8859 for (i = 0; i < I40E_MAX_VEB; i++) {
8860 if (!pf->veb[i])
8861 continue;
8862 if (pf->veb[i]->uplink_seid == uplink_seid)
8863 n++; /* count the VEBs */
8864 if (pf->veb[i]->seid == uplink_seid)
8865 veb = pf->veb[i];
8866 }
8867 if (n == 0 && veb && veb->uplink_seid != 0)
8868 i40e_veb_release(veb);
8869
8870 return 0;
8871}
8872
8873/**
8874 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
8875 * @vsi: ptr to the VSI
8876 *
8877 * This should only be called after i40e_vsi_mem_alloc() which allocates the
8878 * corresponding SW VSI structure and initializes num_queue_pairs for the
8879 * newly allocated VSI.
8880 *
8881 * Returns 0 on success or negative on failure
8882 **/
8883static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
8884{
8885 int ret = -ENOENT;
8886 struct i40e_pf *pf = vsi->back;
8887
Alexander Duyck493fb302013-09-28 07:01:44 +00008888 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008889 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
8890 vsi->seid);
8891 return -EEXIST;
8892 }
8893
8894 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00008895 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008896 vsi->seid, vsi->base_vector);
8897 return -EEXIST;
8898 }
8899
Greg Rose90e04072014-03-06 08:59:57 +00008900 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008901 if (ret) {
8902 dev_info(&pf->pdev->dev,
8903 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
8904 vsi->num_q_vectors, vsi->seid, ret);
8905 vsi->num_q_vectors = 0;
8906 goto vector_setup_out;
8907 }
8908
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04008909 /* In Legacy mode, we do not have to get any other vector since we
8910 * piggyback on the misc/ICR0 for queue interrupts.
8911 */
8912 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8913 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00008914 if (vsi->num_q_vectors)
8915 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
8916 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008917 if (vsi->base_vector < 0) {
8918 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00008919 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
8920 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008921 i40e_vsi_free_q_vectors(vsi);
8922 ret = -ENOENT;
8923 goto vector_setup_out;
8924 }
8925
8926vector_setup_out:
8927 return ret;
8928}
8929
8930/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008931 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
8932 * @vsi: pointer to the vsi.
8933 *
8934 * This re-allocates a vsi's queue resources.
8935 *
8936 * Returns pointer to the successfully allocated and configured VSI sw struct
8937 * on success, otherwise returns NULL on failure.
8938 **/
8939static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
8940{
8941 struct i40e_pf *pf = vsi->back;
8942 u8 enabled_tc;
8943 int ret;
8944
8945 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
8946 i40e_vsi_clear_rings(vsi);
8947
8948 i40e_vsi_free_arrays(vsi, false);
8949 i40e_set_num_rings_in_vsi(vsi);
8950 ret = i40e_vsi_alloc_arrays(vsi, false);
8951 if (ret)
8952 goto err_vsi;
8953
8954 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
8955 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00008956 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008957 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00008958 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008959 goto err_vsi;
8960 }
8961 vsi->base_queue = ret;
8962
8963 /* Update the FW view of the VSI. Force a reset of TC and queue
8964 * layout configurations.
8965 */
8966 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
8967 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
8968 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
8969 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
8970
8971 /* assign it some queues */
8972 ret = i40e_alloc_rings(vsi);
8973 if (ret)
8974 goto err_rings;
8975
8976 /* map all of the rings to the q_vectors */
8977 i40e_vsi_map_rings_to_vectors(vsi);
8978 return vsi;
8979
8980err_rings:
8981 i40e_vsi_free_q_vectors(vsi);
8982 if (vsi->netdev_registered) {
8983 vsi->netdev_registered = false;
8984 unregister_netdev(vsi->netdev);
8985 free_netdev(vsi->netdev);
8986 vsi->netdev = NULL;
8987 }
8988 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
8989err_vsi:
8990 i40e_vsi_clear(vsi);
8991 return NULL;
8992}
8993
8994/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008995 * i40e_vsi_setup - Set up a VSI by a given type
8996 * @pf: board private structure
8997 * @type: VSI type
8998 * @uplink_seid: the switch element to link to
8999 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9000 *
9001 * This allocates the sw VSI structure and its queue resources, then add a VSI
9002 * to the identified VEB.
9003 *
9004 * Returns pointer to the successfully allocated and configure VSI sw struct on
9005 * success, otherwise returns NULL on failure.
9006 **/
9007struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9008 u16 uplink_seid, u32 param1)
9009{
9010 struct i40e_vsi *vsi = NULL;
9011 struct i40e_veb *veb = NULL;
9012 int ret, i;
9013 int v_idx;
9014
9015 /* The requested uplink_seid must be either
9016 * - the PF's port seid
9017 * no VEB is needed because this is the PF
9018 * or this is a Flow Director special case VSI
9019 * - seid of an existing VEB
9020 * - seid of a VSI that owns an existing VEB
9021 * - seid of a VSI that doesn't own a VEB
9022 * a new VEB is created and the VSI becomes the owner
9023 * - seid of the PF VSI, which is what creates the first VEB
9024 * this is a special case of the previous
9025 *
9026 * Find which uplink_seid we were given and create a new VEB if needed
9027 */
9028 for (i = 0; i < I40E_MAX_VEB; i++) {
9029 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9030 veb = pf->veb[i];
9031 break;
9032 }
9033 }
9034
9035 if (!veb && uplink_seid != pf->mac_seid) {
9036
Mitch Williams505682c2014-05-20 08:01:37 +00009037 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009038 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9039 vsi = pf->vsi[i];
9040 break;
9041 }
9042 }
9043 if (!vsi) {
9044 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9045 uplink_seid);
9046 return NULL;
9047 }
9048
9049 if (vsi->uplink_seid == pf->mac_seid)
9050 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9051 vsi->tc_config.enabled_tc);
9052 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9053 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9054 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009055 if (veb) {
9056 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9057 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009058 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009059 return NULL;
9060 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009061 /* We come up by default in VEPA mode if SRIOV is not
9062 * already enabled, in which case we can't force VEPA
9063 * mode.
9064 */
9065 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9066 veb->bridge_mode = BRIDGE_MODE_VEPA;
9067 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9068 }
Neerav Parikh51616012015-02-06 08:52:14 +00009069 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009070 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009071 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9072 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9073 veb = pf->veb[i];
9074 }
9075 if (!veb) {
9076 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9077 return NULL;
9078 }
9079
9080 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9081 uplink_seid = veb->seid;
9082 }
9083
9084 /* get vsi sw struct */
9085 v_idx = i40e_vsi_mem_alloc(pf, type);
9086 if (v_idx < 0)
9087 goto err_alloc;
9088 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009089 if (!vsi)
9090 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009091 vsi->type = type;
9092 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9093
9094 if (type == I40E_VSI_MAIN)
9095 pf->lan_vsi = v_idx;
9096 else if (type == I40E_VSI_SRIOV)
9097 vsi->vf_id = param1;
9098 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009099 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9100 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009101 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009102 dev_info(&pf->pdev->dev,
9103 "failed to get tracking for %d queues for VSI %d err=%d\n",
9104 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009105 goto err_vsi;
9106 }
9107 vsi->base_queue = ret;
9108
9109 /* get a VSI from the hardware */
9110 vsi->uplink_seid = uplink_seid;
9111 ret = i40e_add_vsi(vsi);
9112 if (ret)
9113 goto err_vsi;
9114
9115 switch (vsi->type) {
9116 /* setup the netdev if needed */
9117 case I40E_VSI_MAIN:
9118 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07009119 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009120 ret = i40e_config_netdev(vsi);
9121 if (ret)
9122 goto err_netdev;
9123 ret = register_netdev(vsi->netdev);
9124 if (ret)
9125 goto err_netdev;
9126 vsi->netdev_registered = true;
9127 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009128#ifdef CONFIG_I40E_DCB
9129 /* Setup DCB netlink interface */
9130 i40e_dcbnl_setup(vsi);
9131#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009132 /* fall through */
9133
9134 case I40E_VSI_FDIR:
9135 /* set up vectors and rings if needed */
9136 ret = i40e_vsi_setup_vectors(vsi);
9137 if (ret)
9138 goto err_msix;
9139
9140 ret = i40e_alloc_rings(vsi);
9141 if (ret)
9142 goto err_rings;
9143
9144 /* map all of the rings to the q_vectors */
9145 i40e_vsi_map_rings_to_vectors(vsi);
9146
9147 i40e_vsi_reset_stats(vsi);
9148 break;
9149
9150 default:
9151 /* no netdev or rings for the other VSI types */
9152 break;
9153 }
9154
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04009155 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
9156 (vsi->type == I40E_VSI_VMDQ2)) {
9157 ret = i40e_vsi_config_rss(vsi);
9158 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009159 return vsi;
9160
9161err_rings:
9162 i40e_vsi_free_q_vectors(vsi);
9163err_msix:
9164 if (vsi->netdev_registered) {
9165 vsi->netdev_registered = false;
9166 unregister_netdev(vsi->netdev);
9167 free_netdev(vsi->netdev);
9168 vsi->netdev = NULL;
9169 }
9170err_netdev:
9171 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9172err_vsi:
9173 i40e_vsi_clear(vsi);
9174err_alloc:
9175 return NULL;
9176}
9177
9178/**
9179 * i40e_veb_get_bw_info - Query VEB BW information
9180 * @veb: the veb to query
9181 *
9182 * Query the Tx scheduler BW configuration data for given VEB
9183 **/
9184static int i40e_veb_get_bw_info(struct i40e_veb *veb)
9185{
9186 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
9187 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
9188 struct i40e_pf *pf = veb->pf;
9189 struct i40e_hw *hw = &pf->hw;
9190 u32 tc_bw_max;
9191 int ret = 0;
9192 int i;
9193
9194 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
9195 &bw_data, NULL);
9196 if (ret) {
9197 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009198 "query veb bw config failed, err %s aq_err %s\n",
9199 i40e_stat_str(&pf->hw, ret),
9200 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009201 goto out;
9202 }
9203
9204 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
9205 &ets_data, NULL);
9206 if (ret) {
9207 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009208 "query veb bw ets config failed, err %s aq_err %s\n",
9209 i40e_stat_str(&pf->hw, ret),
9210 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009211 goto out;
9212 }
9213
9214 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
9215 veb->bw_max_quanta = ets_data.tc_bw_max;
9216 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +00009217 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009218 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
9219 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
9220 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
9221 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
9222 veb->bw_tc_limit_credits[i] =
9223 le16_to_cpu(bw_data.tc_bw_limits[i]);
9224 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
9225 }
9226
9227out:
9228 return ret;
9229}
9230
9231/**
9232 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
9233 * @pf: board private structure
9234 *
9235 * On error: returns error code (negative)
9236 * On success: returns vsi index in PF (positive)
9237 **/
9238static int i40e_veb_mem_alloc(struct i40e_pf *pf)
9239{
9240 int ret = -ENOENT;
9241 struct i40e_veb *veb;
9242 int i;
9243
9244 /* Need to protect the allocation of switch elements at the PF level */
9245 mutex_lock(&pf->switch_mutex);
9246
9247 /* VEB list may be fragmented if VEB creation/destruction has
9248 * been happening. We can afford to do a quick scan to look
9249 * for any free slots in the list.
9250 *
9251 * find next empty veb slot, looping back around if necessary
9252 */
9253 i = 0;
9254 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
9255 i++;
9256 if (i >= I40E_MAX_VEB) {
9257 ret = -ENOMEM;
9258 goto err_alloc_veb; /* out of VEB slots! */
9259 }
9260
9261 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
9262 if (!veb) {
9263 ret = -ENOMEM;
9264 goto err_alloc_veb;
9265 }
9266 veb->pf = pf;
9267 veb->idx = i;
9268 veb->enabled_tc = 1;
9269
9270 pf->veb[i] = veb;
9271 ret = i;
9272err_alloc_veb:
9273 mutex_unlock(&pf->switch_mutex);
9274 return ret;
9275}
9276
9277/**
9278 * i40e_switch_branch_release - Delete a branch of the switch tree
9279 * @branch: where to start deleting
9280 *
9281 * This uses recursion to find the tips of the branch to be
9282 * removed, deleting until we get back to and can delete this VEB.
9283 **/
9284static void i40e_switch_branch_release(struct i40e_veb *branch)
9285{
9286 struct i40e_pf *pf = branch->pf;
9287 u16 branch_seid = branch->seid;
9288 u16 veb_idx = branch->idx;
9289 int i;
9290
9291 /* release any VEBs on this VEB - RECURSION */
9292 for (i = 0; i < I40E_MAX_VEB; i++) {
9293 if (!pf->veb[i])
9294 continue;
9295 if (pf->veb[i]->uplink_seid == branch->seid)
9296 i40e_switch_branch_release(pf->veb[i]);
9297 }
9298
9299 /* Release the VSIs on this VEB, but not the owner VSI.
9300 *
9301 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
9302 * the VEB itself, so don't use (*branch) after this loop.
9303 */
Mitch Williams505682c2014-05-20 08:01:37 +00009304 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009305 if (!pf->vsi[i])
9306 continue;
9307 if (pf->vsi[i]->uplink_seid == branch_seid &&
9308 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9309 i40e_vsi_release(pf->vsi[i]);
9310 }
9311 }
9312
9313 /* There's one corner case where the VEB might not have been
9314 * removed, so double check it here and remove it if needed.
9315 * This case happens if the veb was created from the debugfs
9316 * commands and no VSIs were added to it.
9317 */
9318 if (pf->veb[veb_idx])
9319 i40e_veb_release(pf->veb[veb_idx]);
9320}
9321
9322/**
9323 * i40e_veb_clear - remove veb struct
9324 * @veb: the veb to remove
9325 **/
9326static void i40e_veb_clear(struct i40e_veb *veb)
9327{
9328 if (!veb)
9329 return;
9330
9331 if (veb->pf) {
9332 struct i40e_pf *pf = veb->pf;
9333
9334 mutex_lock(&pf->switch_mutex);
9335 if (pf->veb[veb->idx] == veb)
9336 pf->veb[veb->idx] = NULL;
9337 mutex_unlock(&pf->switch_mutex);
9338 }
9339
9340 kfree(veb);
9341}
9342
9343/**
9344 * i40e_veb_release - Delete a VEB and free its resources
9345 * @veb: the VEB being removed
9346 **/
9347void i40e_veb_release(struct i40e_veb *veb)
9348{
9349 struct i40e_vsi *vsi = NULL;
9350 struct i40e_pf *pf;
9351 int i, n = 0;
9352
9353 pf = veb->pf;
9354
9355 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +00009356 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009357 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
9358 n++;
9359 vsi = pf->vsi[i];
9360 }
9361 }
9362 if (n != 1) {
9363 dev_info(&pf->pdev->dev,
9364 "can't remove VEB %d with %d VSIs left\n",
9365 veb->seid, n);
9366 return;
9367 }
9368
9369 /* move the remaining VSI to uplink veb */
9370 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
9371 if (veb->uplink_seid) {
9372 vsi->uplink_seid = veb->uplink_seid;
9373 if (veb->uplink_seid == pf->mac_seid)
9374 vsi->veb_idx = I40E_NO_VEB;
9375 else
9376 vsi->veb_idx = veb->veb_idx;
9377 } else {
9378 /* floating VEB */
9379 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
9380 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
9381 }
9382
9383 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
9384 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009385}
9386
9387/**
9388 * i40e_add_veb - create the VEB in the switch
9389 * @veb: the VEB to be instantiated
9390 * @vsi: the controlling VSI
9391 **/
9392static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
9393{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009394 struct i40e_pf *pf = veb->pf;
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04009395 bool is_default = veb->pf->cur_promisc;
Kevin Scotte1c51b952013-11-20 10:02:51 +00009396 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009397 int ret;
9398
9399 /* get a VEB from the hardware */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009400 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00009401 veb->enabled_tc, is_default,
9402 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009403 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009404 dev_info(&pf->pdev->dev,
9405 "couldn't add VEB, err %s aq_err %s\n",
9406 i40e_stat_str(&pf->hw, ret),
9407 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009408 return -EPERM;
9409 }
9410
9411 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009412 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009413 &veb->stats_idx, NULL, NULL, NULL);
9414 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009415 dev_info(&pf->pdev->dev,
9416 "couldn't get VEB statistics idx, err %s aq_err %s\n",
9417 i40e_stat_str(&pf->hw, ret),
9418 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009419 return -EPERM;
9420 }
9421 ret = i40e_veb_get_bw_info(veb);
9422 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009423 dev_info(&pf->pdev->dev,
9424 "couldn't get VEB bw info, err %s aq_err %s\n",
9425 i40e_stat_str(&pf->hw, ret),
9426 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9427 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009428 return -ENOENT;
9429 }
9430
9431 vsi->uplink_seid = veb->seid;
9432 vsi->veb_idx = veb->idx;
9433 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9434
9435 return 0;
9436}
9437
9438/**
9439 * i40e_veb_setup - Set up a VEB
9440 * @pf: board private structure
9441 * @flags: VEB setup flags
9442 * @uplink_seid: the switch element to link to
9443 * @vsi_seid: the initial VSI seid
9444 * @enabled_tc: Enabled TC bit-map
9445 *
9446 * This allocates the sw VEB structure and links it into the switch
9447 * It is possible and legal for this to be a duplicate of an already
9448 * existing VEB. It is also possible for both uplink and vsi seids
9449 * to be zero, in order to create a floating VEB.
9450 *
9451 * Returns pointer to the successfully allocated VEB sw struct on
9452 * success, otherwise returns NULL on failure.
9453 **/
9454struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
9455 u16 uplink_seid, u16 vsi_seid,
9456 u8 enabled_tc)
9457{
9458 struct i40e_veb *veb, *uplink_veb = NULL;
9459 int vsi_idx, veb_idx;
9460 int ret;
9461
9462 /* if one seid is 0, the other must be 0 to create a floating relay */
9463 if ((uplink_seid == 0 || vsi_seid == 0) &&
9464 (uplink_seid + vsi_seid != 0)) {
9465 dev_info(&pf->pdev->dev,
9466 "one, not both seid's are 0: uplink=%d vsi=%d\n",
9467 uplink_seid, vsi_seid);
9468 return NULL;
9469 }
9470
9471 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +00009472 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009473 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
9474 break;
Mitch Williams505682c2014-05-20 08:01:37 +00009475 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009476 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
9477 vsi_seid);
9478 return NULL;
9479 }
9480
9481 if (uplink_seid && uplink_seid != pf->mac_seid) {
9482 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
9483 if (pf->veb[veb_idx] &&
9484 pf->veb[veb_idx]->seid == uplink_seid) {
9485 uplink_veb = pf->veb[veb_idx];
9486 break;
9487 }
9488 }
9489 if (!uplink_veb) {
9490 dev_info(&pf->pdev->dev,
9491 "uplink seid %d not found\n", uplink_seid);
9492 return NULL;
9493 }
9494 }
9495
9496 /* get veb sw struct */
9497 veb_idx = i40e_veb_mem_alloc(pf);
9498 if (veb_idx < 0)
9499 goto err_alloc;
9500 veb = pf->veb[veb_idx];
9501 veb->flags = flags;
9502 veb->uplink_seid = uplink_seid;
9503 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
9504 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
9505
9506 /* create the VEB in the switch */
9507 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
9508 if (ret)
9509 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +00009510 if (vsi_idx == pf->lan_vsi)
9511 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009512
9513 return veb;
9514
9515err_veb:
9516 i40e_veb_clear(veb);
9517err_alloc:
9518 return NULL;
9519}
9520
9521/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00009522 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009523 * @pf: board private structure
9524 * @ele: element we are building info from
9525 * @num_reported: total number of elements
9526 * @printconfig: should we print the contents
9527 *
9528 * helper function to assist in extracting a few useful SEID values.
9529 **/
9530static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
9531 struct i40e_aqc_switch_config_element_resp *ele,
9532 u16 num_reported, bool printconfig)
9533{
9534 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
9535 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
9536 u8 element_type = ele->element_type;
9537 u16 seid = le16_to_cpu(ele->seid);
9538
9539 if (printconfig)
9540 dev_info(&pf->pdev->dev,
9541 "type=%d seid=%d uplink=%d downlink=%d\n",
9542 element_type, seid, uplink_seid, downlink_seid);
9543
9544 switch (element_type) {
9545 case I40E_SWITCH_ELEMENT_TYPE_MAC:
9546 pf->mac_seid = seid;
9547 break;
9548 case I40E_SWITCH_ELEMENT_TYPE_VEB:
9549 /* Main VEB? */
9550 if (uplink_seid != pf->mac_seid)
9551 break;
9552 if (pf->lan_veb == I40E_NO_VEB) {
9553 int v;
9554
9555 /* find existing or else empty VEB */
9556 for (v = 0; v < I40E_MAX_VEB; v++) {
9557 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
9558 pf->lan_veb = v;
9559 break;
9560 }
9561 }
9562 if (pf->lan_veb == I40E_NO_VEB) {
9563 v = i40e_veb_mem_alloc(pf);
9564 if (v < 0)
9565 break;
9566 pf->lan_veb = v;
9567 }
9568 }
9569
9570 pf->veb[pf->lan_veb]->seid = seid;
9571 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
9572 pf->veb[pf->lan_veb]->pf = pf;
9573 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
9574 break;
9575 case I40E_SWITCH_ELEMENT_TYPE_VSI:
9576 if (num_reported != 1)
9577 break;
9578 /* This is immediately after a reset so we can assume this is
9579 * the PF's VSI
9580 */
9581 pf->mac_seid = uplink_seid;
9582 pf->pf_seid = downlink_seid;
9583 pf->main_vsi_seid = seid;
9584 if (printconfig)
9585 dev_info(&pf->pdev->dev,
9586 "pf_seid=%d main_vsi_seid=%d\n",
9587 pf->pf_seid, pf->main_vsi_seid);
9588 break;
9589 case I40E_SWITCH_ELEMENT_TYPE_PF:
9590 case I40E_SWITCH_ELEMENT_TYPE_VF:
9591 case I40E_SWITCH_ELEMENT_TYPE_EMP:
9592 case I40E_SWITCH_ELEMENT_TYPE_BMC:
9593 case I40E_SWITCH_ELEMENT_TYPE_PE:
9594 case I40E_SWITCH_ELEMENT_TYPE_PA:
9595 /* ignore these for now */
9596 break;
9597 default:
9598 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
9599 element_type, seid);
9600 break;
9601 }
9602}
9603
9604/**
9605 * i40e_fetch_switch_configuration - Get switch config from firmware
9606 * @pf: board private structure
9607 * @printconfig: should we print the contents
9608 *
9609 * Get the current switch configuration from the device and
9610 * extract a few useful SEID values.
9611 **/
9612int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
9613{
9614 struct i40e_aqc_get_switch_config_resp *sw_config;
9615 u16 next_seid = 0;
9616 int ret = 0;
9617 u8 *aq_buf;
9618 int i;
9619
9620 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
9621 if (!aq_buf)
9622 return -ENOMEM;
9623
9624 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
9625 do {
9626 u16 num_reported, num_total;
9627
9628 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
9629 I40E_AQ_LARGE_BUF,
9630 &next_seid, NULL);
9631 if (ret) {
9632 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009633 "get switch config failed err %s aq_err %s\n",
9634 i40e_stat_str(&pf->hw, ret),
9635 i40e_aq_str(&pf->hw,
9636 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009637 kfree(aq_buf);
9638 return -ENOENT;
9639 }
9640
9641 num_reported = le16_to_cpu(sw_config->header.num_reported);
9642 num_total = le16_to_cpu(sw_config->header.num_total);
9643
9644 if (printconfig)
9645 dev_info(&pf->pdev->dev,
9646 "header: %d reported %d total\n",
9647 num_reported, num_total);
9648
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009649 for (i = 0; i < num_reported; i++) {
9650 struct i40e_aqc_switch_config_element_resp *ele =
9651 &sw_config->element[i];
9652
9653 i40e_setup_pf_switch_element(pf, ele, num_reported,
9654 printconfig);
9655 }
9656 } while (next_seid != 0);
9657
9658 kfree(aq_buf);
9659 return ret;
9660}
9661
9662/**
9663 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
9664 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009665 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009666 *
9667 * Returns 0 on success, negative value on failure
9668 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009669static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009670{
9671 int ret;
9672
9673 /* find out what's out there already */
9674 ret = i40e_fetch_switch_configuration(pf, false);
9675 if (ret) {
9676 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009677 "couldn't fetch switch config, err %s aq_err %s\n",
9678 i40e_stat_str(&pf->hw, ret),
9679 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009680 return ret;
9681 }
9682 i40e_pf_reset_stats(pf);
9683
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009684 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009685 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009686 struct i40e_vsi *vsi = NULL;
9687 u16 uplink_seid;
9688
9689 /* Set up the PF VSI associated with the PF's main VSI
9690 * that is already in the HW switch
9691 */
9692 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
9693 uplink_seid = pf->veb[pf->lan_veb]->seid;
9694 else
9695 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009696 if (pf->lan_vsi == I40E_NO_VSI)
9697 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
9698 else if (reinit)
9699 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009700 if (!vsi) {
9701 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
9702 i40e_fdir_teardown(pf);
9703 return -EAGAIN;
9704 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009705 } else {
9706 /* force a reset of TC and queue layout configurations */
9707 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04009708
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009709 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9710 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9711 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9712 }
9713 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
9714
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009715 i40e_fdir_sb_setup(pf);
9716
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009717 /* Setup static PF queue filter control settings */
9718 ret = i40e_setup_pf_filter_control(pf);
9719 if (ret) {
9720 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
9721 ret);
9722 /* Failure here should not stop continuing other steps */
9723 }
9724
9725 /* enable RSS in the HW, even for only one queue, as the stack can use
9726 * the hash
9727 */
9728 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
9729 i40e_config_rss(pf);
9730
9731 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -04009732 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009733 i40e_link_event(pf);
9734
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00009735 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009736 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
9737 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00009738
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009739 i40e_ptp_init(pf);
9740
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009741 return ret;
9742}
9743
9744/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009745 * i40e_determine_queue_usage - Work out queue distribution
9746 * @pf: board private structure
9747 **/
9748static void i40e_determine_queue_usage(struct i40e_pf *pf)
9749{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009750 int queues_left;
9751
9752 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07009753#ifdef I40E_FCOE
9754 pf->num_fcoe_qps = 0;
9755#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009756
9757 /* Find the max queues to be put into basic use. We'll always be
9758 * using TC0, whether or not DCB is running, and TC0 will get the
9759 * big RSS set.
9760 */
9761 queues_left = pf->hw.func_caps.num_tx_qp;
9762
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009763 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +00009764 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009765 /* one qp for PF, no queues for anything else */
9766 queues_left = 0;
9767 pf->rss_size = pf->num_lan_qps = 1;
9768
9769 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009770 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07009771#ifdef I40E_FCOE
9772 I40E_FLAG_FCOE_ENABLED |
9773#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009774 I40E_FLAG_FD_SB_ENABLED |
9775 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009776 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009777 I40E_FLAG_SRIOV_ENABLED |
9778 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +00009779 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
9780 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +00009781 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009782 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +00009783 /* one qp for PF */
9784 pf->rss_size = pf->num_lan_qps = 1;
9785 queues_left -= pf->num_lan_qps;
9786
9787 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07009788#ifdef I40E_FCOE
9789 I40E_FLAG_FCOE_ENABLED |
9790#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +00009791 I40E_FLAG_FD_SB_ENABLED |
9792 I40E_FLAG_FD_ATR_ENABLED |
9793 I40E_FLAG_DCB_ENABLED |
9794 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009795 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009796 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009797 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009798 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009799 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009800 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
9801 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00009802 pf->num_lan_qps = max_t(int, pf->rss_size_max,
9803 num_online_cpus());
9804 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
9805 pf->hw.func_caps.num_tx_qp);
9806
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009807 queues_left -= pf->num_lan_qps;
9808 }
9809
Vasu Dev38e00432014-08-01 13:27:03 -07009810#ifdef I40E_FCOE
9811 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
9812 if (I40E_DEFAULT_FCOE <= queues_left) {
9813 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
9814 } else if (I40E_MINIMUM_FCOE <= queues_left) {
9815 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
9816 } else {
9817 pf->num_fcoe_qps = 0;
9818 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
9819 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
9820 }
9821
9822 queues_left -= pf->num_fcoe_qps;
9823 }
9824
9825#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009826 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9827 if (queues_left > 1) {
9828 queues_left -= 1; /* save 1 queue for FD */
9829 } else {
9830 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9831 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
9832 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009833 }
9834
9835 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
9836 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009837 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
9838 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009839 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
9840 }
9841
9842 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
9843 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
9844 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
9845 (queues_left / pf->num_vmdq_qps));
9846 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
9847 }
9848
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009849 pf->queues_left = queues_left;
Vasu Dev38e00432014-08-01 13:27:03 -07009850#ifdef I40E_FCOE
9851 dev_info(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
9852#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009853}
9854
9855/**
9856 * i40e_setup_pf_filter_control - Setup PF static filter control
9857 * @pf: PF to be setup
9858 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00009859 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009860 * settings. If PE/FCoE are enabled then it will also set the per PF
9861 * based filter sizes required for them. It also enables Flow director,
9862 * ethertype and macvlan type filter settings for the pf.
9863 *
9864 * Returns 0 on success, negative on failure
9865 **/
9866static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
9867{
9868 struct i40e_filter_control_settings *settings = &pf->filter_settings;
9869
9870 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
9871
9872 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009873 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009874 settings->enable_fdir = true;
9875
9876 /* Ethtype and MACVLAN filters enabled for PF */
9877 settings->enable_ethtype = true;
9878 settings->enable_macvlan = true;
9879
9880 if (i40e_set_filter_control(&pf->hw, settings))
9881 return -ENOENT;
9882
9883 return 0;
9884}
9885
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009886#define INFO_STRING_LEN 255
9887static void i40e_print_features(struct i40e_pf *pf)
9888{
9889 struct i40e_hw *hw = &pf->hw;
9890 char *buf, *string;
9891
9892 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
9893 if (!string) {
9894 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
9895 return;
9896 }
9897
9898 buf = string;
9899
9900 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
9901#ifdef CONFIG_PCI_IOV
9902 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
9903#endif
Mitch Williamsaba237d2015-02-06 08:52:17 +00009904 buf += sprintf(buf, "VSIs: %d QP: %d RX: %s ",
9905 pf->hw.func_caps.num_vsis,
9906 pf->vsi[pf->lan_vsi]->num_queue_pairs,
9907 pf->flags & I40E_FLAG_RX_PS_ENABLED ? "PS" : "1BUF");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009908
9909 if (pf->flags & I40E_FLAG_RSS_ENABLED)
9910 buf += sprintf(buf, "RSS ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009911 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00009912 buf += sprintf(buf, "FD_ATR ");
9913 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9914 buf += sprintf(buf, "FD_SB ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009915 buf += sprintf(buf, "NTUPLE ");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00009916 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009917 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009918 buf += sprintf(buf, "DCB ");
Jesse Brandeburgce6fcb32015-08-28 17:55:59 -04009919#if IS_ENABLED(CONFIG_VXLAN)
9920 buf += sprintf(buf, "VxLAN ");
9921#endif
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009922 if (pf->flags & I40E_FLAG_PTP)
9923 buf += sprintf(buf, "PTP ");
Vasu Dev38e00432014-08-01 13:27:03 -07009924#ifdef I40E_FCOE
9925 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
9926 buf += sprintf(buf, "FCOE ");
9927#endif
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009928
9929 BUG_ON(buf > (string + INFO_STRING_LEN));
9930 dev_info(&pf->pdev->dev, "%s\n", string);
9931 kfree(string);
9932}
9933
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009934/**
9935 * i40e_probe - Device initialization routine
9936 * @pdev: PCI device information struct
9937 * @ent: entry in i40e_pci_tbl
9938 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00009939 * i40e_probe initializes a PF identified by a pci_dev structure.
9940 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009941 * and a hardware reset occur.
9942 *
9943 * Returns 0 on success, negative on failure
9944 **/
9945static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
9946{
Catherine Sullivane8278452015-02-06 08:52:08 +00009947 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009948 struct i40e_pf *pf;
9949 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00009950 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -04009951 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00009952 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009953 int err = 0;
9954 u32 len;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00009955 u32 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009956
9957 err = pci_enable_device_mem(pdev);
9958 if (err)
9959 return err;
9960
9961 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +00009962 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00009963 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00009964 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
9965 if (err) {
9966 dev_err(&pdev->dev,
9967 "DMA configuration failed: 0x%x\n", err);
9968 goto err_dma;
9969 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009970 }
9971
9972 /* set up pci connections */
9973 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
9974 IORESOURCE_MEM), i40e_driver_name);
9975 if (err) {
9976 dev_info(&pdev->dev,
9977 "pci_request_selected_regions failed %d\n", err);
9978 goto err_pci_reg;
9979 }
9980
9981 pci_enable_pcie_error_reporting(pdev);
9982 pci_set_master(pdev);
9983
9984 /* Now that we have a PCI connection, we need to do the
9985 * low level device setup. This is primarily setting up
9986 * the Admin Queue structures and then querying for the
9987 * device's current profile information.
9988 */
9989 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
9990 if (!pf) {
9991 err = -ENOMEM;
9992 goto err_pf_alloc;
9993 }
9994 pf->next_vsi = 0;
9995 pf->pdev = pdev;
9996 set_bit(__I40E_DOWN, &pf->state);
9997
9998 hw = &pf->hw;
9999 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010000
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010001 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10002 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010003
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010004 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010005 if (!hw->hw_addr) {
10006 err = -EIO;
10007 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10008 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010009 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010010 goto err_ioremap;
10011 }
10012 hw->vendor_id = pdev->vendor;
10013 hw->device_id = pdev->device;
10014 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10015 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10016 hw->subsystem_device_id = pdev->subsystem_device;
10017 hw->bus.device = PCI_SLOT(pdev->devfn);
10018 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010019 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010020
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010021 if (debug != -1) {
10022 pf->msg_enable = pf->hw.debug_mask;
10023 pf->msg_enable = debug;
10024 }
10025
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010026 /* do a special CORER for clearing PXE mode once at init */
10027 if (hw->revision_id == 0 &&
10028 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10029 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10030 i40e_flush(hw);
10031 msleep(200);
10032 pf->corer_count++;
10033
10034 i40e_clear_pxe_mode(hw);
10035 }
10036
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010037 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010038 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010039 err = i40e_pf_reset(hw);
10040 if (err) {
10041 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10042 goto err_pf_reset;
10043 }
10044 pf->pfr_count++;
10045
10046 hw->aq.num_arq_entries = I40E_AQ_LEN;
10047 hw->aq.num_asq_entries = I40E_AQ_LEN;
10048 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10049 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10050 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010051
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010052 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010053 "%s-%s:misc",
10054 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010055
10056 err = i40e_init_shared_code(hw);
10057 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010058 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10059 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010060 goto err_pf_reset;
10061 }
10062
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010063 /* set up a default setting for link flow control */
10064 pf->hw.fc.requested_mode = I40E_FC_NONE;
10065
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010066 err = i40e_init_adminq(hw);
10067 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010068
10069 /* provide additional fw info, like api and ver */
10070 dev_info(&pdev->dev, "fw_version:%d.%d.%05d\n",
10071 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build);
10072 dev_info(&pdev->dev, "fw api version:%d.%d\n",
10073 hw->aq.api_maj_ver, hw->aq.api_min_ver);
10074
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010075 if (err) {
10076 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010077 "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");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010078 goto err_pf_reset;
10079 }
10080
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010081 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10082 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010083 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010084 "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");
10085 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
10086 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000010087 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010088 "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 +000010089
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010090 i40e_verify_eeprom(pf);
10091
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000010092 /* Rev 0 hardware was never productized */
10093 if (hw->revision_id < 1)
10094 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");
10095
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000010096 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010097 err = i40e_get_capabilities(pf);
10098 if (err)
10099 goto err_adminq_setup;
10100
10101 err = i40e_sw_init(pf);
10102 if (err) {
10103 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
10104 goto err_sw_init;
10105 }
10106
10107 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10108 hw->func_caps.num_rx_qp,
10109 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
10110 if (err) {
10111 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
10112 goto err_init_lan_hmc;
10113 }
10114
10115 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10116 if (err) {
10117 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
10118 err = -ENOENT;
10119 goto err_configure_lan_hmc;
10120 }
10121
Neerav Parikhb686ece2014-12-14 01:55:11 +000010122 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
10123 * Ignore error return codes because if it was already disabled via
10124 * hardware settings this will fail
10125 */
10126 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
10127 (pf->hw.aq.fw_maj_ver < 4)) {
10128 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
10129 i40e_aq_stop_lldp(hw, true, NULL);
10130 }
10131
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010132 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000010133 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010134 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
10135 err = -EIO;
10136 goto err_mac_addr;
10137 }
10138 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000010139 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010140 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
10141 if (is_valid_ether_addr(hw->mac.port_addr))
10142 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070010143#ifdef I40E_FCOE
10144 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
10145 if (err)
10146 dev_info(&pdev->dev,
10147 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
10148 if (!is_valid_ether_addr(hw->mac.san_addr)) {
10149 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
10150 hw->mac.san_addr);
10151 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
10152 }
10153 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
10154#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010155
10156 pci_set_drvdata(pdev, pf);
10157 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010158#ifdef CONFIG_I40E_DCB
10159 err = i40e_init_pf_dcb(pf);
10160 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000010161 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010162 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Neerav Parikh014269f2014-04-01 07:11:48 +000010163 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010164 }
10165#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010166
10167 /* set up periodic task facility */
10168 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
10169 pf->service_timer_period = HZ;
10170
10171 INIT_WORK(&pf->service_task, i40e_service_task);
10172 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
10173 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010174
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010175 /* NVM bit on means WoL disabled for the port */
10176 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
10177 if ((1 << hw->port) & wol_nvm_bits || hw->partition_id != 1)
10178 pf->wol_en = false;
10179 else
10180 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010181 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
10182
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010183 /* set up the main switch operations */
10184 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010185 err = i40e_init_interrupt_scheme(pf);
10186 if (err)
10187 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010188
Mitch Williams505682c2014-05-20 08:01:37 +000010189 /* The number of VSIs reported by the FW is the minimum guaranteed
10190 * to us; HW supports far more and we share the remaining pool with
10191 * the other PFs. We allocate space for more than the guarantee with
10192 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010193 */
Mitch Williams505682c2014-05-20 08:01:37 +000010194 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
10195 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
10196 else
10197 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
10198
10199 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
10200 len = sizeof(struct i40e_vsi *) * pf->num_alloc_vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010201 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000010202 if (!pf->vsi) {
10203 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010204 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000010205 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010206
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010207#ifdef CONFIG_PCI_IOV
10208 /* prep for VF support */
10209 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10210 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10211 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
10212 if (pci_num_vf(pdev))
10213 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
10214 }
10215#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010216 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010217 if (err) {
10218 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
10219 goto err_vsis;
10220 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010221 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000010222 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010223 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
10224 i40e_vsi_open(pf->vsi[i]);
10225 break;
10226 }
10227 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010228
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000010229 /* driver is only interested in link up/down and module qualification
10230 * reports from firmware
10231 */
10232 err = i40e_aq_set_phy_int_mask(&pf->hw,
10233 I40E_AQ_EVENT_LINK_UPDOWN |
10234 I40E_AQ_EVENT_MODULE_QUAL_FAIL, NULL);
10235 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010236 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10237 i40e_stat_str(&pf->hw, err),
10238 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000010239
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000010240 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
10241 (pf->hw.aq.fw_maj_ver < 4)) {
10242 msleep(75);
10243 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10244 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010245 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10246 i40e_stat_str(&pf->hw, err),
10247 i40e_aq_str(&pf->hw,
10248 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000010249 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010250 /* The main driver is (mostly) up and happy. We need to set this state
10251 * before setting up the misc vector or we get a race and the vector
10252 * ends up disabled forever.
10253 */
10254 clear_bit(__I40E_DOWN, &pf->state);
10255
10256 /* In case of MSIX we are going to setup the misc vector right here
10257 * to handle admin queue events etc. In case of legacy and MSI
10258 * the misc functionality and queue processing is combined in
10259 * the same vector and that gets setup at open.
10260 */
10261 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
10262 err = i40e_setup_misc_vector(pf);
10263 if (err) {
10264 dev_info(&pdev->dev,
10265 "setup of misc vector failed: %d\n", err);
10266 goto err_vsis;
10267 }
10268 }
10269
Greg Rosedf805f62014-04-04 04:43:16 +000010270#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010271 /* prep for VF support */
10272 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010273 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10274 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010275 u32 val;
10276
10277 /* disable link interrupts for VFs */
10278 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
10279 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
10280 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
10281 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080010282
10283 if (pci_num_vf(pdev)) {
10284 dev_info(&pdev->dev,
10285 "Active VFs found, allocating resources.\n");
10286 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
10287 if (err)
10288 dev_info(&pdev->dev,
10289 "Error %d allocating resources for existing VFs\n",
10290 err);
10291 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010292 }
Greg Rosedf805f62014-04-04 04:43:16 +000010293#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010294
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010295 pfs_found++;
10296
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010297 i40e_dbg_pf_init(pf);
10298
10299 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000010300 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010301
10302 /* since everything's happy, start the service_task timer */
10303 mod_timer(&pf->service_timer,
10304 round_jiffies(jiffies + pf->service_timer_period));
10305
Vasu Dev38e00432014-08-01 13:27:03 -070010306#ifdef I40E_FCOE
10307 /* create FCoE interface */
10308 i40e_fcoe_vsi_setup(pf);
10309
10310#endif
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010311 /* Get the negotiated link width and speed from PCI config space */
10312 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
10313
10314 i40e_set_pci_config_data(hw, link_status);
10315
Jesse Brandeburg69bfb112014-02-11 08:24:13 +000010316 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010317 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
10318 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
10319 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
10320 "Unknown"),
10321 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
10322 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
10323 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
10324 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
10325 "Unknown"));
10326
10327 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
10328 hw->bus.speed < i40e_bus_speed_8000) {
10329 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
10330 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
10331 }
10332
Catherine Sullivane8278452015-02-06 08:52:08 +000010333 /* get the requested speeds from the fw */
10334 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
10335 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010336 dev_info(&pf->pdev->dev,
10337 "get phy capabilities failed, err %s aq_err %s, advertised speed settings may not be correct\n",
10338 i40e_stat_str(&pf->hw, err),
10339 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000010340 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
10341
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040010342 /* get the supported phy types from the fw */
10343 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
10344 if (err)
10345 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
10346 i40e_stat_str(&pf->hw, err),
10347 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10348 pf->hw.phy.phy_types = le32_to_cpu(abilities.phy_type);
10349
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010350 /* print a string summarizing features */
10351 i40e_print_features(pf);
10352
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010353 return 0;
10354
10355 /* Unwind what we've done if something failed in the setup */
10356err_vsis:
10357 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010358 i40e_clear_interrupt_scheme(pf);
10359 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000010360err_switch_setup:
10361 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010362 del_timer_sync(&pf->service_timer);
10363err_mac_addr:
10364err_configure_lan_hmc:
10365 (void)i40e_shutdown_lan_hmc(hw);
10366err_init_lan_hmc:
10367 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010368err_sw_init:
10369err_adminq_setup:
10370 (void)i40e_shutdown_adminq(hw);
10371err_pf_reset:
10372 iounmap(hw->hw_addr);
10373err_ioremap:
10374 kfree(pf);
10375err_pf_alloc:
10376 pci_disable_pcie_error_reporting(pdev);
10377 pci_release_selected_regions(pdev,
10378 pci_select_bars(pdev, IORESOURCE_MEM));
10379err_pci_reg:
10380err_dma:
10381 pci_disable_device(pdev);
10382 return err;
10383}
10384
10385/**
10386 * i40e_remove - Device removal routine
10387 * @pdev: PCI device information struct
10388 *
10389 * i40e_remove is called by the PCI subsystem to alert the driver
10390 * that is should release a PCI device. This could be caused by a
10391 * Hot-Plug event, or because the driver is going to be removed from
10392 * memory.
10393 **/
10394static void i40e_remove(struct pci_dev *pdev)
10395{
10396 struct i40e_pf *pf = pci_get_drvdata(pdev);
10397 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010398 int i;
10399
10400 i40e_dbg_pf_exit(pf);
10401
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010402 i40e_ptp_stop(pf);
10403
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010404 /* no more scheduling of any task */
10405 set_bit(__I40E_DOWN, &pf->state);
10406 del_timer_sync(&pf->service_timer);
10407 cancel_work_sync(&pf->service_task);
Mitch A Williams33c62b32015-02-21 06:44:51 +000010408 i40e_fdir_teardown(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010409
Mitch Williamseb2d80b2014-02-13 03:48:48 -080010410 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
10411 i40e_free_vfs(pf);
10412 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
10413 }
10414
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010415 i40e_fdir_teardown(pf);
10416
10417 /* If there is a switch structure or any orphans, remove them.
10418 * This will leave only the PF's VSI remaining.
10419 */
10420 for (i = 0; i < I40E_MAX_VEB; i++) {
10421 if (!pf->veb[i])
10422 continue;
10423
10424 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
10425 pf->veb[i]->uplink_seid == 0)
10426 i40e_switch_branch_release(pf->veb[i]);
10427 }
10428
10429 /* Now we can shutdown the PF's VSI, just before we kill
10430 * adminq and hmc.
10431 */
10432 if (pf->vsi[pf->lan_vsi])
10433 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
10434
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010435 /* shutdown and destroy the HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +000010436 if (pf->hw.hmc.hmc_obj) {
10437 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
10438 if (ret_code)
10439 dev_warn(&pdev->dev,
10440 "Failed to destroy the HMC resources: %d\n",
10441 ret_code);
10442 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010443
10444 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010445 ret_code = i40e_shutdown_adminq(&pf->hw);
10446 if (ret_code)
10447 dev_warn(&pdev->dev,
10448 "Failed to destroy the Admin Queue resources: %d\n",
10449 ret_code);
10450
10451 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
10452 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000010453 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010454 if (pf->vsi[i]) {
10455 i40e_vsi_clear_rings(pf->vsi[i]);
10456 i40e_vsi_clear(pf->vsi[i]);
10457 pf->vsi[i] = NULL;
10458 }
10459 }
10460
10461 for (i = 0; i < I40E_MAX_VEB; i++) {
10462 kfree(pf->veb[i]);
10463 pf->veb[i] = NULL;
10464 }
10465
10466 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010467 kfree(pf->vsi);
10468
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010469 iounmap(pf->hw.hw_addr);
10470 kfree(pf);
10471 pci_release_selected_regions(pdev,
10472 pci_select_bars(pdev, IORESOURCE_MEM));
10473
10474 pci_disable_pcie_error_reporting(pdev);
10475 pci_disable_device(pdev);
10476}
10477
10478/**
10479 * i40e_pci_error_detected - warning that something funky happened in PCI land
10480 * @pdev: PCI device information struct
10481 *
10482 * Called to warn that something happened and the error handling steps
10483 * are in progress. Allows the driver to quiesce things, be ready for
10484 * remediation.
10485 **/
10486static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
10487 enum pci_channel_state error)
10488{
10489 struct i40e_pf *pf = pci_get_drvdata(pdev);
10490
10491 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
10492
10493 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010494 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
10495 rtnl_lock();
10496 i40e_prep_for_reset(pf);
10497 rtnl_unlock();
10498 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010499
10500 /* Request a slot reset */
10501 return PCI_ERS_RESULT_NEED_RESET;
10502}
10503
10504/**
10505 * i40e_pci_error_slot_reset - a PCI slot reset just happened
10506 * @pdev: PCI device information struct
10507 *
10508 * Called to find if the driver can work with the device now that
10509 * the pci slot has been reset. If a basic connection seems good
10510 * (registers are readable and have sane content) then return a
10511 * happy little PCI_ERS_RESULT_xxx.
10512 **/
10513static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
10514{
10515 struct i40e_pf *pf = pci_get_drvdata(pdev);
10516 pci_ers_result_t result;
10517 int err;
10518 u32 reg;
10519
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040010520 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010521 if (pci_enable_device_mem(pdev)) {
10522 dev_info(&pdev->dev,
10523 "Cannot re-enable PCI device after reset.\n");
10524 result = PCI_ERS_RESULT_DISCONNECT;
10525 } else {
10526 pci_set_master(pdev);
10527 pci_restore_state(pdev);
10528 pci_save_state(pdev);
10529 pci_wake_from_d3(pdev, false);
10530
10531 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
10532 if (reg == 0)
10533 result = PCI_ERS_RESULT_RECOVERED;
10534 else
10535 result = PCI_ERS_RESULT_DISCONNECT;
10536 }
10537
10538 err = pci_cleanup_aer_uncorrect_error_status(pdev);
10539 if (err) {
10540 dev_info(&pdev->dev,
10541 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
10542 err);
10543 /* non-fatal, continue */
10544 }
10545
10546 return result;
10547}
10548
10549/**
10550 * i40e_pci_error_resume - restart operations after PCI error recovery
10551 * @pdev: PCI device information struct
10552 *
10553 * Called to allow the driver to bring things back up after PCI error
10554 * and/or reset recovery has finished.
10555 **/
10556static void i40e_pci_error_resume(struct pci_dev *pdev)
10557{
10558 struct i40e_pf *pf = pci_get_drvdata(pdev);
10559
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040010560 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010561 if (test_bit(__I40E_SUSPENDED, &pf->state))
10562 return;
10563
10564 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010565 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030010566 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010567}
10568
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010569/**
10570 * i40e_shutdown - PCI callback for shutting down
10571 * @pdev: PCI device information struct
10572 **/
10573static void i40e_shutdown(struct pci_dev *pdev)
10574{
10575 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010576 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010577
10578 set_bit(__I40E_SUSPENDED, &pf->state);
10579 set_bit(__I40E_DOWN, &pf->state);
10580 rtnl_lock();
10581 i40e_prep_for_reset(pf);
10582 rtnl_unlock();
10583
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010584 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
10585 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
10586
Catherine Sullivan02b42492015-07-10 19:35:59 -040010587 del_timer_sync(&pf->service_timer);
10588 cancel_work_sync(&pf->service_task);
10589 i40e_fdir_teardown(pf);
10590
10591 rtnl_lock();
10592 i40e_prep_for_reset(pf);
10593 rtnl_unlock();
10594
10595 wr32(hw, I40E_PFPM_APM,
10596 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
10597 wr32(hw, I40E_PFPM_WUFC,
10598 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
10599
Shannon Nelsone1477582015-02-21 06:44:33 +000010600 i40e_clear_interrupt_scheme(pf);
10601
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010602 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010603 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010604 pci_set_power_state(pdev, PCI_D3hot);
10605 }
10606}
10607
10608#ifdef CONFIG_PM
10609/**
10610 * i40e_suspend - PCI callback for moving to D3
10611 * @pdev: PCI device information struct
10612 **/
10613static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
10614{
10615 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010616 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010617
10618 set_bit(__I40E_SUSPENDED, &pf->state);
10619 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070010620
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010621 rtnl_lock();
10622 i40e_prep_for_reset(pf);
10623 rtnl_unlock();
10624
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010625 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
10626 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
10627
10628 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010629 pci_set_power_state(pdev, PCI_D3hot);
10630
10631 return 0;
10632}
10633
10634/**
10635 * i40e_resume - PCI callback for waking up from D3
10636 * @pdev: PCI device information struct
10637 **/
10638static int i40e_resume(struct pci_dev *pdev)
10639{
10640 struct i40e_pf *pf = pci_get_drvdata(pdev);
10641 u32 err;
10642
10643 pci_set_power_state(pdev, PCI_D0);
10644 pci_restore_state(pdev);
10645 /* pci_restore_state() clears dev->state_saves, so
10646 * call pci_save_state() again to restore it.
10647 */
10648 pci_save_state(pdev);
10649
10650 err = pci_enable_device_mem(pdev);
10651 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040010652 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010653 return err;
10654 }
10655 pci_set_master(pdev);
10656
10657 /* no wakeup events while running */
10658 pci_wake_from_d3(pdev, false);
10659
10660 /* handling the reset will rebuild the device state */
10661 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
10662 clear_bit(__I40E_DOWN, &pf->state);
10663 rtnl_lock();
10664 i40e_reset_and_rebuild(pf, false);
10665 rtnl_unlock();
10666 }
10667
10668 return 0;
10669}
10670
10671#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010672static const struct pci_error_handlers i40e_err_handler = {
10673 .error_detected = i40e_pci_error_detected,
10674 .slot_reset = i40e_pci_error_slot_reset,
10675 .resume = i40e_pci_error_resume,
10676};
10677
10678static struct pci_driver i40e_driver = {
10679 .name = i40e_driver_name,
10680 .id_table = i40e_pci_tbl,
10681 .probe = i40e_probe,
10682 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010683#ifdef CONFIG_PM
10684 .suspend = i40e_suspend,
10685 .resume = i40e_resume,
10686#endif
10687 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010688 .err_handler = &i40e_err_handler,
10689 .sriov_configure = i40e_pci_sriov_configure,
10690};
10691
10692/**
10693 * i40e_init_module - Driver registration routine
10694 *
10695 * i40e_init_module is the first routine called when the driver is
10696 * loaded. All it does is register with the PCI subsystem.
10697 **/
10698static int __init i40e_init_module(void)
10699{
10700 pr_info("%s: %s - version %s\n", i40e_driver_name,
10701 i40e_driver_string, i40e_driver_version_str);
10702 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000010703
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010704 i40e_dbg_init();
10705 return pci_register_driver(&i40e_driver);
10706}
10707module_init(i40e_init_module);
10708
10709/**
10710 * i40e_exit_module - Driver exit cleanup routine
10711 *
10712 * i40e_exit_module is called just before the driver is removed
10713 * from memory.
10714 **/
10715static void __exit i40e_exit_module(void)
10716{
10717 pci_unregister_driver(&i40e_driver);
10718 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010719}
10720module_exit(i40e_exit_module);