blob: 0ef6a153b13485c0aef266a2c8d7bc0314a65ef0 [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 Sullivanf91638a2015-08-28 17:56:01 -040042#define DRV_VERSION_BUILD 21
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 } else {
220 /* not enough, so skip over it and continue looking */
221 i += j;
222 }
223 }
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++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +0000506 memset(&vsi->rx_rings[i]->stats, 0 ,
507 sizeof(vsi->rx_rings[i]->stats));
508 memset(&vsi->rx_rings[i]->rx_stats, 0 ,
509 sizeof(vsi->rx_rings[i]->rx_stats));
510 memset(&vsi->tx_rings[i]->stats, 0 ,
511 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];
846 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
847 pf->stat_offsets_loaded,
848 &osd->priority_xoff_rx[i],
849 &nsd->priority_xoff_rx[i]);
850
851 /* No new PFC xoff rx */
852 if (!(nsd->priority_xoff_rx[i] - prio_xoff))
853 continue;
854 /* Get the TC for given priority */
855 tc = dcb_cfg->etscfg.prioritytable[i];
856 xoff[tc] = true;
857 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000858}
859
860/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000861 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000862 * @vsi: the VSI to be updated
863 *
864 * There are a few instances where we store the same stat in a
865 * couple of different structs. This is partly because we have
866 * the netdev stats that need to be filled out, which is slightly
867 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000868 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000869 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000870static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000871{
872 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000873 struct rtnl_link_stats64 *ons;
874 struct rtnl_link_stats64 *ns; /* netdev stats */
875 struct i40e_eth_stats *oes;
876 struct i40e_eth_stats *es; /* device's eth stats */
877 u32 tx_restart, tx_busy;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000878 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000879 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000880 u64 bytes, packets;
881 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000882 u64 rx_p, rx_b;
883 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000884 u16 q;
885
886 if (test_bit(__I40E_DOWN, &vsi->state) ||
887 test_bit(__I40E_CONFIG_BUSY, &pf->state))
888 return;
889
890 ns = i40e_get_vsi_stats_struct(vsi);
891 ons = &vsi->net_stats_offsets;
892 es = &vsi->eth_stats;
893 oes = &vsi->eth_stats_offsets;
894
895 /* Gather up the netdev and vsi stats that the driver collects
896 * on the fly during packet processing
897 */
898 rx_b = rx_p = 0;
899 tx_b = tx_p = 0;
900 tx_restart = tx_busy = 0;
901 rx_page = 0;
902 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000903 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000904 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000905 /* locate Tx ring */
906 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000907
Alexander Duyck980e9b12013-09-28 06:01:03 +0000908 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700909 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000910 packets = p->stats.packets;
911 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700912 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000913 tx_b += bytes;
914 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000915 tx_restart += p->tx_stats.restart_queue;
916 tx_busy += p->tx_stats.tx_busy;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000917
918 /* Rx queue is part of the same block as Tx queue */
919 p = &p[1];
920 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700921 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000922 packets = p->stats.packets;
923 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700924 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000925 rx_b += bytes;
926 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000927 rx_buf += p->rx_stats.alloc_buff_failed;
928 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000929 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000930 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000931 vsi->tx_restart = tx_restart;
932 vsi->tx_busy = tx_busy;
933 vsi->rx_page_failed = rx_page;
934 vsi->rx_buf_failed = rx_buf;
935
936 ns->rx_packets = rx_p;
937 ns->rx_bytes = rx_b;
938 ns->tx_packets = tx_p;
939 ns->tx_bytes = tx_b;
940
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000941 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000942 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000943 ons->tx_errors = oes->tx_errors;
944 ns->tx_errors = es->tx_errors;
945 ons->multicast = oes->rx_multicast;
946 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000947 ons->rx_dropped = oes->rx_discards;
948 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000949 ons->tx_dropped = oes->tx_discards;
950 ns->tx_dropped = es->tx_discards;
951
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000952 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000953 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000954 ns->rx_crc_errors = pf->stats.crc_errors;
955 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
956 ns->rx_length_errors = pf->stats.rx_length_errors;
957 }
958}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000959
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000960/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000961 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000962 * @pf: the PF to be updated
963 **/
964static void i40e_update_pf_stats(struct i40e_pf *pf)
965{
966 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
967 struct i40e_hw_port_stats *nsd = &pf->stats;
968 struct i40e_hw *hw = &pf->hw;
969 u32 val;
970 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000971
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000972 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
973 I40E_GLPRT_GORCL(hw->port),
974 pf->stat_offsets_loaded,
975 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
976 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
977 I40E_GLPRT_GOTCL(hw->port),
978 pf->stat_offsets_loaded,
979 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
980 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
981 pf->stat_offsets_loaded,
982 &osd->eth.rx_discards,
983 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000984 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
985 I40E_GLPRT_UPRCL(hw->port),
986 pf->stat_offsets_loaded,
987 &osd->eth.rx_unicast,
988 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000989 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
990 I40E_GLPRT_MPRCL(hw->port),
991 pf->stat_offsets_loaded,
992 &osd->eth.rx_multicast,
993 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000994 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
995 I40E_GLPRT_BPRCL(hw->port),
996 pf->stat_offsets_loaded,
997 &osd->eth.rx_broadcast,
998 &nsd->eth.rx_broadcast);
999 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1000 I40E_GLPRT_UPTCL(hw->port),
1001 pf->stat_offsets_loaded,
1002 &osd->eth.tx_unicast,
1003 &nsd->eth.tx_unicast);
1004 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1005 I40E_GLPRT_MPTCL(hw->port),
1006 pf->stat_offsets_loaded,
1007 &osd->eth.tx_multicast,
1008 &nsd->eth.tx_multicast);
1009 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1010 I40E_GLPRT_BPTCL(hw->port),
1011 pf->stat_offsets_loaded,
1012 &osd->eth.tx_broadcast,
1013 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001014
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001015 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1016 pf->stat_offsets_loaded,
1017 &osd->tx_dropped_link_down,
1018 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001019
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001020 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1021 pf->stat_offsets_loaded,
1022 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001023
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001024 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1025 pf->stat_offsets_loaded,
1026 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001027
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001028 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1029 pf->stat_offsets_loaded,
1030 &osd->mac_local_faults,
1031 &nsd->mac_local_faults);
1032 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1033 pf->stat_offsets_loaded,
1034 &osd->mac_remote_faults,
1035 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001036
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001037 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1038 pf->stat_offsets_loaded,
1039 &osd->rx_length_errors,
1040 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001041
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001042 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1043 pf->stat_offsets_loaded,
1044 &osd->link_xon_rx, &nsd->link_xon_rx);
1045 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1046 pf->stat_offsets_loaded,
1047 &osd->link_xon_tx, &nsd->link_xon_tx);
1048 i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
1049 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1050 pf->stat_offsets_loaded,
1051 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001052
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001053 for (i = 0; i < 8; i++) {
1054 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001055 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001056 &osd->priority_xon_rx[i],
1057 &nsd->priority_xon_rx[i]);
1058 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(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_tx[i],
1061 &nsd->priority_xon_tx[i]);
1062 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(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_xoff_tx[i],
1065 &nsd->priority_xoff_tx[i]);
1066 i40e_stat_update32(hw,
1067 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001068 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001069 &osd->priority_xon_2_xoff[i],
1070 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001071 }
1072
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001073 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1074 I40E_GLPRT_PRC64L(hw->port),
1075 pf->stat_offsets_loaded,
1076 &osd->rx_size_64, &nsd->rx_size_64);
1077 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1078 I40E_GLPRT_PRC127L(hw->port),
1079 pf->stat_offsets_loaded,
1080 &osd->rx_size_127, &nsd->rx_size_127);
1081 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1082 I40E_GLPRT_PRC255L(hw->port),
1083 pf->stat_offsets_loaded,
1084 &osd->rx_size_255, &nsd->rx_size_255);
1085 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1086 I40E_GLPRT_PRC511L(hw->port),
1087 pf->stat_offsets_loaded,
1088 &osd->rx_size_511, &nsd->rx_size_511);
1089 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1090 I40E_GLPRT_PRC1023L(hw->port),
1091 pf->stat_offsets_loaded,
1092 &osd->rx_size_1023, &nsd->rx_size_1023);
1093 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1094 I40E_GLPRT_PRC1522L(hw->port),
1095 pf->stat_offsets_loaded,
1096 &osd->rx_size_1522, &nsd->rx_size_1522);
1097 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1098 I40E_GLPRT_PRC9522L(hw->port),
1099 pf->stat_offsets_loaded,
1100 &osd->rx_size_big, &nsd->rx_size_big);
1101
1102 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1103 I40E_GLPRT_PTC64L(hw->port),
1104 pf->stat_offsets_loaded,
1105 &osd->tx_size_64, &nsd->tx_size_64);
1106 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1107 I40E_GLPRT_PTC127L(hw->port),
1108 pf->stat_offsets_loaded,
1109 &osd->tx_size_127, &nsd->tx_size_127);
1110 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1111 I40E_GLPRT_PTC255L(hw->port),
1112 pf->stat_offsets_loaded,
1113 &osd->tx_size_255, &nsd->tx_size_255);
1114 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1115 I40E_GLPRT_PTC511L(hw->port),
1116 pf->stat_offsets_loaded,
1117 &osd->tx_size_511, &nsd->tx_size_511);
1118 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1119 I40E_GLPRT_PTC1023L(hw->port),
1120 pf->stat_offsets_loaded,
1121 &osd->tx_size_1023, &nsd->tx_size_1023);
1122 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1123 I40E_GLPRT_PTC1522L(hw->port),
1124 pf->stat_offsets_loaded,
1125 &osd->tx_size_1522, &nsd->tx_size_1522);
1126 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1127 I40E_GLPRT_PTC9522L(hw->port),
1128 pf->stat_offsets_loaded,
1129 &osd->tx_size_big, &nsd->tx_size_big);
1130
1131 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1132 pf->stat_offsets_loaded,
1133 &osd->rx_undersize, &nsd->rx_undersize);
1134 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1135 pf->stat_offsets_loaded,
1136 &osd->rx_fragments, &nsd->rx_fragments);
1137 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1138 pf->stat_offsets_loaded,
1139 &osd->rx_oversize, &nsd->rx_oversize);
1140 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1141 pf->stat_offsets_loaded,
1142 &osd->rx_jabber, &nsd->rx_jabber);
1143
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001144 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001145 i40e_stat_update32(hw,
1146 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001147 pf->stat_offsets_loaded,
1148 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001149 i40e_stat_update32(hw,
1150 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001151 pf->stat_offsets_loaded,
1152 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001153 i40e_stat_update32(hw,
1154 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1155 pf->stat_offsets_loaded,
1156 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001157
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001158 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1159 nsd->tx_lpi_status =
1160 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1161 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1162 nsd->rx_lpi_status =
1163 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1164 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1165 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1166 pf->stat_offsets_loaded,
1167 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1168 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1169 pf->stat_offsets_loaded,
1170 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1171
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001172 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1173 !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
1174 nsd->fd_sb_status = true;
1175 else
1176 nsd->fd_sb_status = false;
1177
1178 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1179 !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1180 nsd->fd_atr_status = true;
1181 else
1182 nsd->fd_atr_status = false;
1183
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001184 pf->stat_offsets_loaded = true;
1185}
1186
1187/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001188 * i40e_update_stats - Update the various statistics counters.
1189 * @vsi: the VSI to be updated
1190 *
1191 * Update the various stats for this VSI and its related entities.
1192 **/
1193void i40e_update_stats(struct i40e_vsi *vsi)
1194{
1195 struct i40e_pf *pf = vsi->back;
1196
1197 if (vsi == pf->vsi[pf->lan_vsi])
1198 i40e_update_pf_stats(pf);
1199
1200 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001201#ifdef I40E_FCOE
1202 i40e_update_fcoe_stats(vsi);
1203#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001204}
1205
1206/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001207 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1208 * @vsi: the VSI to be searched
1209 * @macaddr: the MAC address
1210 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001211 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001212 * @is_netdev: make sure its a netdev filter, else doesn't matter
1213 *
1214 * Returns ptr to the filter object or NULL
1215 **/
1216static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1217 u8 *macaddr, s16 vlan,
1218 bool is_vf, bool is_netdev)
1219{
1220 struct i40e_mac_filter *f;
1221
1222 if (!vsi || !macaddr)
1223 return NULL;
1224
1225 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1226 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1227 (vlan == f->vlan) &&
1228 (!is_vf || f->is_vf) &&
1229 (!is_netdev || f->is_netdev))
1230 return f;
1231 }
1232 return NULL;
1233}
1234
1235/**
1236 * i40e_find_mac - Find a mac addr in the macvlan filters list
1237 * @vsi: the VSI to be searched
1238 * @macaddr: the MAC address we are searching for
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001239 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001240 * @is_netdev: make sure its a netdev filter, else doesn't matter
1241 *
1242 * Returns the first filter with the provided MAC address or NULL if
1243 * MAC address was not found
1244 **/
1245struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1246 bool is_vf, bool is_netdev)
1247{
1248 struct i40e_mac_filter *f;
1249
1250 if (!vsi || !macaddr)
1251 return NULL;
1252
1253 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1254 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1255 (!is_vf || f->is_vf) &&
1256 (!is_netdev || f->is_netdev))
1257 return f;
1258 }
1259 return NULL;
1260}
1261
1262/**
1263 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1264 * @vsi: the VSI to be searched
1265 *
1266 * Returns true if VSI is in vlan mode or false otherwise
1267 **/
1268bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1269{
1270 struct i40e_mac_filter *f;
1271
1272 /* Only -1 for all the filters denotes not in vlan mode
1273 * so we have to go through all the list in order to make sure
1274 */
1275 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Greg Rosed9b68f82015-07-23 16:54:31 -04001276 if (f->vlan >= 0 || vsi->info.pvid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001277 return true;
1278 }
1279
1280 return false;
1281}
1282
1283/**
1284 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1285 * @vsi: the VSI to be searched
1286 * @macaddr: the mac address to be filtered
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001287 * @is_vf: true if it is a VF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001288 * @is_netdev: true if it is a netdev
1289 *
1290 * Goes through all the macvlan filters and adds a
1291 * macvlan filter for each unique vlan that already exists
1292 *
1293 * Returns first filter found on success, else NULL
1294 **/
1295struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1296 bool is_vf, bool is_netdev)
1297{
1298 struct i40e_mac_filter *f;
1299
1300 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Mitch Williamsecbb44e2015-07-10 19:35:56 -04001301 if (vsi->info.pvid)
1302 f->vlan = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001303 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1304 is_vf, is_netdev)) {
1305 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001306 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001307 return NULL;
1308 }
1309 }
1310
1311 return list_first_entry_or_null(&vsi->mac_filter_list,
1312 struct i40e_mac_filter, list);
1313}
1314
1315/**
Greg Rose8c27d422014-05-22 06:31:56 +00001316 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1317 * @vsi: the PF Main VSI - inappropriate for any other VSI
1318 * @macaddr: the MAC address
Shannon Nelson30650cc2014-07-29 04:01:50 +00001319 *
1320 * Some older firmware configurations set up a default promiscuous VLAN
1321 * filter that needs to be removed.
Greg Rose8c27d422014-05-22 06:31:56 +00001322 **/
Shannon Nelson30650cc2014-07-29 04:01:50 +00001323static int i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
Greg Rose8c27d422014-05-22 06:31:56 +00001324{
1325 struct i40e_aqc_remove_macvlan_element_data element;
1326 struct i40e_pf *pf = vsi->back;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001327 i40e_status ret;
Greg Rose8c27d422014-05-22 06:31:56 +00001328
1329 /* Only appropriate for the PF main VSI */
1330 if (vsi->type != I40E_VSI_MAIN)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001331 return -EINVAL;
Greg Rose8c27d422014-05-22 06:31:56 +00001332
Shannon Nelson30650cc2014-07-29 04:01:50 +00001333 memset(&element, 0, sizeof(element));
Greg Rose8c27d422014-05-22 06:31:56 +00001334 ether_addr_copy(element.mac_addr, macaddr);
1335 element.vlan_tag = 0;
1336 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1337 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001338 ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1339 if (ret)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001340 return -ENOENT;
1341
1342 return 0;
Greg Rose8c27d422014-05-22 06:31:56 +00001343}
1344
1345/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001346 * i40e_add_filter - Add a mac/vlan filter to the VSI
1347 * @vsi: the VSI to be searched
1348 * @macaddr: the MAC address
1349 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001350 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001351 * @is_netdev: make sure its a netdev filter, else doesn't matter
1352 *
1353 * Returns ptr to the filter object or NULL when no memory available.
1354 **/
1355struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1356 u8 *macaddr, s16 vlan,
1357 bool is_vf, bool is_netdev)
1358{
1359 struct i40e_mac_filter *f;
1360
1361 if (!vsi || !macaddr)
1362 return NULL;
1363
1364 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1365 if (!f) {
1366 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1367 if (!f)
1368 goto add_filter_out;
1369
Greg Rose9a173902014-05-22 06:32:02 +00001370 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001371 f->vlan = vlan;
1372 f->changed = true;
1373
1374 INIT_LIST_HEAD(&f->list);
1375 list_add(&f->list, &vsi->mac_filter_list);
1376 }
1377
1378 /* increment counter and add a new flag if needed */
1379 if (is_vf) {
1380 if (!f->is_vf) {
1381 f->is_vf = true;
1382 f->counter++;
1383 }
1384 } else if (is_netdev) {
1385 if (!f->is_netdev) {
1386 f->is_netdev = true;
1387 f->counter++;
1388 }
1389 } else {
1390 f->counter++;
1391 }
1392
1393 /* changed tells sync_filters_subtask to
1394 * push the filter down to the firmware
1395 */
1396 if (f->changed) {
1397 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1398 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1399 }
1400
1401add_filter_out:
1402 return f;
1403}
1404
1405/**
1406 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1407 * @vsi: the VSI to be searched
1408 * @macaddr: the MAC address
1409 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001410 * @is_vf: make sure it's a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001411 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1412 **/
1413void i40e_del_filter(struct i40e_vsi *vsi,
1414 u8 *macaddr, s16 vlan,
1415 bool is_vf, bool is_netdev)
1416{
1417 struct i40e_mac_filter *f;
1418
1419 if (!vsi || !macaddr)
1420 return;
1421
1422 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1423 if (!f || f->counter == 0)
1424 return;
1425
1426 if (is_vf) {
1427 if (f->is_vf) {
1428 f->is_vf = false;
1429 f->counter--;
1430 }
1431 } else if (is_netdev) {
1432 if (f->is_netdev) {
1433 f->is_netdev = false;
1434 f->counter--;
1435 }
1436 } else {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001437 /* make sure we don't remove a filter in use by VF or netdev */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001438 int min_f = 0;
1439 min_f += (f->is_vf ? 1 : 0);
1440 min_f += (f->is_netdev ? 1 : 0);
1441
1442 if (f->counter > min_f)
1443 f->counter--;
1444 }
1445
1446 /* counter == 0 tells sync_filters_subtask to
1447 * remove the filter from the firmware's list
1448 */
1449 if (f->counter == 0) {
1450 f->changed = true;
1451 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1452 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1453 }
1454}
1455
1456/**
1457 * i40e_set_mac - NDO callback to set mac address
1458 * @netdev: network interface device structure
1459 * @p: pointer to an address structure
1460 *
1461 * Returns 0 on success, negative on failure
1462 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001463#ifdef I40E_FCOE
1464int i40e_set_mac(struct net_device *netdev, void *p)
1465#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001466static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001467#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001468{
1469 struct i40e_netdev_priv *np = netdev_priv(netdev);
1470 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001471 struct i40e_pf *pf = vsi->back;
1472 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001473 struct sockaddr *addr = p;
1474 struct i40e_mac_filter *f;
1475
1476 if (!is_valid_ether_addr(addr->sa_data))
1477 return -EADDRNOTAVAIL;
1478
Shannon Nelson30650cc2014-07-29 04:01:50 +00001479 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1480 netdev_info(netdev, "already using mac address %pM\n",
1481 addr->sa_data);
1482 return 0;
1483 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001484
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001485 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1486 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1487 return -EADDRNOTAVAIL;
1488
Shannon Nelson30650cc2014-07-29 04:01:50 +00001489 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1490 netdev_info(netdev, "returning to hw mac address %pM\n",
1491 hw->mac.addr);
1492 else
1493 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1494
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001495 if (vsi->type == I40E_VSI_MAIN) {
1496 i40e_status ret;
1497 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001498 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001499 addr->sa_data, NULL);
1500 if (ret) {
1501 netdev_info(netdev,
1502 "Addr change for Main VSI failed: %d\n",
1503 ret);
1504 return -EADDRNOTAVAIL;
1505 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001506 }
1507
Shannon Nelson30650cc2014-07-29 04:01:50 +00001508 if (ether_addr_equal(netdev->dev_addr, hw->mac.addr)) {
1509 struct i40e_aqc_remove_macvlan_element_data element;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001510
Shannon Nelson30650cc2014-07-29 04:01:50 +00001511 memset(&element, 0, sizeof(element));
1512 ether_addr_copy(element.mac_addr, netdev->dev_addr);
1513 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1514 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1515 } else {
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001516 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1517 false, false);
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001518 }
1519
Shannon Nelson30650cc2014-07-29 04:01:50 +00001520 if (ether_addr_equal(addr->sa_data, hw->mac.addr)) {
1521 struct i40e_aqc_add_macvlan_element_data element;
1522
1523 memset(&element, 0, sizeof(element));
1524 ether_addr_copy(element.mac_addr, hw->mac.addr);
1525 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
1526 i40e_aq_add_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1527 } else {
1528 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY,
1529 false, false);
1530 if (f)
1531 f->is_laa = true;
1532 }
1533
Anjali Singhai30e25612015-09-28 13:37:12 -07001534 i40e_sync_vsi_filters(vsi, false);
Shannon Nelson30650cc2014-07-29 04:01:50 +00001535 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001536
1537 return 0;
1538}
1539
1540/**
1541 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1542 * @vsi: the VSI being setup
1543 * @ctxt: VSI context structure
1544 * @enabled_tc: Enabled TCs bitmap
1545 * @is_add: True if called before Add VSI
1546 *
1547 * Setup VSI queue mapping for enabled traffic classes.
1548 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001549#ifdef I40E_FCOE
1550void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1551 struct i40e_vsi_context *ctxt,
1552 u8 enabled_tc,
1553 bool is_add)
1554#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001555static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1556 struct i40e_vsi_context *ctxt,
1557 u8 enabled_tc,
1558 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001559#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001560{
1561 struct i40e_pf *pf = vsi->back;
1562 u16 sections = 0;
1563 u8 netdev_tc = 0;
1564 u16 numtc = 0;
1565 u16 qcount;
1566 u8 offset;
1567 u16 qmap;
1568 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001569 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001570
1571 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1572 offset = 0;
1573
1574 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1575 /* Find numtc from enabled TC bitmap */
1576 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001577 if (enabled_tc & BIT_ULL(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001578 numtc++;
1579 }
1580 if (!numtc) {
1581 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1582 numtc = 1;
1583 }
1584 } else {
1585 /* At least TC0 is enabled in case of non-DCB case */
1586 numtc = 1;
1587 }
1588
1589 vsi->tc_config.numtc = numtc;
1590 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001591 /* Number of queues per enabled TC */
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001592 /* In MFP case we can have a much lower count of MSIx
1593 * vectors available and so we need to lower the used
1594 * q count.
1595 */
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001596 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1597 qcount = min_t(int, vsi->alloc_queue_pairs, pf->num_lan_msix);
1598 else
1599 qcount = vsi->alloc_queue_pairs;
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001600 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001601 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001602
1603 /* Setup queue offset/count for all TCs for given VSI */
1604 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1605 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001606 if (vsi->tc_config.enabled_tc & BIT_ULL(i)) {
1607 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001608 int pow, num_qps;
1609
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001610 switch (vsi->type) {
1611 case I40E_VSI_MAIN:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001612 qcount = min_t(int, pf->rss_size, num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001613 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001614#ifdef I40E_FCOE
1615 case I40E_VSI_FCOE:
1616 qcount = num_tc_qps;
1617 break;
1618#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001619 case I40E_VSI_FDIR:
1620 case I40E_VSI_SRIOV:
1621 case I40E_VSI_VMDQ2:
1622 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001623 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001624 WARN_ON(i != 0);
1625 break;
1626 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001627 vsi->tc_config.tc_info[i].qoffset = offset;
1628 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001629
Shannon Nelson1e200e42015-02-27 09:15:24 +00001630 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001631 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001632 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001633 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001634 pow++;
1635 num_qps >>= 1;
1636 }
1637
1638 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1639 qmap =
1640 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1641 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1642
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001643 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001644 } else {
1645 /* TC is not enabled so set the offset to
1646 * default queue and allocate one queue
1647 * for the given TC.
1648 */
1649 vsi->tc_config.tc_info[i].qoffset = 0;
1650 vsi->tc_config.tc_info[i].qcount = 1;
1651 vsi->tc_config.tc_info[i].netdev_tc = 0;
1652
1653 qmap = 0;
1654 }
1655 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1656 }
1657
1658 /* Set actual Tx/Rx queue pairs */
1659 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001660 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1661 if (vsi->req_queue_pairs > 0)
1662 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001663 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001664 vsi->num_queue_pairs = pf->num_lan_msix;
1665 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001666
1667 /* Scheduler section valid can only be set for ADD VSI */
1668 if (is_add) {
1669 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1670
1671 ctxt->info.up_enable_bits = enabled_tc;
1672 }
1673 if (vsi->type == I40E_VSI_SRIOV) {
1674 ctxt->info.mapping_flags |=
1675 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1676 for (i = 0; i < vsi->num_queue_pairs; i++)
1677 ctxt->info.queue_mapping[i] =
1678 cpu_to_le16(vsi->base_queue + i);
1679 } else {
1680 ctxt->info.mapping_flags |=
1681 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1682 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1683 }
1684 ctxt->info.valid_sections |= cpu_to_le16(sections);
1685}
1686
1687/**
1688 * i40e_set_rx_mode - NDO callback to set the netdev filters
1689 * @netdev: network interface device structure
1690 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001691#ifdef I40E_FCOE
1692void i40e_set_rx_mode(struct net_device *netdev)
1693#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001694static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001695#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001696{
1697 struct i40e_netdev_priv *np = netdev_priv(netdev);
1698 struct i40e_mac_filter *f, *ftmp;
1699 struct i40e_vsi *vsi = np->vsi;
1700 struct netdev_hw_addr *uca;
1701 struct netdev_hw_addr *mca;
1702 struct netdev_hw_addr *ha;
1703
1704 /* add addr if not already in the filter list */
1705 netdev_for_each_uc_addr(uca, netdev) {
1706 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1707 if (i40e_is_vsi_in_vlan(vsi))
1708 i40e_put_mac_in_vlan(vsi, uca->addr,
1709 false, true);
1710 else
1711 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1712 false, true);
1713 }
1714 }
1715
1716 netdev_for_each_mc_addr(mca, netdev) {
1717 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1718 if (i40e_is_vsi_in_vlan(vsi))
1719 i40e_put_mac_in_vlan(vsi, mca->addr,
1720 false, true);
1721 else
1722 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1723 false, true);
1724 }
1725 }
1726
1727 /* remove filter if not in netdev list */
1728 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1729 bool found = false;
1730
1731 if (!f->is_netdev)
1732 continue;
1733
1734 if (is_multicast_ether_addr(f->macaddr)) {
1735 netdev_for_each_mc_addr(mca, netdev) {
1736 if (ether_addr_equal(mca->addr, f->macaddr)) {
1737 found = true;
1738 break;
1739 }
1740 }
1741 } else {
1742 netdev_for_each_uc_addr(uca, netdev) {
1743 if (ether_addr_equal(uca->addr, f->macaddr)) {
1744 found = true;
1745 break;
1746 }
1747 }
1748
1749 for_each_dev_addr(netdev, ha) {
1750 if (ether_addr_equal(ha->addr, f->macaddr)) {
1751 found = true;
1752 break;
1753 }
1754 }
1755 }
1756 if (!found)
1757 i40e_del_filter(
1758 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1759 }
1760
1761 /* check for other flag changes */
1762 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1763 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1764 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1765 }
1766}
1767
1768/**
1769 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1770 * @vsi: ptr to the VSI
Anjali Singhai30e25612015-09-28 13:37:12 -07001771 * @grab_rtnl: whether RTNL needs to be grabbed
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001772 *
1773 * Push any outstanding VSI filter changes through the AdminQ.
1774 *
1775 * Returns 0 or error value
1776 **/
Anjali Singhai30e25612015-09-28 13:37:12 -07001777int i40e_sync_vsi_filters(struct i40e_vsi *vsi, bool grab_rtnl)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001778{
1779 struct i40e_mac_filter *f, *ftmp;
1780 bool promisc_forced_on = false;
1781 bool add_happened = false;
1782 int filter_list_len = 0;
1783 u32 changed_flags = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001784 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001785 struct i40e_pf *pf;
1786 int num_add = 0;
1787 int num_del = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001788 int aq_err = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001789 u16 cmd_flags;
1790
1791 /* empty array typed pointers, kcalloc later */
1792 struct i40e_aqc_add_macvlan_element_data *add_list;
1793 struct i40e_aqc_remove_macvlan_element_data *del_list;
1794
1795 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1796 usleep_range(1000, 2000);
1797 pf = vsi->back;
1798
1799 if (vsi->netdev) {
1800 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1801 vsi->current_netdev_flags = vsi->netdev->flags;
1802 }
1803
1804 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1805 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1806
1807 filter_list_len = pf->hw.aq.asq_buf_size /
1808 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1809 del_list = kcalloc(filter_list_len,
1810 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1811 GFP_KERNEL);
1812 if (!del_list)
1813 return -ENOMEM;
1814
1815 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1816 if (!f->changed)
1817 continue;
1818
1819 if (f->counter != 0)
1820 continue;
1821 f->changed = false;
1822 cmd_flags = 0;
1823
1824 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001825 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001826 del_list[num_del].vlan_tag =
1827 cpu_to_le16((u16)(f->vlan ==
1828 I40E_VLAN_ANY ? 0 : f->vlan));
1829
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001830 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1831 del_list[num_del].flags = cmd_flags;
1832 num_del++;
1833
1834 /* unlink from filter list */
1835 list_del(&f->list);
1836 kfree(f);
1837
1838 /* flush a full buffer */
1839 if (num_del == filter_list_len) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001840 ret = i40e_aq_remove_macvlan(&pf->hw,
1841 vsi->seid, del_list, num_del,
1842 NULL);
1843 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001844 num_del = 0;
1845 memset(del_list, 0, sizeof(*del_list));
1846
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001847 if (ret && aq_err != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001848 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001849 "ignoring delete macvlan error, err %s, aq_err %s while flushing a full buffer\n",
1850 i40e_stat_str(&pf->hw, ret),
1851 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001852 }
1853 }
1854 if (num_del) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001855 ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001856 del_list, num_del, NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001857 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001858 num_del = 0;
1859
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001860 if (ret && aq_err != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001861 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001862 "ignoring delete macvlan error, err %s aq_err %s\n",
1863 i40e_stat_str(&pf->hw, ret),
1864 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001865 }
1866
1867 kfree(del_list);
1868 del_list = NULL;
1869
1870 /* do all the adds now */
1871 filter_list_len = pf->hw.aq.asq_buf_size /
1872 sizeof(struct i40e_aqc_add_macvlan_element_data),
1873 add_list = kcalloc(filter_list_len,
1874 sizeof(struct i40e_aqc_add_macvlan_element_data),
1875 GFP_KERNEL);
1876 if (!add_list)
1877 return -ENOMEM;
1878
1879 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1880 if (!f->changed)
1881 continue;
1882
1883 if (f->counter == 0)
1884 continue;
1885 f->changed = false;
1886 add_happened = true;
1887 cmd_flags = 0;
1888
1889 /* add to add array */
Greg Rose9a173902014-05-22 06:32:02 +00001890 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001891 add_list[num_add].vlan_tag =
1892 cpu_to_le16(
1893 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1894 add_list[num_add].queue_number = 0;
1895
1896 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001897 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1898 num_add++;
1899
1900 /* flush a full buffer */
1901 if (num_add == filter_list_len) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001902 ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1903 add_list, num_add,
1904 NULL);
1905 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001906 num_add = 0;
1907
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001908 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001909 break;
1910 memset(add_list, 0, sizeof(*add_list));
1911 }
1912 }
1913 if (num_add) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001914 ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1915 add_list, num_add, NULL);
1916 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001917 num_add = 0;
1918 }
1919 kfree(add_list);
1920 add_list = NULL;
1921
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001922 if (add_happened && ret && aq_err != I40E_AQ_RC_EINVAL) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001923 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001924 "add filter failed, err %s aq_err %s\n",
1925 i40e_stat_str(&pf->hw, ret),
1926 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001927 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1928 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1929 &vsi->state)) {
1930 promisc_forced_on = true;
1931 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1932 &vsi->state);
1933 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1934 }
1935 }
1936 }
1937
1938 /* check for changes in promiscuous modes */
1939 if (changed_flags & IFF_ALLMULTI) {
1940 bool cur_multipromisc;
1941 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001942 ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1943 vsi->seid,
1944 cur_multipromisc,
1945 NULL);
1946 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001947 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001948 "set multi promisc failed, err %s aq_err %s\n",
1949 i40e_stat_str(&pf->hw, ret),
1950 i40e_aq_str(&pf->hw,
1951 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001952 }
1953 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1954 bool cur_promisc;
1955 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1956 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1957 &vsi->state));
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04001958 if (vsi->type == I40E_VSI_MAIN && pf->lan_veb != I40E_NO_VEB) {
1959 /* set defport ON for Main VSI instead of true promisc
1960 * this way we will get all unicast/multicast and VLAN
1961 * promisc behavior but will not get VF or VMDq traffic
1962 * replicated on the Main VSI.
1963 */
1964 if (pf->cur_promisc != cur_promisc) {
1965 pf->cur_promisc = cur_promisc;
Anjali Singhai30e25612015-09-28 13:37:12 -07001966 if (grab_rtnl)
1967 i40e_do_reset_safe(pf,
1968 BIT(__I40E_PF_RESET_REQUESTED));
1969 else
1970 i40e_do_reset(pf,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04001971 BIT(__I40E_PF_RESET_REQUESTED));
1972 }
1973 } else {
1974 ret = i40e_aq_set_vsi_unicast_promiscuous(
1975 &vsi->back->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001976 vsi->seid,
1977 cur_promisc, NULL);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04001978 if (ret)
1979 dev_info(&pf->pdev->dev,
1980 "set unicast promisc failed, err %d, aq_err %d\n",
1981 ret, pf->hw.aq.asq_last_status);
1982 ret = i40e_aq_set_vsi_multicast_promiscuous(
1983 &vsi->back->hw,
1984 vsi->seid,
1985 cur_promisc, NULL);
1986 if (ret)
1987 dev_info(&pf->pdev->dev,
1988 "set multicast promisc failed, err %d, aq_err %d\n",
1989 ret, pf->hw.aq.asq_last_status);
1990 }
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001991 ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1992 vsi->seid,
1993 cur_promisc, NULL);
1994 if (ret)
Greg Rose1a103702013-11-28 06:42:39 +00001995 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001996 "set brdcast promisc failed, err %s, aq_err %s\n",
1997 i40e_stat_str(&pf->hw, ret),
1998 i40e_aq_str(&pf->hw,
1999 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002000 }
2001
2002 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2003 return 0;
2004}
2005
2006/**
2007 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2008 * @pf: board private structure
2009 **/
2010static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2011{
2012 int v;
2013
2014 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2015 return;
2016 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2017
Mitch Williams505682c2014-05-20 08:01:37 +00002018 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002019 if (pf->vsi[v] &&
2020 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
Anjali Singhai30e25612015-09-28 13:37:12 -07002021 i40e_sync_vsi_filters(pf->vsi[v], true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002022 }
2023}
2024
2025/**
2026 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2027 * @netdev: network interface device structure
2028 * @new_mtu: new value for maximum frame size
2029 *
2030 * Returns 0 on success, negative on failure
2031 **/
2032static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2033{
2034 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00002035 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002036 struct i40e_vsi *vsi = np->vsi;
2037
2038 /* MTU < 68 is an error and causes problems on some kernels */
2039 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2040 return -EINVAL;
2041
2042 netdev_info(netdev, "changing MTU from %d to %d\n",
2043 netdev->mtu, new_mtu);
2044 netdev->mtu = new_mtu;
2045 if (netif_running(netdev))
2046 i40e_vsi_reinit_locked(vsi);
2047
2048 return 0;
2049}
2050
2051/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002052 * i40e_ioctl - Access the hwtstamp interface
2053 * @netdev: network interface device structure
2054 * @ifr: interface request data
2055 * @cmd: ioctl command
2056 **/
2057int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2058{
2059 struct i40e_netdev_priv *np = netdev_priv(netdev);
2060 struct i40e_pf *pf = np->vsi->back;
2061
2062 switch (cmd) {
2063 case SIOCGHWTSTAMP:
2064 return i40e_ptp_get_ts_config(pf, ifr);
2065 case SIOCSHWTSTAMP:
2066 return i40e_ptp_set_ts_config(pf, ifr);
2067 default:
2068 return -EOPNOTSUPP;
2069 }
2070}
2071
2072/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002073 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2074 * @vsi: the vsi being adjusted
2075 **/
2076void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2077{
2078 struct i40e_vsi_context ctxt;
2079 i40e_status ret;
2080
2081 if ((vsi->info.valid_sections &
2082 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2083 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2084 return; /* already enabled */
2085
2086 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2087 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2088 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2089
2090 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002091 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002092 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2093 if (ret) {
2094 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002095 "update vlan stripping failed, err %s aq_err %s\n",
2096 i40e_stat_str(&vsi->back->hw, ret),
2097 i40e_aq_str(&vsi->back->hw,
2098 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002099 }
2100}
2101
2102/**
2103 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2104 * @vsi: the vsi being adjusted
2105 **/
2106void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2107{
2108 struct i40e_vsi_context ctxt;
2109 i40e_status ret;
2110
2111 if ((vsi->info.valid_sections &
2112 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2113 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2114 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2115 return; /* already disabled */
2116
2117 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2118 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2119 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2120
2121 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002122 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002123 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2124 if (ret) {
2125 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002126 "update vlan stripping failed, err %s aq_err %s\n",
2127 i40e_stat_str(&vsi->back->hw, ret),
2128 i40e_aq_str(&vsi->back->hw,
2129 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002130 }
2131}
2132
2133/**
2134 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2135 * @netdev: network interface to be adjusted
2136 * @features: netdev features to test if VLAN offload is enabled or not
2137 **/
2138static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2139{
2140 struct i40e_netdev_priv *np = netdev_priv(netdev);
2141 struct i40e_vsi *vsi = np->vsi;
2142
2143 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2144 i40e_vlan_stripping_enable(vsi);
2145 else
2146 i40e_vlan_stripping_disable(vsi);
2147}
2148
2149/**
2150 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2151 * @vsi: the vsi being configured
2152 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2153 **/
2154int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2155{
2156 struct i40e_mac_filter *f, *add_f;
2157 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002158
2159 is_vf = (vsi->type == I40E_VSI_SRIOV);
2160 is_netdev = !!(vsi->netdev);
2161
2162 if (is_netdev) {
2163 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
2164 is_vf, is_netdev);
2165 if (!add_f) {
2166 dev_info(&vsi->back->pdev->dev,
2167 "Could not add vlan filter %d for %pM\n",
2168 vid, vsi->netdev->dev_addr);
2169 return -ENOMEM;
2170 }
2171 }
2172
2173 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2174 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2175 if (!add_f) {
2176 dev_info(&vsi->back->pdev->dev,
2177 "Could not add vlan filter %d for %pM\n",
2178 vid, f->macaddr);
2179 return -ENOMEM;
2180 }
2181 }
2182
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002183 /* Now if we add a vlan tag, make sure to check if it is the first
2184 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2185 * with 0, so we now accept untagged and specified tagged traffic
2186 * (and not any taged and untagged)
2187 */
2188 if (vid > 0) {
2189 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
2190 I40E_VLAN_ANY,
2191 is_vf, is_netdev)) {
2192 i40e_del_filter(vsi, vsi->netdev->dev_addr,
2193 I40E_VLAN_ANY, is_vf, is_netdev);
2194 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
2195 is_vf, is_netdev);
2196 if (!add_f) {
2197 dev_info(&vsi->back->pdev->dev,
2198 "Could not add filter 0 for %pM\n",
2199 vsi->netdev->dev_addr);
2200 return -ENOMEM;
2201 }
2202 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002203 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002204
Greg Rose8d82a7c2014-01-13 16:13:04 -08002205 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2206 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002207 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2208 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2209 is_vf, is_netdev)) {
2210 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2211 is_vf, is_netdev);
2212 add_f = i40e_add_filter(vsi, f->macaddr,
2213 0, is_vf, is_netdev);
2214 if (!add_f) {
2215 dev_info(&vsi->back->pdev->dev,
2216 "Could not add filter 0 for %pM\n",
2217 f->macaddr);
2218 return -ENOMEM;
2219 }
2220 }
2221 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002222 }
2223
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002224 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2225 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2226 return 0;
2227
Anjali Singhai30e25612015-09-28 13:37:12 -07002228 return i40e_sync_vsi_filters(vsi, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002229}
2230
2231/**
2232 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2233 * @vsi: the vsi being configured
2234 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002235 *
2236 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002237 **/
2238int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
2239{
2240 struct net_device *netdev = vsi->netdev;
2241 struct i40e_mac_filter *f, *add_f;
2242 bool is_vf, is_netdev;
2243 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002244
2245 is_vf = (vsi->type == I40E_VSI_SRIOV);
2246 is_netdev = !!(netdev);
2247
2248 if (is_netdev)
2249 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
2250
2251 list_for_each_entry(f, &vsi->mac_filter_list, list)
2252 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2253
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002254 /* go through all the filters for this VSI and if there is only
2255 * vid == 0 it means there are no other filters, so vid 0 must
2256 * be replaced with -1. This signifies that we should from now
2257 * on accept any traffic (with any tag present, or untagged)
2258 */
2259 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2260 if (is_netdev) {
2261 if (f->vlan &&
2262 ether_addr_equal(netdev->dev_addr, f->macaddr))
2263 filter_count++;
2264 }
2265
2266 if (f->vlan)
2267 filter_count++;
2268 }
2269
2270 if (!filter_count && is_netdev) {
2271 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2272 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2273 is_vf, is_netdev);
2274 if (!f) {
2275 dev_info(&vsi->back->pdev->dev,
2276 "Could not add filter %d for %pM\n",
2277 I40E_VLAN_ANY, netdev->dev_addr);
2278 return -ENOMEM;
2279 }
2280 }
2281
2282 if (!filter_count) {
2283 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2284 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2285 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2286 is_vf, is_netdev);
2287 if (!add_f) {
2288 dev_info(&vsi->back->pdev->dev,
2289 "Could not add filter %d for %pM\n",
2290 I40E_VLAN_ANY, f->macaddr);
2291 return -ENOMEM;
2292 }
2293 }
2294 }
2295
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002296 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2297 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2298 return 0;
2299
Anjali Singhai30e25612015-09-28 13:37:12 -07002300 return i40e_sync_vsi_filters(vsi, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002301}
2302
2303/**
2304 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2305 * @netdev: network interface to be adjusted
2306 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002307 *
2308 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002309 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002310#ifdef I40E_FCOE
2311int i40e_vlan_rx_add_vid(struct net_device *netdev,
2312 __always_unused __be16 proto, u16 vid)
2313#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002314static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2315 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002316#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002317{
2318 struct i40e_netdev_priv *np = netdev_priv(netdev);
2319 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002320 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002321
2322 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002323 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002324
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002325 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2326
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002327 /* If the network stack called us with vid = 0 then
2328 * it is asking to receive priority tagged packets with
2329 * vlan id 0. Our HW receives them by default when configured
2330 * to receive untagged packets so there is no need to add an
2331 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002332 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002333 if (vid)
2334 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002335
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002336 if (!ret && (vid < VLAN_N_VID))
2337 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002338
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002339 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002340}
2341
2342/**
2343 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2344 * @netdev: network interface to be adjusted
2345 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002346 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002347 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002348 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002349#ifdef I40E_FCOE
2350int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2351 __always_unused __be16 proto, u16 vid)
2352#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002353static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2354 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002355#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002356{
2357 struct i40e_netdev_priv *np = netdev_priv(netdev);
2358 struct i40e_vsi *vsi = np->vsi;
2359
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002360 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2361
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002362 /* return code is ignored as there is nothing a user
2363 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002364 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002365 */
2366 i40e_vsi_kill_vlan(vsi, vid);
2367
2368 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002369
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002370 return 0;
2371}
2372
2373/**
2374 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2375 * @vsi: the vsi being brought back up
2376 **/
2377static void i40e_restore_vlan(struct i40e_vsi *vsi)
2378{
2379 u16 vid;
2380
2381 if (!vsi->netdev)
2382 return;
2383
2384 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2385
2386 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2387 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2388 vid);
2389}
2390
2391/**
2392 * i40e_vsi_add_pvid - Add pvid for the VSI
2393 * @vsi: the vsi being adjusted
2394 * @vid: the vlan id to set as a PVID
2395 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002396int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002397{
2398 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002399 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002400
2401 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2402 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002403 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2404 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002405 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002406
2407 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002408 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002409 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2410 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002411 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002412 "add pvid failed, err %s aq_err %s\n",
2413 i40e_stat_str(&vsi->back->hw, ret),
2414 i40e_aq_str(&vsi->back->hw,
2415 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002416 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002417 }
2418
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002419 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002420}
2421
2422/**
2423 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2424 * @vsi: the vsi being adjusted
2425 *
2426 * Just use the vlan_rx_register() service to put it back to normal
2427 **/
2428void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2429{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002430 i40e_vlan_stripping_disable(vsi);
2431
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002432 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002433}
2434
2435/**
2436 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2437 * @vsi: ptr to the VSI
2438 *
2439 * If this function returns with an error, then it's possible one or
2440 * more of the rings is populated (while the rest are not). It is the
2441 * callers duty to clean those orphaned rings.
2442 *
2443 * Return 0 on success, negative on failure
2444 **/
2445static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2446{
2447 int i, err = 0;
2448
2449 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002450 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002451
2452 return err;
2453}
2454
2455/**
2456 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2457 * @vsi: ptr to the VSI
2458 *
2459 * Free VSI's transmit software resources
2460 **/
2461static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2462{
2463 int i;
2464
Greg Rose8e9dca52013-12-18 13:45:53 +00002465 if (!vsi->tx_rings)
2466 return;
2467
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002468 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002469 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002470 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002471}
2472
2473/**
2474 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2475 * @vsi: ptr to the VSI
2476 *
2477 * If this function returns with an error, then it's possible one or
2478 * more of the rings is populated (while the rest are not). It is the
2479 * callers duty to clean those orphaned rings.
2480 *
2481 * Return 0 on success, negative on failure
2482 **/
2483static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2484{
2485 int i, err = 0;
2486
2487 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002488 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002489#ifdef I40E_FCOE
2490 i40e_fcoe_setup_ddp_resources(vsi);
2491#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002492 return err;
2493}
2494
2495/**
2496 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2497 * @vsi: ptr to the VSI
2498 *
2499 * Free all receive software resources
2500 **/
2501static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2502{
2503 int i;
2504
Greg Rose8e9dca52013-12-18 13:45:53 +00002505 if (!vsi->rx_rings)
2506 return;
2507
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002508 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002509 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002510 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002511#ifdef I40E_FCOE
2512 i40e_fcoe_free_ddp_resources(vsi);
2513#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002514}
2515
2516/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002517 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2518 * @ring: The Tx ring to configure
2519 *
2520 * This enables/disables XPS for a given Tx descriptor ring
2521 * based on the TCs enabled for the VSI that ring belongs to.
2522 **/
2523static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2524{
2525 struct i40e_vsi *vsi = ring->vsi;
2526 cpumask_var_t mask;
2527
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002528 if (!ring->q_vector || !ring->netdev)
2529 return;
2530
2531 /* Single TC mode enable XPS */
2532 if (vsi->tc_config.numtc <= 1) {
2533 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002534 netif_set_xps_queue(ring->netdev,
2535 &ring->q_vector->affinity_mask,
2536 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002537 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2538 /* Disable XPS to allow selection based on TC */
2539 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2540 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2541 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002542 }
2543}
2544
2545/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002546 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2547 * @ring: The Tx ring to configure
2548 *
2549 * Configure the Tx descriptor ring in the HMC context.
2550 **/
2551static int i40e_configure_tx_ring(struct i40e_ring *ring)
2552{
2553 struct i40e_vsi *vsi = ring->vsi;
2554 u16 pf_q = vsi->base_queue + ring->queue_index;
2555 struct i40e_hw *hw = &vsi->back->hw;
2556 struct i40e_hmc_obj_txq tx_ctx;
2557 i40e_status err = 0;
2558 u32 qtx_ctl = 0;
2559
2560 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002561 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002562 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2563 ring->atr_count = 0;
2564 } else {
2565 ring->atr_sample_rate = 0;
2566 }
2567
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002568 /* configure XPS */
2569 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002570
2571 /* clear the context structure first */
2572 memset(&tx_ctx, 0, sizeof(tx_ctx));
2573
2574 tx_ctx.new_context = 1;
2575 tx_ctx.base = (ring->dma / 128);
2576 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002577 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2578 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002579#ifdef I40E_FCOE
2580 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2581#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002582 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002583 /* FDIR VSI tx ring can still use RS bit and writebacks */
2584 if (vsi->type != I40E_VSI_FDIR)
2585 tx_ctx.head_wb_ena = 1;
2586 tx_ctx.head_wb_addr = ring->dma +
2587 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002588
2589 /* As part of VSI creation/update, FW allocates certain
2590 * Tx arbitration queue sets for each TC enabled for
2591 * the VSI. The FW returns the handles to these queue
2592 * sets as part of the response buffer to Add VSI,
2593 * Update VSI, etc. AQ commands. It is expected that
2594 * these queue set handles be associated with the Tx
2595 * queues by the driver as part of the TX queue context
2596 * initialization. This has to be done regardless of
2597 * DCB as by default everything is mapped to TC0.
2598 */
2599 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2600 tx_ctx.rdylist_act = 0;
2601
2602 /* clear the context in the HMC */
2603 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2604 if (err) {
2605 dev_info(&vsi->back->pdev->dev,
2606 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2607 ring->queue_index, pf_q, err);
2608 return -ENOMEM;
2609 }
2610
2611 /* set the context in the HMC */
2612 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2613 if (err) {
2614 dev_info(&vsi->back->pdev->dev,
2615 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2616 ring->queue_index, pf_q, err);
2617 return -ENOMEM;
2618 }
2619
2620 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002621 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002622 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002623 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2624 I40E_QTX_CTL_VFVM_INDX_MASK;
2625 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002626 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002627 }
2628
Shannon Nelson13fd977492013-09-28 07:14:19 +00002629 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2630 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002631 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2632 i40e_flush(hw);
2633
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002634 /* cache tail off for easier writes later */
2635 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2636
2637 return 0;
2638}
2639
2640/**
2641 * i40e_configure_rx_ring - Configure a receive ring context
2642 * @ring: The Rx ring to configure
2643 *
2644 * Configure the Rx descriptor ring in the HMC context.
2645 **/
2646static int i40e_configure_rx_ring(struct i40e_ring *ring)
2647{
2648 struct i40e_vsi *vsi = ring->vsi;
2649 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2650 u16 pf_q = vsi->base_queue + ring->queue_index;
2651 struct i40e_hw *hw = &vsi->back->hw;
2652 struct i40e_hmc_obj_rxq rx_ctx;
2653 i40e_status err = 0;
2654
2655 ring->state = 0;
2656
2657 /* clear the context structure first */
2658 memset(&rx_ctx, 0, sizeof(rx_ctx));
2659
2660 ring->rx_buf_len = vsi->rx_buf_len;
2661 ring->rx_hdr_len = vsi->rx_hdr_len;
2662
2663 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2664 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2665
2666 rx_ctx.base = (ring->dma / 128);
2667 rx_ctx.qlen = ring->count;
2668
2669 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2670 set_ring_16byte_desc_enabled(ring);
2671 rx_ctx.dsize = 0;
2672 } else {
2673 rx_ctx.dsize = 1;
2674 }
2675
2676 rx_ctx.dtype = vsi->dtype;
2677 if (vsi->dtype) {
2678 set_ring_ps_enabled(ring);
2679 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2680 I40E_RX_SPLIT_IP |
2681 I40E_RX_SPLIT_TCP_UDP |
2682 I40E_RX_SPLIT_SCTP;
2683 } else {
2684 rx_ctx.hsplit_0 = 0;
2685 }
2686
2687 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2688 (chain_len * ring->rx_buf_len));
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002689 if (hw->revision_id == 0)
2690 rx_ctx.lrxqthresh = 0;
2691 else
2692 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002693 rx_ctx.crcstrip = 1;
2694 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07002695 /* this controls whether VLAN is stripped from inner headers */
2696 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07002697#ifdef I40E_FCOE
2698 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2699#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002700 /* set the prefena field to 1 because the manual says to */
2701 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002702
2703 /* clear the context in the HMC */
2704 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2705 if (err) {
2706 dev_info(&vsi->back->pdev->dev,
2707 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2708 ring->queue_index, pf_q, err);
2709 return -ENOMEM;
2710 }
2711
2712 /* set the context in the HMC */
2713 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2714 if (err) {
2715 dev_info(&vsi->back->pdev->dev,
2716 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2717 ring->queue_index, pf_q, err);
2718 return -ENOMEM;
2719 }
2720
2721 /* cache tail for quicker writes, and clear the reg before use */
2722 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2723 writel(0, ring->tail);
2724
Mitch Williamsa132af22015-01-24 09:58:35 +00002725 if (ring_is_ps_enabled(ring)) {
2726 i40e_alloc_rx_headers(ring);
2727 i40e_alloc_rx_buffers_ps(ring, I40E_DESC_UNUSED(ring));
2728 } else {
2729 i40e_alloc_rx_buffers_1buf(ring, I40E_DESC_UNUSED(ring));
2730 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002731
2732 return 0;
2733}
2734
2735/**
2736 * i40e_vsi_configure_tx - Configure the VSI for Tx
2737 * @vsi: VSI structure describing this set of rings and resources
2738 *
2739 * Configure the Tx VSI for operation.
2740 **/
2741static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2742{
2743 int err = 0;
2744 u16 i;
2745
Alexander Duyck9f65e152013-09-28 06:00:58 +00002746 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2747 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002748
2749 return err;
2750}
2751
2752/**
2753 * i40e_vsi_configure_rx - Configure the VSI for Rx
2754 * @vsi: the VSI being configured
2755 *
2756 * Configure the Rx VSI for operation.
2757 **/
2758static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2759{
2760 int err = 0;
2761 u16 i;
2762
2763 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2764 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2765 + ETH_FCS_LEN + VLAN_HLEN;
2766 else
2767 vsi->max_frame = I40E_RXBUFFER_2048;
2768
2769 /* figure out correct receive buffer length */
2770 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2771 I40E_FLAG_RX_PS_ENABLED)) {
2772 case I40E_FLAG_RX_1BUF_ENABLED:
2773 vsi->rx_hdr_len = 0;
2774 vsi->rx_buf_len = vsi->max_frame;
2775 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2776 break;
2777 case I40E_FLAG_RX_PS_ENABLED:
2778 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2779 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2780 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2781 break;
2782 default:
2783 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2784 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2785 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2786 break;
2787 }
2788
Vasu Dev38e00432014-08-01 13:27:03 -07002789#ifdef I40E_FCOE
2790 /* setup rx buffer for FCoE */
2791 if ((vsi->type == I40E_VSI_FCOE) &&
2792 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
2793 vsi->rx_hdr_len = 0;
2794 vsi->rx_buf_len = I40E_RXBUFFER_3072;
2795 vsi->max_frame = I40E_RXBUFFER_3072;
2796 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2797 }
2798
2799#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002800 /* round up for the chip's needs */
2801 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002802 BIT_ULL(I40E_RXQ_CTX_HBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002803 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002804 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002805
2806 /* set up individual rings */
2807 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002808 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002809
2810 return err;
2811}
2812
2813/**
2814 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2815 * @vsi: ptr to the VSI
2816 **/
2817static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2818{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002819 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002820 u16 qoffset, qcount;
2821 int i, n;
2822
Parikh, Neeravcd238a32015-02-21 06:43:37 +00002823 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2824 /* Reset the TC information */
2825 for (i = 0; i < vsi->num_queue_pairs; i++) {
2826 rx_ring = vsi->rx_rings[i];
2827 tx_ring = vsi->tx_rings[i];
2828 rx_ring->dcb_tc = 0;
2829 tx_ring->dcb_tc = 0;
2830 }
2831 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002832
2833 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002834 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002835 continue;
2836
2837 qoffset = vsi->tc_config.tc_info[n].qoffset;
2838 qcount = vsi->tc_config.tc_info[n].qcount;
2839 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002840 rx_ring = vsi->rx_rings[i];
2841 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002842 rx_ring->dcb_tc = n;
2843 tx_ring->dcb_tc = n;
2844 }
2845 }
2846}
2847
2848/**
2849 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2850 * @vsi: ptr to the VSI
2851 **/
2852static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2853{
2854 if (vsi->netdev)
2855 i40e_set_rx_mode(vsi->netdev);
2856}
2857
2858/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002859 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
2860 * @vsi: Pointer to the targeted VSI
2861 *
2862 * This function replays the hlist on the hw where all the SB Flow Director
2863 * filters were saved.
2864 **/
2865static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
2866{
2867 struct i40e_fdir_filter *filter;
2868 struct i40e_pf *pf = vsi->back;
2869 struct hlist_node *node;
2870
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002871 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2872 return;
2873
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002874 hlist_for_each_entry_safe(filter, node,
2875 &pf->fdir_filter_list, fdir_node) {
2876 i40e_add_del_fdir(vsi, filter, true);
2877 }
2878}
2879
2880/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002881 * i40e_vsi_configure - Set up the VSI for action
2882 * @vsi: the VSI being configured
2883 **/
2884static int i40e_vsi_configure(struct i40e_vsi *vsi)
2885{
2886 int err;
2887
2888 i40e_set_vsi_rx_mode(vsi);
2889 i40e_restore_vlan(vsi);
2890 i40e_vsi_config_dcb_rings(vsi);
2891 err = i40e_vsi_configure_tx(vsi);
2892 if (!err)
2893 err = i40e_vsi_configure_rx(vsi);
2894
2895 return err;
2896}
2897
2898/**
2899 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2900 * @vsi: the VSI being configured
2901 **/
2902static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2903{
2904 struct i40e_pf *pf = vsi->back;
2905 struct i40e_q_vector *q_vector;
2906 struct i40e_hw *hw = &pf->hw;
2907 u16 vector;
2908 int i, q;
2909 u32 val;
2910 u32 qp;
2911
2912 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2913 * and PFINT_LNKLSTn registers, e.g.:
2914 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2915 */
2916 qp = vsi->base_queue;
2917 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002918 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2919 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002920 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2921 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2922 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2923 q_vector->rx.itr);
2924 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2925 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2926 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2927 q_vector->tx.itr);
2928
2929 /* Linked list for the queuepairs assigned to this vector */
2930 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2931 for (q = 0; q < q_vector->num_ringpairs; q++) {
2932 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2933 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2934 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2935 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2936 (I40E_QUEUE_TYPE_TX
2937 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2938
2939 wr32(hw, I40E_QINT_RQCTL(qp), val);
2940
2941 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2942 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2943 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2944 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2945 (I40E_QUEUE_TYPE_RX
2946 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2947
2948 /* Terminate the linked list */
2949 if (q == (q_vector->num_ringpairs - 1))
2950 val |= (I40E_QUEUE_END_OF_LIST
2951 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2952
2953 wr32(hw, I40E_QINT_TQCTL(qp), val);
2954 qp++;
2955 }
2956 }
2957
2958 i40e_flush(hw);
2959}
2960
2961/**
2962 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2963 * @hw: ptr to the hardware info
2964 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00002965static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002966{
Jacob Kellerab437b52014-12-14 01:55:08 +00002967 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002968 u32 val;
2969
2970 /* clear things first */
2971 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2972 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2973
2974 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2975 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2976 I40E_PFINT_ICR0_ENA_GRST_MASK |
2977 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2978 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002979 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2980 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2981 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2982
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04002983 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
2984 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
2985
Jacob Kellerab437b52014-12-14 01:55:08 +00002986 if (pf->flags & I40E_FLAG_PTP)
2987 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
2988
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002989 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2990
2991 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002992 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2993 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002994
2995 /* OTHER_ITR_IDX = 0 */
2996 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2997}
2998
2999/**
3000 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3001 * @vsi: the VSI being configured
3002 **/
3003static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3004{
Alexander Duyck493fb302013-09-28 07:01:44 +00003005 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003006 struct i40e_pf *pf = vsi->back;
3007 struct i40e_hw *hw = &pf->hw;
3008 u32 val;
3009
3010 /* set the ITR configuration */
3011 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
3012 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3013 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
3014 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
3015 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3016 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3017
Jacob Kellerab437b52014-12-14 01:55:08 +00003018 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003019
3020 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3021 wr32(hw, I40E_PFINT_LNKLST0, 0);
3022
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003023 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003024 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3025 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3026 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3027
3028 wr32(hw, I40E_QINT_RQCTL(0), val);
3029
3030 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3031 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3032 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3033
3034 wr32(hw, I40E_QINT_TQCTL(0), val);
3035 i40e_flush(hw);
3036}
3037
3038/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003039 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3040 * @pf: board private structure
3041 **/
3042void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3043{
3044 struct i40e_hw *hw = &pf->hw;
3045
3046 wr32(hw, I40E_PFINT_DYN_CTL0,
3047 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3048 i40e_flush(hw);
3049}
3050
3051/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003052 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3053 * @pf: board private structure
3054 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00003055void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003056{
3057 struct i40e_hw *hw = &pf->hw;
3058 u32 val;
3059
3060 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
3061 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3062 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3063
3064 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3065 i40e_flush(hw);
3066}
3067
3068/**
3069 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
3070 * @vsi: pointer to a vsi
Jesse Brandeburg78455482015-07-23 16:54:41 -04003071 * @vector: enable a particular Hw Interrupt vector, without base_vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003072 **/
3073void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
3074{
3075 struct i40e_pf *pf = vsi->back;
3076 struct i40e_hw *hw = &pf->hw;
3077 u32 val;
3078
3079 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
3080 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
3081 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
Jesse Brandeburg78455482015-07-23 16:54:41 -04003082 wr32(hw, I40E_PFINT_DYN_CTLN(vector + vsi->base_vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003083 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003084}
3085
3086/**
Carolyn Wyborny5c2cebd2014-06-04 01:23:18 +00003087 * i40e_irq_dynamic_disable - Disable default interrupt generation settings
3088 * @vsi: pointer to a vsi
Greg Rose03147772015-01-24 09:58:29 +00003089 * @vector: disable a particular Hw Interrupt vector
Carolyn Wyborny5c2cebd2014-06-04 01:23:18 +00003090 **/
3091void i40e_irq_dynamic_disable(struct i40e_vsi *vsi, int vector)
3092{
3093 struct i40e_pf *pf = vsi->back;
3094 struct i40e_hw *hw = &pf->hw;
3095 u32 val;
3096
3097 val = I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT;
3098 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
3099 i40e_flush(hw);
3100}
3101
3102/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003103 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3104 * @irq: interrupt number
3105 * @data: pointer to a q_vector
3106 **/
3107static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3108{
3109 struct i40e_q_vector *q_vector = data;
3110
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003111 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003112 return IRQ_HANDLED;
3113
3114 napi_schedule(&q_vector->napi);
3115
3116 return IRQ_HANDLED;
3117}
3118
3119/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003120 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3121 * @vsi: the VSI being configured
3122 * @basename: name for the vector
3123 *
3124 * Allocates MSI-X vectors and requests interrupts from the kernel.
3125 **/
3126static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3127{
3128 int q_vectors = vsi->num_q_vectors;
3129 struct i40e_pf *pf = vsi->back;
3130 int base = vsi->base_vector;
3131 int rx_int_idx = 0;
3132 int tx_int_idx = 0;
3133 int vector, err;
3134
3135 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003136 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003137
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003138 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003139 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3140 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3141 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003142 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003143 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3144 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003145 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003146 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3147 "%s-%s-%d", basename, "tx", tx_int_idx++);
3148 } else {
3149 /* skip this unused q_vector */
3150 continue;
3151 }
3152 err = request_irq(pf->msix_entries[base + vector].vector,
3153 vsi->irq_handler,
3154 0,
3155 q_vector->name,
3156 q_vector);
3157 if (err) {
3158 dev_info(&pf->pdev->dev,
3159 "%s: request_irq failed, error: %d\n",
3160 __func__, err);
3161 goto free_queue_irqs;
3162 }
3163 /* assign the mask for this irq */
3164 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3165 &q_vector->affinity_mask);
3166 }
3167
Shannon Nelson63741842014-04-23 04:50:16 +00003168 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003169 return 0;
3170
3171free_queue_irqs:
3172 while (vector) {
3173 vector--;
3174 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3175 NULL);
3176 free_irq(pf->msix_entries[base + vector].vector,
3177 &(vsi->q_vectors[vector]));
3178 }
3179 return err;
3180}
3181
3182/**
3183 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3184 * @vsi: the VSI being un-configured
3185 **/
3186static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3187{
3188 struct i40e_pf *pf = vsi->back;
3189 struct i40e_hw *hw = &pf->hw;
3190 int base = vsi->base_vector;
3191 int i;
3192
3193 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003194 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3195 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003196 }
3197
3198 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3199 for (i = vsi->base_vector;
3200 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3201 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3202
3203 i40e_flush(hw);
3204 for (i = 0; i < vsi->num_q_vectors; i++)
3205 synchronize_irq(pf->msix_entries[i + base].vector);
3206 } else {
3207 /* Legacy and MSI mode - this stops all interrupt handling */
3208 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3209 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3210 i40e_flush(hw);
3211 synchronize_irq(pf->pdev->irq);
3212 }
3213}
3214
3215/**
3216 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3217 * @vsi: the VSI being configured
3218 **/
3219static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3220{
3221 struct i40e_pf *pf = vsi->back;
3222 int i;
3223
3224 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003225 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003226 i40e_irq_dynamic_enable(vsi, i);
3227 } else {
3228 i40e_irq_dynamic_enable_icr0(pf);
3229 }
3230
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003231 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003232 return 0;
3233}
3234
3235/**
3236 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3237 * @pf: board private structure
3238 **/
3239static void i40e_stop_misc_vector(struct i40e_pf *pf)
3240{
3241 /* Disable ICR 0 */
3242 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3243 i40e_flush(&pf->hw);
3244}
3245
3246/**
3247 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3248 * @irq: interrupt number
3249 * @data: pointer to a q_vector
3250 *
3251 * This is the handler used for all MSI/Legacy interrupts, and deals
3252 * with both queue and non-queue interrupts. This is also used in
3253 * MSIX mode to handle the non-queue interrupts.
3254 **/
3255static irqreturn_t i40e_intr(int irq, void *data)
3256{
3257 struct i40e_pf *pf = (struct i40e_pf *)data;
3258 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003259 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003260 u32 icr0, icr0_remaining;
3261 u32 val, ena_mask;
3262
3263 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003264 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003265
Shannon Nelson116a57d2013-09-28 07:13:59 +00003266 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3267 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003268 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003269
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003270 /* if interrupt but no bits showing, must be SWINT */
3271 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3272 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3273 pf->sw_int_count++;
3274
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003275 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3276 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3277 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3278 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3279 dev_info(&pf->pdev->dev, "cleared PE_CRITERR\n");
3280 }
3281
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003282 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3283 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
3284
3285 /* temporarily disable queue cause for NAPI processing */
3286 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
3287 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3288 wr32(hw, I40E_QINT_RQCTL(0), qval);
3289
3290 qval = rd32(hw, I40E_QINT_TQCTL(0));
3291 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3292 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003293
3294 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00003295 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003296 }
3297
3298 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3299 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3300 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
3301 }
3302
3303 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3304 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3305 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3306 }
3307
3308 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3309 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3310 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3311 }
3312
3313 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3314 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3315 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3316 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3317 val = rd32(hw, I40E_GLGEN_RSTAT);
3318 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3319 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003320 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003321 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003322 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003323 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003324 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003325 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003326 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003327 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003328 }
3329
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003330 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3331 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3332 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003333 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3334 rd32(hw, I40E_PFHMC_ERRORINFO),
3335 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003336 }
3337
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003338 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3339 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3340
3341 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003342 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003343 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003344 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003345 }
3346
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003347 /* If a critical error is pending we have no choice but to reset the
3348 * device.
3349 * Report and mask out any remaining unexpected interrupts.
3350 */
3351 icr0_remaining = icr0 & ena_mask;
3352 if (icr0_remaining) {
3353 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3354 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003355 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003356 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003357 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003358 dev_info(&pf->pdev->dev, "device will be reset\n");
3359 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3360 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003361 }
3362 ena_mask &= ~icr0_remaining;
3363 }
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003364 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003365
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003366enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003367 /* re-enable interrupt causes */
3368 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003369 if (!test_bit(__I40E_DOWN, &pf->state)) {
3370 i40e_service_event_schedule(pf);
3371 i40e_irq_dynamic_enable_icr0(pf);
3372 }
3373
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003374 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003375}
3376
3377/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003378 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3379 * @tx_ring: tx ring to clean
3380 * @budget: how many cleans we're allowed
3381 *
3382 * Returns true if there's any budget left (e.g. the clean is finished)
3383 **/
3384static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3385{
3386 struct i40e_vsi *vsi = tx_ring->vsi;
3387 u16 i = tx_ring->next_to_clean;
3388 struct i40e_tx_buffer *tx_buf;
3389 struct i40e_tx_desc *tx_desc;
3390
3391 tx_buf = &tx_ring->tx_bi[i];
3392 tx_desc = I40E_TX_DESC(tx_ring, i);
3393 i -= tx_ring->count;
3394
3395 do {
3396 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3397
3398 /* if next_to_watch is not set then there is no work pending */
3399 if (!eop_desc)
3400 break;
3401
3402 /* prevent any other reads prior to eop_desc */
3403 read_barrier_depends();
3404
3405 /* if the descriptor isn't done, no work yet to do */
3406 if (!(eop_desc->cmd_type_offset_bsz &
3407 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3408 break;
3409
3410 /* clear next_to_watch to prevent false hangs */
3411 tx_buf->next_to_watch = NULL;
3412
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003413 tx_desc->buffer_addr = 0;
3414 tx_desc->cmd_type_offset_bsz = 0;
3415 /* move past filter desc */
3416 tx_buf++;
3417 tx_desc++;
3418 i++;
3419 if (unlikely(!i)) {
3420 i -= tx_ring->count;
3421 tx_buf = tx_ring->tx_bi;
3422 tx_desc = I40E_TX_DESC(tx_ring, 0);
3423 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003424 /* unmap skb header data */
3425 dma_unmap_single(tx_ring->dev,
3426 dma_unmap_addr(tx_buf, dma),
3427 dma_unmap_len(tx_buf, len),
3428 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003429 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3430 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003431
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003432 tx_buf->raw_buf = NULL;
3433 tx_buf->tx_flags = 0;
3434 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003435 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003436 tx_desc->buffer_addr = 0;
3437 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003438
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003439 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003440 tx_buf++;
3441 tx_desc++;
3442 i++;
3443 if (unlikely(!i)) {
3444 i -= tx_ring->count;
3445 tx_buf = tx_ring->tx_bi;
3446 tx_desc = I40E_TX_DESC(tx_ring, 0);
3447 }
3448
3449 /* update budget accounting */
3450 budget--;
3451 } while (likely(budget));
3452
3453 i += tx_ring->count;
3454 tx_ring->next_to_clean = i;
3455
3456 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003457 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003458 }
3459 return budget > 0;
3460}
3461
3462/**
3463 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3464 * @irq: interrupt number
3465 * @data: pointer to a q_vector
3466 **/
3467static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3468{
3469 struct i40e_q_vector *q_vector = data;
3470 struct i40e_vsi *vsi;
3471
3472 if (!q_vector->tx.ring)
3473 return IRQ_HANDLED;
3474
3475 vsi = q_vector->tx.ring->vsi;
3476 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3477
3478 return IRQ_HANDLED;
3479}
3480
3481/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003482 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003483 * @vsi: the VSI being configured
3484 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003485 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003486 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003487static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003488{
Alexander Duyck493fb302013-09-28 07:01:44 +00003489 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003490 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3491 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003492
3493 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003494 tx_ring->next = q_vector->tx.ring;
3495 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003496 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003497
3498 rx_ring->q_vector = q_vector;
3499 rx_ring->next = q_vector->rx.ring;
3500 q_vector->rx.ring = rx_ring;
3501 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003502}
3503
3504/**
3505 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3506 * @vsi: the VSI being configured
3507 *
3508 * This function maps descriptor rings to the queue-specific vectors
3509 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3510 * one vector per queue pair, but on a constrained vector budget, we
3511 * group the queue pairs as "efficiently" as possible.
3512 **/
3513static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3514{
3515 int qp_remaining = vsi->num_queue_pairs;
3516 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003517 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003518 int v_start = 0;
3519 int qp_idx = 0;
3520
3521 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3522 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003523 * It is also important to go through all the vectors available to be
3524 * sure that if we don't use all the vectors, that the remaining vectors
3525 * are cleared. This is especially important when decreasing the
3526 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003527 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003528 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003529 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3530
3531 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3532
3533 q_vector->num_ringpairs = num_ringpairs;
3534
3535 q_vector->rx.count = 0;
3536 q_vector->tx.count = 0;
3537 q_vector->rx.ring = NULL;
3538 q_vector->tx.ring = NULL;
3539
3540 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003541 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003542 qp_idx++;
3543 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003544 }
3545 }
3546}
3547
3548/**
3549 * i40e_vsi_request_irq - Request IRQ from the OS
3550 * @vsi: the VSI being configured
3551 * @basename: name for the vector
3552 **/
3553static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3554{
3555 struct i40e_pf *pf = vsi->back;
3556 int err;
3557
3558 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3559 err = i40e_vsi_request_irq_msix(vsi, basename);
3560 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3561 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003562 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003563 else
3564 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003565 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003566
3567 if (err)
3568 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3569
3570 return err;
3571}
3572
3573#ifdef CONFIG_NET_POLL_CONTROLLER
3574/**
3575 * i40e_netpoll - A Polling 'interrupt'handler
3576 * @netdev: network interface device structure
3577 *
3578 * This is used by netconsole to send skbs without having to re-enable
3579 * interrupts. It's not called while the normal interrupt routine is executing.
3580 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003581#ifdef I40E_FCOE
3582void i40e_netpoll(struct net_device *netdev)
3583#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003584static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003585#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003586{
3587 struct i40e_netdev_priv *np = netdev_priv(netdev);
3588 struct i40e_vsi *vsi = np->vsi;
3589 struct i40e_pf *pf = vsi->back;
3590 int i;
3591
3592 /* if interface is down do nothing */
3593 if (test_bit(__I40E_DOWN, &vsi->state))
3594 return;
3595
3596 pf->flags |= I40E_FLAG_IN_NETPOLL;
3597 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3598 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003599 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003600 } else {
3601 i40e_intr(pf->pdev->irq, netdev);
3602 }
3603 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3604}
3605#endif
3606
3607/**
Neerav Parikh23527302014-06-03 23:50:15 +00003608 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3609 * @pf: the PF being configured
3610 * @pf_q: the PF queue
3611 * @enable: enable or disable state of the queue
3612 *
3613 * This routine will wait for the given Tx queue of the PF to reach the
3614 * enabled or disabled state.
3615 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3616 * multiple retries; else will return 0 in case of success.
3617 **/
3618static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3619{
3620 int i;
3621 u32 tx_reg;
3622
3623 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3624 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3625 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3626 break;
3627
Neerav Parikhf98a2002014-09-13 07:40:44 +00003628 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003629 }
3630 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3631 return -ETIMEDOUT;
3632
3633 return 0;
3634}
3635
3636/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003637 * i40e_vsi_control_tx - Start or stop a VSI's rings
3638 * @vsi: the VSI being configured
3639 * @enable: start or stop the rings
3640 **/
3641static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3642{
3643 struct i40e_pf *pf = vsi->back;
3644 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003645 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003646 u32 tx_reg;
3647
3648 pf_q = vsi->base_queue;
3649 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003650
3651 /* warn the TX unit of coming changes */
3652 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3653 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003654 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00003655
Mitch Williams6c5ef622014-02-20 19:29:16 -08003656 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003657 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003658 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3659 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3660 break;
3661 usleep_range(1000, 2000);
3662 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003663 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c1220072014-03-14 07:32:29 +00003664 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003665 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003666
3667 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003668 if (enable) {
3669 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003670 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003671 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003672 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003673 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003674
3675 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003676 /* No waiting for the Tx queue to disable */
3677 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3678 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003679
3680 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003681 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3682 if (ret) {
3683 dev_info(&pf->pdev->dev,
3684 "%s: VSI seid %d Tx ring %d %sable timeout\n",
3685 __func__, vsi->seid, pf_q,
3686 (enable ? "en" : "dis"));
3687 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003688 }
3689 }
3690
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003691 if (hw->revision_id == 0)
3692 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003693 return ret;
3694}
3695
3696/**
3697 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3698 * @pf: the PF being configured
3699 * @pf_q: the PF queue
3700 * @enable: enable or disable state of the queue
3701 *
3702 * This routine will wait for the given Rx queue of the PF to reach the
3703 * enabled or disabled state.
3704 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3705 * multiple retries; else will return 0 in case of success.
3706 **/
3707static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3708{
3709 int i;
3710 u32 rx_reg;
3711
3712 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3713 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3714 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3715 break;
3716
Neerav Parikhf98a2002014-09-13 07:40:44 +00003717 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003718 }
3719 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3720 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003721
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003722 return 0;
3723}
3724
3725/**
3726 * i40e_vsi_control_rx - Start or stop a VSI's rings
3727 * @vsi: the VSI being configured
3728 * @enable: start or stop the rings
3729 **/
3730static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3731{
3732 struct i40e_pf *pf = vsi->back;
3733 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003734 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003735 u32 rx_reg;
3736
3737 pf_q = vsi->base_queue;
3738 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003739 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003740 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003741 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3742 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3743 break;
3744 usleep_range(1000, 2000);
3745 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003746
Catherine Sullivan7c1220072014-03-14 07:32:29 +00003747 /* Skip if the queue is already in the requested state */
3748 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3749 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003750
3751 /* turn on/off the queue */
3752 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003753 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003754 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003755 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003756 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3757
3758 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003759 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3760 if (ret) {
3761 dev_info(&pf->pdev->dev,
3762 "%s: VSI seid %d Rx ring %d %sable timeout\n",
3763 __func__, vsi->seid, pf_q,
3764 (enable ? "en" : "dis"));
3765 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003766 }
3767 }
3768
Neerav Parikh23527302014-06-03 23:50:15 +00003769 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003770}
3771
3772/**
3773 * i40e_vsi_control_rings - Start or stop a VSI's rings
3774 * @vsi: the VSI being configured
3775 * @enable: start or stop the rings
3776 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003777int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003778{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003779 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003780
3781 /* do rx first for enable and last for disable */
3782 if (request) {
3783 ret = i40e_vsi_control_rx(vsi, request);
3784 if (ret)
3785 return ret;
3786 ret = i40e_vsi_control_tx(vsi, request);
3787 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003788 /* Ignore return value, we need to shutdown whatever we can */
3789 i40e_vsi_control_tx(vsi, request);
3790 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003791 }
3792
3793 return ret;
3794}
3795
3796/**
3797 * i40e_vsi_free_irq - Free the irq association with the OS
3798 * @vsi: the VSI being configured
3799 **/
3800static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3801{
3802 struct i40e_pf *pf = vsi->back;
3803 struct i40e_hw *hw = &pf->hw;
3804 int base = vsi->base_vector;
3805 u32 val, qp;
3806 int i;
3807
3808 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3809 if (!vsi->q_vectors)
3810 return;
3811
Shannon Nelson63741842014-04-23 04:50:16 +00003812 if (!vsi->irqs_ready)
3813 return;
3814
3815 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003816 for (i = 0; i < vsi->num_q_vectors; i++) {
3817 u16 vector = i + base;
3818
3819 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003820 if (!vsi->q_vectors[i] ||
3821 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003822 continue;
3823
3824 /* clear the affinity_mask in the IRQ descriptor */
3825 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3826 NULL);
3827 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003828 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003829
3830 /* Tear down the interrupt queue link list
3831 *
3832 * We know that they come in pairs and always
3833 * the Rx first, then the Tx. To clear the
3834 * link list, stick the EOL value into the
3835 * next_q field of the registers.
3836 */
3837 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3838 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3839 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3840 val |= I40E_QUEUE_END_OF_LIST
3841 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3842 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3843
3844 while (qp != I40E_QUEUE_END_OF_LIST) {
3845 u32 next;
3846
3847 val = rd32(hw, I40E_QINT_RQCTL(qp));
3848
3849 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3850 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3851 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3852 I40E_QINT_RQCTL_INTEVENT_MASK);
3853
3854 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3855 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3856
3857 wr32(hw, I40E_QINT_RQCTL(qp), val);
3858
3859 val = rd32(hw, I40E_QINT_TQCTL(qp));
3860
3861 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3862 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3863
3864 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3865 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3866 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3867 I40E_QINT_TQCTL_INTEVENT_MASK);
3868
3869 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3870 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3871
3872 wr32(hw, I40E_QINT_TQCTL(qp), val);
3873 qp = next;
3874 }
3875 }
3876 } else {
3877 free_irq(pf->pdev->irq, pf);
3878
3879 val = rd32(hw, I40E_PFINT_LNKLST0);
3880 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3881 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3882 val |= I40E_QUEUE_END_OF_LIST
3883 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3884 wr32(hw, I40E_PFINT_LNKLST0, val);
3885
3886 val = rd32(hw, I40E_QINT_RQCTL(qp));
3887 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3888 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3889 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3890 I40E_QINT_RQCTL_INTEVENT_MASK);
3891
3892 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3893 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3894
3895 wr32(hw, I40E_QINT_RQCTL(qp), val);
3896
3897 val = rd32(hw, I40E_QINT_TQCTL(qp));
3898
3899 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3900 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3901 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3902 I40E_QINT_TQCTL_INTEVENT_MASK);
3903
3904 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3905 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3906
3907 wr32(hw, I40E_QINT_TQCTL(qp), val);
3908 }
3909}
3910
3911/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003912 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3913 * @vsi: the VSI being configured
3914 * @v_idx: Index of vector to be freed
3915 *
3916 * This function frees the memory allocated to the q_vector. In addition if
3917 * NAPI is enabled it will delete any references to the NAPI struct prior
3918 * to freeing the q_vector.
3919 **/
3920static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3921{
3922 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003923 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003924
3925 if (!q_vector)
3926 return;
3927
3928 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003929 i40e_for_each_ring(ring, q_vector->tx)
3930 ring->q_vector = NULL;
3931
3932 i40e_for_each_ring(ring, q_vector->rx)
3933 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003934
3935 /* only VSI w/ an associated netdev is set up w/ NAPI */
3936 if (vsi->netdev)
3937 netif_napi_del(&q_vector->napi);
3938
3939 vsi->q_vectors[v_idx] = NULL;
3940
3941 kfree_rcu(q_vector, rcu);
3942}
3943
3944/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003945 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3946 * @vsi: the VSI being un-configured
3947 *
3948 * This frees the memory allocated to the q_vectors and
3949 * deletes references to the NAPI struct.
3950 **/
3951static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3952{
3953 int v_idx;
3954
Alexander Duyck493fb302013-09-28 07:01:44 +00003955 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3956 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003957}
3958
3959/**
3960 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3961 * @pf: board private structure
3962 **/
3963static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3964{
3965 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3966 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3967 pci_disable_msix(pf->pdev);
3968 kfree(pf->msix_entries);
3969 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00003970 kfree(pf->irq_pile);
3971 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003972 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3973 pci_disable_msi(pf->pdev);
3974 }
3975 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3976}
3977
3978/**
3979 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3980 * @pf: board private structure
3981 *
3982 * We go through and clear interrupt specific resources and reset the structure
3983 * to pre-load conditions
3984 **/
3985static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3986{
3987 int i;
3988
Shannon Nelsone1477582015-02-21 06:44:33 +00003989 i40e_stop_misc_vector(pf);
3990 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3991 synchronize_irq(pf->msix_entries[0].vector);
3992 free_irq(pf->msix_entries[0].vector, pf);
3993 }
3994
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003995 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00003996 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003997 if (pf->vsi[i])
3998 i40e_vsi_free_q_vectors(pf->vsi[i]);
3999 i40e_reset_interrupt_capability(pf);
4000}
4001
4002/**
4003 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4004 * @vsi: the VSI being configured
4005 **/
4006static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4007{
4008 int q_idx;
4009
4010 if (!vsi->netdev)
4011 return;
4012
4013 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004014 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004015}
4016
4017/**
4018 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4019 * @vsi: the VSI being configured
4020 **/
4021static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4022{
4023 int q_idx;
4024
4025 if (!vsi->netdev)
4026 return;
4027
4028 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004029 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004030}
4031
4032/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004033 * i40e_vsi_close - Shut down a VSI
4034 * @vsi: the vsi to be quelled
4035 **/
4036static void i40e_vsi_close(struct i40e_vsi *vsi)
4037{
4038 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4039 i40e_down(vsi);
4040 i40e_vsi_free_irq(vsi);
4041 i40e_vsi_free_tx_resources(vsi);
4042 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004043 vsi->current_netdev_flags = 0;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004044}
4045
4046/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004047 * i40e_quiesce_vsi - Pause a given VSI
4048 * @vsi: the VSI being paused
4049 **/
4050static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4051{
4052 if (test_bit(__I40E_DOWN, &vsi->state))
4053 return;
4054
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004055 /* No need to disable FCoE VSI when Tx suspended */
4056 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4057 vsi->type == I40E_VSI_FCOE) {
4058 dev_dbg(&vsi->back->pdev->dev,
4059 "%s: VSI seid %d skipping FCoE VSI disable\n",
4060 __func__, vsi->seid);
4061 return;
4062 }
4063
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004064 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
4065 if (vsi->netdev && netif_running(vsi->netdev)) {
4066 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
4067 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004068 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004069 }
4070}
4071
4072/**
4073 * i40e_unquiesce_vsi - Resume a given VSI
4074 * @vsi: the VSI being resumed
4075 **/
4076static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4077{
4078 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4079 return;
4080
4081 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4082 if (vsi->netdev && netif_running(vsi->netdev))
4083 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4084 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004085 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004086}
4087
4088/**
4089 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4090 * @pf: the PF
4091 **/
4092static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4093{
4094 int v;
4095
Mitch Williams505682c2014-05-20 08:01:37 +00004096 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004097 if (pf->vsi[v])
4098 i40e_quiesce_vsi(pf->vsi[v]);
4099 }
4100}
4101
4102/**
4103 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4104 * @pf: the PF
4105 **/
4106static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4107{
4108 int v;
4109
Mitch Williams505682c2014-05-20 08:01:37 +00004110 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004111 if (pf->vsi[v])
4112 i40e_unquiesce_vsi(pf->vsi[v]);
4113 }
4114}
4115
Neerav Parikh69129dc2014-11-12 00:18:46 +00004116#ifdef CONFIG_I40E_DCB
4117/**
4118 * i40e_vsi_wait_txq_disabled - Wait for VSI's queues to be disabled
4119 * @vsi: the VSI being configured
4120 *
4121 * This function waits for the given VSI's Tx queues to be disabled.
4122 **/
4123static int i40e_vsi_wait_txq_disabled(struct i40e_vsi *vsi)
4124{
4125 struct i40e_pf *pf = vsi->back;
4126 int i, pf_q, ret;
4127
4128 pf_q = vsi->base_queue;
4129 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4130 /* Check and wait for the disable status of the queue */
4131 ret = i40e_pf_txq_wait(pf, pf_q, false);
4132 if (ret) {
4133 dev_info(&pf->pdev->dev,
4134 "%s: VSI seid %d Tx ring %d disable timeout\n",
4135 __func__, vsi->seid, pf_q);
4136 return ret;
4137 }
4138 }
4139
4140 return 0;
4141}
4142
4143/**
4144 * i40e_pf_wait_txq_disabled - Wait for all queues of PF VSIs to be disabled
4145 * @pf: the PF
4146 *
4147 * This function waits for the Tx queues to be in disabled state for all the
4148 * VSIs that are managed by this PF.
4149 **/
4150static int i40e_pf_wait_txq_disabled(struct i40e_pf *pf)
4151{
4152 int v, ret = 0;
4153
4154 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004155 /* No need to wait for FCoE VSI queues */
4156 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh69129dc2014-11-12 00:18:46 +00004157 ret = i40e_vsi_wait_txq_disabled(pf->vsi[v]);
4158 if (ret)
4159 break;
4160 }
4161 }
4162
4163 return ret;
4164}
4165
4166#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004167
4168/**
4169 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4170 * @q_idx: TX queue number
4171 * @vsi: Pointer to VSI struct
4172 *
4173 * This function checks specified queue for given VSI. Detects hung condition.
4174 * Sets hung bit since it is two step process. Before next run of service task
4175 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4176 * hung condition remain unchanged and during subsequent run, this function
4177 * issues SW interrupt to recover from hung condition.
4178 **/
4179static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4180{
4181 struct i40e_ring *tx_ring = NULL;
4182 struct i40e_pf *pf;
4183 u32 head, val, tx_pending;
4184 int i;
4185
4186 pf = vsi->back;
4187
4188 /* now that we have an index, find the tx_ring struct */
4189 for (i = 0; i < vsi->num_queue_pairs; i++) {
4190 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4191 if (q_idx == vsi->tx_rings[i]->queue_index) {
4192 tx_ring = vsi->tx_rings[i];
4193 break;
4194 }
4195 }
4196 }
4197
4198 if (!tx_ring)
4199 return;
4200
4201 /* Read interrupt register */
4202 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4203 val = rd32(&pf->hw,
4204 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4205 tx_ring->vsi->base_vector - 1));
4206 else
4207 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4208
4209 head = i40e_get_head(tx_ring);
4210
4211 tx_pending = i40e_get_tx_pending(tx_ring);
4212
4213 /* Interrupts are disabled and TX pending is non-zero,
4214 * trigger the SW interrupt (don't wait). Worst case
4215 * there will be one extra interrupt which may result
4216 * into not cleaning any queues because queues are cleaned.
4217 */
4218 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4219 i40e_force_wb(vsi, tx_ring->q_vector);
4220}
4221
4222/**
4223 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4224 * @pf: pointer to PF struct
4225 *
4226 * LAN VSI has netdev and netdev has TX queues. This function is to check
4227 * each of those TX queues if they are hung, trigger recovery by issuing
4228 * SW interrupt.
4229 **/
4230static void i40e_detect_recover_hung(struct i40e_pf *pf)
4231{
4232 struct net_device *netdev;
4233 struct i40e_vsi *vsi;
4234 int i;
4235
4236 /* Only for LAN VSI */
4237 vsi = pf->vsi[pf->lan_vsi];
4238
4239 if (!vsi)
4240 return;
4241
4242 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4243 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4244 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4245 return;
4246
4247 /* Make sure type is MAIN VSI */
4248 if (vsi->type != I40E_VSI_MAIN)
4249 return;
4250
4251 netdev = vsi->netdev;
4252 if (!netdev)
4253 return;
4254
4255 /* Bail out if netif_carrier is not OK */
4256 if (!netif_carrier_ok(netdev))
4257 return;
4258
4259 /* Go thru' TX queues for netdev */
4260 for (i = 0; i < netdev->num_tx_queues; i++) {
4261 struct netdev_queue *q;
4262
4263 q = netdev_get_tx_queue(netdev, i);
4264 if (q)
4265 i40e_detect_recover_hung_queue(i, vsi);
4266 }
4267}
4268
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004269/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004270 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004271 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004272 *
4273 * Get TC map for ISCSI PF type that will include iSCSI TC
4274 * and LAN TC.
4275 **/
4276static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4277{
4278 struct i40e_dcb_app_priority_table app;
4279 struct i40e_hw *hw = &pf->hw;
4280 u8 enabled_tc = 1; /* TC0 is always enabled */
4281 u8 tc, i;
4282 /* Get the iSCSI APP TLV */
4283 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4284
4285 for (i = 0; i < dcbcfg->numapps; i++) {
4286 app = dcbcfg->app[i];
4287 if (app.selector == I40E_APP_SEL_TCPIP &&
4288 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4289 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004290 enabled_tc |= BIT_ULL(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004291 break;
4292 }
4293 }
4294
4295 return enabled_tc;
4296}
4297
4298/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004299 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4300 * @dcbcfg: the corresponding DCBx configuration structure
4301 *
4302 * Return the number of TCs from given DCBx configuration
4303 **/
4304static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4305{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004306 u8 num_tc = 0;
4307 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004308
4309 /* Scan the ETS Config Priority Table to find
4310 * traffic class enabled for a given priority
4311 * and use the traffic class index to get the
4312 * number of traffic classes enabled
4313 */
4314 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4315 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
4316 num_tc = dcbcfg->etscfg.prioritytable[i];
4317 }
4318
4319 /* Traffic class index starts from zero so
4320 * increment to return the actual count
4321 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004322 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004323}
4324
4325/**
4326 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4327 * @dcbcfg: the corresponding DCBx configuration structure
4328 *
4329 * Query the current DCB configuration and return the number of
4330 * traffic classes enabled from the given DCBX config
4331 **/
4332static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4333{
4334 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4335 u8 enabled_tc = 1;
4336 u8 i;
4337
4338 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004339 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004340
4341 return enabled_tc;
4342}
4343
4344/**
4345 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4346 * @pf: PF being queried
4347 *
4348 * Return number of traffic classes enabled for the given PF
4349 **/
4350static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4351{
4352 struct i40e_hw *hw = &pf->hw;
4353 u8 i, enabled_tc;
4354 u8 num_tc = 0;
4355 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4356
4357 /* If DCB is not enabled then always in single TC */
4358 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4359 return 1;
4360
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004361 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004362 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4363 return i40e_dcb_get_num_tc(dcbcfg);
4364
4365 /* MFP mode return count of enabled TCs for this PF */
4366 if (pf->hw.func_caps.iscsi)
4367 enabled_tc = i40e_get_iscsi_tc_map(pf);
4368 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004369 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004370
4371 /* At least have TC0 */
4372 enabled_tc = (enabled_tc ? enabled_tc : 0x1);
4373 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004374 if (enabled_tc & BIT_ULL(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004375 num_tc++;
4376 }
4377 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004378}
4379
4380/**
4381 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
4382 * @pf: PF being queried
4383 *
4384 * Return a bitmap for first enabled traffic class for this PF.
4385 **/
4386static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
4387{
4388 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
4389 u8 i = 0;
4390
4391 if (!enabled_tc)
4392 return 0x1; /* TC0 */
4393
4394 /* Find the first enabled TC */
4395 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004396 if (enabled_tc & BIT_ULL(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004397 break;
4398 }
4399
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004400 return BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004401}
4402
4403/**
4404 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4405 * @pf: PF being queried
4406 *
4407 * Return a bitmap for enabled traffic classes for this PF.
4408 **/
4409static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4410{
4411 /* If DCB is not enabled for this PF then just return default TC */
4412 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4413 return i40e_pf_get_default_tc(pf);
4414
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004415 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004416 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4417 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4418
Neerav Parikhfc51de92015-02-24 06:58:53 +00004419 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004420 if (pf->hw.func_caps.iscsi)
4421 return i40e_get_iscsi_tc_map(pf);
4422 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004423 return i40e_pf_get_default_tc(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004424}
4425
4426/**
4427 * i40e_vsi_get_bw_info - Query VSI BW Information
4428 * @vsi: the VSI being queried
4429 *
4430 * Returns 0 on success, negative value on failure
4431 **/
4432static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4433{
4434 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4435 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4436 struct i40e_pf *pf = vsi->back;
4437 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004438 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004439 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004440 int i;
4441
4442 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004443 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4444 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004445 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004446 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4447 i40e_stat_str(&pf->hw, ret),
4448 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004449 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004450 }
4451
4452 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004453 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4454 NULL);
4455 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004456 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004457 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4458 i40e_stat_str(&pf->hw, ret),
4459 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004460 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004461 }
4462
4463 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4464 dev_info(&pf->pdev->dev,
4465 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4466 bw_config.tc_valid_bits,
4467 bw_ets_config.tc_valid_bits);
4468 /* Still continuing */
4469 }
4470
4471 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4472 vsi->bw_max_quanta = bw_config.max_bw;
4473 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4474 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4475 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4476 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4477 vsi->bw_ets_limit_credits[i] =
4478 le16_to_cpu(bw_ets_config.credits[i]);
4479 /* 3 bits out of 4 for each TC */
4480 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4481 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004482
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004483 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004484}
4485
4486/**
4487 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4488 * @vsi: the VSI being configured
4489 * @enabled_tc: TC bitmap
4490 * @bw_credits: BW shared credits per TC
4491 *
4492 * Returns 0 on success, negative value on failure
4493 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004494static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004495 u8 *bw_share)
4496{
4497 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004498 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004499 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004500
4501 bw_data.tc_valid_bits = enabled_tc;
4502 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4503 bw_data.tc_bw_credits[i] = bw_share[i];
4504
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004505 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4506 NULL);
4507 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004508 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004509 "AQ command Config VSI BW allocation per TC failed = %d\n",
4510 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004511 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004512 }
4513
4514 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4515 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4516
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004517 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004518}
4519
4520/**
4521 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4522 * @vsi: the VSI being configured
4523 * @enabled_tc: TC map to be enabled
4524 *
4525 **/
4526static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4527{
4528 struct net_device *netdev = vsi->netdev;
4529 struct i40e_pf *pf = vsi->back;
4530 struct i40e_hw *hw = &pf->hw;
4531 u8 netdev_tc = 0;
4532 int i;
4533 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4534
4535 if (!netdev)
4536 return;
4537
4538 if (!enabled_tc) {
4539 netdev_reset_tc(netdev);
4540 return;
4541 }
4542
4543 /* Set up actual enabled TCs on the VSI */
4544 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4545 return;
4546
4547 /* set per TC queues for the VSI */
4548 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4549 /* Only set TC queues for enabled tcs
4550 *
4551 * e.g. For a VSI that has TC0 and TC3 enabled the
4552 * enabled_tc bitmap would be 0x00001001; the driver
4553 * will set the numtc for netdev as 2 that will be
4554 * referenced by the netdev layer as TC 0 and 1.
4555 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004556 if (vsi->tc_config.enabled_tc & BIT_ULL(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004557 netdev_set_tc_queue(netdev,
4558 vsi->tc_config.tc_info[i].netdev_tc,
4559 vsi->tc_config.tc_info[i].qcount,
4560 vsi->tc_config.tc_info[i].qoffset);
4561 }
4562
4563 /* Assign UP2TC map for the VSI */
4564 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4565 /* Get the actual TC# for the UP */
4566 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4567 /* Get the mapped netdev TC# for the UP */
4568 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4569 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4570 }
4571}
4572
4573/**
4574 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4575 * @vsi: the VSI being configured
4576 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4577 **/
4578static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4579 struct i40e_vsi_context *ctxt)
4580{
4581 /* copy just the sections touched not the entire info
4582 * since not all sections are valid as returned by
4583 * update vsi params
4584 */
4585 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4586 memcpy(&vsi->info.queue_mapping,
4587 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4588 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4589 sizeof(vsi->info.tc_mapping));
4590}
4591
4592/**
4593 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4594 * @vsi: VSI to be configured
4595 * @enabled_tc: TC bitmap
4596 *
4597 * This configures a particular VSI for TCs that are mapped to the
4598 * given TC bitmap. It uses default bandwidth share for TCs across
4599 * VSIs to configure TC for a particular VSI.
4600 *
4601 * NOTE:
4602 * It is expected that the VSI queues have been quisced before calling
4603 * this function.
4604 **/
4605static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4606{
4607 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4608 struct i40e_vsi_context ctxt;
4609 int ret = 0;
4610 int i;
4611
4612 /* Check if enabled_tc is same as existing or new TCs */
4613 if (vsi->tc_config.enabled_tc == enabled_tc)
4614 return ret;
4615
4616 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4617 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004618 if (enabled_tc & BIT_ULL(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004619 bw_share[i] = 1;
4620 }
4621
4622 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4623 if (ret) {
4624 dev_info(&vsi->back->pdev->dev,
4625 "Failed configuring TC map %d for VSI %d\n",
4626 enabled_tc, vsi->seid);
4627 goto out;
4628 }
4629
4630 /* Update Queue Pairs Mapping for currently enabled UPs */
4631 ctxt.seid = vsi->seid;
4632 ctxt.pf_num = vsi->back->hw.pf_id;
4633 ctxt.vf_num = 0;
4634 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004635 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004636 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4637
4638 /* Update the VSI after updating the VSI queue-mapping information */
4639 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4640 if (ret) {
4641 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004642 "Update vsi tc config failed, err %s aq_err %s\n",
4643 i40e_stat_str(&vsi->back->hw, ret),
4644 i40e_aq_str(&vsi->back->hw,
4645 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004646 goto out;
4647 }
4648 /* update the local VSI info with updated queue map */
4649 i40e_vsi_update_queue_map(vsi, &ctxt);
4650 vsi->info.valid_sections = 0;
4651
4652 /* Update current VSI BW information */
4653 ret = i40e_vsi_get_bw_info(vsi);
4654 if (ret) {
4655 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004656 "Failed updating vsi bw info, err %s aq_err %s\n",
4657 i40e_stat_str(&vsi->back->hw, ret),
4658 i40e_aq_str(&vsi->back->hw,
4659 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004660 goto out;
4661 }
4662
4663 /* Update the netdev TC setup */
4664 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4665out:
4666 return ret;
4667}
4668
4669/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004670 * i40e_veb_config_tc - Configure TCs for given VEB
4671 * @veb: given VEB
4672 * @enabled_tc: TC bitmap
4673 *
4674 * Configures given TC bitmap for VEB (switching) element
4675 **/
4676int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4677{
4678 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4679 struct i40e_pf *pf = veb->pf;
4680 int ret = 0;
4681 int i;
4682
4683 /* No TCs or already enabled TCs just return */
4684 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4685 return ret;
4686
4687 bw_data.tc_valid_bits = enabled_tc;
4688 /* bw_data.absolute_credits is not set (relative) */
4689
4690 /* Enable ETS TCs with equal BW Share for now */
4691 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004692 if (enabled_tc & BIT_ULL(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004693 bw_data.tc_bw_share_credits[i] = 1;
4694 }
4695
4696 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4697 &bw_data, NULL);
4698 if (ret) {
4699 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004700 "VEB bw config failed, err %s aq_err %s\n",
4701 i40e_stat_str(&pf->hw, ret),
4702 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004703 goto out;
4704 }
4705
4706 /* Update the BW information */
4707 ret = i40e_veb_get_bw_info(veb);
4708 if (ret) {
4709 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004710 "Failed getting veb bw config, err %s aq_err %s\n",
4711 i40e_stat_str(&pf->hw, ret),
4712 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004713 }
4714
4715out:
4716 return ret;
4717}
4718
4719#ifdef CONFIG_I40E_DCB
4720/**
4721 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4722 * @pf: PF struct
4723 *
4724 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4725 * the caller would've quiesce all the VSIs before calling
4726 * this function
4727 **/
4728static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4729{
4730 u8 tc_map = 0;
4731 int ret;
4732 u8 v;
4733
4734 /* Enable the TCs available on PF to all VEBs */
4735 tc_map = i40e_pf_get_tc_map(pf);
4736 for (v = 0; v < I40E_MAX_VEB; v++) {
4737 if (!pf->veb[v])
4738 continue;
4739 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4740 if (ret) {
4741 dev_info(&pf->pdev->dev,
4742 "Failed configuring TC for VEB seid=%d\n",
4743 pf->veb[v]->seid);
4744 /* Will try to configure as many components */
4745 }
4746 }
4747
4748 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004749 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004750 if (!pf->vsi[v])
4751 continue;
4752
4753 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07004754#ifdef I40E_FCOE
4755 * - For FCoE VSI only enable the TC configured
4756 * as per the APP TLV
4757#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004758 * - For all others keep them at TC0 for now
4759 */
4760 if (v == pf->lan_vsi)
4761 tc_map = i40e_pf_get_tc_map(pf);
4762 else
4763 tc_map = i40e_pf_get_default_tc(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07004764#ifdef I40E_FCOE
4765 if (pf->vsi[v]->type == I40E_VSI_FCOE)
4766 tc_map = i40e_get_fcoe_tc_map(pf);
4767#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004768
4769 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4770 if (ret) {
4771 dev_info(&pf->pdev->dev,
4772 "Failed configuring TC for VSI seid=%d\n",
4773 pf->vsi[v]->seid);
4774 /* Will try to configure as many components */
4775 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004776 /* Re-configure VSI vectors based on updated TC map */
4777 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004778 if (pf->vsi[v]->netdev)
4779 i40e_dcbnl_set_all(pf->vsi[v]);
4780 }
4781 }
4782}
4783
4784/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00004785 * i40e_resume_port_tx - Resume port Tx
4786 * @pf: PF struct
4787 *
4788 * Resume a port's Tx and issue a PF reset in case of failure to
4789 * resume.
4790 **/
4791static int i40e_resume_port_tx(struct i40e_pf *pf)
4792{
4793 struct i40e_hw *hw = &pf->hw;
4794 int ret;
4795
4796 ret = i40e_aq_resume_port_tx(hw, NULL);
4797 if (ret) {
4798 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004799 "Resume Port Tx failed, err %s aq_err %s\n",
4800 i40e_stat_str(&pf->hw, ret),
4801 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00004802 /* Schedule PF reset to recover */
4803 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4804 i40e_service_event_schedule(pf);
4805 }
4806
4807 return ret;
4808}
4809
4810/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004811 * i40e_init_pf_dcb - Initialize DCB configuration
4812 * @pf: PF being configured
4813 *
4814 * Query the current DCB configuration and cache it
4815 * in the hardware structure
4816 **/
4817static int i40e_init_pf_dcb(struct i40e_pf *pf)
4818{
4819 struct i40e_hw *hw = &pf->hw;
4820 int err = 0;
4821
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00004822 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
4823 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
4824 (pf->hw.aq.fw_maj_ver < 4))
4825 goto out;
4826
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004827 /* Get the initial DCB configuration */
4828 err = i40e_init_dcb(hw);
4829 if (!err) {
4830 /* Device/Function is not DCBX capable */
4831 if ((!hw->func_caps.dcb) ||
4832 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4833 dev_info(&pf->pdev->dev,
4834 "DCBX offload is not supported or is disabled for this PF.\n");
4835
4836 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4837 goto out;
4838
4839 } else {
4840 /* When status is not DISABLED then DCBX in FW */
4841 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4842 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004843
4844 pf->flags |= I40E_FLAG_DCB_CAPABLE;
4845 /* Enable DCB tagging only when more than one TC */
4846 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
4847 pf->flags |= I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00004848 dev_dbg(&pf->pdev->dev,
4849 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004850 }
Neerav Parikh014269f2014-04-01 07:11:48 +00004851 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00004852 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004853 "Query for DCB configuration failed, err %s aq_err %s\n",
4854 i40e_stat_str(&pf->hw, err),
4855 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004856 }
4857
4858out:
4859 return err;
4860}
4861#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004862#define SPEED_SIZE 14
4863#define FC_SIZE 8
4864/**
4865 * i40e_print_link_message - print link up or down
4866 * @vsi: the VSI for which link needs a message
4867 */
4868static void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
4869{
4870 char speed[SPEED_SIZE] = "Unknown";
4871 char fc[FC_SIZE] = "RX/TX";
4872
4873 if (!isup) {
4874 netdev_info(vsi->netdev, "NIC Link is Down\n");
4875 return;
4876 }
4877
Greg Rose148c2d82014-12-11 07:06:27 +00004878 /* Warn user if link speed on NPAR enabled partition is not at
4879 * least 10GB
4880 */
4881 if (vsi->back->hw.func_caps.npar_enable &&
4882 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
4883 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
4884 netdev_warn(vsi->netdev,
4885 "The partition detected link speed that is less than 10Gbps\n");
4886
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004887 switch (vsi->back->hw.phy.link_info.link_speed) {
4888 case I40E_LINK_SPEED_40GB:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004889 strlcpy(speed, "40 Gbps", SPEED_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004890 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07004891 case I40E_LINK_SPEED_20GB:
4892 strncpy(speed, "20 Gbps", SPEED_SIZE);
4893 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004894 case I40E_LINK_SPEED_10GB:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004895 strlcpy(speed, "10 Gbps", SPEED_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004896 break;
4897 case I40E_LINK_SPEED_1GB:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004898 strlcpy(speed, "1000 Mbps", SPEED_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004899 break;
Mitch Williams5960d332014-09-13 07:40:47 +00004900 case I40E_LINK_SPEED_100MB:
4901 strncpy(speed, "100 Mbps", SPEED_SIZE);
4902 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004903 default:
4904 break;
4905 }
4906
4907 switch (vsi->back->hw.fc.current_mode) {
4908 case I40E_FC_FULL:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004909 strlcpy(fc, "RX/TX", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004910 break;
4911 case I40E_FC_TX_PAUSE:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004912 strlcpy(fc, "TX", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004913 break;
4914 case I40E_FC_RX_PAUSE:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004915 strlcpy(fc, "RX", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004916 break;
4917 default:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004918 strlcpy(fc, "None", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004919 break;
4920 }
4921
4922 netdev_info(vsi->netdev, "NIC Link is Up %s Full Duplex, Flow Control: %s\n",
4923 speed, fc);
4924}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004925
4926/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004927 * i40e_up_complete - Finish the last steps of bringing up a connection
4928 * @vsi: the VSI being configured
4929 **/
4930static int i40e_up_complete(struct i40e_vsi *vsi)
4931{
4932 struct i40e_pf *pf = vsi->back;
4933 int err;
4934
4935 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4936 i40e_vsi_configure_msix(vsi);
4937 else
4938 i40e_configure_msi_and_legacy(vsi);
4939
4940 /* start rings */
4941 err = i40e_vsi_control_rings(vsi, true);
4942 if (err)
4943 return err;
4944
4945 clear_bit(__I40E_DOWN, &vsi->state);
4946 i40e_napi_enable_all(vsi);
4947 i40e_vsi_enable_irq(vsi);
4948
4949 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4950 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004951 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004952 netif_tx_start_all_queues(vsi->netdev);
4953 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004954 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004955 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00004956 /* need to check for qualified module here*/
4957 if ((pf->hw.phy.link_info.link_info &
4958 I40E_AQ_MEDIA_AVAILABLE) &&
4959 (!(pf->hw.phy.link_info.an_info &
4960 I40E_AQ_QUALIFIED_MODULE)))
4961 netdev_err(vsi->netdev,
4962 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004963 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004964
4965 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00004966 if (vsi->type == I40E_VSI_FDIR) {
4967 /* reset fd counters */
4968 pf->fd_add_err = pf->fd_atr_cnt = 0;
4969 if (pf->fd_tcp_rule > 0) {
4970 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04004971 if (I40E_DEBUG_FD & pf->hw.debug_mask)
4972 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00004973 pf->fd_tcp_rule = 0;
4974 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004975 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00004976 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004977 i40e_service_event_schedule(pf);
4978
4979 return 0;
4980}
4981
4982/**
4983 * i40e_vsi_reinit_locked - Reset the VSI
4984 * @vsi: the VSI being configured
4985 *
4986 * Rebuild the ring structs after some configuration
4987 * has changed, e.g. MTU size.
4988 **/
4989static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4990{
4991 struct i40e_pf *pf = vsi->back;
4992
4993 WARN_ON(in_interrupt());
4994 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4995 usleep_range(1000, 2000);
4996 i40e_down(vsi);
4997
4998 /* Give a VF some time to respond to the reset. The
4999 * two second wait is based upon the watchdog cycle in
5000 * the VF driver.
5001 */
5002 if (vsi->type == I40E_VSI_SRIOV)
5003 msleep(2000);
5004 i40e_up(vsi);
5005 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5006}
5007
5008/**
5009 * i40e_up - Bring the connection back up after being down
5010 * @vsi: the VSI being configured
5011 **/
5012int i40e_up(struct i40e_vsi *vsi)
5013{
5014 int err;
5015
5016 err = i40e_vsi_configure(vsi);
5017 if (!err)
5018 err = i40e_up_complete(vsi);
5019
5020 return err;
5021}
5022
5023/**
5024 * i40e_down - Shutdown the connection processing
5025 * @vsi: the VSI being stopped
5026 **/
5027void i40e_down(struct i40e_vsi *vsi)
5028{
5029 int i;
5030
5031 /* It is assumed that the caller of this function
5032 * sets the vsi->state __I40E_DOWN bit.
5033 */
5034 if (vsi->netdev) {
5035 netif_carrier_off(vsi->netdev);
5036 netif_tx_disable(vsi->netdev);
5037 }
5038 i40e_vsi_disable_irq(vsi);
5039 i40e_vsi_control_rings(vsi, false);
5040 i40e_napi_disable_all(vsi);
5041
5042 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005043 i40e_clean_tx_ring(vsi->tx_rings[i]);
5044 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005045 }
5046}
5047
5048/**
5049 * i40e_setup_tc - configure multiple traffic classes
5050 * @netdev: net device to configure
5051 * @tc: number of traffic classes to enable
5052 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005053#ifdef I40E_FCOE
5054int i40e_setup_tc(struct net_device *netdev, u8 tc)
5055#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005056static int i40e_setup_tc(struct net_device *netdev, u8 tc)
Vasu Dev38e00432014-08-01 13:27:03 -07005057#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005058{
5059 struct i40e_netdev_priv *np = netdev_priv(netdev);
5060 struct i40e_vsi *vsi = np->vsi;
5061 struct i40e_pf *pf = vsi->back;
5062 u8 enabled_tc = 0;
5063 int ret = -EINVAL;
5064 int i;
5065
5066 /* Check if DCB enabled to continue */
5067 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5068 netdev_info(netdev, "DCB is not enabled for adapter\n");
5069 goto exit;
5070 }
5071
5072 /* Check if MFP enabled */
5073 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5074 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5075 goto exit;
5076 }
5077
5078 /* Check whether tc count is within enabled limit */
5079 if (tc > i40e_pf_get_num_tc(pf)) {
5080 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5081 goto exit;
5082 }
5083
5084 /* Generate TC map for number of tc requested */
5085 for (i = 0; i < tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005086 enabled_tc |= BIT_ULL(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005087
5088 /* Requesting same TC configuration as already enabled */
5089 if (enabled_tc == vsi->tc_config.enabled_tc)
5090 return 0;
5091
5092 /* Quiesce VSI queues */
5093 i40e_quiesce_vsi(vsi);
5094
5095 /* Configure VSI for enabled TCs */
5096 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5097 if (ret) {
5098 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5099 vsi->seid);
5100 goto exit;
5101 }
5102
5103 /* Unquiesce VSI */
5104 i40e_unquiesce_vsi(vsi);
5105
5106exit:
5107 return ret;
5108}
5109
5110/**
5111 * i40e_open - Called when a network interface is made active
5112 * @netdev: network interface device structure
5113 *
5114 * The open entry point is called when a network interface is made
5115 * active by the system (IFF_UP). At this point all resources needed
5116 * for transmit and receive operations are allocated, the interrupt
5117 * handler is registered with the OS, the netdev watchdog subtask is
5118 * enabled, and the stack is notified that the interface is ready.
5119 *
5120 * Returns 0 on success, negative value on failure
5121 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005122int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005123{
5124 struct i40e_netdev_priv *np = netdev_priv(netdev);
5125 struct i40e_vsi *vsi = np->vsi;
5126 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005127 int err;
5128
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005129 /* disallow open during test or if eeprom is broken */
5130 if (test_bit(__I40E_TESTING, &pf->state) ||
5131 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005132 return -EBUSY;
5133
5134 netif_carrier_off(netdev);
5135
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005136 err = i40e_vsi_open(vsi);
5137 if (err)
5138 return err;
5139
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005140 /* configure global TSO hardware offload settings */
5141 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5142 TCP_FLAG_FIN) >> 16);
5143 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5144 TCP_FLAG_FIN |
5145 TCP_FLAG_CWR) >> 16);
5146 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5147
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005148#ifdef CONFIG_I40E_VXLAN
5149 vxlan_get_rx_port(netdev);
5150#endif
5151
5152 return 0;
5153}
5154
5155/**
5156 * i40e_vsi_open -
5157 * @vsi: the VSI to open
5158 *
5159 * Finish initialization of the VSI.
5160 *
5161 * Returns 0 on success, negative value on failure
5162 **/
5163int i40e_vsi_open(struct i40e_vsi *vsi)
5164{
5165 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005166 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005167 int err;
5168
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005169 /* allocate descriptors */
5170 err = i40e_vsi_setup_tx_resources(vsi);
5171 if (err)
5172 goto err_setup_tx;
5173 err = i40e_vsi_setup_rx_resources(vsi);
5174 if (err)
5175 goto err_setup_rx;
5176
5177 err = i40e_vsi_configure(vsi);
5178 if (err)
5179 goto err_setup_rx;
5180
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005181 if (vsi->netdev) {
5182 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5183 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5184 err = i40e_vsi_request_irq(vsi, int_name);
5185 if (err)
5186 goto err_setup_rx;
5187
5188 /* Notify the stack of the actual queue counts. */
5189 err = netif_set_real_num_tx_queues(vsi->netdev,
5190 vsi->num_queue_pairs);
5191 if (err)
5192 goto err_set_queues;
5193
5194 err = netif_set_real_num_rx_queues(vsi->netdev,
5195 vsi->num_queue_pairs);
5196 if (err)
5197 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005198
5199 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005200 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005201 dev_driver_string(&pf->pdev->dev),
5202 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005203 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005204
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005205 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005206 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005207 goto err_setup_rx;
5208 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005209
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005210 err = i40e_up_complete(vsi);
5211 if (err)
5212 goto err_up_complete;
5213
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005214 return 0;
5215
5216err_up_complete:
5217 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005218err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005219 i40e_vsi_free_irq(vsi);
5220err_setup_rx:
5221 i40e_vsi_free_rx_resources(vsi);
5222err_setup_tx:
5223 i40e_vsi_free_tx_resources(vsi);
5224 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005225 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005226
5227 return err;
5228}
5229
5230/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005231 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005232 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005233 *
5234 * This function destroys the hlist where all the Flow Director
5235 * filters were saved.
5236 **/
5237static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5238{
5239 struct i40e_fdir_filter *filter;
5240 struct hlist_node *node2;
5241
5242 hlist_for_each_entry_safe(filter, node2,
5243 &pf->fdir_filter_list, fdir_node) {
5244 hlist_del(&filter->fdir_node);
5245 kfree(filter);
5246 }
5247 pf->fdir_pf_active_filters = 0;
5248}
5249
5250/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005251 * i40e_close - Disables a network interface
5252 * @netdev: network interface device structure
5253 *
5254 * The close entry point is called when an interface is de-activated
5255 * by the OS. The hardware is still under the driver's control, but
5256 * this netdev interface is disabled.
5257 *
5258 * Returns 0, this is not allowed to fail
5259 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005260#ifdef I40E_FCOE
5261int i40e_close(struct net_device *netdev)
5262#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005263static int i40e_close(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07005264#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005265{
5266 struct i40e_netdev_priv *np = netdev_priv(netdev);
5267 struct i40e_vsi *vsi = np->vsi;
5268
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005269 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005270
5271 return 0;
5272}
5273
5274/**
5275 * i40e_do_reset - Start a PF or Core Reset sequence
5276 * @pf: board private structure
5277 * @reset_flags: which reset is requested
5278 *
5279 * The essential difference in resets is that the PF Reset
5280 * doesn't clear the packet buffers, doesn't reset the PE
5281 * firmware, and doesn't bother the other PFs on the chip.
5282 **/
5283void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5284{
5285 u32 val;
5286
5287 WARN_ON(in_interrupt());
5288
Mitch Williams263fc482014-04-23 04:50:11 +00005289 if (i40e_check_asq_alive(&pf->hw))
5290 i40e_vc_notify_reset(pf);
5291
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005292 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005293 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005294
5295 /* Request a Global Reset
5296 *
5297 * This will start the chip's countdown to the actual full
5298 * chip reset event, and a warning interrupt to be sent
5299 * to all PFs, including the requestor. Our handler
5300 * for the warning interrupt will deal with the shutdown
5301 * and recovery of the switch setup.
5302 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005303 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005304 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5305 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5306 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5307
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005308 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005309
5310 /* Request a Core Reset
5311 *
5312 * Same as Global Reset, except does *not* include the MAC/PHY
5313 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005314 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005315 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5316 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5317 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5318 i40e_flush(&pf->hw);
5319
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005320 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005321
5322 /* Request a PF Reset
5323 *
5324 * Resets only the PF-specific registers
5325 *
5326 * This goes directly to the tear-down and rebuild of
5327 * the switch, since we need to do all the recovery as
5328 * for the Core Reset.
5329 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005330 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005331 i40e_handle_reset_warning(pf);
5332
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005333 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005334 int v;
5335
5336 /* Find the VSI(s) that requested a re-init */
5337 dev_info(&pf->pdev->dev,
5338 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005339 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005340 struct i40e_vsi *vsi = pf->vsi[v];
5341 if (vsi != NULL &&
5342 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5343 i40e_vsi_reinit_locked(pf->vsi[v]);
5344 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5345 }
5346 }
5347
5348 /* no further action needed, so return now */
5349 return;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005350 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005351 int v;
5352
5353 /* Find the VSI(s) that needs to be brought down */
5354 dev_info(&pf->pdev->dev, "VSI down requested\n");
5355 for (v = 0; v < pf->num_alloc_vsi; v++) {
5356 struct i40e_vsi *vsi = pf->vsi[v];
5357 if (vsi != NULL &&
5358 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5359 set_bit(__I40E_DOWN, &vsi->state);
5360 i40e_down(vsi);
5361 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5362 }
5363 }
5364
5365 /* no further action needed, so return now */
5366 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005367 } else {
5368 dev_info(&pf->pdev->dev,
5369 "bad reset request 0x%08x\n", reset_flags);
5370 return;
5371 }
5372}
5373
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005374#ifdef CONFIG_I40E_DCB
5375/**
5376 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5377 * @pf: board private structure
5378 * @old_cfg: current DCB config
5379 * @new_cfg: new DCB config
5380 **/
5381bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5382 struct i40e_dcbx_config *old_cfg,
5383 struct i40e_dcbx_config *new_cfg)
5384{
5385 bool need_reconfig = false;
5386
5387 /* Check if ETS configuration has changed */
5388 if (memcmp(&new_cfg->etscfg,
5389 &old_cfg->etscfg,
5390 sizeof(new_cfg->etscfg))) {
5391 /* If Priority Table has changed reconfig is needed */
5392 if (memcmp(&new_cfg->etscfg.prioritytable,
5393 &old_cfg->etscfg.prioritytable,
5394 sizeof(new_cfg->etscfg.prioritytable))) {
5395 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005396 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005397 }
5398
5399 if (memcmp(&new_cfg->etscfg.tcbwtable,
5400 &old_cfg->etscfg.tcbwtable,
5401 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005402 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005403
5404 if (memcmp(&new_cfg->etscfg.tsatable,
5405 &old_cfg->etscfg.tsatable,
5406 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005407 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005408 }
5409
5410 /* Check if PFC configuration has changed */
5411 if (memcmp(&new_cfg->pfc,
5412 &old_cfg->pfc,
5413 sizeof(new_cfg->pfc))) {
5414 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005415 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005416 }
5417
5418 /* Check if APP Table has changed */
5419 if (memcmp(&new_cfg->app,
5420 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005421 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005422 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005423 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005424 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005425
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005426 dev_dbg(&pf->pdev->dev, "%s: need_reconfig=%d\n", __func__,
5427 need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005428 return need_reconfig;
5429}
5430
5431/**
5432 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5433 * @pf: board private structure
5434 * @e: event info posted on ARQ
5435 **/
5436static int i40e_handle_lldp_event(struct i40e_pf *pf,
5437 struct i40e_arq_event_info *e)
5438{
5439 struct i40e_aqc_lldp_get_mib *mib =
5440 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5441 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005442 struct i40e_dcbx_config tmp_dcbx_cfg;
5443 bool need_reconfig = false;
5444 int ret = 0;
5445 u8 type;
5446
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005447 /* Not DCB capable or capability disabled */
5448 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
5449 return ret;
5450
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005451 /* Ignore if event is not for Nearest Bridge */
5452 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5453 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005454 dev_dbg(&pf->pdev->dev,
5455 "%s: LLDP event mib bridge type 0x%x\n", __func__, type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005456 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5457 return ret;
5458
5459 /* Check MIB Type and return if event for Remote MIB update */
5460 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005461 dev_dbg(&pf->pdev->dev,
5462 "%s: LLDP event mib type %s\n", __func__,
5463 type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005464 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5465 /* Update the remote cached instance and return */
5466 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5467 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5468 &hw->remote_dcbx_config);
5469 goto exit;
5470 }
5471
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005472 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005473 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005474
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005475 /* Reset the old DCBx configuration data */
5476 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005477 /* Get updated DCBX data from firmware */
5478 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005479 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005480 dev_info(&pf->pdev->dev,
5481 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5482 i40e_stat_str(&pf->hw, ret),
5483 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005484 goto exit;
5485 }
5486
5487 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005488 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5489 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005490 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005491 goto exit;
5492 }
5493
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005494 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5495 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005496
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005497 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005498
5499 if (!need_reconfig)
5500 goto exit;
5501
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005502 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005503 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005504 pf->flags |= I40E_FLAG_DCB_ENABLED;
5505 else
5506 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5507
Neerav Parikh69129dc2014-11-12 00:18:46 +00005508 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005509 /* Reconfiguration needed quiesce all VSIs */
5510 i40e_pf_quiesce_all_vsi(pf);
5511
5512 /* Changes in configuration update VEB/VSI */
5513 i40e_dcb_reconfigure(pf);
5514
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005515 ret = i40e_resume_port_tx(pf);
5516
Neerav Parikh69129dc2014-11-12 00:18:46 +00005517 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005518 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005519 if (ret)
5520 goto exit;
5521
5522 /* Wait for the PF's Tx queues to be disabled */
5523 ret = i40e_pf_wait_txq_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005524 if (ret) {
5525 /* Schedule PF reset to recover */
5526 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5527 i40e_service_event_schedule(pf);
5528 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005529 i40e_pf_unquiesce_all_vsi(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005530 }
5531
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005532exit:
5533 return ret;
5534}
5535#endif /* CONFIG_I40E_DCB */
5536
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005537/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005538 * i40e_do_reset_safe - Protected reset path for userland calls.
5539 * @pf: board private structure
5540 * @reset_flags: which reset is requested
5541 *
5542 **/
5543void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5544{
5545 rtnl_lock();
5546 i40e_do_reset(pf, reset_flags);
5547 rtnl_unlock();
5548}
5549
5550/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005551 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5552 * @pf: board private structure
5553 * @e: event info posted on ARQ
5554 *
5555 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5556 * and VF queues
5557 **/
5558static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5559 struct i40e_arq_event_info *e)
5560{
5561 struct i40e_aqc_lan_overflow *data =
5562 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5563 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5564 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5565 struct i40e_hw *hw = &pf->hw;
5566 struct i40e_vf *vf;
5567 u16 vf_id;
5568
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005569 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5570 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005571
5572 /* Queue belongs to VF, find the VF and issue VF reset */
5573 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5574 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5575 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5576 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5577 vf_id -= hw->func_caps.vf_base_id;
5578 vf = &pf->vf[vf_id];
5579 i40e_vc_notify_vf_reset(vf);
5580 /* Allow VF to process pending reset notification */
5581 msleep(20);
5582 i40e_reset_vf(vf, false);
5583 }
5584}
5585
5586/**
5587 * i40e_service_event_complete - Finish up the service event
5588 * @pf: board private structure
5589 **/
5590static void i40e_service_event_complete(struct i40e_pf *pf)
5591{
5592 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
5593
5594 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005595 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005596 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5597}
5598
5599/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005600 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5601 * @pf: board private structure
5602 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005603u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005604{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005605 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005606
5607 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5608 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5609 return fcnt_prog;
5610}
5611
5612/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005613 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005614 * @pf: board private structure
5615 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005616u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005617{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005618 u32 val, fcnt_prog;
5619
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005620 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5621 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5622 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5623 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5624 return fcnt_prog;
5625}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005626
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005627/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005628 * i40e_get_global_fd_count - Get total FD filters programmed on device
5629 * @pf: board private structure
5630 **/
5631u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5632{
5633 u32 val, fcnt_prog;
5634
5635 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5636 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5637 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5638 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5639 return fcnt_prog;
5640}
5641
5642/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005643 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5644 * @pf: board private structure
5645 **/
5646void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5647{
5648 u32 fcnt_prog, fcnt_avail;
5649
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005650 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5651 return;
5652
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005653 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5654 * to re-enable
5655 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005656 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005657 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005658 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5659 (pf->fd_add_err == 0) ||
5660 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005661 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5662 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5663 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005664 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5665 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 +00005666 }
5667 }
5668 /* Wait for some more space to be available to turn on ATR */
5669 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5670 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
5671 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
5672 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005673 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5674 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 +00005675 }
5676 }
5677}
5678
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005679#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005680#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005681/**
5682 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
5683 * @pf: board private structure
5684 **/
5685static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
5686{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005687 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005688 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005689 bool disable_atr = false;
5690 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005691 int reg;
5692
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005693 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5694 return;
5695
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005696 if (time_after(jiffies, pf->fd_flush_timestamp +
5697 (I40E_MIN_FD_FLUSH_INTERVAL * HZ))) {
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005698 /* If the flush is happening too quick and we have mostly
5699 * SB rules we should not re-enable ATR for some time.
5700 */
5701 min_flush_time = pf->fd_flush_timestamp
5702 + (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
5703 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
5704
5705 if (!(time_after(jiffies, min_flush_time)) &&
5706 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005707 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5708 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005709 disable_atr = true;
5710 }
5711
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005712 pf->fd_flush_timestamp = jiffies;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005713 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
5714 /* flush all filters */
5715 wr32(&pf->hw, I40E_PFQF_CTL_1,
5716 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
5717 i40e_flush(&pf->hw);
Anjali Singhai Jain60793f4a2014-07-09 07:46:23 +00005718 pf->fd_flush_cnt++;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005719 pf->fd_add_err = 0;
5720 do {
5721 /* Check FD flush status every 5-6msec */
5722 usleep_range(5000, 6000);
5723 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
5724 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
5725 break;
5726 } while (flush_wait_retry--);
5727 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
5728 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
5729 } else {
5730 /* replay sideband filters */
5731 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005732 if (!disable_atr)
5733 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005734 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005735 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5736 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005737 }
5738 }
5739}
5740
5741/**
5742 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
5743 * @pf: board private structure
5744 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005745u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005746{
5747 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
5748}
5749
5750/* We can see up to 256 filter programming desc in transit if the filters are
5751 * being applied really fast; before we see the first
5752 * filter miss error on Rx queue 0. Accumulating enough error messages before
5753 * reacting will make sure we don't cause flush too often.
5754 */
5755#define I40E_MAX_FD_PROGRAM_ERROR 256
5756
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005757/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005758 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
5759 * @pf: board private structure
5760 **/
5761static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
5762{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005763
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005764 /* if interface is down do nothing */
5765 if (test_bit(__I40E_DOWN, &pf->state))
5766 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005767
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005768 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5769 return;
5770
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005771 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005772 i40e_fdir_flush_and_replay(pf);
5773
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005774 i40e_fdir_check_and_reenable(pf);
5775
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005776}
5777
5778/**
5779 * i40e_vsi_link_event - notify VSI of a link event
5780 * @vsi: vsi to be notified
5781 * @link_up: link up or down
5782 **/
5783static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
5784{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00005785 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005786 return;
5787
5788 switch (vsi->type) {
5789 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07005790#ifdef I40E_FCOE
5791 case I40E_VSI_FCOE:
5792#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005793 if (!vsi->netdev || !vsi->netdev_registered)
5794 break;
5795
5796 if (link_up) {
5797 netif_carrier_on(vsi->netdev);
5798 netif_tx_wake_all_queues(vsi->netdev);
5799 } else {
5800 netif_carrier_off(vsi->netdev);
5801 netif_tx_stop_all_queues(vsi->netdev);
5802 }
5803 break;
5804
5805 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005806 case I40E_VSI_VMDQ2:
5807 case I40E_VSI_CTRL:
5808 case I40E_VSI_MIRROR:
5809 default:
5810 /* there is no notification for other VSIs */
5811 break;
5812 }
5813}
5814
5815/**
5816 * i40e_veb_link_event - notify elements on the veb of a link event
5817 * @veb: veb to be notified
5818 * @link_up: link up or down
5819 **/
5820static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
5821{
5822 struct i40e_pf *pf;
5823 int i;
5824
5825 if (!veb || !veb->pf)
5826 return;
5827 pf = veb->pf;
5828
5829 /* depth first... */
5830 for (i = 0; i < I40E_MAX_VEB; i++)
5831 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
5832 i40e_veb_link_event(pf->veb[i], link_up);
5833
5834 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00005835 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005836 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
5837 i40e_vsi_link_event(pf->vsi[i], link_up);
5838}
5839
5840/**
5841 * i40e_link_event - Update netif_carrier status
5842 * @pf: board private structure
5843 **/
5844static void i40e_link_event(struct i40e_pf *pf)
5845{
5846 bool new_link, old_link;
Mitch Williams320684c2014-10-17 03:14:43 +00005847 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00005848 u8 new_link_speed, old_link_speed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005849
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005850 /* set this to force the get_link_status call to refresh state */
5851 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005852
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005853 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
5854 new_link = i40e_get_link_status(&pf->hw);
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00005855 old_link_speed = pf->hw.phy.link_info_old.link_speed;
5856 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005857
5858 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00005859 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00005860 (test_bit(__I40E_DOWN, &vsi->state) ||
5861 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005862 return;
Mitch Williams320684c2014-10-17 03:14:43 +00005863
5864 if (!test_bit(__I40E_DOWN, &vsi->state))
5865 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005866
5867 /* Notify the base of the switch tree connected to
5868 * the link. Floating VEBs are not notified.
5869 */
5870 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
5871 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
5872 else
Mitch Williams320684c2014-10-17 03:14:43 +00005873 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005874
5875 if (pf->vf)
5876 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005877
5878 if (pf->flags & I40E_FLAG_PTP)
5879 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005880}
5881
5882/**
Shannon Nelson21536712014-10-25 10:35:25 +00005883 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005884 * @pf: board private structure
5885 **/
5886static void i40e_watchdog_subtask(struct i40e_pf *pf)
5887{
5888 int i;
5889
5890 /* if interface is down do nothing */
5891 if (test_bit(__I40E_DOWN, &pf->state) ||
5892 test_bit(__I40E_CONFIG_BUSY, &pf->state))
5893 return;
5894
Shannon Nelson21536712014-10-25 10:35:25 +00005895 /* make sure we don't do these things too often */
5896 if (time_before(jiffies, (pf->service_timer_previous +
5897 pf->service_timer_period)))
5898 return;
5899 pf->service_timer_previous = jiffies;
5900
Shannon Nelson21536712014-10-25 10:35:25 +00005901 i40e_link_event(pf);
5902
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005903 /* Update the stats for active netdevs so the network stack
5904 * can look at updated numbers whenever it cares to
5905 */
Mitch Williams505682c2014-05-20 08:01:37 +00005906 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005907 if (pf->vsi[i] && pf->vsi[i]->netdev)
5908 i40e_update_stats(pf->vsi[i]);
5909
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04005910 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
5911 /* Update the stats for the active switching components */
5912 for (i = 0; i < I40E_MAX_VEB; i++)
5913 if (pf->veb[i])
5914 i40e_update_veb_stats(pf->veb[i]);
5915 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005916
5917 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005918}
5919
5920/**
5921 * i40e_reset_subtask - Set up for resetting the device and driver
5922 * @pf: board private structure
5923 **/
5924static void i40e_reset_subtask(struct i40e_pf *pf)
5925{
5926 u32 reset_flags = 0;
5927
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005928 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005929 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005930 reset_flags |= BIT_ULL(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005931 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
5932 }
5933 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005934 reset_flags |= BIT_ULL(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005935 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5936 }
5937 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005938 reset_flags |= BIT_ULL(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005939 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
5940 }
5941 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005942 reset_flags |= BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005943 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
5944 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005945 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005946 reset_flags |= BIT_ULL(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005947 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
5948 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005949
5950 /* If there's a recovery already waiting, it takes
5951 * precedence before starting a new reset sequence.
5952 */
5953 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
5954 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005955 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005956 }
5957
5958 /* If we're already down or resetting, just bail */
5959 if (reset_flags &&
5960 !test_bit(__I40E_DOWN, &pf->state) &&
5961 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
5962 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005963
5964unlock:
5965 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005966}
5967
5968/**
5969 * i40e_handle_link_event - Handle link event
5970 * @pf: board private structure
5971 * @e: event info posted on ARQ
5972 **/
5973static void i40e_handle_link_event(struct i40e_pf *pf,
5974 struct i40e_arq_event_info *e)
5975{
5976 struct i40e_hw *hw = &pf->hw;
5977 struct i40e_aqc_get_link_status *status =
5978 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005979
5980 /* save off old link status information */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005981 hw->phy.link_info_old = hw->phy.link_info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005982
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005983 /* Do a new status request to re-enable LSE reporting
5984 * and load new status information into the hw struct
5985 * This completely ignores any state information
5986 * in the ARQ event info, instead choosing to always
5987 * issue the AQ update link status command.
5988 */
5989 i40e_link_event(pf);
5990
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005991 /* check for unqualified module, if link is down */
5992 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
5993 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
5994 (!(status->link_info & I40E_AQ_LINK_UP)))
5995 dev_err(&pf->pdev->dev,
5996 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005997}
5998
5999/**
6000 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6001 * @pf: board private structure
6002 **/
6003static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6004{
6005 struct i40e_arq_event_info event;
6006 struct i40e_hw *hw = &pf->hw;
6007 u16 pending, i = 0;
6008 i40e_status ret;
6009 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006010 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006011 u32 val;
6012
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006013 /* Do not run clean AQ when PF reset fails */
6014 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6015 return;
6016
Shannon Nelson86df2422014-05-20 08:01:35 +00006017 /* check for error indications */
6018 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6019 oldval = val;
6020 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
6021 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
6022 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6023 }
6024 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
6025 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
6026 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
6027 }
6028 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
6029 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
6030 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6031 }
6032 if (oldval != val)
6033 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6034
6035 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6036 oldval = val;
6037 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
6038 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
6039 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6040 }
6041 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
6042 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
6043 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6044 }
6045 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
6046 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
6047 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6048 }
6049 if (oldval != val)
6050 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6051
Mitch Williams1001dc32014-11-11 20:02:19 +00006052 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6053 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006054 if (!event.msg_buf)
6055 return;
6056
6057 do {
6058 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006059 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006060 break;
Mitch Williams56497972014-06-04 08:45:18 +00006061 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006062 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6063 break;
6064 }
6065
6066 opcode = le16_to_cpu(event.desc.opcode);
6067 switch (opcode) {
6068
6069 case i40e_aqc_opc_get_link_status:
6070 i40e_handle_link_event(pf, &event);
6071 break;
6072 case i40e_aqc_opc_send_msg_to_pf:
6073 ret = i40e_vc_process_vf_msg(pf,
6074 le16_to_cpu(event.desc.retval),
6075 le32_to_cpu(event.desc.cookie_high),
6076 le32_to_cpu(event.desc.cookie_low),
6077 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006078 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006079 break;
6080 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006081 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006082#ifdef CONFIG_I40E_DCB
6083 rtnl_lock();
6084 ret = i40e_handle_lldp_event(pf, &event);
6085 rtnl_unlock();
6086#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006087 break;
6088 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006089 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006090 i40e_handle_lan_overflow_event(pf, &event);
6091 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006092 case i40e_aqc_opc_send_msg_to_peer:
6093 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6094 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006095 case i40e_aqc_opc_nvm_erase:
6096 case i40e_aqc_opc_nvm_update:
6097 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "ARQ NVM operation completed\n");
6098 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006099 default:
6100 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00006101 "ARQ Error: Unknown event 0x%04x received\n",
6102 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006103 break;
6104 }
6105 } while (pending && (i++ < pf->adminq_work_limit));
6106
6107 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6108 /* re-enable Admin queue interrupt cause */
6109 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6110 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6111 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6112 i40e_flush(hw);
6113
6114 kfree(event.msg_buf);
6115}
6116
6117/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006118 * i40e_verify_eeprom - make sure eeprom is good to use
6119 * @pf: board private structure
6120 **/
6121static void i40e_verify_eeprom(struct i40e_pf *pf)
6122{
6123 int err;
6124
6125 err = i40e_diag_eeprom_test(&pf->hw);
6126 if (err) {
6127 /* retry in case of garbage read */
6128 err = i40e_diag_eeprom_test(&pf->hw);
6129 if (err) {
6130 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6131 err);
6132 set_bit(__I40E_BAD_EEPROM, &pf->state);
6133 }
6134 }
6135
6136 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6137 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6138 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6139 }
6140}
6141
6142/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006143 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006144 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006145 *
6146 * enable switch loop back or die - no point in a return value
6147 **/
6148static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6149{
6150 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6151 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006152 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006153
6154 ctxt.seid = pf->main_vsi_seid;
6155 ctxt.pf_num = pf->hw.pf_id;
6156 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006157 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6158 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006159 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006160 "couldn't get PF vsi config, err %s aq_err %s\n",
6161 i40e_stat_str(&pf->hw, ret),
6162 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006163 return;
6164 }
6165 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6166 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6167 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6168
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006169 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6170 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006171 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006172 "update vsi switch failed, err %s aq_err %s\n",
6173 i40e_stat_str(&pf->hw, ret),
6174 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006175 }
6176}
6177
6178/**
6179 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006180 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006181 *
6182 * disable switch loop back or die - no point in a return value
6183 **/
6184static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6185{
6186 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6187 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006188 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006189
6190 ctxt.seid = pf->main_vsi_seid;
6191 ctxt.pf_num = pf->hw.pf_id;
6192 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006193 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6194 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006195 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006196 "couldn't get PF vsi config, err %s aq_err %s\n",
6197 i40e_stat_str(&pf->hw, ret),
6198 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006199 return;
6200 }
6201 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6202 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6203 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6204
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006205 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6206 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006207 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006208 "update vsi switch failed, err %s aq_err %s\n",
6209 i40e_stat_str(&pf->hw, ret),
6210 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006211 }
6212}
6213
6214/**
Neerav Parikh51616012015-02-06 08:52:14 +00006215 * i40e_config_bridge_mode - Configure the HW bridge mode
6216 * @veb: pointer to the bridge instance
6217 *
6218 * Configure the loop back mode for the LAN VSI that is downlink to the
6219 * specified HW bridge instance. It is expected this function is called
6220 * when a new HW bridge is instantiated.
6221 **/
6222static void i40e_config_bridge_mode(struct i40e_veb *veb)
6223{
6224 struct i40e_pf *pf = veb->pf;
6225
6226 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6227 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
6228 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6229 i40e_disable_pf_switch_lb(pf);
6230 else
6231 i40e_enable_pf_switch_lb(pf);
6232}
6233
6234/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006235 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6236 * @veb: pointer to the VEB instance
6237 *
6238 * This is a recursive function that first builds the attached VSIs then
6239 * recurses in to build the next layer of VEB. We track the connections
6240 * through our own index numbers because the seid's from the HW could
6241 * change across the reset.
6242 **/
6243static int i40e_reconstitute_veb(struct i40e_veb *veb)
6244{
6245 struct i40e_vsi *ctl_vsi = NULL;
6246 struct i40e_pf *pf = veb->pf;
6247 int v, veb_idx;
6248 int ret;
6249
6250 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006251 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006252 if (pf->vsi[v] &&
6253 pf->vsi[v]->veb_idx == veb->idx &&
6254 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6255 ctl_vsi = pf->vsi[v];
6256 break;
6257 }
6258 }
6259 if (!ctl_vsi) {
6260 dev_info(&pf->pdev->dev,
6261 "missing owner VSI for veb_idx %d\n", veb->idx);
6262 ret = -ENOENT;
6263 goto end_reconstitute;
6264 }
6265 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6266 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6267 ret = i40e_add_vsi(ctl_vsi);
6268 if (ret) {
6269 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006270 "rebuild of veb_idx %d owner VSI failed: %d\n",
6271 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006272 goto end_reconstitute;
6273 }
6274 i40e_vsi_reset_stats(ctl_vsi);
6275
6276 /* create the VEB in the switch and move the VSI onto the VEB */
6277 ret = i40e_add_veb(veb, ctl_vsi);
6278 if (ret)
6279 goto end_reconstitute;
6280
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006281 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6282 veb->bridge_mode = BRIDGE_MODE_VEB;
6283 else
6284 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006285 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006286
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006287 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006288 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006289 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6290 continue;
6291
6292 if (pf->vsi[v]->veb_idx == veb->idx) {
6293 struct i40e_vsi *vsi = pf->vsi[v];
6294 vsi->uplink_seid = veb->seid;
6295 ret = i40e_add_vsi(vsi);
6296 if (ret) {
6297 dev_info(&pf->pdev->dev,
6298 "rebuild of vsi_idx %d failed: %d\n",
6299 v, ret);
6300 goto end_reconstitute;
6301 }
6302 i40e_vsi_reset_stats(vsi);
6303 }
6304 }
6305
6306 /* create any VEBs attached to this VEB - RECURSION */
6307 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6308 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6309 pf->veb[veb_idx]->uplink_seid = veb->seid;
6310 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6311 if (ret)
6312 break;
6313 }
6314 }
6315
6316end_reconstitute:
6317 return ret;
6318}
6319
6320/**
6321 * i40e_get_capabilities - get info about the HW
6322 * @pf: the PF struct
6323 **/
6324static int i40e_get_capabilities(struct i40e_pf *pf)
6325{
6326 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6327 u16 data_size;
6328 int buf_len;
6329 int err;
6330
6331 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6332 do {
6333 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6334 if (!cap_buf)
6335 return -ENOMEM;
6336
6337 /* this loads the data into the hw struct for us */
6338 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6339 &data_size,
6340 i40e_aqc_opc_list_func_capabilities,
6341 NULL);
6342 /* data loaded, buffer no longer needed */
6343 kfree(cap_buf);
6344
6345 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6346 /* retry with a larger buffer */
6347 buf_len = data_size;
6348 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6349 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006350 "capability discovery failed, err %s aq_err %s\n",
6351 i40e_stat_str(&pf->hw, err),
6352 i40e_aq_str(&pf->hw,
6353 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006354 return -ENODEV;
6355 }
6356 } while (err);
6357
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00006358 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
6359 (pf->hw.aq.fw_maj_ver < 2)) {
6360 pf->hw.func_caps.num_msix_vectors++;
6361 pf->hw.func_caps.num_msix_vectors_vf++;
6362 }
6363
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006364 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6365 dev_info(&pf->pdev->dev,
6366 "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",
6367 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6368 pf->hw.func_caps.num_msix_vectors,
6369 pf->hw.func_caps.num_msix_vectors_vf,
6370 pf->hw.func_caps.fd_filters_guaranteed,
6371 pf->hw.func_caps.fd_filters_best_effort,
6372 pf->hw.func_caps.num_tx_qp,
6373 pf->hw.func_caps.num_vsis);
6374
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006375#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6376 + pf->hw.func_caps.num_vfs)
6377 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6378 dev_info(&pf->pdev->dev,
6379 "got num_vsis %d, setting num_vsis to %d\n",
6380 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6381 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6382 }
6383
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006384 return 0;
6385}
6386
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006387static int i40e_vsi_clear(struct i40e_vsi *vsi);
6388
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006389/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006390 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006391 * @pf: board private structure
6392 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006393static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006394{
6395 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006396 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006397
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006398 /* quick workaround for an NVM issue that leaves a critical register
6399 * uninitialized
6400 */
6401 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6402 static const u32 hkey[] = {
6403 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6404 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6405 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6406 0x95b3a76d};
6407
6408 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6409 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6410 }
6411
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006412 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006413 return;
6414
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006415 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006416 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00006417 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006418 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006419 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006420 break;
6421 }
6422 }
6423
6424 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006425 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006426 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6427 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006428 if (!vsi) {
6429 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006430 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6431 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006432 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006433 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006434
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006435 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006436}
6437
6438/**
6439 * i40e_fdir_teardown - release the Flow Director resources
6440 * @pf: board private structure
6441 **/
6442static void i40e_fdir_teardown(struct i40e_pf *pf)
6443{
6444 int i;
6445
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006446 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00006447 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006448 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
6449 i40e_vsi_release(pf->vsi[i]);
6450 break;
6451 }
6452 }
6453}
6454
6455/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006456 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006457 * @pf: board private structure
6458 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006459 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006460 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006461static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006462{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006463 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006464 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006465 u32 v;
6466
6467 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6468 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006469 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006470
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006471 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006472
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006473 /* quiesce the VSIs and their queues that are not already DOWN */
6474 i40e_pf_quiesce_all_vsi(pf);
6475
Mitch Williams505682c2014-05-20 08:01:37 +00006476 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006477 if (pf->vsi[v])
6478 pf->vsi[v]->seid = 0;
6479 }
6480
6481 i40e_shutdown_adminq(&pf->hw);
6482
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006483 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006484 if (hw->hmc.hmc_obj) {
6485 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006486 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006487 dev_warn(&pf->pdev->dev,
6488 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006489 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006490}
6491
6492/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006493 * i40e_send_version - update firmware with driver version
6494 * @pf: PF struct
6495 */
6496static void i40e_send_version(struct i40e_pf *pf)
6497{
6498 struct i40e_driver_version dv;
6499
6500 dv.major_version = DRV_VERSION_MAJOR;
6501 dv.minor_version = DRV_VERSION_MINOR;
6502 dv.build_version = DRV_VERSION_BUILD;
6503 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006504 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006505 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6506}
6507
6508/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006509 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006510 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006511 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006512 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006513static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006514{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006515 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006516 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006517 i40e_status ret;
6518 u32 v;
6519
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006520 /* Now we wait for GRST to settle out.
6521 * We don't have to delete the VEBs or VSIs from the hw switch
6522 * because the reset will make them disappear.
6523 */
6524 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006525 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006526 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006527 set_bit(__I40E_RESET_FAILED, &pf->state);
6528 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006529 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006530 pf->pfr_count++;
6531
6532 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006533 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006534 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006535
6536 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6537 ret = i40e_init_adminq(&pf->hw);
6538 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006539 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6540 i40e_stat_str(&pf->hw, ret),
6541 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006542 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006543 }
6544
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006545 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006546 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006547 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006548
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006549 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006550 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006551 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006552 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006553
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006554 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6555 hw->func_caps.num_rx_qp,
6556 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6557 if (ret) {
6558 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6559 goto end_core_reset;
6560 }
6561 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6562 if (ret) {
6563 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6564 goto end_core_reset;
6565 }
6566
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006567#ifdef CONFIG_I40E_DCB
6568 ret = i40e_init_pf_dcb(pf);
6569 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006570 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6571 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6572 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006573 }
6574#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006575#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006576 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006577
Vasu Dev38e00432014-08-01 13:27:03 -07006578#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006579 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006580 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006581 if (ret)
6582 goto end_core_reset;
6583
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006584 /* driver is only interested in link up/down and module qualification
6585 * reports from firmware
6586 */
6587 ret = i40e_aq_set_phy_int_mask(&pf->hw,
6588 I40E_AQ_EVENT_LINK_UPDOWN |
6589 I40E_AQ_EVENT_MODULE_QUAL_FAIL, NULL);
6590 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006591 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6592 i40e_stat_str(&pf->hw, ret),
6593 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006594
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006595 /* make sure our flow control settings are restored */
6596 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6597 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006598 dev_info(&pf->pdev->dev, "set fc fail, err %s aq_err %s\n",
6599 i40e_stat_str(&pf->hw, ret),
6600 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006601
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006602 /* Rebuild the VSIs and VEBs that existed before reset.
6603 * They are still in our local switch element arrays, so only
6604 * need to rebuild the switch model in the HW.
6605 *
6606 * If there were VEBs but the reconstitution failed, we'll try
6607 * try to recover minimal use by getting the basic PF VSI working.
6608 */
6609 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006610 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006611 /* find the one VEB connected to the MAC, and find orphans */
6612 for (v = 0; v < I40E_MAX_VEB; v++) {
6613 if (!pf->veb[v])
6614 continue;
6615
6616 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6617 pf->veb[v]->uplink_seid == 0) {
6618 ret = i40e_reconstitute_veb(pf->veb[v]);
6619
6620 if (!ret)
6621 continue;
6622
6623 /* If Main VEB failed, we're in deep doodoo,
6624 * so give up rebuilding the switch and set up
6625 * for minimal rebuild of PF VSI.
6626 * If orphan failed, we'll report the error
6627 * but try to keep going.
6628 */
6629 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6630 dev_info(&pf->pdev->dev,
6631 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6632 ret);
6633 pf->vsi[pf->lan_vsi]->uplink_seid
6634 = pf->mac_seid;
6635 break;
6636 } else if (pf->veb[v]->uplink_seid == 0) {
6637 dev_info(&pf->pdev->dev,
6638 "rebuild of orphan VEB failed: %d\n",
6639 ret);
6640 }
6641 }
6642 }
6643 }
6644
6645 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006646 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006647 /* no VEB, so rebuild only the Main VSI */
6648 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6649 if (ret) {
6650 dev_info(&pf->pdev->dev,
6651 "rebuild of Main VSI failed: %d\n", ret);
6652 goto end_core_reset;
6653 }
6654 }
6655
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006656 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
6657 (pf->hw.aq.fw_maj_ver < 4)) {
6658 msleep(75);
6659 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6660 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006661 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
6662 i40e_stat_str(&pf->hw, ret),
6663 i40e_aq_str(&pf->hw,
6664 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006665 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006666 /* reinit the misc interrupt */
6667 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6668 ret = i40e_setup_misc_vector(pf);
6669
6670 /* restart the VSIs that were rebuilt and running before the reset */
6671 i40e_pf_unquiesce_all_vsi(pf);
6672
Mitch Williams69f64b22014-02-13 03:48:46 -08006673 if (pf->num_alloc_vfs) {
6674 for (v = 0; v < pf->num_alloc_vfs; v++)
6675 i40e_reset_vf(&pf->vf[v], true);
6676 }
6677
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006678 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006679 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006680
6681end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006682 clear_bit(__I40E_RESET_FAILED, &pf->state);
6683clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006684 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6685}
6686
6687/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006688 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006689 * @pf: board private structure
6690 *
6691 * Close up the VFs and other things in prep for a Core Reset,
6692 * then get ready to rebuild the world.
6693 **/
6694static void i40e_handle_reset_warning(struct i40e_pf *pf)
6695{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006696 i40e_prep_for_reset(pf);
6697 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006698}
6699
6700/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006701 * i40e_handle_mdd_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006702 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006703 *
6704 * Called from the MDD irq handler to identify possibly malicious vfs
6705 **/
6706static void i40e_handle_mdd_event(struct i40e_pf *pf)
6707{
6708 struct i40e_hw *hw = &pf->hw;
6709 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00006710 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006711 struct i40e_vf *vf;
6712 u32 reg;
6713 int i;
6714
6715 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
6716 return;
6717
6718 /* find what triggered the MDD event */
6719 reg = rd32(hw, I40E_GL_MDET_TX);
6720 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006721 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
6722 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006723 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006724 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006725 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006726 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006727 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
6728 I40E_GL_MDET_TX_QUEUE_SHIFT) -
6729 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006730 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006731 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 +00006732 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006733 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
6734 mdd_detected = true;
6735 }
6736 reg = rd32(hw, I40E_GL_MDET_RX);
6737 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006738 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
6739 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006740 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006741 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006742 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
6743 I40E_GL_MDET_RX_QUEUE_SHIFT) -
6744 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006745 if (netif_msg_rx_err(pf))
6746 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
6747 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006748 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
6749 mdd_detected = true;
6750 }
6751
Neerav Parikhdf430b12014-06-04 01:23:15 +00006752 if (mdd_detected) {
6753 reg = rd32(hw, I40E_PF_MDET_TX);
6754 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
6755 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006756 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00006757 pf_mdd_detected = true;
6758 }
6759 reg = rd32(hw, I40E_PF_MDET_RX);
6760 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
6761 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006762 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00006763 pf_mdd_detected = true;
6764 }
6765 /* Queue belongs to the PF, initiate a reset */
6766 if (pf_mdd_detected) {
6767 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6768 i40e_service_event_schedule(pf);
6769 }
6770 }
6771
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006772 /* see if one of the VFs needs its hand slapped */
6773 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
6774 vf = &(pf->vf[i]);
6775 reg = rd32(hw, I40E_VP_MDET_TX(i));
6776 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
6777 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
6778 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006779 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
6780 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006781 }
6782
6783 reg = rd32(hw, I40E_VP_MDET_RX(i));
6784 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
6785 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
6786 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006787 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
6788 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006789 }
6790
6791 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
6792 dev_info(&pf->pdev->dev,
6793 "Too many MDD events on VF %d, disabled\n", i);
6794 dev_info(&pf->pdev->dev,
6795 "Use PF Control I/F to re-enable the VF\n");
6796 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
6797 }
6798 }
6799
6800 /* re-enable mdd interrupt cause */
6801 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
6802 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
6803 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
6804 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
6805 i40e_flush(hw);
6806}
6807
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006808#ifdef CONFIG_I40E_VXLAN
6809/**
6810 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
6811 * @pf: board private structure
6812 **/
6813static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
6814{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006815 struct i40e_hw *hw = &pf->hw;
6816 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006817 __be16 port;
6818 int i;
6819
6820 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
6821 return;
6822
6823 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
6824
6825 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04006826 if (pf->pending_vxlan_bitmap & BIT_ULL(i)) {
6827 pf->pending_vxlan_bitmap &= ~BIT_ULL(i);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006828 port = pf->vxlan_ports[i];
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07006829 if (port)
6830 ret = i40e_aq_add_udp_tunnel(hw, ntohs(port),
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006831 I40E_AQC_TUNNEL_TYPE_VXLAN,
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07006832 NULL, NULL);
6833 else
6834 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006835
6836 if (ret) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07006837 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006838 "%s vxlan port %d, index %d failed, err %s aq_err %s\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07006839 port ? "add" : "delete",
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006840 ntohs(port), i,
6841 i40e_stat_str(&pf->hw, ret),
6842 i40e_aq_str(&pf->hw,
6843 pf->hw.aq.asq_last_status));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006844 pf->vxlan_ports[i] = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006845 }
6846 }
6847 }
6848}
6849
6850#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006851/**
6852 * i40e_service_task - Run the driver's async subtasks
6853 * @work: pointer to work_struct containing our data
6854 **/
6855static void i40e_service_task(struct work_struct *work)
6856{
6857 struct i40e_pf *pf = container_of(work,
6858 struct i40e_pf,
6859 service_task);
6860 unsigned long start_time = jiffies;
6861
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00006862 /* don't bother with service tasks if a reset is in progress */
6863 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6864 i40e_service_event_complete(pf);
6865 return;
6866 }
6867
Kiran Patilb03a8c12015-09-24 18:13:15 -04006868 i40e_detect_recover_hung(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006869 i40e_reset_subtask(pf);
6870 i40e_handle_mdd_event(pf);
6871 i40e_vc_process_vflr_event(pf);
6872 i40e_watchdog_subtask(pf);
6873 i40e_fdir_reinit_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006874 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006875#ifdef CONFIG_I40E_VXLAN
6876 i40e_sync_vxlan_filters_subtask(pf);
6877#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006878 i40e_clean_adminq_subtask(pf);
6879
6880 i40e_service_event_complete(pf);
6881
6882 /* If the tasks have taken longer than one timer cycle or there
6883 * is more work to be done, reschedule the service task now
6884 * rather than wait for the timer to tick again.
6885 */
6886 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
6887 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
6888 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
6889 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
6890 i40e_service_event_schedule(pf);
6891}
6892
6893/**
6894 * i40e_service_timer - timer callback
6895 * @data: pointer to PF struct
6896 **/
6897static void i40e_service_timer(unsigned long data)
6898{
6899 struct i40e_pf *pf = (struct i40e_pf *)data;
6900
6901 mod_timer(&pf->service_timer,
6902 round_jiffies(jiffies + pf->service_timer_period));
6903 i40e_service_event_schedule(pf);
6904}
6905
6906/**
6907 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
6908 * @vsi: the VSI being configured
6909 **/
6910static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
6911{
6912 struct i40e_pf *pf = vsi->back;
6913
6914 switch (vsi->type) {
6915 case I40E_VSI_MAIN:
6916 vsi->alloc_queue_pairs = pf->num_lan_qps;
6917 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6918 I40E_REQ_DESCRIPTOR_MULTIPLE);
6919 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6920 vsi->num_q_vectors = pf->num_lan_msix;
6921 else
6922 vsi->num_q_vectors = 1;
6923
6924 break;
6925
6926 case I40E_VSI_FDIR:
6927 vsi->alloc_queue_pairs = 1;
6928 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
6929 I40E_REQ_DESCRIPTOR_MULTIPLE);
6930 vsi->num_q_vectors = 1;
6931 break;
6932
6933 case I40E_VSI_VMDQ2:
6934 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
6935 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6936 I40E_REQ_DESCRIPTOR_MULTIPLE);
6937 vsi->num_q_vectors = pf->num_vmdq_msix;
6938 break;
6939
6940 case I40E_VSI_SRIOV:
6941 vsi->alloc_queue_pairs = pf->num_vf_qps;
6942 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6943 I40E_REQ_DESCRIPTOR_MULTIPLE);
6944 break;
6945
Vasu Dev38e00432014-08-01 13:27:03 -07006946#ifdef I40E_FCOE
6947 case I40E_VSI_FCOE:
6948 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
6949 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6950 I40E_REQ_DESCRIPTOR_MULTIPLE);
6951 vsi->num_q_vectors = pf->num_fcoe_msix;
6952 break;
6953
6954#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006955 default:
6956 WARN_ON(1);
6957 return -ENODATA;
6958 }
6959
6960 return 0;
6961}
6962
6963/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006964 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
6965 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006966 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006967 *
6968 * On error: returns error code (negative)
6969 * On success: returns 0
6970 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006971static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006972{
6973 int size;
6974 int ret = 0;
6975
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00006976 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006977 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
6978 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
6979 if (!vsi->tx_rings)
6980 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006981 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
6982
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006983 if (alloc_qvectors) {
6984 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00006985 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006986 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
6987 if (!vsi->q_vectors) {
6988 ret = -ENOMEM;
6989 goto err_vectors;
6990 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006991 }
6992 return ret;
6993
6994err_vectors:
6995 kfree(vsi->tx_rings);
6996 return ret;
6997}
6998
6999/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007000 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7001 * @pf: board private structure
7002 * @type: type of VSI
7003 *
7004 * On error: returns error code (negative)
7005 * On success: returns vsi index in PF (positive)
7006 **/
7007static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7008{
7009 int ret = -ENODEV;
7010 struct i40e_vsi *vsi;
7011 int vsi_idx;
7012 int i;
7013
7014 /* Need to protect the allocation of the VSIs at the PF level */
7015 mutex_lock(&pf->switch_mutex);
7016
7017 /* VSI list may be fragmented if VSI creation/destruction has
7018 * been happening. We can afford to do a quick scan to look
7019 * for any free VSIs in the list.
7020 *
7021 * find next empty vsi slot, looping back around if necessary
7022 */
7023 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007024 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007025 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007026 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007027 i = 0;
7028 while (i < pf->next_vsi && pf->vsi[i])
7029 i++;
7030 }
7031
Mitch Williams505682c2014-05-20 08:01:37 +00007032 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007033 vsi_idx = i; /* Found one! */
7034 } else {
7035 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007036 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007037 }
7038 pf->next_vsi = ++i;
7039
7040 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7041 if (!vsi) {
7042 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007043 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007044 }
7045 vsi->type = type;
7046 vsi->back = pf;
7047 set_bit(__I40E_DOWN, &vsi->state);
7048 vsi->flags = 0;
7049 vsi->idx = vsi_idx;
7050 vsi->rx_itr_setting = pf->rx_itr_default;
7051 vsi->tx_itr_setting = pf->tx_itr_default;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007052 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7053 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007054 vsi->netdev_registered = false;
7055 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
7056 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00007057 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007058
Alexander Duyck9f65e152013-09-28 06:00:58 +00007059 ret = i40e_set_num_rings_in_vsi(vsi);
7060 if (ret)
7061 goto err_rings;
7062
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007063 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007064 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007065 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007066
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007067 /* Setup default MSIX irq handler for VSI */
7068 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7069
7070 pf->vsi[vsi_idx] = vsi;
7071 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007072 goto unlock_pf;
7073
Alexander Duyck9f65e152013-09-28 06:00:58 +00007074err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007075 pf->next_vsi = i - 1;
7076 kfree(vsi);
7077unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007078 mutex_unlock(&pf->switch_mutex);
7079 return ret;
7080}
7081
7082/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007083 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7084 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007085 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007086 *
7087 * On error: returns error code (negative)
7088 * On success: returns 0
7089 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007090static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007091{
7092 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007093 if (free_qvectors) {
7094 kfree(vsi->q_vectors);
7095 vsi->q_vectors = NULL;
7096 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007097 kfree(vsi->tx_rings);
7098 vsi->tx_rings = NULL;
7099 vsi->rx_rings = NULL;
7100}
7101
7102/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007103 * i40e_vsi_clear - Deallocate the VSI provided
7104 * @vsi: the VSI being un-configured
7105 **/
7106static int i40e_vsi_clear(struct i40e_vsi *vsi)
7107{
7108 struct i40e_pf *pf;
7109
7110 if (!vsi)
7111 return 0;
7112
7113 if (!vsi->back)
7114 goto free_vsi;
7115 pf = vsi->back;
7116
7117 mutex_lock(&pf->switch_mutex);
7118 if (!pf->vsi[vsi->idx]) {
7119 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7120 vsi->idx, vsi->idx, vsi, vsi->type);
7121 goto unlock_vsi;
7122 }
7123
7124 if (pf->vsi[vsi->idx] != vsi) {
7125 dev_err(&pf->pdev->dev,
7126 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7127 pf->vsi[vsi->idx]->idx,
7128 pf->vsi[vsi->idx],
7129 pf->vsi[vsi->idx]->type,
7130 vsi->idx, vsi, vsi->type);
7131 goto unlock_vsi;
7132 }
7133
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007134 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007135 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7136 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7137
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007138 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00007139
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007140 pf->vsi[vsi->idx] = NULL;
7141 if (vsi->idx < pf->next_vsi)
7142 pf->next_vsi = vsi->idx;
7143
7144unlock_vsi:
7145 mutex_unlock(&pf->switch_mutex);
7146free_vsi:
7147 kfree(vsi);
7148
7149 return 0;
7150}
7151
7152/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007153 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7154 * @vsi: the VSI being cleaned
7155 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007156static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007157{
7158 int i;
7159
Greg Rose8e9dca52013-12-18 13:45:53 +00007160 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007161 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007162 kfree_rcu(vsi->tx_rings[i], rcu);
7163 vsi->tx_rings[i] = NULL;
7164 vsi->rx_rings[i] = NULL;
7165 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007166 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007167}
7168
7169/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007170 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7171 * @vsi: the VSI being configured
7172 **/
7173static int i40e_alloc_rings(struct i40e_vsi *vsi)
7174{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007175 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007176 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007177 int i;
7178
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007179 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007180 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007181 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007182 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7183 if (!tx_ring)
7184 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007185
7186 tx_ring->queue_index = i;
7187 tx_ring->reg_idx = vsi->base_queue + i;
7188 tx_ring->ring_active = false;
7189 tx_ring->vsi = vsi;
7190 tx_ring->netdev = vsi->netdev;
7191 tx_ring->dev = &pf->pdev->dev;
7192 tx_ring->count = vsi->num_desc;
7193 tx_ring->size = 0;
7194 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007195 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7196 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Anjali Singhai Jain527274c2015-06-05 12:20:31 -04007197 if (vsi->back->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE)
7198 tx_ring->flags |= I40E_TXR_FLAGS_OUTER_UDP_CSUM;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007199 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007200
Alexander Duyck9f65e152013-09-28 06:00:58 +00007201 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007202 rx_ring->queue_index = i;
7203 rx_ring->reg_idx = vsi->base_queue + i;
7204 rx_ring->ring_active = false;
7205 rx_ring->vsi = vsi;
7206 rx_ring->netdev = vsi->netdev;
7207 rx_ring->dev = &pf->pdev->dev;
7208 rx_ring->count = vsi->num_desc;
7209 rx_ring->size = 0;
7210 rx_ring->dcb_tc = 0;
7211 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
7212 set_ring_16byte_desc_enabled(rx_ring);
7213 else
7214 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e152013-09-28 06:00:58 +00007215 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007216 }
7217
7218 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007219
7220err_out:
7221 i40e_vsi_clear_rings(vsi);
7222 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007223}
7224
7225/**
7226 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7227 * @pf: board private structure
7228 * @vectors: the number of MSI-X vectors to request
7229 *
7230 * Returns the number of vectors reserved, or error
7231 **/
7232static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7233{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007234 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7235 I40E_MIN_MSIX, vectors);
7236 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007237 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007238 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007239 vectors = 0;
7240 }
7241
7242 return vectors;
7243}
7244
7245/**
7246 * i40e_init_msix - Setup the MSIX capability
7247 * @pf: board private structure
7248 *
7249 * Work with the OS to set up the MSIX vectors needed.
7250 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007251 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007252 **/
7253static int i40e_init_msix(struct i40e_pf *pf)
7254{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007255 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007256 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007257 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007258 int v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007259
7260 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7261 return -ENODEV;
7262
7263 /* The number of vectors we'll request will be comprised of:
7264 * - Add 1 for "other" cause for Admin Queue events, etc.
7265 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007266 * - Queues being used for RSS.
7267 * We don't need as many as max_rss_size vectors.
7268 * use rss_size instead in the calculation since that
7269 * is governed by number of cpus in the system.
7270 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007271 * - The number of VMDq pairs
Vasu Dev38e00432014-08-01 13:27:03 -07007272#ifdef I40E_FCOE
7273 * - The number of FCOE qps.
7274#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007275 * Once we count this up, try the request.
7276 *
7277 * If we can't get what we want, we'll simplify to nearly nothing
7278 * and try again. If that still fails, we punt.
7279 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007280 vectors_left = hw->func_caps.num_msix_vectors;
7281 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007282
Shannon Nelson1e200e42015-02-27 09:15:24 +00007283 /* reserve one vector for miscellaneous handler */
7284 if (vectors_left) {
7285 v_budget++;
7286 vectors_left--;
7287 }
7288
7289 /* reserve vectors for the main PF traffic queues */
7290 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7291 vectors_left -= pf->num_lan_msix;
7292 v_budget += pf->num_lan_msix;
7293
7294 /* reserve one vector for sideband flow director */
7295 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7296 if (vectors_left) {
7297 v_budget++;
7298 vectors_left--;
7299 } else {
7300 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7301 }
7302 }
John W Linville83840e42015-01-14 03:06:28 +00007303
Vasu Dev38e00432014-08-01 13:27:03 -07007304#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007305 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007306 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007307 if (!vectors_left)
7308 pf->num_fcoe_msix = 0;
7309 else if (vectors_left >= pf->num_fcoe_qps)
7310 pf->num_fcoe_msix = pf->num_fcoe_qps;
7311 else
7312 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007313 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007314 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007315 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007316
Vasu Dev38e00432014-08-01 13:27:03 -07007317#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007318 /* any vectors left over go for VMDq support */
7319 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7320 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7321 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7322
7323 /* if we're short on vectors for what's desired, we limit
7324 * the queues per vmdq. If this is still more than are
7325 * available, the user will need to change the number of
7326 * queues/vectors used by the PF later with the ethtool
7327 * channels command
7328 */
7329 if (vmdq_vecs < vmdq_vecs_wanted)
7330 pf->num_vmdq_qps = 1;
7331 pf->num_vmdq_msix = pf->num_vmdq_qps;
7332
7333 v_budget += vmdq_vecs;
7334 vectors_left -= vmdq_vecs;
7335 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007336
7337 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7338 GFP_KERNEL);
7339 if (!pf->msix_entries)
7340 return -ENOMEM;
7341
7342 for (i = 0; i < v_budget; i++)
7343 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007344 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007345
Shannon Nelson3b444392015-02-26 16:15:57 +00007346 if (v_actual != v_budget) {
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007347 /* If we have limited resources, we will start with no vectors
7348 * for the special features and then allocate vectors to some
7349 * of these features based on the policy and at the end disable
7350 * the features that did not get any vectors.
7351 */
Vasu Dev38e00432014-08-01 13:27:03 -07007352#ifdef I40E_FCOE
7353 pf->num_fcoe_qps = 0;
7354 pf->num_fcoe_msix = 0;
7355#endif
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007356 pf->num_vmdq_msix = 0;
7357 }
7358
Shannon Nelson3b444392015-02-26 16:15:57 +00007359 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007360 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7361 kfree(pf->msix_entries);
7362 pf->msix_entries = NULL;
7363 return -ENODEV;
7364
Shannon Nelson3b444392015-02-26 16:15:57 +00007365 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007366 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007367 pf->num_vmdq_vsis = 0;
7368 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007369 pf->num_lan_qps = 1;
7370 pf->num_lan_msix = 1;
7371
Shannon Nelson3b444392015-02-26 16:15:57 +00007372 } else if (v_actual != v_budget) {
7373 int vec;
7374
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007375 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007376 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007377
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007378 /* Scale vector usage down */
7379 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007380 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007381 pf->num_vmdq_qps = 1;
7382 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007383
7384 /* partition out the remaining vectors */
7385 switch (vec) {
7386 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007387 pf->num_lan_msix = 1;
7388 break;
7389 case 3:
Vasu Dev38e00432014-08-01 13:27:03 -07007390#ifdef I40E_FCOE
7391 /* give one vector to FCoE */
7392 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7393 pf->num_lan_msix = 1;
7394 pf->num_fcoe_msix = 1;
7395 }
7396#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007397 pf->num_lan_msix = 2;
Vasu Dev38e00432014-08-01 13:27:03 -07007398#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007399 break;
7400 default:
Vasu Dev38e00432014-08-01 13:27:03 -07007401#ifdef I40E_FCOE
7402 /* give one vector to FCoE */
7403 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7404 pf->num_fcoe_msix = 1;
7405 vec--;
7406 }
7407#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007408 /* give the rest to the PF */
7409 pf->num_lan_msix = min_t(int, vec, pf->num_lan_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007410 break;
7411 }
7412 }
7413
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007414 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7415 (pf->num_vmdq_msix == 0)) {
7416 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7417 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7418 }
Vasu Dev38e00432014-08-01 13:27:03 -07007419#ifdef I40E_FCOE
7420
7421 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7422 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7423 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7424 }
7425#endif
Shannon Nelson3b444392015-02-26 16:15:57 +00007426 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007427}
7428
7429/**
Greg Rose90e04072014-03-06 08:59:57 +00007430 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007431 * @vsi: the VSI being configured
7432 * @v_idx: index of the vector in the vsi struct
7433 *
7434 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7435 **/
Greg Rose90e04072014-03-06 08:59:57 +00007436static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00007437{
7438 struct i40e_q_vector *q_vector;
7439
7440 /* allocate q_vector */
7441 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7442 if (!q_vector)
7443 return -ENOMEM;
7444
7445 q_vector->vsi = vsi;
7446 q_vector->v_idx = v_idx;
7447 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
7448 if (vsi->netdev)
7449 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007450 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007451
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007452 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7453 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7454
Alexander Duyck493fb302013-09-28 07:01:44 +00007455 /* tie q_vector and vsi together */
7456 vsi->q_vectors[v_idx] = q_vector;
7457
7458 return 0;
7459}
7460
7461/**
Greg Rose90e04072014-03-06 08:59:57 +00007462 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007463 * @vsi: the VSI being configured
7464 *
7465 * We allocate one q_vector per queue interrupt. If allocation fails we
7466 * return -ENOMEM.
7467 **/
Greg Rose90e04072014-03-06 08:59:57 +00007468static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007469{
7470 struct i40e_pf *pf = vsi->back;
7471 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00007472 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007473
7474 /* if not MSIX, give the one vector only to the LAN VSI */
7475 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7476 num_q_vectors = vsi->num_q_vectors;
7477 else if (vsi == pf->vsi[pf->lan_vsi])
7478 num_q_vectors = 1;
7479 else
7480 return -EINVAL;
7481
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007482 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00007483 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00007484 if (err)
7485 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007486 }
7487
7488 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007489
7490err_out:
7491 while (v_idx--)
7492 i40e_free_q_vector(vsi, v_idx);
7493
7494 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007495}
7496
7497/**
7498 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7499 * @pf: board private structure to initialize
7500 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007501static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007502{
Shannon Nelson3b444392015-02-26 16:15:57 +00007503 int vectors = 0;
7504 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007505
7506 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007507 vectors = i40e_init_msix(pf);
7508 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007509 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007510#ifdef I40E_FCOE
7511 I40E_FLAG_FCOE_ENABLED |
7512#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007513 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007514 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007515 I40E_FLAG_SRIOV_ENABLED |
7516 I40E_FLAG_FD_SB_ENABLED |
7517 I40E_FLAG_FD_ATR_ENABLED |
7518 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007519
7520 /* rework the queue expectations without MSIX */
7521 i40e_determine_queue_usage(pf);
7522 }
7523 }
7524
7525 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7526 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007527 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007528 vectors = pci_enable_msi(pf->pdev);
7529 if (vectors < 0) {
7530 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7531 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007532 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7533 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007534 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007535 }
7536
Shannon Nelson958a3e32013-09-28 07:13:28 +00007537 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007538 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007539
Shannon Nelson3b444392015-02-26 16:15:57 +00007540 /* set up vector assignment tracking */
7541 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7542 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007543 if (!pf->irq_pile) {
7544 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7545 return -ENOMEM;
7546 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007547 pf->irq_pile->num_entries = vectors;
7548 pf->irq_pile->search_hint = 0;
7549
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007550 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007551 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007552
7553 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007554}
7555
7556/**
7557 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7558 * @pf: board private structure
7559 *
7560 * This sets up the handler for MSIX 0, which is used to manage the
7561 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7562 * when in MSI or Legacy interrupt mode.
7563 **/
7564static int i40e_setup_misc_vector(struct i40e_pf *pf)
7565{
7566 struct i40e_hw *hw = &pf->hw;
7567 int err = 0;
7568
7569 /* Only request the irq if this is the first time through, and
7570 * not when we're rebuilding after a Reset
7571 */
7572 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7573 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007574 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007575 if (err) {
7576 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007577 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007578 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007579 return -EFAULT;
7580 }
7581 }
7582
Jacob Kellerab437b52014-12-14 01:55:08 +00007583 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007584
7585 /* associate no queues to the misc vector */
7586 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
7587 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
7588
7589 i40e_flush(hw);
7590
7591 i40e_irq_dynamic_enable_icr0(pf);
7592
7593 return err;
7594}
7595
7596/**
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007597 * i40e_config_rss_aq - Prepare for RSS using AQ commands
7598 * @vsi: vsi structure
7599 * @seed: RSS hash seed
7600 **/
7601static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed)
7602{
7603 struct i40e_aqc_get_set_rss_key_data rss_key;
7604 struct i40e_pf *pf = vsi->back;
7605 struct i40e_hw *hw = &pf->hw;
7606 bool pf_lut = false;
7607 u8 *rss_lut;
7608 int ret, i;
7609
7610 memset(&rss_key, 0, sizeof(rss_key));
7611 memcpy(&rss_key, seed, sizeof(rss_key));
7612
7613 rss_lut = kzalloc(pf->rss_table_size, GFP_KERNEL);
7614 if (!rss_lut)
7615 return -ENOMEM;
7616
7617 /* Populate the LUT with max no. of queues in round robin fashion */
7618 for (i = 0; i < vsi->rss_table_size; i++)
7619 rss_lut[i] = i % vsi->rss_size;
7620
7621 ret = i40e_aq_set_rss_key(hw, vsi->id, &rss_key);
7622 if (ret) {
7623 dev_info(&pf->pdev->dev,
7624 "Cannot set RSS key, err %s aq_err %s\n",
7625 i40e_stat_str(&pf->hw, ret),
7626 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai126b63d2015-08-24 13:26:53 -07007627 goto config_rss_aq_out;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007628 }
7629
7630 if (vsi->type == I40E_VSI_MAIN)
7631 pf_lut = true;
7632
7633 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, rss_lut,
7634 vsi->rss_table_size);
7635 if (ret)
7636 dev_info(&pf->pdev->dev,
7637 "Cannot set RSS lut, err %s aq_err %s\n",
7638 i40e_stat_str(&pf->hw, ret),
7639 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7640
Anjali Singhai126b63d2015-08-24 13:26:53 -07007641config_rss_aq_out:
7642 kfree(rss_lut);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007643 return ret;
7644}
7645
7646/**
7647 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
7648 * @vsi: VSI structure
7649 **/
7650static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
7651{
7652 u8 seed[I40E_HKEY_ARRAY_SIZE];
7653 struct i40e_pf *pf = vsi->back;
7654
7655 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
7656 vsi->rss_size = min_t(int, pf->rss_size, vsi->num_queue_pairs);
7657
7658 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
7659 return i40e_config_rss_aq(vsi, seed);
7660
7661 return 0;
7662}
7663
7664/**
7665 * i40e_config_rss_reg - Prepare for RSS if used
7666 * @pf: board private structure
7667 * @seed: RSS hash seed
7668 **/
7669static int i40e_config_rss_reg(struct i40e_pf *pf, const u8 *seed)
7670{
7671 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
7672 struct i40e_hw *hw = &pf->hw;
7673 u32 *seed_dw = (u32 *)seed;
7674 u32 current_queue = 0;
7675 u32 lut = 0;
7676 int i, j;
7677
7678 /* Fill out hash function seed */
7679 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
7680 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
7681
7682 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++) {
7683 lut = 0;
7684 for (j = 0; j < 4; j++) {
7685 if (current_queue == vsi->rss_size)
7686 current_queue = 0;
7687 lut |= ((current_queue) << (8 * j));
7688 current_queue++;
7689 }
7690 wr32(&pf->hw, I40E_PFQF_HLUT(i), lut);
7691 }
7692 i40e_flush(hw);
7693
7694 return 0;
7695}
7696
7697/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007698 * i40e_config_rss - Prepare for RSS if used
7699 * @pf: board private structure
7700 **/
7701static int i40e_config_rss(struct i40e_pf *pf)
7702{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00007703 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007704 u8 seed[I40E_HKEY_ARRAY_SIZE];
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00007705 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007706 u32 reg_val;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007707 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007708
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007709 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007710
7711 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
7712 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
7713 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007714 hena |= i40e_pf_get_default_rss_hena(pf);
7715
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007716 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
7717 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
7718
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00007719 vsi->rss_size = min_t(int, pf->rss_size, vsi->num_queue_pairs);
7720
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007721 /* Determine the RSS table size based on the hardware capabilities */
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007722 reg_val = rd32(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007723 reg_val = (pf->rss_table_size == 512) ?
7724 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
7725 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007726 wr32(hw, I40E_PFQF_CTL_0, reg_val);
7727
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007728 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
7729 return i40e_config_rss_aq(pf->vsi[pf->lan_vsi], seed);
7730 else
7731 return i40e_config_rss_reg(pf, seed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007732}
7733
7734/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007735 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
7736 * @pf: board private structure
7737 * @queue_count: the requested queue count for rss.
7738 *
7739 * returns 0 if rss is not enabled, if enabled returns the final rss queue
7740 * count which may be different from the requested queue count.
7741 **/
7742int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
7743{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007744 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
7745 int new_rss_size;
7746
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007747 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
7748 return 0;
7749
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007750 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007751
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007752 if (queue_count != vsi->num_queue_pairs) {
7753 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007754 i40e_prep_for_reset(pf);
7755
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007756 pf->rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007757
7758 i40e_reset_and_rebuild(pf, true);
7759 i40e_config_rss(pf);
7760 }
7761 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
7762 return pf->rss_size;
7763}
7764
7765/**
Greg Rosef4492db2015-02-06 08:52:12 +00007766 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
7767 * @pf: board private structure
7768 **/
7769i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
7770{
7771 i40e_status status;
7772 bool min_valid, max_valid;
7773 u32 max_bw, min_bw;
7774
7775 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
7776 &min_valid, &max_valid);
7777
7778 if (!status) {
7779 if (min_valid)
7780 pf->npar_min_bw = min_bw;
7781 if (max_valid)
7782 pf->npar_max_bw = max_bw;
7783 }
7784
7785 return status;
7786}
7787
7788/**
7789 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
7790 * @pf: board private structure
7791 **/
7792i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
7793{
7794 struct i40e_aqc_configure_partition_bw_data bw_data;
7795 i40e_status status;
7796
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007797 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007798 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00007799 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
7800 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
7801
7802 /* Set the new bandwidths */
7803 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
7804
7805 return status;
7806}
7807
7808/**
7809 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
7810 * @pf: board private structure
7811 **/
7812i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
7813{
7814 /* Commit temporary BW setting to permanent NVM image */
7815 enum i40e_admin_queue_err last_aq_status;
7816 i40e_status ret;
7817 u16 nvm_word;
7818
7819 if (pf->hw.partition_id != 1) {
7820 dev_info(&pf->pdev->dev,
7821 "Commit BW only works on partition 1! This is partition %d",
7822 pf->hw.partition_id);
7823 ret = I40E_NOT_SUPPORTED;
7824 goto bw_commit_out;
7825 }
7826
7827 /* Acquire NVM for read access */
7828 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
7829 last_aq_status = pf->hw.aq.asq_last_status;
7830 if (ret) {
7831 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007832 "Cannot acquire NVM for read access, err %s aq_err %s\n",
7833 i40e_stat_str(&pf->hw, ret),
7834 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00007835 goto bw_commit_out;
7836 }
7837
7838 /* Read word 0x10 of NVM - SW compatibility word 1 */
7839 ret = i40e_aq_read_nvm(&pf->hw,
7840 I40E_SR_NVM_CONTROL_WORD,
7841 0x10, sizeof(nvm_word), &nvm_word,
7842 false, NULL);
7843 /* Save off last admin queue command status before releasing
7844 * the NVM
7845 */
7846 last_aq_status = pf->hw.aq.asq_last_status;
7847 i40e_release_nvm(&pf->hw);
7848 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007849 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
7850 i40e_stat_str(&pf->hw, ret),
7851 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00007852 goto bw_commit_out;
7853 }
7854
7855 /* Wait a bit for NVM release to complete */
7856 msleep(50);
7857
7858 /* Acquire NVM for write access */
7859 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
7860 last_aq_status = pf->hw.aq.asq_last_status;
7861 if (ret) {
7862 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007863 "Cannot acquire NVM for write access, err %s aq_err %s\n",
7864 i40e_stat_str(&pf->hw, ret),
7865 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00007866 goto bw_commit_out;
7867 }
7868 /* Write it back out unchanged to initiate update NVM,
7869 * which will force a write of the shadow (alt) RAM to
7870 * the NVM - thus storing the bandwidth values permanently.
7871 */
7872 ret = i40e_aq_update_nvm(&pf->hw,
7873 I40E_SR_NVM_CONTROL_WORD,
7874 0x10, sizeof(nvm_word),
7875 &nvm_word, true, NULL);
7876 /* Save off last admin queue command status before releasing
7877 * the NVM
7878 */
7879 last_aq_status = pf->hw.aq.asq_last_status;
7880 i40e_release_nvm(&pf->hw);
7881 if (ret)
7882 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007883 "BW settings NOT SAVED, err %s aq_err %s\n",
7884 i40e_stat_str(&pf->hw, ret),
7885 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00007886bw_commit_out:
7887
7888 return ret;
7889}
7890
7891/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007892 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
7893 * @pf: board private structure to initialize
7894 *
7895 * i40e_sw_init initializes the Adapter private data structure.
7896 * Fields are initialized based on PCI device information and
7897 * OS network device settings (MTU size).
7898 **/
7899static int i40e_sw_init(struct i40e_pf *pf)
7900{
7901 int err = 0;
7902 int size;
7903
7904 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
7905 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00007906 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007907 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
7908 if (I40E_DEBUG_USER & debug)
7909 pf->hw.debug_mask = debug;
7910 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
7911 I40E_DEFAULT_MSG_ENABLE);
7912 }
7913
7914 /* Set default capability flags */
7915 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
7916 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00007917 I40E_FLAG_MSIX_ENABLED;
7918
7919 if (iommu_present(&pci_bus_type))
7920 pf->flags |= I40E_FLAG_RX_PS_ENABLED;
7921 else
7922 pf->flags |= I40E_FLAG_RX_1BUF_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007923
Mitch Williamsca99eb92014-04-04 04:43:07 +00007924 /* Set default ITR */
7925 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
7926 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
7927
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00007928 /* Depending on PF configurations, it is possible that the RSS
7929 * maximum might end up larger than the available queues
7930 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007931 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Paul M Stillwell Jrec9a7db2014-07-09 07:46:10 +00007932 pf->rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007933 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00007934 pf->rss_size_max = min_t(int, pf->rss_size_max,
7935 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007936 if (pf->hw.func_caps.rss) {
7937 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00007938 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007939 }
7940
Catherine Sullivan2050bc62013-12-18 13:46:03 +00007941 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04007942 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00007943 pf->flags |= I40E_FLAG_MFP_ENABLED;
7944 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00007945 if (i40e_get_npar_bw_setting(pf))
7946 dev_warn(&pf->pdev->dev,
7947 "Could not get NPAR bw settings\n");
7948 else
7949 dev_info(&pf->pdev->dev,
7950 "Min BW = %8.8x, Max BW = %8.8x\n",
7951 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00007952 }
7953
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007954 /* FW/NVM is not yet fixed in this regard */
7955 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
7956 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
7957 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
7958 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007959 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007960 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007961 } else {
7962 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00007963 "Flow Director Sideband mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007964 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007965 pf->fdir_pf_filter_count =
7966 pf->hw.func_caps.fd_filters_guaranteed;
7967 pf->hw.fdir_shared_filter_count =
7968 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007969 }
7970
7971 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007972 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007973 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007974 }
7975
Vasu Dev38e00432014-08-01 13:27:03 -07007976#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04007977 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07007978
7979#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007980#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00007981 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007982 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
7983 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
7984 pf->num_req_vfs = min_t(int,
7985 pf->hw.func_caps.num_vfs,
7986 I40E_MAX_VF_COUNT);
7987 }
7988#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04007989 if (pf->hw.mac.type == I40E_MAC_X722) {
7990 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
7991 I40E_FLAG_128_QP_RSS_CAPABLE |
7992 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
7993 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
7994 I40E_FLAG_WB_ON_ITR_CAPABLE |
7995 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE;
7996 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007997 pf->eeprom_version = 0xDEAD;
7998 pf->lan_veb = I40E_NO_VEB;
7999 pf->lan_vsi = I40E_NO_VSI;
8000
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008001 /* By default FW has this off for performance reasons */
8002 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8003
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008004 /* set up queue assignment tracking */
8005 size = sizeof(struct i40e_lump_tracking)
8006 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8007 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8008 if (!pf->qp_pile) {
8009 err = -ENOMEM;
8010 goto sw_init_done;
8011 }
8012 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8013 pf->qp_pile->search_hint = 0;
8014
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008015 pf->tx_timeout_recovery_level = 1;
8016
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008017 mutex_init(&pf->switch_mutex);
8018
Greg Rosec668a122015-02-26 16:10:39 +00008019 /* If NPAR is enabled nudge the Tx scheduler */
8020 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8021 i40e_set_npar_bw_setting(pf);
8022
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008023sw_init_done:
8024 return err;
8025}
8026
8027/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008028 * i40e_set_ntuple - set the ntuple feature flag and take action
8029 * @pf: board private structure to initialize
8030 * @features: the feature set that the stack is suggesting
8031 *
8032 * returns a bool to indicate if reset needs to happen
8033 **/
8034bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8035{
8036 bool need_reset = false;
8037
8038 /* Check if Flow Director n-tuple support was enabled or disabled. If
8039 * the state changed, we need to reset.
8040 */
8041 if (features & NETIF_F_NTUPLE) {
8042 /* Enable filters and mark for reset */
8043 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8044 need_reset = true;
8045 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8046 } else {
8047 /* turn off filters, mark for reset and clear SW filter list */
8048 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8049 need_reset = true;
8050 i40e_fdir_filter_exit(pf);
8051 }
8052 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008053 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008054 /* reset fd counters */
8055 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8056 pf->fdir_pf_active_filters = 0;
8057 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04008058 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8059 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008060 /* if ATR was auto disabled it can be re-enabled. */
8061 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8062 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
8063 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008064 }
8065 return need_reset;
8066}
8067
8068/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008069 * i40e_set_features - set the netdev feature flags
8070 * @netdev: ptr to the netdev being adjusted
8071 * @features: the feature set that the stack is suggesting
8072 **/
8073static int i40e_set_features(struct net_device *netdev,
8074 netdev_features_t features)
8075{
8076 struct i40e_netdev_priv *np = netdev_priv(netdev);
8077 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008078 struct i40e_pf *pf = vsi->back;
8079 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008080
8081 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8082 i40e_vlan_stripping_enable(vsi);
8083 else
8084 i40e_vlan_stripping_disable(vsi);
8085
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008086 need_reset = i40e_set_ntuple(pf, features);
8087
8088 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008089 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008090
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008091 return 0;
8092}
8093
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008094#ifdef CONFIG_I40E_VXLAN
8095/**
8096 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
8097 * @pf: board private structure
8098 * @port: The UDP port to look up
8099 *
8100 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8101 **/
8102static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
8103{
8104 u8 i;
8105
8106 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
8107 if (pf->vxlan_ports[i] == port)
8108 return i;
8109 }
8110
8111 return i;
8112}
8113
8114/**
8115 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
8116 * @netdev: This physical port's netdev
8117 * @sa_family: Socket Family that VXLAN is notifying us about
8118 * @port: New UDP port number that VXLAN started listening to
8119 **/
8120static void i40e_add_vxlan_port(struct net_device *netdev,
8121 sa_family_t sa_family, __be16 port)
8122{
8123 struct i40e_netdev_priv *np = netdev_priv(netdev);
8124 struct i40e_vsi *vsi = np->vsi;
8125 struct i40e_pf *pf = vsi->back;
8126 u8 next_idx;
8127 u8 idx;
8128
8129 if (sa_family == AF_INET6)
8130 return;
8131
8132 idx = i40e_get_vxlan_port_idx(pf, port);
8133
8134 /* Check if port already exists */
8135 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008136 netdev_info(netdev, "vxlan port %d already offloaded\n",
8137 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008138 return;
8139 }
8140
8141 /* Now check if there is space to add the new port */
8142 next_idx = i40e_get_vxlan_port_idx(pf, 0);
8143
8144 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008145 netdev_info(netdev, "maximum number of vxlan UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008146 ntohs(port));
8147 return;
8148 }
8149
8150 /* New port: add it and mark its index in the bitmap */
8151 pf->vxlan_ports[next_idx] = port;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008152 pf->pending_vxlan_bitmap |= BIT_ULL(next_idx);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008153 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
8154}
8155
8156/**
8157 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
8158 * @netdev: This physical port's netdev
8159 * @sa_family: Socket Family that VXLAN is notifying us about
8160 * @port: UDP port number that VXLAN stopped listening to
8161 **/
8162static void i40e_del_vxlan_port(struct net_device *netdev,
8163 sa_family_t sa_family, __be16 port)
8164{
8165 struct i40e_netdev_priv *np = netdev_priv(netdev);
8166 struct i40e_vsi *vsi = np->vsi;
8167 struct i40e_pf *pf = vsi->back;
8168 u8 idx;
8169
8170 if (sa_family == AF_INET6)
8171 return;
8172
8173 idx = i40e_get_vxlan_port_idx(pf, port);
8174
8175 /* Check if port already exists */
8176 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8177 /* if port exists, set it to 0 (mark for deletion)
8178 * and make it pending
8179 */
8180 pf->vxlan_ports[idx] = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008181 pf->pending_vxlan_bitmap |= BIT_ULL(idx);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008182 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
8183 } else {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008184 netdev_warn(netdev, "vxlan port %d was not found, not deleting\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008185 ntohs(port));
8186 }
8187}
8188
8189#endif
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008190static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008191 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008192{
8193 struct i40e_netdev_priv *np = netdev_priv(netdev);
8194 struct i40e_pf *pf = np->vsi->back;
8195 struct i40e_hw *hw = &pf->hw;
8196
8197 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8198 return -EOPNOTSUPP;
8199
8200 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8201 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8202
8203 return 0;
8204}
8205
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008206/**
8207 * i40e_ndo_fdb_add - add an entry to the hardware database
8208 * @ndm: the input from the stack
8209 * @tb: pointer to array of nladdr (unused)
8210 * @dev: the net device pointer
8211 * @addr: the MAC address entry being added
8212 * @flags: instructions from stack about fdb operation
8213 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008214static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8215 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008216 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008217 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008218{
8219 struct i40e_netdev_priv *np = netdev_priv(dev);
8220 struct i40e_pf *pf = np->vsi->back;
8221 int err = 0;
8222
8223 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8224 return -EOPNOTSUPP;
8225
Or Gerlitz65891fe2014-12-14 18:19:05 +02008226 if (vid) {
8227 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8228 return -EINVAL;
8229 }
8230
Greg Rose4ba0dea2014-03-06 08:59:55 +00008231 /* Hardware does not support aging addresses so if a
8232 * ndm_state is given only allow permanent addresses
8233 */
8234 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
8235 netdev_info(dev, "FDB only supports static addresses\n");
8236 return -EINVAL;
8237 }
8238
8239 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
8240 err = dev_uc_add_excl(dev, addr);
8241 else if (is_multicast_ether_addr(addr))
8242 err = dev_mc_add_excl(dev, addr);
8243 else
8244 err = -EINVAL;
8245
8246 /* Only return duplicate errors if NLM_F_EXCL is set */
8247 if (err == -EEXIST && !(flags & NLM_F_EXCL))
8248 err = 0;
8249
8250 return err;
8251}
8252
Neerav Parikh51616012015-02-06 08:52:14 +00008253/**
8254 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
8255 * @dev: the netdev being configured
8256 * @nlh: RTNL message
8257 *
8258 * Inserts a new hardware bridge if not already created and
8259 * enables the bridging mode requested (VEB or VEPA). If the
8260 * hardware bridge has already been inserted and the request
8261 * is to change the mode then that requires a PF reset to
8262 * allow rebuild of the components with required hardware
8263 * bridge mode enabled.
8264 **/
8265static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04008266 struct nlmsghdr *nlh,
8267 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00008268{
8269 struct i40e_netdev_priv *np = netdev_priv(dev);
8270 struct i40e_vsi *vsi = np->vsi;
8271 struct i40e_pf *pf = vsi->back;
8272 struct i40e_veb *veb = NULL;
8273 struct nlattr *attr, *br_spec;
8274 int i, rem;
8275
8276 /* Only for PF VSI for now */
8277 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8278 return -EOPNOTSUPP;
8279
8280 /* Find the HW bridge for PF VSI */
8281 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8282 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8283 veb = pf->veb[i];
8284 }
8285
8286 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8287
8288 nla_for_each_nested(attr, br_spec, rem) {
8289 __u16 mode;
8290
8291 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8292 continue;
8293
8294 mode = nla_get_u16(attr);
8295 if ((mode != BRIDGE_MODE_VEPA) &&
8296 (mode != BRIDGE_MODE_VEB))
8297 return -EINVAL;
8298
8299 /* Insert a new HW bridge */
8300 if (!veb) {
8301 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8302 vsi->tc_config.enabled_tc);
8303 if (veb) {
8304 veb->bridge_mode = mode;
8305 i40e_config_bridge_mode(veb);
8306 } else {
8307 /* No Bridge HW offload available */
8308 return -ENOENT;
8309 }
8310 break;
8311 } else if (mode != veb->bridge_mode) {
8312 /* Existing HW bridge but different mode needs reset */
8313 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008314 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
8315 if (mode == BRIDGE_MODE_VEB)
8316 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
8317 else
8318 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
8319 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00008320 break;
8321 }
8322 }
8323
8324 return 0;
8325}
8326
8327/**
8328 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
8329 * @skb: skb buff
8330 * @pid: process id
8331 * @seq: RTNL message seq #
8332 * @dev: the netdev being configured
8333 * @filter_mask: unused
8334 *
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,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07008340 u32 filter_mask, int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00008341{
8342 struct i40e_netdev_priv *np = netdev_priv(dev);
8343 struct i40e_vsi *vsi = np->vsi;
8344 struct i40e_pf *pf = vsi->back;
8345 struct i40e_veb *veb = NULL;
8346 int i;
8347
8348 /* Only for PF VSI for now */
8349 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8350 return -EOPNOTSUPP;
8351
8352 /* Find the HW bridge for the PF VSI */
8353 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8354 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8355 veb = pf->veb[i];
8356 }
8357
8358 if (!veb)
8359 return 0;
8360
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02008361 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07008362 nlflags, 0, 0, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00008363}
Neerav Parikh51616012015-02-06 08:52:14 +00008364
Joe Stringerf44a75e2015-04-14 17:09:14 -07008365#define I40E_MAX_TUNNEL_HDR_LEN 80
8366/**
8367 * i40e_features_check - Validate encapsulated packet conforms to limits
8368 * @skb: skb buff
8369 * @netdev: This physical port's netdev
8370 * @features: Offload features that the stack believes apply
8371 **/
8372static netdev_features_t i40e_features_check(struct sk_buff *skb,
8373 struct net_device *dev,
8374 netdev_features_t features)
8375{
8376 if (skb->encapsulation &&
8377 (skb_inner_mac_header(skb) - skb_transport_header(skb) >
8378 I40E_MAX_TUNNEL_HDR_LEN))
8379 return features & ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
8380
8381 return features;
8382}
8383
Shannon Nelson37a29732015-02-27 09:15:19 +00008384static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008385 .ndo_open = i40e_open,
8386 .ndo_stop = i40e_close,
8387 .ndo_start_xmit = i40e_lan_xmit_frame,
8388 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
8389 .ndo_set_rx_mode = i40e_set_rx_mode,
8390 .ndo_validate_addr = eth_validate_addr,
8391 .ndo_set_mac_address = i40e_set_mac,
8392 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008393 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008394 .ndo_tx_timeout = i40e_tx_timeout,
8395 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
8396 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
8397#ifdef CONFIG_NET_POLL_CONTROLLER
8398 .ndo_poll_controller = i40e_netpoll,
8399#endif
8400 .ndo_setup_tc = i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07008401#ifdef I40E_FCOE
8402 .ndo_fcoe_enable = i40e_fcoe_enable,
8403 .ndo_fcoe_disable = i40e_fcoe_disable,
8404#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008405 .ndo_set_features = i40e_set_features,
8406 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
8407 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04008408 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008409 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00008410 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00008411 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008412#ifdef CONFIG_I40E_VXLAN
8413 .ndo_add_vxlan_port = i40e_add_vxlan_port,
8414 .ndo_del_vxlan_port = i40e_del_vxlan_port,
8415#endif
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008416 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008417 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07008418 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00008419 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
8420 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008421};
8422
8423/**
8424 * i40e_config_netdev - Setup the netdev flags
8425 * @vsi: the VSI being configured
8426 *
8427 * Returns 0 on success, negative value on failure
8428 **/
8429static int i40e_config_netdev(struct i40e_vsi *vsi)
8430{
Greg Rose1a103702013-11-28 06:42:39 +00008431 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008432 struct i40e_pf *pf = vsi->back;
8433 struct i40e_hw *hw = &pf->hw;
8434 struct i40e_netdev_priv *np;
8435 struct net_device *netdev;
8436 u8 mac_addr[ETH_ALEN];
8437 int etherdev_size;
8438
8439 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008440 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008441 if (!netdev)
8442 return -ENOMEM;
8443
8444 vsi->netdev = netdev;
8445 np = netdev_priv(netdev);
8446 np->vsi = vsi;
8447
Or Gerlitzd70e9412014-03-18 10:36:45 +02008448 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008449 NETIF_F_GSO_UDP_TUNNEL |
Or Gerlitzd70e9412014-03-18 10:36:45 +02008450 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008451
8452 netdev->features = NETIF_F_SG |
8453 NETIF_F_IP_CSUM |
8454 NETIF_F_SCTP_CSUM |
8455 NETIF_F_HIGHDMA |
8456 NETIF_F_GSO_UDP_TUNNEL |
8457 NETIF_F_HW_VLAN_CTAG_TX |
8458 NETIF_F_HW_VLAN_CTAG_RX |
8459 NETIF_F_HW_VLAN_CTAG_FILTER |
8460 NETIF_F_IPV6_CSUM |
8461 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00008462 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008463 NETIF_F_TSO6 |
8464 NETIF_F_RXCSUM |
8465 NETIF_F_RXHASH |
8466 0;
8467
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00008468 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
8469 netdev->features |= NETIF_F_NTUPLE;
8470
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008471 /* copy netdev features into list of user selectable features */
8472 netdev->hw_features |= netdev->features;
8473
8474 if (vsi->type == I40E_VSI_MAIN) {
8475 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00008476 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Shannon Nelson30650cc2014-07-29 04:01:50 +00008477 /* The following steps are necessary to prevent reception
8478 * of tagged packets - some older NVM configurations load a
8479 * default a MAC-VLAN filter that accepts any tagged packet
8480 * which must be replaced by a normal filter.
Greg Rose8c27d422014-05-22 06:31:56 +00008481 */
Shannon Nelson30650cc2014-07-29 04:01:50 +00008482 if (!i40e_rm_default_mac_filter(vsi, mac_addr))
8483 i40e_add_filter(vsi, mac_addr,
8484 I40E_VLAN_ANY, false, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008485 } else {
8486 /* relate the VSI_VMDQ name to the VSI_MAIN name */
8487 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
8488 pf->vsi[pf->lan_vsi]->netdev->name);
8489 random_ether_addr(mac_addr);
8490 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
8491 }
Greg Rose1a103702013-11-28 06:42:39 +00008492 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008493
Greg Rose9a173902014-05-22 06:32:02 +00008494 ether_addr_copy(netdev->dev_addr, mac_addr);
8495 ether_addr_copy(netdev->perm_addr, mac_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008496 /* vlan gets same features (except vlan offload)
8497 * after any tweaks for specific VSI types
8498 */
8499 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
8500 NETIF_F_HW_VLAN_CTAG_RX |
8501 NETIF_F_HW_VLAN_CTAG_FILTER);
8502 netdev->priv_flags |= IFF_UNICAST_FLT;
8503 netdev->priv_flags |= IFF_SUPP_NOFCS;
8504 /* Setup netdev TC information */
8505 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
8506
8507 netdev->netdev_ops = &i40e_netdev_ops;
8508 netdev->watchdog_timeo = 5 * HZ;
8509 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07008510#ifdef I40E_FCOE
8511 i40e_fcoe_config_netdev(netdev, vsi);
8512#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008513
8514 return 0;
8515}
8516
8517/**
8518 * i40e_vsi_delete - Delete a VSI from the switch
8519 * @vsi: the VSI being removed
8520 *
8521 * Returns 0 on success, negative value on failure
8522 **/
8523static void i40e_vsi_delete(struct i40e_vsi *vsi)
8524{
8525 /* remove default VSI is not allowed */
8526 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
8527 return;
8528
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008529 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008530}
8531
8532/**
Neerav Parikh51616012015-02-06 08:52:14 +00008533 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
8534 * @vsi: the VSI being queried
8535 *
8536 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
8537 **/
8538int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
8539{
8540 struct i40e_veb *veb;
8541 struct i40e_pf *pf = vsi->back;
8542
8543 /* Uplink is not a bridge so default to VEB */
8544 if (vsi->veb_idx == I40E_NO_VEB)
8545 return 1;
8546
8547 veb = pf->veb[vsi->veb_idx];
8548 /* Uplink is a bridge in VEPA mode */
8549 if (veb && (veb->bridge_mode & BRIDGE_MODE_VEPA))
8550 return 0;
8551
8552 /* Uplink is a bridge in VEB mode */
8553 return 1;
8554}
8555
8556/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008557 * i40e_add_vsi - Add a VSI to the switch
8558 * @vsi: the VSI being configured
8559 *
8560 * This initializes a VSI context depending on the VSI type to be added and
8561 * passes it down to the add_vsi aq command.
8562 **/
8563static int i40e_add_vsi(struct i40e_vsi *vsi)
8564{
8565 int ret = -ENODEV;
8566 struct i40e_mac_filter *f, *ftmp;
8567 struct i40e_pf *pf = vsi->back;
8568 struct i40e_hw *hw = &pf->hw;
8569 struct i40e_vsi_context ctxt;
8570 u8 enabled_tc = 0x1; /* TC0 enabled */
8571 int f_count = 0;
8572
8573 memset(&ctxt, 0, sizeof(ctxt));
8574 switch (vsi->type) {
8575 case I40E_VSI_MAIN:
8576 /* The PF's main VSI is already setup as part of the
8577 * device initialization, so we'll not bother with
8578 * the add_vsi call, but we will retrieve the current
8579 * VSI context.
8580 */
8581 ctxt.seid = pf->main_vsi_seid;
8582 ctxt.pf_num = pf->hw.pf_id;
8583 ctxt.vf_num = 0;
8584 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8585 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8586 if (ret) {
8587 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008588 "couldn't get PF vsi config, err %s aq_err %s\n",
8589 i40e_stat_str(&pf->hw, ret),
8590 i40e_aq_str(&pf->hw,
8591 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008592 return -ENOENT;
8593 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07008594 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008595 vsi->info.valid_sections = 0;
8596
8597 vsi->seid = ctxt.seid;
8598 vsi->id = ctxt.vsi_number;
8599
8600 enabled_tc = i40e_pf_get_tc_map(pf);
8601
8602 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00008603 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
8604 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008605 memset(&ctxt, 0, sizeof(ctxt));
8606 ctxt.seid = pf->main_vsi_seid;
8607 ctxt.pf_num = pf->hw.pf_id;
8608 ctxt.vf_num = 0;
8609 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
8610 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
8611 if (ret) {
8612 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008613 "update vsi failed, err %s aq_err %s\n",
8614 i40e_stat_str(&pf->hw, ret),
8615 i40e_aq_str(&pf->hw,
8616 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008617 ret = -ENOENT;
8618 goto err;
8619 }
8620 /* update the local VSI info queue map */
8621 i40e_vsi_update_queue_map(vsi, &ctxt);
8622 vsi->info.valid_sections = 0;
8623 } else {
8624 /* Default/Main VSI is only enabled for TC0
8625 * reconfigure it to enable all TCs that are
8626 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00008627 * For MFP case the iSCSI PF would use this
8628 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008629 */
8630 ret = i40e_vsi_config_tc(vsi, enabled_tc);
8631 if (ret) {
8632 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008633 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
8634 enabled_tc,
8635 i40e_stat_str(&pf->hw, ret),
8636 i40e_aq_str(&pf->hw,
8637 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008638 ret = -ENOENT;
8639 }
8640 }
8641 break;
8642
8643 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008644 ctxt.pf_num = hw->pf_id;
8645 ctxt.vf_num = 0;
8646 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00008647 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008648 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008649 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
8650 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00008651 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008652 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00008653 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008654 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00008655 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008656 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008657 break;
8658
8659 case I40E_VSI_VMDQ2:
8660 ctxt.pf_num = hw->pf_id;
8661 ctxt.vf_num = 0;
8662 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00008663 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008664 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
8665
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008666 /* This VSI is connected to VEB so the switch_id
8667 * should be set to zero by default.
8668 */
Neerav Parikh51616012015-02-06 08:52:14 +00008669 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
8670 ctxt.info.valid_sections |=
8671 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8672 ctxt.info.switch_id =
8673 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8674 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008675
8676 /* Setup the VSI tx/rx queue map for TC0 only for now */
8677 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
8678 break;
8679
8680 case I40E_VSI_SRIOV:
8681 ctxt.pf_num = hw->pf_id;
8682 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
8683 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00008684 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008685 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
8686
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008687 /* This VSI is connected to VEB so the switch_id
8688 * should be set to zero by default.
8689 */
Neerav Parikh51616012015-02-06 08:52:14 +00008690 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
8691 ctxt.info.valid_sections |=
8692 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8693 ctxt.info.switch_id =
8694 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8695 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008696
8697 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
8698 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00008699 if (pf->vf[vsi->vf_id].spoofchk) {
8700 ctxt.info.valid_sections |=
8701 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
8702 ctxt.info.sec_flags |=
8703 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
8704 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
8705 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008706 /* Setup the VSI tx/rx queue map for TC0 only for now */
8707 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
8708 break;
8709
Vasu Dev38e00432014-08-01 13:27:03 -07008710#ifdef I40E_FCOE
8711 case I40E_VSI_FCOE:
8712 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
8713 if (ret) {
8714 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
8715 return ret;
8716 }
8717 break;
8718
8719#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008720 default:
8721 return -ENODEV;
8722 }
8723
8724 if (vsi->type != I40E_VSI_MAIN) {
8725 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
8726 if (ret) {
8727 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008728 "add vsi failed, err %s aq_err %s\n",
8729 i40e_stat_str(&pf->hw, ret),
8730 i40e_aq_str(&pf->hw,
8731 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008732 ret = -ENOENT;
8733 goto err;
8734 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07008735 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008736 vsi->info.valid_sections = 0;
8737 vsi->seid = ctxt.seid;
8738 vsi->id = ctxt.vsi_number;
8739 }
8740
8741 /* If macvlan filters already exist, force them to get loaded */
8742 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
8743 f->changed = true;
8744 f_count++;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00008745
8746 if (f->is_laa && vsi->type == I40E_VSI_MAIN) {
Shannon Nelson30650cc2014-07-29 04:01:50 +00008747 struct i40e_aqc_remove_macvlan_element_data element;
8748
8749 memset(&element, 0, sizeof(element));
8750 ether_addr_copy(element.mac_addr, f->macaddr);
8751 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
8752 ret = i40e_aq_remove_macvlan(hw, vsi->seid,
8753 &element, 1, NULL);
8754 if (ret) {
8755 /* some older FW has a different default */
8756 element.flags |=
8757 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
8758 i40e_aq_remove_macvlan(hw, vsi->seid,
8759 &element, 1, NULL);
8760 }
8761
8762 i40e_aq_mac_address_write(hw,
Shannon Nelson6252c7e2014-06-04 01:23:23 +00008763 I40E_AQC_WRITE_TYPE_LAA_WOL,
8764 f->macaddr, NULL);
8765 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008766 }
8767 if (f_count) {
8768 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
8769 pf->flags |= I40E_FLAG_FILTER_SYNC;
8770 }
8771
8772 /* Update VSI BW information */
8773 ret = i40e_vsi_get_bw_info(vsi);
8774 if (ret) {
8775 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008776 "couldn't get vsi bw info, err %s aq_err %s\n",
8777 i40e_stat_str(&pf->hw, ret),
8778 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008779 /* VSI is already added so not tearing that up */
8780 ret = 0;
8781 }
8782
8783err:
8784 return ret;
8785}
8786
8787/**
8788 * i40e_vsi_release - Delete a VSI and free its resources
8789 * @vsi: the VSI being removed
8790 *
8791 * Returns 0 on success or < 0 on error
8792 **/
8793int i40e_vsi_release(struct i40e_vsi *vsi)
8794{
8795 struct i40e_mac_filter *f, *ftmp;
8796 struct i40e_veb *veb = NULL;
8797 struct i40e_pf *pf;
8798 u16 uplink_seid;
8799 int i, n;
8800
8801 pf = vsi->back;
8802
8803 /* release of a VEB-owner or last VSI is not allowed */
8804 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
8805 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
8806 vsi->seid, vsi->uplink_seid);
8807 return -ENODEV;
8808 }
8809 if (vsi == pf->vsi[pf->lan_vsi] &&
8810 !test_bit(__I40E_DOWN, &pf->state)) {
8811 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
8812 return -ENODEV;
8813 }
8814
8815 uplink_seid = vsi->uplink_seid;
8816 if (vsi->type != I40E_VSI_SRIOV) {
8817 if (vsi->netdev_registered) {
8818 vsi->netdev_registered = false;
8819 if (vsi->netdev) {
8820 /* results in a call to i40e_close() */
8821 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008822 }
8823 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00008824 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008825 }
8826 i40e_vsi_disable_irq(vsi);
8827 }
8828
8829 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
8830 i40e_del_filter(vsi, f->macaddr, f->vlan,
8831 f->is_vf, f->is_netdev);
Anjali Singhai30e25612015-09-28 13:37:12 -07008832 i40e_sync_vsi_filters(vsi, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008833
8834 i40e_vsi_delete(vsi);
8835 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00008836 if (vsi->netdev) {
8837 free_netdev(vsi->netdev);
8838 vsi->netdev = NULL;
8839 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008840 i40e_vsi_clear_rings(vsi);
8841 i40e_vsi_clear(vsi);
8842
8843 /* If this was the last thing on the VEB, except for the
8844 * controlling VSI, remove the VEB, which puts the controlling
8845 * VSI onto the next level down in the switch.
8846 *
8847 * Well, okay, there's one more exception here: don't remove
8848 * the orphan VEBs yet. We'll wait for an explicit remove request
8849 * from up the network stack.
8850 */
Mitch Williams505682c2014-05-20 08:01:37 +00008851 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008852 if (pf->vsi[i] &&
8853 pf->vsi[i]->uplink_seid == uplink_seid &&
8854 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
8855 n++; /* count the VSIs */
8856 }
8857 }
8858 for (i = 0; i < I40E_MAX_VEB; i++) {
8859 if (!pf->veb[i])
8860 continue;
8861 if (pf->veb[i]->uplink_seid == uplink_seid)
8862 n++; /* count the VEBs */
8863 if (pf->veb[i]->seid == uplink_seid)
8864 veb = pf->veb[i];
8865 }
8866 if (n == 0 && veb && veb->uplink_seid != 0)
8867 i40e_veb_release(veb);
8868
8869 return 0;
8870}
8871
8872/**
8873 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
8874 * @vsi: ptr to the VSI
8875 *
8876 * This should only be called after i40e_vsi_mem_alloc() which allocates the
8877 * corresponding SW VSI structure and initializes num_queue_pairs for the
8878 * newly allocated VSI.
8879 *
8880 * Returns 0 on success or negative on failure
8881 **/
8882static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
8883{
8884 int ret = -ENOENT;
8885 struct i40e_pf *pf = vsi->back;
8886
Alexander Duyck493fb302013-09-28 07:01:44 +00008887 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008888 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
8889 vsi->seid);
8890 return -EEXIST;
8891 }
8892
8893 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00008894 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008895 vsi->seid, vsi->base_vector);
8896 return -EEXIST;
8897 }
8898
Greg Rose90e04072014-03-06 08:59:57 +00008899 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008900 if (ret) {
8901 dev_info(&pf->pdev->dev,
8902 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
8903 vsi->num_q_vectors, vsi->seid, ret);
8904 vsi->num_q_vectors = 0;
8905 goto vector_setup_out;
8906 }
8907
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04008908 /* In Legacy mode, we do not have to get any other vector since we
8909 * piggyback on the misc/ICR0 for queue interrupts.
8910 */
8911 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8912 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00008913 if (vsi->num_q_vectors)
8914 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
8915 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008916 if (vsi->base_vector < 0) {
8917 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00008918 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
8919 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008920 i40e_vsi_free_q_vectors(vsi);
8921 ret = -ENOENT;
8922 goto vector_setup_out;
8923 }
8924
8925vector_setup_out:
8926 return ret;
8927}
8928
8929/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008930 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
8931 * @vsi: pointer to the vsi.
8932 *
8933 * This re-allocates a vsi's queue resources.
8934 *
8935 * Returns pointer to the successfully allocated and configured VSI sw struct
8936 * on success, otherwise returns NULL on failure.
8937 **/
8938static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
8939{
8940 struct i40e_pf *pf = vsi->back;
8941 u8 enabled_tc;
8942 int ret;
8943
8944 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
8945 i40e_vsi_clear_rings(vsi);
8946
8947 i40e_vsi_free_arrays(vsi, false);
8948 i40e_set_num_rings_in_vsi(vsi);
8949 ret = i40e_vsi_alloc_arrays(vsi, false);
8950 if (ret)
8951 goto err_vsi;
8952
8953 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
8954 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00008955 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008956 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00008957 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008958 goto err_vsi;
8959 }
8960 vsi->base_queue = ret;
8961
8962 /* Update the FW view of the VSI. Force a reset of TC and queue
8963 * layout configurations.
8964 */
8965 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
8966 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
8967 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
8968 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
8969
8970 /* assign it some queues */
8971 ret = i40e_alloc_rings(vsi);
8972 if (ret)
8973 goto err_rings;
8974
8975 /* map all of the rings to the q_vectors */
8976 i40e_vsi_map_rings_to_vectors(vsi);
8977 return vsi;
8978
8979err_rings:
8980 i40e_vsi_free_q_vectors(vsi);
8981 if (vsi->netdev_registered) {
8982 vsi->netdev_registered = false;
8983 unregister_netdev(vsi->netdev);
8984 free_netdev(vsi->netdev);
8985 vsi->netdev = NULL;
8986 }
8987 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
8988err_vsi:
8989 i40e_vsi_clear(vsi);
8990 return NULL;
8991}
8992
8993/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008994 * i40e_vsi_setup - Set up a VSI by a given type
8995 * @pf: board private structure
8996 * @type: VSI type
8997 * @uplink_seid: the switch element to link to
8998 * @param1: usage depends upon VSI type. For VF types, indicates VF id
8999 *
9000 * This allocates the sw VSI structure and its queue resources, then add a VSI
9001 * to the identified VEB.
9002 *
9003 * Returns pointer to the successfully allocated and configure VSI sw struct on
9004 * success, otherwise returns NULL on failure.
9005 **/
9006struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9007 u16 uplink_seid, u32 param1)
9008{
9009 struct i40e_vsi *vsi = NULL;
9010 struct i40e_veb *veb = NULL;
9011 int ret, i;
9012 int v_idx;
9013
9014 /* The requested uplink_seid must be either
9015 * - the PF's port seid
9016 * no VEB is needed because this is the PF
9017 * or this is a Flow Director special case VSI
9018 * - seid of an existing VEB
9019 * - seid of a VSI that owns an existing VEB
9020 * - seid of a VSI that doesn't own a VEB
9021 * a new VEB is created and the VSI becomes the owner
9022 * - seid of the PF VSI, which is what creates the first VEB
9023 * this is a special case of the previous
9024 *
9025 * Find which uplink_seid we were given and create a new VEB if needed
9026 */
9027 for (i = 0; i < I40E_MAX_VEB; i++) {
9028 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9029 veb = pf->veb[i];
9030 break;
9031 }
9032 }
9033
9034 if (!veb && uplink_seid != pf->mac_seid) {
9035
Mitch Williams505682c2014-05-20 08:01:37 +00009036 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009037 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9038 vsi = pf->vsi[i];
9039 break;
9040 }
9041 }
9042 if (!vsi) {
9043 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9044 uplink_seid);
9045 return NULL;
9046 }
9047
9048 if (vsi->uplink_seid == pf->mac_seid)
9049 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9050 vsi->tc_config.enabled_tc);
9051 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9052 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9053 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009054 if (veb) {
9055 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9056 dev_info(&vsi->back->pdev->dev,
9057 "%s: New VSI creation error, uplink seid of LAN VSI expected.\n",
9058 __func__);
9059 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;
9708 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9709 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9710 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9711 }
9712 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
9713
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009714 i40e_fdir_sb_setup(pf);
9715
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009716 /* Setup static PF queue filter control settings */
9717 ret = i40e_setup_pf_filter_control(pf);
9718 if (ret) {
9719 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
9720 ret);
9721 /* Failure here should not stop continuing other steps */
9722 }
9723
9724 /* enable RSS in the HW, even for only one queue, as the stack can use
9725 * the hash
9726 */
9727 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
9728 i40e_config_rss(pf);
9729
9730 /* fill in link information and enable LSE reporting */
Catherine Sullivan21af70f2015-01-24 09:58:41 +00009731 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009732 i40e_link_event(pf);
9733
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00009734 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009735 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
9736 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00009737
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009738 i40e_ptp_init(pf);
9739
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009740 return ret;
9741}
9742
9743/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009744 * i40e_determine_queue_usage - Work out queue distribution
9745 * @pf: board private structure
9746 **/
9747static void i40e_determine_queue_usage(struct i40e_pf *pf)
9748{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009749 int queues_left;
9750
9751 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07009752#ifdef I40E_FCOE
9753 pf->num_fcoe_qps = 0;
9754#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009755
9756 /* Find the max queues to be put into basic use. We'll always be
9757 * using TC0, whether or not DCB is running, and TC0 will get the
9758 * big RSS set.
9759 */
9760 queues_left = pf->hw.func_caps.num_tx_qp;
9761
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009762 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +00009763 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009764 /* one qp for PF, no queues for anything else */
9765 queues_left = 0;
9766 pf->rss_size = pf->num_lan_qps = 1;
9767
9768 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009769 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07009770#ifdef I40E_FCOE
9771 I40E_FLAG_FCOE_ENABLED |
9772#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009773 I40E_FLAG_FD_SB_ENABLED |
9774 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009775 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009776 I40E_FLAG_SRIOV_ENABLED |
9777 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +00009778 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
9779 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +00009780 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009781 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +00009782 /* one qp for PF */
9783 pf->rss_size = pf->num_lan_qps = 1;
9784 queues_left -= pf->num_lan_qps;
9785
9786 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07009787#ifdef I40E_FCOE
9788 I40E_FLAG_FCOE_ENABLED |
9789#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +00009790 I40E_FLAG_FD_SB_ENABLED |
9791 I40E_FLAG_FD_ATR_ENABLED |
9792 I40E_FLAG_DCB_ENABLED |
9793 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009794 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009795 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009796 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009797 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009798 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009799 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
9800 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00009801 pf->num_lan_qps = max_t(int, pf->rss_size_max,
9802 num_online_cpus());
9803 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
9804 pf->hw.func_caps.num_tx_qp);
9805
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009806 queues_left -= pf->num_lan_qps;
9807 }
9808
Vasu Dev38e00432014-08-01 13:27:03 -07009809#ifdef I40E_FCOE
9810 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
9811 if (I40E_DEFAULT_FCOE <= queues_left) {
9812 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
9813 } else if (I40E_MINIMUM_FCOE <= queues_left) {
9814 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
9815 } else {
9816 pf->num_fcoe_qps = 0;
9817 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
9818 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
9819 }
9820
9821 queues_left -= pf->num_fcoe_qps;
9822 }
9823
9824#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009825 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9826 if (queues_left > 1) {
9827 queues_left -= 1; /* save 1 queue for FD */
9828 } else {
9829 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9830 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
9831 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009832 }
9833
9834 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
9835 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009836 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
9837 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009838 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
9839 }
9840
9841 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
9842 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
9843 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
9844 (queues_left / pf->num_vmdq_qps));
9845 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
9846 }
9847
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009848 pf->queues_left = queues_left;
Vasu Dev38e00432014-08-01 13:27:03 -07009849#ifdef I40E_FCOE
9850 dev_info(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
9851#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009852}
9853
9854/**
9855 * i40e_setup_pf_filter_control - Setup PF static filter control
9856 * @pf: PF to be setup
9857 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00009858 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009859 * settings. If PE/FCoE are enabled then it will also set the per PF
9860 * based filter sizes required for them. It also enables Flow director,
9861 * ethertype and macvlan type filter settings for the pf.
9862 *
9863 * Returns 0 on success, negative on failure
9864 **/
9865static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
9866{
9867 struct i40e_filter_control_settings *settings = &pf->filter_settings;
9868
9869 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
9870
9871 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009872 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009873 settings->enable_fdir = true;
9874
9875 /* Ethtype and MACVLAN filters enabled for PF */
9876 settings->enable_ethtype = true;
9877 settings->enable_macvlan = true;
9878
9879 if (i40e_set_filter_control(&pf->hw, settings))
9880 return -ENOENT;
9881
9882 return 0;
9883}
9884
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009885#define INFO_STRING_LEN 255
9886static void i40e_print_features(struct i40e_pf *pf)
9887{
9888 struct i40e_hw *hw = &pf->hw;
9889 char *buf, *string;
9890
9891 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
9892 if (!string) {
9893 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
9894 return;
9895 }
9896
9897 buf = string;
9898
9899 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
9900#ifdef CONFIG_PCI_IOV
9901 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
9902#endif
Mitch Williamsaba237d2015-02-06 08:52:17 +00009903 buf += sprintf(buf, "VSIs: %d QP: %d RX: %s ",
9904 pf->hw.func_caps.num_vsis,
9905 pf->vsi[pf->lan_vsi]->num_queue_pairs,
9906 pf->flags & I40E_FLAG_RX_PS_ENABLED ? "PS" : "1BUF");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009907
9908 if (pf->flags & I40E_FLAG_RSS_ENABLED)
9909 buf += sprintf(buf, "RSS ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009910 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00009911 buf += sprintf(buf, "FD_ATR ");
9912 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9913 buf += sprintf(buf, "FD_SB ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009914 buf += sprintf(buf, "NTUPLE ");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00009915 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009916 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009917 buf += sprintf(buf, "DCB ");
9918 if (pf->flags & I40E_FLAG_PTP)
9919 buf += sprintf(buf, "PTP ");
Vasu Dev38e00432014-08-01 13:27:03 -07009920#ifdef I40E_FCOE
9921 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
9922 buf += sprintf(buf, "FCOE ");
9923#endif
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009924
9925 BUG_ON(buf > (string + INFO_STRING_LEN));
9926 dev_info(&pf->pdev->dev, "%s\n", string);
9927 kfree(string);
9928}
9929
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009930/**
9931 * i40e_probe - Device initialization routine
9932 * @pdev: PCI device information struct
9933 * @ent: entry in i40e_pci_tbl
9934 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00009935 * i40e_probe initializes a PF identified by a pci_dev structure.
9936 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009937 * and a hardware reset occur.
9938 *
9939 * Returns 0 on success, negative on failure
9940 **/
9941static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
9942{
Catherine Sullivane8278452015-02-06 08:52:08 +00009943 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009944 struct i40e_pf *pf;
9945 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00009946 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -04009947 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00009948 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009949 int err = 0;
9950 u32 len;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00009951 u32 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009952
9953 err = pci_enable_device_mem(pdev);
9954 if (err)
9955 return err;
9956
9957 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +00009958 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00009959 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00009960 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
9961 if (err) {
9962 dev_err(&pdev->dev,
9963 "DMA configuration failed: 0x%x\n", err);
9964 goto err_dma;
9965 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009966 }
9967
9968 /* set up pci connections */
9969 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
9970 IORESOURCE_MEM), i40e_driver_name);
9971 if (err) {
9972 dev_info(&pdev->dev,
9973 "pci_request_selected_regions failed %d\n", err);
9974 goto err_pci_reg;
9975 }
9976
9977 pci_enable_pcie_error_reporting(pdev);
9978 pci_set_master(pdev);
9979
9980 /* Now that we have a PCI connection, we need to do the
9981 * low level device setup. This is primarily setting up
9982 * the Admin Queue structures and then querying for the
9983 * device's current profile information.
9984 */
9985 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
9986 if (!pf) {
9987 err = -ENOMEM;
9988 goto err_pf_alloc;
9989 }
9990 pf->next_vsi = 0;
9991 pf->pdev = pdev;
9992 set_bit(__I40E_DOWN, &pf->state);
9993
9994 hw = &pf->hw;
9995 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +00009996
Shannon Nelson2ac8b672015-07-23 16:54:37 -04009997 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
9998 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +00009999
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010000 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010001 if (!hw->hw_addr) {
10002 err = -EIO;
10003 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10004 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010005 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010006 goto err_ioremap;
10007 }
10008 hw->vendor_id = pdev->vendor;
10009 hw->device_id = pdev->device;
10010 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10011 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10012 hw->subsystem_device_id = pdev->subsystem_device;
10013 hw->bus.device = PCI_SLOT(pdev->devfn);
10014 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010015 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010016
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010017 if (debug != -1) {
10018 pf->msg_enable = pf->hw.debug_mask;
10019 pf->msg_enable = debug;
10020 }
10021
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010022 /* do a special CORER for clearing PXE mode once at init */
10023 if (hw->revision_id == 0 &&
10024 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10025 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10026 i40e_flush(hw);
10027 msleep(200);
10028 pf->corer_count++;
10029
10030 i40e_clear_pxe_mode(hw);
10031 }
10032
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010033 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010034 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010035 err = i40e_pf_reset(hw);
10036 if (err) {
10037 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10038 goto err_pf_reset;
10039 }
10040 pf->pfr_count++;
10041
10042 hw->aq.num_arq_entries = I40E_AQ_LEN;
10043 hw->aq.num_asq_entries = I40E_AQ_LEN;
10044 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10045 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10046 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010047
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010048 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010049 "%s-%s:misc",
10050 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010051
10052 err = i40e_init_shared_code(hw);
10053 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010054 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10055 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010056 goto err_pf_reset;
10057 }
10058
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010059 /* set up a default setting for link flow control */
10060 pf->hw.fc.requested_mode = I40E_FC_NONE;
10061
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010062 err = i40e_init_adminq(hw);
10063 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
10064 if (err) {
10065 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010066 "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 +000010067 goto err_pf_reset;
10068 }
10069
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010070 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10071 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010072 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010073 "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");
10074 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
10075 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000010076 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010077 "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 +000010078
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010079 i40e_verify_eeprom(pf);
10080
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000010081 /* Rev 0 hardware was never productized */
10082 if (hw->revision_id < 1)
10083 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");
10084
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000010085 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010086 err = i40e_get_capabilities(pf);
10087 if (err)
10088 goto err_adminq_setup;
10089
10090 err = i40e_sw_init(pf);
10091 if (err) {
10092 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
10093 goto err_sw_init;
10094 }
10095
10096 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10097 hw->func_caps.num_rx_qp,
10098 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
10099 if (err) {
10100 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
10101 goto err_init_lan_hmc;
10102 }
10103
10104 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10105 if (err) {
10106 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
10107 err = -ENOENT;
10108 goto err_configure_lan_hmc;
10109 }
10110
Neerav Parikhb686ece2014-12-14 01:55:11 +000010111 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
10112 * Ignore error return codes because if it was already disabled via
10113 * hardware settings this will fail
10114 */
10115 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
10116 (pf->hw.aq.fw_maj_ver < 4)) {
10117 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
10118 i40e_aq_stop_lldp(hw, true, NULL);
10119 }
10120
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010121 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000010122 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010123 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
10124 err = -EIO;
10125 goto err_mac_addr;
10126 }
10127 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000010128 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010129 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
10130 if (is_valid_ether_addr(hw->mac.port_addr))
10131 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070010132#ifdef I40E_FCOE
10133 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
10134 if (err)
10135 dev_info(&pdev->dev,
10136 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
10137 if (!is_valid_ether_addr(hw->mac.san_addr)) {
10138 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
10139 hw->mac.san_addr);
10140 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
10141 }
10142 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
10143#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010144
10145 pci_set_drvdata(pdev, pf);
10146 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010147#ifdef CONFIG_I40E_DCB
10148 err = i40e_init_pf_dcb(pf);
10149 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000010150 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010151 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Neerav Parikh014269f2014-04-01 07:11:48 +000010152 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010153 }
10154#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010155
10156 /* set up periodic task facility */
10157 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
10158 pf->service_timer_period = HZ;
10159
10160 INIT_WORK(&pf->service_task, i40e_service_task);
10161 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
10162 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010163
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010164 /* NVM bit on means WoL disabled for the port */
10165 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
10166 if ((1 << hw->port) & wol_nvm_bits || hw->partition_id != 1)
10167 pf->wol_en = false;
10168 else
10169 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010170 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
10171
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010172 /* set up the main switch operations */
10173 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010174 err = i40e_init_interrupt_scheme(pf);
10175 if (err)
10176 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010177
Mitch Williams505682c2014-05-20 08:01:37 +000010178 /* The number of VSIs reported by the FW is the minimum guaranteed
10179 * to us; HW supports far more and we share the remaining pool with
10180 * the other PFs. We allocate space for more than the guarantee with
10181 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010182 */
Mitch Williams505682c2014-05-20 08:01:37 +000010183 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
10184 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
10185 else
10186 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
10187
10188 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
10189 len = sizeof(struct i40e_vsi *) * pf->num_alloc_vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010190 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000010191 if (!pf->vsi) {
10192 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010193 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000010194 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010195
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010196#ifdef CONFIG_PCI_IOV
10197 /* prep for VF support */
10198 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10199 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10200 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
10201 if (pci_num_vf(pdev))
10202 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
10203 }
10204#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010205 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010206 if (err) {
10207 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
10208 goto err_vsis;
10209 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010210 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000010211 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010212 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
10213 i40e_vsi_open(pf->vsi[i]);
10214 break;
10215 }
10216 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010217
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000010218 /* driver is only interested in link up/down and module qualification
10219 * reports from firmware
10220 */
10221 err = i40e_aq_set_phy_int_mask(&pf->hw,
10222 I40E_AQ_EVENT_LINK_UPDOWN |
10223 I40E_AQ_EVENT_MODULE_QUAL_FAIL, NULL);
10224 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010225 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10226 i40e_stat_str(&pf->hw, err),
10227 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000010228
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000010229 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
10230 (pf->hw.aq.fw_maj_ver < 4)) {
10231 msleep(75);
10232 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10233 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010234 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10235 i40e_stat_str(&pf->hw, err),
10236 i40e_aq_str(&pf->hw,
10237 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000010238 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010239 /* The main driver is (mostly) up and happy. We need to set this state
10240 * before setting up the misc vector or we get a race and the vector
10241 * ends up disabled forever.
10242 */
10243 clear_bit(__I40E_DOWN, &pf->state);
10244
10245 /* In case of MSIX we are going to setup the misc vector right here
10246 * to handle admin queue events etc. In case of legacy and MSI
10247 * the misc functionality and queue processing is combined in
10248 * the same vector and that gets setup at open.
10249 */
10250 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
10251 err = i40e_setup_misc_vector(pf);
10252 if (err) {
10253 dev_info(&pdev->dev,
10254 "setup of misc vector failed: %d\n", err);
10255 goto err_vsis;
10256 }
10257 }
10258
Greg Rosedf805f62014-04-04 04:43:16 +000010259#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010260 /* prep for VF support */
10261 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010262 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10263 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010264 u32 val;
10265
10266 /* disable link interrupts for VFs */
10267 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
10268 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
10269 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
10270 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080010271
10272 if (pci_num_vf(pdev)) {
10273 dev_info(&pdev->dev,
10274 "Active VFs found, allocating resources.\n");
10275 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
10276 if (err)
10277 dev_info(&pdev->dev,
10278 "Error %d allocating resources for existing VFs\n",
10279 err);
10280 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010281 }
Greg Rosedf805f62014-04-04 04:43:16 +000010282#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010283
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010284 pfs_found++;
10285
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010286 i40e_dbg_pf_init(pf);
10287
10288 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000010289 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010290
10291 /* since everything's happy, start the service_task timer */
10292 mod_timer(&pf->service_timer,
10293 round_jiffies(jiffies + pf->service_timer_period));
10294
Vasu Dev38e00432014-08-01 13:27:03 -070010295#ifdef I40E_FCOE
10296 /* create FCoE interface */
10297 i40e_fcoe_vsi_setup(pf);
10298
10299#endif
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010300 /* Get the negotiated link width and speed from PCI config space */
10301 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
10302
10303 i40e_set_pci_config_data(hw, link_status);
10304
Jesse Brandeburg69bfb112014-02-11 08:24:13 +000010305 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010306 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
10307 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
10308 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
10309 "Unknown"),
10310 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
10311 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
10312 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
10313 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
10314 "Unknown"));
10315
10316 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
10317 hw->bus.speed < i40e_bus_speed_8000) {
10318 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
10319 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
10320 }
10321
Catherine Sullivane8278452015-02-06 08:52:08 +000010322 /* get the requested speeds from the fw */
10323 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
10324 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010325 dev_info(&pf->pdev->dev,
10326 "get phy capabilities failed, err %s aq_err %s, advertised speed settings may not be correct\n",
10327 i40e_stat_str(&pf->hw, err),
10328 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000010329 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
10330
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010331 /* print a string summarizing features */
10332 i40e_print_features(pf);
10333
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010334 return 0;
10335
10336 /* Unwind what we've done if something failed in the setup */
10337err_vsis:
10338 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010339 i40e_clear_interrupt_scheme(pf);
10340 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000010341err_switch_setup:
10342 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010343 del_timer_sync(&pf->service_timer);
10344err_mac_addr:
10345err_configure_lan_hmc:
10346 (void)i40e_shutdown_lan_hmc(hw);
10347err_init_lan_hmc:
10348 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010349err_sw_init:
10350err_adminq_setup:
10351 (void)i40e_shutdown_adminq(hw);
10352err_pf_reset:
10353 iounmap(hw->hw_addr);
10354err_ioremap:
10355 kfree(pf);
10356err_pf_alloc:
10357 pci_disable_pcie_error_reporting(pdev);
10358 pci_release_selected_regions(pdev,
10359 pci_select_bars(pdev, IORESOURCE_MEM));
10360err_pci_reg:
10361err_dma:
10362 pci_disable_device(pdev);
10363 return err;
10364}
10365
10366/**
10367 * i40e_remove - Device removal routine
10368 * @pdev: PCI device information struct
10369 *
10370 * i40e_remove is called by the PCI subsystem to alert the driver
10371 * that is should release a PCI device. This could be caused by a
10372 * Hot-Plug event, or because the driver is going to be removed from
10373 * memory.
10374 **/
10375static void i40e_remove(struct pci_dev *pdev)
10376{
10377 struct i40e_pf *pf = pci_get_drvdata(pdev);
10378 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010379 int i;
10380
10381 i40e_dbg_pf_exit(pf);
10382
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010383 i40e_ptp_stop(pf);
10384
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010385 /* no more scheduling of any task */
10386 set_bit(__I40E_DOWN, &pf->state);
10387 del_timer_sync(&pf->service_timer);
10388 cancel_work_sync(&pf->service_task);
Mitch A Williams33c62b32015-02-21 06:44:51 +000010389 i40e_fdir_teardown(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010390
Mitch Williamseb2d80b2014-02-13 03:48:48 -080010391 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
10392 i40e_free_vfs(pf);
10393 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
10394 }
10395
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010396 i40e_fdir_teardown(pf);
10397
10398 /* If there is a switch structure or any orphans, remove them.
10399 * This will leave only the PF's VSI remaining.
10400 */
10401 for (i = 0; i < I40E_MAX_VEB; i++) {
10402 if (!pf->veb[i])
10403 continue;
10404
10405 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
10406 pf->veb[i]->uplink_seid == 0)
10407 i40e_switch_branch_release(pf->veb[i]);
10408 }
10409
10410 /* Now we can shutdown the PF's VSI, just before we kill
10411 * adminq and hmc.
10412 */
10413 if (pf->vsi[pf->lan_vsi])
10414 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
10415
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010416 /* shutdown and destroy the HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +000010417 if (pf->hw.hmc.hmc_obj) {
10418 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
10419 if (ret_code)
10420 dev_warn(&pdev->dev,
10421 "Failed to destroy the HMC resources: %d\n",
10422 ret_code);
10423 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010424
10425 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010426 ret_code = i40e_shutdown_adminq(&pf->hw);
10427 if (ret_code)
10428 dev_warn(&pdev->dev,
10429 "Failed to destroy the Admin Queue resources: %d\n",
10430 ret_code);
10431
10432 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
10433 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000010434 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010435 if (pf->vsi[i]) {
10436 i40e_vsi_clear_rings(pf->vsi[i]);
10437 i40e_vsi_clear(pf->vsi[i]);
10438 pf->vsi[i] = NULL;
10439 }
10440 }
10441
10442 for (i = 0; i < I40E_MAX_VEB; i++) {
10443 kfree(pf->veb[i]);
10444 pf->veb[i] = NULL;
10445 }
10446
10447 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010448 kfree(pf->vsi);
10449
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010450 iounmap(pf->hw.hw_addr);
10451 kfree(pf);
10452 pci_release_selected_regions(pdev,
10453 pci_select_bars(pdev, IORESOURCE_MEM));
10454
10455 pci_disable_pcie_error_reporting(pdev);
10456 pci_disable_device(pdev);
10457}
10458
10459/**
10460 * i40e_pci_error_detected - warning that something funky happened in PCI land
10461 * @pdev: PCI device information struct
10462 *
10463 * Called to warn that something happened and the error handling steps
10464 * are in progress. Allows the driver to quiesce things, be ready for
10465 * remediation.
10466 **/
10467static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
10468 enum pci_channel_state error)
10469{
10470 struct i40e_pf *pf = pci_get_drvdata(pdev);
10471
10472 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
10473
10474 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010475 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
10476 rtnl_lock();
10477 i40e_prep_for_reset(pf);
10478 rtnl_unlock();
10479 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010480
10481 /* Request a slot reset */
10482 return PCI_ERS_RESULT_NEED_RESET;
10483}
10484
10485/**
10486 * i40e_pci_error_slot_reset - a PCI slot reset just happened
10487 * @pdev: PCI device information struct
10488 *
10489 * Called to find if the driver can work with the device now that
10490 * the pci slot has been reset. If a basic connection seems good
10491 * (registers are readable and have sane content) then return a
10492 * happy little PCI_ERS_RESULT_xxx.
10493 **/
10494static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
10495{
10496 struct i40e_pf *pf = pci_get_drvdata(pdev);
10497 pci_ers_result_t result;
10498 int err;
10499 u32 reg;
10500
10501 dev_info(&pdev->dev, "%s\n", __func__);
10502 if (pci_enable_device_mem(pdev)) {
10503 dev_info(&pdev->dev,
10504 "Cannot re-enable PCI device after reset.\n");
10505 result = PCI_ERS_RESULT_DISCONNECT;
10506 } else {
10507 pci_set_master(pdev);
10508 pci_restore_state(pdev);
10509 pci_save_state(pdev);
10510 pci_wake_from_d3(pdev, false);
10511
10512 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
10513 if (reg == 0)
10514 result = PCI_ERS_RESULT_RECOVERED;
10515 else
10516 result = PCI_ERS_RESULT_DISCONNECT;
10517 }
10518
10519 err = pci_cleanup_aer_uncorrect_error_status(pdev);
10520 if (err) {
10521 dev_info(&pdev->dev,
10522 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
10523 err);
10524 /* non-fatal, continue */
10525 }
10526
10527 return result;
10528}
10529
10530/**
10531 * i40e_pci_error_resume - restart operations after PCI error recovery
10532 * @pdev: PCI device information struct
10533 *
10534 * Called to allow the driver to bring things back up after PCI error
10535 * and/or reset recovery has finished.
10536 **/
10537static void i40e_pci_error_resume(struct pci_dev *pdev)
10538{
10539 struct i40e_pf *pf = pci_get_drvdata(pdev);
10540
10541 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010542 if (test_bit(__I40E_SUSPENDED, &pf->state))
10543 return;
10544
10545 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010546 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030010547 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010548}
10549
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010550/**
10551 * i40e_shutdown - PCI callback for shutting down
10552 * @pdev: PCI device information struct
10553 **/
10554static void i40e_shutdown(struct pci_dev *pdev)
10555{
10556 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010557 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010558
10559 set_bit(__I40E_SUSPENDED, &pf->state);
10560 set_bit(__I40E_DOWN, &pf->state);
10561 rtnl_lock();
10562 i40e_prep_for_reset(pf);
10563 rtnl_unlock();
10564
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010565 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
10566 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
10567
Catherine Sullivan02b42492015-07-10 19:35:59 -040010568 del_timer_sync(&pf->service_timer);
10569 cancel_work_sync(&pf->service_task);
10570 i40e_fdir_teardown(pf);
10571
10572 rtnl_lock();
10573 i40e_prep_for_reset(pf);
10574 rtnl_unlock();
10575
10576 wr32(hw, I40E_PFPM_APM,
10577 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
10578 wr32(hw, I40E_PFPM_WUFC,
10579 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
10580
Shannon Nelsone1477582015-02-21 06:44:33 +000010581 i40e_clear_interrupt_scheme(pf);
10582
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010583 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010584 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010585 pci_set_power_state(pdev, PCI_D3hot);
10586 }
10587}
10588
10589#ifdef CONFIG_PM
10590/**
10591 * i40e_suspend - PCI callback for moving to D3
10592 * @pdev: PCI device information struct
10593 **/
10594static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
10595{
10596 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010597 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010598
10599 set_bit(__I40E_SUSPENDED, &pf->state);
10600 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070010601
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010602 rtnl_lock();
10603 i40e_prep_for_reset(pf);
10604 rtnl_unlock();
10605
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010606 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
10607 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
10608
10609 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010610 pci_set_power_state(pdev, PCI_D3hot);
10611
10612 return 0;
10613}
10614
10615/**
10616 * i40e_resume - PCI callback for waking up from D3
10617 * @pdev: PCI device information struct
10618 **/
10619static int i40e_resume(struct pci_dev *pdev)
10620{
10621 struct i40e_pf *pf = pci_get_drvdata(pdev);
10622 u32 err;
10623
10624 pci_set_power_state(pdev, PCI_D0);
10625 pci_restore_state(pdev);
10626 /* pci_restore_state() clears dev->state_saves, so
10627 * call pci_save_state() again to restore it.
10628 */
10629 pci_save_state(pdev);
10630
10631 err = pci_enable_device_mem(pdev);
10632 if (err) {
10633 dev_err(&pdev->dev,
10634 "%s: Cannot enable PCI device from suspend\n",
10635 __func__);
10636 return err;
10637 }
10638 pci_set_master(pdev);
10639
10640 /* no wakeup events while running */
10641 pci_wake_from_d3(pdev, false);
10642
10643 /* handling the reset will rebuild the device state */
10644 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
10645 clear_bit(__I40E_DOWN, &pf->state);
10646 rtnl_lock();
10647 i40e_reset_and_rebuild(pf, false);
10648 rtnl_unlock();
10649 }
10650
10651 return 0;
10652}
10653
10654#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010655static const struct pci_error_handlers i40e_err_handler = {
10656 .error_detected = i40e_pci_error_detected,
10657 .slot_reset = i40e_pci_error_slot_reset,
10658 .resume = i40e_pci_error_resume,
10659};
10660
10661static struct pci_driver i40e_driver = {
10662 .name = i40e_driver_name,
10663 .id_table = i40e_pci_tbl,
10664 .probe = i40e_probe,
10665 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010666#ifdef CONFIG_PM
10667 .suspend = i40e_suspend,
10668 .resume = i40e_resume,
10669#endif
10670 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010671 .err_handler = &i40e_err_handler,
10672 .sriov_configure = i40e_pci_sriov_configure,
10673};
10674
10675/**
10676 * i40e_init_module - Driver registration routine
10677 *
10678 * i40e_init_module is the first routine called when the driver is
10679 * loaded. All it does is register with the PCI subsystem.
10680 **/
10681static int __init i40e_init_module(void)
10682{
10683 pr_info("%s: %s - version %s\n", i40e_driver_name,
10684 i40e_driver_string, i40e_driver_version_str);
10685 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000010686
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010687 i40e_dbg_init();
10688 return pci_register_driver(&i40e_driver);
10689}
10690module_init(i40e_init_module);
10691
10692/**
10693 * i40e_exit_module - Driver exit cleanup routine
10694 *
10695 * i40e_exit_module is called just before the driver is removed
10696 * from memory.
10697 **/
10698static void __exit i40e_exit_module(void)
10699{
10700 pci_unregister_driver(&i40e_driver);
10701 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010702}
10703module_exit(i40e_exit_module);