blob: 52d7d8b8f1f97542f78147ce7ae5a4d4508015e8 [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 Sullivanf0290942015-04-16 20:06:12 -040042#define DRV_VERSION_BUILD 4
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},
Jesse Brandeburgae24b402015-03-27 00:12:09 -070078 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000079 /* required last entry */
80 {0, }
81};
82MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
83
84#define I40E_MAX_VF_COUNT 128
85static int debug = -1;
86module_param(debug, int, 0);
87MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
88
89MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
90MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
91MODULE_LICENSE("GPL");
92MODULE_VERSION(DRV_VERSION);
93
94/**
95 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
96 * @hw: pointer to the HW structure
97 * @mem: ptr to mem struct to fill out
98 * @size: size of memory requested
99 * @alignment: what to align the allocation to
100 **/
101int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
102 u64 size, u32 alignment)
103{
104 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
105
106 mem->size = ALIGN(size, alignment);
107 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
108 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000109 if (!mem->va)
110 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000111
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000112 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000113}
114
115/**
116 * i40e_free_dma_mem_d - OS specific memory free for shared code
117 * @hw: pointer to the HW structure
118 * @mem: ptr to mem struct to free
119 **/
120int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
121{
122 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
123
124 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
125 mem->va = NULL;
126 mem->pa = 0;
127 mem->size = 0;
128
129 return 0;
130}
131
132/**
133 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
134 * @hw: pointer to the HW structure
135 * @mem: ptr to mem struct to fill out
136 * @size: size of memory requested
137 **/
138int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
139 u32 size)
140{
141 mem->size = size;
142 mem->va = kzalloc(size, GFP_KERNEL);
143
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000144 if (!mem->va)
145 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000146
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000147 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000148}
149
150/**
151 * i40e_free_virt_mem_d - OS specific memory free for shared code
152 * @hw: pointer to the HW structure
153 * @mem: ptr to mem struct to free
154 **/
155int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
156{
157 /* it's ok to kfree a NULL pointer */
158 kfree(mem->va);
159 mem->va = NULL;
160 mem->size = 0;
161
162 return 0;
163}
164
165/**
166 * i40e_get_lump - find a lump of free generic resource
167 * @pf: board private structure
168 * @pile: the pile of resource to search
169 * @needed: the number of items needed
170 * @id: an owner id to stick on the items assigned
171 *
172 * Returns the base item index of the lump, or negative for error
173 *
174 * The search_hint trick and lack of advanced fit-finding only work
175 * because we're highly likely to have all the same size lump requests.
176 * Linear search time and any fragmentation should be minimal.
177 **/
178static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
179 u16 needed, u16 id)
180{
181 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000182 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000183
184 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
185 dev_info(&pf->pdev->dev,
186 "param err: pile=%p needed=%d id=0x%04x\n",
187 pile, needed, id);
188 return -EINVAL;
189 }
190
191 /* start the linear search with an imperfect hint */
192 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000193 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000194 /* skip already allocated entries */
195 if (pile->list[i] & I40E_PILE_VALID_BIT) {
196 i++;
197 continue;
198 }
199
200 /* do we have enough in this lump? */
201 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
202 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
203 break;
204 }
205
206 if (j == needed) {
207 /* there was enough, so assign it to the requestor */
208 for (j = 0; j < needed; j++)
209 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
210 ret = i;
211 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000212 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000213 } else {
214 /* not enough, so skip over it and continue looking */
215 i += j;
216 }
217 }
218
219 return ret;
220}
221
222/**
223 * i40e_put_lump - return a lump of generic resource
224 * @pile: the pile of resource to search
225 * @index: the base item index
226 * @id: the owner id of the items assigned
227 *
228 * Returns the count of items in the lump
229 **/
230static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
231{
232 int valid_id = (id | I40E_PILE_VALID_BIT);
233 int count = 0;
234 int i;
235
236 if (!pile || index >= pile->num_entries)
237 return -EINVAL;
238
239 for (i = index;
240 i < pile->num_entries && pile->list[i] == valid_id;
241 i++) {
242 pile->list[i] = 0;
243 count++;
244 }
245
246 if (count && index < pile->search_hint)
247 pile->search_hint = index;
248
249 return count;
250}
251
252/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700253 * i40e_find_vsi_from_id - searches for the vsi with the given id
254 * @pf - the pf structure to search for the vsi
255 * @id - id of the vsi it is searching for
256 **/
257struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
258{
259 int i;
260
261 for (i = 0; i < pf->num_alloc_vsi; i++)
262 if (pf->vsi[i] && (pf->vsi[i]->id == id))
263 return pf->vsi[i];
264
265 return NULL;
266}
267
268/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000269 * i40e_service_event_schedule - Schedule the service task to wake up
270 * @pf: board private structure
271 *
272 * If not already scheduled, this puts the task into the work queue
273 **/
274static void i40e_service_event_schedule(struct i40e_pf *pf)
275{
276 if (!test_bit(__I40E_DOWN, &pf->state) &&
277 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
278 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
279 schedule_work(&pf->service_task);
280}
281
282/**
283 * i40e_tx_timeout - Respond to a Tx Hang
284 * @netdev: network interface device structure
285 *
286 * If any port has noticed a Tx timeout, it is likely that the whole
287 * device is munged, not just the one netdev port, so go for the full
288 * reset.
289 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700290#ifdef I40E_FCOE
291void i40e_tx_timeout(struct net_device *netdev)
292#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000293static void i40e_tx_timeout(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -0700294#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000295{
296 struct i40e_netdev_priv *np = netdev_priv(netdev);
297 struct i40e_vsi *vsi = np->vsi;
298 struct i40e_pf *pf = vsi->back;
299
300 pf->tx_timeout_count++;
301
302 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Anjali Singhai Jain327fe042014-06-04 01:23:26 +0000303 pf->tx_timeout_recovery_level = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000304 pf->tx_timeout_last_recovery = jiffies;
305 netdev_info(netdev, "tx_timeout recovery level %d\n",
306 pf->tx_timeout_recovery_level);
307
308 switch (pf->tx_timeout_recovery_level) {
309 case 0:
310 /* disable and re-enable queues for the VSI */
311 if (in_interrupt()) {
312 set_bit(__I40E_REINIT_REQUESTED, &pf->state);
313 set_bit(__I40E_REINIT_REQUESTED, &vsi->state);
314 } else {
315 i40e_vsi_reinit_locked(vsi);
316 }
317 break;
318 case 1:
319 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
320 break;
321 case 2:
322 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
323 break;
324 case 3:
325 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
326 break;
327 default:
328 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Neerav Parikhb5d06f02014-06-03 23:50:17 +0000329 set_bit(__I40E_DOWN_REQUESTED, &pf->state);
330 set_bit(__I40E_DOWN_REQUESTED, &vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000331 break;
332 }
333 i40e_service_event_schedule(pf);
334 pf->tx_timeout_recovery_level++;
335}
336
337/**
338 * i40e_release_rx_desc - Store the new tail and head values
339 * @rx_ring: ring to bump
340 * @val: new head index
341 **/
342static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
343{
344 rx_ring->next_to_use = val;
345
346 /* Force memory writes to complete before letting h/w
347 * know there are new descriptors to fetch. (Only
348 * applicable for weak-ordered memory model archs,
349 * such as IA-64).
350 */
351 wmb();
352 writel(val, rx_ring->tail);
353}
354
355/**
356 * i40e_get_vsi_stats_struct - Get System Network Statistics
357 * @vsi: the VSI we care about
358 *
359 * Returns the address of the device statistics structure.
360 * The statistics are actually updated from the service task.
361 **/
362struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
363{
364 return &vsi->net_stats;
365}
366
367/**
368 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
369 * @netdev: network interface device structure
370 *
371 * Returns the address of the device statistics structure.
372 * The statistics are actually updated from the service task.
373 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700374#ifdef I40E_FCOE
375struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
376 struct net_device *netdev,
377 struct rtnl_link_stats64 *stats)
378#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000379static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
380 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000381 struct rtnl_link_stats64 *stats)
Vasu Dev38e00432014-08-01 13:27:03 -0700382#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000383{
384 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000385 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000386 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000387 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
388 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000389
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000390 if (test_bit(__I40E_DOWN, &vsi->state))
391 return stats;
392
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800393 if (!vsi->tx_rings)
394 return stats;
395
Alexander Duyck980e9b12013-09-28 06:01:03 +0000396 rcu_read_lock();
397 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000398 u64 bytes, packets;
399 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000400
Alexander Duyck980e9b12013-09-28 06:01:03 +0000401 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
402 if (!tx_ring)
403 continue;
404
405 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700406 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000407 packets = tx_ring->stats.packets;
408 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700409 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000410
411 stats->tx_packets += packets;
412 stats->tx_bytes += bytes;
413 rx_ring = &tx_ring[1];
414
415 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700416 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000417 packets = rx_ring->stats.packets;
418 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700419 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000420
421 stats->rx_packets += packets;
422 stats->rx_bytes += bytes;
423 }
424 rcu_read_unlock();
425
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000426 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000427 stats->multicast = vsi_stats->multicast;
428 stats->tx_errors = vsi_stats->tx_errors;
429 stats->tx_dropped = vsi_stats->tx_dropped;
430 stats->rx_errors = vsi_stats->rx_errors;
431 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
432 stats->rx_length_errors = vsi_stats->rx_length_errors;
433
434 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000435}
436
437/**
438 * i40e_vsi_reset_stats - Resets all stats of the given vsi
439 * @vsi: the VSI to have its stats reset
440 **/
441void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
442{
443 struct rtnl_link_stats64 *ns;
444 int i;
445
446 if (!vsi)
447 return;
448
449 ns = i40e_get_vsi_stats_struct(vsi);
450 memset(ns, 0, sizeof(*ns));
451 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
452 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
453 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000454 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000455 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000456 memset(&vsi->rx_rings[i]->stats, 0 ,
457 sizeof(vsi->rx_rings[i]->stats));
458 memset(&vsi->rx_rings[i]->rx_stats, 0 ,
459 sizeof(vsi->rx_rings[i]->rx_stats));
460 memset(&vsi->tx_rings[i]->stats, 0 ,
461 sizeof(vsi->tx_rings[i]->stats));
462 memset(&vsi->tx_rings[i]->tx_stats, 0,
463 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000464 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000465 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000466 vsi->stat_offsets_loaded = false;
467}
468
469/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000470 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000471 * @pf: the PF to be reset
472 **/
473void i40e_pf_reset_stats(struct i40e_pf *pf)
474{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000475 int i;
476
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000477 memset(&pf->stats, 0, sizeof(pf->stats));
478 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
479 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000480
481 for (i = 0; i < I40E_MAX_VEB; i++) {
482 if (pf->veb[i]) {
483 memset(&pf->veb[i]->stats, 0,
484 sizeof(pf->veb[i]->stats));
485 memset(&pf->veb[i]->stats_offsets, 0,
486 sizeof(pf->veb[i]->stats_offsets));
487 pf->veb[i]->stat_offsets_loaded = false;
488 }
489 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000490}
491
492/**
493 * i40e_stat_update48 - read and update a 48 bit stat from the chip
494 * @hw: ptr to the hardware info
495 * @hireg: the high 32 bit reg to read
496 * @loreg: the low 32 bit reg to read
497 * @offset_loaded: has the initial offset been loaded yet
498 * @offset: ptr to current offset value
499 * @stat: ptr to the stat
500 *
501 * Since the device stats are not reset at PFReset, they likely will not
502 * be zeroed when the driver starts. We'll save the first values read
503 * and use them as offsets to be subtracted from the raw values in order
504 * to report stats that count from zero. In the process, we also manage
505 * the potential roll-over.
506 **/
507static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
508 bool offset_loaded, u64 *offset, u64 *stat)
509{
510 u64 new_data;
511
Shannon Nelsonab600852014-01-17 15:36:39 -0800512 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000513 new_data = rd32(hw, loreg);
514 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
515 } else {
516 new_data = rd64(hw, loreg);
517 }
518 if (!offset_loaded)
519 *offset = new_data;
520 if (likely(new_data >= *offset))
521 *stat = new_data - *offset;
522 else
523 *stat = (new_data + ((u64)1 << 48)) - *offset;
524 *stat &= 0xFFFFFFFFFFFFULL;
525}
526
527/**
528 * i40e_stat_update32 - read and update a 32 bit stat from the chip
529 * @hw: ptr to the hardware info
530 * @reg: the hw reg to read
531 * @offset_loaded: has the initial offset been loaded yet
532 * @offset: ptr to current offset value
533 * @stat: ptr to the stat
534 **/
535static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
536 bool offset_loaded, u64 *offset, u64 *stat)
537{
538 u32 new_data;
539
540 new_data = rd32(hw, reg);
541 if (!offset_loaded)
542 *offset = new_data;
543 if (likely(new_data >= *offset))
544 *stat = (u32)(new_data - *offset);
545 else
546 *stat = (u32)((new_data + ((u64)1 << 32)) - *offset);
547}
548
549/**
550 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
551 * @vsi: the VSI to be updated
552 **/
553void i40e_update_eth_stats(struct i40e_vsi *vsi)
554{
555 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
556 struct i40e_pf *pf = vsi->back;
557 struct i40e_hw *hw = &pf->hw;
558 struct i40e_eth_stats *oes;
559 struct i40e_eth_stats *es; /* device's eth stats */
560
561 es = &vsi->eth_stats;
562 oes = &vsi->eth_stats_offsets;
563
564 /* Gather up the stats that the hw collects */
565 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
566 vsi->stat_offsets_loaded,
567 &oes->tx_errors, &es->tx_errors);
568 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
569 vsi->stat_offsets_loaded,
570 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000571 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
572 vsi->stat_offsets_loaded,
573 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
574 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
575 vsi->stat_offsets_loaded,
576 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000577
578 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
579 I40E_GLV_GORCL(stat_idx),
580 vsi->stat_offsets_loaded,
581 &oes->rx_bytes, &es->rx_bytes);
582 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
583 I40E_GLV_UPRCL(stat_idx),
584 vsi->stat_offsets_loaded,
585 &oes->rx_unicast, &es->rx_unicast);
586 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
587 I40E_GLV_MPRCL(stat_idx),
588 vsi->stat_offsets_loaded,
589 &oes->rx_multicast, &es->rx_multicast);
590 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
591 I40E_GLV_BPRCL(stat_idx),
592 vsi->stat_offsets_loaded,
593 &oes->rx_broadcast, &es->rx_broadcast);
594
595 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
596 I40E_GLV_GOTCL(stat_idx),
597 vsi->stat_offsets_loaded,
598 &oes->tx_bytes, &es->tx_bytes);
599 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
600 I40E_GLV_UPTCL(stat_idx),
601 vsi->stat_offsets_loaded,
602 &oes->tx_unicast, &es->tx_unicast);
603 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
604 I40E_GLV_MPTCL(stat_idx),
605 vsi->stat_offsets_loaded,
606 &oes->tx_multicast, &es->tx_multicast);
607 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
608 I40E_GLV_BPTCL(stat_idx),
609 vsi->stat_offsets_loaded,
610 &oes->tx_broadcast, &es->tx_broadcast);
611 vsi->stat_offsets_loaded = true;
612}
613
614/**
615 * i40e_update_veb_stats - Update Switch component statistics
616 * @veb: the VEB being updated
617 **/
618static void i40e_update_veb_stats(struct i40e_veb *veb)
619{
620 struct i40e_pf *pf = veb->pf;
621 struct i40e_hw *hw = &pf->hw;
622 struct i40e_eth_stats *oes;
623 struct i40e_eth_stats *es; /* device's eth stats */
624 int idx = 0;
625
626 idx = veb->stats_idx;
627 es = &veb->stats;
628 oes = &veb->stats_offsets;
629
630 /* Gather up the stats that the hw collects */
631 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
632 veb->stat_offsets_loaded,
633 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000634 if (hw->revision_id > 0)
635 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
636 veb->stat_offsets_loaded,
637 &oes->rx_unknown_protocol,
638 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000639 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
640 veb->stat_offsets_loaded,
641 &oes->rx_bytes, &es->rx_bytes);
642 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
643 veb->stat_offsets_loaded,
644 &oes->rx_unicast, &es->rx_unicast);
645 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
646 veb->stat_offsets_loaded,
647 &oes->rx_multicast, &es->rx_multicast);
648 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
649 veb->stat_offsets_loaded,
650 &oes->rx_broadcast, &es->rx_broadcast);
651
652 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
653 veb->stat_offsets_loaded,
654 &oes->tx_bytes, &es->tx_bytes);
655 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
656 veb->stat_offsets_loaded,
657 &oes->tx_unicast, &es->tx_unicast);
658 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
659 veb->stat_offsets_loaded,
660 &oes->tx_multicast, &es->tx_multicast);
661 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
662 veb->stat_offsets_loaded,
663 &oes->tx_broadcast, &es->tx_broadcast);
664 veb->stat_offsets_loaded = true;
665}
666
Vasu Dev38e00432014-08-01 13:27:03 -0700667#ifdef I40E_FCOE
668/**
669 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
670 * @vsi: the VSI that is capable of doing FCoE
671 **/
672static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
673{
674 struct i40e_pf *pf = vsi->back;
675 struct i40e_hw *hw = &pf->hw;
676 struct i40e_fcoe_stats *ofs;
677 struct i40e_fcoe_stats *fs; /* device's eth stats */
678 int idx;
679
680 if (vsi->type != I40E_VSI_FCOE)
681 return;
682
683 idx = (pf->pf_seid - I40E_BASE_PF_SEID) + I40E_FCOE_PF_STAT_OFFSET;
684 fs = &vsi->fcoe_stats;
685 ofs = &vsi->fcoe_stats_offsets;
686
687 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
688 vsi->fcoe_stat_offsets_loaded,
689 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
690 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
691 vsi->fcoe_stat_offsets_loaded,
692 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
693 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
694 vsi->fcoe_stat_offsets_loaded,
695 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
696 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
697 vsi->fcoe_stat_offsets_loaded,
698 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
699 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
700 vsi->fcoe_stat_offsets_loaded,
701 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
702 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
703 vsi->fcoe_stat_offsets_loaded,
704 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
705 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
706 vsi->fcoe_stat_offsets_loaded,
707 &ofs->fcoe_last_error, &fs->fcoe_last_error);
708 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
709 vsi->fcoe_stat_offsets_loaded,
710 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
711
712 vsi->fcoe_stat_offsets_loaded = true;
713}
714
715#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000716/**
717 * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
718 * @pf: the corresponding PF
719 *
720 * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
721 **/
722static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
723{
724 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
725 struct i40e_hw_port_stats *nsd = &pf->stats;
726 struct i40e_hw *hw = &pf->hw;
727 u64 xoff = 0;
728 u16 i, v;
729
730 if ((hw->fc.current_mode != I40E_FC_FULL) &&
731 (hw->fc.current_mode != I40E_FC_RX_PAUSE))
732 return;
733
734 xoff = nsd->link_xoff_rx;
735 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
736 pf->stat_offsets_loaded,
737 &osd->link_xoff_rx, &nsd->link_xoff_rx);
738
739 /* No new LFC xoff rx */
740 if (!(nsd->link_xoff_rx - xoff))
741 return;
742
743 /* Clear the __I40E_HANG_CHECK_ARMED bit for all Tx rings */
Mitch Williams505682c2014-05-20 08:01:37 +0000744 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000745 struct i40e_vsi *vsi = pf->vsi[v];
746
Mitch Williamsddfda802014-05-10 04:49:10 +0000747 if (!vsi || !vsi->tx_rings[0])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000748 continue;
749
750 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000751 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000752 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
753 }
754 }
755}
756
757/**
758 * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
759 * @pf: the corresponding PF
760 *
761 * Update the Rx XOFF counter (PAUSE frames) in PFC mode
762 **/
763static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
764{
765 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
766 struct i40e_hw_port_stats *nsd = &pf->stats;
767 bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
768 struct i40e_dcbx_config *dcb_cfg;
769 struct i40e_hw *hw = &pf->hw;
770 u16 i, v;
771 u8 tc;
772
773 dcb_cfg = &hw->local_dcbx_config;
774
Neerav Parikhe1208142015-04-16 20:05:58 -0400775 /* Collect Link XOFF stats when PFC is disabled */
776 if (!dcb_cfg->pfc.pfcenable) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000777 i40e_update_link_xoff_rx(pf);
778 return;
779 }
780
781 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
782 u64 prio_xoff = nsd->priority_xoff_rx[i];
783 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
784 pf->stat_offsets_loaded,
785 &osd->priority_xoff_rx[i],
786 &nsd->priority_xoff_rx[i]);
787
788 /* No new PFC xoff rx */
789 if (!(nsd->priority_xoff_rx[i] - prio_xoff))
790 continue;
791 /* Get the TC for given priority */
792 tc = dcb_cfg->etscfg.prioritytable[i];
793 xoff[tc] = true;
794 }
795
796 /* Clear the __I40E_HANG_CHECK_ARMED bit for Tx rings */
Mitch Williams505682c2014-05-20 08:01:37 +0000797 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000798 struct i40e_vsi *vsi = pf->vsi[v];
799
Mitch Williamsddfda802014-05-10 04:49:10 +0000800 if (!vsi || !vsi->tx_rings[0])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000801 continue;
802
803 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000804 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000805
806 tc = ring->dcb_tc;
807 if (xoff[tc])
808 clear_bit(__I40E_HANG_CHECK_ARMED,
809 &ring->state);
810 }
811 }
812}
813
814/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000815 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000816 * @vsi: the VSI to be updated
817 *
818 * There are a few instances where we store the same stat in a
819 * couple of different structs. This is partly because we have
820 * the netdev stats that need to be filled out, which is slightly
821 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000822 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000823 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000824static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000825{
826 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000827 struct rtnl_link_stats64 *ons;
828 struct rtnl_link_stats64 *ns; /* netdev stats */
829 struct i40e_eth_stats *oes;
830 struct i40e_eth_stats *es; /* device's eth stats */
831 u32 tx_restart, tx_busy;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000832 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000833 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000834 u64 bytes, packets;
835 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000836 u64 rx_p, rx_b;
837 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000838 u16 q;
839
840 if (test_bit(__I40E_DOWN, &vsi->state) ||
841 test_bit(__I40E_CONFIG_BUSY, &pf->state))
842 return;
843
844 ns = i40e_get_vsi_stats_struct(vsi);
845 ons = &vsi->net_stats_offsets;
846 es = &vsi->eth_stats;
847 oes = &vsi->eth_stats_offsets;
848
849 /* Gather up the netdev and vsi stats that the driver collects
850 * on the fly during packet processing
851 */
852 rx_b = rx_p = 0;
853 tx_b = tx_p = 0;
854 tx_restart = tx_busy = 0;
855 rx_page = 0;
856 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000857 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000858 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000859 /* locate Tx ring */
860 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000861
Alexander Duyck980e9b12013-09-28 06:01:03 +0000862 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700863 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000864 packets = p->stats.packets;
865 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700866 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000867 tx_b += bytes;
868 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000869 tx_restart += p->tx_stats.restart_queue;
870 tx_busy += p->tx_stats.tx_busy;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000871
872 /* Rx queue is part of the same block as Tx queue */
873 p = &p[1];
874 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700875 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000876 packets = p->stats.packets;
877 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700878 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000879 rx_b += bytes;
880 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000881 rx_buf += p->rx_stats.alloc_buff_failed;
882 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000883 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000884 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000885 vsi->tx_restart = tx_restart;
886 vsi->tx_busy = tx_busy;
887 vsi->rx_page_failed = rx_page;
888 vsi->rx_buf_failed = rx_buf;
889
890 ns->rx_packets = rx_p;
891 ns->rx_bytes = rx_b;
892 ns->tx_packets = tx_p;
893 ns->tx_bytes = tx_b;
894
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000895 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000896 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000897 ons->tx_errors = oes->tx_errors;
898 ns->tx_errors = es->tx_errors;
899 ons->multicast = oes->rx_multicast;
900 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000901 ons->rx_dropped = oes->rx_discards;
902 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000903 ons->tx_dropped = oes->tx_discards;
904 ns->tx_dropped = es->tx_discards;
905
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000906 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000907 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000908 ns->rx_crc_errors = pf->stats.crc_errors;
909 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
910 ns->rx_length_errors = pf->stats.rx_length_errors;
911 }
912}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000913
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000914/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000915 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000916 * @pf: the PF to be updated
917 **/
918static void i40e_update_pf_stats(struct i40e_pf *pf)
919{
920 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
921 struct i40e_hw_port_stats *nsd = &pf->stats;
922 struct i40e_hw *hw = &pf->hw;
923 u32 val;
924 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000925
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000926 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
927 I40E_GLPRT_GORCL(hw->port),
928 pf->stat_offsets_loaded,
929 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
930 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
931 I40E_GLPRT_GOTCL(hw->port),
932 pf->stat_offsets_loaded,
933 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
934 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
935 pf->stat_offsets_loaded,
936 &osd->eth.rx_discards,
937 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000938 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
939 I40E_GLPRT_UPRCL(hw->port),
940 pf->stat_offsets_loaded,
941 &osd->eth.rx_unicast,
942 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000943 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
944 I40E_GLPRT_MPRCL(hw->port),
945 pf->stat_offsets_loaded,
946 &osd->eth.rx_multicast,
947 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000948 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
949 I40E_GLPRT_BPRCL(hw->port),
950 pf->stat_offsets_loaded,
951 &osd->eth.rx_broadcast,
952 &nsd->eth.rx_broadcast);
953 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
954 I40E_GLPRT_UPTCL(hw->port),
955 pf->stat_offsets_loaded,
956 &osd->eth.tx_unicast,
957 &nsd->eth.tx_unicast);
958 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
959 I40E_GLPRT_MPTCL(hw->port),
960 pf->stat_offsets_loaded,
961 &osd->eth.tx_multicast,
962 &nsd->eth.tx_multicast);
963 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
964 I40E_GLPRT_BPTCL(hw->port),
965 pf->stat_offsets_loaded,
966 &osd->eth.tx_broadcast,
967 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000968
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000969 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
970 pf->stat_offsets_loaded,
971 &osd->tx_dropped_link_down,
972 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000973
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000974 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
975 pf->stat_offsets_loaded,
976 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000977
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000978 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
979 pf->stat_offsets_loaded,
980 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000981
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000982 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
983 pf->stat_offsets_loaded,
984 &osd->mac_local_faults,
985 &nsd->mac_local_faults);
986 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
987 pf->stat_offsets_loaded,
988 &osd->mac_remote_faults,
989 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000990
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000991 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
992 pf->stat_offsets_loaded,
993 &osd->rx_length_errors,
994 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000995
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000996 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
997 pf->stat_offsets_loaded,
998 &osd->link_xon_rx, &nsd->link_xon_rx);
999 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1000 pf->stat_offsets_loaded,
1001 &osd->link_xon_tx, &nsd->link_xon_tx);
1002 i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
1003 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1004 pf->stat_offsets_loaded,
1005 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001006
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001007 for (i = 0; i < 8; i++) {
1008 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001009 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001010 &osd->priority_xon_rx[i],
1011 &nsd->priority_xon_rx[i]);
1012 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001013 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001014 &osd->priority_xon_tx[i],
1015 &nsd->priority_xon_tx[i]);
1016 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001017 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001018 &osd->priority_xoff_tx[i],
1019 &nsd->priority_xoff_tx[i]);
1020 i40e_stat_update32(hw,
1021 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001022 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001023 &osd->priority_xon_2_xoff[i],
1024 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001025 }
1026
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001027 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1028 I40E_GLPRT_PRC64L(hw->port),
1029 pf->stat_offsets_loaded,
1030 &osd->rx_size_64, &nsd->rx_size_64);
1031 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1032 I40E_GLPRT_PRC127L(hw->port),
1033 pf->stat_offsets_loaded,
1034 &osd->rx_size_127, &nsd->rx_size_127);
1035 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1036 I40E_GLPRT_PRC255L(hw->port),
1037 pf->stat_offsets_loaded,
1038 &osd->rx_size_255, &nsd->rx_size_255);
1039 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1040 I40E_GLPRT_PRC511L(hw->port),
1041 pf->stat_offsets_loaded,
1042 &osd->rx_size_511, &nsd->rx_size_511);
1043 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1044 I40E_GLPRT_PRC1023L(hw->port),
1045 pf->stat_offsets_loaded,
1046 &osd->rx_size_1023, &nsd->rx_size_1023);
1047 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1048 I40E_GLPRT_PRC1522L(hw->port),
1049 pf->stat_offsets_loaded,
1050 &osd->rx_size_1522, &nsd->rx_size_1522);
1051 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1052 I40E_GLPRT_PRC9522L(hw->port),
1053 pf->stat_offsets_loaded,
1054 &osd->rx_size_big, &nsd->rx_size_big);
1055
1056 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1057 I40E_GLPRT_PTC64L(hw->port),
1058 pf->stat_offsets_loaded,
1059 &osd->tx_size_64, &nsd->tx_size_64);
1060 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1061 I40E_GLPRT_PTC127L(hw->port),
1062 pf->stat_offsets_loaded,
1063 &osd->tx_size_127, &nsd->tx_size_127);
1064 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1065 I40E_GLPRT_PTC255L(hw->port),
1066 pf->stat_offsets_loaded,
1067 &osd->tx_size_255, &nsd->tx_size_255);
1068 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1069 I40E_GLPRT_PTC511L(hw->port),
1070 pf->stat_offsets_loaded,
1071 &osd->tx_size_511, &nsd->tx_size_511);
1072 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1073 I40E_GLPRT_PTC1023L(hw->port),
1074 pf->stat_offsets_loaded,
1075 &osd->tx_size_1023, &nsd->tx_size_1023);
1076 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1077 I40E_GLPRT_PTC1522L(hw->port),
1078 pf->stat_offsets_loaded,
1079 &osd->tx_size_1522, &nsd->tx_size_1522);
1080 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1081 I40E_GLPRT_PTC9522L(hw->port),
1082 pf->stat_offsets_loaded,
1083 &osd->tx_size_big, &nsd->tx_size_big);
1084
1085 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1086 pf->stat_offsets_loaded,
1087 &osd->rx_undersize, &nsd->rx_undersize);
1088 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1089 pf->stat_offsets_loaded,
1090 &osd->rx_fragments, &nsd->rx_fragments);
1091 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1092 pf->stat_offsets_loaded,
1093 &osd->rx_oversize, &nsd->rx_oversize);
1094 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1095 pf->stat_offsets_loaded,
1096 &osd->rx_jabber, &nsd->rx_jabber);
1097
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001098 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001099 i40e_stat_update32(hw,
1100 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001101 pf->stat_offsets_loaded,
1102 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001103 i40e_stat_update32(hw,
1104 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001105 pf->stat_offsets_loaded,
1106 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001107 i40e_stat_update32(hw,
1108 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1109 pf->stat_offsets_loaded,
1110 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001111
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001112 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1113 nsd->tx_lpi_status =
1114 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1115 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1116 nsd->rx_lpi_status =
1117 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1118 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1119 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1120 pf->stat_offsets_loaded,
1121 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1122 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1123 pf->stat_offsets_loaded,
1124 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1125
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001126 pf->stat_offsets_loaded = true;
1127}
1128
1129/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001130 * i40e_update_stats - Update the various statistics counters.
1131 * @vsi: the VSI to be updated
1132 *
1133 * Update the various stats for this VSI and its related entities.
1134 **/
1135void i40e_update_stats(struct i40e_vsi *vsi)
1136{
1137 struct i40e_pf *pf = vsi->back;
1138
1139 if (vsi == pf->vsi[pf->lan_vsi])
1140 i40e_update_pf_stats(pf);
1141
1142 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001143#ifdef I40E_FCOE
1144 i40e_update_fcoe_stats(vsi);
1145#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001146}
1147
1148/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001149 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1150 * @vsi: the VSI to be searched
1151 * @macaddr: the MAC address
1152 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001153 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001154 * @is_netdev: make sure its a netdev filter, else doesn't matter
1155 *
1156 * Returns ptr to the filter object or NULL
1157 **/
1158static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1159 u8 *macaddr, s16 vlan,
1160 bool is_vf, bool is_netdev)
1161{
1162 struct i40e_mac_filter *f;
1163
1164 if (!vsi || !macaddr)
1165 return NULL;
1166
1167 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1168 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1169 (vlan == f->vlan) &&
1170 (!is_vf || f->is_vf) &&
1171 (!is_netdev || f->is_netdev))
1172 return f;
1173 }
1174 return NULL;
1175}
1176
1177/**
1178 * i40e_find_mac - Find a mac addr in the macvlan filters list
1179 * @vsi: the VSI to be searched
1180 * @macaddr: the MAC address we are searching for
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001181 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001182 * @is_netdev: make sure its a netdev filter, else doesn't matter
1183 *
1184 * Returns the first filter with the provided MAC address or NULL if
1185 * MAC address was not found
1186 **/
1187struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1188 bool is_vf, bool is_netdev)
1189{
1190 struct i40e_mac_filter *f;
1191
1192 if (!vsi || !macaddr)
1193 return NULL;
1194
1195 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1196 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1197 (!is_vf || f->is_vf) &&
1198 (!is_netdev || f->is_netdev))
1199 return f;
1200 }
1201 return NULL;
1202}
1203
1204/**
1205 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1206 * @vsi: the VSI to be searched
1207 *
1208 * Returns true if VSI is in vlan mode or false otherwise
1209 **/
1210bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1211{
1212 struct i40e_mac_filter *f;
1213
1214 /* Only -1 for all the filters denotes not in vlan mode
1215 * so we have to go through all the list in order to make sure
1216 */
1217 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1218 if (f->vlan >= 0)
1219 return true;
1220 }
1221
1222 return false;
1223}
1224
1225/**
1226 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1227 * @vsi: the VSI to be searched
1228 * @macaddr: the mac address to be filtered
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001229 * @is_vf: true if it is a VF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001230 * @is_netdev: true if it is a netdev
1231 *
1232 * Goes through all the macvlan filters and adds a
1233 * macvlan filter for each unique vlan that already exists
1234 *
1235 * Returns first filter found on success, else NULL
1236 **/
1237struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1238 bool is_vf, bool is_netdev)
1239{
1240 struct i40e_mac_filter *f;
1241
1242 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1243 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1244 is_vf, is_netdev)) {
1245 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001246 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001247 return NULL;
1248 }
1249 }
1250
1251 return list_first_entry_or_null(&vsi->mac_filter_list,
1252 struct i40e_mac_filter, list);
1253}
1254
1255/**
Greg Rose8c27d422014-05-22 06:31:56 +00001256 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1257 * @vsi: the PF Main VSI - inappropriate for any other VSI
1258 * @macaddr: the MAC address
Shannon Nelson30650cc2014-07-29 04:01:50 +00001259 *
1260 * Some older firmware configurations set up a default promiscuous VLAN
1261 * filter that needs to be removed.
Greg Rose8c27d422014-05-22 06:31:56 +00001262 **/
Shannon Nelson30650cc2014-07-29 04:01:50 +00001263static int i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
Greg Rose8c27d422014-05-22 06:31:56 +00001264{
1265 struct i40e_aqc_remove_macvlan_element_data element;
1266 struct i40e_pf *pf = vsi->back;
1267 i40e_status aq_ret;
1268
1269 /* Only appropriate for the PF main VSI */
1270 if (vsi->type != I40E_VSI_MAIN)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001271 return -EINVAL;
Greg Rose8c27d422014-05-22 06:31:56 +00001272
Shannon Nelson30650cc2014-07-29 04:01:50 +00001273 memset(&element, 0, sizeof(element));
Greg Rose8c27d422014-05-22 06:31:56 +00001274 ether_addr_copy(element.mac_addr, macaddr);
1275 element.vlan_tag = 0;
1276 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1277 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1278 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1279 if (aq_ret)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001280 return -ENOENT;
1281
1282 return 0;
Greg Rose8c27d422014-05-22 06:31:56 +00001283}
1284
1285/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001286 * i40e_add_filter - Add a mac/vlan filter to the VSI
1287 * @vsi: the VSI to be searched
1288 * @macaddr: the MAC address
1289 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001290 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001291 * @is_netdev: make sure its a netdev filter, else doesn't matter
1292 *
1293 * Returns ptr to the filter object or NULL when no memory available.
1294 **/
1295struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1296 u8 *macaddr, s16 vlan,
1297 bool is_vf, bool is_netdev)
1298{
1299 struct i40e_mac_filter *f;
1300
1301 if (!vsi || !macaddr)
1302 return NULL;
1303
1304 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1305 if (!f) {
1306 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1307 if (!f)
1308 goto add_filter_out;
1309
Greg Rose9a173902014-05-22 06:32:02 +00001310 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001311 f->vlan = vlan;
1312 f->changed = true;
1313
1314 INIT_LIST_HEAD(&f->list);
1315 list_add(&f->list, &vsi->mac_filter_list);
1316 }
1317
1318 /* increment counter and add a new flag if needed */
1319 if (is_vf) {
1320 if (!f->is_vf) {
1321 f->is_vf = true;
1322 f->counter++;
1323 }
1324 } else if (is_netdev) {
1325 if (!f->is_netdev) {
1326 f->is_netdev = true;
1327 f->counter++;
1328 }
1329 } else {
1330 f->counter++;
1331 }
1332
1333 /* changed tells sync_filters_subtask to
1334 * push the filter down to the firmware
1335 */
1336 if (f->changed) {
1337 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1338 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1339 }
1340
1341add_filter_out:
1342 return f;
1343}
1344
1345/**
1346 * i40e_del_filter - Remove a mac/vlan filter from 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 it's a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001351 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1352 **/
1353void i40e_del_filter(struct i40e_vsi *vsi,
1354 u8 *macaddr, s16 vlan,
1355 bool is_vf, bool is_netdev)
1356{
1357 struct i40e_mac_filter *f;
1358
1359 if (!vsi || !macaddr)
1360 return;
1361
1362 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1363 if (!f || f->counter == 0)
1364 return;
1365
1366 if (is_vf) {
1367 if (f->is_vf) {
1368 f->is_vf = false;
1369 f->counter--;
1370 }
1371 } else if (is_netdev) {
1372 if (f->is_netdev) {
1373 f->is_netdev = false;
1374 f->counter--;
1375 }
1376 } else {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001377 /* make sure we don't remove a filter in use by VF or netdev */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001378 int min_f = 0;
1379 min_f += (f->is_vf ? 1 : 0);
1380 min_f += (f->is_netdev ? 1 : 0);
1381
1382 if (f->counter > min_f)
1383 f->counter--;
1384 }
1385
1386 /* counter == 0 tells sync_filters_subtask to
1387 * remove the filter from the firmware's list
1388 */
1389 if (f->counter == 0) {
1390 f->changed = true;
1391 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1392 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1393 }
1394}
1395
1396/**
1397 * i40e_set_mac - NDO callback to set mac address
1398 * @netdev: network interface device structure
1399 * @p: pointer to an address structure
1400 *
1401 * Returns 0 on success, negative on failure
1402 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001403#ifdef I40E_FCOE
1404int i40e_set_mac(struct net_device *netdev, void *p)
1405#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001406static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001407#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001408{
1409 struct i40e_netdev_priv *np = netdev_priv(netdev);
1410 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001411 struct i40e_pf *pf = vsi->back;
1412 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001413 struct sockaddr *addr = p;
1414 struct i40e_mac_filter *f;
1415
1416 if (!is_valid_ether_addr(addr->sa_data))
1417 return -EADDRNOTAVAIL;
1418
Shannon Nelson30650cc2014-07-29 04:01:50 +00001419 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1420 netdev_info(netdev, "already using mac address %pM\n",
1421 addr->sa_data);
1422 return 0;
1423 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001424
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001425 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1426 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1427 return -EADDRNOTAVAIL;
1428
Shannon Nelson30650cc2014-07-29 04:01:50 +00001429 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1430 netdev_info(netdev, "returning to hw mac address %pM\n",
1431 hw->mac.addr);
1432 else
1433 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1434
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001435 if (vsi->type == I40E_VSI_MAIN) {
1436 i40e_status ret;
1437 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001438 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001439 addr->sa_data, NULL);
1440 if (ret) {
1441 netdev_info(netdev,
1442 "Addr change for Main VSI failed: %d\n",
1443 ret);
1444 return -EADDRNOTAVAIL;
1445 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001446 }
1447
Shannon Nelson30650cc2014-07-29 04:01:50 +00001448 if (ether_addr_equal(netdev->dev_addr, hw->mac.addr)) {
1449 struct i40e_aqc_remove_macvlan_element_data element;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001450
Shannon Nelson30650cc2014-07-29 04:01:50 +00001451 memset(&element, 0, sizeof(element));
1452 ether_addr_copy(element.mac_addr, netdev->dev_addr);
1453 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1454 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1455 } else {
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001456 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1457 false, false);
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001458 }
1459
Shannon Nelson30650cc2014-07-29 04:01:50 +00001460 if (ether_addr_equal(addr->sa_data, hw->mac.addr)) {
1461 struct i40e_aqc_add_macvlan_element_data element;
1462
1463 memset(&element, 0, sizeof(element));
1464 ether_addr_copy(element.mac_addr, hw->mac.addr);
1465 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
1466 i40e_aq_add_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1467 } else {
1468 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY,
1469 false, false);
1470 if (f)
1471 f->is_laa = true;
1472 }
1473
1474 i40e_sync_vsi_filters(vsi);
1475 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001476
1477 return 0;
1478}
1479
1480/**
1481 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1482 * @vsi: the VSI being setup
1483 * @ctxt: VSI context structure
1484 * @enabled_tc: Enabled TCs bitmap
1485 * @is_add: True if called before Add VSI
1486 *
1487 * Setup VSI queue mapping for enabled traffic classes.
1488 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001489#ifdef I40E_FCOE
1490void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1491 struct i40e_vsi_context *ctxt,
1492 u8 enabled_tc,
1493 bool is_add)
1494#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001495static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1496 struct i40e_vsi_context *ctxt,
1497 u8 enabled_tc,
1498 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001499#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001500{
1501 struct i40e_pf *pf = vsi->back;
1502 u16 sections = 0;
1503 u8 netdev_tc = 0;
1504 u16 numtc = 0;
1505 u16 qcount;
1506 u8 offset;
1507 u16 qmap;
1508 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001509 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001510
1511 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1512 offset = 0;
1513
1514 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1515 /* Find numtc from enabled TC bitmap */
1516 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1517 if (enabled_tc & (1 << i)) /* TC is enabled */
1518 numtc++;
1519 }
1520 if (!numtc) {
1521 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1522 numtc = 1;
1523 }
1524 } else {
1525 /* At least TC0 is enabled in case of non-DCB case */
1526 numtc = 1;
1527 }
1528
1529 vsi->tc_config.numtc = numtc;
1530 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001531 /* Number of queues per enabled TC */
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001532 /* In MFP case we can have a much lower count of MSIx
1533 * vectors available and so we need to lower the used
1534 * q count.
1535 */
1536 qcount = min_t(int, vsi->alloc_queue_pairs, pf->num_lan_msix);
1537 num_tc_qps = qcount / numtc;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001538 num_tc_qps = min_t(int, num_tc_qps, I40E_MAX_QUEUES_PER_TC);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001539
1540 /* Setup queue offset/count for all TCs for given VSI */
1541 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1542 /* See if the given TC is enabled for the given VSI */
1543 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1544 int pow, num_qps;
1545
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001546 switch (vsi->type) {
1547 case I40E_VSI_MAIN:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001548 qcount = min_t(int, pf->rss_size, num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001549 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001550#ifdef I40E_FCOE
1551 case I40E_VSI_FCOE:
1552 qcount = num_tc_qps;
1553 break;
1554#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001555 case I40E_VSI_FDIR:
1556 case I40E_VSI_SRIOV:
1557 case I40E_VSI_VMDQ2:
1558 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001559 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001560 WARN_ON(i != 0);
1561 break;
1562 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001563 vsi->tc_config.tc_info[i].qoffset = offset;
1564 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001565
Shannon Nelson1e200e42015-02-27 09:15:24 +00001566 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001567 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001568 pow = 0;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001569 while (num_qps && ((1 << pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001570 pow++;
1571 num_qps >>= 1;
1572 }
1573
1574 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1575 qmap =
1576 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1577 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1578
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001579 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001580 } else {
1581 /* TC is not enabled so set the offset to
1582 * default queue and allocate one queue
1583 * for the given TC.
1584 */
1585 vsi->tc_config.tc_info[i].qoffset = 0;
1586 vsi->tc_config.tc_info[i].qcount = 1;
1587 vsi->tc_config.tc_info[i].netdev_tc = 0;
1588
1589 qmap = 0;
1590 }
1591 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1592 }
1593
1594 /* Set actual Tx/Rx queue pairs */
1595 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001596 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1597 if (vsi->req_queue_pairs > 0)
1598 vsi->num_queue_pairs = vsi->req_queue_pairs;
1599 else
1600 vsi->num_queue_pairs = pf->num_lan_msix;
1601 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001602
1603 /* Scheduler section valid can only be set for ADD VSI */
1604 if (is_add) {
1605 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1606
1607 ctxt->info.up_enable_bits = enabled_tc;
1608 }
1609 if (vsi->type == I40E_VSI_SRIOV) {
1610 ctxt->info.mapping_flags |=
1611 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1612 for (i = 0; i < vsi->num_queue_pairs; i++)
1613 ctxt->info.queue_mapping[i] =
1614 cpu_to_le16(vsi->base_queue + i);
1615 } else {
1616 ctxt->info.mapping_flags |=
1617 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1618 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1619 }
1620 ctxt->info.valid_sections |= cpu_to_le16(sections);
1621}
1622
1623/**
1624 * i40e_set_rx_mode - NDO callback to set the netdev filters
1625 * @netdev: network interface device structure
1626 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001627#ifdef I40E_FCOE
1628void i40e_set_rx_mode(struct net_device *netdev)
1629#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001630static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001631#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001632{
1633 struct i40e_netdev_priv *np = netdev_priv(netdev);
1634 struct i40e_mac_filter *f, *ftmp;
1635 struct i40e_vsi *vsi = np->vsi;
1636 struct netdev_hw_addr *uca;
1637 struct netdev_hw_addr *mca;
1638 struct netdev_hw_addr *ha;
1639
1640 /* add addr if not already in the filter list */
1641 netdev_for_each_uc_addr(uca, netdev) {
1642 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1643 if (i40e_is_vsi_in_vlan(vsi))
1644 i40e_put_mac_in_vlan(vsi, uca->addr,
1645 false, true);
1646 else
1647 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1648 false, true);
1649 }
1650 }
1651
1652 netdev_for_each_mc_addr(mca, netdev) {
1653 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1654 if (i40e_is_vsi_in_vlan(vsi))
1655 i40e_put_mac_in_vlan(vsi, mca->addr,
1656 false, true);
1657 else
1658 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1659 false, true);
1660 }
1661 }
1662
1663 /* remove filter if not in netdev list */
1664 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1665 bool found = false;
1666
1667 if (!f->is_netdev)
1668 continue;
1669
1670 if (is_multicast_ether_addr(f->macaddr)) {
1671 netdev_for_each_mc_addr(mca, netdev) {
1672 if (ether_addr_equal(mca->addr, f->macaddr)) {
1673 found = true;
1674 break;
1675 }
1676 }
1677 } else {
1678 netdev_for_each_uc_addr(uca, netdev) {
1679 if (ether_addr_equal(uca->addr, f->macaddr)) {
1680 found = true;
1681 break;
1682 }
1683 }
1684
1685 for_each_dev_addr(netdev, ha) {
1686 if (ether_addr_equal(ha->addr, f->macaddr)) {
1687 found = true;
1688 break;
1689 }
1690 }
1691 }
1692 if (!found)
1693 i40e_del_filter(
1694 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1695 }
1696
1697 /* check for other flag changes */
1698 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1699 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1700 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1701 }
1702}
1703
1704/**
1705 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1706 * @vsi: ptr to the VSI
1707 *
1708 * Push any outstanding VSI filter changes through the AdminQ.
1709 *
1710 * Returns 0 or error value
1711 **/
1712int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1713{
1714 struct i40e_mac_filter *f, *ftmp;
1715 bool promisc_forced_on = false;
1716 bool add_happened = false;
1717 int filter_list_len = 0;
1718 u32 changed_flags = 0;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001719 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001720 struct i40e_pf *pf;
1721 int num_add = 0;
1722 int num_del = 0;
1723 u16 cmd_flags;
1724
1725 /* empty array typed pointers, kcalloc later */
1726 struct i40e_aqc_add_macvlan_element_data *add_list;
1727 struct i40e_aqc_remove_macvlan_element_data *del_list;
1728
1729 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1730 usleep_range(1000, 2000);
1731 pf = vsi->back;
1732
1733 if (vsi->netdev) {
1734 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1735 vsi->current_netdev_flags = vsi->netdev->flags;
1736 }
1737
1738 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1739 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1740
1741 filter_list_len = pf->hw.aq.asq_buf_size /
1742 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1743 del_list = kcalloc(filter_list_len,
1744 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1745 GFP_KERNEL);
1746 if (!del_list)
1747 return -ENOMEM;
1748
1749 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1750 if (!f->changed)
1751 continue;
1752
1753 if (f->counter != 0)
1754 continue;
1755 f->changed = false;
1756 cmd_flags = 0;
1757
1758 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001759 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001760 del_list[num_del].vlan_tag =
1761 cpu_to_le16((u16)(f->vlan ==
1762 I40E_VLAN_ANY ? 0 : f->vlan));
1763
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001764 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1765 del_list[num_del].flags = cmd_flags;
1766 num_del++;
1767
1768 /* unlink from filter list */
1769 list_del(&f->list);
1770 kfree(f);
1771
1772 /* flush a full buffer */
1773 if (num_del == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001774 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001775 vsi->seid, del_list, num_del,
1776 NULL);
1777 num_del = 0;
1778 memset(del_list, 0, sizeof(*del_list));
1779
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001780 if (aq_ret &&
1781 pf->hw.aq.asq_last_status !=
1782 I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001783 dev_info(&pf->pdev->dev,
1784 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001785 aq_ret,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001786 pf->hw.aq.asq_last_status);
1787 }
1788 }
1789 if (num_del) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001790 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001791 del_list, num_del, NULL);
1792 num_del = 0;
1793
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001794 if (aq_ret &&
1795 pf->hw.aq.asq_last_status != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001796 dev_info(&pf->pdev->dev,
1797 "ignoring delete macvlan error, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001798 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001799 }
1800
1801 kfree(del_list);
1802 del_list = NULL;
1803
1804 /* do all the adds now */
1805 filter_list_len = pf->hw.aq.asq_buf_size /
1806 sizeof(struct i40e_aqc_add_macvlan_element_data),
1807 add_list = kcalloc(filter_list_len,
1808 sizeof(struct i40e_aqc_add_macvlan_element_data),
1809 GFP_KERNEL);
1810 if (!add_list)
1811 return -ENOMEM;
1812
1813 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1814 if (!f->changed)
1815 continue;
1816
1817 if (f->counter == 0)
1818 continue;
1819 f->changed = false;
1820 add_happened = true;
1821 cmd_flags = 0;
1822
1823 /* add to add array */
Greg Rose9a173902014-05-22 06:32:02 +00001824 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001825 add_list[num_add].vlan_tag =
1826 cpu_to_le16(
1827 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1828 add_list[num_add].queue_number = 0;
1829
1830 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001831 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1832 num_add++;
1833
1834 /* flush a full buffer */
1835 if (num_add == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001836 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1837 add_list, num_add,
1838 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001839 num_add = 0;
1840
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001841 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001842 break;
1843 memset(add_list, 0, sizeof(*add_list));
1844 }
1845 }
1846 if (num_add) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001847 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1848 add_list, num_add, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001849 num_add = 0;
1850 }
1851 kfree(add_list);
1852 add_list = NULL;
1853
Shannon Nelson30650cc2014-07-29 04:01:50 +00001854 if (add_happened && aq_ret &&
1855 pf->hw.aq.asq_last_status != I40E_AQ_RC_EINVAL) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001856 dev_info(&pf->pdev->dev,
1857 "add filter failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001858 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001859 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1860 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1861 &vsi->state)) {
1862 promisc_forced_on = true;
1863 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1864 &vsi->state);
1865 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1866 }
1867 }
1868 }
1869
1870 /* check for changes in promiscuous modes */
1871 if (changed_flags & IFF_ALLMULTI) {
1872 bool cur_multipromisc;
1873 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001874 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1875 vsi->seid,
1876 cur_multipromisc,
1877 NULL);
1878 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001879 dev_info(&pf->pdev->dev,
1880 "set multi promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001881 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001882 }
1883 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1884 bool cur_promisc;
1885 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1886 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1887 &vsi->state));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001888 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1889 vsi->seid,
1890 cur_promisc, NULL);
1891 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001892 dev_info(&pf->pdev->dev,
1893 "set uni promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001894 aq_ret, pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00001895 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1896 vsi->seid,
1897 cur_promisc, NULL);
1898 if (aq_ret)
1899 dev_info(&pf->pdev->dev,
1900 "set brdcast promisc failed, err %d, aq_err %d\n",
1901 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001902 }
1903
1904 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1905 return 0;
1906}
1907
1908/**
1909 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1910 * @pf: board private structure
1911 **/
1912static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1913{
1914 int v;
1915
1916 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1917 return;
1918 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1919
Mitch Williams505682c2014-05-20 08:01:37 +00001920 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001921 if (pf->vsi[v] &&
1922 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1923 i40e_sync_vsi_filters(pf->vsi[v]);
1924 }
1925}
1926
1927/**
1928 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1929 * @netdev: network interface device structure
1930 * @new_mtu: new value for maximum frame size
1931 *
1932 * Returns 0 on success, negative on failure
1933 **/
1934static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1935{
1936 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00001937 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001938 struct i40e_vsi *vsi = np->vsi;
1939
1940 /* MTU < 68 is an error and causes problems on some kernels */
1941 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1942 return -EINVAL;
1943
1944 netdev_info(netdev, "changing MTU from %d to %d\n",
1945 netdev->mtu, new_mtu);
1946 netdev->mtu = new_mtu;
1947 if (netif_running(netdev))
1948 i40e_vsi_reinit_locked(vsi);
1949
1950 return 0;
1951}
1952
1953/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001954 * i40e_ioctl - Access the hwtstamp interface
1955 * @netdev: network interface device structure
1956 * @ifr: interface request data
1957 * @cmd: ioctl command
1958 **/
1959int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1960{
1961 struct i40e_netdev_priv *np = netdev_priv(netdev);
1962 struct i40e_pf *pf = np->vsi->back;
1963
1964 switch (cmd) {
1965 case SIOCGHWTSTAMP:
1966 return i40e_ptp_get_ts_config(pf, ifr);
1967 case SIOCSHWTSTAMP:
1968 return i40e_ptp_set_ts_config(pf, ifr);
1969 default:
1970 return -EOPNOTSUPP;
1971 }
1972}
1973
1974/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001975 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1976 * @vsi: the vsi being adjusted
1977 **/
1978void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1979{
1980 struct i40e_vsi_context ctxt;
1981 i40e_status ret;
1982
1983 if ((vsi->info.valid_sections &
1984 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1985 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1986 return; /* already enabled */
1987
1988 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1989 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1990 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1991
1992 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07001993 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001994 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1995 if (ret) {
1996 dev_info(&vsi->back->pdev->dev,
1997 "%s: update vsi failed, aq_err=%d\n",
1998 __func__, vsi->back->hw.aq.asq_last_status);
1999 }
2000}
2001
2002/**
2003 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2004 * @vsi: the vsi being adjusted
2005 **/
2006void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2007{
2008 struct i40e_vsi_context ctxt;
2009 i40e_status ret;
2010
2011 if ((vsi->info.valid_sections &
2012 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2013 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2014 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2015 return; /* already disabled */
2016
2017 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2018 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2019 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2020
2021 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002022 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002023 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2024 if (ret) {
2025 dev_info(&vsi->back->pdev->dev,
2026 "%s: update vsi failed, aq_err=%d\n",
2027 __func__, vsi->back->hw.aq.asq_last_status);
2028 }
2029}
2030
2031/**
2032 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2033 * @netdev: network interface to be adjusted
2034 * @features: netdev features to test if VLAN offload is enabled or not
2035 **/
2036static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2037{
2038 struct i40e_netdev_priv *np = netdev_priv(netdev);
2039 struct i40e_vsi *vsi = np->vsi;
2040
2041 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2042 i40e_vlan_stripping_enable(vsi);
2043 else
2044 i40e_vlan_stripping_disable(vsi);
2045}
2046
2047/**
2048 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2049 * @vsi: the vsi being configured
2050 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2051 **/
2052int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2053{
2054 struct i40e_mac_filter *f, *add_f;
2055 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002056
2057 is_vf = (vsi->type == I40E_VSI_SRIOV);
2058 is_netdev = !!(vsi->netdev);
2059
2060 if (is_netdev) {
2061 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
2062 is_vf, is_netdev);
2063 if (!add_f) {
2064 dev_info(&vsi->back->pdev->dev,
2065 "Could not add vlan filter %d for %pM\n",
2066 vid, vsi->netdev->dev_addr);
2067 return -ENOMEM;
2068 }
2069 }
2070
2071 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2072 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2073 if (!add_f) {
2074 dev_info(&vsi->back->pdev->dev,
2075 "Could not add vlan filter %d for %pM\n",
2076 vid, f->macaddr);
2077 return -ENOMEM;
2078 }
2079 }
2080
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002081 /* Now if we add a vlan tag, make sure to check if it is the first
2082 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2083 * with 0, so we now accept untagged and specified tagged traffic
2084 * (and not any taged and untagged)
2085 */
2086 if (vid > 0) {
2087 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
2088 I40E_VLAN_ANY,
2089 is_vf, is_netdev)) {
2090 i40e_del_filter(vsi, vsi->netdev->dev_addr,
2091 I40E_VLAN_ANY, is_vf, is_netdev);
2092 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
2093 is_vf, is_netdev);
2094 if (!add_f) {
2095 dev_info(&vsi->back->pdev->dev,
2096 "Could not add filter 0 for %pM\n",
2097 vsi->netdev->dev_addr);
2098 return -ENOMEM;
2099 }
2100 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002101 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002102
Greg Rose8d82a7c2014-01-13 16:13:04 -08002103 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2104 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002105 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2106 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2107 is_vf, is_netdev)) {
2108 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2109 is_vf, is_netdev);
2110 add_f = i40e_add_filter(vsi, f->macaddr,
2111 0, is_vf, is_netdev);
2112 if (!add_f) {
2113 dev_info(&vsi->back->pdev->dev,
2114 "Could not add filter 0 for %pM\n",
2115 f->macaddr);
2116 return -ENOMEM;
2117 }
2118 }
2119 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002120 }
2121
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002122 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2123 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2124 return 0;
2125
2126 return i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002127}
2128
2129/**
2130 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2131 * @vsi: the vsi being configured
2132 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002133 *
2134 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002135 **/
2136int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
2137{
2138 struct net_device *netdev = vsi->netdev;
2139 struct i40e_mac_filter *f, *add_f;
2140 bool is_vf, is_netdev;
2141 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002142
2143 is_vf = (vsi->type == I40E_VSI_SRIOV);
2144 is_netdev = !!(netdev);
2145
2146 if (is_netdev)
2147 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
2148
2149 list_for_each_entry(f, &vsi->mac_filter_list, list)
2150 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2151
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002152 /* go through all the filters for this VSI and if there is only
2153 * vid == 0 it means there are no other filters, so vid 0 must
2154 * be replaced with -1. This signifies that we should from now
2155 * on accept any traffic (with any tag present, or untagged)
2156 */
2157 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2158 if (is_netdev) {
2159 if (f->vlan &&
2160 ether_addr_equal(netdev->dev_addr, f->macaddr))
2161 filter_count++;
2162 }
2163
2164 if (f->vlan)
2165 filter_count++;
2166 }
2167
2168 if (!filter_count && is_netdev) {
2169 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2170 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2171 is_vf, is_netdev);
2172 if (!f) {
2173 dev_info(&vsi->back->pdev->dev,
2174 "Could not add filter %d for %pM\n",
2175 I40E_VLAN_ANY, netdev->dev_addr);
2176 return -ENOMEM;
2177 }
2178 }
2179
2180 if (!filter_count) {
2181 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2182 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2183 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2184 is_vf, is_netdev);
2185 if (!add_f) {
2186 dev_info(&vsi->back->pdev->dev,
2187 "Could not add filter %d for %pM\n",
2188 I40E_VLAN_ANY, f->macaddr);
2189 return -ENOMEM;
2190 }
2191 }
2192 }
2193
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002194 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2195 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2196 return 0;
2197
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002198 return i40e_sync_vsi_filters(vsi);
2199}
2200
2201/**
2202 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2203 * @netdev: network interface to be adjusted
2204 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002205 *
2206 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002207 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002208#ifdef I40E_FCOE
2209int i40e_vlan_rx_add_vid(struct net_device *netdev,
2210 __always_unused __be16 proto, u16 vid)
2211#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002212static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2213 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002214#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002215{
2216 struct i40e_netdev_priv *np = netdev_priv(netdev);
2217 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002218 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002219
2220 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002221 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002222
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002223 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2224
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002225 /* If the network stack called us with vid = 0 then
2226 * it is asking to receive priority tagged packets with
2227 * vlan id 0. Our HW receives them by default when configured
2228 * to receive untagged packets so there is no need to add an
2229 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002230 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002231 if (vid)
2232 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002233
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002234 if (!ret && (vid < VLAN_N_VID))
2235 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002236
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002237 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002238}
2239
2240/**
2241 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2242 * @netdev: network interface to be adjusted
2243 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002244 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002245 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002246 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002247#ifdef I40E_FCOE
2248int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2249 __always_unused __be16 proto, u16 vid)
2250#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002251static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2252 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002253#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002254{
2255 struct i40e_netdev_priv *np = netdev_priv(netdev);
2256 struct i40e_vsi *vsi = np->vsi;
2257
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002258 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2259
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002260 /* return code is ignored as there is nothing a user
2261 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002262 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002263 */
2264 i40e_vsi_kill_vlan(vsi, vid);
2265
2266 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002267
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002268 return 0;
2269}
2270
2271/**
2272 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2273 * @vsi: the vsi being brought back up
2274 **/
2275static void i40e_restore_vlan(struct i40e_vsi *vsi)
2276{
2277 u16 vid;
2278
2279 if (!vsi->netdev)
2280 return;
2281
2282 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2283
2284 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2285 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2286 vid);
2287}
2288
2289/**
2290 * i40e_vsi_add_pvid - Add pvid for the VSI
2291 * @vsi: the vsi being adjusted
2292 * @vid: the vlan id to set as a PVID
2293 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002294int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002295{
2296 struct i40e_vsi_context ctxt;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002297 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002298
2299 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2300 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002301 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2302 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002303 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002304
2305 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002306 ctxt.info = vsi->info;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002307 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2308 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002309 dev_info(&vsi->back->pdev->dev,
2310 "%s: update vsi failed, aq_err=%d\n",
2311 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002312 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002313 }
2314
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002315 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002316}
2317
2318/**
2319 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2320 * @vsi: the vsi being adjusted
2321 *
2322 * Just use the vlan_rx_register() service to put it back to normal
2323 **/
2324void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2325{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002326 i40e_vlan_stripping_disable(vsi);
2327
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002328 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002329}
2330
2331/**
2332 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2333 * @vsi: ptr to the VSI
2334 *
2335 * If this function returns with an error, then it's possible one or
2336 * more of the rings is populated (while the rest are not). It is the
2337 * callers duty to clean those orphaned rings.
2338 *
2339 * Return 0 on success, negative on failure
2340 **/
2341static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2342{
2343 int i, err = 0;
2344
2345 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002346 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002347
2348 return err;
2349}
2350
2351/**
2352 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2353 * @vsi: ptr to the VSI
2354 *
2355 * Free VSI's transmit software resources
2356 **/
2357static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2358{
2359 int i;
2360
Greg Rose8e9dca52013-12-18 13:45:53 +00002361 if (!vsi->tx_rings)
2362 return;
2363
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002364 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002365 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002366 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002367}
2368
2369/**
2370 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2371 * @vsi: ptr to the VSI
2372 *
2373 * If this function returns with an error, then it's possible one or
2374 * more of the rings is populated (while the rest are not). It is the
2375 * callers duty to clean those orphaned rings.
2376 *
2377 * Return 0 on success, negative on failure
2378 **/
2379static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2380{
2381 int i, err = 0;
2382
2383 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002384 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002385#ifdef I40E_FCOE
2386 i40e_fcoe_setup_ddp_resources(vsi);
2387#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002388 return err;
2389}
2390
2391/**
2392 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2393 * @vsi: ptr to the VSI
2394 *
2395 * Free all receive software resources
2396 **/
2397static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2398{
2399 int i;
2400
Greg Rose8e9dca52013-12-18 13:45:53 +00002401 if (!vsi->rx_rings)
2402 return;
2403
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002404 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002405 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002406 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002407#ifdef I40E_FCOE
2408 i40e_fcoe_free_ddp_resources(vsi);
2409#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002410}
2411
2412/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002413 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2414 * @ring: The Tx ring to configure
2415 *
2416 * This enables/disables XPS for a given Tx descriptor ring
2417 * based on the TCs enabled for the VSI that ring belongs to.
2418 **/
2419static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2420{
2421 struct i40e_vsi *vsi = ring->vsi;
2422 cpumask_var_t mask;
2423
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002424 if (!ring->q_vector || !ring->netdev)
2425 return;
2426
2427 /* Single TC mode enable XPS */
2428 if (vsi->tc_config.numtc <= 1) {
2429 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002430 netif_set_xps_queue(ring->netdev,
2431 &ring->q_vector->affinity_mask,
2432 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002433 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2434 /* Disable XPS to allow selection based on TC */
2435 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2436 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2437 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002438 }
2439}
2440
2441/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002442 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2443 * @ring: The Tx ring to configure
2444 *
2445 * Configure the Tx descriptor ring in the HMC context.
2446 **/
2447static int i40e_configure_tx_ring(struct i40e_ring *ring)
2448{
2449 struct i40e_vsi *vsi = ring->vsi;
2450 u16 pf_q = vsi->base_queue + ring->queue_index;
2451 struct i40e_hw *hw = &vsi->back->hw;
2452 struct i40e_hmc_obj_txq tx_ctx;
2453 i40e_status err = 0;
2454 u32 qtx_ctl = 0;
2455
2456 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002457 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002458 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2459 ring->atr_count = 0;
2460 } else {
2461 ring->atr_sample_rate = 0;
2462 }
2463
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002464 /* configure XPS */
2465 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002466
2467 /* clear the context structure first */
2468 memset(&tx_ctx, 0, sizeof(tx_ctx));
2469
2470 tx_ctx.new_context = 1;
2471 tx_ctx.base = (ring->dma / 128);
2472 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002473 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2474 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002475#ifdef I40E_FCOE
2476 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2477#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002478 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002479 /* FDIR VSI tx ring can still use RS bit and writebacks */
2480 if (vsi->type != I40E_VSI_FDIR)
2481 tx_ctx.head_wb_ena = 1;
2482 tx_ctx.head_wb_addr = ring->dma +
2483 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002484
2485 /* As part of VSI creation/update, FW allocates certain
2486 * Tx arbitration queue sets for each TC enabled for
2487 * the VSI. The FW returns the handles to these queue
2488 * sets as part of the response buffer to Add VSI,
2489 * Update VSI, etc. AQ commands. It is expected that
2490 * these queue set handles be associated with the Tx
2491 * queues by the driver as part of the TX queue context
2492 * initialization. This has to be done regardless of
2493 * DCB as by default everything is mapped to TC0.
2494 */
2495 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2496 tx_ctx.rdylist_act = 0;
2497
2498 /* clear the context in the HMC */
2499 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2500 if (err) {
2501 dev_info(&vsi->back->pdev->dev,
2502 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2503 ring->queue_index, pf_q, err);
2504 return -ENOMEM;
2505 }
2506
2507 /* set the context in the HMC */
2508 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2509 if (err) {
2510 dev_info(&vsi->back->pdev->dev,
2511 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2512 ring->queue_index, pf_q, err);
2513 return -ENOMEM;
2514 }
2515
2516 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002517 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002518 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002519 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2520 I40E_QTX_CTL_VFVM_INDX_MASK;
2521 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002522 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002523 }
2524
Shannon Nelson13fd9772013-09-28 07:14:19 +00002525 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2526 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002527 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2528 i40e_flush(hw);
2529
2530 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2531
2532 /* cache tail off for easier writes later */
2533 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2534
2535 return 0;
2536}
2537
2538/**
2539 * i40e_configure_rx_ring - Configure a receive ring context
2540 * @ring: The Rx ring to configure
2541 *
2542 * Configure the Rx descriptor ring in the HMC context.
2543 **/
2544static int i40e_configure_rx_ring(struct i40e_ring *ring)
2545{
2546 struct i40e_vsi *vsi = ring->vsi;
2547 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2548 u16 pf_q = vsi->base_queue + ring->queue_index;
2549 struct i40e_hw *hw = &vsi->back->hw;
2550 struct i40e_hmc_obj_rxq rx_ctx;
2551 i40e_status err = 0;
2552
2553 ring->state = 0;
2554
2555 /* clear the context structure first */
2556 memset(&rx_ctx, 0, sizeof(rx_ctx));
2557
2558 ring->rx_buf_len = vsi->rx_buf_len;
2559 ring->rx_hdr_len = vsi->rx_hdr_len;
2560
2561 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2562 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2563
2564 rx_ctx.base = (ring->dma / 128);
2565 rx_ctx.qlen = ring->count;
2566
2567 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2568 set_ring_16byte_desc_enabled(ring);
2569 rx_ctx.dsize = 0;
2570 } else {
2571 rx_ctx.dsize = 1;
2572 }
2573
2574 rx_ctx.dtype = vsi->dtype;
2575 if (vsi->dtype) {
2576 set_ring_ps_enabled(ring);
2577 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2578 I40E_RX_SPLIT_IP |
2579 I40E_RX_SPLIT_TCP_UDP |
2580 I40E_RX_SPLIT_SCTP;
2581 } else {
2582 rx_ctx.hsplit_0 = 0;
2583 }
2584
2585 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2586 (chain_len * ring->rx_buf_len));
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002587 if (hw->revision_id == 0)
2588 rx_ctx.lrxqthresh = 0;
2589 else
2590 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002591 rx_ctx.crcstrip = 1;
2592 rx_ctx.l2tsel = 1;
2593 rx_ctx.showiv = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07002594#ifdef I40E_FCOE
2595 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2596#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002597 /* set the prefena field to 1 because the manual says to */
2598 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002599
2600 /* clear the context in the HMC */
2601 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2602 if (err) {
2603 dev_info(&vsi->back->pdev->dev,
2604 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2605 ring->queue_index, pf_q, err);
2606 return -ENOMEM;
2607 }
2608
2609 /* set the context in the HMC */
2610 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2611 if (err) {
2612 dev_info(&vsi->back->pdev->dev,
2613 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2614 ring->queue_index, pf_q, err);
2615 return -ENOMEM;
2616 }
2617
2618 /* cache tail for quicker writes, and clear the reg before use */
2619 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2620 writel(0, ring->tail);
2621
Mitch Williamsa132af22015-01-24 09:58:35 +00002622 if (ring_is_ps_enabled(ring)) {
2623 i40e_alloc_rx_headers(ring);
2624 i40e_alloc_rx_buffers_ps(ring, I40E_DESC_UNUSED(ring));
2625 } else {
2626 i40e_alloc_rx_buffers_1buf(ring, I40E_DESC_UNUSED(ring));
2627 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002628
2629 return 0;
2630}
2631
2632/**
2633 * i40e_vsi_configure_tx - Configure the VSI for Tx
2634 * @vsi: VSI structure describing this set of rings and resources
2635 *
2636 * Configure the Tx VSI for operation.
2637 **/
2638static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2639{
2640 int err = 0;
2641 u16 i;
2642
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002643 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2644 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002645
2646 return err;
2647}
2648
2649/**
2650 * i40e_vsi_configure_rx - Configure the VSI for Rx
2651 * @vsi: the VSI being configured
2652 *
2653 * Configure the Rx VSI for operation.
2654 **/
2655static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2656{
2657 int err = 0;
2658 u16 i;
2659
2660 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2661 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2662 + ETH_FCS_LEN + VLAN_HLEN;
2663 else
2664 vsi->max_frame = I40E_RXBUFFER_2048;
2665
2666 /* figure out correct receive buffer length */
2667 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2668 I40E_FLAG_RX_PS_ENABLED)) {
2669 case I40E_FLAG_RX_1BUF_ENABLED:
2670 vsi->rx_hdr_len = 0;
2671 vsi->rx_buf_len = vsi->max_frame;
2672 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2673 break;
2674 case I40E_FLAG_RX_PS_ENABLED:
2675 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2676 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2677 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2678 break;
2679 default:
2680 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2681 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2682 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2683 break;
2684 }
2685
Vasu Dev38e00432014-08-01 13:27:03 -07002686#ifdef I40E_FCOE
2687 /* setup rx buffer for FCoE */
2688 if ((vsi->type == I40E_VSI_FCOE) &&
2689 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
2690 vsi->rx_hdr_len = 0;
2691 vsi->rx_buf_len = I40E_RXBUFFER_3072;
2692 vsi->max_frame = I40E_RXBUFFER_3072;
2693 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2694 }
2695
2696#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002697 /* round up for the chip's needs */
2698 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2699 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2700 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2701 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2702
2703 /* set up individual rings */
2704 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002705 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002706
2707 return err;
2708}
2709
2710/**
2711 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2712 * @vsi: ptr to the VSI
2713 **/
2714static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2715{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002716 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002717 u16 qoffset, qcount;
2718 int i, n;
2719
Parikh, Neeravcd238a32015-02-21 06:43:37 +00002720 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2721 /* Reset the TC information */
2722 for (i = 0; i < vsi->num_queue_pairs; i++) {
2723 rx_ring = vsi->rx_rings[i];
2724 tx_ring = vsi->tx_rings[i];
2725 rx_ring->dcb_tc = 0;
2726 tx_ring->dcb_tc = 0;
2727 }
2728 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002729
2730 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2731 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2732 continue;
2733
2734 qoffset = vsi->tc_config.tc_info[n].qoffset;
2735 qcount = vsi->tc_config.tc_info[n].qcount;
2736 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002737 rx_ring = vsi->rx_rings[i];
2738 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002739 rx_ring->dcb_tc = n;
2740 tx_ring->dcb_tc = n;
2741 }
2742 }
2743}
2744
2745/**
2746 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2747 * @vsi: ptr to the VSI
2748 **/
2749static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2750{
2751 if (vsi->netdev)
2752 i40e_set_rx_mode(vsi->netdev);
2753}
2754
2755/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002756 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
2757 * @vsi: Pointer to the targeted VSI
2758 *
2759 * This function replays the hlist on the hw where all the SB Flow Director
2760 * filters were saved.
2761 **/
2762static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
2763{
2764 struct i40e_fdir_filter *filter;
2765 struct i40e_pf *pf = vsi->back;
2766 struct hlist_node *node;
2767
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002768 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2769 return;
2770
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002771 hlist_for_each_entry_safe(filter, node,
2772 &pf->fdir_filter_list, fdir_node) {
2773 i40e_add_del_fdir(vsi, filter, true);
2774 }
2775}
2776
2777/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002778 * i40e_vsi_configure - Set up the VSI for action
2779 * @vsi: the VSI being configured
2780 **/
2781static int i40e_vsi_configure(struct i40e_vsi *vsi)
2782{
2783 int err;
2784
2785 i40e_set_vsi_rx_mode(vsi);
2786 i40e_restore_vlan(vsi);
2787 i40e_vsi_config_dcb_rings(vsi);
2788 err = i40e_vsi_configure_tx(vsi);
2789 if (!err)
2790 err = i40e_vsi_configure_rx(vsi);
2791
2792 return err;
2793}
2794
2795/**
2796 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2797 * @vsi: the VSI being configured
2798 **/
2799static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2800{
2801 struct i40e_pf *pf = vsi->back;
2802 struct i40e_q_vector *q_vector;
2803 struct i40e_hw *hw = &pf->hw;
2804 u16 vector;
2805 int i, q;
2806 u32 val;
2807 u32 qp;
2808
2809 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2810 * and PFINT_LNKLSTn registers, e.g.:
2811 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2812 */
2813 qp = vsi->base_queue;
2814 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002815 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2816 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002817 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2818 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2819 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2820 q_vector->rx.itr);
2821 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2822 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2823 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2824 q_vector->tx.itr);
2825
2826 /* Linked list for the queuepairs assigned to this vector */
2827 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2828 for (q = 0; q < q_vector->num_ringpairs; q++) {
2829 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2830 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2831 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2832 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2833 (I40E_QUEUE_TYPE_TX
2834 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2835
2836 wr32(hw, I40E_QINT_RQCTL(qp), val);
2837
2838 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2839 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2840 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2841 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2842 (I40E_QUEUE_TYPE_RX
2843 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2844
2845 /* Terminate the linked list */
2846 if (q == (q_vector->num_ringpairs - 1))
2847 val |= (I40E_QUEUE_END_OF_LIST
2848 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2849
2850 wr32(hw, I40E_QINT_TQCTL(qp), val);
2851 qp++;
2852 }
2853 }
2854
2855 i40e_flush(hw);
2856}
2857
2858/**
2859 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2860 * @hw: ptr to the hardware info
2861 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00002862static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002863{
Jacob Kellerab437b52014-12-14 01:55:08 +00002864 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002865 u32 val;
2866
2867 /* clear things first */
2868 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2869 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2870
2871 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2872 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2873 I40E_PFINT_ICR0_ENA_GRST_MASK |
2874 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2875 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002876 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2877 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2878 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2879
Jacob Kellerab437b52014-12-14 01:55:08 +00002880 if (pf->flags & I40E_FLAG_PTP)
2881 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
2882
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002883 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2884
2885 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002886 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2887 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002888
2889 /* OTHER_ITR_IDX = 0 */
2890 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2891}
2892
2893/**
2894 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2895 * @vsi: the VSI being configured
2896 **/
2897static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2898{
Alexander Duyck493fb302013-09-28 07:01:44 +00002899 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002900 struct i40e_pf *pf = vsi->back;
2901 struct i40e_hw *hw = &pf->hw;
2902 u32 val;
2903
2904 /* set the ITR configuration */
2905 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2906 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2907 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2908 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2909 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2910 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2911
Jacob Kellerab437b52014-12-14 01:55:08 +00002912 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002913
2914 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2915 wr32(hw, I40E_PFINT_LNKLST0, 0);
2916
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00002917 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002918 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2919 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2920 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2921
2922 wr32(hw, I40E_QINT_RQCTL(0), val);
2923
2924 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2925 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2926 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2927
2928 wr32(hw, I40E_QINT_TQCTL(0), val);
2929 i40e_flush(hw);
2930}
2931
2932/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00002933 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2934 * @pf: board private structure
2935 **/
2936void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2937{
2938 struct i40e_hw *hw = &pf->hw;
2939
2940 wr32(hw, I40E_PFINT_DYN_CTL0,
2941 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2942 i40e_flush(hw);
2943}
2944
2945/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002946 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2947 * @pf: board private structure
2948 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002949void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002950{
2951 struct i40e_hw *hw = &pf->hw;
2952 u32 val;
2953
2954 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2955 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2956 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2957
2958 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2959 i40e_flush(hw);
2960}
2961
2962/**
2963 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2964 * @vsi: pointer to a vsi
2965 * @vector: enable a particular Hw Interrupt vector
2966 **/
2967void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2968{
2969 struct i40e_pf *pf = vsi->back;
2970 struct i40e_hw *hw = &pf->hw;
2971 u32 val;
2972
2973 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2974 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2975 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2976 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002977 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002978}
2979
2980/**
Carolyn Wyborny5c2cebd2014-06-04 01:23:18 +00002981 * i40e_irq_dynamic_disable - Disable default interrupt generation settings
2982 * @vsi: pointer to a vsi
Greg Rose03147772015-01-24 09:58:29 +00002983 * @vector: disable a particular Hw Interrupt vector
Carolyn Wyborny5c2cebd2014-06-04 01:23:18 +00002984 **/
2985void i40e_irq_dynamic_disable(struct i40e_vsi *vsi, int vector)
2986{
2987 struct i40e_pf *pf = vsi->back;
2988 struct i40e_hw *hw = &pf->hw;
2989 u32 val;
2990
2991 val = I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT;
2992 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
2993 i40e_flush(hw);
2994}
2995
2996/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002997 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2998 * @irq: interrupt number
2999 * @data: pointer to a q_vector
3000 **/
3001static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3002{
3003 struct i40e_q_vector *q_vector = data;
3004
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003005 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003006 return IRQ_HANDLED;
3007
3008 napi_schedule(&q_vector->napi);
3009
3010 return IRQ_HANDLED;
3011}
3012
3013/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003014 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3015 * @vsi: the VSI being configured
3016 * @basename: name for the vector
3017 *
3018 * Allocates MSI-X vectors and requests interrupts from the kernel.
3019 **/
3020static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3021{
3022 int q_vectors = vsi->num_q_vectors;
3023 struct i40e_pf *pf = vsi->back;
3024 int base = vsi->base_vector;
3025 int rx_int_idx = 0;
3026 int tx_int_idx = 0;
3027 int vector, err;
3028
3029 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003030 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003031
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003032 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003033 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3034 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3035 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003036 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003037 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3038 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003039 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003040 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3041 "%s-%s-%d", basename, "tx", tx_int_idx++);
3042 } else {
3043 /* skip this unused q_vector */
3044 continue;
3045 }
3046 err = request_irq(pf->msix_entries[base + vector].vector,
3047 vsi->irq_handler,
3048 0,
3049 q_vector->name,
3050 q_vector);
3051 if (err) {
3052 dev_info(&pf->pdev->dev,
3053 "%s: request_irq failed, error: %d\n",
3054 __func__, err);
3055 goto free_queue_irqs;
3056 }
3057 /* assign the mask for this irq */
3058 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3059 &q_vector->affinity_mask);
3060 }
3061
Shannon Nelson63741842014-04-23 04:50:16 +00003062 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003063 return 0;
3064
3065free_queue_irqs:
3066 while (vector) {
3067 vector--;
3068 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3069 NULL);
3070 free_irq(pf->msix_entries[base + vector].vector,
3071 &(vsi->q_vectors[vector]));
3072 }
3073 return err;
3074}
3075
3076/**
3077 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3078 * @vsi: the VSI being un-configured
3079 **/
3080static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3081{
3082 struct i40e_pf *pf = vsi->back;
3083 struct i40e_hw *hw = &pf->hw;
3084 int base = vsi->base_vector;
3085 int i;
3086
3087 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003088 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3089 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003090 }
3091
3092 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3093 for (i = vsi->base_vector;
3094 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3095 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3096
3097 i40e_flush(hw);
3098 for (i = 0; i < vsi->num_q_vectors; i++)
3099 synchronize_irq(pf->msix_entries[i + base].vector);
3100 } else {
3101 /* Legacy and MSI mode - this stops all interrupt handling */
3102 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3103 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3104 i40e_flush(hw);
3105 synchronize_irq(pf->pdev->irq);
3106 }
3107}
3108
3109/**
3110 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3111 * @vsi: the VSI being configured
3112 **/
3113static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3114{
3115 struct i40e_pf *pf = vsi->back;
3116 int i;
3117
3118 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3119 for (i = vsi->base_vector;
3120 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3121 i40e_irq_dynamic_enable(vsi, i);
3122 } else {
3123 i40e_irq_dynamic_enable_icr0(pf);
3124 }
3125
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003126 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003127 return 0;
3128}
3129
3130/**
3131 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3132 * @pf: board private structure
3133 **/
3134static void i40e_stop_misc_vector(struct i40e_pf *pf)
3135{
3136 /* Disable ICR 0 */
3137 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3138 i40e_flush(&pf->hw);
3139}
3140
3141/**
3142 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3143 * @irq: interrupt number
3144 * @data: pointer to a q_vector
3145 *
3146 * This is the handler used for all MSI/Legacy interrupts, and deals
3147 * with both queue and non-queue interrupts. This is also used in
3148 * MSIX mode to handle the non-queue interrupts.
3149 **/
3150static irqreturn_t i40e_intr(int irq, void *data)
3151{
3152 struct i40e_pf *pf = (struct i40e_pf *)data;
3153 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003154 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003155 u32 icr0, icr0_remaining;
3156 u32 val, ena_mask;
3157
3158 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003159 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003160
Shannon Nelson116a57d2013-09-28 07:13:59 +00003161 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3162 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003163 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003164
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003165 /* if interrupt but no bits showing, must be SWINT */
3166 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3167 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3168 pf->sw_int_count++;
3169
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003170 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3171 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
3172
3173 /* temporarily disable queue cause for NAPI processing */
3174 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
3175 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3176 wr32(hw, I40E_QINT_RQCTL(0), qval);
3177
3178 qval = rd32(hw, I40E_QINT_TQCTL(0));
3179 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3180 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003181
3182 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00003183 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003184 }
3185
3186 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3187 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3188 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
3189 }
3190
3191 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3192 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3193 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3194 }
3195
3196 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3197 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3198 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3199 }
3200
3201 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3202 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3203 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3204 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3205 val = rd32(hw, I40E_GLGEN_RSTAT);
3206 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3207 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003208 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003209 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003210 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003211 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003212 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003213 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003214 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003215 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003216 }
3217
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003218 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3219 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3220 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003221 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3222 rd32(hw, I40E_PFHMC_ERRORINFO),
3223 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003224 }
3225
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003226 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3227 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3228
3229 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003230 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003231 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003232 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003233 }
3234
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003235 /* If a critical error is pending we have no choice but to reset the
3236 * device.
3237 * Report and mask out any remaining unexpected interrupts.
3238 */
3239 icr0_remaining = icr0 & ena_mask;
3240 if (icr0_remaining) {
3241 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3242 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003243 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003244 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003245 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003246 dev_info(&pf->pdev->dev, "device will be reset\n");
3247 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3248 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003249 }
3250 ena_mask &= ~icr0_remaining;
3251 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003252 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003253
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003254enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003255 /* re-enable interrupt causes */
3256 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003257 if (!test_bit(__I40E_DOWN, &pf->state)) {
3258 i40e_service_event_schedule(pf);
3259 i40e_irq_dynamic_enable_icr0(pf);
3260 }
3261
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003262 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003263}
3264
3265/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003266 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3267 * @tx_ring: tx ring to clean
3268 * @budget: how many cleans we're allowed
3269 *
3270 * Returns true if there's any budget left (e.g. the clean is finished)
3271 **/
3272static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3273{
3274 struct i40e_vsi *vsi = tx_ring->vsi;
3275 u16 i = tx_ring->next_to_clean;
3276 struct i40e_tx_buffer *tx_buf;
3277 struct i40e_tx_desc *tx_desc;
3278
3279 tx_buf = &tx_ring->tx_bi[i];
3280 tx_desc = I40E_TX_DESC(tx_ring, i);
3281 i -= tx_ring->count;
3282
3283 do {
3284 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3285
3286 /* if next_to_watch is not set then there is no work pending */
3287 if (!eop_desc)
3288 break;
3289
3290 /* prevent any other reads prior to eop_desc */
3291 read_barrier_depends();
3292
3293 /* if the descriptor isn't done, no work yet to do */
3294 if (!(eop_desc->cmd_type_offset_bsz &
3295 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3296 break;
3297
3298 /* clear next_to_watch to prevent false hangs */
3299 tx_buf->next_to_watch = NULL;
3300
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003301 tx_desc->buffer_addr = 0;
3302 tx_desc->cmd_type_offset_bsz = 0;
3303 /* move past filter desc */
3304 tx_buf++;
3305 tx_desc++;
3306 i++;
3307 if (unlikely(!i)) {
3308 i -= tx_ring->count;
3309 tx_buf = tx_ring->tx_bi;
3310 tx_desc = I40E_TX_DESC(tx_ring, 0);
3311 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003312 /* unmap skb header data */
3313 dma_unmap_single(tx_ring->dev,
3314 dma_unmap_addr(tx_buf, dma),
3315 dma_unmap_len(tx_buf, len),
3316 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003317 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3318 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003319
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003320 tx_buf->raw_buf = NULL;
3321 tx_buf->tx_flags = 0;
3322 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003323 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003324 tx_desc->buffer_addr = 0;
3325 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003326
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003327 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003328 tx_buf++;
3329 tx_desc++;
3330 i++;
3331 if (unlikely(!i)) {
3332 i -= tx_ring->count;
3333 tx_buf = tx_ring->tx_bi;
3334 tx_desc = I40E_TX_DESC(tx_ring, 0);
3335 }
3336
3337 /* update budget accounting */
3338 budget--;
3339 } while (likely(budget));
3340
3341 i += tx_ring->count;
3342 tx_ring->next_to_clean = i;
3343
3344 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
3345 i40e_irq_dynamic_enable(vsi,
3346 tx_ring->q_vector->v_idx + vsi->base_vector);
3347 }
3348 return budget > 0;
3349}
3350
3351/**
3352 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3353 * @irq: interrupt number
3354 * @data: pointer to a q_vector
3355 **/
3356static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3357{
3358 struct i40e_q_vector *q_vector = data;
3359 struct i40e_vsi *vsi;
3360
3361 if (!q_vector->tx.ring)
3362 return IRQ_HANDLED;
3363
3364 vsi = q_vector->tx.ring->vsi;
3365 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3366
3367 return IRQ_HANDLED;
3368}
3369
3370/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003371 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003372 * @vsi: the VSI being configured
3373 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003374 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003375 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003376static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003377{
Alexander Duyck493fb302013-09-28 07:01:44 +00003378 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003379 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3380 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003381
3382 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003383 tx_ring->next = q_vector->tx.ring;
3384 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003385 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003386
3387 rx_ring->q_vector = q_vector;
3388 rx_ring->next = q_vector->rx.ring;
3389 q_vector->rx.ring = rx_ring;
3390 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003391}
3392
3393/**
3394 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3395 * @vsi: the VSI being configured
3396 *
3397 * This function maps descriptor rings to the queue-specific vectors
3398 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3399 * one vector per queue pair, but on a constrained vector budget, we
3400 * group the queue pairs as "efficiently" as possible.
3401 **/
3402static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3403{
3404 int qp_remaining = vsi->num_queue_pairs;
3405 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003406 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003407 int v_start = 0;
3408 int qp_idx = 0;
3409
3410 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3411 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003412 * It is also important to go through all the vectors available to be
3413 * sure that if we don't use all the vectors, that the remaining vectors
3414 * are cleared. This is especially important when decreasing the
3415 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003416 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003417 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003418 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3419
3420 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3421
3422 q_vector->num_ringpairs = num_ringpairs;
3423
3424 q_vector->rx.count = 0;
3425 q_vector->tx.count = 0;
3426 q_vector->rx.ring = NULL;
3427 q_vector->tx.ring = NULL;
3428
3429 while (num_ringpairs--) {
3430 map_vector_to_qp(vsi, v_start, qp_idx);
3431 qp_idx++;
3432 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003433 }
3434 }
3435}
3436
3437/**
3438 * i40e_vsi_request_irq - Request IRQ from the OS
3439 * @vsi: the VSI being configured
3440 * @basename: name for the vector
3441 **/
3442static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3443{
3444 struct i40e_pf *pf = vsi->back;
3445 int err;
3446
3447 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3448 err = i40e_vsi_request_irq_msix(vsi, basename);
3449 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3450 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003451 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003452 else
3453 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003454 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003455
3456 if (err)
3457 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3458
3459 return err;
3460}
3461
3462#ifdef CONFIG_NET_POLL_CONTROLLER
3463/**
3464 * i40e_netpoll - A Polling 'interrupt'handler
3465 * @netdev: network interface device structure
3466 *
3467 * This is used by netconsole to send skbs without having to re-enable
3468 * interrupts. It's not called while the normal interrupt routine is executing.
3469 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003470#ifdef I40E_FCOE
3471void i40e_netpoll(struct net_device *netdev)
3472#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003473static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003474#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003475{
3476 struct i40e_netdev_priv *np = netdev_priv(netdev);
3477 struct i40e_vsi *vsi = np->vsi;
3478 struct i40e_pf *pf = vsi->back;
3479 int i;
3480
3481 /* if interface is down do nothing */
3482 if (test_bit(__I40E_DOWN, &vsi->state))
3483 return;
3484
3485 pf->flags |= I40E_FLAG_IN_NETPOLL;
3486 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3487 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003488 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003489 } else {
3490 i40e_intr(pf->pdev->irq, netdev);
3491 }
3492 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3493}
3494#endif
3495
3496/**
Neerav Parikh23527302014-06-03 23:50:15 +00003497 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3498 * @pf: the PF being configured
3499 * @pf_q: the PF queue
3500 * @enable: enable or disable state of the queue
3501 *
3502 * This routine will wait for the given Tx queue of the PF to reach the
3503 * enabled or disabled state.
3504 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3505 * multiple retries; else will return 0 in case of success.
3506 **/
3507static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3508{
3509 int i;
3510 u32 tx_reg;
3511
3512 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3513 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3514 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3515 break;
3516
Neerav Parikhf98a2002014-09-13 07:40:44 +00003517 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003518 }
3519 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3520 return -ETIMEDOUT;
3521
3522 return 0;
3523}
3524
3525/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003526 * i40e_vsi_control_tx - Start or stop a VSI's rings
3527 * @vsi: the VSI being configured
3528 * @enable: start or stop the rings
3529 **/
3530static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3531{
3532 struct i40e_pf *pf = vsi->back;
3533 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003534 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003535 u32 tx_reg;
3536
3537 pf_q = vsi->base_queue;
3538 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499a2014-04-23 04:50:03 +00003539
3540 /* warn the TX unit of coming changes */
3541 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3542 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003543 usleep_range(10, 20);
Matt Jared351499a2014-04-23 04:50:03 +00003544
Mitch Williams6c5ef622014-02-20 19:29:16 -08003545 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003546 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003547 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3548 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3549 break;
3550 usleep_range(1000, 2000);
3551 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003552 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003553 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003554 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003555
3556 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003557 if (enable) {
3558 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003559 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003560 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003561 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003562 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003563
3564 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003565 /* No waiting for the Tx queue to disable */
3566 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3567 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003568
3569 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003570 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3571 if (ret) {
3572 dev_info(&pf->pdev->dev,
3573 "%s: VSI seid %d Tx ring %d %sable timeout\n",
3574 __func__, vsi->seid, pf_q,
3575 (enable ? "en" : "dis"));
3576 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003577 }
3578 }
3579
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003580 if (hw->revision_id == 0)
3581 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003582 return ret;
3583}
3584
3585/**
3586 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3587 * @pf: the PF being configured
3588 * @pf_q: the PF queue
3589 * @enable: enable or disable state of the queue
3590 *
3591 * This routine will wait for the given Rx queue of the PF to reach the
3592 * enabled or disabled state.
3593 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3594 * multiple retries; else will return 0 in case of success.
3595 **/
3596static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3597{
3598 int i;
3599 u32 rx_reg;
3600
3601 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3602 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3603 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3604 break;
3605
Neerav Parikhf98a2002014-09-13 07:40:44 +00003606 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003607 }
3608 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3609 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003610
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003611 return 0;
3612}
3613
3614/**
3615 * i40e_vsi_control_rx - Start or stop a VSI's rings
3616 * @vsi: the VSI being configured
3617 * @enable: start or stop the rings
3618 **/
3619static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3620{
3621 struct i40e_pf *pf = vsi->back;
3622 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003623 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003624 u32 rx_reg;
3625
3626 pf_q = vsi->base_queue;
3627 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003628 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003629 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003630 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3631 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3632 break;
3633 usleep_range(1000, 2000);
3634 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003635
Catherine Sullivan7c122002014-03-14 07:32:29 +00003636 /* Skip if the queue is already in the requested state */
3637 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3638 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003639
3640 /* turn on/off the queue */
3641 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003642 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003643 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003644 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003645 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3646
3647 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003648 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3649 if (ret) {
3650 dev_info(&pf->pdev->dev,
3651 "%s: VSI seid %d Rx ring %d %sable timeout\n",
3652 __func__, vsi->seid, pf_q,
3653 (enable ? "en" : "dis"));
3654 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003655 }
3656 }
3657
Neerav Parikh23527302014-06-03 23:50:15 +00003658 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003659}
3660
3661/**
3662 * i40e_vsi_control_rings - Start or stop a VSI's rings
3663 * @vsi: the VSI being configured
3664 * @enable: start or stop the rings
3665 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003666int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003667{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003668 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003669
3670 /* do rx first for enable and last for disable */
3671 if (request) {
3672 ret = i40e_vsi_control_rx(vsi, request);
3673 if (ret)
3674 return ret;
3675 ret = i40e_vsi_control_tx(vsi, request);
3676 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003677 /* Ignore return value, we need to shutdown whatever we can */
3678 i40e_vsi_control_tx(vsi, request);
3679 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003680 }
3681
3682 return ret;
3683}
3684
3685/**
3686 * i40e_vsi_free_irq - Free the irq association with the OS
3687 * @vsi: the VSI being configured
3688 **/
3689static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3690{
3691 struct i40e_pf *pf = vsi->back;
3692 struct i40e_hw *hw = &pf->hw;
3693 int base = vsi->base_vector;
3694 u32 val, qp;
3695 int i;
3696
3697 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3698 if (!vsi->q_vectors)
3699 return;
3700
Shannon Nelson63741842014-04-23 04:50:16 +00003701 if (!vsi->irqs_ready)
3702 return;
3703
3704 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003705 for (i = 0; i < vsi->num_q_vectors; i++) {
3706 u16 vector = i + base;
3707
3708 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003709 if (!vsi->q_vectors[i] ||
3710 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003711 continue;
3712
3713 /* clear the affinity_mask in the IRQ descriptor */
3714 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3715 NULL);
3716 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003717 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003718
3719 /* Tear down the interrupt queue link list
3720 *
3721 * We know that they come in pairs and always
3722 * the Rx first, then the Tx. To clear the
3723 * link list, stick the EOL value into the
3724 * next_q field of the registers.
3725 */
3726 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3727 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3728 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3729 val |= I40E_QUEUE_END_OF_LIST
3730 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3731 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3732
3733 while (qp != I40E_QUEUE_END_OF_LIST) {
3734 u32 next;
3735
3736 val = rd32(hw, I40E_QINT_RQCTL(qp));
3737
3738 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3739 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3740 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3741 I40E_QINT_RQCTL_INTEVENT_MASK);
3742
3743 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3744 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3745
3746 wr32(hw, I40E_QINT_RQCTL(qp), val);
3747
3748 val = rd32(hw, I40E_QINT_TQCTL(qp));
3749
3750 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3751 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3752
3753 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3754 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3755 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3756 I40E_QINT_TQCTL_INTEVENT_MASK);
3757
3758 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3759 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3760
3761 wr32(hw, I40E_QINT_TQCTL(qp), val);
3762 qp = next;
3763 }
3764 }
3765 } else {
3766 free_irq(pf->pdev->irq, pf);
3767
3768 val = rd32(hw, I40E_PFINT_LNKLST0);
3769 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3770 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3771 val |= I40E_QUEUE_END_OF_LIST
3772 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3773 wr32(hw, I40E_PFINT_LNKLST0, val);
3774
3775 val = rd32(hw, I40E_QINT_RQCTL(qp));
3776 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3777 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3778 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3779 I40E_QINT_RQCTL_INTEVENT_MASK);
3780
3781 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3782 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3783
3784 wr32(hw, I40E_QINT_RQCTL(qp), val);
3785
3786 val = rd32(hw, I40E_QINT_TQCTL(qp));
3787
3788 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3789 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3790 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3791 I40E_QINT_TQCTL_INTEVENT_MASK);
3792
3793 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3794 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3795
3796 wr32(hw, I40E_QINT_TQCTL(qp), val);
3797 }
3798}
3799
3800/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003801 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3802 * @vsi: the VSI being configured
3803 * @v_idx: Index of vector to be freed
3804 *
3805 * This function frees the memory allocated to the q_vector. In addition if
3806 * NAPI is enabled it will delete any references to the NAPI struct prior
3807 * to freeing the q_vector.
3808 **/
3809static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3810{
3811 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003812 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003813
3814 if (!q_vector)
3815 return;
3816
3817 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003818 i40e_for_each_ring(ring, q_vector->tx)
3819 ring->q_vector = NULL;
3820
3821 i40e_for_each_ring(ring, q_vector->rx)
3822 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003823
3824 /* only VSI w/ an associated netdev is set up w/ NAPI */
3825 if (vsi->netdev)
3826 netif_napi_del(&q_vector->napi);
3827
3828 vsi->q_vectors[v_idx] = NULL;
3829
3830 kfree_rcu(q_vector, rcu);
3831}
3832
3833/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003834 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3835 * @vsi: the VSI being un-configured
3836 *
3837 * This frees the memory allocated to the q_vectors and
3838 * deletes references to the NAPI struct.
3839 **/
3840static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3841{
3842 int v_idx;
3843
Alexander Duyck493fb302013-09-28 07:01:44 +00003844 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3845 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003846}
3847
3848/**
3849 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3850 * @pf: board private structure
3851 **/
3852static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3853{
3854 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3855 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3856 pci_disable_msix(pf->pdev);
3857 kfree(pf->msix_entries);
3858 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00003859 kfree(pf->irq_pile);
3860 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003861 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3862 pci_disable_msi(pf->pdev);
3863 }
3864 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3865}
3866
3867/**
3868 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3869 * @pf: board private structure
3870 *
3871 * We go through and clear interrupt specific resources and reset the structure
3872 * to pre-load conditions
3873 **/
3874static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3875{
3876 int i;
3877
Shannon Nelsone1477582015-02-21 06:44:33 +00003878 i40e_stop_misc_vector(pf);
3879 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3880 synchronize_irq(pf->msix_entries[0].vector);
3881 free_irq(pf->msix_entries[0].vector, pf);
3882 }
3883
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003884 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00003885 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003886 if (pf->vsi[i])
3887 i40e_vsi_free_q_vectors(pf->vsi[i]);
3888 i40e_reset_interrupt_capability(pf);
3889}
3890
3891/**
3892 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3893 * @vsi: the VSI being configured
3894 **/
3895static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3896{
3897 int q_idx;
3898
3899 if (!vsi->netdev)
3900 return;
3901
3902 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003903 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003904}
3905
3906/**
3907 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3908 * @vsi: the VSI being configured
3909 **/
3910static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3911{
3912 int q_idx;
3913
3914 if (!vsi->netdev)
3915 return;
3916
3917 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003918 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003919}
3920
3921/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003922 * i40e_vsi_close - Shut down a VSI
3923 * @vsi: the vsi to be quelled
3924 **/
3925static void i40e_vsi_close(struct i40e_vsi *vsi)
3926{
3927 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
3928 i40e_down(vsi);
3929 i40e_vsi_free_irq(vsi);
3930 i40e_vsi_free_tx_resources(vsi);
3931 i40e_vsi_free_rx_resources(vsi);
3932}
3933
3934/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003935 * i40e_quiesce_vsi - Pause a given VSI
3936 * @vsi: the VSI being paused
3937 **/
3938static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3939{
3940 if (test_bit(__I40E_DOWN, &vsi->state))
3941 return;
3942
Neerav Parikhd341b7a2014-11-12 00:18:51 +00003943 /* No need to disable FCoE VSI when Tx suspended */
3944 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
3945 vsi->type == I40E_VSI_FCOE) {
3946 dev_dbg(&vsi->back->pdev->dev,
3947 "%s: VSI seid %d skipping FCoE VSI disable\n",
3948 __func__, vsi->seid);
3949 return;
3950 }
3951
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003952 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3953 if (vsi->netdev && netif_running(vsi->netdev)) {
3954 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3955 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003956 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003957 }
3958}
3959
3960/**
3961 * i40e_unquiesce_vsi - Resume a given VSI
3962 * @vsi: the VSI being resumed
3963 **/
3964static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3965{
3966 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3967 return;
3968
3969 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3970 if (vsi->netdev && netif_running(vsi->netdev))
3971 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3972 else
Shannon Nelson8276f752014-03-14 07:32:27 +00003973 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003974}
3975
3976/**
3977 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3978 * @pf: the PF
3979 **/
3980static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3981{
3982 int v;
3983
Mitch Williams505682c2014-05-20 08:01:37 +00003984 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003985 if (pf->vsi[v])
3986 i40e_quiesce_vsi(pf->vsi[v]);
3987 }
3988}
3989
3990/**
3991 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3992 * @pf: the PF
3993 **/
3994static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3995{
3996 int v;
3997
Mitch Williams505682c2014-05-20 08:01:37 +00003998 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003999 if (pf->vsi[v])
4000 i40e_unquiesce_vsi(pf->vsi[v]);
4001 }
4002}
4003
Neerav Parikh69129dc2014-11-12 00:18:46 +00004004#ifdef CONFIG_I40E_DCB
4005/**
4006 * i40e_vsi_wait_txq_disabled - Wait for VSI's queues to be disabled
4007 * @vsi: the VSI being configured
4008 *
4009 * This function waits for the given VSI's Tx queues to be disabled.
4010 **/
4011static int i40e_vsi_wait_txq_disabled(struct i40e_vsi *vsi)
4012{
4013 struct i40e_pf *pf = vsi->back;
4014 int i, pf_q, ret;
4015
4016 pf_q = vsi->base_queue;
4017 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4018 /* Check and wait for the disable status of the queue */
4019 ret = i40e_pf_txq_wait(pf, pf_q, false);
4020 if (ret) {
4021 dev_info(&pf->pdev->dev,
4022 "%s: VSI seid %d Tx ring %d disable timeout\n",
4023 __func__, vsi->seid, pf_q);
4024 return ret;
4025 }
4026 }
4027
4028 return 0;
4029}
4030
4031/**
4032 * i40e_pf_wait_txq_disabled - Wait for all queues of PF VSIs to be disabled
4033 * @pf: the PF
4034 *
4035 * This function waits for the Tx queues to be in disabled state for all the
4036 * VSIs that are managed by this PF.
4037 **/
4038static int i40e_pf_wait_txq_disabled(struct i40e_pf *pf)
4039{
4040 int v, ret = 0;
4041
4042 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004043 /* No need to wait for FCoE VSI queues */
4044 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh69129dc2014-11-12 00:18:46 +00004045 ret = i40e_vsi_wait_txq_disabled(pf->vsi[v]);
4046 if (ret)
4047 break;
4048 }
4049 }
4050
4051 return ret;
4052}
4053
4054#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004055/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004056 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004057 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004058 *
4059 * Get TC map for ISCSI PF type that will include iSCSI TC
4060 * and LAN TC.
4061 **/
4062static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4063{
4064 struct i40e_dcb_app_priority_table app;
4065 struct i40e_hw *hw = &pf->hw;
4066 u8 enabled_tc = 1; /* TC0 is always enabled */
4067 u8 tc, i;
4068 /* Get the iSCSI APP TLV */
4069 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4070
4071 for (i = 0; i < dcbcfg->numapps; i++) {
4072 app = dcbcfg->app[i];
4073 if (app.selector == I40E_APP_SEL_TCPIP &&
4074 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4075 tc = dcbcfg->etscfg.prioritytable[app.priority];
4076 enabled_tc |= (1 << tc);
4077 break;
4078 }
4079 }
4080
4081 return enabled_tc;
4082}
4083
4084/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004085 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4086 * @dcbcfg: the corresponding DCBx configuration structure
4087 *
4088 * Return the number of TCs from given DCBx configuration
4089 **/
4090static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4091{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004092 u8 num_tc = 0;
4093 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004094
4095 /* Scan the ETS Config Priority Table to find
4096 * traffic class enabled for a given priority
4097 * and use the traffic class index to get the
4098 * number of traffic classes enabled
4099 */
4100 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4101 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
4102 num_tc = dcbcfg->etscfg.prioritytable[i];
4103 }
4104
4105 /* Traffic class index starts from zero so
4106 * increment to return the actual count
4107 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004108 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004109}
4110
4111/**
4112 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4113 * @dcbcfg: the corresponding DCBx configuration structure
4114 *
4115 * Query the current DCB configuration and return the number of
4116 * traffic classes enabled from the given DCBX config
4117 **/
4118static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4119{
4120 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4121 u8 enabled_tc = 1;
4122 u8 i;
4123
4124 for (i = 0; i < num_tc; i++)
4125 enabled_tc |= 1 << i;
4126
4127 return enabled_tc;
4128}
4129
4130/**
4131 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4132 * @pf: PF being queried
4133 *
4134 * Return number of traffic classes enabled for the given PF
4135 **/
4136static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4137{
4138 struct i40e_hw *hw = &pf->hw;
4139 u8 i, enabled_tc;
4140 u8 num_tc = 0;
4141 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4142
4143 /* If DCB is not enabled then always in single TC */
4144 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4145 return 1;
4146
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004147 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004148 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4149 return i40e_dcb_get_num_tc(dcbcfg);
4150
4151 /* MFP mode return count of enabled TCs for this PF */
4152 if (pf->hw.func_caps.iscsi)
4153 enabled_tc = i40e_get_iscsi_tc_map(pf);
4154 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004155 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004156
4157 /* At least have TC0 */
4158 enabled_tc = (enabled_tc ? enabled_tc : 0x1);
4159 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4160 if (enabled_tc & (1 << i))
4161 num_tc++;
4162 }
4163 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004164}
4165
4166/**
4167 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
4168 * @pf: PF being queried
4169 *
4170 * Return a bitmap for first enabled traffic class for this PF.
4171 **/
4172static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
4173{
4174 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
4175 u8 i = 0;
4176
4177 if (!enabled_tc)
4178 return 0x1; /* TC0 */
4179
4180 /* Find the first enabled TC */
4181 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4182 if (enabled_tc & (1 << i))
4183 break;
4184 }
4185
4186 return 1 << i;
4187}
4188
4189/**
4190 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4191 * @pf: PF being queried
4192 *
4193 * Return a bitmap for enabled traffic classes for this PF.
4194 **/
4195static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4196{
4197 /* If DCB is not enabled for this PF then just return default TC */
4198 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4199 return i40e_pf_get_default_tc(pf);
4200
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004201 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004202 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4203 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4204
Neerav Parikhfc51de92015-02-24 06:58:53 +00004205 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004206 if (pf->hw.func_caps.iscsi)
4207 return i40e_get_iscsi_tc_map(pf);
4208 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004209 return i40e_pf_get_default_tc(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004210}
4211
4212/**
4213 * i40e_vsi_get_bw_info - Query VSI BW Information
4214 * @vsi: the VSI being queried
4215 *
4216 * Returns 0 on success, negative value on failure
4217 **/
4218static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4219{
4220 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4221 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4222 struct i40e_pf *pf = vsi->back;
4223 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004224 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004225 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004226 int i;
4227
4228 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004229 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4230 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004231 dev_info(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004232 "couldn't get PF vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004233 aq_ret, pf->hw.aq.asq_last_status);
4234 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004235 }
4236
4237 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004238 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
Jesse Brandeburg6838b532014-01-14 00:49:52 -08004239 NULL);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004240 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004241 dev_info(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004242 "couldn't get PF vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004243 aq_ret, pf->hw.aq.asq_last_status);
4244 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004245 }
4246
4247 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4248 dev_info(&pf->pdev->dev,
4249 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4250 bw_config.tc_valid_bits,
4251 bw_ets_config.tc_valid_bits);
4252 /* Still continuing */
4253 }
4254
4255 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4256 vsi->bw_max_quanta = bw_config.max_bw;
4257 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4258 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4259 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4260 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4261 vsi->bw_ets_limit_credits[i] =
4262 le16_to_cpu(bw_ets_config.credits[i]);
4263 /* 3 bits out of 4 for each TC */
4264 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4265 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004266
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004267 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004268}
4269
4270/**
4271 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4272 * @vsi: the VSI being configured
4273 * @enabled_tc: TC bitmap
4274 * @bw_credits: BW shared credits per TC
4275 *
4276 * Returns 0 on success, negative value on failure
4277 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004278static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004279 u8 *bw_share)
4280{
4281 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004282 i40e_status aq_ret;
4283 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004284
4285 bw_data.tc_valid_bits = enabled_tc;
4286 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4287 bw_data.tc_bw_credits[i] = bw_share[i];
4288
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004289 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4290 NULL);
4291 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004292 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004293 "AQ command Config VSI BW allocation per TC failed = %d\n",
4294 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004295 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004296 }
4297
4298 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4299 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4300
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004301 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004302}
4303
4304/**
4305 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4306 * @vsi: the VSI being configured
4307 * @enabled_tc: TC map to be enabled
4308 *
4309 **/
4310static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4311{
4312 struct net_device *netdev = vsi->netdev;
4313 struct i40e_pf *pf = vsi->back;
4314 struct i40e_hw *hw = &pf->hw;
4315 u8 netdev_tc = 0;
4316 int i;
4317 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4318
4319 if (!netdev)
4320 return;
4321
4322 if (!enabled_tc) {
4323 netdev_reset_tc(netdev);
4324 return;
4325 }
4326
4327 /* Set up actual enabled TCs on the VSI */
4328 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4329 return;
4330
4331 /* set per TC queues for the VSI */
4332 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4333 /* Only set TC queues for enabled tcs
4334 *
4335 * e.g. For a VSI that has TC0 and TC3 enabled the
4336 * enabled_tc bitmap would be 0x00001001; the driver
4337 * will set the numtc for netdev as 2 that will be
4338 * referenced by the netdev layer as TC 0 and 1.
4339 */
4340 if (vsi->tc_config.enabled_tc & (1 << i))
4341 netdev_set_tc_queue(netdev,
4342 vsi->tc_config.tc_info[i].netdev_tc,
4343 vsi->tc_config.tc_info[i].qcount,
4344 vsi->tc_config.tc_info[i].qoffset);
4345 }
4346
4347 /* Assign UP2TC map for the VSI */
4348 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4349 /* Get the actual TC# for the UP */
4350 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4351 /* Get the mapped netdev TC# for the UP */
4352 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4353 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4354 }
4355}
4356
4357/**
4358 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4359 * @vsi: the VSI being configured
4360 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4361 **/
4362static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4363 struct i40e_vsi_context *ctxt)
4364{
4365 /* copy just the sections touched not the entire info
4366 * since not all sections are valid as returned by
4367 * update vsi params
4368 */
4369 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4370 memcpy(&vsi->info.queue_mapping,
4371 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4372 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4373 sizeof(vsi->info.tc_mapping));
4374}
4375
4376/**
4377 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4378 * @vsi: VSI to be configured
4379 * @enabled_tc: TC bitmap
4380 *
4381 * This configures a particular VSI for TCs that are mapped to the
4382 * given TC bitmap. It uses default bandwidth share for TCs across
4383 * VSIs to configure TC for a particular VSI.
4384 *
4385 * NOTE:
4386 * It is expected that the VSI queues have been quisced before calling
4387 * this function.
4388 **/
4389static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4390{
4391 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4392 struct i40e_vsi_context ctxt;
4393 int ret = 0;
4394 int i;
4395
4396 /* Check if enabled_tc is same as existing or new TCs */
4397 if (vsi->tc_config.enabled_tc == enabled_tc)
4398 return ret;
4399
4400 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4401 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4402 if (enabled_tc & (1 << i))
4403 bw_share[i] = 1;
4404 }
4405
4406 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4407 if (ret) {
4408 dev_info(&vsi->back->pdev->dev,
4409 "Failed configuring TC map %d for VSI %d\n",
4410 enabled_tc, vsi->seid);
4411 goto out;
4412 }
4413
4414 /* Update Queue Pairs Mapping for currently enabled UPs */
4415 ctxt.seid = vsi->seid;
4416 ctxt.pf_num = vsi->back->hw.pf_id;
4417 ctxt.vf_num = 0;
4418 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004419 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004420 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4421
4422 /* Update the VSI after updating the VSI queue-mapping information */
4423 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4424 if (ret) {
4425 dev_info(&vsi->back->pdev->dev,
4426 "update vsi failed, aq_err=%d\n",
4427 vsi->back->hw.aq.asq_last_status);
4428 goto out;
4429 }
4430 /* update the local VSI info with updated queue map */
4431 i40e_vsi_update_queue_map(vsi, &ctxt);
4432 vsi->info.valid_sections = 0;
4433
4434 /* Update current VSI BW information */
4435 ret = i40e_vsi_get_bw_info(vsi);
4436 if (ret) {
4437 dev_info(&vsi->back->pdev->dev,
4438 "Failed updating vsi bw info, aq_err=%d\n",
4439 vsi->back->hw.aq.asq_last_status);
4440 goto out;
4441 }
4442
4443 /* Update the netdev TC setup */
4444 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4445out:
4446 return ret;
4447}
4448
4449/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004450 * i40e_veb_config_tc - Configure TCs for given VEB
4451 * @veb: given VEB
4452 * @enabled_tc: TC bitmap
4453 *
4454 * Configures given TC bitmap for VEB (switching) element
4455 **/
4456int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4457{
4458 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4459 struct i40e_pf *pf = veb->pf;
4460 int ret = 0;
4461 int i;
4462
4463 /* No TCs or already enabled TCs just return */
4464 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4465 return ret;
4466
4467 bw_data.tc_valid_bits = enabled_tc;
4468 /* bw_data.absolute_credits is not set (relative) */
4469
4470 /* Enable ETS TCs with equal BW Share for now */
4471 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4472 if (enabled_tc & (1 << i))
4473 bw_data.tc_bw_share_credits[i] = 1;
4474 }
4475
4476 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4477 &bw_data, NULL);
4478 if (ret) {
4479 dev_info(&pf->pdev->dev,
4480 "veb bw config failed, aq_err=%d\n",
4481 pf->hw.aq.asq_last_status);
4482 goto out;
4483 }
4484
4485 /* Update the BW information */
4486 ret = i40e_veb_get_bw_info(veb);
4487 if (ret) {
4488 dev_info(&pf->pdev->dev,
4489 "Failed getting veb bw config, aq_err=%d\n",
4490 pf->hw.aq.asq_last_status);
4491 }
4492
4493out:
4494 return ret;
4495}
4496
4497#ifdef CONFIG_I40E_DCB
4498/**
4499 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4500 * @pf: PF struct
4501 *
4502 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4503 * the caller would've quiesce all the VSIs before calling
4504 * this function
4505 **/
4506static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4507{
4508 u8 tc_map = 0;
4509 int ret;
4510 u8 v;
4511
4512 /* Enable the TCs available on PF to all VEBs */
4513 tc_map = i40e_pf_get_tc_map(pf);
4514 for (v = 0; v < I40E_MAX_VEB; v++) {
4515 if (!pf->veb[v])
4516 continue;
4517 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4518 if (ret) {
4519 dev_info(&pf->pdev->dev,
4520 "Failed configuring TC for VEB seid=%d\n",
4521 pf->veb[v]->seid);
4522 /* Will try to configure as many components */
4523 }
4524 }
4525
4526 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004527 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004528 if (!pf->vsi[v])
4529 continue;
4530
4531 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07004532#ifdef I40E_FCOE
4533 * - For FCoE VSI only enable the TC configured
4534 * as per the APP TLV
4535#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004536 * - For all others keep them at TC0 for now
4537 */
4538 if (v == pf->lan_vsi)
4539 tc_map = i40e_pf_get_tc_map(pf);
4540 else
4541 tc_map = i40e_pf_get_default_tc(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07004542#ifdef I40E_FCOE
4543 if (pf->vsi[v]->type == I40E_VSI_FCOE)
4544 tc_map = i40e_get_fcoe_tc_map(pf);
4545#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004546
4547 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4548 if (ret) {
4549 dev_info(&pf->pdev->dev,
4550 "Failed configuring TC for VSI seid=%d\n",
4551 pf->vsi[v]->seid);
4552 /* Will try to configure as many components */
4553 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004554 /* Re-configure VSI vectors based on updated TC map */
4555 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004556 if (pf->vsi[v]->netdev)
4557 i40e_dcbnl_set_all(pf->vsi[v]);
4558 }
4559 }
4560}
4561
4562/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00004563 * i40e_resume_port_tx - Resume port Tx
4564 * @pf: PF struct
4565 *
4566 * Resume a port's Tx and issue a PF reset in case of failure to
4567 * resume.
4568 **/
4569static int i40e_resume_port_tx(struct i40e_pf *pf)
4570{
4571 struct i40e_hw *hw = &pf->hw;
4572 int ret;
4573
4574 ret = i40e_aq_resume_port_tx(hw, NULL);
4575 if (ret) {
4576 dev_info(&pf->pdev->dev,
4577 "AQ command Resume Port Tx failed = %d\n",
4578 pf->hw.aq.asq_last_status);
4579 /* Schedule PF reset to recover */
4580 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4581 i40e_service_event_schedule(pf);
4582 }
4583
4584 return ret;
4585}
4586
4587/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004588 * i40e_init_pf_dcb - Initialize DCB configuration
4589 * @pf: PF being configured
4590 *
4591 * Query the current DCB configuration and cache it
4592 * in the hardware structure
4593 **/
4594static int i40e_init_pf_dcb(struct i40e_pf *pf)
4595{
4596 struct i40e_hw *hw = &pf->hw;
4597 int err = 0;
4598
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00004599 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
4600 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
4601 (pf->hw.aq.fw_maj_ver < 4))
4602 goto out;
4603
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004604 /* Get the initial DCB configuration */
4605 err = i40e_init_dcb(hw);
4606 if (!err) {
4607 /* Device/Function is not DCBX capable */
4608 if ((!hw->func_caps.dcb) ||
4609 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4610 dev_info(&pf->pdev->dev,
4611 "DCBX offload is not supported or is disabled for this PF.\n");
4612
4613 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4614 goto out;
4615
4616 } else {
4617 /* When status is not DISABLED then DCBX in FW */
4618 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4619 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004620
4621 pf->flags |= I40E_FLAG_DCB_CAPABLE;
4622 /* Enable DCB tagging only when more than one TC */
4623 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
4624 pf->flags |= I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00004625 dev_dbg(&pf->pdev->dev,
4626 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004627 }
Neerav Parikh014269f2014-04-01 07:11:48 +00004628 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00004629 dev_info(&pf->pdev->dev,
4630 "AQ Querying DCB configuration failed: aq_err %d\n",
Neerav Parikh014269f2014-04-01 07:11:48 +00004631 pf->hw.aq.asq_last_status);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004632 }
4633
4634out:
4635 return err;
4636}
4637#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004638#define SPEED_SIZE 14
4639#define FC_SIZE 8
4640/**
4641 * i40e_print_link_message - print link up or down
4642 * @vsi: the VSI for which link needs a message
4643 */
4644static void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
4645{
4646 char speed[SPEED_SIZE] = "Unknown";
4647 char fc[FC_SIZE] = "RX/TX";
4648
4649 if (!isup) {
4650 netdev_info(vsi->netdev, "NIC Link is Down\n");
4651 return;
4652 }
4653
Greg Rose148c2d82014-12-11 07:06:27 +00004654 /* Warn user if link speed on NPAR enabled partition is not at
4655 * least 10GB
4656 */
4657 if (vsi->back->hw.func_caps.npar_enable &&
4658 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
4659 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
4660 netdev_warn(vsi->netdev,
4661 "The partition detected link speed that is less than 10Gbps\n");
4662
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004663 switch (vsi->back->hw.phy.link_info.link_speed) {
4664 case I40E_LINK_SPEED_40GB:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004665 strlcpy(speed, "40 Gbps", SPEED_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004666 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07004667 case I40E_LINK_SPEED_20GB:
4668 strncpy(speed, "20 Gbps", SPEED_SIZE);
4669 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004670 case I40E_LINK_SPEED_10GB:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004671 strlcpy(speed, "10 Gbps", SPEED_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004672 break;
4673 case I40E_LINK_SPEED_1GB:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004674 strlcpy(speed, "1000 Mbps", SPEED_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004675 break;
Mitch Williams5960d332014-09-13 07:40:47 +00004676 case I40E_LINK_SPEED_100MB:
4677 strncpy(speed, "100 Mbps", SPEED_SIZE);
4678 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004679 default:
4680 break;
4681 }
4682
4683 switch (vsi->back->hw.fc.current_mode) {
4684 case I40E_FC_FULL:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004685 strlcpy(fc, "RX/TX", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004686 break;
4687 case I40E_FC_TX_PAUSE:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004688 strlcpy(fc, "TX", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004689 break;
4690 case I40E_FC_RX_PAUSE:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004691 strlcpy(fc, "RX", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004692 break;
4693 default:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004694 strlcpy(fc, "None", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004695 break;
4696 }
4697
4698 netdev_info(vsi->netdev, "NIC Link is Up %s Full Duplex, Flow Control: %s\n",
4699 speed, fc);
4700}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004701
4702/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004703 * i40e_up_complete - Finish the last steps of bringing up a connection
4704 * @vsi: the VSI being configured
4705 **/
4706static int i40e_up_complete(struct i40e_vsi *vsi)
4707{
4708 struct i40e_pf *pf = vsi->back;
4709 int err;
4710
4711 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4712 i40e_vsi_configure_msix(vsi);
4713 else
4714 i40e_configure_msi_and_legacy(vsi);
4715
4716 /* start rings */
4717 err = i40e_vsi_control_rings(vsi, true);
4718 if (err)
4719 return err;
4720
4721 clear_bit(__I40E_DOWN, &vsi->state);
4722 i40e_napi_enable_all(vsi);
4723 i40e_vsi_enable_irq(vsi);
4724
4725 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4726 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004727 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004728 netif_tx_start_all_queues(vsi->netdev);
4729 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004730 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004731 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00004732 /* need to check for qualified module here*/
4733 if ((pf->hw.phy.link_info.link_info &
4734 I40E_AQ_MEDIA_AVAILABLE) &&
4735 (!(pf->hw.phy.link_info.an_info &
4736 I40E_AQ_QUALIFIED_MODULE)))
4737 netdev_err(vsi->netdev,
4738 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004739 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004740
4741 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00004742 if (vsi->type == I40E_VSI_FDIR) {
4743 /* reset fd counters */
4744 pf->fd_add_err = pf->fd_atr_cnt = 0;
4745 if (pf->fd_tcp_rule > 0) {
4746 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04004747 if (I40E_DEBUG_FD & pf->hw.debug_mask)
4748 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00004749 pf->fd_tcp_rule = 0;
4750 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004751 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00004752 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004753 i40e_service_event_schedule(pf);
4754
4755 return 0;
4756}
4757
4758/**
4759 * i40e_vsi_reinit_locked - Reset the VSI
4760 * @vsi: the VSI being configured
4761 *
4762 * Rebuild the ring structs after some configuration
4763 * has changed, e.g. MTU size.
4764 **/
4765static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4766{
4767 struct i40e_pf *pf = vsi->back;
4768
4769 WARN_ON(in_interrupt());
4770 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4771 usleep_range(1000, 2000);
4772 i40e_down(vsi);
4773
4774 /* Give a VF some time to respond to the reset. The
4775 * two second wait is based upon the watchdog cycle in
4776 * the VF driver.
4777 */
4778 if (vsi->type == I40E_VSI_SRIOV)
4779 msleep(2000);
4780 i40e_up(vsi);
4781 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
4782}
4783
4784/**
4785 * i40e_up - Bring the connection back up after being down
4786 * @vsi: the VSI being configured
4787 **/
4788int i40e_up(struct i40e_vsi *vsi)
4789{
4790 int err;
4791
4792 err = i40e_vsi_configure(vsi);
4793 if (!err)
4794 err = i40e_up_complete(vsi);
4795
4796 return err;
4797}
4798
4799/**
4800 * i40e_down - Shutdown the connection processing
4801 * @vsi: the VSI being stopped
4802 **/
4803void i40e_down(struct i40e_vsi *vsi)
4804{
4805 int i;
4806
4807 /* It is assumed that the caller of this function
4808 * sets the vsi->state __I40E_DOWN bit.
4809 */
4810 if (vsi->netdev) {
4811 netif_carrier_off(vsi->netdev);
4812 netif_tx_disable(vsi->netdev);
4813 }
4814 i40e_vsi_disable_irq(vsi);
4815 i40e_vsi_control_rings(vsi, false);
4816 i40e_napi_disable_all(vsi);
4817
4818 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004819 i40e_clean_tx_ring(vsi->tx_rings[i]);
4820 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004821 }
4822}
4823
4824/**
4825 * i40e_setup_tc - configure multiple traffic classes
4826 * @netdev: net device to configure
4827 * @tc: number of traffic classes to enable
4828 **/
Vasu Dev38e00432014-08-01 13:27:03 -07004829#ifdef I40E_FCOE
4830int i40e_setup_tc(struct net_device *netdev, u8 tc)
4831#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004832static int i40e_setup_tc(struct net_device *netdev, u8 tc)
Vasu Dev38e00432014-08-01 13:27:03 -07004833#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004834{
4835 struct i40e_netdev_priv *np = netdev_priv(netdev);
4836 struct i40e_vsi *vsi = np->vsi;
4837 struct i40e_pf *pf = vsi->back;
4838 u8 enabled_tc = 0;
4839 int ret = -EINVAL;
4840 int i;
4841
4842 /* Check if DCB enabled to continue */
4843 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
4844 netdev_info(netdev, "DCB is not enabled for adapter\n");
4845 goto exit;
4846 }
4847
4848 /* Check if MFP enabled */
4849 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4850 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
4851 goto exit;
4852 }
4853
4854 /* Check whether tc count is within enabled limit */
4855 if (tc > i40e_pf_get_num_tc(pf)) {
4856 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
4857 goto exit;
4858 }
4859
4860 /* Generate TC map for number of tc requested */
4861 for (i = 0; i < tc; i++)
4862 enabled_tc |= (1 << i);
4863
4864 /* Requesting same TC configuration as already enabled */
4865 if (enabled_tc == vsi->tc_config.enabled_tc)
4866 return 0;
4867
4868 /* Quiesce VSI queues */
4869 i40e_quiesce_vsi(vsi);
4870
4871 /* Configure VSI for enabled TCs */
4872 ret = i40e_vsi_config_tc(vsi, enabled_tc);
4873 if (ret) {
4874 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
4875 vsi->seid);
4876 goto exit;
4877 }
4878
4879 /* Unquiesce VSI */
4880 i40e_unquiesce_vsi(vsi);
4881
4882exit:
4883 return ret;
4884}
4885
4886/**
4887 * i40e_open - Called when a network interface is made active
4888 * @netdev: network interface device structure
4889 *
4890 * The open entry point is called when a network interface is made
4891 * active by the system (IFF_UP). At this point all resources needed
4892 * for transmit and receive operations are allocated, the interrupt
4893 * handler is registered with the OS, the netdev watchdog subtask is
4894 * enabled, and the stack is notified that the interface is ready.
4895 *
4896 * Returns 0 on success, negative value on failure
4897 **/
Vasu Dev38e00432014-08-01 13:27:03 -07004898int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004899{
4900 struct i40e_netdev_priv *np = netdev_priv(netdev);
4901 struct i40e_vsi *vsi = np->vsi;
4902 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004903 int err;
4904
Shannon Nelson4eb3f762014-03-06 08:59:58 +00004905 /* disallow open during test or if eeprom is broken */
4906 if (test_bit(__I40E_TESTING, &pf->state) ||
4907 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004908 return -EBUSY;
4909
4910 netif_carrier_off(netdev);
4911
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004912 err = i40e_vsi_open(vsi);
4913 if (err)
4914 return err;
4915
Jesse Brandeburg059dab62014-04-01 09:07:20 +00004916 /* configure global TSO hardware offload settings */
4917 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
4918 TCP_FLAG_FIN) >> 16);
4919 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
4920 TCP_FLAG_FIN |
4921 TCP_FLAG_CWR) >> 16);
4922 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
4923
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004924#ifdef CONFIG_I40E_VXLAN
4925 vxlan_get_rx_port(netdev);
4926#endif
4927
4928 return 0;
4929}
4930
4931/**
4932 * i40e_vsi_open -
4933 * @vsi: the VSI to open
4934 *
4935 * Finish initialization of the VSI.
4936 *
4937 * Returns 0 on success, negative value on failure
4938 **/
4939int i40e_vsi_open(struct i40e_vsi *vsi)
4940{
4941 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00004942 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004943 int err;
4944
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004945 /* allocate descriptors */
4946 err = i40e_vsi_setup_tx_resources(vsi);
4947 if (err)
4948 goto err_setup_tx;
4949 err = i40e_vsi_setup_rx_resources(vsi);
4950 if (err)
4951 goto err_setup_rx;
4952
4953 err = i40e_vsi_configure(vsi);
4954 if (err)
4955 goto err_setup_rx;
4956
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004957 if (vsi->netdev) {
4958 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4959 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
4960 err = i40e_vsi_request_irq(vsi, int_name);
4961 if (err)
4962 goto err_setup_rx;
4963
4964 /* Notify the stack of the actual queue counts. */
4965 err = netif_set_real_num_tx_queues(vsi->netdev,
4966 vsi->num_queue_pairs);
4967 if (err)
4968 goto err_set_queues;
4969
4970 err = netif_set_real_num_rx_queues(vsi->netdev,
4971 vsi->num_queue_pairs);
4972 if (err)
4973 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00004974
4975 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00004976 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00004977 dev_driver_string(&pf->pdev->dev),
4978 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00004979 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00004980
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004981 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00004982 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004983 goto err_setup_rx;
4984 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004985
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004986 err = i40e_up_complete(vsi);
4987 if (err)
4988 goto err_up_complete;
4989
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004990 return 0;
4991
4992err_up_complete:
4993 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004994err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004995 i40e_vsi_free_irq(vsi);
4996err_setup_rx:
4997 i40e_vsi_free_rx_resources(vsi);
4998err_setup_tx:
4999 i40e_vsi_free_tx_resources(vsi);
5000 if (vsi == pf->vsi[pf->lan_vsi])
5001 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
5002
5003 return err;
5004}
5005
5006/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005007 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005008 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005009 *
5010 * This function destroys the hlist where all the Flow Director
5011 * filters were saved.
5012 **/
5013static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5014{
5015 struct i40e_fdir_filter *filter;
5016 struct hlist_node *node2;
5017
5018 hlist_for_each_entry_safe(filter, node2,
5019 &pf->fdir_filter_list, fdir_node) {
5020 hlist_del(&filter->fdir_node);
5021 kfree(filter);
5022 }
5023 pf->fdir_pf_active_filters = 0;
5024}
5025
5026/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005027 * i40e_close - Disables a network interface
5028 * @netdev: network interface device structure
5029 *
5030 * The close entry point is called when an interface is de-activated
5031 * by the OS. The hardware is still under the driver's control, but
5032 * this netdev interface is disabled.
5033 *
5034 * Returns 0, this is not allowed to fail
5035 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005036#ifdef I40E_FCOE
5037int i40e_close(struct net_device *netdev)
5038#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005039static int i40e_close(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07005040#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005041{
5042 struct i40e_netdev_priv *np = netdev_priv(netdev);
5043 struct i40e_vsi *vsi = np->vsi;
5044
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005045 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005046
5047 return 0;
5048}
5049
5050/**
5051 * i40e_do_reset - Start a PF or Core Reset sequence
5052 * @pf: board private structure
5053 * @reset_flags: which reset is requested
5054 *
5055 * The essential difference in resets is that the PF Reset
5056 * doesn't clear the packet buffers, doesn't reset the PE
5057 * firmware, and doesn't bother the other PFs on the chip.
5058 **/
5059void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5060{
5061 u32 val;
5062
5063 WARN_ON(in_interrupt());
5064
Mitch Williams263fc482014-04-23 04:50:11 +00005065 if (i40e_check_asq_alive(&pf->hw))
5066 i40e_vc_notify_reset(pf);
5067
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005068 /* do the biggest reset indicated */
5069 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
5070
5071 /* Request a Global Reset
5072 *
5073 * This will start the chip's countdown to the actual full
5074 * chip reset event, and a warning interrupt to be sent
5075 * to all PFs, including the requestor. Our handler
5076 * for the warning interrupt will deal with the shutdown
5077 * and recovery of the switch setup.
5078 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005079 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005080 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5081 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5082 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5083
5084 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
5085
5086 /* Request a Core Reset
5087 *
5088 * Same as Global Reset, except does *not* include the MAC/PHY
5089 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005090 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005091 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5092 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5093 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5094 i40e_flush(&pf->hw);
5095
5096 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
5097
5098 /* Request a PF Reset
5099 *
5100 * Resets only the PF-specific registers
5101 *
5102 * This goes directly to the tear-down and rebuild of
5103 * the switch, since we need to do all the recovery as
5104 * for the Core Reset.
5105 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005106 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005107 i40e_handle_reset_warning(pf);
5108
5109 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
5110 int v;
5111
5112 /* Find the VSI(s) that requested a re-init */
5113 dev_info(&pf->pdev->dev,
5114 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005115 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005116 struct i40e_vsi *vsi = pf->vsi[v];
5117 if (vsi != NULL &&
5118 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5119 i40e_vsi_reinit_locked(pf->vsi[v]);
5120 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5121 }
5122 }
5123
5124 /* no further action needed, so return now */
5125 return;
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005126 } else if (reset_flags & (1 << __I40E_DOWN_REQUESTED)) {
5127 int v;
5128
5129 /* Find the VSI(s) that needs to be brought down */
5130 dev_info(&pf->pdev->dev, "VSI down requested\n");
5131 for (v = 0; v < pf->num_alloc_vsi; v++) {
5132 struct i40e_vsi *vsi = pf->vsi[v];
5133 if (vsi != NULL &&
5134 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5135 set_bit(__I40E_DOWN, &vsi->state);
5136 i40e_down(vsi);
5137 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5138 }
5139 }
5140
5141 /* no further action needed, so return now */
5142 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005143 } else {
5144 dev_info(&pf->pdev->dev,
5145 "bad reset request 0x%08x\n", reset_flags);
5146 return;
5147 }
5148}
5149
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005150#ifdef CONFIG_I40E_DCB
5151/**
5152 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5153 * @pf: board private structure
5154 * @old_cfg: current DCB config
5155 * @new_cfg: new DCB config
5156 **/
5157bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5158 struct i40e_dcbx_config *old_cfg,
5159 struct i40e_dcbx_config *new_cfg)
5160{
5161 bool need_reconfig = false;
5162
5163 /* Check if ETS configuration has changed */
5164 if (memcmp(&new_cfg->etscfg,
5165 &old_cfg->etscfg,
5166 sizeof(new_cfg->etscfg))) {
5167 /* If Priority Table has changed reconfig is needed */
5168 if (memcmp(&new_cfg->etscfg.prioritytable,
5169 &old_cfg->etscfg.prioritytable,
5170 sizeof(new_cfg->etscfg.prioritytable))) {
5171 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005172 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005173 }
5174
5175 if (memcmp(&new_cfg->etscfg.tcbwtable,
5176 &old_cfg->etscfg.tcbwtable,
5177 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005178 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005179
5180 if (memcmp(&new_cfg->etscfg.tsatable,
5181 &old_cfg->etscfg.tsatable,
5182 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005183 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005184 }
5185
5186 /* Check if PFC configuration has changed */
5187 if (memcmp(&new_cfg->pfc,
5188 &old_cfg->pfc,
5189 sizeof(new_cfg->pfc))) {
5190 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005191 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005192 }
5193
5194 /* Check if APP Table has changed */
5195 if (memcmp(&new_cfg->app,
5196 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005197 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005198 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005199 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005200 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005201
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005202 dev_dbg(&pf->pdev->dev, "%s: need_reconfig=%d\n", __func__,
5203 need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005204 return need_reconfig;
5205}
5206
5207/**
5208 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5209 * @pf: board private structure
5210 * @e: event info posted on ARQ
5211 **/
5212static int i40e_handle_lldp_event(struct i40e_pf *pf,
5213 struct i40e_arq_event_info *e)
5214{
5215 struct i40e_aqc_lldp_get_mib *mib =
5216 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5217 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005218 struct i40e_dcbx_config tmp_dcbx_cfg;
5219 bool need_reconfig = false;
5220 int ret = 0;
5221 u8 type;
5222
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005223 /* Not DCB capable or capability disabled */
5224 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
5225 return ret;
5226
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005227 /* Ignore if event is not for Nearest Bridge */
5228 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5229 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005230 dev_dbg(&pf->pdev->dev,
5231 "%s: LLDP event mib bridge type 0x%x\n", __func__, type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005232 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5233 return ret;
5234
5235 /* Check MIB Type and return if event for Remote MIB update */
5236 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005237 dev_dbg(&pf->pdev->dev,
5238 "%s: LLDP event mib type %s\n", __func__,
5239 type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005240 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5241 /* Update the remote cached instance and return */
5242 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5243 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5244 &hw->remote_dcbx_config);
5245 goto exit;
5246 }
5247
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005248 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005249 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005250
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005251 /* Reset the old DCBx configuration data */
5252 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005253 /* Get updated DCBX data from firmware */
5254 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005255 if (ret) {
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005256 dev_info(&pf->pdev->dev, "Failed querying DCB configuration data from firmware.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005257 goto exit;
5258 }
5259
5260 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005261 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5262 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005263 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005264 goto exit;
5265 }
5266
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005267 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5268 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005269
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005270 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005271
5272 if (!need_reconfig)
5273 goto exit;
5274
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005275 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005276 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005277 pf->flags |= I40E_FLAG_DCB_ENABLED;
5278 else
5279 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5280
Neerav Parikh69129dc2014-11-12 00:18:46 +00005281 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005282 /* Reconfiguration needed quiesce all VSIs */
5283 i40e_pf_quiesce_all_vsi(pf);
5284
5285 /* Changes in configuration update VEB/VSI */
5286 i40e_dcb_reconfigure(pf);
5287
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005288 ret = i40e_resume_port_tx(pf);
5289
Neerav Parikh69129dc2014-11-12 00:18:46 +00005290 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005291 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005292 if (ret)
5293 goto exit;
5294
5295 /* Wait for the PF's Tx queues to be disabled */
5296 ret = i40e_pf_wait_txq_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005297 if (ret) {
5298 /* Schedule PF reset to recover */
5299 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5300 i40e_service_event_schedule(pf);
5301 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005302 i40e_pf_unquiesce_all_vsi(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005303 }
5304
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005305exit:
5306 return ret;
5307}
5308#endif /* CONFIG_I40E_DCB */
5309
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005310/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005311 * i40e_do_reset_safe - Protected reset path for userland calls.
5312 * @pf: board private structure
5313 * @reset_flags: which reset is requested
5314 *
5315 **/
5316void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5317{
5318 rtnl_lock();
5319 i40e_do_reset(pf, reset_flags);
5320 rtnl_unlock();
5321}
5322
5323/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005324 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5325 * @pf: board private structure
5326 * @e: event info posted on ARQ
5327 *
5328 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5329 * and VF queues
5330 **/
5331static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5332 struct i40e_arq_event_info *e)
5333{
5334 struct i40e_aqc_lan_overflow *data =
5335 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5336 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5337 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5338 struct i40e_hw *hw = &pf->hw;
5339 struct i40e_vf *vf;
5340 u16 vf_id;
5341
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005342 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5343 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005344
5345 /* Queue belongs to VF, find the VF and issue VF reset */
5346 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5347 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5348 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5349 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5350 vf_id -= hw->func_caps.vf_base_id;
5351 vf = &pf->vf[vf_id];
5352 i40e_vc_notify_vf_reset(vf);
5353 /* Allow VF to process pending reset notification */
5354 msleep(20);
5355 i40e_reset_vf(vf, false);
5356 }
5357}
5358
5359/**
5360 * i40e_service_event_complete - Finish up the service event
5361 * @pf: board private structure
5362 **/
5363static void i40e_service_event_complete(struct i40e_pf *pf)
5364{
5365 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
5366
5367 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005368 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005369 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5370}
5371
5372/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005373 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5374 * @pf: board private structure
5375 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005376u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005377{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005378 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005379
5380 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5381 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5382 return fcnt_prog;
5383}
5384
5385/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005386 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005387 * @pf: board private structure
5388 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005389u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005390{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005391 u32 val, fcnt_prog;
5392
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005393 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5394 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5395 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5396 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5397 return fcnt_prog;
5398}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005399
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005400/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005401 * i40e_get_global_fd_count - Get total FD filters programmed on device
5402 * @pf: board private structure
5403 **/
5404u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5405{
5406 u32 val, fcnt_prog;
5407
5408 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5409 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5410 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5411 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5412 return fcnt_prog;
5413}
5414
5415/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005416 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5417 * @pf: board private structure
5418 **/
5419void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5420{
5421 u32 fcnt_prog, fcnt_avail;
5422
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005423 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5424 return;
5425
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005426 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5427 * to re-enable
5428 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005429 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005430 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005431 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5432 (pf->fd_add_err == 0) ||
5433 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005434 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5435 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5436 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005437 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5438 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 +00005439 }
5440 }
5441 /* Wait for some more space to be available to turn on ATR */
5442 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5443 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
5444 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
5445 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005446 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5447 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 +00005448 }
5449 }
5450}
5451
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005452#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005453#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005454/**
5455 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
5456 * @pf: board private structure
5457 **/
5458static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
5459{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005460 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005461 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005462 bool disable_atr = false;
5463 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005464 int reg;
5465
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005466 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5467 return;
5468
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005469 if (time_after(jiffies, pf->fd_flush_timestamp +
5470 (I40E_MIN_FD_FLUSH_INTERVAL * HZ))) {
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005471 /* If the flush is happening too quick and we have mostly
5472 * SB rules we should not re-enable ATR for some time.
5473 */
5474 min_flush_time = pf->fd_flush_timestamp
5475 + (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
5476 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
5477
5478 if (!(time_after(jiffies, min_flush_time)) &&
5479 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005480 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5481 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005482 disable_atr = true;
5483 }
5484
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005485 pf->fd_flush_timestamp = jiffies;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005486 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
5487 /* flush all filters */
5488 wr32(&pf->hw, I40E_PFQF_CTL_1,
5489 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
5490 i40e_flush(&pf->hw);
Anjali Singhai Jain60793f42014-07-09 07:46:23 +00005491 pf->fd_flush_cnt++;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005492 pf->fd_add_err = 0;
5493 do {
5494 /* Check FD flush status every 5-6msec */
5495 usleep_range(5000, 6000);
5496 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
5497 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
5498 break;
5499 } while (flush_wait_retry--);
5500 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
5501 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
5502 } else {
5503 /* replay sideband filters */
5504 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005505 if (!disable_atr)
5506 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005507 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005508 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5509 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005510 }
5511 }
5512}
5513
5514/**
5515 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
5516 * @pf: board private structure
5517 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005518u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005519{
5520 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
5521}
5522
5523/* We can see up to 256 filter programming desc in transit if the filters are
5524 * being applied really fast; before we see the first
5525 * filter miss error on Rx queue 0. Accumulating enough error messages before
5526 * reacting will make sure we don't cause flush too often.
5527 */
5528#define I40E_MAX_FD_PROGRAM_ERROR 256
5529
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005530/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005531 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
5532 * @pf: board private structure
5533 **/
5534static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
5535{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005536
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005537 /* if interface is down do nothing */
5538 if (test_bit(__I40E_DOWN, &pf->state))
5539 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005540
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005541 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5542 return;
5543
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005544 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005545 i40e_fdir_flush_and_replay(pf);
5546
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005547 i40e_fdir_check_and_reenable(pf);
5548
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005549}
5550
5551/**
5552 * i40e_vsi_link_event - notify VSI of a link event
5553 * @vsi: vsi to be notified
5554 * @link_up: link up or down
5555 **/
5556static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
5557{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00005558 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005559 return;
5560
5561 switch (vsi->type) {
5562 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07005563#ifdef I40E_FCOE
5564 case I40E_VSI_FCOE:
5565#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005566 if (!vsi->netdev || !vsi->netdev_registered)
5567 break;
5568
5569 if (link_up) {
5570 netif_carrier_on(vsi->netdev);
5571 netif_tx_wake_all_queues(vsi->netdev);
5572 } else {
5573 netif_carrier_off(vsi->netdev);
5574 netif_tx_stop_all_queues(vsi->netdev);
5575 }
5576 break;
5577
5578 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005579 case I40E_VSI_VMDQ2:
5580 case I40E_VSI_CTRL:
5581 case I40E_VSI_MIRROR:
5582 default:
5583 /* there is no notification for other VSIs */
5584 break;
5585 }
5586}
5587
5588/**
5589 * i40e_veb_link_event - notify elements on the veb of a link event
5590 * @veb: veb to be notified
5591 * @link_up: link up or down
5592 **/
5593static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
5594{
5595 struct i40e_pf *pf;
5596 int i;
5597
5598 if (!veb || !veb->pf)
5599 return;
5600 pf = veb->pf;
5601
5602 /* depth first... */
5603 for (i = 0; i < I40E_MAX_VEB; i++)
5604 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
5605 i40e_veb_link_event(pf->veb[i], link_up);
5606
5607 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00005608 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005609 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
5610 i40e_vsi_link_event(pf->vsi[i], link_up);
5611}
5612
5613/**
5614 * i40e_link_event - Update netif_carrier status
5615 * @pf: board private structure
5616 **/
5617static void i40e_link_event(struct i40e_pf *pf)
5618{
5619 bool new_link, old_link;
Mitch Williams320684c2014-10-17 03:14:43 +00005620 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00005621 u8 new_link_speed, old_link_speed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005622
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005623 /* set this to force the get_link_status call to refresh state */
5624 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005625
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005626 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
5627 new_link = i40e_get_link_status(&pf->hw);
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00005628 old_link_speed = pf->hw.phy.link_info_old.link_speed;
5629 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005630
5631 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00005632 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00005633 (test_bit(__I40E_DOWN, &vsi->state) ||
5634 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005635 return;
Mitch Williams320684c2014-10-17 03:14:43 +00005636
5637 if (!test_bit(__I40E_DOWN, &vsi->state))
5638 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005639
5640 /* Notify the base of the switch tree connected to
5641 * the link. Floating VEBs are not notified.
5642 */
5643 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
5644 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
5645 else
Mitch Williams320684c2014-10-17 03:14:43 +00005646 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005647
5648 if (pf->vf)
5649 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005650
5651 if (pf->flags & I40E_FLAG_PTP)
5652 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005653}
5654
5655/**
5656 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
5657 * @pf: board private structure
5658 *
5659 * Set the per-queue flags to request a check for stuck queues in the irq
5660 * clean functions, then force interrupts to be sure the irq clean is called.
5661 **/
5662static void i40e_check_hang_subtask(struct i40e_pf *pf)
5663{
5664 int i, v;
5665
5666 /* If we're down or resetting, just bail */
Akeem G Abodunrinb67a0332015-02-21 06:42:15 +00005667 if (test_bit(__I40E_DOWN, &pf->state) ||
5668 test_bit(__I40E_CONFIG_BUSY, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005669 return;
5670
5671 /* for each VSI/netdev
5672 * for each Tx queue
5673 * set the check flag
5674 * for each q_vector
5675 * force an interrupt
5676 */
Mitch Williams505682c2014-05-20 08:01:37 +00005677 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005678 struct i40e_vsi *vsi = pf->vsi[v];
5679 int armed = 0;
5680
5681 if (!pf->vsi[v] ||
5682 test_bit(__I40E_DOWN, &vsi->state) ||
5683 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
5684 continue;
5685
5686 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005687 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005688 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005689 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005690 armed++;
5691 }
5692
5693 if (armed) {
5694 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
5695 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
5696 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
Shannon Nelson5d1ff1062014-11-11 20:04:35 +00005697 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK |
5698 I40E_PFINT_DYN_CTL0_ITR_INDX_MASK |
5699 I40E_PFINT_DYN_CTL0_SW_ITR_INDX_ENA_MASK |
5700 I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005701 } else {
5702 u16 vec = vsi->base_vector - 1;
5703 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
Shannon Nelson5d1ff1062014-11-11 20:04:35 +00005704 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK |
5705 I40E_PFINT_DYN_CTLN_ITR_INDX_MASK |
5706 I40E_PFINT_DYN_CTLN_SW_ITR_INDX_ENA_MASK |
5707 I40E_PFINT_DYN_CTLN_SW_ITR_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005708 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
5709 wr32(&vsi->back->hw,
5710 I40E_PFINT_DYN_CTLN(vec), val);
5711 }
5712 i40e_flush(&vsi->back->hw);
5713 }
5714 }
5715}
5716
5717/**
Shannon Nelson21536712014-10-25 10:35:25 +00005718 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005719 * @pf: board private structure
5720 **/
5721static void i40e_watchdog_subtask(struct i40e_pf *pf)
5722{
5723 int i;
5724
5725 /* if interface is down do nothing */
5726 if (test_bit(__I40E_DOWN, &pf->state) ||
5727 test_bit(__I40E_CONFIG_BUSY, &pf->state))
5728 return;
5729
Shannon Nelson21536712014-10-25 10:35:25 +00005730 /* make sure we don't do these things too often */
5731 if (time_before(jiffies, (pf->service_timer_previous +
5732 pf->service_timer_period)))
5733 return;
5734 pf->service_timer_previous = jiffies;
5735
5736 i40e_check_hang_subtask(pf);
5737 i40e_link_event(pf);
5738
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005739 /* Update the stats for active netdevs so the network stack
5740 * can look at updated numbers whenever it cares to
5741 */
Mitch Williams505682c2014-05-20 08:01:37 +00005742 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005743 if (pf->vsi[i] && pf->vsi[i]->netdev)
5744 i40e_update_stats(pf->vsi[i]);
5745
5746 /* Update the stats for the active switching components */
5747 for (i = 0; i < I40E_MAX_VEB; i++)
5748 if (pf->veb[i])
5749 i40e_update_veb_stats(pf->veb[i]);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005750
5751 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005752}
5753
5754/**
5755 * i40e_reset_subtask - Set up for resetting the device and driver
5756 * @pf: board private structure
5757 **/
5758static void i40e_reset_subtask(struct i40e_pf *pf)
5759{
5760 u32 reset_flags = 0;
5761
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005762 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005763 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
5764 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
5765 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
5766 }
5767 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
5768 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
5769 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5770 }
5771 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
5772 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
5773 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
5774 }
5775 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
5776 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
5777 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
5778 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005779 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
5780 reset_flags |= (1 << __I40E_DOWN_REQUESTED);
5781 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
5782 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005783
5784 /* If there's a recovery already waiting, it takes
5785 * precedence before starting a new reset sequence.
5786 */
5787 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
5788 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005789 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005790 }
5791
5792 /* If we're already down or resetting, just bail */
5793 if (reset_flags &&
5794 !test_bit(__I40E_DOWN, &pf->state) &&
5795 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
5796 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005797
5798unlock:
5799 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005800}
5801
5802/**
5803 * i40e_handle_link_event - Handle link event
5804 * @pf: board private structure
5805 * @e: event info posted on ARQ
5806 **/
5807static void i40e_handle_link_event(struct i40e_pf *pf,
5808 struct i40e_arq_event_info *e)
5809{
5810 struct i40e_hw *hw = &pf->hw;
5811 struct i40e_aqc_get_link_status *status =
5812 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005813
5814 /* save off old link status information */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005815 hw->phy.link_info_old = hw->phy.link_info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005816
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005817 /* Do a new status request to re-enable LSE reporting
5818 * and load new status information into the hw struct
5819 * This completely ignores any state information
5820 * in the ARQ event info, instead choosing to always
5821 * issue the AQ update link status command.
5822 */
5823 i40e_link_event(pf);
5824
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005825 /* check for unqualified module, if link is down */
5826 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
5827 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
5828 (!(status->link_info & I40E_AQ_LINK_UP)))
5829 dev_err(&pf->pdev->dev,
5830 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005831}
5832
5833/**
5834 * i40e_clean_adminq_subtask - Clean the AdminQ rings
5835 * @pf: board private structure
5836 **/
5837static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
5838{
5839 struct i40e_arq_event_info event;
5840 struct i40e_hw *hw = &pf->hw;
5841 u16 pending, i = 0;
5842 i40e_status ret;
5843 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00005844 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005845 u32 val;
5846
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00005847 /* Do not run clean AQ when PF reset fails */
5848 if (test_bit(__I40E_RESET_FAILED, &pf->state))
5849 return;
5850
Shannon Nelson86df2422014-05-20 08:01:35 +00005851 /* check for error indications */
5852 val = rd32(&pf->hw, pf->hw.aq.arq.len);
5853 oldval = val;
5854 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
5855 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
5856 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
5857 }
5858 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
5859 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
5860 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
5861 }
5862 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
5863 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
5864 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
5865 }
5866 if (oldval != val)
5867 wr32(&pf->hw, pf->hw.aq.arq.len, val);
5868
5869 val = rd32(&pf->hw, pf->hw.aq.asq.len);
5870 oldval = val;
5871 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
5872 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
5873 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
5874 }
5875 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
5876 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
5877 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
5878 }
5879 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
5880 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
5881 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
5882 }
5883 if (oldval != val)
5884 wr32(&pf->hw, pf->hw.aq.asq.len, val);
5885
Mitch Williams1001dc32014-11-11 20:02:19 +00005886 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
5887 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005888 if (!event.msg_buf)
5889 return;
5890
5891 do {
5892 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00005893 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005894 break;
Mitch Williams56497972014-06-04 08:45:18 +00005895 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005896 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
5897 break;
5898 }
5899
5900 opcode = le16_to_cpu(event.desc.opcode);
5901 switch (opcode) {
5902
5903 case i40e_aqc_opc_get_link_status:
5904 i40e_handle_link_event(pf, &event);
5905 break;
5906 case i40e_aqc_opc_send_msg_to_pf:
5907 ret = i40e_vc_process_vf_msg(pf,
5908 le16_to_cpu(event.desc.retval),
5909 le32_to_cpu(event.desc.cookie_high),
5910 le32_to_cpu(event.desc.cookie_low),
5911 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00005912 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005913 break;
5914 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005915 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005916#ifdef CONFIG_I40E_DCB
5917 rtnl_lock();
5918 ret = i40e_handle_lldp_event(pf, &event);
5919 rtnl_unlock();
5920#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005921 break;
5922 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005923 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005924 i40e_handle_lan_overflow_event(pf, &event);
5925 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00005926 case i40e_aqc_opc_send_msg_to_peer:
5927 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
5928 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07005929 case i40e_aqc_opc_nvm_erase:
5930 case i40e_aqc_opc_nvm_update:
5931 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "ARQ NVM operation completed\n");
5932 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005933 default:
5934 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00005935 "ARQ Error: Unknown event 0x%04x received\n",
5936 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005937 break;
5938 }
5939 } while (pending && (i++ < pf->adminq_work_limit));
5940
5941 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
5942 /* re-enable Admin queue interrupt cause */
5943 val = rd32(hw, I40E_PFINT_ICR0_ENA);
5944 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
5945 wr32(hw, I40E_PFINT_ICR0_ENA, val);
5946 i40e_flush(hw);
5947
5948 kfree(event.msg_buf);
5949}
5950
5951/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005952 * i40e_verify_eeprom - make sure eeprom is good to use
5953 * @pf: board private structure
5954 **/
5955static void i40e_verify_eeprom(struct i40e_pf *pf)
5956{
5957 int err;
5958
5959 err = i40e_diag_eeprom_test(&pf->hw);
5960 if (err) {
5961 /* retry in case of garbage read */
5962 err = i40e_diag_eeprom_test(&pf->hw);
5963 if (err) {
5964 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
5965 err);
5966 set_bit(__I40E_BAD_EEPROM, &pf->state);
5967 }
5968 }
5969
5970 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
5971 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
5972 clear_bit(__I40E_BAD_EEPROM, &pf->state);
5973 }
5974}
5975
5976/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00005977 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005978 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00005979 *
5980 * enable switch loop back or die - no point in a return value
5981 **/
5982static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
5983{
5984 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5985 struct i40e_vsi_context ctxt;
5986 int aq_ret;
5987
5988 ctxt.seid = pf->main_vsi_seid;
5989 ctxt.pf_num = pf->hw.pf_id;
5990 ctxt.vf_num = 0;
5991 aq_ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
5992 if (aq_ret) {
5993 dev_info(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005994 "%s couldn't get PF vsi config, err %d, aq_err %d\n",
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00005995 __func__, aq_ret, pf->hw.aq.asq_last_status);
5996 return;
5997 }
5998 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
5999 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6000 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6001
6002 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6003 if (aq_ret) {
6004 dev_info(&pf->pdev->dev,
6005 "%s: update vsi switch failed, aq_err=%d\n",
6006 __func__, vsi->back->hw.aq.asq_last_status);
6007 }
6008}
6009
6010/**
6011 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006012 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006013 *
6014 * disable switch loop back or die - no point in a return value
6015 **/
6016static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6017{
6018 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6019 struct i40e_vsi_context ctxt;
6020 int aq_ret;
6021
6022 ctxt.seid = pf->main_vsi_seid;
6023 ctxt.pf_num = pf->hw.pf_id;
6024 ctxt.vf_num = 0;
6025 aq_ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6026 if (aq_ret) {
6027 dev_info(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006028 "%s couldn't get PF vsi config, err %d, aq_err %d\n",
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006029 __func__, aq_ret, pf->hw.aq.asq_last_status);
6030 return;
6031 }
6032 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6033 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6034 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6035
6036 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6037 if (aq_ret) {
6038 dev_info(&pf->pdev->dev,
6039 "%s: update vsi switch failed, aq_err=%d\n",
6040 __func__, vsi->back->hw.aq.asq_last_status);
6041 }
6042}
6043
6044/**
Neerav Parikh51616012015-02-06 08:52:14 +00006045 * i40e_config_bridge_mode - Configure the HW bridge mode
6046 * @veb: pointer to the bridge instance
6047 *
6048 * Configure the loop back mode for the LAN VSI that is downlink to the
6049 * specified HW bridge instance. It is expected this function is called
6050 * when a new HW bridge is instantiated.
6051 **/
6052static void i40e_config_bridge_mode(struct i40e_veb *veb)
6053{
6054 struct i40e_pf *pf = veb->pf;
6055
6056 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6057 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
6058 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6059 i40e_disable_pf_switch_lb(pf);
6060 else
6061 i40e_enable_pf_switch_lb(pf);
6062}
6063
6064/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006065 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6066 * @veb: pointer to the VEB instance
6067 *
6068 * This is a recursive function that first builds the attached VSIs then
6069 * recurses in to build the next layer of VEB. We track the connections
6070 * through our own index numbers because the seid's from the HW could
6071 * change across the reset.
6072 **/
6073static int i40e_reconstitute_veb(struct i40e_veb *veb)
6074{
6075 struct i40e_vsi *ctl_vsi = NULL;
6076 struct i40e_pf *pf = veb->pf;
6077 int v, veb_idx;
6078 int ret;
6079
6080 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006081 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006082 if (pf->vsi[v] &&
6083 pf->vsi[v]->veb_idx == veb->idx &&
6084 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6085 ctl_vsi = pf->vsi[v];
6086 break;
6087 }
6088 }
6089 if (!ctl_vsi) {
6090 dev_info(&pf->pdev->dev,
6091 "missing owner VSI for veb_idx %d\n", veb->idx);
6092 ret = -ENOENT;
6093 goto end_reconstitute;
6094 }
6095 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6096 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6097 ret = i40e_add_vsi(ctl_vsi);
6098 if (ret) {
6099 dev_info(&pf->pdev->dev,
6100 "rebuild of owner VSI failed: %d\n", ret);
6101 goto end_reconstitute;
6102 }
6103 i40e_vsi_reset_stats(ctl_vsi);
6104
6105 /* create the VEB in the switch and move the VSI onto the VEB */
6106 ret = i40e_add_veb(veb, ctl_vsi);
6107 if (ret)
6108 goto end_reconstitute;
6109
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006110 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6111 veb->bridge_mode = BRIDGE_MODE_VEB;
6112 else
6113 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006114 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006115
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006116 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006117 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006118 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6119 continue;
6120
6121 if (pf->vsi[v]->veb_idx == veb->idx) {
6122 struct i40e_vsi *vsi = pf->vsi[v];
6123 vsi->uplink_seid = veb->seid;
6124 ret = i40e_add_vsi(vsi);
6125 if (ret) {
6126 dev_info(&pf->pdev->dev,
6127 "rebuild of vsi_idx %d failed: %d\n",
6128 v, ret);
6129 goto end_reconstitute;
6130 }
6131 i40e_vsi_reset_stats(vsi);
6132 }
6133 }
6134
6135 /* create any VEBs attached to this VEB - RECURSION */
6136 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6137 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6138 pf->veb[veb_idx]->uplink_seid = veb->seid;
6139 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6140 if (ret)
6141 break;
6142 }
6143 }
6144
6145end_reconstitute:
6146 return ret;
6147}
6148
6149/**
6150 * i40e_get_capabilities - get info about the HW
6151 * @pf: the PF struct
6152 **/
6153static int i40e_get_capabilities(struct i40e_pf *pf)
6154{
6155 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6156 u16 data_size;
6157 int buf_len;
6158 int err;
6159
6160 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6161 do {
6162 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6163 if (!cap_buf)
6164 return -ENOMEM;
6165
6166 /* this loads the data into the hw struct for us */
6167 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6168 &data_size,
6169 i40e_aqc_opc_list_func_capabilities,
6170 NULL);
6171 /* data loaded, buffer no longer needed */
6172 kfree(cap_buf);
6173
6174 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6175 /* retry with a larger buffer */
6176 buf_len = data_size;
6177 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6178 dev_info(&pf->pdev->dev,
6179 "capability discovery failed: aq=%d\n",
6180 pf->hw.aq.asq_last_status);
6181 return -ENODEV;
6182 }
6183 } while (err);
6184
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00006185 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
6186 (pf->hw.aq.fw_maj_ver < 2)) {
6187 pf->hw.func_caps.num_msix_vectors++;
6188 pf->hw.func_caps.num_msix_vectors_vf++;
6189 }
6190
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006191 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6192 dev_info(&pf->pdev->dev,
6193 "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",
6194 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6195 pf->hw.func_caps.num_msix_vectors,
6196 pf->hw.func_caps.num_msix_vectors_vf,
6197 pf->hw.func_caps.fd_filters_guaranteed,
6198 pf->hw.func_caps.fd_filters_best_effort,
6199 pf->hw.func_caps.num_tx_qp,
6200 pf->hw.func_caps.num_vsis);
6201
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006202#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6203 + pf->hw.func_caps.num_vfs)
6204 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6205 dev_info(&pf->pdev->dev,
6206 "got num_vsis %d, setting num_vsis to %d\n",
6207 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6208 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6209 }
6210
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006211 return 0;
6212}
6213
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006214static int i40e_vsi_clear(struct i40e_vsi *vsi);
6215
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006216/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006217 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006218 * @pf: board private structure
6219 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006220static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006221{
6222 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006223 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006224
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006225 /* quick workaround for an NVM issue that leaves a critical register
6226 * uninitialized
6227 */
6228 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6229 static const u32 hkey[] = {
6230 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6231 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6232 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6233 0x95b3a76d};
6234
6235 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6236 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6237 }
6238
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006239 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006240 return;
6241
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006242 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006243 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00006244 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006245 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006246 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006247 break;
6248 }
6249 }
6250
6251 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006252 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006253 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6254 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006255 if (!vsi) {
6256 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006257 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6258 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006259 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006260 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006261
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006262 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006263}
6264
6265/**
6266 * i40e_fdir_teardown - release the Flow Director resources
6267 * @pf: board private structure
6268 **/
6269static void i40e_fdir_teardown(struct i40e_pf *pf)
6270{
6271 int i;
6272
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006273 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00006274 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006275 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
6276 i40e_vsi_release(pf->vsi[i]);
6277 break;
6278 }
6279 }
6280}
6281
6282/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006283 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006284 * @pf: board private structure
6285 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006286 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006287 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006288static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006289{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006290 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006291 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006292 u32 v;
6293
6294 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6295 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006296 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006297
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006298 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006299
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006300 /* quiesce the VSIs and their queues that are not already DOWN */
6301 i40e_pf_quiesce_all_vsi(pf);
6302
Mitch Williams505682c2014-05-20 08:01:37 +00006303 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006304 if (pf->vsi[v])
6305 pf->vsi[v]->seid = 0;
6306 }
6307
6308 i40e_shutdown_adminq(&pf->hw);
6309
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006310 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006311 if (hw->hmc.hmc_obj) {
6312 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006313 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006314 dev_warn(&pf->pdev->dev,
6315 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006316 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006317}
6318
6319/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006320 * i40e_send_version - update firmware with driver version
6321 * @pf: PF struct
6322 */
6323static void i40e_send_version(struct i40e_pf *pf)
6324{
6325 struct i40e_driver_version dv;
6326
6327 dv.major_version = DRV_VERSION_MAJOR;
6328 dv.minor_version = DRV_VERSION_MINOR;
6329 dv.build_version = DRV_VERSION_BUILD;
6330 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006331 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006332 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6333}
6334
6335/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006336 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006337 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006338 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006339 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006340static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006341{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006342 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006343 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006344 i40e_status ret;
6345 u32 v;
6346
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006347 /* Now we wait for GRST to settle out.
6348 * We don't have to delete the VEBs or VSIs from the hw switch
6349 * because the reset will make them disappear.
6350 */
6351 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006352 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006353 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006354 set_bit(__I40E_RESET_FAILED, &pf->state);
6355 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006356 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006357 pf->pfr_count++;
6358
6359 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006360 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006361 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006362
6363 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6364 ret = i40e_init_adminq(&pf->hw);
6365 if (ret) {
6366 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006367 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006368 }
6369
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006370 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006371 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006372 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006373
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006374 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006375 ret = i40e_get_capabilities(pf);
6376 if (ret) {
6377 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
6378 ret);
6379 goto end_core_reset;
6380 }
6381
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006382 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6383 hw->func_caps.num_rx_qp,
6384 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6385 if (ret) {
6386 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6387 goto end_core_reset;
6388 }
6389 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6390 if (ret) {
6391 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6392 goto end_core_reset;
6393 }
6394
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006395#ifdef CONFIG_I40E_DCB
6396 ret = i40e_init_pf_dcb(pf);
6397 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006398 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6399 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6400 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006401 }
6402#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006403#ifdef I40E_FCOE
6404 ret = i40e_init_pf_fcoe(pf);
6405 if (ret)
6406 dev_info(&pf->pdev->dev, "init_pf_fcoe failed: %d\n", ret);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006407
Vasu Dev38e00432014-08-01 13:27:03 -07006408#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006409 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006410 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006411 if (ret)
6412 goto end_core_reset;
6413
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006414 /* driver is only interested in link up/down and module qualification
6415 * reports from firmware
6416 */
6417 ret = i40e_aq_set_phy_int_mask(&pf->hw,
6418 I40E_AQ_EVENT_LINK_UPDOWN |
6419 I40E_AQ_EVENT_MODULE_QUAL_FAIL, NULL);
6420 if (ret)
6421 dev_info(&pf->pdev->dev, "set phy mask fail, aq_err %d\n", ret);
6422
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006423 /* make sure our flow control settings are restored */
6424 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6425 if (ret)
6426 dev_info(&pf->pdev->dev, "set fc fail, aq_err %d\n", ret);
6427
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006428 /* Rebuild the VSIs and VEBs that existed before reset.
6429 * They are still in our local switch element arrays, so only
6430 * need to rebuild the switch model in the HW.
6431 *
6432 * If there were VEBs but the reconstitution failed, we'll try
6433 * try to recover minimal use by getting the basic PF VSI working.
6434 */
6435 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006436 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006437 /* find the one VEB connected to the MAC, and find orphans */
6438 for (v = 0; v < I40E_MAX_VEB; v++) {
6439 if (!pf->veb[v])
6440 continue;
6441
6442 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6443 pf->veb[v]->uplink_seid == 0) {
6444 ret = i40e_reconstitute_veb(pf->veb[v]);
6445
6446 if (!ret)
6447 continue;
6448
6449 /* If Main VEB failed, we're in deep doodoo,
6450 * so give up rebuilding the switch and set up
6451 * for minimal rebuild of PF VSI.
6452 * If orphan failed, we'll report the error
6453 * but try to keep going.
6454 */
6455 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6456 dev_info(&pf->pdev->dev,
6457 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6458 ret);
6459 pf->vsi[pf->lan_vsi]->uplink_seid
6460 = pf->mac_seid;
6461 break;
6462 } else if (pf->veb[v]->uplink_seid == 0) {
6463 dev_info(&pf->pdev->dev,
6464 "rebuild of orphan VEB failed: %d\n",
6465 ret);
6466 }
6467 }
6468 }
6469 }
6470
6471 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006472 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006473 /* no VEB, so rebuild only the Main VSI */
6474 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6475 if (ret) {
6476 dev_info(&pf->pdev->dev,
6477 "rebuild of Main VSI failed: %d\n", ret);
6478 goto end_core_reset;
6479 }
6480 }
6481
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006482 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
6483 (pf->hw.aq.fw_maj_ver < 4)) {
6484 msleep(75);
6485 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6486 if (ret)
6487 dev_info(&pf->pdev->dev, "link restart failed, aq_err=%d\n",
6488 pf->hw.aq.asq_last_status);
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006489 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006490 /* reinit the misc interrupt */
6491 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6492 ret = i40e_setup_misc_vector(pf);
6493
6494 /* restart the VSIs that were rebuilt and running before the reset */
6495 i40e_pf_unquiesce_all_vsi(pf);
6496
Mitch Williams69f64b22014-02-13 03:48:46 -08006497 if (pf->num_alloc_vfs) {
6498 for (v = 0; v < pf->num_alloc_vfs; v++)
6499 i40e_reset_vf(&pf->vf[v], true);
6500 }
6501
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006502 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006503 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006504
6505end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006506 clear_bit(__I40E_RESET_FAILED, &pf->state);
6507clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006508 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6509}
6510
6511/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006512 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006513 * @pf: board private structure
6514 *
6515 * Close up the VFs and other things in prep for a Core Reset,
6516 * then get ready to rebuild the world.
6517 **/
6518static void i40e_handle_reset_warning(struct i40e_pf *pf)
6519{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006520 i40e_prep_for_reset(pf);
6521 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006522}
6523
6524/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006525 * i40e_handle_mdd_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006526 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006527 *
6528 * Called from the MDD irq handler to identify possibly malicious vfs
6529 **/
6530static void i40e_handle_mdd_event(struct i40e_pf *pf)
6531{
6532 struct i40e_hw *hw = &pf->hw;
6533 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00006534 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006535 struct i40e_vf *vf;
6536 u32 reg;
6537 int i;
6538
6539 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
6540 return;
6541
6542 /* find what triggered the MDD event */
6543 reg = rd32(hw, I40E_GL_MDET_TX);
6544 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006545 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
6546 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006547 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006548 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006549 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006550 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006551 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
6552 I40E_GL_MDET_TX_QUEUE_SHIFT) -
6553 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006554 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006555 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 +00006556 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006557 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
6558 mdd_detected = true;
6559 }
6560 reg = rd32(hw, I40E_GL_MDET_RX);
6561 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006562 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
6563 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006564 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006565 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006566 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
6567 I40E_GL_MDET_RX_QUEUE_SHIFT) -
6568 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006569 if (netif_msg_rx_err(pf))
6570 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
6571 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006572 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
6573 mdd_detected = true;
6574 }
6575
Neerav Parikhdf430b12014-06-04 01:23:15 +00006576 if (mdd_detected) {
6577 reg = rd32(hw, I40E_PF_MDET_TX);
6578 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
6579 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006580 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00006581 pf_mdd_detected = true;
6582 }
6583 reg = rd32(hw, I40E_PF_MDET_RX);
6584 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
6585 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006586 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00006587 pf_mdd_detected = true;
6588 }
6589 /* Queue belongs to the PF, initiate a reset */
6590 if (pf_mdd_detected) {
6591 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6592 i40e_service_event_schedule(pf);
6593 }
6594 }
6595
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006596 /* see if one of the VFs needs its hand slapped */
6597 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
6598 vf = &(pf->vf[i]);
6599 reg = rd32(hw, I40E_VP_MDET_TX(i));
6600 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
6601 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
6602 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006603 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
6604 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006605 }
6606
6607 reg = rd32(hw, I40E_VP_MDET_RX(i));
6608 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
6609 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
6610 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006611 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
6612 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006613 }
6614
6615 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
6616 dev_info(&pf->pdev->dev,
6617 "Too many MDD events on VF %d, disabled\n", i);
6618 dev_info(&pf->pdev->dev,
6619 "Use PF Control I/F to re-enable the VF\n");
6620 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
6621 }
6622 }
6623
6624 /* re-enable mdd interrupt cause */
6625 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
6626 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
6627 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
6628 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
6629 i40e_flush(hw);
6630}
6631
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006632#ifdef CONFIG_I40E_VXLAN
6633/**
6634 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
6635 * @pf: board private structure
6636 **/
6637static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
6638{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006639 struct i40e_hw *hw = &pf->hw;
6640 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006641 __be16 port;
6642 int i;
6643
6644 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
6645 return;
6646
6647 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
6648
6649 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6650 if (pf->pending_vxlan_bitmap & (1 << i)) {
6651 pf->pending_vxlan_bitmap &= ~(1 << i);
6652 port = pf->vxlan_ports[i];
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07006653 if (port)
6654 ret = i40e_aq_add_udp_tunnel(hw, ntohs(port),
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006655 I40E_AQC_TUNNEL_TYPE_VXLAN,
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07006656 NULL, NULL);
6657 else
6658 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006659
6660 if (ret) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07006661 dev_info(&pf->pdev->dev,
6662 "%s vxlan port %d, index %d failed, err %d, aq_err %d\n",
6663 port ? "add" : "delete",
6664 ntohs(port), i, ret,
6665 pf->hw.aq.asq_last_status);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006666 pf->vxlan_ports[i] = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006667 }
6668 }
6669 }
6670}
6671
6672#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006673/**
6674 * i40e_service_task - Run the driver's async subtasks
6675 * @work: pointer to work_struct containing our data
6676 **/
6677static void i40e_service_task(struct work_struct *work)
6678{
6679 struct i40e_pf *pf = container_of(work,
6680 struct i40e_pf,
6681 service_task);
6682 unsigned long start_time = jiffies;
6683
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00006684 /* don't bother with service tasks if a reset is in progress */
6685 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6686 i40e_service_event_complete(pf);
6687 return;
6688 }
6689
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006690 i40e_reset_subtask(pf);
6691 i40e_handle_mdd_event(pf);
6692 i40e_vc_process_vflr_event(pf);
6693 i40e_watchdog_subtask(pf);
6694 i40e_fdir_reinit_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006695 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006696#ifdef CONFIG_I40E_VXLAN
6697 i40e_sync_vxlan_filters_subtask(pf);
6698#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006699 i40e_clean_adminq_subtask(pf);
6700
6701 i40e_service_event_complete(pf);
6702
6703 /* If the tasks have taken longer than one timer cycle or there
6704 * is more work to be done, reschedule the service task now
6705 * rather than wait for the timer to tick again.
6706 */
6707 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
6708 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
6709 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
6710 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
6711 i40e_service_event_schedule(pf);
6712}
6713
6714/**
6715 * i40e_service_timer - timer callback
6716 * @data: pointer to PF struct
6717 **/
6718static void i40e_service_timer(unsigned long data)
6719{
6720 struct i40e_pf *pf = (struct i40e_pf *)data;
6721
6722 mod_timer(&pf->service_timer,
6723 round_jiffies(jiffies + pf->service_timer_period));
6724 i40e_service_event_schedule(pf);
6725}
6726
6727/**
6728 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
6729 * @vsi: the VSI being configured
6730 **/
6731static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
6732{
6733 struct i40e_pf *pf = vsi->back;
6734
6735 switch (vsi->type) {
6736 case I40E_VSI_MAIN:
6737 vsi->alloc_queue_pairs = pf->num_lan_qps;
6738 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6739 I40E_REQ_DESCRIPTOR_MULTIPLE);
6740 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6741 vsi->num_q_vectors = pf->num_lan_msix;
6742 else
6743 vsi->num_q_vectors = 1;
6744
6745 break;
6746
6747 case I40E_VSI_FDIR:
6748 vsi->alloc_queue_pairs = 1;
6749 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
6750 I40E_REQ_DESCRIPTOR_MULTIPLE);
6751 vsi->num_q_vectors = 1;
6752 break;
6753
6754 case I40E_VSI_VMDQ2:
6755 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
6756 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6757 I40E_REQ_DESCRIPTOR_MULTIPLE);
6758 vsi->num_q_vectors = pf->num_vmdq_msix;
6759 break;
6760
6761 case I40E_VSI_SRIOV:
6762 vsi->alloc_queue_pairs = pf->num_vf_qps;
6763 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6764 I40E_REQ_DESCRIPTOR_MULTIPLE);
6765 break;
6766
Vasu Dev38e00432014-08-01 13:27:03 -07006767#ifdef I40E_FCOE
6768 case I40E_VSI_FCOE:
6769 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
6770 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6771 I40E_REQ_DESCRIPTOR_MULTIPLE);
6772 vsi->num_q_vectors = pf->num_fcoe_msix;
6773 break;
6774
6775#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006776 default:
6777 WARN_ON(1);
6778 return -ENODATA;
6779 }
6780
6781 return 0;
6782}
6783
6784/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006785 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
6786 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006787 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006788 *
6789 * On error: returns error code (negative)
6790 * On success: returns 0
6791 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006792static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006793{
6794 int size;
6795 int ret = 0;
6796
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00006797 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006798 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
6799 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
6800 if (!vsi->tx_rings)
6801 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006802 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
6803
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006804 if (alloc_qvectors) {
6805 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00006806 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006807 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
6808 if (!vsi->q_vectors) {
6809 ret = -ENOMEM;
6810 goto err_vectors;
6811 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006812 }
6813 return ret;
6814
6815err_vectors:
6816 kfree(vsi->tx_rings);
6817 return ret;
6818}
6819
6820/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006821 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
6822 * @pf: board private structure
6823 * @type: type of VSI
6824 *
6825 * On error: returns error code (negative)
6826 * On success: returns vsi index in PF (positive)
6827 **/
6828static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
6829{
6830 int ret = -ENODEV;
6831 struct i40e_vsi *vsi;
6832 int vsi_idx;
6833 int i;
6834
6835 /* Need to protect the allocation of the VSIs at the PF level */
6836 mutex_lock(&pf->switch_mutex);
6837
6838 /* VSI list may be fragmented if VSI creation/destruction has
6839 * been happening. We can afford to do a quick scan to look
6840 * for any free VSIs in the list.
6841 *
6842 * find next empty vsi slot, looping back around if necessary
6843 */
6844 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00006845 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006846 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00006847 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006848 i = 0;
6849 while (i < pf->next_vsi && pf->vsi[i])
6850 i++;
6851 }
6852
Mitch Williams505682c2014-05-20 08:01:37 +00006853 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006854 vsi_idx = i; /* Found one! */
6855 } else {
6856 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00006857 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006858 }
6859 pf->next_vsi = ++i;
6860
6861 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
6862 if (!vsi) {
6863 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00006864 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006865 }
6866 vsi->type = type;
6867 vsi->back = pf;
6868 set_bit(__I40E_DOWN, &vsi->state);
6869 vsi->flags = 0;
6870 vsi->idx = vsi_idx;
6871 vsi->rx_itr_setting = pf->rx_itr_default;
6872 vsi->tx_itr_setting = pf->tx_itr_default;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00006873 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
6874 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006875 vsi->netdev_registered = false;
6876 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
6877 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00006878 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006879
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006880 ret = i40e_set_num_rings_in_vsi(vsi);
6881 if (ret)
6882 goto err_rings;
6883
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006884 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006885 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006886 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00006887
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006888 /* Setup default MSIX irq handler for VSI */
6889 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
6890
6891 pf->vsi[vsi_idx] = vsi;
6892 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00006893 goto unlock_pf;
6894
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006895err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00006896 pf->next_vsi = i - 1;
6897 kfree(vsi);
6898unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006899 mutex_unlock(&pf->switch_mutex);
6900 return ret;
6901}
6902
6903/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006904 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
6905 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006906 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006907 *
6908 * On error: returns error code (negative)
6909 * On success: returns 0
6910 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006911static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006912{
6913 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006914 if (free_qvectors) {
6915 kfree(vsi->q_vectors);
6916 vsi->q_vectors = NULL;
6917 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006918 kfree(vsi->tx_rings);
6919 vsi->tx_rings = NULL;
6920 vsi->rx_rings = NULL;
6921}
6922
6923/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006924 * i40e_vsi_clear - Deallocate the VSI provided
6925 * @vsi: the VSI being un-configured
6926 **/
6927static int i40e_vsi_clear(struct i40e_vsi *vsi)
6928{
6929 struct i40e_pf *pf;
6930
6931 if (!vsi)
6932 return 0;
6933
6934 if (!vsi->back)
6935 goto free_vsi;
6936 pf = vsi->back;
6937
6938 mutex_lock(&pf->switch_mutex);
6939 if (!pf->vsi[vsi->idx]) {
6940 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
6941 vsi->idx, vsi->idx, vsi, vsi->type);
6942 goto unlock_vsi;
6943 }
6944
6945 if (pf->vsi[vsi->idx] != vsi) {
6946 dev_err(&pf->pdev->dev,
6947 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
6948 pf->vsi[vsi->idx]->idx,
6949 pf->vsi[vsi->idx],
6950 pf->vsi[vsi->idx]->type,
6951 vsi->idx, vsi, vsi->type);
6952 goto unlock_vsi;
6953 }
6954
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006955 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006956 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
6957 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
6958
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006959 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00006960
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006961 pf->vsi[vsi->idx] = NULL;
6962 if (vsi->idx < pf->next_vsi)
6963 pf->next_vsi = vsi->idx;
6964
6965unlock_vsi:
6966 mutex_unlock(&pf->switch_mutex);
6967free_vsi:
6968 kfree(vsi);
6969
6970 return 0;
6971}
6972
6973/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006974 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
6975 * @vsi: the VSI being cleaned
6976 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006977static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006978{
6979 int i;
6980
Greg Rose8e9dca52013-12-18 13:45:53 +00006981 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00006982 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00006983 kfree_rcu(vsi->tx_rings[i], rcu);
6984 vsi->tx_rings[i] = NULL;
6985 vsi->rx_rings[i] = NULL;
6986 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006987 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006988}
6989
6990/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006991 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
6992 * @vsi: the VSI being configured
6993 **/
6994static int i40e_alloc_rings(struct i40e_vsi *vsi)
6995{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00006996 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006997 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006998 int i;
6999
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007000 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007001 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007002 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007003 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7004 if (!tx_ring)
7005 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007006
7007 tx_ring->queue_index = i;
7008 tx_ring->reg_idx = vsi->base_queue + i;
7009 tx_ring->ring_active = false;
7010 tx_ring->vsi = vsi;
7011 tx_ring->netdev = vsi->netdev;
7012 tx_ring->dev = &pf->pdev->dev;
7013 tx_ring->count = vsi->num_desc;
7014 tx_ring->size = 0;
7015 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007016 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007017
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007018 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007019 rx_ring->queue_index = i;
7020 rx_ring->reg_idx = vsi->base_queue + i;
7021 rx_ring->ring_active = false;
7022 rx_ring->vsi = vsi;
7023 rx_ring->netdev = vsi->netdev;
7024 rx_ring->dev = &pf->pdev->dev;
7025 rx_ring->count = vsi->num_desc;
7026 rx_ring->size = 0;
7027 rx_ring->dcb_tc = 0;
7028 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
7029 set_ring_16byte_desc_enabled(rx_ring);
7030 else
7031 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007032 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007033 }
7034
7035 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007036
7037err_out:
7038 i40e_vsi_clear_rings(vsi);
7039 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007040}
7041
7042/**
7043 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7044 * @pf: board private structure
7045 * @vectors: the number of MSI-X vectors to request
7046 *
7047 * Returns the number of vectors reserved, or error
7048 **/
7049static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7050{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007051 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7052 I40E_MIN_MSIX, vectors);
7053 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007054 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007055 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007056 vectors = 0;
7057 }
7058
7059 return vectors;
7060}
7061
7062/**
7063 * i40e_init_msix - Setup the MSIX capability
7064 * @pf: board private structure
7065 *
7066 * Work with the OS to set up the MSIX vectors needed.
7067 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007068 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007069 **/
7070static int i40e_init_msix(struct i40e_pf *pf)
7071{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007072 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007073 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007074 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007075 int v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007076
7077 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7078 return -ENODEV;
7079
7080 /* The number of vectors we'll request will be comprised of:
7081 * - Add 1 for "other" cause for Admin Queue events, etc.
7082 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007083 * - Queues being used for RSS.
7084 * We don't need as many as max_rss_size vectors.
7085 * use rss_size instead in the calculation since that
7086 * is governed by number of cpus in the system.
7087 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007088 * - The number of VMDq pairs
Vasu Dev38e00432014-08-01 13:27:03 -07007089#ifdef I40E_FCOE
7090 * - The number of FCOE qps.
7091#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007092 * Once we count this up, try the request.
7093 *
7094 * If we can't get what we want, we'll simplify to nearly nothing
7095 * and try again. If that still fails, we punt.
7096 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007097 vectors_left = hw->func_caps.num_msix_vectors;
7098 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007099
Shannon Nelson1e200e42015-02-27 09:15:24 +00007100 /* reserve one vector for miscellaneous handler */
7101 if (vectors_left) {
7102 v_budget++;
7103 vectors_left--;
7104 }
7105
7106 /* reserve vectors for the main PF traffic queues */
7107 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7108 vectors_left -= pf->num_lan_msix;
7109 v_budget += pf->num_lan_msix;
7110
7111 /* reserve one vector for sideband flow director */
7112 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7113 if (vectors_left) {
7114 v_budget++;
7115 vectors_left--;
7116 } else {
7117 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7118 }
7119 }
John W Linville83840e42015-01-14 03:06:28 +00007120
Vasu Dev38e00432014-08-01 13:27:03 -07007121#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007122 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007123 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007124 if (!vectors_left)
7125 pf->num_fcoe_msix = 0;
7126 else if (vectors_left >= pf->num_fcoe_qps)
7127 pf->num_fcoe_msix = pf->num_fcoe_qps;
7128 else
7129 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007130 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007131 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007132 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007133
Vasu Dev38e00432014-08-01 13:27:03 -07007134#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007135 /* any vectors left over go for VMDq support */
7136 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7137 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7138 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7139
7140 /* if we're short on vectors for what's desired, we limit
7141 * the queues per vmdq. If this is still more than are
7142 * available, the user will need to change the number of
7143 * queues/vectors used by the PF later with the ethtool
7144 * channels command
7145 */
7146 if (vmdq_vecs < vmdq_vecs_wanted)
7147 pf->num_vmdq_qps = 1;
7148 pf->num_vmdq_msix = pf->num_vmdq_qps;
7149
7150 v_budget += vmdq_vecs;
7151 vectors_left -= vmdq_vecs;
7152 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007153
7154 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7155 GFP_KERNEL);
7156 if (!pf->msix_entries)
7157 return -ENOMEM;
7158
7159 for (i = 0; i < v_budget; i++)
7160 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007161 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007162
Shannon Nelson3b444392015-02-26 16:15:57 +00007163 if (v_actual != v_budget) {
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007164 /* If we have limited resources, we will start with no vectors
7165 * for the special features and then allocate vectors to some
7166 * of these features based on the policy and at the end disable
7167 * the features that did not get any vectors.
7168 */
Vasu Dev38e00432014-08-01 13:27:03 -07007169#ifdef I40E_FCOE
7170 pf->num_fcoe_qps = 0;
7171 pf->num_fcoe_msix = 0;
7172#endif
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007173 pf->num_vmdq_msix = 0;
7174 }
7175
Shannon Nelson3b444392015-02-26 16:15:57 +00007176 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007177 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7178 kfree(pf->msix_entries);
7179 pf->msix_entries = NULL;
7180 return -ENODEV;
7181
Shannon Nelson3b444392015-02-26 16:15:57 +00007182 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007183 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007184 pf->num_vmdq_vsis = 0;
7185 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007186 pf->num_lan_qps = 1;
7187 pf->num_lan_msix = 1;
7188
Shannon Nelson3b444392015-02-26 16:15:57 +00007189 } else if (v_actual != v_budget) {
7190 int vec;
7191
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007192 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007193 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007194
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007195 /* Scale vector usage down */
7196 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007197 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007198 pf->num_vmdq_qps = 1;
7199 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007200
7201 /* partition out the remaining vectors */
7202 switch (vec) {
7203 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007204 pf->num_lan_msix = 1;
7205 break;
7206 case 3:
Vasu Dev38e00432014-08-01 13:27:03 -07007207#ifdef I40E_FCOE
7208 /* give one vector to FCoE */
7209 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7210 pf->num_lan_msix = 1;
7211 pf->num_fcoe_msix = 1;
7212 }
7213#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007214 pf->num_lan_msix = 2;
Vasu Dev38e00432014-08-01 13:27:03 -07007215#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007216 break;
7217 default:
Vasu Dev38e00432014-08-01 13:27:03 -07007218#ifdef I40E_FCOE
7219 /* give one vector to FCoE */
7220 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7221 pf->num_fcoe_msix = 1;
7222 vec--;
7223 }
7224#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007225 /* give the rest to the PF */
7226 pf->num_lan_msix = min_t(int, vec, pf->num_lan_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007227 break;
7228 }
7229 }
7230
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007231 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7232 (pf->num_vmdq_msix == 0)) {
7233 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7234 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7235 }
Vasu Dev38e00432014-08-01 13:27:03 -07007236#ifdef I40E_FCOE
7237
7238 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7239 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7240 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7241 }
7242#endif
Shannon Nelson3b444392015-02-26 16:15:57 +00007243 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007244}
7245
7246/**
Greg Rose90e04072014-03-06 08:59:57 +00007247 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007248 * @vsi: the VSI being configured
7249 * @v_idx: index of the vector in the vsi struct
7250 *
7251 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7252 **/
Greg Rose90e04072014-03-06 08:59:57 +00007253static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00007254{
7255 struct i40e_q_vector *q_vector;
7256
7257 /* allocate q_vector */
7258 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7259 if (!q_vector)
7260 return -ENOMEM;
7261
7262 q_vector->vsi = vsi;
7263 q_vector->v_idx = v_idx;
7264 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
7265 if (vsi->netdev)
7266 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007267 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007268
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007269 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7270 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7271
Alexander Duyck493fb302013-09-28 07:01:44 +00007272 /* tie q_vector and vsi together */
7273 vsi->q_vectors[v_idx] = q_vector;
7274
7275 return 0;
7276}
7277
7278/**
Greg Rose90e04072014-03-06 08:59:57 +00007279 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007280 * @vsi: the VSI being configured
7281 *
7282 * We allocate one q_vector per queue interrupt. If allocation fails we
7283 * return -ENOMEM.
7284 **/
Greg Rose90e04072014-03-06 08:59:57 +00007285static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007286{
7287 struct i40e_pf *pf = vsi->back;
7288 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00007289 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007290
7291 /* if not MSIX, give the one vector only to the LAN VSI */
7292 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7293 num_q_vectors = vsi->num_q_vectors;
7294 else if (vsi == pf->vsi[pf->lan_vsi])
7295 num_q_vectors = 1;
7296 else
7297 return -EINVAL;
7298
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007299 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00007300 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00007301 if (err)
7302 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007303 }
7304
7305 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007306
7307err_out:
7308 while (v_idx--)
7309 i40e_free_q_vector(vsi, v_idx);
7310
7311 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007312}
7313
7314/**
7315 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7316 * @pf: board private structure to initialize
7317 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007318static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007319{
Shannon Nelson3b444392015-02-26 16:15:57 +00007320 int vectors = 0;
7321 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007322
7323 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007324 vectors = i40e_init_msix(pf);
7325 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007326 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007327#ifdef I40E_FCOE
7328 I40E_FLAG_FCOE_ENABLED |
7329#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007330 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007331 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007332 I40E_FLAG_SRIOV_ENABLED |
7333 I40E_FLAG_FD_SB_ENABLED |
7334 I40E_FLAG_FD_ATR_ENABLED |
7335 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007336
7337 /* rework the queue expectations without MSIX */
7338 i40e_determine_queue_usage(pf);
7339 }
7340 }
7341
7342 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7343 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007344 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007345 vectors = pci_enable_msi(pf->pdev);
7346 if (vectors < 0) {
7347 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7348 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007349 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7350 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007351 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007352 }
7353
Shannon Nelson958a3e32013-09-28 07:13:28 +00007354 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007355 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007356
Shannon Nelson3b444392015-02-26 16:15:57 +00007357 /* set up vector assignment tracking */
7358 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7359 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007360 if (!pf->irq_pile) {
7361 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7362 return -ENOMEM;
7363 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007364 pf->irq_pile->num_entries = vectors;
7365 pf->irq_pile->search_hint = 0;
7366
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007367 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007368 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007369
7370 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007371}
7372
7373/**
7374 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7375 * @pf: board private structure
7376 *
7377 * This sets up the handler for MSIX 0, which is used to manage the
7378 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7379 * when in MSI or Legacy interrupt mode.
7380 **/
7381static int i40e_setup_misc_vector(struct i40e_pf *pf)
7382{
7383 struct i40e_hw *hw = &pf->hw;
7384 int err = 0;
7385
7386 /* Only request the irq if this is the first time through, and
7387 * not when we're rebuilding after a Reset
7388 */
7389 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7390 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007391 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007392 if (err) {
7393 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007394 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007395 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007396 return -EFAULT;
7397 }
7398 }
7399
Jacob Kellerab437b52014-12-14 01:55:08 +00007400 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007401
7402 /* associate no queues to the misc vector */
7403 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
7404 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
7405
7406 i40e_flush(hw);
7407
7408 i40e_irq_dynamic_enable_icr0(pf);
7409
7410 return err;
7411}
7412
7413/**
7414 * i40e_config_rss - Prepare for RSS if used
7415 * @pf: board private structure
7416 **/
7417static int i40e_config_rss(struct i40e_pf *pf)
7418{
Eric Dumazet22f258a2014-11-16 06:23:13 -08007419 u32 rss_key[I40E_PFQF_HKEY_MAX_INDEX + 1];
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00007420 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00007421 struct i40e_hw *hw = &pf->hw;
7422 u32 lut = 0;
7423 int i, j;
7424 u64 hena;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007425 u32 reg_val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007426
Eric Dumazet22f258a2014-11-16 06:23:13 -08007427 netdev_rss_key_fill(rss_key, sizeof(rss_key));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007428 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Eric Dumazet22f258a2014-11-16 06:23:13 -08007429 wr32(hw, I40E_PFQF_HKEY(i), rss_key[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007430
7431 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
7432 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
7433 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00007434 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007435 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
7436 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
7437
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00007438 vsi->rss_size = min_t(int, pf->rss_size, vsi->num_queue_pairs);
7439
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007440 /* Check capability and Set table size and register per hw expectation*/
7441 reg_val = rd32(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaind9e894e2015-02-27 09:15:30 +00007442 if (pf->rss_table_size == 512)
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007443 reg_val |= I40E_PFQF_CTL_0_HASHLUTSIZE_512;
Anjali Singhai Jaind9e894e2015-02-27 09:15:30 +00007444 else
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007445 reg_val &= ~I40E_PFQF_CTL_0_HASHLUTSIZE_512;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007446 wr32(hw, I40E_PFQF_CTL_0, reg_val);
7447
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007448 /* Populate the LUT with max no. of queues in round robin fashion */
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007449 for (i = 0, j = 0; i < pf->rss_table_size; i++, j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007450
7451 /* The assumption is that lan qp count will be the highest
7452 * qp count for any PF VSI that needs RSS.
7453 * If multiple VSIs need RSS support, all the qp counts
7454 * for those VSIs should be a power of 2 for RSS to work.
7455 * If LAN VSI is the only consumer for RSS then this requirement
7456 * is not necessary.
7457 */
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00007458 if (j == vsi->rss_size)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007459 j = 0;
7460 /* lut = 4-byte sliding window of 4 lut entries */
7461 lut = (lut << 8) | (j &
7462 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
7463 /* On i = 3, we have 4 entries in lut; write to the register */
7464 if ((i & 3) == 3)
7465 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
7466 }
7467 i40e_flush(hw);
7468
7469 return 0;
7470}
7471
7472/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007473 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
7474 * @pf: board private structure
7475 * @queue_count: the requested queue count for rss.
7476 *
7477 * returns 0 if rss is not enabled, if enabled returns the final rss queue
7478 * count which may be different from the requested queue count.
7479 **/
7480int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
7481{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007482 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
7483 int new_rss_size;
7484
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007485 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
7486 return 0;
7487
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007488 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007489
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007490 if (queue_count != vsi->num_queue_pairs) {
7491 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007492 i40e_prep_for_reset(pf);
7493
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007494 pf->rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007495
7496 i40e_reset_and_rebuild(pf, true);
7497 i40e_config_rss(pf);
7498 }
7499 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
7500 return pf->rss_size;
7501}
7502
7503/**
Greg Rosef4492db2015-02-06 08:52:12 +00007504 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
7505 * @pf: board private structure
7506 **/
7507i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
7508{
7509 i40e_status status;
7510 bool min_valid, max_valid;
7511 u32 max_bw, min_bw;
7512
7513 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
7514 &min_valid, &max_valid);
7515
7516 if (!status) {
7517 if (min_valid)
7518 pf->npar_min_bw = min_bw;
7519 if (max_valid)
7520 pf->npar_max_bw = max_bw;
7521 }
7522
7523 return status;
7524}
7525
7526/**
7527 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
7528 * @pf: board private structure
7529 **/
7530i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
7531{
7532 struct i40e_aqc_configure_partition_bw_data bw_data;
7533 i40e_status status;
7534
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007535 /* Set the valid bit for this PF */
Greg Rosef4492db2015-02-06 08:52:12 +00007536 bw_data.pf_valid_bits = cpu_to_le16(1 << pf->hw.pf_id);
7537 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
7538 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
7539
7540 /* Set the new bandwidths */
7541 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
7542
7543 return status;
7544}
7545
7546/**
7547 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
7548 * @pf: board private structure
7549 **/
7550i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
7551{
7552 /* Commit temporary BW setting to permanent NVM image */
7553 enum i40e_admin_queue_err last_aq_status;
7554 i40e_status ret;
7555 u16 nvm_word;
7556
7557 if (pf->hw.partition_id != 1) {
7558 dev_info(&pf->pdev->dev,
7559 "Commit BW only works on partition 1! This is partition %d",
7560 pf->hw.partition_id);
7561 ret = I40E_NOT_SUPPORTED;
7562 goto bw_commit_out;
7563 }
7564
7565 /* Acquire NVM for read access */
7566 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
7567 last_aq_status = pf->hw.aq.asq_last_status;
7568 if (ret) {
7569 dev_info(&pf->pdev->dev,
7570 "Cannot acquire NVM for read access, err %d: aq_err %d\n",
7571 ret, last_aq_status);
7572 goto bw_commit_out;
7573 }
7574
7575 /* Read word 0x10 of NVM - SW compatibility word 1 */
7576 ret = i40e_aq_read_nvm(&pf->hw,
7577 I40E_SR_NVM_CONTROL_WORD,
7578 0x10, sizeof(nvm_word), &nvm_word,
7579 false, NULL);
7580 /* Save off last admin queue command status before releasing
7581 * the NVM
7582 */
7583 last_aq_status = pf->hw.aq.asq_last_status;
7584 i40e_release_nvm(&pf->hw);
7585 if (ret) {
7586 dev_info(&pf->pdev->dev, "NVM read error, err %d aq_err %d\n",
7587 ret, last_aq_status);
7588 goto bw_commit_out;
7589 }
7590
7591 /* Wait a bit for NVM release to complete */
7592 msleep(50);
7593
7594 /* Acquire NVM for write access */
7595 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
7596 last_aq_status = pf->hw.aq.asq_last_status;
7597 if (ret) {
7598 dev_info(&pf->pdev->dev,
7599 "Cannot acquire NVM for write access, err %d: aq_err %d\n",
7600 ret, last_aq_status);
7601 goto bw_commit_out;
7602 }
7603 /* Write it back out unchanged to initiate update NVM,
7604 * which will force a write of the shadow (alt) RAM to
7605 * the NVM - thus storing the bandwidth values permanently.
7606 */
7607 ret = i40e_aq_update_nvm(&pf->hw,
7608 I40E_SR_NVM_CONTROL_WORD,
7609 0x10, sizeof(nvm_word),
7610 &nvm_word, true, NULL);
7611 /* Save off last admin queue command status before releasing
7612 * the NVM
7613 */
7614 last_aq_status = pf->hw.aq.asq_last_status;
7615 i40e_release_nvm(&pf->hw);
7616 if (ret)
7617 dev_info(&pf->pdev->dev,
7618 "BW settings NOT SAVED, err %d aq_err %d\n",
7619 ret, last_aq_status);
7620bw_commit_out:
7621
7622 return ret;
7623}
7624
7625/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007626 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
7627 * @pf: board private structure to initialize
7628 *
7629 * i40e_sw_init initializes the Adapter private data structure.
7630 * Fields are initialized based on PCI device information and
7631 * OS network device settings (MTU size).
7632 **/
7633static int i40e_sw_init(struct i40e_pf *pf)
7634{
7635 int err = 0;
7636 int size;
7637
7638 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
7639 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00007640 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007641 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
7642 if (I40E_DEBUG_USER & debug)
7643 pf->hw.debug_mask = debug;
7644 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
7645 I40E_DEFAULT_MSG_ENABLE);
7646 }
7647
7648 /* Set default capability flags */
7649 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
7650 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00007651 I40E_FLAG_MSIX_ENABLED;
7652
7653 if (iommu_present(&pci_bus_type))
7654 pf->flags |= I40E_FLAG_RX_PS_ENABLED;
7655 else
7656 pf->flags |= I40E_FLAG_RX_1BUF_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007657
Mitch Williamsca99eb92014-04-04 04:43:07 +00007658 /* Set default ITR */
7659 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
7660 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
7661
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00007662 /* Depending on PF configurations, it is possible that the RSS
7663 * maximum might end up larger than the available queues
7664 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007665 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Paul M Stillwell Jrec9a7db2014-07-09 07:46:10 +00007666 pf->rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007667 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00007668 pf->rss_size_max = min_t(int, pf->rss_size_max,
7669 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007670 if (pf->hw.func_caps.rss) {
7671 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00007672 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007673 }
7674
Catherine Sullivan2050bc62013-12-18 13:46:03 +00007675 /* MFP mode enabled */
7676 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
7677 pf->flags |= I40E_FLAG_MFP_ENABLED;
7678 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00007679 if (i40e_get_npar_bw_setting(pf))
7680 dev_warn(&pf->pdev->dev,
7681 "Could not get NPAR bw settings\n");
7682 else
7683 dev_info(&pf->pdev->dev,
7684 "Min BW = %8.8x, Max BW = %8.8x\n",
7685 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00007686 }
7687
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007688 /* FW/NVM is not yet fixed in this regard */
7689 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
7690 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
7691 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
7692 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007693 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007694 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007695 } else {
7696 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00007697 "Flow Director Sideband mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007698 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007699 pf->fdir_pf_filter_count =
7700 pf->hw.func_caps.fd_filters_guaranteed;
7701 pf->hw.fdir_shared_filter_count =
7702 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007703 }
7704
7705 if (pf->hw.func_caps.vmdq) {
7706 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
7707 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
7708 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
7709 }
7710
Vasu Dev38e00432014-08-01 13:27:03 -07007711#ifdef I40E_FCOE
7712 err = i40e_init_pf_fcoe(pf);
7713 if (err)
7714 dev_info(&pf->pdev->dev, "init_pf_fcoe failed: %d\n", err);
7715
7716#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007717#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f12014-12-11 07:06:34 +00007718 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007719 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
7720 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
7721 pf->num_req_vfs = min_t(int,
7722 pf->hw.func_caps.num_vfs,
7723 I40E_MAX_VF_COUNT);
7724 }
7725#endif /* CONFIG_PCI_IOV */
7726 pf->eeprom_version = 0xDEAD;
7727 pf->lan_veb = I40E_NO_VEB;
7728 pf->lan_vsi = I40E_NO_VSI;
7729
7730 /* set up queue assignment tracking */
7731 size = sizeof(struct i40e_lump_tracking)
7732 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
7733 pf->qp_pile = kzalloc(size, GFP_KERNEL);
7734 if (!pf->qp_pile) {
7735 err = -ENOMEM;
7736 goto sw_init_done;
7737 }
7738 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
7739 pf->qp_pile->search_hint = 0;
7740
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00007741 pf->tx_timeout_recovery_level = 1;
7742
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007743 mutex_init(&pf->switch_mutex);
7744
Greg Rosec668a122015-02-26 16:10:39 +00007745 /* If NPAR is enabled nudge the Tx scheduler */
7746 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
7747 i40e_set_npar_bw_setting(pf);
7748
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007749sw_init_done:
7750 return err;
7751}
7752
7753/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00007754 * i40e_set_ntuple - set the ntuple feature flag and take action
7755 * @pf: board private structure to initialize
7756 * @features: the feature set that the stack is suggesting
7757 *
7758 * returns a bool to indicate if reset needs to happen
7759 **/
7760bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
7761{
7762 bool need_reset = false;
7763
7764 /* Check if Flow Director n-tuple support was enabled or disabled. If
7765 * the state changed, we need to reset.
7766 */
7767 if (features & NETIF_F_NTUPLE) {
7768 /* Enable filters and mark for reset */
7769 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
7770 need_reset = true;
7771 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7772 } else {
7773 /* turn off filters, mark for reset and clear SW filter list */
7774 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7775 need_reset = true;
7776 i40e_fdir_filter_exit(pf);
7777 }
7778 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00007779 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00007780 /* reset fd counters */
7781 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
7782 pf->fdir_pf_active_filters = 0;
7783 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04007784 if (I40E_DEBUG_FD & pf->hw.debug_mask)
7785 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00007786 /* if ATR was auto disabled it can be re-enabled. */
7787 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
7788 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
7789 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00007790 }
7791 return need_reset;
7792}
7793
7794/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007795 * i40e_set_features - set the netdev feature flags
7796 * @netdev: ptr to the netdev being adjusted
7797 * @features: the feature set that the stack is suggesting
7798 **/
7799static int i40e_set_features(struct net_device *netdev,
7800 netdev_features_t features)
7801{
7802 struct i40e_netdev_priv *np = netdev_priv(netdev);
7803 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00007804 struct i40e_pf *pf = vsi->back;
7805 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007806
7807 if (features & NETIF_F_HW_VLAN_CTAG_RX)
7808 i40e_vlan_stripping_enable(vsi);
7809 else
7810 i40e_vlan_stripping_disable(vsi);
7811
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00007812 need_reset = i40e_set_ntuple(pf, features);
7813
7814 if (need_reset)
7815 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
7816
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007817 return 0;
7818}
7819
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007820#ifdef CONFIG_I40E_VXLAN
7821/**
7822 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
7823 * @pf: board private structure
7824 * @port: The UDP port to look up
7825 *
7826 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
7827 **/
7828static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
7829{
7830 u8 i;
7831
7832 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
7833 if (pf->vxlan_ports[i] == port)
7834 return i;
7835 }
7836
7837 return i;
7838}
7839
7840/**
7841 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
7842 * @netdev: This physical port's netdev
7843 * @sa_family: Socket Family that VXLAN is notifying us about
7844 * @port: New UDP port number that VXLAN started listening to
7845 **/
7846static void i40e_add_vxlan_port(struct net_device *netdev,
7847 sa_family_t sa_family, __be16 port)
7848{
7849 struct i40e_netdev_priv *np = netdev_priv(netdev);
7850 struct i40e_vsi *vsi = np->vsi;
7851 struct i40e_pf *pf = vsi->back;
7852 u8 next_idx;
7853 u8 idx;
7854
7855 if (sa_family == AF_INET6)
7856 return;
7857
7858 idx = i40e_get_vxlan_port_idx(pf, port);
7859
7860 /* Check if port already exists */
7861 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007862 netdev_info(netdev, "vxlan port %d already offloaded\n",
7863 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007864 return;
7865 }
7866
7867 /* Now check if there is space to add the new port */
7868 next_idx = i40e_get_vxlan_port_idx(pf, 0);
7869
7870 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007871 netdev_info(netdev, "maximum number of vxlan UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007872 ntohs(port));
7873 return;
7874 }
7875
7876 /* New port: add it and mark its index in the bitmap */
7877 pf->vxlan_ports[next_idx] = port;
7878 pf->pending_vxlan_bitmap |= (1 << next_idx);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007879 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007880
7881 dev_info(&pf->pdev->dev, "adding vxlan port %d\n", ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007882}
7883
7884/**
7885 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
7886 * @netdev: This physical port's netdev
7887 * @sa_family: Socket Family that VXLAN is notifying us about
7888 * @port: UDP port number that VXLAN stopped listening to
7889 **/
7890static void i40e_del_vxlan_port(struct net_device *netdev,
7891 sa_family_t sa_family, __be16 port)
7892{
7893 struct i40e_netdev_priv *np = netdev_priv(netdev);
7894 struct i40e_vsi *vsi = np->vsi;
7895 struct i40e_pf *pf = vsi->back;
7896 u8 idx;
7897
7898 if (sa_family == AF_INET6)
7899 return;
7900
7901 idx = i40e_get_vxlan_port_idx(pf, port);
7902
7903 /* Check if port already exists */
7904 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
7905 /* if port exists, set it to 0 (mark for deletion)
7906 * and make it pending
7907 */
7908 pf->vxlan_ports[idx] = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007909 pf->pending_vxlan_bitmap |= (1 << idx);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007910 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007911
7912 dev_info(&pf->pdev->dev, "deleting vxlan port %d\n",
7913 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007914 } else {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007915 netdev_warn(netdev, "vxlan port %d was not found, not deleting\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007916 ntohs(port));
7917 }
7918}
7919
7920#endif
Neerav Parikh1f224ad2014-02-12 01:45:31 +00007921static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01007922 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00007923{
7924 struct i40e_netdev_priv *np = netdev_priv(netdev);
7925 struct i40e_pf *pf = np->vsi->back;
7926 struct i40e_hw *hw = &pf->hw;
7927
7928 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
7929 return -EOPNOTSUPP;
7930
7931 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
7932 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
7933
7934 return 0;
7935}
7936
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08007937/**
7938 * i40e_ndo_fdb_add - add an entry to the hardware database
7939 * @ndm: the input from the stack
7940 * @tb: pointer to array of nladdr (unused)
7941 * @dev: the net device pointer
7942 * @addr: the MAC address entry being added
7943 * @flags: instructions from stack about fdb operation
7944 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00007945static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
7946 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01007947 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00007948 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00007949{
7950 struct i40e_netdev_priv *np = netdev_priv(dev);
7951 struct i40e_pf *pf = np->vsi->back;
7952 int err = 0;
7953
7954 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
7955 return -EOPNOTSUPP;
7956
Or Gerlitz65891fe2014-12-14 18:19:05 +02007957 if (vid) {
7958 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
7959 return -EINVAL;
7960 }
7961
Greg Rose4ba0dea2014-03-06 08:59:55 +00007962 /* Hardware does not support aging addresses so if a
7963 * ndm_state is given only allow permanent addresses
7964 */
7965 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
7966 netdev_info(dev, "FDB only supports static addresses\n");
7967 return -EINVAL;
7968 }
7969
7970 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
7971 err = dev_uc_add_excl(dev, addr);
7972 else if (is_multicast_ether_addr(addr))
7973 err = dev_mc_add_excl(dev, addr);
7974 else
7975 err = -EINVAL;
7976
7977 /* Only return duplicate errors if NLM_F_EXCL is set */
7978 if (err == -EEXIST && !(flags & NLM_F_EXCL))
7979 err = 0;
7980
7981 return err;
7982}
7983
Neerav Parikh51616012015-02-06 08:52:14 +00007984#ifdef HAVE_BRIDGE_ATTRIBS
7985/**
7986 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
7987 * @dev: the netdev being configured
7988 * @nlh: RTNL message
7989 *
7990 * Inserts a new hardware bridge if not already created and
7991 * enables the bridging mode requested (VEB or VEPA). If the
7992 * hardware bridge has already been inserted and the request
7993 * is to change the mode then that requires a PF reset to
7994 * allow rebuild of the components with required hardware
7995 * bridge mode enabled.
7996 **/
7997static int i40e_ndo_bridge_setlink(struct net_device *dev,
7998 struct nlmsghdr *nlh)
7999{
8000 struct i40e_netdev_priv *np = netdev_priv(dev);
8001 struct i40e_vsi *vsi = np->vsi;
8002 struct i40e_pf *pf = vsi->back;
8003 struct i40e_veb *veb = NULL;
8004 struct nlattr *attr, *br_spec;
8005 int i, rem;
8006
8007 /* Only for PF VSI for now */
8008 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8009 return -EOPNOTSUPP;
8010
8011 /* Find the HW bridge for PF VSI */
8012 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8013 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8014 veb = pf->veb[i];
8015 }
8016
8017 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8018
8019 nla_for_each_nested(attr, br_spec, rem) {
8020 __u16 mode;
8021
8022 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8023 continue;
8024
8025 mode = nla_get_u16(attr);
8026 if ((mode != BRIDGE_MODE_VEPA) &&
8027 (mode != BRIDGE_MODE_VEB))
8028 return -EINVAL;
8029
8030 /* Insert a new HW bridge */
8031 if (!veb) {
8032 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8033 vsi->tc_config.enabled_tc);
8034 if (veb) {
8035 veb->bridge_mode = mode;
8036 i40e_config_bridge_mode(veb);
8037 } else {
8038 /* No Bridge HW offload available */
8039 return -ENOENT;
8040 }
8041 break;
8042 } else if (mode != veb->bridge_mode) {
8043 /* Existing HW bridge but different mode needs reset */
8044 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008045 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
8046 if (mode == BRIDGE_MODE_VEB)
8047 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
8048 else
8049 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
8050 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00008051 break;
8052 }
8053 }
8054
8055 return 0;
8056}
8057
8058/**
8059 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
8060 * @skb: skb buff
8061 * @pid: process id
8062 * @seq: RTNL message seq #
8063 * @dev: the netdev being configured
8064 * @filter_mask: unused
8065 *
8066 * Return the mode in which the hardware bridge is operating in
8067 * i.e VEB or VEPA.
8068 **/
8069#ifdef HAVE_BRIDGE_FILTER
8070static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8071 struct net_device *dev,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02008072 u32 __always_unused filter_mask, int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00008073#else
8074static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02008075 struct net_device *dev, int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00008076#endif /* HAVE_BRIDGE_FILTER */
8077{
8078 struct i40e_netdev_priv *np = netdev_priv(dev);
8079 struct i40e_vsi *vsi = np->vsi;
8080 struct i40e_pf *pf = vsi->back;
8081 struct i40e_veb *veb = NULL;
8082 int i;
8083
8084 /* Only for PF VSI for now */
8085 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8086 return -EOPNOTSUPP;
8087
8088 /* Find the HW bridge for the PF VSI */
8089 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8090 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8091 veb = pf->veb[i];
8092 }
8093
8094 if (!veb)
8095 return 0;
8096
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02008097 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
8098 nlflags);
Neerav Parikh51616012015-02-06 08:52:14 +00008099}
8100#endif /* HAVE_BRIDGE_ATTRIBS */
8101
Shannon Nelson37a29732015-02-27 09:15:19 +00008102static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008103 .ndo_open = i40e_open,
8104 .ndo_stop = i40e_close,
8105 .ndo_start_xmit = i40e_lan_xmit_frame,
8106 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
8107 .ndo_set_rx_mode = i40e_set_rx_mode,
8108 .ndo_validate_addr = eth_validate_addr,
8109 .ndo_set_mac_address = i40e_set_mac,
8110 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008111 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008112 .ndo_tx_timeout = i40e_tx_timeout,
8113 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
8114 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
8115#ifdef CONFIG_NET_POLL_CONTROLLER
8116 .ndo_poll_controller = i40e_netpoll,
8117#endif
8118 .ndo_setup_tc = i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07008119#ifdef I40E_FCOE
8120 .ndo_fcoe_enable = i40e_fcoe_enable,
8121 .ndo_fcoe_disable = i40e_fcoe_disable,
8122#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008123 .ndo_set_features = i40e_set_features,
8124 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
8125 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04008126 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008127 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00008128 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00008129 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008130#ifdef CONFIG_I40E_VXLAN
8131 .ndo_add_vxlan_port = i40e_add_vxlan_port,
8132 .ndo_del_vxlan_port = i40e_del_vxlan_port,
8133#endif
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008134 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008135 .ndo_fdb_add = i40e_ndo_fdb_add,
Neerav Parikh51616012015-02-06 08:52:14 +00008136#ifdef HAVE_BRIDGE_ATTRIBS
8137 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
8138 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
8139#endif /* HAVE_BRIDGE_ATTRIBS */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008140};
8141
8142/**
8143 * i40e_config_netdev - Setup the netdev flags
8144 * @vsi: the VSI being configured
8145 *
8146 * Returns 0 on success, negative value on failure
8147 **/
8148static int i40e_config_netdev(struct i40e_vsi *vsi)
8149{
Greg Rose1a103702013-11-28 06:42:39 +00008150 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008151 struct i40e_pf *pf = vsi->back;
8152 struct i40e_hw *hw = &pf->hw;
8153 struct i40e_netdev_priv *np;
8154 struct net_device *netdev;
8155 u8 mac_addr[ETH_ALEN];
8156 int etherdev_size;
8157
8158 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008159 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008160 if (!netdev)
8161 return -ENOMEM;
8162
8163 vsi->netdev = netdev;
8164 np = netdev_priv(netdev);
8165 np->vsi = vsi;
8166
Or Gerlitzd70e9412014-03-18 10:36:45 +02008167 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008168 NETIF_F_GSO_UDP_TUNNEL |
Or Gerlitzd70e9412014-03-18 10:36:45 +02008169 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008170
8171 netdev->features = NETIF_F_SG |
8172 NETIF_F_IP_CSUM |
8173 NETIF_F_SCTP_CSUM |
8174 NETIF_F_HIGHDMA |
8175 NETIF_F_GSO_UDP_TUNNEL |
8176 NETIF_F_HW_VLAN_CTAG_TX |
8177 NETIF_F_HW_VLAN_CTAG_RX |
8178 NETIF_F_HW_VLAN_CTAG_FILTER |
8179 NETIF_F_IPV6_CSUM |
8180 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00008181 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008182 NETIF_F_TSO6 |
8183 NETIF_F_RXCSUM |
8184 NETIF_F_RXHASH |
8185 0;
8186
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00008187 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
8188 netdev->features |= NETIF_F_NTUPLE;
8189
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008190 /* copy netdev features into list of user selectable features */
8191 netdev->hw_features |= netdev->features;
8192
8193 if (vsi->type == I40E_VSI_MAIN) {
8194 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00008195 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Shannon Nelson30650cc2014-07-29 04:01:50 +00008196 /* The following steps are necessary to prevent reception
8197 * of tagged packets - some older NVM configurations load a
8198 * default a MAC-VLAN filter that accepts any tagged packet
8199 * which must be replaced by a normal filter.
Greg Rose8c27d422014-05-22 06:31:56 +00008200 */
Shannon Nelson30650cc2014-07-29 04:01:50 +00008201 if (!i40e_rm_default_mac_filter(vsi, mac_addr))
8202 i40e_add_filter(vsi, mac_addr,
8203 I40E_VLAN_ANY, false, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008204 } else {
8205 /* relate the VSI_VMDQ name to the VSI_MAIN name */
8206 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
8207 pf->vsi[pf->lan_vsi]->netdev->name);
8208 random_ether_addr(mac_addr);
8209 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
8210 }
Greg Rose1a103702013-11-28 06:42:39 +00008211 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008212
Greg Rose9a173902014-05-22 06:32:02 +00008213 ether_addr_copy(netdev->dev_addr, mac_addr);
8214 ether_addr_copy(netdev->perm_addr, mac_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008215 /* vlan gets same features (except vlan offload)
8216 * after any tweaks for specific VSI types
8217 */
8218 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
8219 NETIF_F_HW_VLAN_CTAG_RX |
8220 NETIF_F_HW_VLAN_CTAG_FILTER);
8221 netdev->priv_flags |= IFF_UNICAST_FLT;
8222 netdev->priv_flags |= IFF_SUPP_NOFCS;
8223 /* Setup netdev TC information */
8224 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
8225
8226 netdev->netdev_ops = &i40e_netdev_ops;
8227 netdev->watchdog_timeo = 5 * HZ;
8228 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07008229#ifdef I40E_FCOE
8230 i40e_fcoe_config_netdev(netdev, vsi);
8231#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008232
8233 return 0;
8234}
8235
8236/**
8237 * i40e_vsi_delete - Delete a VSI from the switch
8238 * @vsi: the VSI being removed
8239 *
8240 * Returns 0 on success, negative value on failure
8241 **/
8242static void i40e_vsi_delete(struct i40e_vsi *vsi)
8243{
8244 /* remove default VSI is not allowed */
8245 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
8246 return;
8247
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008248 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008249}
8250
8251/**
Neerav Parikh51616012015-02-06 08:52:14 +00008252 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
8253 * @vsi: the VSI being queried
8254 *
8255 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
8256 **/
8257int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
8258{
8259 struct i40e_veb *veb;
8260 struct i40e_pf *pf = vsi->back;
8261
8262 /* Uplink is not a bridge so default to VEB */
8263 if (vsi->veb_idx == I40E_NO_VEB)
8264 return 1;
8265
8266 veb = pf->veb[vsi->veb_idx];
8267 /* Uplink is a bridge in VEPA mode */
8268 if (veb && (veb->bridge_mode & BRIDGE_MODE_VEPA))
8269 return 0;
8270
8271 /* Uplink is a bridge in VEB mode */
8272 return 1;
8273}
8274
8275/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008276 * i40e_add_vsi - Add a VSI to the switch
8277 * @vsi: the VSI being configured
8278 *
8279 * This initializes a VSI context depending on the VSI type to be added and
8280 * passes it down to the add_vsi aq command.
8281 **/
8282static int i40e_add_vsi(struct i40e_vsi *vsi)
8283{
8284 int ret = -ENODEV;
8285 struct i40e_mac_filter *f, *ftmp;
8286 struct i40e_pf *pf = vsi->back;
8287 struct i40e_hw *hw = &pf->hw;
8288 struct i40e_vsi_context ctxt;
8289 u8 enabled_tc = 0x1; /* TC0 enabled */
8290 int f_count = 0;
8291
8292 memset(&ctxt, 0, sizeof(ctxt));
8293 switch (vsi->type) {
8294 case I40E_VSI_MAIN:
8295 /* The PF's main VSI is already setup as part of the
8296 * device initialization, so we'll not bother with
8297 * the add_vsi call, but we will retrieve the current
8298 * VSI context.
8299 */
8300 ctxt.seid = pf->main_vsi_seid;
8301 ctxt.pf_num = pf->hw.pf_id;
8302 ctxt.vf_num = 0;
8303 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8304 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8305 if (ret) {
8306 dev_info(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00008307 "couldn't get PF vsi config, err %d, aq_err %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008308 ret, pf->hw.aq.asq_last_status);
8309 return -ENOENT;
8310 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07008311 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008312 vsi->info.valid_sections = 0;
8313
8314 vsi->seid = ctxt.seid;
8315 vsi->id = ctxt.vsi_number;
8316
8317 enabled_tc = i40e_pf_get_tc_map(pf);
8318
8319 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00008320 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
8321 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008322 memset(&ctxt, 0, sizeof(ctxt));
8323 ctxt.seid = pf->main_vsi_seid;
8324 ctxt.pf_num = pf->hw.pf_id;
8325 ctxt.vf_num = 0;
8326 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
8327 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
8328 if (ret) {
8329 dev_info(&pf->pdev->dev,
8330 "update vsi failed, aq_err=%d\n",
8331 pf->hw.aq.asq_last_status);
8332 ret = -ENOENT;
8333 goto err;
8334 }
8335 /* update the local VSI info queue map */
8336 i40e_vsi_update_queue_map(vsi, &ctxt);
8337 vsi->info.valid_sections = 0;
8338 } else {
8339 /* Default/Main VSI is only enabled for TC0
8340 * reconfigure it to enable all TCs that are
8341 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00008342 * For MFP case the iSCSI PF would use this
8343 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008344 */
8345 ret = i40e_vsi_config_tc(vsi, enabled_tc);
8346 if (ret) {
8347 dev_info(&pf->pdev->dev,
8348 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
8349 enabled_tc, ret,
8350 pf->hw.aq.asq_last_status);
8351 ret = -ENOENT;
8352 }
8353 }
8354 break;
8355
8356 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008357 ctxt.pf_num = hw->pf_id;
8358 ctxt.vf_num = 0;
8359 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00008360 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008361 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008362 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
8363 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00008364 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008365 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00008366 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008367 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00008368 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008369 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008370 break;
8371
8372 case I40E_VSI_VMDQ2:
8373 ctxt.pf_num = hw->pf_id;
8374 ctxt.vf_num = 0;
8375 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00008376 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008377 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
8378
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008379 /* This VSI is connected to VEB so the switch_id
8380 * should be set to zero by default.
8381 */
Neerav Parikh51616012015-02-06 08:52:14 +00008382 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
8383 ctxt.info.valid_sections |=
8384 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8385 ctxt.info.switch_id =
8386 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8387 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008388
8389 /* Setup the VSI tx/rx queue map for TC0 only for now */
8390 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
8391 break;
8392
8393 case I40E_VSI_SRIOV:
8394 ctxt.pf_num = hw->pf_id;
8395 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
8396 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00008397 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008398 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
8399
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008400 /* This VSI is connected to VEB so the switch_id
8401 * should be set to zero by default.
8402 */
Neerav Parikh51616012015-02-06 08:52:14 +00008403 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
8404 ctxt.info.valid_sections |=
8405 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8406 ctxt.info.switch_id =
8407 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8408 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008409
8410 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
8411 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00008412 if (pf->vf[vsi->vf_id].spoofchk) {
8413 ctxt.info.valid_sections |=
8414 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
8415 ctxt.info.sec_flags |=
8416 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
8417 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
8418 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008419 /* Setup the VSI tx/rx queue map for TC0 only for now */
8420 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
8421 break;
8422
Vasu Dev38e00432014-08-01 13:27:03 -07008423#ifdef I40E_FCOE
8424 case I40E_VSI_FCOE:
8425 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
8426 if (ret) {
8427 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
8428 return ret;
8429 }
8430 break;
8431
8432#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008433 default:
8434 return -ENODEV;
8435 }
8436
8437 if (vsi->type != I40E_VSI_MAIN) {
8438 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
8439 if (ret) {
8440 dev_info(&vsi->back->pdev->dev,
8441 "add vsi failed, aq_err=%d\n",
8442 vsi->back->hw.aq.asq_last_status);
8443 ret = -ENOENT;
8444 goto err;
8445 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07008446 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008447 vsi->info.valid_sections = 0;
8448 vsi->seid = ctxt.seid;
8449 vsi->id = ctxt.vsi_number;
8450 }
8451
8452 /* If macvlan filters already exist, force them to get loaded */
8453 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
8454 f->changed = true;
8455 f_count++;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00008456
8457 if (f->is_laa && vsi->type == I40E_VSI_MAIN) {
Shannon Nelson30650cc2014-07-29 04:01:50 +00008458 struct i40e_aqc_remove_macvlan_element_data element;
8459
8460 memset(&element, 0, sizeof(element));
8461 ether_addr_copy(element.mac_addr, f->macaddr);
8462 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
8463 ret = i40e_aq_remove_macvlan(hw, vsi->seid,
8464 &element, 1, NULL);
8465 if (ret) {
8466 /* some older FW has a different default */
8467 element.flags |=
8468 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
8469 i40e_aq_remove_macvlan(hw, vsi->seid,
8470 &element, 1, NULL);
8471 }
8472
8473 i40e_aq_mac_address_write(hw,
Shannon Nelson6252c7e2014-06-04 01:23:23 +00008474 I40E_AQC_WRITE_TYPE_LAA_WOL,
8475 f->macaddr, NULL);
8476 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008477 }
8478 if (f_count) {
8479 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
8480 pf->flags |= I40E_FLAG_FILTER_SYNC;
8481 }
8482
8483 /* Update VSI BW information */
8484 ret = i40e_vsi_get_bw_info(vsi);
8485 if (ret) {
8486 dev_info(&pf->pdev->dev,
8487 "couldn't get vsi bw info, err %d, aq_err %d\n",
8488 ret, pf->hw.aq.asq_last_status);
8489 /* VSI is already added so not tearing that up */
8490 ret = 0;
8491 }
8492
8493err:
8494 return ret;
8495}
8496
8497/**
8498 * i40e_vsi_release - Delete a VSI and free its resources
8499 * @vsi: the VSI being removed
8500 *
8501 * Returns 0 on success or < 0 on error
8502 **/
8503int i40e_vsi_release(struct i40e_vsi *vsi)
8504{
8505 struct i40e_mac_filter *f, *ftmp;
8506 struct i40e_veb *veb = NULL;
8507 struct i40e_pf *pf;
8508 u16 uplink_seid;
8509 int i, n;
8510
8511 pf = vsi->back;
8512
8513 /* release of a VEB-owner or last VSI is not allowed */
8514 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
8515 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
8516 vsi->seid, vsi->uplink_seid);
8517 return -ENODEV;
8518 }
8519 if (vsi == pf->vsi[pf->lan_vsi] &&
8520 !test_bit(__I40E_DOWN, &pf->state)) {
8521 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
8522 return -ENODEV;
8523 }
8524
8525 uplink_seid = vsi->uplink_seid;
8526 if (vsi->type != I40E_VSI_SRIOV) {
8527 if (vsi->netdev_registered) {
8528 vsi->netdev_registered = false;
8529 if (vsi->netdev) {
8530 /* results in a call to i40e_close() */
8531 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008532 }
8533 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00008534 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008535 }
8536 i40e_vsi_disable_irq(vsi);
8537 }
8538
8539 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
8540 i40e_del_filter(vsi, f->macaddr, f->vlan,
8541 f->is_vf, f->is_netdev);
8542 i40e_sync_vsi_filters(vsi);
8543
8544 i40e_vsi_delete(vsi);
8545 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00008546 if (vsi->netdev) {
8547 free_netdev(vsi->netdev);
8548 vsi->netdev = NULL;
8549 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008550 i40e_vsi_clear_rings(vsi);
8551 i40e_vsi_clear(vsi);
8552
8553 /* If this was the last thing on the VEB, except for the
8554 * controlling VSI, remove the VEB, which puts the controlling
8555 * VSI onto the next level down in the switch.
8556 *
8557 * Well, okay, there's one more exception here: don't remove
8558 * the orphan VEBs yet. We'll wait for an explicit remove request
8559 * from up the network stack.
8560 */
Mitch Williams505682c2014-05-20 08:01:37 +00008561 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008562 if (pf->vsi[i] &&
8563 pf->vsi[i]->uplink_seid == uplink_seid &&
8564 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
8565 n++; /* count the VSIs */
8566 }
8567 }
8568 for (i = 0; i < I40E_MAX_VEB; i++) {
8569 if (!pf->veb[i])
8570 continue;
8571 if (pf->veb[i]->uplink_seid == uplink_seid)
8572 n++; /* count the VEBs */
8573 if (pf->veb[i]->seid == uplink_seid)
8574 veb = pf->veb[i];
8575 }
8576 if (n == 0 && veb && veb->uplink_seid != 0)
8577 i40e_veb_release(veb);
8578
8579 return 0;
8580}
8581
8582/**
8583 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
8584 * @vsi: ptr to the VSI
8585 *
8586 * This should only be called after i40e_vsi_mem_alloc() which allocates the
8587 * corresponding SW VSI structure and initializes num_queue_pairs for the
8588 * newly allocated VSI.
8589 *
8590 * Returns 0 on success or negative on failure
8591 **/
8592static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
8593{
8594 int ret = -ENOENT;
8595 struct i40e_pf *pf = vsi->back;
8596
Alexander Duyck493fb302013-09-28 07:01:44 +00008597 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008598 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
8599 vsi->seid);
8600 return -EEXIST;
8601 }
8602
8603 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00008604 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008605 vsi->seid, vsi->base_vector);
8606 return -EEXIST;
8607 }
8608
Greg Rose90e04072014-03-06 08:59:57 +00008609 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008610 if (ret) {
8611 dev_info(&pf->pdev->dev,
8612 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
8613 vsi->num_q_vectors, vsi->seid, ret);
8614 vsi->num_q_vectors = 0;
8615 goto vector_setup_out;
8616 }
8617
Shannon Nelson958a3e32013-09-28 07:13:28 +00008618 if (vsi->num_q_vectors)
8619 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
8620 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008621 if (vsi->base_vector < 0) {
8622 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00008623 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
8624 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008625 i40e_vsi_free_q_vectors(vsi);
8626 ret = -ENOENT;
8627 goto vector_setup_out;
8628 }
8629
8630vector_setup_out:
8631 return ret;
8632}
8633
8634/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008635 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
8636 * @vsi: pointer to the vsi.
8637 *
8638 * This re-allocates a vsi's queue resources.
8639 *
8640 * Returns pointer to the successfully allocated and configured VSI sw struct
8641 * on success, otherwise returns NULL on failure.
8642 **/
8643static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
8644{
8645 struct i40e_pf *pf = vsi->back;
8646 u8 enabled_tc;
8647 int ret;
8648
8649 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
8650 i40e_vsi_clear_rings(vsi);
8651
8652 i40e_vsi_free_arrays(vsi, false);
8653 i40e_set_num_rings_in_vsi(vsi);
8654 ret = i40e_vsi_alloc_arrays(vsi, false);
8655 if (ret)
8656 goto err_vsi;
8657
8658 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
8659 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00008660 dev_info(&pf->pdev->dev,
8661 "failed to get tracking for %d queues for VSI %d err=%d\n",
8662 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008663 goto err_vsi;
8664 }
8665 vsi->base_queue = ret;
8666
8667 /* Update the FW view of the VSI. Force a reset of TC and queue
8668 * layout configurations.
8669 */
8670 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
8671 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
8672 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
8673 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
8674
8675 /* assign it some queues */
8676 ret = i40e_alloc_rings(vsi);
8677 if (ret)
8678 goto err_rings;
8679
8680 /* map all of the rings to the q_vectors */
8681 i40e_vsi_map_rings_to_vectors(vsi);
8682 return vsi;
8683
8684err_rings:
8685 i40e_vsi_free_q_vectors(vsi);
8686 if (vsi->netdev_registered) {
8687 vsi->netdev_registered = false;
8688 unregister_netdev(vsi->netdev);
8689 free_netdev(vsi->netdev);
8690 vsi->netdev = NULL;
8691 }
8692 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
8693err_vsi:
8694 i40e_vsi_clear(vsi);
8695 return NULL;
8696}
8697
8698/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008699 * i40e_vsi_setup - Set up a VSI by a given type
8700 * @pf: board private structure
8701 * @type: VSI type
8702 * @uplink_seid: the switch element to link to
8703 * @param1: usage depends upon VSI type. For VF types, indicates VF id
8704 *
8705 * This allocates the sw VSI structure and its queue resources, then add a VSI
8706 * to the identified VEB.
8707 *
8708 * Returns pointer to the successfully allocated and configure VSI sw struct on
8709 * success, otherwise returns NULL on failure.
8710 **/
8711struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
8712 u16 uplink_seid, u32 param1)
8713{
8714 struct i40e_vsi *vsi = NULL;
8715 struct i40e_veb *veb = NULL;
8716 int ret, i;
8717 int v_idx;
8718
8719 /* The requested uplink_seid must be either
8720 * - the PF's port seid
8721 * no VEB is needed because this is the PF
8722 * or this is a Flow Director special case VSI
8723 * - seid of an existing VEB
8724 * - seid of a VSI that owns an existing VEB
8725 * - seid of a VSI that doesn't own a VEB
8726 * a new VEB is created and the VSI becomes the owner
8727 * - seid of the PF VSI, which is what creates the first VEB
8728 * this is a special case of the previous
8729 *
8730 * Find which uplink_seid we were given and create a new VEB if needed
8731 */
8732 for (i = 0; i < I40E_MAX_VEB; i++) {
8733 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
8734 veb = pf->veb[i];
8735 break;
8736 }
8737 }
8738
8739 if (!veb && uplink_seid != pf->mac_seid) {
8740
Mitch Williams505682c2014-05-20 08:01:37 +00008741 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008742 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
8743 vsi = pf->vsi[i];
8744 break;
8745 }
8746 }
8747 if (!vsi) {
8748 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
8749 uplink_seid);
8750 return NULL;
8751 }
8752
8753 if (vsi->uplink_seid == pf->mac_seid)
8754 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
8755 vsi->tc_config.enabled_tc);
8756 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
8757 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8758 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00008759 if (veb) {
8760 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
8761 dev_info(&vsi->back->pdev->dev,
8762 "%s: New VSI creation error, uplink seid of LAN VSI expected.\n",
8763 __func__);
8764 return NULL;
8765 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04008766 /* We come up by default in VEPA mode if SRIOV is not
8767 * already enabled, in which case we can't force VEPA
8768 * mode.
8769 */
8770 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
8771 veb->bridge_mode = BRIDGE_MODE_VEPA;
8772 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
8773 }
Neerav Parikh51616012015-02-06 08:52:14 +00008774 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00008775 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008776 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8777 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8778 veb = pf->veb[i];
8779 }
8780 if (!veb) {
8781 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
8782 return NULL;
8783 }
8784
8785 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
8786 uplink_seid = veb->seid;
8787 }
8788
8789 /* get vsi sw struct */
8790 v_idx = i40e_vsi_mem_alloc(pf, type);
8791 if (v_idx < 0)
8792 goto err_alloc;
8793 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008794 if (!vsi)
8795 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008796 vsi->type = type;
8797 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
8798
8799 if (type == I40E_VSI_MAIN)
8800 pf->lan_vsi = v_idx;
8801 else if (type == I40E_VSI_SRIOV)
8802 vsi->vf_id = param1;
8803 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008804 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
8805 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008806 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00008807 dev_info(&pf->pdev->dev,
8808 "failed to get tracking for %d queues for VSI %d err=%d\n",
8809 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008810 goto err_vsi;
8811 }
8812 vsi->base_queue = ret;
8813
8814 /* get a VSI from the hardware */
8815 vsi->uplink_seid = uplink_seid;
8816 ret = i40e_add_vsi(vsi);
8817 if (ret)
8818 goto err_vsi;
8819
8820 switch (vsi->type) {
8821 /* setup the netdev if needed */
8822 case I40E_VSI_MAIN:
8823 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07008824 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008825 ret = i40e_config_netdev(vsi);
8826 if (ret)
8827 goto err_netdev;
8828 ret = register_netdev(vsi->netdev);
8829 if (ret)
8830 goto err_netdev;
8831 vsi->netdev_registered = true;
8832 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008833#ifdef CONFIG_I40E_DCB
8834 /* Setup DCB netlink interface */
8835 i40e_dcbnl_setup(vsi);
8836#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008837 /* fall through */
8838
8839 case I40E_VSI_FDIR:
8840 /* set up vectors and rings if needed */
8841 ret = i40e_vsi_setup_vectors(vsi);
8842 if (ret)
8843 goto err_msix;
8844
8845 ret = i40e_alloc_rings(vsi);
8846 if (ret)
8847 goto err_rings;
8848
8849 /* map all of the rings to the q_vectors */
8850 i40e_vsi_map_rings_to_vectors(vsi);
8851
8852 i40e_vsi_reset_stats(vsi);
8853 break;
8854
8855 default:
8856 /* no netdev or rings for the other VSI types */
8857 break;
8858 }
8859
8860 return vsi;
8861
8862err_rings:
8863 i40e_vsi_free_q_vectors(vsi);
8864err_msix:
8865 if (vsi->netdev_registered) {
8866 vsi->netdev_registered = false;
8867 unregister_netdev(vsi->netdev);
8868 free_netdev(vsi->netdev);
8869 vsi->netdev = NULL;
8870 }
8871err_netdev:
8872 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
8873err_vsi:
8874 i40e_vsi_clear(vsi);
8875err_alloc:
8876 return NULL;
8877}
8878
8879/**
8880 * i40e_veb_get_bw_info - Query VEB BW information
8881 * @veb: the veb to query
8882 *
8883 * Query the Tx scheduler BW configuration data for given VEB
8884 **/
8885static int i40e_veb_get_bw_info(struct i40e_veb *veb)
8886{
8887 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
8888 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
8889 struct i40e_pf *pf = veb->pf;
8890 struct i40e_hw *hw = &pf->hw;
8891 u32 tc_bw_max;
8892 int ret = 0;
8893 int i;
8894
8895 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
8896 &bw_data, NULL);
8897 if (ret) {
8898 dev_info(&pf->pdev->dev,
8899 "query veb bw config failed, aq_err=%d\n",
8900 hw->aq.asq_last_status);
8901 goto out;
8902 }
8903
8904 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
8905 &ets_data, NULL);
8906 if (ret) {
8907 dev_info(&pf->pdev->dev,
8908 "query veb bw ets config failed, aq_err=%d\n",
8909 hw->aq.asq_last_status);
8910 goto out;
8911 }
8912
8913 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
8914 veb->bw_max_quanta = ets_data.tc_bw_max;
8915 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +00008916 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008917 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
8918 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
8919 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
8920 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
8921 veb->bw_tc_limit_credits[i] =
8922 le16_to_cpu(bw_data.tc_bw_limits[i]);
8923 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
8924 }
8925
8926out:
8927 return ret;
8928}
8929
8930/**
8931 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
8932 * @pf: board private structure
8933 *
8934 * On error: returns error code (negative)
8935 * On success: returns vsi index in PF (positive)
8936 **/
8937static int i40e_veb_mem_alloc(struct i40e_pf *pf)
8938{
8939 int ret = -ENOENT;
8940 struct i40e_veb *veb;
8941 int i;
8942
8943 /* Need to protect the allocation of switch elements at the PF level */
8944 mutex_lock(&pf->switch_mutex);
8945
8946 /* VEB list may be fragmented if VEB creation/destruction has
8947 * been happening. We can afford to do a quick scan to look
8948 * for any free slots in the list.
8949 *
8950 * find next empty veb slot, looping back around if necessary
8951 */
8952 i = 0;
8953 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
8954 i++;
8955 if (i >= I40E_MAX_VEB) {
8956 ret = -ENOMEM;
8957 goto err_alloc_veb; /* out of VEB slots! */
8958 }
8959
8960 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
8961 if (!veb) {
8962 ret = -ENOMEM;
8963 goto err_alloc_veb;
8964 }
8965 veb->pf = pf;
8966 veb->idx = i;
8967 veb->enabled_tc = 1;
8968
8969 pf->veb[i] = veb;
8970 ret = i;
8971err_alloc_veb:
8972 mutex_unlock(&pf->switch_mutex);
8973 return ret;
8974}
8975
8976/**
8977 * i40e_switch_branch_release - Delete a branch of the switch tree
8978 * @branch: where to start deleting
8979 *
8980 * This uses recursion to find the tips of the branch to be
8981 * removed, deleting until we get back to and can delete this VEB.
8982 **/
8983static void i40e_switch_branch_release(struct i40e_veb *branch)
8984{
8985 struct i40e_pf *pf = branch->pf;
8986 u16 branch_seid = branch->seid;
8987 u16 veb_idx = branch->idx;
8988 int i;
8989
8990 /* release any VEBs on this VEB - RECURSION */
8991 for (i = 0; i < I40E_MAX_VEB; i++) {
8992 if (!pf->veb[i])
8993 continue;
8994 if (pf->veb[i]->uplink_seid == branch->seid)
8995 i40e_switch_branch_release(pf->veb[i]);
8996 }
8997
8998 /* Release the VSIs on this VEB, but not the owner VSI.
8999 *
9000 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
9001 * the VEB itself, so don't use (*branch) after this loop.
9002 */
Mitch Williams505682c2014-05-20 08:01:37 +00009003 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009004 if (!pf->vsi[i])
9005 continue;
9006 if (pf->vsi[i]->uplink_seid == branch_seid &&
9007 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9008 i40e_vsi_release(pf->vsi[i]);
9009 }
9010 }
9011
9012 /* There's one corner case where the VEB might not have been
9013 * removed, so double check it here and remove it if needed.
9014 * This case happens if the veb was created from the debugfs
9015 * commands and no VSIs were added to it.
9016 */
9017 if (pf->veb[veb_idx])
9018 i40e_veb_release(pf->veb[veb_idx]);
9019}
9020
9021/**
9022 * i40e_veb_clear - remove veb struct
9023 * @veb: the veb to remove
9024 **/
9025static void i40e_veb_clear(struct i40e_veb *veb)
9026{
9027 if (!veb)
9028 return;
9029
9030 if (veb->pf) {
9031 struct i40e_pf *pf = veb->pf;
9032
9033 mutex_lock(&pf->switch_mutex);
9034 if (pf->veb[veb->idx] == veb)
9035 pf->veb[veb->idx] = NULL;
9036 mutex_unlock(&pf->switch_mutex);
9037 }
9038
9039 kfree(veb);
9040}
9041
9042/**
9043 * i40e_veb_release - Delete a VEB and free its resources
9044 * @veb: the VEB being removed
9045 **/
9046void i40e_veb_release(struct i40e_veb *veb)
9047{
9048 struct i40e_vsi *vsi = NULL;
9049 struct i40e_pf *pf;
9050 int i, n = 0;
9051
9052 pf = veb->pf;
9053
9054 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +00009055 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009056 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
9057 n++;
9058 vsi = pf->vsi[i];
9059 }
9060 }
9061 if (n != 1) {
9062 dev_info(&pf->pdev->dev,
9063 "can't remove VEB %d with %d VSIs left\n",
9064 veb->seid, n);
9065 return;
9066 }
9067
9068 /* move the remaining VSI to uplink veb */
9069 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
9070 if (veb->uplink_seid) {
9071 vsi->uplink_seid = veb->uplink_seid;
9072 if (veb->uplink_seid == pf->mac_seid)
9073 vsi->veb_idx = I40E_NO_VEB;
9074 else
9075 vsi->veb_idx = veb->veb_idx;
9076 } else {
9077 /* floating VEB */
9078 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
9079 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
9080 }
9081
9082 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
9083 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009084}
9085
9086/**
9087 * i40e_add_veb - create the VEB in the switch
9088 * @veb: the VEB to be instantiated
9089 * @vsi: the controlling VSI
9090 **/
9091static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
9092{
Greg Rose56747262013-11-28 06:39:37 +00009093 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00009094 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009095 int ret;
9096
9097 /* get a VEB from the hardware */
9098 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00009099 veb->enabled_tc, is_default,
9100 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009101 if (ret) {
9102 dev_info(&veb->pf->pdev->dev,
9103 "couldn't add VEB, err %d, aq_err %d\n",
9104 ret, veb->pf->hw.aq.asq_last_status);
9105 return -EPERM;
9106 }
9107
9108 /* get statistics counter */
9109 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
9110 &veb->stats_idx, NULL, NULL, NULL);
9111 if (ret) {
9112 dev_info(&veb->pf->pdev->dev,
9113 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
9114 ret, veb->pf->hw.aq.asq_last_status);
9115 return -EPERM;
9116 }
9117 ret = i40e_veb_get_bw_info(veb);
9118 if (ret) {
9119 dev_info(&veb->pf->pdev->dev,
9120 "couldn't get VEB bw info, err %d, aq_err %d\n",
9121 ret, veb->pf->hw.aq.asq_last_status);
9122 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
9123 return -ENOENT;
9124 }
9125
9126 vsi->uplink_seid = veb->seid;
9127 vsi->veb_idx = veb->idx;
9128 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9129
9130 return 0;
9131}
9132
9133/**
9134 * i40e_veb_setup - Set up a VEB
9135 * @pf: board private structure
9136 * @flags: VEB setup flags
9137 * @uplink_seid: the switch element to link to
9138 * @vsi_seid: the initial VSI seid
9139 * @enabled_tc: Enabled TC bit-map
9140 *
9141 * This allocates the sw VEB structure and links it into the switch
9142 * It is possible and legal for this to be a duplicate of an already
9143 * existing VEB. It is also possible for both uplink and vsi seids
9144 * to be zero, in order to create a floating VEB.
9145 *
9146 * Returns pointer to the successfully allocated VEB sw struct on
9147 * success, otherwise returns NULL on failure.
9148 **/
9149struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
9150 u16 uplink_seid, u16 vsi_seid,
9151 u8 enabled_tc)
9152{
9153 struct i40e_veb *veb, *uplink_veb = NULL;
9154 int vsi_idx, veb_idx;
9155 int ret;
9156
9157 /* if one seid is 0, the other must be 0 to create a floating relay */
9158 if ((uplink_seid == 0 || vsi_seid == 0) &&
9159 (uplink_seid + vsi_seid != 0)) {
9160 dev_info(&pf->pdev->dev,
9161 "one, not both seid's are 0: uplink=%d vsi=%d\n",
9162 uplink_seid, vsi_seid);
9163 return NULL;
9164 }
9165
9166 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +00009167 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009168 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
9169 break;
Mitch Williams505682c2014-05-20 08:01:37 +00009170 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009171 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
9172 vsi_seid);
9173 return NULL;
9174 }
9175
9176 if (uplink_seid && uplink_seid != pf->mac_seid) {
9177 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
9178 if (pf->veb[veb_idx] &&
9179 pf->veb[veb_idx]->seid == uplink_seid) {
9180 uplink_veb = pf->veb[veb_idx];
9181 break;
9182 }
9183 }
9184 if (!uplink_veb) {
9185 dev_info(&pf->pdev->dev,
9186 "uplink seid %d not found\n", uplink_seid);
9187 return NULL;
9188 }
9189 }
9190
9191 /* get veb sw struct */
9192 veb_idx = i40e_veb_mem_alloc(pf);
9193 if (veb_idx < 0)
9194 goto err_alloc;
9195 veb = pf->veb[veb_idx];
9196 veb->flags = flags;
9197 veb->uplink_seid = uplink_seid;
9198 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
9199 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
9200
9201 /* create the VEB in the switch */
9202 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
9203 if (ret)
9204 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +00009205 if (vsi_idx == pf->lan_vsi)
9206 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009207
9208 return veb;
9209
9210err_veb:
9211 i40e_veb_clear(veb);
9212err_alloc:
9213 return NULL;
9214}
9215
9216/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00009217 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009218 * @pf: board private structure
9219 * @ele: element we are building info from
9220 * @num_reported: total number of elements
9221 * @printconfig: should we print the contents
9222 *
9223 * helper function to assist in extracting a few useful SEID values.
9224 **/
9225static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
9226 struct i40e_aqc_switch_config_element_resp *ele,
9227 u16 num_reported, bool printconfig)
9228{
9229 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
9230 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
9231 u8 element_type = ele->element_type;
9232 u16 seid = le16_to_cpu(ele->seid);
9233
9234 if (printconfig)
9235 dev_info(&pf->pdev->dev,
9236 "type=%d seid=%d uplink=%d downlink=%d\n",
9237 element_type, seid, uplink_seid, downlink_seid);
9238
9239 switch (element_type) {
9240 case I40E_SWITCH_ELEMENT_TYPE_MAC:
9241 pf->mac_seid = seid;
9242 break;
9243 case I40E_SWITCH_ELEMENT_TYPE_VEB:
9244 /* Main VEB? */
9245 if (uplink_seid != pf->mac_seid)
9246 break;
9247 if (pf->lan_veb == I40E_NO_VEB) {
9248 int v;
9249
9250 /* find existing or else empty VEB */
9251 for (v = 0; v < I40E_MAX_VEB; v++) {
9252 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
9253 pf->lan_veb = v;
9254 break;
9255 }
9256 }
9257 if (pf->lan_veb == I40E_NO_VEB) {
9258 v = i40e_veb_mem_alloc(pf);
9259 if (v < 0)
9260 break;
9261 pf->lan_veb = v;
9262 }
9263 }
9264
9265 pf->veb[pf->lan_veb]->seid = seid;
9266 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
9267 pf->veb[pf->lan_veb]->pf = pf;
9268 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
9269 break;
9270 case I40E_SWITCH_ELEMENT_TYPE_VSI:
9271 if (num_reported != 1)
9272 break;
9273 /* This is immediately after a reset so we can assume this is
9274 * the PF's VSI
9275 */
9276 pf->mac_seid = uplink_seid;
9277 pf->pf_seid = downlink_seid;
9278 pf->main_vsi_seid = seid;
9279 if (printconfig)
9280 dev_info(&pf->pdev->dev,
9281 "pf_seid=%d main_vsi_seid=%d\n",
9282 pf->pf_seid, pf->main_vsi_seid);
9283 break;
9284 case I40E_SWITCH_ELEMENT_TYPE_PF:
9285 case I40E_SWITCH_ELEMENT_TYPE_VF:
9286 case I40E_SWITCH_ELEMENT_TYPE_EMP:
9287 case I40E_SWITCH_ELEMENT_TYPE_BMC:
9288 case I40E_SWITCH_ELEMENT_TYPE_PE:
9289 case I40E_SWITCH_ELEMENT_TYPE_PA:
9290 /* ignore these for now */
9291 break;
9292 default:
9293 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
9294 element_type, seid);
9295 break;
9296 }
9297}
9298
9299/**
9300 * i40e_fetch_switch_configuration - Get switch config from firmware
9301 * @pf: board private structure
9302 * @printconfig: should we print the contents
9303 *
9304 * Get the current switch configuration from the device and
9305 * extract a few useful SEID values.
9306 **/
9307int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
9308{
9309 struct i40e_aqc_get_switch_config_resp *sw_config;
9310 u16 next_seid = 0;
9311 int ret = 0;
9312 u8 *aq_buf;
9313 int i;
9314
9315 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
9316 if (!aq_buf)
9317 return -ENOMEM;
9318
9319 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
9320 do {
9321 u16 num_reported, num_total;
9322
9323 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
9324 I40E_AQ_LARGE_BUF,
9325 &next_seid, NULL);
9326 if (ret) {
9327 dev_info(&pf->pdev->dev,
9328 "get switch config failed %d aq_err=%x\n",
9329 ret, pf->hw.aq.asq_last_status);
9330 kfree(aq_buf);
9331 return -ENOENT;
9332 }
9333
9334 num_reported = le16_to_cpu(sw_config->header.num_reported);
9335 num_total = le16_to_cpu(sw_config->header.num_total);
9336
9337 if (printconfig)
9338 dev_info(&pf->pdev->dev,
9339 "header: %d reported %d total\n",
9340 num_reported, num_total);
9341
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009342 for (i = 0; i < num_reported; i++) {
9343 struct i40e_aqc_switch_config_element_resp *ele =
9344 &sw_config->element[i];
9345
9346 i40e_setup_pf_switch_element(pf, ele, num_reported,
9347 printconfig);
9348 }
9349 } while (next_seid != 0);
9350
9351 kfree(aq_buf);
9352 return ret;
9353}
9354
9355/**
9356 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
9357 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009358 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009359 *
9360 * Returns 0 on success, negative value on failure
9361 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009362static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009363{
9364 int ret;
9365
9366 /* find out what's out there already */
9367 ret = i40e_fetch_switch_configuration(pf, false);
9368 if (ret) {
9369 dev_info(&pf->pdev->dev,
9370 "couldn't fetch switch config, err %d, aq_err %d\n",
9371 ret, pf->hw.aq.asq_last_status);
9372 return ret;
9373 }
9374 i40e_pf_reset_stats(pf);
9375
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009376 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009377 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009378 struct i40e_vsi *vsi = NULL;
9379 u16 uplink_seid;
9380
9381 /* Set up the PF VSI associated with the PF's main VSI
9382 * that is already in the HW switch
9383 */
9384 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
9385 uplink_seid = pf->veb[pf->lan_veb]->seid;
9386 else
9387 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009388 if (pf->lan_vsi == I40E_NO_VSI)
9389 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
9390 else if (reinit)
9391 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009392 if (!vsi) {
9393 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
9394 i40e_fdir_teardown(pf);
9395 return -EAGAIN;
9396 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009397 } else {
9398 /* force a reset of TC and queue layout configurations */
9399 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9400 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9401 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9402 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9403 }
9404 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
9405
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009406 i40e_fdir_sb_setup(pf);
9407
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009408 /* Setup static PF queue filter control settings */
9409 ret = i40e_setup_pf_filter_control(pf);
9410 if (ret) {
9411 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
9412 ret);
9413 /* Failure here should not stop continuing other steps */
9414 }
9415
9416 /* enable RSS in the HW, even for only one queue, as the stack can use
9417 * the hash
9418 */
9419 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
9420 i40e_config_rss(pf);
9421
9422 /* fill in link information and enable LSE reporting */
Catherine Sullivan21af70f2015-01-24 09:58:41 +00009423 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009424 i40e_link_event(pf);
9425
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00009426 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009427 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
9428 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00009429
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009430 i40e_ptp_init(pf);
9431
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009432 return ret;
9433}
9434
9435/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009436 * i40e_determine_queue_usage - Work out queue distribution
9437 * @pf: board private structure
9438 **/
9439static void i40e_determine_queue_usage(struct i40e_pf *pf)
9440{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009441 int queues_left;
9442
9443 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07009444#ifdef I40E_FCOE
9445 pf->num_fcoe_qps = 0;
9446#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009447
9448 /* Find the max queues to be put into basic use. We'll always be
9449 * using TC0, whether or not DCB is running, and TC0 will get the
9450 * big RSS set.
9451 */
9452 queues_left = pf->hw.func_caps.num_tx_qp;
9453
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009454 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +00009455 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009456 /* one qp for PF, no queues for anything else */
9457 queues_left = 0;
9458 pf->rss_size = pf->num_lan_qps = 1;
9459
9460 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009461 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07009462#ifdef I40E_FCOE
9463 I40E_FLAG_FCOE_ENABLED |
9464#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009465 I40E_FLAG_FD_SB_ENABLED |
9466 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009467 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009468 I40E_FLAG_SRIOV_ENABLED |
9469 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +00009470 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
9471 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +00009472 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009473 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +00009474 /* one qp for PF */
9475 pf->rss_size = pf->num_lan_qps = 1;
9476 queues_left -= pf->num_lan_qps;
9477
9478 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07009479#ifdef I40E_FCOE
9480 I40E_FLAG_FCOE_ENABLED |
9481#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +00009482 I40E_FLAG_FD_SB_ENABLED |
9483 I40E_FLAG_FD_ATR_ENABLED |
9484 I40E_FLAG_DCB_ENABLED |
9485 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009486 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009487 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009488 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009489 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009490 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009491 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
9492 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00009493 pf->num_lan_qps = max_t(int, pf->rss_size_max,
9494 num_online_cpus());
9495 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
9496 pf->hw.func_caps.num_tx_qp);
9497
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009498 queues_left -= pf->num_lan_qps;
9499 }
9500
Vasu Dev38e00432014-08-01 13:27:03 -07009501#ifdef I40E_FCOE
9502 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
9503 if (I40E_DEFAULT_FCOE <= queues_left) {
9504 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
9505 } else if (I40E_MINIMUM_FCOE <= queues_left) {
9506 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
9507 } else {
9508 pf->num_fcoe_qps = 0;
9509 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
9510 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
9511 }
9512
9513 queues_left -= pf->num_fcoe_qps;
9514 }
9515
9516#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009517 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9518 if (queues_left > 1) {
9519 queues_left -= 1; /* save 1 queue for FD */
9520 } else {
9521 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9522 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
9523 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009524 }
9525
9526 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
9527 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009528 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
9529 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009530 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
9531 }
9532
9533 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
9534 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
9535 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
9536 (queues_left / pf->num_vmdq_qps));
9537 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
9538 }
9539
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009540 pf->queues_left = queues_left;
Vasu Dev38e00432014-08-01 13:27:03 -07009541#ifdef I40E_FCOE
9542 dev_info(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
9543#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009544}
9545
9546/**
9547 * i40e_setup_pf_filter_control - Setup PF static filter control
9548 * @pf: PF to be setup
9549 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00009550 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009551 * settings. If PE/FCoE are enabled then it will also set the per PF
9552 * based filter sizes required for them. It also enables Flow director,
9553 * ethertype and macvlan type filter settings for the pf.
9554 *
9555 * Returns 0 on success, negative on failure
9556 **/
9557static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
9558{
9559 struct i40e_filter_control_settings *settings = &pf->filter_settings;
9560
9561 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
9562
9563 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009564 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009565 settings->enable_fdir = true;
9566
9567 /* Ethtype and MACVLAN filters enabled for PF */
9568 settings->enable_ethtype = true;
9569 settings->enable_macvlan = true;
9570
9571 if (i40e_set_filter_control(&pf->hw, settings))
9572 return -ENOENT;
9573
9574 return 0;
9575}
9576
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009577#define INFO_STRING_LEN 255
9578static void i40e_print_features(struct i40e_pf *pf)
9579{
9580 struct i40e_hw *hw = &pf->hw;
9581 char *buf, *string;
9582
9583 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
9584 if (!string) {
9585 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
9586 return;
9587 }
9588
9589 buf = string;
9590
9591 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
9592#ifdef CONFIG_PCI_IOV
9593 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
9594#endif
Mitch Williamsaba237d2015-02-06 08:52:17 +00009595 buf += sprintf(buf, "VSIs: %d QP: %d RX: %s ",
9596 pf->hw.func_caps.num_vsis,
9597 pf->vsi[pf->lan_vsi]->num_queue_pairs,
9598 pf->flags & I40E_FLAG_RX_PS_ENABLED ? "PS" : "1BUF");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009599
9600 if (pf->flags & I40E_FLAG_RSS_ENABLED)
9601 buf += sprintf(buf, "RSS ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009602 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00009603 buf += sprintf(buf, "FD_ATR ");
9604 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9605 buf += sprintf(buf, "FD_SB ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009606 buf += sprintf(buf, "NTUPLE ");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00009607 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009608 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009609 buf += sprintf(buf, "DCB ");
9610 if (pf->flags & I40E_FLAG_PTP)
9611 buf += sprintf(buf, "PTP ");
Vasu Dev38e00432014-08-01 13:27:03 -07009612#ifdef I40E_FCOE
9613 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
9614 buf += sprintf(buf, "FCOE ");
9615#endif
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009616
9617 BUG_ON(buf > (string + INFO_STRING_LEN));
9618 dev_info(&pf->pdev->dev, "%s\n", string);
9619 kfree(string);
9620}
9621
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009622/**
9623 * i40e_probe - Device initialization routine
9624 * @pdev: PCI device information struct
9625 * @ent: entry in i40e_pci_tbl
9626 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00009627 * i40e_probe initializes a PF identified by a pci_dev structure.
9628 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009629 * and a hardware reset occur.
9630 *
9631 * Returns 0 on success, negative on failure
9632 **/
9633static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
9634{
Catherine Sullivane8278452015-02-06 08:52:08 +00009635 struct i40e_aq_get_phy_abilities_resp abilities;
Jeff Kirshere8156652015-03-05 19:02:35 -08009636 unsigned long ioremap_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009637 struct i40e_pf *pf;
9638 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00009639 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00009640 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009641 int err = 0;
9642 u32 len;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00009643 u32 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009644
9645 err = pci_enable_device_mem(pdev);
9646 if (err)
9647 return err;
9648
9649 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +00009650 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00009651 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00009652 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
9653 if (err) {
9654 dev_err(&pdev->dev,
9655 "DMA configuration failed: 0x%x\n", err);
9656 goto err_dma;
9657 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009658 }
9659
9660 /* set up pci connections */
9661 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
9662 IORESOURCE_MEM), i40e_driver_name);
9663 if (err) {
9664 dev_info(&pdev->dev,
9665 "pci_request_selected_regions failed %d\n", err);
9666 goto err_pci_reg;
9667 }
9668
9669 pci_enable_pcie_error_reporting(pdev);
9670 pci_set_master(pdev);
9671
9672 /* Now that we have a PCI connection, we need to do the
9673 * low level device setup. This is primarily setting up
9674 * the Admin Queue structures and then querying for the
9675 * device's current profile information.
9676 */
9677 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
9678 if (!pf) {
9679 err = -ENOMEM;
9680 goto err_pf_alloc;
9681 }
9682 pf->next_vsi = 0;
9683 pf->pdev = pdev;
9684 set_bit(__I40E_DOWN, &pf->state);
9685
9686 hw = &pf->hw;
9687 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +00009688
Jeff Kirshere8156652015-03-05 19:02:35 -08009689 ioremap_len = min_t(unsigned long, pci_resource_len(pdev, 0),
Anjali Singhai232f4702015-02-26 16:15:39 +00009690 I40E_MAX_CSR_SPACE);
9691
9692 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009693 if (!hw->hw_addr) {
9694 err = -EIO;
9695 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
9696 (unsigned int)pci_resource_start(pdev, 0),
9697 (unsigned int)pci_resource_len(pdev, 0), err);
9698 goto err_ioremap;
9699 }
9700 hw->vendor_id = pdev->vendor;
9701 hw->device_id = pdev->device;
9702 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
9703 hw->subsystem_vendor_id = pdev->subsystem_vendor;
9704 hw->subsystem_device_id = pdev->subsystem_device;
9705 hw->bus.device = PCI_SLOT(pdev->devfn);
9706 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00009707 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009708
Shannon Nelson5b5faa42014-10-17 03:14:51 +00009709 if (debug != -1) {
9710 pf->msg_enable = pf->hw.debug_mask;
9711 pf->msg_enable = debug;
9712 }
9713
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00009714 /* do a special CORER for clearing PXE mode once at init */
9715 if (hw->revision_id == 0 &&
9716 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
9717 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
9718 i40e_flush(hw);
9719 msleep(200);
9720 pf->corer_count++;
9721
9722 i40e_clear_pxe_mode(hw);
9723 }
9724
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009725 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +00009726 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009727 err = i40e_pf_reset(hw);
9728 if (err) {
9729 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
9730 goto err_pf_reset;
9731 }
9732 pf->pfr_count++;
9733
9734 hw->aq.num_arq_entries = I40E_AQ_LEN;
9735 hw->aq.num_asq_entries = I40E_AQ_LEN;
9736 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
9737 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
9738 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00009739
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00009740 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00009741 "%s-%s:misc",
9742 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009743
9744 err = i40e_init_shared_code(hw);
9745 if (err) {
9746 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
9747 goto err_pf_reset;
9748 }
9749
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00009750 /* set up a default setting for link flow control */
9751 pf->hw.fc.requested_mode = I40E_FC_NONE;
9752
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009753 err = i40e_init_adminq(hw);
9754 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
9755 if (err) {
9756 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +00009757 "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 +00009758 goto err_pf_reset;
9759 }
9760
Catherine Sullivan7aa67612014-07-09 07:46:17 +00009761 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
9762 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +00009763 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +00009764 "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");
9765 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
9766 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +00009767 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +00009768 "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 +00009769
Shannon Nelson4eb3f762014-03-06 08:59:58 +00009770 i40e_verify_eeprom(pf);
9771
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +00009772 /* Rev 0 hardware was never productized */
9773 if (hw->revision_id < 1)
9774 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");
9775
Shannon Nelson6ff4ef82013-12-21 05:44:49 +00009776 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009777 err = i40e_get_capabilities(pf);
9778 if (err)
9779 goto err_adminq_setup;
9780
9781 err = i40e_sw_init(pf);
9782 if (err) {
9783 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
9784 goto err_sw_init;
9785 }
9786
9787 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
9788 hw->func_caps.num_rx_qp,
9789 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
9790 if (err) {
9791 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
9792 goto err_init_lan_hmc;
9793 }
9794
9795 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
9796 if (err) {
9797 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
9798 err = -ENOENT;
9799 goto err_configure_lan_hmc;
9800 }
9801
Neerav Parikhb686ece2014-12-14 01:55:11 +00009802 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
9803 * Ignore error return codes because if it was already disabled via
9804 * hardware settings this will fail
9805 */
9806 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
9807 (pf->hw.aq.fw_maj_ver < 4)) {
9808 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
9809 i40e_aq_stop_lldp(hw, true, NULL);
9810 }
9811
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009812 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00009813 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009814 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
9815 err = -EIO;
9816 goto err_mac_addr;
9817 }
9818 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +00009819 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009820 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
9821 if (is_valid_ether_addr(hw->mac.port_addr))
9822 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -07009823#ifdef I40E_FCOE
9824 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
9825 if (err)
9826 dev_info(&pdev->dev,
9827 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
9828 if (!is_valid_ether_addr(hw->mac.san_addr)) {
9829 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
9830 hw->mac.san_addr);
9831 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
9832 }
9833 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
9834#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009835
9836 pci_set_drvdata(pdev, pf);
9837 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009838#ifdef CONFIG_I40E_DCB
9839 err = i40e_init_pf_dcb(pf);
9840 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00009841 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009842 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Neerav Parikh014269f2014-04-01 07:11:48 +00009843 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009844 }
9845#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009846
9847 /* set up periodic task facility */
9848 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
9849 pf->service_timer_period = HZ;
9850
9851 INIT_WORK(&pf->service_task, i40e_service_task);
9852 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
9853 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
9854 pf->link_check_timeout = jiffies;
9855
Shannon Nelson8e2773a2013-11-28 06:39:22 +00009856 /* WoL defaults to disabled */
9857 pf->wol_en = false;
9858 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
9859
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009860 /* set up the main switch operations */
9861 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04009862 err = i40e_init_interrupt_scheme(pf);
9863 if (err)
9864 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009865
Mitch Williams505682c2014-05-20 08:01:37 +00009866 /* The number of VSIs reported by the FW is the minimum guaranteed
9867 * to us; HW supports far more and we share the remaining pool with
9868 * the other PFs. We allocate space for more than the guarantee with
9869 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009870 */
Mitch Williams505682c2014-05-20 08:01:37 +00009871 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
9872 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
9873 else
9874 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
9875
9876 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
9877 len = sizeof(struct i40e_vsi *) * pf->num_alloc_vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009878 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00009879 if (!pf->vsi) {
9880 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009881 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00009882 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009883
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009884#ifdef CONFIG_PCI_IOV
9885 /* prep for VF support */
9886 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
9887 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
9888 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
9889 if (pci_num_vf(pdev))
9890 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9891 }
9892#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009893 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009894 if (err) {
9895 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
9896 goto err_vsis;
9897 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00009898 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +00009899 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00009900 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
9901 i40e_vsi_open(pf->vsi[i]);
9902 break;
9903 }
9904 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009905
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00009906 /* driver is only interested in link up/down and module qualification
9907 * reports from firmware
9908 */
9909 err = i40e_aq_set_phy_int_mask(&pf->hw,
9910 I40E_AQ_EVENT_LINK_UPDOWN |
9911 I40E_AQ_EVENT_MODULE_QUAL_FAIL, NULL);
9912 if (err)
9913 dev_info(&pf->pdev->dev, "set phy mask fail, aq_err %d\n", err);
9914
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00009915 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
9916 (pf->hw.aq.fw_maj_ver < 4)) {
9917 msleep(75);
9918 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
9919 if (err)
9920 dev_info(&pf->pdev->dev, "link restart failed, aq_err=%d\n",
9921 pf->hw.aq.asq_last_status);
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00009922 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009923 /* The main driver is (mostly) up and happy. We need to set this state
9924 * before setting up the misc vector or we get a race and the vector
9925 * ends up disabled forever.
9926 */
9927 clear_bit(__I40E_DOWN, &pf->state);
9928
9929 /* In case of MSIX we are going to setup the misc vector right here
9930 * to handle admin queue events etc. In case of legacy and MSI
9931 * the misc functionality and queue processing is combined in
9932 * the same vector and that gets setup at open.
9933 */
9934 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
9935 err = i40e_setup_misc_vector(pf);
9936 if (err) {
9937 dev_info(&pdev->dev,
9938 "setup of misc vector failed: %d\n", err);
9939 goto err_vsis;
9940 }
9941 }
9942
Greg Rosedf805f62014-04-04 04:43:16 +00009943#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009944 /* prep for VF support */
9945 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +00009946 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
9947 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009948 u32 val;
9949
9950 /* disable link interrupts for VFs */
9951 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
9952 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
9953 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
9954 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -08009955
9956 if (pci_num_vf(pdev)) {
9957 dev_info(&pdev->dev,
9958 "Active VFs found, allocating resources.\n");
9959 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
9960 if (err)
9961 dev_info(&pdev->dev,
9962 "Error %d allocating resources for existing VFs\n",
9963 err);
9964 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009965 }
Greg Rosedf805f62014-04-04 04:43:16 +00009966#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009967
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00009968 pfs_found++;
9969
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009970 i40e_dbg_pf_init(pf);
9971
9972 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00009973 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009974
9975 /* since everything's happy, start the service_task timer */
9976 mod_timer(&pf->service_timer,
9977 round_jiffies(jiffies + pf->service_timer_period));
9978
Vasu Dev38e00432014-08-01 13:27:03 -07009979#ifdef I40E_FCOE
9980 /* create FCoE interface */
9981 i40e_fcoe_vsi_setup(pf);
9982
9983#endif
Catherine Sullivand4dfb812013-11-28 06:39:21 +00009984 /* Get the negotiated link width and speed from PCI config space */
9985 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
9986
9987 i40e_set_pci_config_data(hw, link_status);
9988
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00009989 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +00009990 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
9991 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
9992 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
9993 "Unknown"),
9994 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
9995 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
9996 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
9997 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
9998 "Unknown"));
9999
10000 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
10001 hw->bus.speed < i40e_bus_speed_8000) {
10002 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
10003 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
10004 }
10005
Catherine Sullivane8278452015-02-06 08:52:08 +000010006 /* get the requested speeds from the fw */
10007 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
10008 if (err)
10009 dev_info(&pf->pdev->dev, "get phy abilities failed, aq_err %d, advertised speed settings may not be correct\n",
10010 err);
10011 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
10012
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010013 /* print a string summarizing features */
10014 i40e_print_features(pf);
10015
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010016 return 0;
10017
10018 /* Unwind what we've done if something failed in the setup */
10019err_vsis:
10020 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010021 i40e_clear_interrupt_scheme(pf);
10022 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000010023err_switch_setup:
10024 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010025 del_timer_sync(&pf->service_timer);
10026err_mac_addr:
10027err_configure_lan_hmc:
10028 (void)i40e_shutdown_lan_hmc(hw);
10029err_init_lan_hmc:
10030 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010031err_sw_init:
10032err_adminq_setup:
10033 (void)i40e_shutdown_adminq(hw);
10034err_pf_reset:
10035 iounmap(hw->hw_addr);
10036err_ioremap:
10037 kfree(pf);
10038err_pf_alloc:
10039 pci_disable_pcie_error_reporting(pdev);
10040 pci_release_selected_regions(pdev,
10041 pci_select_bars(pdev, IORESOURCE_MEM));
10042err_pci_reg:
10043err_dma:
10044 pci_disable_device(pdev);
10045 return err;
10046}
10047
10048/**
10049 * i40e_remove - Device removal routine
10050 * @pdev: PCI device information struct
10051 *
10052 * i40e_remove is called by the PCI subsystem to alert the driver
10053 * that is should release a PCI device. This could be caused by a
10054 * Hot-Plug event, or because the driver is going to be removed from
10055 * memory.
10056 **/
10057static void i40e_remove(struct pci_dev *pdev)
10058{
10059 struct i40e_pf *pf = pci_get_drvdata(pdev);
10060 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010061 int i;
10062
10063 i40e_dbg_pf_exit(pf);
10064
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010065 i40e_ptp_stop(pf);
10066
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010067 /* no more scheduling of any task */
10068 set_bit(__I40E_DOWN, &pf->state);
10069 del_timer_sync(&pf->service_timer);
10070 cancel_work_sync(&pf->service_task);
Mitch A Williams33c62b32015-02-21 06:44:51 +000010071 i40e_fdir_teardown(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010072
Mitch Williamseb2d80b2014-02-13 03:48:48 -080010073 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
10074 i40e_free_vfs(pf);
10075 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
10076 }
10077
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010078 i40e_fdir_teardown(pf);
10079
10080 /* If there is a switch structure or any orphans, remove them.
10081 * This will leave only the PF's VSI remaining.
10082 */
10083 for (i = 0; i < I40E_MAX_VEB; i++) {
10084 if (!pf->veb[i])
10085 continue;
10086
10087 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
10088 pf->veb[i]->uplink_seid == 0)
10089 i40e_switch_branch_release(pf->veb[i]);
10090 }
10091
10092 /* Now we can shutdown the PF's VSI, just before we kill
10093 * adminq and hmc.
10094 */
10095 if (pf->vsi[pf->lan_vsi])
10096 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
10097
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010098 /* shutdown and destroy the HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +000010099 if (pf->hw.hmc.hmc_obj) {
10100 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
10101 if (ret_code)
10102 dev_warn(&pdev->dev,
10103 "Failed to destroy the HMC resources: %d\n",
10104 ret_code);
10105 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010106
10107 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010108 ret_code = i40e_shutdown_adminq(&pf->hw);
10109 if (ret_code)
10110 dev_warn(&pdev->dev,
10111 "Failed to destroy the Admin Queue resources: %d\n",
10112 ret_code);
10113
10114 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
10115 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000010116 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010117 if (pf->vsi[i]) {
10118 i40e_vsi_clear_rings(pf->vsi[i]);
10119 i40e_vsi_clear(pf->vsi[i]);
10120 pf->vsi[i] = NULL;
10121 }
10122 }
10123
10124 for (i = 0; i < I40E_MAX_VEB; i++) {
10125 kfree(pf->veb[i]);
10126 pf->veb[i] = NULL;
10127 }
10128
10129 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010130 kfree(pf->vsi);
10131
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010132 iounmap(pf->hw.hw_addr);
10133 kfree(pf);
10134 pci_release_selected_regions(pdev,
10135 pci_select_bars(pdev, IORESOURCE_MEM));
10136
10137 pci_disable_pcie_error_reporting(pdev);
10138 pci_disable_device(pdev);
10139}
10140
10141/**
10142 * i40e_pci_error_detected - warning that something funky happened in PCI land
10143 * @pdev: PCI device information struct
10144 *
10145 * Called to warn that something happened and the error handling steps
10146 * are in progress. Allows the driver to quiesce things, be ready for
10147 * remediation.
10148 **/
10149static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
10150 enum pci_channel_state error)
10151{
10152 struct i40e_pf *pf = pci_get_drvdata(pdev);
10153
10154 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
10155
10156 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010157 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
10158 rtnl_lock();
10159 i40e_prep_for_reset(pf);
10160 rtnl_unlock();
10161 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010162
10163 /* Request a slot reset */
10164 return PCI_ERS_RESULT_NEED_RESET;
10165}
10166
10167/**
10168 * i40e_pci_error_slot_reset - a PCI slot reset just happened
10169 * @pdev: PCI device information struct
10170 *
10171 * Called to find if the driver can work with the device now that
10172 * the pci slot has been reset. If a basic connection seems good
10173 * (registers are readable and have sane content) then return a
10174 * happy little PCI_ERS_RESULT_xxx.
10175 **/
10176static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
10177{
10178 struct i40e_pf *pf = pci_get_drvdata(pdev);
10179 pci_ers_result_t result;
10180 int err;
10181 u32 reg;
10182
10183 dev_info(&pdev->dev, "%s\n", __func__);
10184 if (pci_enable_device_mem(pdev)) {
10185 dev_info(&pdev->dev,
10186 "Cannot re-enable PCI device after reset.\n");
10187 result = PCI_ERS_RESULT_DISCONNECT;
10188 } else {
10189 pci_set_master(pdev);
10190 pci_restore_state(pdev);
10191 pci_save_state(pdev);
10192 pci_wake_from_d3(pdev, false);
10193
10194 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
10195 if (reg == 0)
10196 result = PCI_ERS_RESULT_RECOVERED;
10197 else
10198 result = PCI_ERS_RESULT_DISCONNECT;
10199 }
10200
10201 err = pci_cleanup_aer_uncorrect_error_status(pdev);
10202 if (err) {
10203 dev_info(&pdev->dev,
10204 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
10205 err);
10206 /* non-fatal, continue */
10207 }
10208
10209 return result;
10210}
10211
10212/**
10213 * i40e_pci_error_resume - restart operations after PCI error recovery
10214 * @pdev: PCI device information struct
10215 *
10216 * Called to allow the driver to bring things back up after PCI error
10217 * and/or reset recovery has finished.
10218 **/
10219static void i40e_pci_error_resume(struct pci_dev *pdev)
10220{
10221 struct i40e_pf *pf = pci_get_drvdata(pdev);
10222
10223 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010224 if (test_bit(__I40E_SUSPENDED, &pf->state))
10225 return;
10226
10227 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010228 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010229 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010230}
10231
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010232/**
10233 * i40e_shutdown - PCI callback for shutting down
10234 * @pdev: PCI device information struct
10235 **/
10236static void i40e_shutdown(struct pci_dev *pdev)
10237{
10238 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010239 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010240
10241 set_bit(__I40E_SUSPENDED, &pf->state);
10242 set_bit(__I40E_DOWN, &pf->state);
10243 rtnl_lock();
10244 i40e_prep_for_reset(pf);
10245 rtnl_unlock();
10246
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010247 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
10248 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
10249
Shannon Nelsone1477582015-02-21 06:44:33 +000010250 i40e_clear_interrupt_scheme(pf);
10251
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010252 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010253 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010254 pci_set_power_state(pdev, PCI_D3hot);
10255 }
10256}
10257
10258#ifdef CONFIG_PM
10259/**
10260 * i40e_suspend - PCI callback for moving to D3
10261 * @pdev: PCI device information struct
10262 **/
10263static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
10264{
10265 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010266 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010267
10268 set_bit(__I40E_SUSPENDED, &pf->state);
10269 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams88086e5d2015-01-09 11:18:19 +000010270 del_timer_sync(&pf->service_timer);
10271 cancel_work_sync(&pf->service_task);
Mitch Williams3932dbf2015-03-31 00:45:04 -070010272 i40e_fdir_teardown(pf);
10273
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010274 rtnl_lock();
10275 i40e_prep_for_reset(pf);
10276 rtnl_unlock();
10277
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010278 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
10279 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
10280
10281 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010282 pci_set_power_state(pdev, PCI_D3hot);
10283
10284 return 0;
10285}
10286
10287/**
10288 * i40e_resume - PCI callback for waking up from D3
10289 * @pdev: PCI device information struct
10290 **/
10291static int i40e_resume(struct pci_dev *pdev)
10292{
10293 struct i40e_pf *pf = pci_get_drvdata(pdev);
10294 u32 err;
10295
10296 pci_set_power_state(pdev, PCI_D0);
10297 pci_restore_state(pdev);
10298 /* pci_restore_state() clears dev->state_saves, so
10299 * call pci_save_state() again to restore it.
10300 */
10301 pci_save_state(pdev);
10302
10303 err = pci_enable_device_mem(pdev);
10304 if (err) {
10305 dev_err(&pdev->dev,
10306 "%s: Cannot enable PCI device from suspend\n",
10307 __func__);
10308 return err;
10309 }
10310 pci_set_master(pdev);
10311
10312 /* no wakeup events while running */
10313 pci_wake_from_d3(pdev, false);
10314
10315 /* handling the reset will rebuild the device state */
10316 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
10317 clear_bit(__I40E_DOWN, &pf->state);
10318 rtnl_lock();
10319 i40e_reset_and_rebuild(pf, false);
10320 rtnl_unlock();
10321 }
10322
10323 return 0;
10324}
10325
10326#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010327static const struct pci_error_handlers i40e_err_handler = {
10328 .error_detected = i40e_pci_error_detected,
10329 .slot_reset = i40e_pci_error_slot_reset,
10330 .resume = i40e_pci_error_resume,
10331};
10332
10333static struct pci_driver i40e_driver = {
10334 .name = i40e_driver_name,
10335 .id_table = i40e_pci_tbl,
10336 .probe = i40e_probe,
10337 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010338#ifdef CONFIG_PM
10339 .suspend = i40e_suspend,
10340 .resume = i40e_resume,
10341#endif
10342 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010343 .err_handler = &i40e_err_handler,
10344 .sriov_configure = i40e_pci_sriov_configure,
10345};
10346
10347/**
10348 * i40e_init_module - Driver registration routine
10349 *
10350 * i40e_init_module is the first routine called when the driver is
10351 * loaded. All it does is register with the PCI subsystem.
10352 **/
10353static int __init i40e_init_module(void)
10354{
10355 pr_info("%s: %s - version %s\n", i40e_driver_name,
10356 i40e_driver_string, i40e_driver_version_str);
10357 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000010358
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010359 i40e_dbg_init();
10360 return pci_register_driver(&i40e_driver);
10361}
10362module_init(i40e_init_module);
10363
10364/**
10365 * i40e_exit_module - Driver exit cleanup routine
10366 *
10367 * i40e_exit_module is called just before the driver is removed
10368 * from memory.
10369 **/
10370static void __exit i40e_exit_module(void)
10371{
10372 pci_unregister_driver(&i40e_driver);
10373 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010374}
10375module_exit(i40e_exit_module);