blob: 4289ff1e84ef7d6a7b89f8d6407740805c9c5e04 [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Greg Rosedc641b72013-12-18 13:45:51 +00004 * Copyright(c) 2013 - 2014 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
40#define DRV_VERSION_MAJOR 0
Catherine Sullivane454d6b2014-04-23 04:50:17 +000041#define DRV_VERSION_MINOR 4
Catherine Sullivan7974d5e2014-06-03 23:50:25 +000042#define DRV_VERSION_BUILD 13
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 */
68static DEFINE_PCI_DEVICE_TABLE(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},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000077 /* required last entry */
78 {0, }
79};
80MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
81
82#define I40E_MAX_VF_COUNT 128
83static int debug = -1;
84module_param(debug, int, 0);
85MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
86
87MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
88MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
89MODULE_LICENSE("GPL");
90MODULE_VERSION(DRV_VERSION);
91
92/**
93 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
94 * @hw: pointer to the HW structure
95 * @mem: ptr to mem struct to fill out
96 * @size: size of memory requested
97 * @alignment: what to align the allocation to
98 **/
99int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
100 u64 size, u32 alignment)
101{
102 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
103
104 mem->size = ALIGN(size, alignment);
105 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
106 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000107 if (!mem->va)
108 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000109
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000110 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000111}
112
113/**
114 * i40e_free_dma_mem_d - OS specific memory free for shared code
115 * @hw: pointer to the HW structure
116 * @mem: ptr to mem struct to free
117 **/
118int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
119{
120 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
121
122 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
123 mem->va = NULL;
124 mem->pa = 0;
125 mem->size = 0;
126
127 return 0;
128}
129
130/**
131 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
132 * @hw: pointer to the HW structure
133 * @mem: ptr to mem struct to fill out
134 * @size: size of memory requested
135 **/
136int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
137 u32 size)
138{
139 mem->size = size;
140 mem->va = kzalloc(size, GFP_KERNEL);
141
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000142 if (!mem->va)
143 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000144
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000145 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000146}
147
148/**
149 * i40e_free_virt_mem_d - OS specific memory free for shared code
150 * @hw: pointer to the HW structure
151 * @mem: ptr to mem struct to free
152 **/
153int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
154{
155 /* it's ok to kfree a NULL pointer */
156 kfree(mem->va);
157 mem->va = NULL;
158 mem->size = 0;
159
160 return 0;
161}
162
163/**
164 * i40e_get_lump - find a lump of free generic resource
165 * @pf: board private structure
166 * @pile: the pile of resource to search
167 * @needed: the number of items needed
168 * @id: an owner id to stick on the items assigned
169 *
170 * Returns the base item index of the lump, or negative for error
171 *
172 * The search_hint trick and lack of advanced fit-finding only work
173 * because we're highly likely to have all the same size lump requests.
174 * Linear search time and any fragmentation should be minimal.
175 **/
176static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
177 u16 needed, u16 id)
178{
179 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000180 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000181
182 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
183 dev_info(&pf->pdev->dev,
184 "param err: pile=%p needed=%d id=0x%04x\n",
185 pile, needed, id);
186 return -EINVAL;
187 }
188
189 /* start the linear search with an imperfect hint */
190 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000191 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000192 /* skip already allocated entries */
193 if (pile->list[i] & I40E_PILE_VALID_BIT) {
194 i++;
195 continue;
196 }
197
198 /* do we have enough in this lump? */
199 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
200 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
201 break;
202 }
203
204 if (j == needed) {
205 /* there was enough, so assign it to the requestor */
206 for (j = 0; j < needed; j++)
207 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
208 ret = i;
209 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000210 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000211 } else {
212 /* not enough, so skip over it and continue looking */
213 i += j;
214 }
215 }
216
217 return ret;
218}
219
220/**
221 * i40e_put_lump - return a lump of generic resource
222 * @pile: the pile of resource to search
223 * @index: the base item index
224 * @id: the owner id of the items assigned
225 *
226 * Returns the count of items in the lump
227 **/
228static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
229{
230 int valid_id = (id | I40E_PILE_VALID_BIT);
231 int count = 0;
232 int i;
233
234 if (!pile || index >= pile->num_entries)
235 return -EINVAL;
236
237 for (i = index;
238 i < pile->num_entries && pile->list[i] == valid_id;
239 i++) {
240 pile->list[i] = 0;
241 count++;
242 }
243
244 if (count && index < pile->search_hint)
245 pile->search_hint = index;
246
247 return count;
248}
249
250/**
251 * i40e_service_event_schedule - Schedule the service task to wake up
252 * @pf: board private structure
253 *
254 * If not already scheduled, this puts the task into the work queue
255 **/
256static void i40e_service_event_schedule(struct i40e_pf *pf)
257{
258 if (!test_bit(__I40E_DOWN, &pf->state) &&
259 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
260 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
261 schedule_work(&pf->service_task);
262}
263
264/**
265 * i40e_tx_timeout - Respond to a Tx Hang
266 * @netdev: network interface device structure
267 *
268 * If any port has noticed a Tx timeout, it is likely that the whole
269 * device is munged, not just the one netdev port, so go for the full
270 * reset.
271 **/
272static void i40e_tx_timeout(struct net_device *netdev)
273{
274 struct i40e_netdev_priv *np = netdev_priv(netdev);
275 struct i40e_vsi *vsi = np->vsi;
276 struct i40e_pf *pf = vsi->back;
277
278 pf->tx_timeout_count++;
279
280 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
281 pf->tx_timeout_recovery_level = 0;
282 pf->tx_timeout_last_recovery = jiffies;
283 netdev_info(netdev, "tx_timeout recovery level %d\n",
284 pf->tx_timeout_recovery_level);
285
286 switch (pf->tx_timeout_recovery_level) {
287 case 0:
288 /* disable and re-enable queues for the VSI */
289 if (in_interrupt()) {
290 set_bit(__I40E_REINIT_REQUESTED, &pf->state);
291 set_bit(__I40E_REINIT_REQUESTED, &vsi->state);
292 } else {
293 i40e_vsi_reinit_locked(vsi);
294 }
295 break;
296 case 1:
297 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
298 break;
299 case 2:
300 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
301 break;
302 case 3:
303 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
304 break;
305 default:
306 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Neerav Parikhb5d06f02014-06-03 23:50:17 +0000307 set_bit(__I40E_DOWN_REQUESTED, &pf->state);
308 set_bit(__I40E_DOWN_REQUESTED, &vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000309 break;
310 }
311 i40e_service_event_schedule(pf);
312 pf->tx_timeout_recovery_level++;
313}
314
315/**
316 * i40e_release_rx_desc - Store the new tail and head values
317 * @rx_ring: ring to bump
318 * @val: new head index
319 **/
320static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
321{
322 rx_ring->next_to_use = val;
323
324 /* Force memory writes to complete before letting h/w
325 * know there are new descriptors to fetch. (Only
326 * applicable for weak-ordered memory model archs,
327 * such as IA-64).
328 */
329 wmb();
330 writel(val, rx_ring->tail);
331}
332
333/**
334 * i40e_get_vsi_stats_struct - Get System Network Statistics
335 * @vsi: the VSI we care about
336 *
337 * Returns the address of the device statistics structure.
338 * The statistics are actually updated from the service task.
339 **/
340struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
341{
342 return &vsi->net_stats;
343}
344
345/**
346 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
347 * @netdev: network interface device structure
348 *
349 * Returns the address of the device statistics structure.
350 * The statistics are actually updated from the service task.
351 **/
352static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
353 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000354 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000355{
356 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000357 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000358 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000359 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
360 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000361
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000362 if (test_bit(__I40E_DOWN, &vsi->state))
363 return stats;
364
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800365 if (!vsi->tx_rings)
366 return stats;
367
Alexander Duyck980e9b12013-09-28 06:01:03 +0000368 rcu_read_lock();
369 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000370 u64 bytes, packets;
371 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000372
Alexander Duyck980e9b12013-09-28 06:01:03 +0000373 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
374 if (!tx_ring)
375 continue;
376
377 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700378 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000379 packets = tx_ring->stats.packets;
380 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700381 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000382
383 stats->tx_packets += packets;
384 stats->tx_bytes += bytes;
385 rx_ring = &tx_ring[1];
386
387 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700388 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000389 packets = rx_ring->stats.packets;
390 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700391 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000392
393 stats->rx_packets += packets;
394 stats->rx_bytes += bytes;
395 }
396 rcu_read_unlock();
397
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000398 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000399 stats->multicast = vsi_stats->multicast;
400 stats->tx_errors = vsi_stats->tx_errors;
401 stats->tx_dropped = vsi_stats->tx_dropped;
402 stats->rx_errors = vsi_stats->rx_errors;
403 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
404 stats->rx_length_errors = vsi_stats->rx_length_errors;
405
406 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000407}
408
409/**
410 * i40e_vsi_reset_stats - Resets all stats of the given vsi
411 * @vsi: the VSI to have its stats reset
412 **/
413void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
414{
415 struct rtnl_link_stats64 *ns;
416 int i;
417
418 if (!vsi)
419 return;
420
421 ns = i40e_get_vsi_stats_struct(vsi);
422 memset(ns, 0, sizeof(*ns));
423 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
424 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
425 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000426 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000427 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000428 memset(&vsi->rx_rings[i]->stats, 0 ,
429 sizeof(vsi->rx_rings[i]->stats));
430 memset(&vsi->rx_rings[i]->rx_stats, 0 ,
431 sizeof(vsi->rx_rings[i]->rx_stats));
432 memset(&vsi->tx_rings[i]->stats, 0 ,
433 sizeof(vsi->tx_rings[i]->stats));
434 memset(&vsi->tx_rings[i]->tx_stats, 0,
435 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000436 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000437 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000438 vsi->stat_offsets_loaded = false;
439}
440
441/**
442 * i40e_pf_reset_stats - Reset all of the stats for the given pf
443 * @pf: the PF to be reset
444 **/
445void i40e_pf_reset_stats(struct i40e_pf *pf)
446{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000447 int i;
448
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000449 memset(&pf->stats, 0, sizeof(pf->stats));
450 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
451 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000452
453 for (i = 0; i < I40E_MAX_VEB; i++) {
454 if (pf->veb[i]) {
455 memset(&pf->veb[i]->stats, 0,
456 sizeof(pf->veb[i]->stats));
457 memset(&pf->veb[i]->stats_offsets, 0,
458 sizeof(pf->veb[i]->stats_offsets));
459 pf->veb[i]->stat_offsets_loaded = false;
460 }
461 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000462}
463
464/**
465 * i40e_stat_update48 - read and update a 48 bit stat from the chip
466 * @hw: ptr to the hardware info
467 * @hireg: the high 32 bit reg to read
468 * @loreg: the low 32 bit reg to read
469 * @offset_loaded: has the initial offset been loaded yet
470 * @offset: ptr to current offset value
471 * @stat: ptr to the stat
472 *
473 * Since the device stats are not reset at PFReset, they likely will not
474 * be zeroed when the driver starts. We'll save the first values read
475 * and use them as offsets to be subtracted from the raw values in order
476 * to report stats that count from zero. In the process, we also manage
477 * the potential roll-over.
478 **/
479static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
480 bool offset_loaded, u64 *offset, u64 *stat)
481{
482 u64 new_data;
483
Shannon Nelsonab600852014-01-17 15:36:39 -0800484 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000485 new_data = rd32(hw, loreg);
486 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
487 } else {
488 new_data = rd64(hw, loreg);
489 }
490 if (!offset_loaded)
491 *offset = new_data;
492 if (likely(new_data >= *offset))
493 *stat = new_data - *offset;
494 else
495 *stat = (new_data + ((u64)1 << 48)) - *offset;
496 *stat &= 0xFFFFFFFFFFFFULL;
497}
498
499/**
500 * i40e_stat_update32 - read and update a 32 bit stat from the chip
501 * @hw: ptr to the hardware info
502 * @reg: the hw reg to read
503 * @offset_loaded: has the initial offset been loaded yet
504 * @offset: ptr to current offset value
505 * @stat: ptr to the stat
506 **/
507static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
508 bool offset_loaded, u64 *offset, u64 *stat)
509{
510 u32 new_data;
511
512 new_data = rd32(hw, reg);
513 if (!offset_loaded)
514 *offset = new_data;
515 if (likely(new_data >= *offset))
516 *stat = (u32)(new_data - *offset);
517 else
518 *stat = (u32)((new_data + ((u64)1 << 32)) - *offset);
519}
520
521/**
522 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
523 * @vsi: the VSI to be updated
524 **/
525void i40e_update_eth_stats(struct i40e_vsi *vsi)
526{
527 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
528 struct i40e_pf *pf = vsi->back;
529 struct i40e_hw *hw = &pf->hw;
530 struct i40e_eth_stats *oes;
531 struct i40e_eth_stats *es; /* device's eth stats */
532
533 es = &vsi->eth_stats;
534 oes = &vsi->eth_stats_offsets;
535
536 /* Gather up the stats that the hw collects */
537 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
538 vsi->stat_offsets_loaded,
539 &oes->tx_errors, &es->tx_errors);
540 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
541 vsi->stat_offsets_loaded,
542 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000543 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
544 vsi->stat_offsets_loaded,
545 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
546 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
547 vsi->stat_offsets_loaded,
548 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000549
550 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
551 I40E_GLV_GORCL(stat_idx),
552 vsi->stat_offsets_loaded,
553 &oes->rx_bytes, &es->rx_bytes);
554 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
555 I40E_GLV_UPRCL(stat_idx),
556 vsi->stat_offsets_loaded,
557 &oes->rx_unicast, &es->rx_unicast);
558 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
559 I40E_GLV_MPRCL(stat_idx),
560 vsi->stat_offsets_loaded,
561 &oes->rx_multicast, &es->rx_multicast);
562 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
563 I40E_GLV_BPRCL(stat_idx),
564 vsi->stat_offsets_loaded,
565 &oes->rx_broadcast, &es->rx_broadcast);
566
567 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
568 I40E_GLV_GOTCL(stat_idx),
569 vsi->stat_offsets_loaded,
570 &oes->tx_bytes, &es->tx_bytes);
571 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
572 I40E_GLV_UPTCL(stat_idx),
573 vsi->stat_offsets_loaded,
574 &oes->tx_unicast, &es->tx_unicast);
575 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
576 I40E_GLV_MPTCL(stat_idx),
577 vsi->stat_offsets_loaded,
578 &oes->tx_multicast, &es->tx_multicast);
579 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
580 I40E_GLV_BPTCL(stat_idx),
581 vsi->stat_offsets_loaded,
582 &oes->tx_broadcast, &es->tx_broadcast);
583 vsi->stat_offsets_loaded = true;
584}
585
586/**
587 * i40e_update_veb_stats - Update Switch component statistics
588 * @veb: the VEB being updated
589 **/
590static void i40e_update_veb_stats(struct i40e_veb *veb)
591{
592 struct i40e_pf *pf = veb->pf;
593 struct i40e_hw *hw = &pf->hw;
594 struct i40e_eth_stats *oes;
595 struct i40e_eth_stats *es; /* device's eth stats */
596 int idx = 0;
597
598 idx = veb->stats_idx;
599 es = &veb->stats;
600 oes = &veb->stats_offsets;
601
602 /* Gather up the stats that the hw collects */
603 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
604 veb->stat_offsets_loaded,
605 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000606 if (hw->revision_id > 0)
607 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
608 veb->stat_offsets_loaded,
609 &oes->rx_unknown_protocol,
610 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000611 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
612 veb->stat_offsets_loaded,
613 &oes->rx_bytes, &es->rx_bytes);
614 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
615 veb->stat_offsets_loaded,
616 &oes->rx_unicast, &es->rx_unicast);
617 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
618 veb->stat_offsets_loaded,
619 &oes->rx_multicast, &es->rx_multicast);
620 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
621 veb->stat_offsets_loaded,
622 &oes->rx_broadcast, &es->rx_broadcast);
623
624 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
625 veb->stat_offsets_loaded,
626 &oes->tx_bytes, &es->tx_bytes);
627 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
628 veb->stat_offsets_loaded,
629 &oes->tx_unicast, &es->tx_unicast);
630 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
631 veb->stat_offsets_loaded,
632 &oes->tx_multicast, &es->tx_multicast);
633 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
634 veb->stat_offsets_loaded,
635 &oes->tx_broadcast, &es->tx_broadcast);
636 veb->stat_offsets_loaded = true;
637}
638
639/**
640 * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
641 * @pf: the corresponding PF
642 *
643 * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
644 **/
645static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
646{
647 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
648 struct i40e_hw_port_stats *nsd = &pf->stats;
649 struct i40e_hw *hw = &pf->hw;
650 u64 xoff = 0;
651 u16 i, v;
652
653 if ((hw->fc.current_mode != I40E_FC_FULL) &&
654 (hw->fc.current_mode != I40E_FC_RX_PAUSE))
655 return;
656
657 xoff = nsd->link_xoff_rx;
658 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
659 pf->stat_offsets_loaded,
660 &osd->link_xoff_rx, &nsd->link_xoff_rx);
661
662 /* No new LFC xoff rx */
663 if (!(nsd->link_xoff_rx - xoff))
664 return;
665
666 /* Clear the __I40E_HANG_CHECK_ARMED bit for all Tx rings */
Mitch Williams505682c2014-05-20 08:01:37 +0000667 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000668 struct i40e_vsi *vsi = pf->vsi[v];
669
Mitch Williamsddfda802014-05-10 04:49:10 +0000670 if (!vsi || !vsi->tx_rings[0])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000671 continue;
672
673 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000674 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000675 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
676 }
677 }
678}
679
680/**
681 * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
682 * @pf: the corresponding PF
683 *
684 * Update the Rx XOFF counter (PAUSE frames) in PFC mode
685 **/
686static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
687{
688 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
689 struct i40e_hw_port_stats *nsd = &pf->stats;
690 bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
691 struct i40e_dcbx_config *dcb_cfg;
692 struct i40e_hw *hw = &pf->hw;
693 u16 i, v;
694 u8 tc;
695
696 dcb_cfg = &hw->local_dcbx_config;
697
698 /* See if DCB enabled with PFC TC */
699 if (!(pf->flags & I40E_FLAG_DCB_ENABLED) ||
700 !(dcb_cfg->pfc.pfcenable)) {
701 i40e_update_link_xoff_rx(pf);
702 return;
703 }
704
705 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
706 u64 prio_xoff = nsd->priority_xoff_rx[i];
707 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
708 pf->stat_offsets_loaded,
709 &osd->priority_xoff_rx[i],
710 &nsd->priority_xoff_rx[i]);
711
712 /* No new PFC xoff rx */
713 if (!(nsd->priority_xoff_rx[i] - prio_xoff))
714 continue;
715 /* Get the TC for given priority */
716 tc = dcb_cfg->etscfg.prioritytable[i];
717 xoff[tc] = true;
718 }
719
720 /* Clear the __I40E_HANG_CHECK_ARMED bit for Tx rings */
Mitch Williams505682c2014-05-20 08:01:37 +0000721 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000722 struct i40e_vsi *vsi = pf->vsi[v];
723
Mitch Williamsddfda802014-05-10 04:49:10 +0000724 if (!vsi || !vsi->tx_rings[0])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000725 continue;
726
727 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000728 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000729
730 tc = ring->dcb_tc;
731 if (xoff[tc])
732 clear_bit(__I40E_HANG_CHECK_ARMED,
733 &ring->state);
734 }
735 }
736}
737
738/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000739 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000740 * @vsi: the VSI to be updated
741 *
742 * There are a few instances where we store the same stat in a
743 * couple of different structs. This is partly because we have
744 * the netdev stats that need to be filled out, which is slightly
745 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000746 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000747 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000748static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000749{
750 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000751 struct rtnl_link_stats64 *ons;
752 struct rtnl_link_stats64 *ns; /* netdev stats */
753 struct i40e_eth_stats *oes;
754 struct i40e_eth_stats *es; /* device's eth stats */
755 u32 tx_restart, tx_busy;
756 u32 rx_page, rx_buf;
757 u64 rx_p, rx_b;
758 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000759 u16 q;
760
761 if (test_bit(__I40E_DOWN, &vsi->state) ||
762 test_bit(__I40E_CONFIG_BUSY, &pf->state))
763 return;
764
765 ns = i40e_get_vsi_stats_struct(vsi);
766 ons = &vsi->net_stats_offsets;
767 es = &vsi->eth_stats;
768 oes = &vsi->eth_stats_offsets;
769
770 /* Gather up the netdev and vsi stats that the driver collects
771 * on the fly during packet processing
772 */
773 rx_b = rx_p = 0;
774 tx_b = tx_p = 0;
775 tx_restart = tx_busy = 0;
776 rx_page = 0;
777 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000778 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000779 for (q = 0; q < vsi->num_queue_pairs; q++) {
780 struct i40e_ring *p;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000781 u64 bytes, packets;
782 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000783
Alexander Duyck980e9b12013-09-28 06:01:03 +0000784 /* locate Tx ring */
785 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000786
Alexander Duyck980e9b12013-09-28 06:01:03 +0000787 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700788 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000789 packets = p->stats.packets;
790 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700791 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000792 tx_b += bytes;
793 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000794 tx_restart += p->tx_stats.restart_queue;
795 tx_busy += p->tx_stats.tx_busy;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000796
797 /* Rx queue is part of the same block as Tx queue */
798 p = &p[1];
799 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700800 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000801 packets = p->stats.packets;
802 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700803 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000804 rx_b += bytes;
805 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000806 rx_buf += p->rx_stats.alloc_buff_failed;
807 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000808 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000809 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000810 vsi->tx_restart = tx_restart;
811 vsi->tx_busy = tx_busy;
812 vsi->rx_page_failed = rx_page;
813 vsi->rx_buf_failed = rx_buf;
814
815 ns->rx_packets = rx_p;
816 ns->rx_bytes = rx_b;
817 ns->tx_packets = tx_p;
818 ns->tx_bytes = tx_b;
819
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000820 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000821 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000822 ons->tx_errors = oes->tx_errors;
823 ns->tx_errors = es->tx_errors;
824 ons->multicast = oes->rx_multicast;
825 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000826 ons->rx_dropped = oes->rx_discards;
827 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000828 ons->tx_dropped = oes->tx_discards;
829 ns->tx_dropped = es->tx_discards;
830
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000831 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000832 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000833 ns->rx_crc_errors = pf->stats.crc_errors;
834 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
835 ns->rx_length_errors = pf->stats.rx_length_errors;
836 }
837}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000838
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000839/**
840 * i40e_update_pf_stats - Update the pf statistics counters.
841 * @pf: the PF to be updated
842 **/
843static void i40e_update_pf_stats(struct i40e_pf *pf)
844{
845 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
846 struct i40e_hw_port_stats *nsd = &pf->stats;
847 struct i40e_hw *hw = &pf->hw;
848 u32 val;
849 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000850
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000851 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
852 I40E_GLPRT_GORCL(hw->port),
853 pf->stat_offsets_loaded,
854 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
855 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
856 I40E_GLPRT_GOTCL(hw->port),
857 pf->stat_offsets_loaded,
858 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
859 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
860 pf->stat_offsets_loaded,
861 &osd->eth.rx_discards,
862 &nsd->eth.rx_discards);
863 i40e_stat_update32(hw, I40E_GLPRT_TDPC(hw->port),
864 pf->stat_offsets_loaded,
865 &osd->eth.tx_discards,
866 &nsd->eth.tx_discards);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000867
Shannon Nelson532d2832014-04-23 04:50:09 +0000868 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
869 I40E_GLPRT_UPRCL(hw->port),
870 pf->stat_offsets_loaded,
871 &osd->eth.rx_unicast,
872 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000873 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
874 I40E_GLPRT_MPRCL(hw->port),
875 pf->stat_offsets_loaded,
876 &osd->eth.rx_multicast,
877 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000878 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
879 I40E_GLPRT_BPRCL(hw->port),
880 pf->stat_offsets_loaded,
881 &osd->eth.rx_broadcast,
882 &nsd->eth.rx_broadcast);
883 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
884 I40E_GLPRT_UPTCL(hw->port),
885 pf->stat_offsets_loaded,
886 &osd->eth.tx_unicast,
887 &nsd->eth.tx_unicast);
888 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
889 I40E_GLPRT_MPTCL(hw->port),
890 pf->stat_offsets_loaded,
891 &osd->eth.tx_multicast,
892 &nsd->eth.tx_multicast);
893 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
894 I40E_GLPRT_BPTCL(hw->port),
895 pf->stat_offsets_loaded,
896 &osd->eth.tx_broadcast,
897 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000898
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000899 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
900 pf->stat_offsets_loaded,
901 &osd->tx_dropped_link_down,
902 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000903
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000904 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
905 pf->stat_offsets_loaded,
906 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000907
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000908 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
909 pf->stat_offsets_loaded,
910 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000911
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000912 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
913 pf->stat_offsets_loaded,
914 &osd->mac_local_faults,
915 &nsd->mac_local_faults);
916 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->mac_remote_faults,
919 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000920
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000921 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
922 pf->stat_offsets_loaded,
923 &osd->rx_length_errors,
924 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000925
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000926 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
927 pf->stat_offsets_loaded,
928 &osd->link_xon_rx, &nsd->link_xon_rx);
929 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
930 pf->stat_offsets_loaded,
931 &osd->link_xon_tx, &nsd->link_xon_tx);
932 i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
933 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
934 pf->stat_offsets_loaded,
935 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000936
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000937 for (i = 0; i < 8; i++) {
938 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000939 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000940 &osd->priority_xon_rx[i],
941 &nsd->priority_xon_rx[i]);
942 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000943 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000944 &osd->priority_xon_tx[i],
945 &nsd->priority_xon_tx[i]);
946 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000947 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000948 &osd->priority_xoff_tx[i],
949 &nsd->priority_xoff_tx[i]);
950 i40e_stat_update32(hw,
951 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000952 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000953 &osd->priority_xon_2_xoff[i],
954 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000955 }
956
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000957 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
958 I40E_GLPRT_PRC64L(hw->port),
959 pf->stat_offsets_loaded,
960 &osd->rx_size_64, &nsd->rx_size_64);
961 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
962 I40E_GLPRT_PRC127L(hw->port),
963 pf->stat_offsets_loaded,
964 &osd->rx_size_127, &nsd->rx_size_127);
965 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
966 I40E_GLPRT_PRC255L(hw->port),
967 pf->stat_offsets_loaded,
968 &osd->rx_size_255, &nsd->rx_size_255);
969 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
970 I40E_GLPRT_PRC511L(hw->port),
971 pf->stat_offsets_loaded,
972 &osd->rx_size_511, &nsd->rx_size_511);
973 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
974 I40E_GLPRT_PRC1023L(hw->port),
975 pf->stat_offsets_loaded,
976 &osd->rx_size_1023, &nsd->rx_size_1023);
977 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
978 I40E_GLPRT_PRC1522L(hw->port),
979 pf->stat_offsets_loaded,
980 &osd->rx_size_1522, &nsd->rx_size_1522);
981 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
982 I40E_GLPRT_PRC9522L(hw->port),
983 pf->stat_offsets_loaded,
984 &osd->rx_size_big, &nsd->rx_size_big);
985
986 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
987 I40E_GLPRT_PTC64L(hw->port),
988 pf->stat_offsets_loaded,
989 &osd->tx_size_64, &nsd->tx_size_64);
990 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
991 I40E_GLPRT_PTC127L(hw->port),
992 pf->stat_offsets_loaded,
993 &osd->tx_size_127, &nsd->tx_size_127);
994 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
995 I40E_GLPRT_PTC255L(hw->port),
996 pf->stat_offsets_loaded,
997 &osd->tx_size_255, &nsd->tx_size_255);
998 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
999 I40E_GLPRT_PTC511L(hw->port),
1000 pf->stat_offsets_loaded,
1001 &osd->tx_size_511, &nsd->tx_size_511);
1002 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1003 I40E_GLPRT_PTC1023L(hw->port),
1004 pf->stat_offsets_loaded,
1005 &osd->tx_size_1023, &nsd->tx_size_1023);
1006 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1007 I40E_GLPRT_PTC1522L(hw->port),
1008 pf->stat_offsets_loaded,
1009 &osd->tx_size_1522, &nsd->tx_size_1522);
1010 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1011 I40E_GLPRT_PTC9522L(hw->port),
1012 pf->stat_offsets_loaded,
1013 &osd->tx_size_big, &nsd->tx_size_big);
1014
1015 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1016 pf->stat_offsets_loaded,
1017 &osd->rx_undersize, &nsd->rx_undersize);
1018 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1019 pf->stat_offsets_loaded,
1020 &osd->rx_fragments, &nsd->rx_fragments);
1021 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1022 pf->stat_offsets_loaded,
1023 &osd->rx_oversize, &nsd->rx_oversize);
1024 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1025 pf->stat_offsets_loaded,
1026 &osd->rx_jabber, &nsd->rx_jabber);
1027
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001028 /* FDIR stats */
1029 i40e_stat_update32(hw, I40E_GLQF_PCNT(pf->fd_atr_cnt_idx),
1030 pf->stat_offsets_loaded,
1031 &osd->fd_atr_match, &nsd->fd_atr_match);
1032 i40e_stat_update32(hw, I40E_GLQF_PCNT(pf->fd_sb_cnt_idx),
1033 pf->stat_offsets_loaded,
1034 &osd->fd_sb_match, &nsd->fd_sb_match);
1035
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001036 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1037 nsd->tx_lpi_status =
1038 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1039 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1040 nsd->rx_lpi_status =
1041 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1042 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1043 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1044 pf->stat_offsets_loaded,
1045 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1046 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1047 pf->stat_offsets_loaded,
1048 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1049
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001050 pf->stat_offsets_loaded = true;
1051}
1052
1053/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001054 * i40e_update_stats - Update the various statistics counters.
1055 * @vsi: the VSI to be updated
1056 *
1057 * Update the various stats for this VSI and its related entities.
1058 **/
1059void i40e_update_stats(struct i40e_vsi *vsi)
1060{
1061 struct i40e_pf *pf = vsi->back;
1062
1063 if (vsi == pf->vsi[pf->lan_vsi])
1064 i40e_update_pf_stats(pf);
1065
1066 i40e_update_vsi_stats(vsi);
1067}
1068
1069/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001070 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1071 * @vsi: the VSI to be searched
1072 * @macaddr: the MAC address
1073 * @vlan: the vlan
1074 * @is_vf: make sure its a vf filter, else doesn't matter
1075 * @is_netdev: make sure its a netdev filter, else doesn't matter
1076 *
1077 * Returns ptr to the filter object or NULL
1078 **/
1079static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1080 u8 *macaddr, s16 vlan,
1081 bool is_vf, bool is_netdev)
1082{
1083 struct i40e_mac_filter *f;
1084
1085 if (!vsi || !macaddr)
1086 return NULL;
1087
1088 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1089 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1090 (vlan == f->vlan) &&
1091 (!is_vf || f->is_vf) &&
1092 (!is_netdev || f->is_netdev))
1093 return f;
1094 }
1095 return NULL;
1096}
1097
1098/**
1099 * i40e_find_mac - Find a mac addr in the macvlan filters list
1100 * @vsi: the VSI to be searched
1101 * @macaddr: the MAC address we are searching for
1102 * @is_vf: make sure its a vf filter, else doesn't matter
1103 * @is_netdev: make sure its a netdev filter, else doesn't matter
1104 *
1105 * Returns the first filter with the provided MAC address or NULL if
1106 * MAC address was not found
1107 **/
1108struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1109 bool is_vf, bool is_netdev)
1110{
1111 struct i40e_mac_filter *f;
1112
1113 if (!vsi || !macaddr)
1114 return NULL;
1115
1116 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1117 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1118 (!is_vf || f->is_vf) &&
1119 (!is_netdev || f->is_netdev))
1120 return f;
1121 }
1122 return NULL;
1123}
1124
1125/**
1126 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1127 * @vsi: the VSI to be searched
1128 *
1129 * Returns true if VSI is in vlan mode or false otherwise
1130 **/
1131bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1132{
1133 struct i40e_mac_filter *f;
1134
1135 /* Only -1 for all the filters denotes not in vlan mode
1136 * so we have to go through all the list in order to make sure
1137 */
1138 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1139 if (f->vlan >= 0)
1140 return true;
1141 }
1142
1143 return false;
1144}
1145
1146/**
1147 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1148 * @vsi: the VSI to be searched
1149 * @macaddr: the mac address to be filtered
1150 * @is_vf: true if it is a vf
1151 * @is_netdev: true if it is a netdev
1152 *
1153 * Goes through all the macvlan filters and adds a
1154 * macvlan filter for each unique vlan that already exists
1155 *
1156 * Returns first filter found on success, else NULL
1157 **/
1158struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1159 bool is_vf, bool is_netdev)
1160{
1161 struct i40e_mac_filter *f;
1162
1163 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1164 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1165 is_vf, is_netdev)) {
1166 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001167 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001168 return NULL;
1169 }
1170 }
1171
1172 return list_first_entry_or_null(&vsi->mac_filter_list,
1173 struct i40e_mac_filter, list);
1174}
1175
1176/**
Greg Rose8c27d422014-05-22 06:31:56 +00001177 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1178 * @vsi: the PF Main VSI - inappropriate for any other VSI
1179 * @macaddr: the MAC address
1180 **/
1181static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1182{
1183 struct i40e_aqc_remove_macvlan_element_data element;
1184 struct i40e_pf *pf = vsi->back;
1185 i40e_status aq_ret;
1186
1187 /* Only appropriate for the PF main VSI */
1188 if (vsi->type != I40E_VSI_MAIN)
1189 return;
1190
1191 ether_addr_copy(element.mac_addr, macaddr);
1192 element.vlan_tag = 0;
1193 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1194 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1195 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1196 if (aq_ret)
1197 dev_err(&pf->pdev->dev, "Could not remove default MAC-VLAN\n");
1198}
1199
1200/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001201 * i40e_add_filter - Add a mac/vlan filter to the VSI
1202 * @vsi: the VSI to be searched
1203 * @macaddr: the MAC address
1204 * @vlan: the vlan
1205 * @is_vf: make sure its a vf filter, else doesn't matter
1206 * @is_netdev: make sure its a netdev filter, else doesn't matter
1207 *
1208 * Returns ptr to the filter object or NULL when no memory available.
1209 **/
1210struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1211 u8 *macaddr, s16 vlan,
1212 bool is_vf, bool is_netdev)
1213{
1214 struct i40e_mac_filter *f;
1215
1216 if (!vsi || !macaddr)
1217 return NULL;
1218
1219 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1220 if (!f) {
1221 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1222 if (!f)
1223 goto add_filter_out;
1224
Greg Rose9a173902014-05-22 06:32:02 +00001225 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001226 f->vlan = vlan;
1227 f->changed = true;
1228
1229 INIT_LIST_HEAD(&f->list);
1230 list_add(&f->list, &vsi->mac_filter_list);
1231 }
1232
1233 /* increment counter and add a new flag if needed */
1234 if (is_vf) {
1235 if (!f->is_vf) {
1236 f->is_vf = true;
1237 f->counter++;
1238 }
1239 } else if (is_netdev) {
1240 if (!f->is_netdev) {
1241 f->is_netdev = true;
1242 f->counter++;
1243 }
1244 } else {
1245 f->counter++;
1246 }
1247
1248 /* changed tells sync_filters_subtask to
1249 * push the filter down to the firmware
1250 */
1251 if (f->changed) {
1252 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1253 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1254 }
1255
1256add_filter_out:
1257 return f;
1258}
1259
1260/**
1261 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1262 * @vsi: the VSI to be searched
1263 * @macaddr: the MAC address
1264 * @vlan: the vlan
1265 * @is_vf: make sure it's a vf filter, else doesn't matter
1266 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1267 **/
1268void i40e_del_filter(struct i40e_vsi *vsi,
1269 u8 *macaddr, s16 vlan,
1270 bool is_vf, bool is_netdev)
1271{
1272 struct i40e_mac_filter *f;
1273
1274 if (!vsi || !macaddr)
1275 return;
1276
1277 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1278 if (!f || f->counter == 0)
1279 return;
1280
1281 if (is_vf) {
1282 if (f->is_vf) {
1283 f->is_vf = false;
1284 f->counter--;
1285 }
1286 } else if (is_netdev) {
1287 if (f->is_netdev) {
1288 f->is_netdev = false;
1289 f->counter--;
1290 }
1291 } else {
1292 /* make sure we don't remove a filter in use by vf or netdev */
1293 int min_f = 0;
1294 min_f += (f->is_vf ? 1 : 0);
1295 min_f += (f->is_netdev ? 1 : 0);
1296
1297 if (f->counter > min_f)
1298 f->counter--;
1299 }
1300
1301 /* counter == 0 tells sync_filters_subtask to
1302 * remove the filter from the firmware's list
1303 */
1304 if (f->counter == 0) {
1305 f->changed = true;
1306 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1307 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1308 }
1309}
1310
1311/**
1312 * i40e_set_mac - NDO callback to set mac address
1313 * @netdev: network interface device structure
1314 * @p: pointer to an address structure
1315 *
1316 * Returns 0 on success, negative on failure
1317 **/
1318static int i40e_set_mac(struct net_device *netdev, void *p)
1319{
1320 struct i40e_netdev_priv *np = netdev_priv(netdev);
1321 struct i40e_vsi *vsi = np->vsi;
1322 struct sockaddr *addr = p;
1323 struct i40e_mac_filter *f;
1324
1325 if (!is_valid_ether_addr(addr->sa_data))
1326 return -EADDRNOTAVAIL;
1327
1328 netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
1329
1330 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
1331 return 0;
1332
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001333 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1334 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1335 return -EADDRNOTAVAIL;
1336
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001337 if (vsi->type == I40E_VSI_MAIN) {
1338 i40e_status ret;
1339 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1340 I40E_AQC_WRITE_TYPE_LAA_ONLY,
1341 addr->sa_data, NULL);
1342 if (ret) {
1343 netdev_info(netdev,
1344 "Addr change for Main VSI failed: %d\n",
1345 ret);
1346 return -EADDRNOTAVAIL;
1347 }
1348
Greg Rose9a173902014-05-22 06:32:02 +00001349 ether_addr_copy(vsi->back->hw.mac.addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001350 }
1351
1352 /* In order to be sure to not drop any packets, add the new address
1353 * then delete the old one.
1354 */
1355 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
1356 if (!f)
1357 return -ENOMEM;
1358
1359 i40e_sync_vsi_filters(vsi);
1360 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
1361 i40e_sync_vsi_filters(vsi);
1362
Greg Rose9a173902014-05-22 06:32:02 +00001363 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001364
1365 return 0;
1366}
1367
1368/**
1369 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1370 * @vsi: the VSI being setup
1371 * @ctxt: VSI context structure
1372 * @enabled_tc: Enabled TCs bitmap
1373 * @is_add: True if called before Add VSI
1374 *
1375 * Setup VSI queue mapping for enabled traffic classes.
1376 **/
1377static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1378 struct i40e_vsi_context *ctxt,
1379 u8 enabled_tc,
1380 bool is_add)
1381{
1382 struct i40e_pf *pf = vsi->back;
1383 u16 sections = 0;
1384 u8 netdev_tc = 0;
1385 u16 numtc = 0;
1386 u16 qcount;
1387 u8 offset;
1388 u16 qmap;
1389 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001390 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001391
1392 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1393 offset = 0;
1394
1395 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1396 /* Find numtc from enabled TC bitmap */
1397 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1398 if (enabled_tc & (1 << i)) /* TC is enabled */
1399 numtc++;
1400 }
1401 if (!numtc) {
1402 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1403 numtc = 1;
1404 }
1405 } else {
1406 /* At least TC0 is enabled in case of non-DCB case */
1407 numtc = 1;
1408 }
1409
1410 vsi->tc_config.numtc = numtc;
1411 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001412 /* Number of queues per enabled TC */
Anjali Singhai Jaineb051af2014-05-20 08:01:46 +00001413 num_tc_qps = vsi->alloc_queue_pairs/numtc;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001414 num_tc_qps = min_t(int, num_tc_qps, I40E_MAX_QUEUES_PER_TC);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001415
1416 /* Setup queue offset/count for all TCs for given VSI */
1417 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1418 /* See if the given TC is enabled for the given VSI */
1419 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1420 int pow, num_qps;
1421
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001422 switch (vsi->type) {
1423 case I40E_VSI_MAIN:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001424 qcount = min_t(int, pf->rss_size, num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001425 break;
1426 case I40E_VSI_FDIR:
1427 case I40E_VSI_SRIOV:
1428 case I40E_VSI_VMDQ2:
1429 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001430 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001431 WARN_ON(i != 0);
1432 break;
1433 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001434 vsi->tc_config.tc_info[i].qoffset = offset;
1435 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001436
1437 /* find the power-of-2 of the number of queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001438 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001439 pow = 0;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001440 while (num_qps && ((1 << pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001441 pow++;
1442 num_qps >>= 1;
1443 }
1444
1445 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1446 qmap =
1447 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1448 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1449
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001450 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001451 } else {
1452 /* TC is not enabled so set the offset to
1453 * default queue and allocate one queue
1454 * for the given TC.
1455 */
1456 vsi->tc_config.tc_info[i].qoffset = 0;
1457 vsi->tc_config.tc_info[i].qcount = 1;
1458 vsi->tc_config.tc_info[i].netdev_tc = 0;
1459
1460 qmap = 0;
1461 }
1462 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1463 }
1464
1465 /* Set actual Tx/Rx queue pairs */
1466 vsi->num_queue_pairs = offset;
1467
1468 /* Scheduler section valid can only be set for ADD VSI */
1469 if (is_add) {
1470 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1471
1472 ctxt->info.up_enable_bits = enabled_tc;
1473 }
1474 if (vsi->type == I40E_VSI_SRIOV) {
1475 ctxt->info.mapping_flags |=
1476 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1477 for (i = 0; i < vsi->num_queue_pairs; i++)
1478 ctxt->info.queue_mapping[i] =
1479 cpu_to_le16(vsi->base_queue + i);
1480 } else {
1481 ctxt->info.mapping_flags |=
1482 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1483 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1484 }
1485 ctxt->info.valid_sections |= cpu_to_le16(sections);
1486}
1487
1488/**
1489 * i40e_set_rx_mode - NDO callback to set the netdev filters
1490 * @netdev: network interface device structure
1491 **/
1492static void i40e_set_rx_mode(struct net_device *netdev)
1493{
1494 struct i40e_netdev_priv *np = netdev_priv(netdev);
1495 struct i40e_mac_filter *f, *ftmp;
1496 struct i40e_vsi *vsi = np->vsi;
1497 struct netdev_hw_addr *uca;
1498 struct netdev_hw_addr *mca;
1499 struct netdev_hw_addr *ha;
1500
1501 /* add addr if not already in the filter list */
1502 netdev_for_each_uc_addr(uca, netdev) {
1503 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1504 if (i40e_is_vsi_in_vlan(vsi))
1505 i40e_put_mac_in_vlan(vsi, uca->addr,
1506 false, true);
1507 else
1508 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1509 false, true);
1510 }
1511 }
1512
1513 netdev_for_each_mc_addr(mca, netdev) {
1514 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1515 if (i40e_is_vsi_in_vlan(vsi))
1516 i40e_put_mac_in_vlan(vsi, mca->addr,
1517 false, true);
1518 else
1519 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1520 false, true);
1521 }
1522 }
1523
1524 /* remove filter if not in netdev list */
1525 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1526 bool found = false;
1527
1528 if (!f->is_netdev)
1529 continue;
1530
1531 if (is_multicast_ether_addr(f->macaddr)) {
1532 netdev_for_each_mc_addr(mca, netdev) {
1533 if (ether_addr_equal(mca->addr, f->macaddr)) {
1534 found = true;
1535 break;
1536 }
1537 }
1538 } else {
1539 netdev_for_each_uc_addr(uca, netdev) {
1540 if (ether_addr_equal(uca->addr, f->macaddr)) {
1541 found = true;
1542 break;
1543 }
1544 }
1545
1546 for_each_dev_addr(netdev, ha) {
1547 if (ether_addr_equal(ha->addr, f->macaddr)) {
1548 found = true;
1549 break;
1550 }
1551 }
1552 }
1553 if (!found)
1554 i40e_del_filter(
1555 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1556 }
1557
1558 /* check for other flag changes */
1559 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1560 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1561 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1562 }
1563}
1564
1565/**
1566 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1567 * @vsi: ptr to the VSI
1568 *
1569 * Push any outstanding VSI filter changes through the AdminQ.
1570 *
1571 * Returns 0 or error value
1572 **/
1573int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1574{
1575 struct i40e_mac_filter *f, *ftmp;
1576 bool promisc_forced_on = false;
1577 bool add_happened = false;
1578 int filter_list_len = 0;
1579 u32 changed_flags = 0;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001580 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001581 struct i40e_pf *pf;
1582 int num_add = 0;
1583 int num_del = 0;
1584 u16 cmd_flags;
1585
1586 /* empty array typed pointers, kcalloc later */
1587 struct i40e_aqc_add_macvlan_element_data *add_list;
1588 struct i40e_aqc_remove_macvlan_element_data *del_list;
1589
1590 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1591 usleep_range(1000, 2000);
1592 pf = vsi->back;
1593
1594 if (vsi->netdev) {
1595 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1596 vsi->current_netdev_flags = vsi->netdev->flags;
1597 }
1598
1599 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1600 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1601
1602 filter_list_len = pf->hw.aq.asq_buf_size /
1603 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1604 del_list = kcalloc(filter_list_len,
1605 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1606 GFP_KERNEL);
1607 if (!del_list)
1608 return -ENOMEM;
1609
1610 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1611 if (!f->changed)
1612 continue;
1613
1614 if (f->counter != 0)
1615 continue;
1616 f->changed = false;
1617 cmd_flags = 0;
1618
1619 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001620 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001621 del_list[num_del].vlan_tag =
1622 cpu_to_le16((u16)(f->vlan ==
1623 I40E_VLAN_ANY ? 0 : f->vlan));
1624
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001625 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1626 del_list[num_del].flags = cmd_flags;
1627 num_del++;
1628
1629 /* unlink from filter list */
1630 list_del(&f->list);
1631 kfree(f);
1632
1633 /* flush a full buffer */
1634 if (num_del == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001635 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001636 vsi->seid, del_list, num_del,
1637 NULL);
1638 num_del = 0;
1639 memset(del_list, 0, sizeof(*del_list));
1640
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001641 if (aq_ret &&
1642 pf->hw.aq.asq_last_status !=
1643 I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001644 dev_info(&pf->pdev->dev,
1645 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001646 aq_ret,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001647 pf->hw.aq.asq_last_status);
1648 }
1649 }
1650 if (num_del) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001651 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001652 del_list, num_del, NULL);
1653 num_del = 0;
1654
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001655 if (aq_ret &&
1656 pf->hw.aq.asq_last_status != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001657 dev_info(&pf->pdev->dev,
1658 "ignoring delete macvlan error, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001659 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001660 }
1661
1662 kfree(del_list);
1663 del_list = NULL;
1664
1665 /* do all the adds now */
1666 filter_list_len = pf->hw.aq.asq_buf_size /
1667 sizeof(struct i40e_aqc_add_macvlan_element_data),
1668 add_list = kcalloc(filter_list_len,
1669 sizeof(struct i40e_aqc_add_macvlan_element_data),
1670 GFP_KERNEL);
1671 if (!add_list)
1672 return -ENOMEM;
1673
1674 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1675 if (!f->changed)
1676 continue;
1677
1678 if (f->counter == 0)
1679 continue;
1680 f->changed = false;
1681 add_happened = true;
1682 cmd_flags = 0;
1683
1684 /* add to add array */
Greg Rose9a173902014-05-22 06:32:02 +00001685 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001686 add_list[num_add].vlan_tag =
1687 cpu_to_le16(
1688 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1689 add_list[num_add].queue_number = 0;
1690
1691 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001692 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1693 num_add++;
1694
1695 /* flush a full buffer */
1696 if (num_add == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001697 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1698 add_list, num_add,
1699 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001700 num_add = 0;
1701
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001702 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001703 break;
1704 memset(add_list, 0, sizeof(*add_list));
1705 }
1706 }
1707 if (num_add) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001708 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1709 add_list, num_add, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001710 num_add = 0;
1711 }
1712 kfree(add_list);
1713 add_list = NULL;
1714
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001715 if (add_happened && (!aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001716 /* do nothing */;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001717 } else if (add_happened && (aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001718 dev_info(&pf->pdev->dev,
1719 "add filter failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001720 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001721 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1722 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1723 &vsi->state)) {
1724 promisc_forced_on = true;
1725 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1726 &vsi->state);
1727 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1728 }
1729 }
1730 }
1731
1732 /* check for changes in promiscuous modes */
1733 if (changed_flags & IFF_ALLMULTI) {
1734 bool cur_multipromisc;
1735 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001736 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1737 vsi->seid,
1738 cur_multipromisc,
1739 NULL);
1740 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001741 dev_info(&pf->pdev->dev,
1742 "set multi promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001743 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001744 }
1745 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1746 bool cur_promisc;
1747 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1748 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1749 &vsi->state));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001750 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1751 vsi->seid,
1752 cur_promisc, NULL);
1753 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001754 dev_info(&pf->pdev->dev,
1755 "set uni promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001756 aq_ret, pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00001757 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1758 vsi->seid,
1759 cur_promisc, NULL);
1760 if (aq_ret)
1761 dev_info(&pf->pdev->dev,
1762 "set brdcast promisc failed, err %d, aq_err %d\n",
1763 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001764 }
1765
1766 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1767 return 0;
1768}
1769
1770/**
1771 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1772 * @pf: board private structure
1773 **/
1774static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1775{
1776 int v;
1777
1778 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1779 return;
1780 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1781
Mitch Williams505682c2014-05-20 08:01:37 +00001782 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001783 if (pf->vsi[v] &&
1784 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1785 i40e_sync_vsi_filters(pf->vsi[v]);
1786 }
1787}
1788
1789/**
1790 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1791 * @netdev: network interface device structure
1792 * @new_mtu: new value for maximum frame size
1793 *
1794 * Returns 0 on success, negative on failure
1795 **/
1796static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1797{
1798 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00001799 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001800 struct i40e_vsi *vsi = np->vsi;
1801
1802 /* MTU < 68 is an error and causes problems on some kernels */
1803 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1804 return -EINVAL;
1805
1806 netdev_info(netdev, "changing MTU from %d to %d\n",
1807 netdev->mtu, new_mtu);
1808 netdev->mtu = new_mtu;
1809 if (netif_running(netdev))
1810 i40e_vsi_reinit_locked(vsi);
1811
1812 return 0;
1813}
1814
1815/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001816 * i40e_ioctl - Access the hwtstamp interface
1817 * @netdev: network interface device structure
1818 * @ifr: interface request data
1819 * @cmd: ioctl command
1820 **/
1821int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1822{
1823 struct i40e_netdev_priv *np = netdev_priv(netdev);
1824 struct i40e_pf *pf = np->vsi->back;
1825
1826 switch (cmd) {
1827 case SIOCGHWTSTAMP:
1828 return i40e_ptp_get_ts_config(pf, ifr);
1829 case SIOCSHWTSTAMP:
1830 return i40e_ptp_set_ts_config(pf, ifr);
1831 default:
1832 return -EOPNOTSUPP;
1833 }
1834}
1835
1836/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001837 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1838 * @vsi: the vsi being adjusted
1839 **/
1840void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1841{
1842 struct i40e_vsi_context ctxt;
1843 i40e_status ret;
1844
1845 if ((vsi->info.valid_sections &
1846 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1847 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1848 return; /* already enabled */
1849
1850 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1851 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1852 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1853
1854 ctxt.seid = vsi->seid;
1855 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1856 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1857 if (ret) {
1858 dev_info(&vsi->back->pdev->dev,
1859 "%s: update vsi failed, aq_err=%d\n",
1860 __func__, vsi->back->hw.aq.asq_last_status);
1861 }
1862}
1863
1864/**
1865 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1866 * @vsi: the vsi being adjusted
1867 **/
1868void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
1869{
1870 struct i40e_vsi_context ctxt;
1871 i40e_status ret;
1872
1873 if ((vsi->info.valid_sections &
1874 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1875 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
1876 I40E_AQ_VSI_PVLAN_EMOD_MASK))
1877 return; /* already disabled */
1878
1879 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1880 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1881 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
1882
1883 ctxt.seid = vsi->seid;
1884 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1885 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1886 if (ret) {
1887 dev_info(&vsi->back->pdev->dev,
1888 "%s: update vsi failed, aq_err=%d\n",
1889 __func__, vsi->back->hw.aq.asq_last_status);
1890 }
1891}
1892
1893/**
1894 * i40e_vlan_rx_register - Setup or shutdown vlan offload
1895 * @netdev: network interface to be adjusted
1896 * @features: netdev features to test if VLAN offload is enabled or not
1897 **/
1898static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
1899{
1900 struct i40e_netdev_priv *np = netdev_priv(netdev);
1901 struct i40e_vsi *vsi = np->vsi;
1902
1903 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1904 i40e_vlan_stripping_enable(vsi);
1905 else
1906 i40e_vlan_stripping_disable(vsi);
1907}
1908
1909/**
1910 * i40e_vsi_add_vlan - Add vsi membership for given vlan
1911 * @vsi: the vsi being configured
1912 * @vid: vlan id to be added (0 = untagged only , -1 = any)
1913 **/
1914int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
1915{
1916 struct i40e_mac_filter *f, *add_f;
1917 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001918
1919 is_vf = (vsi->type == I40E_VSI_SRIOV);
1920 is_netdev = !!(vsi->netdev);
1921
1922 if (is_netdev) {
1923 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
1924 is_vf, is_netdev);
1925 if (!add_f) {
1926 dev_info(&vsi->back->pdev->dev,
1927 "Could not add vlan filter %d for %pM\n",
1928 vid, vsi->netdev->dev_addr);
1929 return -ENOMEM;
1930 }
1931 }
1932
1933 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1934 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1935 if (!add_f) {
1936 dev_info(&vsi->back->pdev->dev,
1937 "Could not add vlan filter %d for %pM\n",
1938 vid, f->macaddr);
1939 return -ENOMEM;
1940 }
1941 }
1942
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001943 /* Now if we add a vlan tag, make sure to check if it is the first
1944 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
1945 * with 0, so we now accept untagged and specified tagged traffic
1946 * (and not any taged and untagged)
1947 */
1948 if (vid > 0) {
1949 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
1950 I40E_VLAN_ANY,
1951 is_vf, is_netdev)) {
1952 i40e_del_filter(vsi, vsi->netdev->dev_addr,
1953 I40E_VLAN_ANY, is_vf, is_netdev);
1954 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
1955 is_vf, is_netdev);
1956 if (!add_f) {
1957 dev_info(&vsi->back->pdev->dev,
1958 "Could not add filter 0 for %pM\n",
1959 vsi->netdev->dev_addr);
1960 return -ENOMEM;
1961 }
1962 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08001963 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001964
Greg Rose8d82a7c2014-01-13 16:13:04 -08001965 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
1966 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001967 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1968 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1969 is_vf, is_netdev)) {
1970 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1971 is_vf, is_netdev);
1972 add_f = i40e_add_filter(vsi, f->macaddr,
1973 0, is_vf, is_netdev);
1974 if (!add_f) {
1975 dev_info(&vsi->back->pdev->dev,
1976 "Could not add filter 0 for %pM\n",
1977 f->macaddr);
1978 return -ENOMEM;
1979 }
1980 }
1981 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001982 }
1983
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001984 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1985 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1986 return 0;
1987
1988 return i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001989}
1990
1991/**
1992 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
1993 * @vsi: the vsi being configured
1994 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001995 *
1996 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001997 **/
1998int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
1999{
2000 struct net_device *netdev = vsi->netdev;
2001 struct i40e_mac_filter *f, *add_f;
2002 bool is_vf, is_netdev;
2003 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002004
2005 is_vf = (vsi->type == I40E_VSI_SRIOV);
2006 is_netdev = !!(netdev);
2007
2008 if (is_netdev)
2009 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
2010
2011 list_for_each_entry(f, &vsi->mac_filter_list, list)
2012 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2013
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002014 /* go through all the filters for this VSI and if there is only
2015 * vid == 0 it means there are no other filters, so vid 0 must
2016 * be replaced with -1. This signifies that we should from now
2017 * on accept any traffic (with any tag present, or untagged)
2018 */
2019 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2020 if (is_netdev) {
2021 if (f->vlan &&
2022 ether_addr_equal(netdev->dev_addr, f->macaddr))
2023 filter_count++;
2024 }
2025
2026 if (f->vlan)
2027 filter_count++;
2028 }
2029
2030 if (!filter_count && is_netdev) {
2031 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2032 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2033 is_vf, is_netdev);
2034 if (!f) {
2035 dev_info(&vsi->back->pdev->dev,
2036 "Could not add filter %d for %pM\n",
2037 I40E_VLAN_ANY, netdev->dev_addr);
2038 return -ENOMEM;
2039 }
2040 }
2041
2042 if (!filter_count) {
2043 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2044 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2045 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2046 is_vf, is_netdev);
2047 if (!add_f) {
2048 dev_info(&vsi->back->pdev->dev,
2049 "Could not add filter %d for %pM\n",
2050 I40E_VLAN_ANY, f->macaddr);
2051 return -ENOMEM;
2052 }
2053 }
2054 }
2055
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002056 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2057 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2058 return 0;
2059
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002060 return i40e_sync_vsi_filters(vsi);
2061}
2062
2063/**
2064 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2065 * @netdev: network interface to be adjusted
2066 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002067 *
2068 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002069 **/
2070static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2071 __always_unused __be16 proto, u16 vid)
2072{
2073 struct i40e_netdev_priv *np = netdev_priv(netdev);
2074 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002075 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002076
2077 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002078 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002079
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002080 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2081
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002082 /* If the network stack called us with vid = 0 then
2083 * it is asking to receive priority tagged packets with
2084 * vlan id 0. Our HW receives them by default when configured
2085 * to receive untagged packets so there is no need to add an
2086 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002087 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002088 if (vid)
2089 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002090
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002091 if (!ret && (vid < VLAN_N_VID))
2092 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002093
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002094 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002095}
2096
2097/**
2098 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2099 * @netdev: network interface to be adjusted
2100 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002101 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002102 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002103 **/
2104static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2105 __always_unused __be16 proto, u16 vid)
2106{
2107 struct i40e_netdev_priv *np = netdev_priv(netdev);
2108 struct i40e_vsi *vsi = np->vsi;
2109
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002110 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2111
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002112 /* return code is ignored as there is nothing a user
2113 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002114 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002115 */
2116 i40e_vsi_kill_vlan(vsi, vid);
2117
2118 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002119
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002120 return 0;
2121}
2122
2123/**
2124 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2125 * @vsi: the vsi being brought back up
2126 **/
2127static void i40e_restore_vlan(struct i40e_vsi *vsi)
2128{
2129 u16 vid;
2130
2131 if (!vsi->netdev)
2132 return;
2133
2134 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2135
2136 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2137 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2138 vid);
2139}
2140
2141/**
2142 * i40e_vsi_add_pvid - Add pvid for the VSI
2143 * @vsi: the vsi being adjusted
2144 * @vid: the vlan id to set as a PVID
2145 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002146int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002147{
2148 struct i40e_vsi_context ctxt;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002149 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002150
2151 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2152 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002153 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2154 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002155 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002156
2157 ctxt.seid = vsi->seid;
2158 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002159 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2160 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002161 dev_info(&vsi->back->pdev->dev,
2162 "%s: update vsi failed, aq_err=%d\n",
2163 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002164 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002165 }
2166
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002167 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002168}
2169
2170/**
2171 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2172 * @vsi: the vsi being adjusted
2173 *
2174 * Just use the vlan_rx_register() service to put it back to normal
2175 **/
2176void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2177{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002178 i40e_vlan_stripping_disable(vsi);
2179
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002180 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002181}
2182
2183/**
2184 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2185 * @vsi: ptr to the VSI
2186 *
2187 * If this function returns with an error, then it's possible one or
2188 * more of the rings is populated (while the rest are not). It is the
2189 * callers duty to clean those orphaned rings.
2190 *
2191 * Return 0 on success, negative on failure
2192 **/
2193static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2194{
2195 int i, err = 0;
2196
2197 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002198 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002199
2200 return err;
2201}
2202
2203/**
2204 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2205 * @vsi: ptr to the VSI
2206 *
2207 * Free VSI's transmit software resources
2208 **/
2209static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2210{
2211 int i;
2212
Greg Rose8e9dca52013-12-18 13:45:53 +00002213 if (!vsi->tx_rings)
2214 return;
2215
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002216 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002217 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002218 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002219}
2220
2221/**
2222 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2223 * @vsi: ptr to the VSI
2224 *
2225 * If this function returns with an error, then it's possible one or
2226 * more of the rings is populated (while the rest are not). It is the
2227 * callers duty to clean those orphaned rings.
2228 *
2229 * Return 0 on success, negative on failure
2230 **/
2231static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2232{
2233 int i, err = 0;
2234
2235 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002236 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002237 return err;
2238}
2239
2240/**
2241 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2242 * @vsi: ptr to the VSI
2243 *
2244 * Free all receive software resources
2245 **/
2246static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2247{
2248 int i;
2249
Greg Rose8e9dca52013-12-18 13:45:53 +00002250 if (!vsi->rx_rings)
2251 return;
2252
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002253 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002254 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002255 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002256}
2257
2258/**
2259 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2260 * @ring: The Tx ring to configure
2261 *
2262 * Configure the Tx descriptor ring in the HMC context.
2263 **/
2264static int i40e_configure_tx_ring(struct i40e_ring *ring)
2265{
2266 struct i40e_vsi *vsi = ring->vsi;
2267 u16 pf_q = vsi->base_queue + ring->queue_index;
2268 struct i40e_hw *hw = &vsi->back->hw;
2269 struct i40e_hmc_obj_txq tx_ctx;
2270 i40e_status err = 0;
2271 u32 qtx_ctl = 0;
2272
2273 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002274 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002275 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2276 ring->atr_count = 0;
2277 } else {
2278 ring->atr_sample_rate = 0;
2279 }
2280
2281 /* initialize XPS */
2282 if (ring->q_vector && ring->netdev &&
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08002283 vsi->tc_config.numtc <= 1 &&
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002284 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
2285 netif_set_xps_queue(ring->netdev,
2286 &ring->q_vector->affinity_mask,
2287 ring->queue_index);
2288
2289 /* clear the context structure first */
2290 memset(&tx_ctx, 0, sizeof(tx_ctx));
2291
2292 tx_ctx.new_context = 1;
2293 tx_ctx.base = (ring->dma / 128);
2294 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002295 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2296 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002297 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002298 /* FDIR VSI tx ring can still use RS bit and writebacks */
2299 if (vsi->type != I40E_VSI_FDIR)
2300 tx_ctx.head_wb_ena = 1;
2301 tx_ctx.head_wb_addr = ring->dma +
2302 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002303
2304 /* As part of VSI creation/update, FW allocates certain
2305 * Tx arbitration queue sets for each TC enabled for
2306 * the VSI. The FW returns the handles to these queue
2307 * sets as part of the response buffer to Add VSI,
2308 * Update VSI, etc. AQ commands. It is expected that
2309 * these queue set handles be associated with the Tx
2310 * queues by the driver as part of the TX queue context
2311 * initialization. This has to be done regardless of
2312 * DCB as by default everything is mapped to TC0.
2313 */
2314 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2315 tx_ctx.rdylist_act = 0;
2316
2317 /* clear the context in the HMC */
2318 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2319 if (err) {
2320 dev_info(&vsi->back->pdev->dev,
2321 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2322 ring->queue_index, pf_q, err);
2323 return -ENOMEM;
2324 }
2325
2326 /* set the context in the HMC */
2327 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2328 if (err) {
2329 dev_info(&vsi->back->pdev->dev,
2330 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2331 ring->queue_index, pf_q, err);
2332 return -ENOMEM;
2333 }
2334
2335 /* Now associate this queue with this PCI function */
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002336 if (vsi->type == I40E_VSI_VMDQ2)
2337 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
2338 else
2339 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +00002340 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2341 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002342 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2343 i40e_flush(hw);
2344
2345 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2346
2347 /* cache tail off for easier writes later */
2348 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2349
2350 return 0;
2351}
2352
2353/**
2354 * i40e_configure_rx_ring - Configure a receive ring context
2355 * @ring: The Rx ring to configure
2356 *
2357 * Configure the Rx descriptor ring in the HMC context.
2358 **/
2359static int i40e_configure_rx_ring(struct i40e_ring *ring)
2360{
2361 struct i40e_vsi *vsi = ring->vsi;
2362 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2363 u16 pf_q = vsi->base_queue + ring->queue_index;
2364 struct i40e_hw *hw = &vsi->back->hw;
2365 struct i40e_hmc_obj_rxq rx_ctx;
2366 i40e_status err = 0;
2367
2368 ring->state = 0;
2369
2370 /* clear the context structure first */
2371 memset(&rx_ctx, 0, sizeof(rx_ctx));
2372
2373 ring->rx_buf_len = vsi->rx_buf_len;
2374 ring->rx_hdr_len = vsi->rx_hdr_len;
2375
2376 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2377 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2378
2379 rx_ctx.base = (ring->dma / 128);
2380 rx_ctx.qlen = ring->count;
2381
2382 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2383 set_ring_16byte_desc_enabled(ring);
2384 rx_ctx.dsize = 0;
2385 } else {
2386 rx_ctx.dsize = 1;
2387 }
2388
2389 rx_ctx.dtype = vsi->dtype;
2390 if (vsi->dtype) {
2391 set_ring_ps_enabled(ring);
2392 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2393 I40E_RX_SPLIT_IP |
2394 I40E_RX_SPLIT_TCP_UDP |
2395 I40E_RX_SPLIT_SCTP;
2396 } else {
2397 rx_ctx.hsplit_0 = 0;
2398 }
2399
2400 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2401 (chain_len * ring->rx_buf_len));
2402 rx_ctx.tphrdesc_ena = 1;
2403 rx_ctx.tphwdesc_ena = 1;
2404 rx_ctx.tphdata_ena = 1;
2405 rx_ctx.tphhead_ena = 1;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002406 if (hw->revision_id == 0)
2407 rx_ctx.lrxqthresh = 0;
2408 else
2409 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002410 rx_ctx.crcstrip = 1;
2411 rx_ctx.l2tsel = 1;
2412 rx_ctx.showiv = 1;
Catherine Sullivanacb36762014-03-06 09:02:30 +00002413 /* set the prefena field to 1 because the manual says to */
2414 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002415
2416 /* clear the context in the HMC */
2417 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2418 if (err) {
2419 dev_info(&vsi->back->pdev->dev,
2420 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2421 ring->queue_index, pf_q, err);
2422 return -ENOMEM;
2423 }
2424
2425 /* set the context in the HMC */
2426 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2427 if (err) {
2428 dev_info(&vsi->back->pdev->dev,
2429 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2430 ring->queue_index, pf_q, err);
2431 return -ENOMEM;
2432 }
2433
2434 /* cache tail for quicker writes, and clear the reg before use */
2435 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2436 writel(0, ring->tail);
2437
2438 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
2439
2440 return 0;
2441}
2442
2443/**
2444 * i40e_vsi_configure_tx - Configure the VSI for Tx
2445 * @vsi: VSI structure describing this set of rings and resources
2446 *
2447 * Configure the Tx VSI for operation.
2448 **/
2449static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2450{
2451 int err = 0;
2452 u16 i;
2453
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002454 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2455 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002456
2457 return err;
2458}
2459
2460/**
2461 * i40e_vsi_configure_rx - Configure the VSI for Rx
2462 * @vsi: the VSI being configured
2463 *
2464 * Configure the Rx VSI for operation.
2465 **/
2466static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2467{
2468 int err = 0;
2469 u16 i;
2470
2471 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2472 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2473 + ETH_FCS_LEN + VLAN_HLEN;
2474 else
2475 vsi->max_frame = I40E_RXBUFFER_2048;
2476
2477 /* figure out correct receive buffer length */
2478 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2479 I40E_FLAG_RX_PS_ENABLED)) {
2480 case I40E_FLAG_RX_1BUF_ENABLED:
2481 vsi->rx_hdr_len = 0;
2482 vsi->rx_buf_len = vsi->max_frame;
2483 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2484 break;
2485 case I40E_FLAG_RX_PS_ENABLED:
2486 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2487 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2488 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2489 break;
2490 default:
2491 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2492 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2493 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2494 break;
2495 }
2496
2497 /* round up for the chip's needs */
2498 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2499 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2500 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2501 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2502
2503 /* set up individual rings */
2504 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002505 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002506
2507 return err;
2508}
2509
2510/**
2511 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2512 * @vsi: ptr to the VSI
2513 **/
2514static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2515{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002516 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002517 u16 qoffset, qcount;
2518 int i, n;
2519
2520 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2521 return;
2522
2523 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2524 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2525 continue;
2526
2527 qoffset = vsi->tc_config.tc_info[n].qoffset;
2528 qcount = vsi->tc_config.tc_info[n].qcount;
2529 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002530 rx_ring = vsi->rx_rings[i];
2531 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002532 rx_ring->dcb_tc = n;
2533 tx_ring->dcb_tc = n;
2534 }
2535 }
2536}
2537
2538/**
2539 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2540 * @vsi: ptr to the VSI
2541 **/
2542static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2543{
2544 if (vsi->netdev)
2545 i40e_set_rx_mode(vsi->netdev);
2546}
2547
2548/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002549 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
2550 * @vsi: Pointer to the targeted VSI
2551 *
2552 * This function replays the hlist on the hw where all the SB Flow Director
2553 * filters were saved.
2554 **/
2555static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
2556{
2557 struct i40e_fdir_filter *filter;
2558 struct i40e_pf *pf = vsi->back;
2559 struct hlist_node *node;
2560
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002561 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2562 return;
2563
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002564 hlist_for_each_entry_safe(filter, node,
2565 &pf->fdir_filter_list, fdir_node) {
2566 i40e_add_del_fdir(vsi, filter, true);
2567 }
2568}
2569
2570/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002571 * i40e_vsi_configure - Set up the VSI for action
2572 * @vsi: the VSI being configured
2573 **/
2574static int i40e_vsi_configure(struct i40e_vsi *vsi)
2575{
2576 int err;
2577
2578 i40e_set_vsi_rx_mode(vsi);
2579 i40e_restore_vlan(vsi);
2580 i40e_vsi_config_dcb_rings(vsi);
2581 err = i40e_vsi_configure_tx(vsi);
2582 if (!err)
2583 err = i40e_vsi_configure_rx(vsi);
2584
2585 return err;
2586}
2587
2588/**
2589 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2590 * @vsi: the VSI being configured
2591 **/
2592static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2593{
2594 struct i40e_pf *pf = vsi->back;
2595 struct i40e_q_vector *q_vector;
2596 struct i40e_hw *hw = &pf->hw;
2597 u16 vector;
2598 int i, q;
2599 u32 val;
2600 u32 qp;
2601
2602 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2603 * and PFINT_LNKLSTn registers, e.g.:
2604 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2605 */
2606 qp = vsi->base_queue;
2607 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002608 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2609 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002610 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2611 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2612 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2613 q_vector->rx.itr);
2614 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2615 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2616 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2617 q_vector->tx.itr);
2618
2619 /* Linked list for the queuepairs assigned to this vector */
2620 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2621 for (q = 0; q < q_vector->num_ringpairs; q++) {
2622 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2623 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2624 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2625 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2626 (I40E_QUEUE_TYPE_TX
2627 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2628
2629 wr32(hw, I40E_QINT_RQCTL(qp), val);
2630
2631 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2632 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2633 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2634 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2635 (I40E_QUEUE_TYPE_RX
2636 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2637
2638 /* Terminate the linked list */
2639 if (q == (q_vector->num_ringpairs - 1))
2640 val |= (I40E_QUEUE_END_OF_LIST
2641 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2642
2643 wr32(hw, I40E_QINT_TQCTL(qp), val);
2644 qp++;
2645 }
2646 }
2647
2648 i40e_flush(hw);
2649}
2650
2651/**
2652 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2653 * @hw: ptr to the hardware info
2654 **/
2655static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2656{
2657 u32 val;
2658
2659 /* clear things first */
2660 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2661 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2662
2663 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2664 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2665 I40E_PFINT_ICR0_ENA_GRST_MASK |
2666 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2667 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002668 I40E_PFINT_ICR0_ENA_TIMESYNC_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002669 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2670 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2671 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2672
2673 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2674
2675 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002676 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2677 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002678
2679 /* OTHER_ITR_IDX = 0 */
2680 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2681}
2682
2683/**
2684 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2685 * @vsi: the VSI being configured
2686 **/
2687static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2688{
Alexander Duyck493fb302013-09-28 07:01:44 +00002689 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002690 struct i40e_pf *pf = vsi->back;
2691 struct i40e_hw *hw = &pf->hw;
2692 u32 val;
2693
2694 /* set the ITR configuration */
2695 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2696 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2697 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2698 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2699 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2700 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2701
2702 i40e_enable_misc_int_causes(hw);
2703
2704 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2705 wr32(hw, I40E_PFINT_LNKLST0, 0);
2706
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00002707 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002708 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2709 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2710 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2711
2712 wr32(hw, I40E_QINT_RQCTL(0), val);
2713
2714 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2715 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2716 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2717
2718 wr32(hw, I40E_QINT_TQCTL(0), val);
2719 i40e_flush(hw);
2720}
2721
2722/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00002723 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2724 * @pf: board private structure
2725 **/
2726void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2727{
2728 struct i40e_hw *hw = &pf->hw;
2729
2730 wr32(hw, I40E_PFINT_DYN_CTL0,
2731 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2732 i40e_flush(hw);
2733}
2734
2735/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002736 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2737 * @pf: board private structure
2738 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002739void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002740{
2741 struct i40e_hw *hw = &pf->hw;
2742 u32 val;
2743
2744 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2745 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2746 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2747
2748 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2749 i40e_flush(hw);
2750}
2751
2752/**
2753 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2754 * @vsi: pointer to a vsi
2755 * @vector: enable a particular Hw Interrupt vector
2756 **/
2757void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2758{
2759 struct i40e_pf *pf = vsi->back;
2760 struct i40e_hw *hw = &pf->hw;
2761 u32 val;
2762
2763 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2764 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2765 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2766 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002767 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002768}
2769
2770/**
2771 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2772 * @irq: interrupt number
2773 * @data: pointer to a q_vector
2774 **/
2775static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2776{
2777 struct i40e_q_vector *q_vector = data;
2778
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002779 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002780 return IRQ_HANDLED;
2781
2782 napi_schedule(&q_vector->napi);
2783
2784 return IRQ_HANDLED;
2785}
2786
2787/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002788 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2789 * @vsi: the VSI being configured
2790 * @basename: name for the vector
2791 *
2792 * Allocates MSI-X vectors and requests interrupts from the kernel.
2793 **/
2794static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2795{
2796 int q_vectors = vsi->num_q_vectors;
2797 struct i40e_pf *pf = vsi->back;
2798 int base = vsi->base_vector;
2799 int rx_int_idx = 0;
2800 int tx_int_idx = 0;
2801 int vector, err;
2802
2803 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00002804 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002805
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002806 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002807 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2808 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2809 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002810 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002811 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2812 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002813 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002814 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2815 "%s-%s-%d", basename, "tx", tx_int_idx++);
2816 } else {
2817 /* skip this unused q_vector */
2818 continue;
2819 }
2820 err = request_irq(pf->msix_entries[base + vector].vector,
2821 vsi->irq_handler,
2822 0,
2823 q_vector->name,
2824 q_vector);
2825 if (err) {
2826 dev_info(&pf->pdev->dev,
2827 "%s: request_irq failed, error: %d\n",
2828 __func__, err);
2829 goto free_queue_irqs;
2830 }
2831 /* assign the mask for this irq */
2832 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2833 &q_vector->affinity_mask);
2834 }
2835
Shannon Nelson63741842014-04-23 04:50:16 +00002836 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002837 return 0;
2838
2839free_queue_irqs:
2840 while (vector) {
2841 vector--;
2842 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2843 NULL);
2844 free_irq(pf->msix_entries[base + vector].vector,
2845 &(vsi->q_vectors[vector]));
2846 }
2847 return err;
2848}
2849
2850/**
2851 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2852 * @vsi: the VSI being un-configured
2853 **/
2854static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2855{
2856 struct i40e_pf *pf = vsi->back;
2857 struct i40e_hw *hw = &pf->hw;
2858 int base = vsi->base_vector;
2859 int i;
2860
2861 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002862 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2863 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002864 }
2865
2866 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2867 for (i = vsi->base_vector;
2868 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2869 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2870
2871 i40e_flush(hw);
2872 for (i = 0; i < vsi->num_q_vectors; i++)
2873 synchronize_irq(pf->msix_entries[i + base].vector);
2874 } else {
2875 /* Legacy and MSI mode - this stops all interrupt handling */
2876 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2877 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2878 i40e_flush(hw);
2879 synchronize_irq(pf->pdev->irq);
2880 }
2881}
2882
2883/**
2884 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2885 * @vsi: the VSI being configured
2886 **/
2887static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2888{
2889 struct i40e_pf *pf = vsi->back;
2890 int i;
2891
2892 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2893 for (i = vsi->base_vector;
2894 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2895 i40e_irq_dynamic_enable(vsi, i);
2896 } else {
2897 i40e_irq_dynamic_enable_icr0(pf);
2898 }
2899
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002900 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002901 return 0;
2902}
2903
2904/**
2905 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2906 * @pf: board private structure
2907 **/
2908static void i40e_stop_misc_vector(struct i40e_pf *pf)
2909{
2910 /* Disable ICR 0 */
2911 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2912 i40e_flush(&pf->hw);
2913}
2914
2915/**
2916 * i40e_intr - MSI/Legacy and non-queue interrupt handler
2917 * @irq: interrupt number
2918 * @data: pointer to a q_vector
2919 *
2920 * This is the handler used for all MSI/Legacy interrupts, and deals
2921 * with both queue and non-queue interrupts. This is also used in
2922 * MSIX mode to handle the non-queue interrupts.
2923 **/
2924static irqreturn_t i40e_intr(int irq, void *data)
2925{
2926 struct i40e_pf *pf = (struct i40e_pf *)data;
2927 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002928 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002929 u32 icr0, icr0_remaining;
2930 u32 val, ena_mask;
2931
2932 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002933 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002934
Shannon Nelson116a57d2013-09-28 07:13:59 +00002935 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2936 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002937 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002938
Shannon Nelsoncd92e722013-11-16 10:00:44 +00002939 /* if interrupt but no bits showing, must be SWINT */
2940 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2941 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2942 pf->sw_int_count++;
2943
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002944 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2945 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2946
2947 /* temporarily disable queue cause for NAPI processing */
2948 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2949 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2950 wr32(hw, I40E_QINT_RQCTL(0), qval);
2951
2952 qval = rd32(hw, I40E_QINT_TQCTL(0));
2953 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2954 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002955
2956 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00002957 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002958 }
2959
2960 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2961 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2962 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2963 }
2964
2965 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2966 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2967 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2968 }
2969
2970 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2971 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2972 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2973 }
2974
2975 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2976 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2977 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2978 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2979 val = rd32(hw, I40E_GLGEN_RSTAT);
2980 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2981 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002982 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002983 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002984 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002985 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002986 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002987 pf->empr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002988 set_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
2989 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002990 }
2991
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002992 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2993 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
2994 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2995 }
2996
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002997 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
2998 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
2999
3000 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003001 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003002 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003003 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003004 }
3005
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003006 /* If a critical error is pending we have no choice but to reset the
3007 * device.
3008 * Report and mask out any remaining unexpected interrupts.
3009 */
3010 icr0_remaining = icr0 & ena_mask;
3011 if (icr0_remaining) {
3012 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3013 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003014 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003015 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003016 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003017 dev_info(&pf->pdev->dev, "device will be reset\n");
3018 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3019 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003020 }
3021 ena_mask &= ~icr0_remaining;
3022 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003023 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003024
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003025enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003026 /* re-enable interrupt causes */
3027 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003028 if (!test_bit(__I40E_DOWN, &pf->state)) {
3029 i40e_service_event_schedule(pf);
3030 i40e_irq_dynamic_enable_icr0(pf);
3031 }
3032
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003033 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003034}
3035
3036/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003037 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3038 * @tx_ring: tx ring to clean
3039 * @budget: how many cleans we're allowed
3040 *
3041 * Returns true if there's any budget left (e.g. the clean is finished)
3042 **/
3043static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3044{
3045 struct i40e_vsi *vsi = tx_ring->vsi;
3046 u16 i = tx_ring->next_to_clean;
3047 struct i40e_tx_buffer *tx_buf;
3048 struct i40e_tx_desc *tx_desc;
3049
3050 tx_buf = &tx_ring->tx_bi[i];
3051 tx_desc = I40E_TX_DESC(tx_ring, i);
3052 i -= tx_ring->count;
3053
3054 do {
3055 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3056
3057 /* if next_to_watch is not set then there is no work pending */
3058 if (!eop_desc)
3059 break;
3060
3061 /* prevent any other reads prior to eop_desc */
3062 read_barrier_depends();
3063
3064 /* if the descriptor isn't done, no work yet to do */
3065 if (!(eop_desc->cmd_type_offset_bsz &
3066 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3067 break;
3068
3069 /* clear next_to_watch to prevent false hangs */
3070 tx_buf->next_to_watch = NULL;
3071
3072 /* unmap skb header data */
3073 dma_unmap_single(tx_ring->dev,
3074 dma_unmap_addr(tx_buf, dma),
3075 dma_unmap_len(tx_buf, len),
3076 DMA_TO_DEVICE);
3077
3078 dma_unmap_len_set(tx_buf, len, 0);
3079
3080
3081 /* move to the next desc and buffer to clean */
3082 tx_buf++;
3083 tx_desc++;
3084 i++;
3085 if (unlikely(!i)) {
3086 i -= tx_ring->count;
3087 tx_buf = tx_ring->tx_bi;
3088 tx_desc = I40E_TX_DESC(tx_ring, 0);
3089 }
3090
3091 /* update budget accounting */
3092 budget--;
3093 } while (likely(budget));
3094
3095 i += tx_ring->count;
3096 tx_ring->next_to_clean = i;
3097
3098 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
3099 i40e_irq_dynamic_enable(vsi,
3100 tx_ring->q_vector->v_idx + vsi->base_vector);
3101 }
3102 return budget > 0;
3103}
3104
3105/**
3106 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3107 * @irq: interrupt number
3108 * @data: pointer to a q_vector
3109 **/
3110static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3111{
3112 struct i40e_q_vector *q_vector = data;
3113 struct i40e_vsi *vsi;
3114
3115 if (!q_vector->tx.ring)
3116 return IRQ_HANDLED;
3117
3118 vsi = q_vector->tx.ring->vsi;
3119 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3120
3121 return IRQ_HANDLED;
3122}
3123
3124/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003125 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003126 * @vsi: the VSI being configured
3127 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003128 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003129 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003130static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003131{
Alexander Duyck493fb302013-09-28 07:01:44 +00003132 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003133 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3134 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003135
3136 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003137 tx_ring->next = q_vector->tx.ring;
3138 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003139 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003140
3141 rx_ring->q_vector = q_vector;
3142 rx_ring->next = q_vector->rx.ring;
3143 q_vector->rx.ring = rx_ring;
3144 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003145}
3146
3147/**
3148 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3149 * @vsi: the VSI being configured
3150 *
3151 * This function maps descriptor rings to the queue-specific vectors
3152 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3153 * one vector per queue pair, but on a constrained vector budget, we
3154 * group the queue pairs as "efficiently" as possible.
3155 **/
3156static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3157{
3158 int qp_remaining = vsi->num_queue_pairs;
3159 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003160 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003161 int v_start = 0;
3162 int qp_idx = 0;
3163
3164 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3165 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003166 * It is also important to go through all the vectors available to be
3167 * sure that if we don't use all the vectors, that the remaining vectors
3168 * are cleared. This is especially important when decreasing the
3169 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003170 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003171 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003172 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3173
3174 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3175
3176 q_vector->num_ringpairs = num_ringpairs;
3177
3178 q_vector->rx.count = 0;
3179 q_vector->tx.count = 0;
3180 q_vector->rx.ring = NULL;
3181 q_vector->tx.ring = NULL;
3182
3183 while (num_ringpairs--) {
3184 map_vector_to_qp(vsi, v_start, qp_idx);
3185 qp_idx++;
3186 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003187 }
3188 }
3189}
3190
3191/**
3192 * i40e_vsi_request_irq - Request IRQ from the OS
3193 * @vsi: the VSI being configured
3194 * @basename: name for the vector
3195 **/
3196static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3197{
3198 struct i40e_pf *pf = vsi->back;
3199 int err;
3200
3201 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3202 err = i40e_vsi_request_irq_msix(vsi, basename);
3203 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3204 err = request_irq(pf->pdev->irq, i40e_intr, 0,
3205 pf->misc_int_name, pf);
3206 else
3207 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
3208 pf->misc_int_name, pf);
3209
3210 if (err)
3211 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3212
3213 return err;
3214}
3215
3216#ifdef CONFIG_NET_POLL_CONTROLLER
3217/**
3218 * i40e_netpoll - A Polling 'interrupt'handler
3219 * @netdev: network interface device structure
3220 *
3221 * This is used by netconsole to send skbs without having to re-enable
3222 * interrupts. It's not called while the normal interrupt routine is executing.
3223 **/
3224static void i40e_netpoll(struct net_device *netdev)
3225{
3226 struct i40e_netdev_priv *np = netdev_priv(netdev);
3227 struct i40e_vsi *vsi = np->vsi;
3228 struct i40e_pf *pf = vsi->back;
3229 int i;
3230
3231 /* if interface is down do nothing */
3232 if (test_bit(__I40E_DOWN, &vsi->state))
3233 return;
3234
3235 pf->flags |= I40E_FLAG_IN_NETPOLL;
3236 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3237 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003238 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003239 } else {
3240 i40e_intr(pf->pdev->irq, netdev);
3241 }
3242 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3243}
3244#endif
3245
3246/**
Neerav Parikh23527302014-06-03 23:50:15 +00003247 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3248 * @pf: the PF being configured
3249 * @pf_q: the PF queue
3250 * @enable: enable or disable state of the queue
3251 *
3252 * This routine will wait for the given Tx queue of the PF to reach the
3253 * enabled or disabled state.
3254 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3255 * multiple retries; else will return 0 in case of success.
3256 **/
3257static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3258{
3259 int i;
3260 u32 tx_reg;
3261
3262 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3263 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3264 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3265 break;
3266
3267 udelay(10);
3268 }
3269 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3270 return -ETIMEDOUT;
3271
3272 return 0;
3273}
3274
3275/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003276 * i40e_vsi_control_tx - Start or stop a VSI's rings
3277 * @vsi: the VSI being configured
3278 * @enable: start or stop the rings
3279 **/
3280static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3281{
3282 struct i40e_pf *pf = vsi->back;
3283 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003284 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003285 u32 tx_reg;
3286
3287 pf_q = vsi->base_queue;
3288 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499a2014-04-23 04:50:03 +00003289
3290 /* warn the TX unit of coming changes */
3291 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3292 if (!enable)
3293 udelay(10);
3294
Mitch Williams6c5ef622014-02-20 19:29:16 -08003295 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003296 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003297 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3298 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3299 break;
3300 usleep_range(1000, 2000);
3301 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003302 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003303 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003304 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003305
3306 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003307 if (enable) {
3308 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003309 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003310 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003311 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003312 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003313
3314 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3315
3316 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003317 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3318 if (ret) {
3319 dev_info(&pf->pdev->dev,
3320 "%s: VSI seid %d Tx ring %d %sable timeout\n",
3321 __func__, vsi->seid, pf_q,
3322 (enable ? "en" : "dis"));
3323 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003324 }
3325 }
3326
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003327 if (hw->revision_id == 0)
3328 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003329 return ret;
3330}
3331
3332/**
3333 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3334 * @pf: the PF being configured
3335 * @pf_q: the PF queue
3336 * @enable: enable or disable state of the queue
3337 *
3338 * This routine will wait for the given Rx queue of the PF to reach the
3339 * enabled or disabled state.
3340 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3341 * multiple retries; else will return 0 in case of success.
3342 **/
3343static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3344{
3345 int i;
3346 u32 rx_reg;
3347
3348 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3349 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3350 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3351 break;
3352
3353 udelay(10);
3354 }
3355 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3356 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003357
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003358 return 0;
3359}
3360
3361/**
3362 * i40e_vsi_control_rx - Start or stop a VSI's rings
3363 * @vsi: the VSI being configured
3364 * @enable: start or stop the rings
3365 **/
3366static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3367{
3368 struct i40e_pf *pf = vsi->back;
3369 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003370 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003371 u32 rx_reg;
3372
3373 pf_q = vsi->base_queue;
3374 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003375 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003376 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003377 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3378 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3379 break;
3380 usleep_range(1000, 2000);
3381 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003382
Catherine Sullivan7c122002014-03-14 07:32:29 +00003383 /* Skip if the queue is already in the requested state */
3384 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3385 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003386
3387 /* turn on/off the queue */
3388 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003389 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003390 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003391 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003392 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3393
3394 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003395 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3396 if (ret) {
3397 dev_info(&pf->pdev->dev,
3398 "%s: VSI seid %d Rx ring %d %sable timeout\n",
3399 __func__, vsi->seid, pf_q,
3400 (enable ? "en" : "dis"));
3401 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003402 }
3403 }
3404
Neerav Parikh23527302014-06-03 23:50:15 +00003405 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003406}
3407
3408/**
3409 * i40e_vsi_control_rings - Start or stop a VSI's rings
3410 * @vsi: the VSI being configured
3411 * @enable: start or stop the rings
3412 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003413int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003414{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003415 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003416
3417 /* do rx first for enable and last for disable */
3418 if (request) {
3419 ret = i40e_vsi_control_rx(vsi, request);
3420 if (ret)
3421 return ret;
3422 ret = i40e_vsi_control_tx(vsi, request);
3423 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003424 /* Ignore return value, we need to shutdown whatever we can */
3425 i40e_vsi_control_tx(vsi, request);
3426 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003427 }
3428
3429 return ret;
3430}
3431
3432/**
3433 * i40e_vsi_free_irq - Free the irq association with the OS
3434 * @vsi: the VSI being configured
3435 **/
3436static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3437{
3438 struct i40e_pf *pf = vsi->back;
3439 struct i40e_hw *hw = &pf->hw;
3440 int base = vsi->base_vector;
3441 u32 val, qp;
3442 int i;
3443
3444 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3445 if (!vsi->q_vectors)
3446 return;
3447
Shannon Nelson63741842014-04-23 04:50:16 +00003448 if (!vsi->irqs_ready)
3449 return;
3450
3451 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003452 for (i = 0; i < vsi->num_q_vectors; i++) {
3453 u16 vector = i + base;
3454
3455 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003456 if (!vsi->q_vectors[i] ||
3457 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003458 continue;
3459
3460 /* clear the affinity_mask in the IRQ descriptor */
3461 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3462 NULL);
3463 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003464 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003465
3466 /* Tear down the interrupt queue link list
3467 *
3468 * We know that they come in pairs and always
3469 * the Rx first, then the Tx. To clear the
3470 * link list, stick the EOL value into the
3471 * next_q field of the registers.
3472 */
3473 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3474 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3475 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3476 val |= I40E_QUEUE_END_OF_LIST
3477 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3478 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3479
3480 while (qp != I40E_QUEUE_END_OF_LIST) {
3481 u32 next;
3482
3483 val = rd32(hw, I40E_QINT_RQCTL(qp));
3484
3485 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3486 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3487 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3488 I40E_QINT_RQCTL_INTEVENT_MASK);
3489
3490 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3491 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3492
3493 wr32(hw, I40E_QINT_RQCTL(qp), val);
3494
3495 val = rd32(hw, I40E_QINT_TQCTL(qp));
3496
3497 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3498 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3499
3500 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3501 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3502 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3503 I40E_QINT_TQCTL_INTEVENT_MASK);
3504
3505 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3506 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3507
3508 wr32(hw, I40E_QINT_TQCTL(qp), val);
3509 qp = next;
3510 }
3511 }
3512 } else {
3513 free_irq(pf->pdev->irq, pf);
3514
3515 val = rd32(hw, I40E_PFINT_LNKLST0);
3516 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3517 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3518 val |= I40E_QUEUE_END_OF_LIST
3519 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3520 wr32(hw, I40E_PFINT_LNKLST0, val);
3521
3522 val = rd32(hw, I40E_QINT_RQCTL(qp));
3523 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3524 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3525 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3526 I40E_QINT_RQCTL_INTEVENT_MASK);
3527
3528 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3529 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3530
3531 wr32(hw, I40E_QINT_RQCTL(qp), val);
3532
3533 val = rd32(hw, I40E_QINT_TQCTL(qp));
3534
3535 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3536 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3537 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3538 I40E_QINT_TQCTL_INTEVENT_MASK);
3539
3540 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3541 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3542
3543 wr32(hw, I40E_QINT_TQCTL(qp), val);
3544 }
3545}
3546
3547/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003548 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3549 * @vsi: the VSI being configured
3550 * @v_idx: Index of vector to be freed
3551 *
3552 * This function frees the memory allocated to the q_vector. In addition if
3553 * NAPI is enabled it will delete any references to the NAPI struct prior
3554 * to freeing the q_vector.
3555 **/
3556static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3557{
3558 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003559 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003560
3561 if (!q_vector)
3562 return;
3563
3564 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003565 i40e_for_each_ring(ring, q_vector->tx)
3566 ring->q_vector = NULL;
3567
3568 i40e_for_each_ring(ring, q_vector->rx)
3569 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003570
3571 /* only VSI w/ an associated netdev is set up w/ NAPI */
3572 if (vsi->netdev)
3573 netif_napi_del(&q_vector->napi);
3574
3575 vsi->q_vectors[v_idx] = NULL;
3576
3577 kfree_rcu(q_vector, rcu);
3578}
3579
3580/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003581 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3582 * @vsi: the VSI being un-configured
3583 *
3584 * This frees the memory allocated to the q_vectors and
3585 * deletes references to the NAPI struct.
3586 **/
3587static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3588{
3589 int v_idx;
3590
Alexander Duyck493fb302013-09-28 07:01:44 +00003591 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3592 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003593}
3594
3595/**
3596 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3597 * @pf: board private structure
3598 **/
3599static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3600{
3601 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3602 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3603 pci_disable_msix(pf->pdev);
3604 kfree(pf->msix_entries);
3605 pf->msix_entries = NULL;
3606 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3607 pci_disable_msi(pf->pdev);
3608 }
3609 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3610}
3611
3612/**
3613 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3614 * @pf: board private structure
3615 *
3616 * We go through and clear interrupt specific resources and reset the structure
3617 * to pre-load conditions
3618 **/
3619static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3620{
3621 int i;
3622
3623 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00003624 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003625 if (pf->vsi[i])
3626 i40e_vsi_free_q_vectors(pf->vsi[i]);
3627 i40e_reset_interrupt_capability(pf);
3628}
3629
3630/**
3631 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3632 * @vsi: the VSI being configured
3633 **/
3634static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3635{
3636 int q_idx;
3637
3638 if (!vsi->netdev)
3639 return;
3640
3641 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003642 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003643}
3644
3645/**
3646 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3647 * @vsi: the VSI being configured
3648 **/
3649static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3650{
3651 int q_idx;
3652
3653 if (!vsi->netdev)
3654 return;
3655
3656 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003657 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003658}
3659
3660/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003661 * i40e_vsi_close - Shut down a VSI
3662 * @vsi: the vsi to be quelled
3663 **/
3664static void i40e_vsi_close(struct i40e_vsi *vsi)
3665{
3666 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
3667 i40e_down(vsi);
3668 i40e_vsi_free_irq(vsi);
3669 i40e_vsi_free_tx_resources(vsi);
3670 i40e_vsi_free_rx_resources(vsi);
3671}
3672
3673/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003674 * i40e_quiesce_vsi - Pause a given VSI
3675 * @vsi: the VSI being paused
3676 **/
3677static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3678{
3679 if (test_bit(__I40E_DOWN, &vsi->state))
3680 return;
3681
3682 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3683 if (vsi->netdev && netif_running(vsi->netdev)) {
3684 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3685 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003686 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003687 }
3688}
3689
3690/**
3691 * i40e_unquiesce_vsi - Resume a given VSI
3692 * @vsi: the VSI being resumed
3693 **/
3694static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3695{
3696 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3697 return;
3698
3699 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3700 if (vsi->netdev && netif_running(vsi->netdev))
3701 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3702 else
Shannon Nelson8276f752014-03-14 07:32:27 +00003703 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003704}
3705
3706/**
3707 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3708 * @pf: the PF
3709 **/
3710static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3711{
3712 int v;
3713
Mitch Williams505682c2014-05-20 08:01:37 +00003714 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003715 if (pf->vsi[v])
3716 i40e_quiesce_vsi(pf->vsi[v]);
3717 }
3718}
3719
3720/**
3721 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3722 * @pf: the PF
3723 **/
3724static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3725{
3726 int v;
3727
Mitch Williams505682c2014-05-20 08:01:37 +00003728 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003729 if (pf->vsi[v])
3730 i40e_unquiesce_vsi(pf->vsi[v]);
3731 }
3732}
3733
3734/**
3735 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3736 * @dcbcfg: the corresponding DCBx configuration structure
3737 *
3738 * Return the number of TCs from given DCBx configuration
3739 **/
3740static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3741{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003742 u8 num_tc = 0;
3743 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003744
3745 /* Scan the ETS Config Priority Table to find
3746 * traffic class enabled for a given priority
3747 * and use the traffic class index to get the
3748 * number of traffic classes enabled
3749 */
3750 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3751 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3752 num_tc = dcbcfg->etscfg.prioritytable[i];
3753 }
3754
3755 /* Traffic class index starts from zero so
3756 * increment to return the actual count
3757 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003758 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003759}
3760
3761/**
3762 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3763 * @dcbcfg: the corresponding DCBx configuration structure
3764 *
3765 * Query the current DCB configuration and return the number of
3766 * traffic classes enabled from the given DCBX config
3767 **/
3768static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3769{
3770 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3771 u8 enabled_tc = 1;
3772 u8 i;
3773
3774 for (i = 0; i < num_tc; i++)
3775 enabled_tc |= 1 << i;
3776
3777 return enabled_tc;
3778}
3779
3780/**
3781 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3782 * @pf: PF being queried
3783 *
3784 * Return number of traffic classes enabled for the given PF
3785 **/
3786static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3787{
3788 struct i40e_hw *hw = &pf->hw;
3789 u8 i, enabled_tc;
3790 u8 num_tc = 0;
3791 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3792
3793 /* If DCB is not enabled then always in single TC */
3794 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3795 return 1;
3796
3797 /* MFP mode return count of enabled TCs for this PF */
3798 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3799 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3800 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3801 if (enabled_tc & (1 << i))
3802 num_tc++;
3803 }
3804 return num_tc;
3805 }
3806
3807 /* SFP mode will be enabled for all TCs on port */
3808 return i40e_dcb_get_num_tc(dcbcfg);
3809}
3810
3811/**
3812 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3813 * @pf: PF being queried
3814 *
3815 * Return a bitmap for first enabled traffic class for this PF.
3816 **/
3817static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3818{
3819 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3820 u8 i = 0;
3821
3822 if (!enabled_tc)
3823 return 0x1; /* TC0 */
3824
3825 /* Find the first enabled TC */
3826 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3827 if (enabled_tc & (1 << i))
3828 break;
3829 }
3830
3831 return 1 << i;
3832}
3833
3834/**
3835 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3836 * @pf: PF being queried
3837 *
3838 * Return a bitmap for enabled traffic classes for this PF.
3839 **/
3840static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3841{
3842 /* If DCB is not enabled for this PF then just return default TC */
3843 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3844 return i40e_pf_get_default_tc(pf);
3845
3846 /* MFP mode will have enabled TCs set by FW */
3847 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3848 return pf->hw.func_caps.enabled_tcmap;
3849
3850 /* SFP mode we want PF to be enabled for all TCs */
3851 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3852}
3853
3854/**
3855 * i40e_vsi_get_bw_info - Query VSI BW Information
3856 * @vsi: the VSI being queried
3857 *
3858 * Returns 0 on success, negative value on failure
3859 **/
3860static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3861{
3862 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3863 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3864 struct i40e_pf *pf = vsi->back;
3865 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003866 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003867 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003868 int i;
3869
3870 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003871 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3872 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003873 dev_info(&pf->pdev->dev,
3874 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003875 aq_ret, pf->hw.aq.asq_last_status);
3876 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003877 }
3878
3879 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003880 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
Jesse Brandeburg6838b532014-01-14 00:49:52 -08003881 NULL);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003882 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003883 dev_info(&pf->pdev->dev,
3884 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003885 aq_ret, pf->hw.aq.asq_last_status);
3886 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003887 }
3888
3889 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3890 dev_info(&pf->pdev->dev,
3891 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3892 bw_config.tc_valid_bits,
3893 bw_ets_config.tc_valid_bits);
3894 /* Still continuing */
3895 }
3896
3897 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3898 vsi->bw_max_quanta = bw_config.max_bw;
3899 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3900 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3901 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3902 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3903 vsi->bw_ets_limit_credits[i] =
3904 le16_to_cpu(bw_ets_config.credits[i]);
3905 /* 3 bits out of 4 for each TC */
3906 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3907 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003908
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003909 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003910}
3911
3912/**
3913 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3914 * @vsi: the VSI being configured
3915 * @enabled_tc: TC bitmap
3916 * @bw_credits: BW shared credits per TC
3917 *
3918 * Returns 0 on success, negative value on failure
3919 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003920static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003921 u8 *bw_share)
3922{
3923 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003924 i40e_status aq_ret;
3925 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003926
3927 bw_data.tc_valid_bits = enabled_tc;
3928 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3929 bw_data.tc_bw_credits[i] = bw_share[i];
3930
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003931 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3932 NULL);
3933 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003934 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00003935 "AQ command Config VSI BW allocation per TC failed = %d\n",
3936 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003937 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003938 }
3939
3940 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3941 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3942
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003943 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003944}
3945
3946/**
3947 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3948 * @vsi: the VSI being configured
3949 * @enabled_tc: TC map to be enabled
3950 *
3951 **/
3952static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3953{
3954 struct net_device *netdev = vsi->netdev;
3955 struct i40e_pf *pf = vsi->back;
3956 struct i40e_hw *hw = &pf->hw;
3957 u8 netdev_tc = 0;
3958 int i;
3959 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3960
3961 if (!netdev)
3962 return;
3963
3964 if (!enabled_tc) {
3965 netdev_reset_tc(netdev);
3966 return;
3967 }
3968
3969 /* Set up actual enabled TCs on the VSI */
3970 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3971 return;
3972
3973 /* set per TC queues for the VSI */
3974 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3975 /* Only set TC queues for enabled tcs
3976 *
3977 * e.g. For a VSI that has TC0 and TC3 enabled the
3978 * enabled_tc bitmap would be 0x00001001; the driver
3979 * will set the numtc for netdev as 2 that will be
3980 * referenced by the netdev layer as TC 0 and 1.
3981 */
3982 if (vsi->tc_config.enabled_tc & (1 << i))
3983 netdev_set_tc_queue(netdev,
3984 vsi->tc_config.tc_info[i].netdev_tc,
3985 vsi->tc_config.tc_info[i].qcount,
3986 vsi->tc_config.tc_info[i].qoffset);
3987 }
3988
3989 /* Assign UP2TC map for the VSI */
3990 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3991 /* Get the actual TC# for the UP */
3992 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3993 /* Get the mapped netdev TC# for the UP */
3994 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3995 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3996 }
3997}
3998
3999/**
4000 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4001 * @vsi: the VSI being configured
4002 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4003 **/
4004static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4005 struct i40e_vsi_context *ctxt)
4006{
4007 /* copy just the sections touched not the entire info
4008 * since not all sections are valid as returned by
4009 * update vsi params
4010 */
4011 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4012 memcpy(&vsi->info.queue_mapping,
4013 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4014 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4015 sizeof(vsi->info.tc_mapping));
4016}
4017
4018/**
4019 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4020 * @vsi: VSI to be configured
4021 * @enabled_tc: TC bitmap
4022 *
4023 * This configures a particular VSI for TCs that are mapped to the
4024 * given TC bitmap. It uses default bandwidth share for TCs across
4025 * VSIs to configure TC for a particular VSI.
4026 *
4027 * NOTE:
4028 * It is expected that the VSI queues have been quisced before calling
4029 * this function.
4030 **/
4031static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4032{
4033 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4034 struct i40e_vsi_context ctxt;
4035 int ret = 0;
4036 int i;
4037
4038 /* Check if enabled_tc is same as existing or new TCs */
4039 if (vsi->tc_config.enabled_tc == enabled_tc)
4040 return ret;
4041
4042 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4043 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4044 if (enabled_tc & (1 << i))
4045 bw_share[i] = 1;
4046 }
4047
4048 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4049 if (ret) {
4050 dev_info(&vsi->back->pdev->dev,
4051 "Failed configuring TC map %d for VSI %d\n",
4052 enabled_tc, vsi->seid);
4053 goto out;
4054 }
4055
4056 /* Update Queue Pairs Mapping for currently enabled UPs */
4057 ctxt.seid = vsi->seid;
4058 ctxt.pf_num = vsi->back->hw.pf_id;
4059 ctxt.vf_num = 0;
4060 ctxt.uplink_seid = vsi->uplink_seid;
4061 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
4062 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4063
4064 /* Update the VSI after updating the VSI queue-mapping information */
4065 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4066 if (ret) {
4067 dev_info(&vsi->back->pdev->dev,
4068 "update vsi failed, aq_err=%d\n",
4069 vsi->back->hw.aq.asq_last_status);
4070 goto out;
4071 }
4072 /* update the local VSI info with updated queue map */
4073 i40e_vsi_update_queue_map(vsi, &ctxt);
4074 vsi->info.valid_sections = 0;
4075
4076 /* Update current VSI BW information */
4077 ret = i40e_vsi_get_bw_info(vsi);
4078 if (ret) {
4079 dev_info(&vsi->back->pdev->dev,
4080 "Failed updating vsi bw info, aq_err=%d\n",
4081 vsi->back->hw.aq.asq_last_status);
4082 goto out;
4083 }
4084
4085 /* Update the netdev TC setup */
4086 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4087out:
4088 return ret;
4089}
4090
4091/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004092 * i40e_veb_config_tc - Configure TCs for given VEB
4093 * @veb: given VEB
4094 * @enabled_tc: TC bitmap
4095 *
4096 * Configures given TC bitmap for VEB (switching) element
4097 **/
4098int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4099{
4100 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4101 struct i40e_pf *pf = veb->pf;
4102 int ret = 0;
4103 int i;
4104
4105 /* No TCs or already enabled TCs just return */
4106 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4107 return ret;
4108
4109 bw_data.tc_valid_bits = enabled_tc;
4110 /* bw_data.absolute_credits is not set (relative) */
4111
4112 /* Enable ETS TCs with equal BW Share for now */
4113 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4114 if (enabled_tc & (1 << i))
4115 bw_data.tc_bw_share_credits[i] = 1;
4116 }
4117
4118 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4119 &bw_data, NULL);
4120 if (ret) {
4121 dev_info(&pf->pdev->dev,
4122 "veb bw config failed, aq_err=%d\n",
4123 pf->hw.aq.asq_last_status);
4124 goto out;
4125 }
4126
4127 /* Update the BW information */
4128 ret = i40e_veb_get_bw_info(veb);
4129 if (ret) {
4130 dev_info(&pf->pdev->dev,
4131 "Failed getting veb bw config, aq_err=%d\n",
4132 pf->hw.aq.asq_last_status);
4133 }
4134
4135out:
4136 return ret;
4137}
4138
4139#ifdef CONFIG_I40E_DCB
4140/**
4141 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4142 * @pf: PF struct
4143 *
4144 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4145 * the caller would've quiesce all the VSIs before calling
4146 * this function
4147 **/
4148static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4149{
4150 u8 tc_map = 0;
4151 int ret;
4152 u8 v;
4153
4154 /* Enable the TCs available on PF to all VEBs */
4155 tc_map = i40e_pf_get_tc_map(pf);
4156 for (v = 0; v < I40E_MAX_VEB; v++) {
4157 if (!pf->veb[v])
4158 continue;
4159 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4160 if (ret) {
4161 dev_info(&pf->pdev->dev,
4162 "Failed configuring TC for VEB seid=%d\n",
4163 pf->veb[v]->seid);
4164 /* Will try to configure as many components */
4165 }
4166 }
4167
4168 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004169 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004170 if (!pf->vsi[v])
4171 continue;
4172
4173 /* - Enable all TCs for the LAN VSI
4174 * - For all others keep them at TC0 for now
4175 */
4176 if (v == pf->lan_vsi)
4177 tc_map = i40e_pf_get_tc_map(pf);
4178 else
4179 tc_map = i40e_pf_get_default_tc(pf);
4180
4181 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4182 if (ret) {
4183 dev_info(&pf->pdev->dev,
4184 "Failed configuring TC for VSI seid=%d\n",
4185 pf->vsi[v]->seid);
4186 /* Will try to configure as many components */
4187 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004188 /* Re-configure VSI vectors based on updated TC map */
4189 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004190 if (pf->vsi[v]->netdev)
4191 i40e_dcbnl_set_all(pf->vsi[v]);
4192 }
4193 }
4194}
4195
4196/**
4197 * i40e_init_pf_dcb - Initialize DCB configuration
4198 * @pf: PF being configured
4199 *
4200 * Query the current DCB configuration and cache it
4201 * in the hardware structure
4202 **/
4203static int i40e_init_pf_dcb(struct i40e_pf *pf)
4204{
4205 struct i40e_hw *hw = &pf->hw;
4206 int err = 0;
4207
4208 if (pf->hw.func_caps.npar_enable)
4209 goto out;
4210
4211 /* Get the initial DCB configuration */
4212 err = i40e_init_dcb(hw);
4213 if (!err) {
4214 /* Device/Function is not DCBX capable */
4215 if ((!hw->func_caps.dcb) ||
4216 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4217 dev_info(&pf->pdev->dev,
4218 "DCBX offload is not supported or is disabled for this PF.\n");
4219
4220 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4221 goto out;
4222
4223 } else {
4224 /* When status is not DISABLED then DCBX in FW */
4225 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4226 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004227
4228 pf->flags |= I40E_FLAG_DCB_CAPABLE;
4229 /* Enable DCB tagging only when more than one TC */
4230 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
4231 pf->flags |= I40E_FLAG_DCB_ENABLED;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004232 }
Neerav Parikh014269f2014-04-01 07:11:48 +00004233 } else {
4234 dev_info(&pf->pdev->dev, "AQ Querying DCB configuration failed: %d\n",
4235 pf->hw.aq.asq_last_status);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004236 }
4237
4238out:
4239 return err;
4240}
4241#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004242#define SPEED_SIZE 14
4243#define FC_SIZE 8
4244/**
4245 * i40e_print_link_message - print link up or down
4246 * @vsi: the VSI for which link needs a message
4247 */
4248static void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
4249{
4250 char speed[SPEED_SIZE] = "Unknown";
4251 char fc[FC_SIZE] = "RX/TX";
4252
4253 if (!isup) {
4254 netdev_info(vsi->netdev, "NIC Link is Down\n");
4255 return;
4256 }
4257
4258 switch (vsi->back->hw.phy.link_info.link_speed) {
4259 case I40E_LINK_SPEED_40GB:
4260 strncpy(speed, "40 Gbps", SPEED_SIZE);
4261 break;
4262 case I40E_LINK_SPEED_10GB:
4263 strncpy(speed, "10 Gbps", SPEED_SIZE);
4264 break;
4265 case I40E_LINK_SPEED_1GB:
4266 strncpy(speed, "1000 Mbps", SPEED_SIZE);
4267 break;
4268 default:
4269 break;
4270 }
4271
4272 switch (vsi->back->hw.fc.current_mode) {
4273 case I40E_FC_FULL:
4274 strncpy(fc, "RX/TX", FC_SIZE);
4275 break;
4276 case I40E_FC_TX_PAUSE:
4277 strncpy(fc, "TX", FC_SIZE);
4278 break;
4279 case I40E_FC_RX_PAUSE:
4280 strncpy(fc, "RX", FC_SIZE);
4281 break;
4282 default:
4283 strncpy(fc, "None", FC_SIZE);
4284 break;
4285 }
4286
4287 netdev_info(vsi->netdev, "NIC Link is Up %s Full Duplex, Flow Control: %s\n",
4288 speed, fc);
4289}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004290
4291/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004292 * i40e_up_complete - Finish the last steps of bringing up a connection
4293 * @vsi: the VSI being configured
4294 **/
4295static int i40e_up_complete(struct i40e_vsi *vsi)
4296{
4297 struct i40e_pf *pf = vsi->back;
4298 int err;
4299
4300 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4301 i40e_vsi_configure_msix(vsi);
4302 else
4303 i40e_configure_msi_and_legacy(vsi);
4304
4305 /* start rings */
4306 err = i40e_vsi_control_rings(vsi, true);
4307 if (err)
4308 return err;
4309
4310 clear_bit(__I40E_DOWN, &vsi->state);
4311 i40e_napi_enable_all(vsi);
4312 i40e_vsi_enable_irq(vsi);
4313
4314 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4315 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004316 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004317 netif_tx_start_all_queues(vsi->netdev);
4318 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004319 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004320 i40e_print_link_message(vsi, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004321 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004322
4323 /* replay FDIR SB filters */
4324 if (vsi->type == I40E_VSI_FDIR)
4325 i40e_fdir_filter_restore(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004326 i40e_service_event_schedule(pf);
4327
4328 return 0;
4329}
4330
4331/**
4332 * i40e_vsi_reinit_locked - Reset the VSI
4333 * @vsi: the VSI being configured
4334 *
4335 * Rebuild the ring structs after some configuration
4336 * has changed, e.g. MTU size.
4337 **/
4338static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4339{
4340 struct i40e_pf *pf = vsi->back;
4341
4342 WARN_ON(in_interrupt());
4343 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4344 usleep_range(1000, 2000);
4345 i40e_down(vsi);
4346
4347 /* Give a VF some time to respond to the reset. The
4348 * two second wait is based upon the watchdog cycle in
4349 * the VF driver.
4350 */
4351 if (vsi->type == I40E_VSI_SRIOV)
4352 msleep(2000);
4353 i40e_up(vsi);
4354 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
4355}
4356
4357/**
4358 * i40e_up - Bring the connection back up after being down
4359 * @vsi: the VSI being configured
4360 **/
4361int i40e_up(struct i40e_vsi *vsi)
4362{
4363 int err;
4364
4365 err = i40e_vsi_configure(vsi);
4366 if (!err)
4367 err = i40e_up_complete(vsi);
4368
4369 return err;
4370}
4371
4372/**
4373 * i40e_down - Shutdown the connection processing
4374 * @vsi: the VSI being stopped
4375 **/
4376void i40e_down(struct i40e_vsi *vsi)
4377{
4378 int i;
4379
4380 /* It is assumed that the caller of this function
4381 * sets the vsi->state __I40E_DOWN bit.
4382 */
4383 if (vsi->netdev) {
4384 netif_carrier_off(vsi->netdev);
4385 netif_tx_disable(vsi->netdev);
4386 }
4387 i40e_vsi_disable_irq(vsi);
4388 i40e_vsi_control_rings(vsi, false);
4389 i40e_napi_disable_all(vsi);
4390
4391 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004392 i40e_clean_tx_ring(vsi->tx_rings[i]);
4393 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004394 }
4395}
4396
4397/**
4398 * i40e_setup_tc - configure multiple traffic classes
4399 * @netdev: net device to configure
4400 * @tc: number of traffic classes to enable
4401 **/
4402static int i40e_setup_tc(struct net_device *netdev, u8 tc)
4403{
4404 struct i40e_netdev_priv *np = netdev_priv(netdev);
4405 struct i40e_vsi *vsi = np->vsi;
4406 struct i40e_pf *pf = vsi->back;
4407 u8 enabled_tc = 0;
4408 int ret = -EINVAL;
4409 int i;
4410
4411 /* Check if DCB enabled to continue */
4412 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
4413 netdev_info(netdev, "DCB is not enabled for adapter\n");
4414 goto exit;
4415 }
4416
4417 /* Check if MFP enabled */
4418 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4419 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
4420 goto exit;
4421 }
4422
4423 /* Check whether tc count is within enabled limit */
4424 if (tc > i40e_pf_get_num_tc(pf)) {
4425 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
4426 goto exit;
4427 }
4428
4429 /* Generate TC map for number of tc requested */
4430 for (i = 0; i < tc; i++)
4431 enabled_tc |= (1 << i);
4432
4433 /* Requesting same TC configuration as already enabled */
4434 if (enabled_tc == vsi->tc_config.enabled_tc)
4435 return 0;
4436
4437 /* Quiesce VSI queues */
4438 i40e_quiesce_vsi(vsi);
4439
4440 /* Configure VSI for enabled TCs */
4441 ret = i40e_vsi_config_tc(vsi, enabled_tc);
4442 if (ret) {
4443 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
4444 vsi->seid);
4445 goto exit;
4446 }
4447
4448 /* Unquiesce VSI */
4449 i40e_unquiesce_vsi(vsi);
4450
4451exit:
4452 return ret;
4453}
4454
4455/**
4456 * i40e_open - Called when a network interface is made active
4457 * @netdev: network interface device structure
4458 *
4459 * The open entry point is called when a network interface is made
4460 * active by the system (IFF_UP). At this point all resources needed
4461 * for transmit and receive operations are allocated, the interrupt
4462 * handler is registered with the OS, the netdev watchdog subtask is
4463 * enabled, and the stack is notified that the interface is ready.
4464 *
4465 * Returns 0 on success, negative value on failure
4466 **/
4467static int i40e_open(struct net_device *netdev)
4468{
4469 struct i40e_netdev_priv *np = netdev_priv(netdev);
4470 struct i40e_vsi *vsi = np->vsi;
4471 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004472 int err;
4473
Shannon Nelson4eb3f762014-03-06 08:59:58 +00004474 /* disallow open during test or if eeprom is broken */
4475 if (test_bit(__I40E_TESTING, &pf->state) ||
4476 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004477 return -EBUSY;
4478
4479 netif_carrier_off(netdev);
4480
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004481 err = i40e_vsi_open(vsi);
4482 if (err)
4483 return err;
4484
Jesse Brandeburg059dab62014-04-01 09:07:20 +00004485 /* configure global TSO hardware offload settings */
4486 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
4487 TCP_FLAG_FIN) >> 16);
4488 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
4489 TCP_FLAG_FIN |
4490 TCP_FLAG_CWR) >> 16);
4491 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
4492
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004493#ifdef CONFIG_I40E_VXLAN
4494 vxlan_get_rx_port(netdev);
4495#endif
4496
4497 return 0;
4498}
4499
4500/**
4501 * i40e_vsi_open -
4502 * @vsi: the VSI to open
4503 *
4504 * Finish initialization of the VSI.
4505 *
4506 * Returns 0 on success, negative value on failure
4507 **/
4508int i40e_vsi_open(struct i40e_vsi *vsi)
4509{
4510 struct i40e_pf *pf = vsi->back;
4511 char int_name[IFNAMSIZ];
4512 int err;
4513
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004514 /* allocate descriptors */
4515 err = i40e_vsi_setup_tx_resources(vsi);
4516 if (err)
4517 goto err_setup_tx;
4518 err = i40e_vsi_setup_rx_resources(vsi);
4519 if (err)
4520 goto err_setup_rx;
4521
4522 err = i40e_vsi_configure(vsi);
4523 if (err)
4524 goto err_setup_rx;
4525
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004526 if (vsi->netdev) {
4527 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4528 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
4529 err = i40e_vsi_request_irq(vsi, int_name);
4530 if (err)
4531 goto err_setup_rx;
4532
4533 /* Notify the stack of the actual queue counts. */
4534 err = netif_set_real_num_tx_queues(vsi->netdev,
4535 vsi->num_queue_pairs);
4536 if (err)
4537 goto err_set_queues;
4538
4539 err = netif_set_real_num_rx_queues(vsi->netdev,
4540 vsi->num_queue_pairs);
4541 if (err)
4542 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00004543
4544 } else if (vsi->type == I40E_VSI_FDIR) {
4545 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4546 dev_driver_string(&pf->pdev->dev));
4547 err = i40e_vsi_request_irq(vsi, int_name);
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004548 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00004549 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004550 goto err_setup_rx;
4551 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004552
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004553 err = i40e_up_complete(vsi);
4554 if (err)
4555 goto err_up_complete;
4556
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004557 return 0;
4558
4559err_up_complete:
4560 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004561err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004562 i40e_vsi_free_irq(vsi);
4563err_setup_rx:
4564 i40e_vsi_free_rx_resources(vsi);
4565err_setup_tx:
4566 i40e_vsi_free_tx_resources(vsi);
4567 if (vsi == pf->vsi[pf->lan_vsi])
4568 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4569
4570 return err;
4571}
4572
4573/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00004574 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
4575 * @pf: Pointer to pf
4576 *
4577 * This function destroys the hlist where all the Flow Director
4578 * filters were saved.
4579 **/
4580static void i40e_fdir_filter_exit(struct i40e_pf *pf)
4581{
4582 struct i40e_fdir_filter *filter;
4583 struct hlist_node *node2;
4584
4585 hlist_for_each_entry_safe(filter, node2,
4586 &pf->fdir_filter_list, fdir_node) {
4587 hlist_del(&filter->fdir_node);
4588 kfree(filter);
4589 }
4590 pf->fdir_pf_active_filters = 0;
4591}
4592
4593/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004594 * i40e_close - Disables a network interface
4595 * @netdev: network interface device structure
4596 *
4597 * The close entry point is called when an interface is de-activated
4598 * by the OS. The hardware is still under the driver's control, but
4599 * this netdev interface is disabled.
4600 *
4601 * Returns 0, this is not allowed to fail
4602 **/
4603static int i40e_close(struct net_device *netdev)
4604{
4605 struct i40e_netdev_priv *np = netdev_priv(netdev);
4606 struct i40e_vsi *vsi = np->vsi;
4607
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004608 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004609
4610 return 0;
4611}
4612
4613/**
4614 * i40e_do_reset - Start a PF or Core Reset sequence
4615 * @pf: board private structure
4616 * @reset_flags: which reset is requested
4617 *
4618 * The essential difference in resets is that the PF Reset
4619 * doesn't clear the packet buffers, doesn't reset the PE
4620 * firmware, and doesn't bother the other PFs on the chip.
4621 **/
4622void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4623{
4624 u32 val;
4625
4626 WARN_ON(in_interrupt());
4627
Mitch Williams263fc482014-04-23 04:50:11 +00004628 if (i40e_check_asq_alive(&pf->hw))
4629 i40e_vc_notify_reset(pf);
4630
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004631 /* do the biggest reset indicated */
4632 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4633
4634 /* Request a Global Reset
4635 *
4636 * This will start the chip's countdown to the actual full
4637 * chip reset event, and a warning interrupt to be sent
4638 * to all PFs, including the requestor. Our handler
4639 * for the warning interrupt will deal with the shutdown
4640 * and recovery of the switch setup.
4641 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004642 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004643 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4644 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4645 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4646
4647 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4648
4649 /* Request a Core Reset
4650 *
4651 * Same as Global Reset, except does *not* include the MAC/PHY
4652 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004653 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004654 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4655 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4656 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4657 i40e_flush(&pf->hw);
4658
Shannon Nelson7823fe32013-11-16 10:00:45 +00004659 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4660
4661 /* Request a Firmware Reset
4662 *
4663 * Same as Global reset, plus restarting the
4664 * embedded firmware engine.
4665 */
4666 /* enable EMP Reset */
4667 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4668 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4669 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4670
4671 /* force the reset */
4672 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4673 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4674 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4675 i40e_flush(&pf->hw);
4676
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004677 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4678
4679 /* Request a PF Reset
4680 *
4681 * Resets only the PF-specific registers
4682 *
4683 * This goes directly to the tear-down and rebuild of
4684 * the switch, since we need to do all the recovery as
4685 * for the Core Reset.
4686 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004687 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004688 i40e_handle_reset_warning(pf);
4689
4690 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4691 int v;
4692
4693 /* Find the VSI(s) that requested a re-init */
4694 dev_info(&pf->pdev->dev,
4695 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00004696 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004697 struct i40e_vsi *vsi = pf->vsi[v];
4698 if (vsi != NULL &&
4699 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4700 i40e_vsi_reinit_locked(pf->vsi[v]);
4701 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4702 }
4703 }
4704
4705 /* no further action needed, so return now */
4706 return;
Neerav Parikhb5d06f02014-06-03 23:50:17 +00004707 } else if (reset_flags & (1 << __I40E_DOWN_REQUESTED)) {
4708 int v;
4709
4710 /* Find the VSI(s) that needs to be brought down */
4711 dev_info(&pf->pdev->dev, "VSI down requested\n");
4712 for (v = 0; v < pf->num_alloc_vsi; v++) {
4713 struct i40e_vsi *vsi = pf->vsi[v];
4714 if (vsi != NULL &&
4715 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
4716 set_bit(__I40E_DOWN, &vsi->state);
4717 i40e_down(vsi);
4718 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
4719 }
4720 }
4721
4722 /* no further action needed, so return now */
4723 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004724 } else {
4725 dev_info(&pf->pdev->dev,
4726 "bad reset request 0x%08x\n", reset_flags);
4727 return;
4728 }
4729}
4730
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004731#ifdef CONFIG_I40E_DCB
4732/**
4733 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
4734 * @pf: board private structure
4735 * @old_cfg: current DCB config
4736 * @new_cfg: new DCB config
4737 **/
4738bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
4739 struct i40e_dcbx_config *old_cfg,
4740 struct i40e_dcbx_config *new_cfg)
4741{
4742 bool need_reconfig = false;
4743
4744 /* Check if ETS configuration has changed */
4745 if (memcmp(&new_cfg->etscfg,
4746 &old_cfg->etscfg,
4747 sizeof(new_cfg->etscfg))) {
4748 /* If Priority Table has changed reconfig is needed */
4749 if (memcmp(&new_cfg->etscfg.prioritytable,
4750 &old_cfg->etscfg.prioritytable,
4751 sizeof(new_cfg->etscfg.prioritytable))) {
4752 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004753 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004754 }
4755
4756 if (memcmp(&new_cfg->etscfg.tcbwtable,
4757 &old_cfg->etscfg.tcbwtable,
4758 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004759 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004760
4761 if (memcmp(&new_cfg->etscfg.tsatable,
4762 &old_cfg->etscfg.tsatable,
4763 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004764 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004765 }
4766
4767 /* Check if PFC configuration has changed */
4768 if (memcmp(&new_cfg->pfc,
4769 &old_cfg->pfc,
4770 sizeof(new_cfg->pfc))) {
4771 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004772 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004773 }
4774
4775 /* Check if APP Table has changed */
4776 if (memcmp(&new_cfg->app,
4777 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05004778 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004779 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004780 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05004781 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004782
4783 return need_reconfig;
4784}
4785
4786/**
4787 * i40e_handle_lldp_event - Handle LLDP Change MIB event
4788 * @pf: board private structure
4789 * @e: event info posted on ARQ
4790 **/
4791static int i40e_handle_lldp_event(struct i40e_pf *pf,
4792 struct i40e_arq_event_info *e)
4793{
4794 struct i40e_aqc_lldp_get_mib *mib =
4795 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
4796 struct i40e_hw *hw = &pf->hw;
4797 struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
4798 struct i40e_dcbx_config tmp_dcbx_cfg;
4799 bool need_reconfig = false;
4800 int ret = 0;
4801 u8 type;
4802
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004803 /* Not DCB capable or capability disabled */
4804 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
4805 return ret;
4806
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004807 /* Ignore if event is not for Nearest Bridge */
4808 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
4809 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
4810 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
4811 return ret;
4812
4813 /* Check MIB Type and return if event for Remote MIB update */
4814 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
4815 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
4816 /* Update the remote cached instance and return */
4817 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
4818 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
4819 &hw->remote_dcbx_config);
4820 goto exit;
4821 }
4822
4823 /* Convert/store the DCBX data from LLDPDU temporarily */
4824 memset(&tmp_dcbx_cfg, 0, sizeof(tmp_dcbx_cfg));
4825 ret = i40e_lldp_to_dcb_config(e->msg_buf, &tmp_dcbx_cfg);
4826 if (ret) {
4827 /* Error in LLDPDU parsing return */
4828 dev_info(&pf->pdev->dev, "Failed parsing LLDPDU from event buffer\n");
4829 goto exit;
4830 }
4831
4832 /* No change detected in DCBX configs */
4833 if (!memcmp(&tmp_dcbx_cfg, dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004834 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004835 goto exit;
4836 }
4837
4838 need_reconfig = i40e_dcb_need_reconfig(pf, dcbx_cfg, &tmp_dcbx_cfg);
4839
4840 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg);
4841
4842 /* Overwrite the new configuration */
4843 *dcbx_cfg = tmp_dcbx_cfg;
4844
4845 if (!need_reconfig)
4846 goto exit;
4847
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004848 /* Enable DCB tagging only when more than one TC */
4849 if (i40e_dcb_get_num_tc(dcbx_cfg) > 1)
4850 pf->flags |= I40E_FLAG_DCB_ENABLED;
4851 else
4852 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
4853
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004854 /* Reconfiguration needed quiesce all VSIs */
4855 i40e_pf_quiesce_all_vsi(pf);
4856
4857 /* Changes in configuration update VEB/VSI */
4858 i40e_dcb_reconfigure(pf);
4859
4860 i40e_pf_unquiesce_all_vsi(pf);
4861exit:
4862 return ret;
4863}
4864#endif /* CONFIG_I40E_DCB */
4865
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004866/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004867 * i40e_do_reset_safe - Protected reset path for userland calls.
4868 * @pf: board private structure
4869 * @reset_flags: which reset is requested
4870 *
4871 **/
4872void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4873{
4874 rtnl_lock();
4875 i40e_do_reset(pf, reset_flags);
4876 rtnl_unlock();
4877}
4878
4879/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004880 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4881 * @pf: board private structure
4882 * @e: event info posted on ARQ
4883 *
4884 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4885 * and VF queues
4886 **/
4887static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4888 struct i40e_arq_event_info *e)
4889{
4890 struct i40e_aqc_lan_overflow *data =
4891 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4892 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4893 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4894 struct i40e_hw *hw = &pf->hw;
4895 struct i40e_vf *vf;
4896 u16 vf_id;
4897
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004898 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
4899 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004900
4901 /* Queue belongs to VF, find the VF and issue VF reset */
4902 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4903 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4904 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4905 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4906 vf_id -= hw->func_caps.vf_base_id;
4907 vf = &pf->vf[vf_id];
4908 i40e_vc_notify_vf_reset(vf);
4909 /* Allow VF to process pending reset notification */
4910 msleep(20);
4911 i40e_reset_vf(vf, false);
4912 }
4913}
4914
4915/**
4916 * i40e_service_event_complete - Finish up the service event
4917 * @pf: board private structure
4918 **/
4919static void i40e_service_event_complete(struct i40e_pf *pf)
4920{
4921 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4922
4923 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01004924 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004925 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4926}
4927
4928/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004929 * i40e_get_current_fd_count - Get the count of FD filters programmed in the HW
4930 * @pf: board private structure
4931 **/
4932int i40e_get_current_fd_count(struct i40e_pf *pf)
4933{
4934 int val, fcnt_prog;
4935 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
4936 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
4937 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
4938 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
4939 return fcnt_prog;
4940}
4941
4942/**
4943 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
4944 * @pf: board private structure
4945 **/
4946void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
4947{
4948 u32 fcnt_prog, fcnt_avail;
4949
4950 /* Check if, FD SB or ATR was auto disabled and if there is enough room
4951 * to re-enable
4952 */
4953 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4954 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4955 return;
4956 fcnt_prog = i40e_get_current_fd_count(pf);
Anjali Singhai Jain89132782014-04-09 05:59:01 +00004957 fcnt_avail = i40e_get_fd_cnt_all(pf);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004958 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) {
4959 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
4960 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
4961 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
4962 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
4963 }
4964 }
4965 /* Wait for some more space to be available to turn on ATR */
4966 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
4967 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4968 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
4969 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
4970 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
4971 }
4972 }
4973}
4974
4975/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004976 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4977 * @pf: board private structure
4978 **/
4979static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4980{
4981 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4982 return;
4983
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004984 /* if interface is down do nothing */
4985 if (test_bit(__I40E_DOWN, &pf->state))
4986 return;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004987 i40e_fdir_check_and_reenable(pf);
4988
4989 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4990 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4991 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004992}
4993
4994/**
4995 * i40e_vsi_link_event - notify VSI of a link event
4996 * @vsi: vsi to be notified
4997 * @link_up: link up or down
4998 **/
4999static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
5000{
5001 if (!vsi)
5002 return;
5003
5004 switch (vsi->type) {
5005 case I40E_VSI_MAIN:
5006 if (!vsi->netdev || !vsi->netdev_registered)
5007 break;
5008
5009 if (link_up) {
5010 netif_carrier_on(vsi->netdev);
5011 netif_tx_wake_all_queues(vsi->netdev);
5012 } else {
5013 netif_carrier_off(vsi->netdev);
5014 netif_tx_stop_all_queues(vsi->netdev);
5015 }
5016 break;
5017
5018 case I40E_VSI_SRIOV:
5019 break;
5020
5021 case I40E_VSI_VMDQ2:
5022 case I40E_VSI_CTRL:
5023 case I40E_VSI_MIRROR:
5024 default:
5025 /* there is no notification for other VSIs */
5026 break;
5027 }
5028}
5029
5030/**
5031 * i40e_veb_link_event - notify elements on the veb of a link event
5032 * @veb: veb to be notified
5033 * @link_up: link up or down
5034 **/
5035static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
5036{
5037 struct i40e_pf *pf;
5038 int i;
5039
5040 if (!veb || !veb->pf)
5041 return;
5042 pf = veb->pf;
5043
5044 /* depth first... */
5045 for (i = 0; i < I40E_MAX_VEB; i++)
5046 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
5047 i40e_veb_link_event(pf->veb[i], link_up);
5048
5049 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00005050 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005051 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
5052 i40e_vsi_link_event(pf->vsi[i], link_up);
5053}
5054
5055/**
5056 * i40e_link_event - Update netif_carrier status
5057 * @pf: board private structure
5058 **/
5059static void i40e_link_event(struct i40e_pf *pf)
5060{
5061 bool new_link, old_link;
5062
5063 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
5064 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
5065
5066 if (new_link == old_link)
5067 return;
Anjali Singhai6d779b42013-09-28 06:00:02 +00005068 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005069 i40e_print_link_message(pf->vsi[pf->lan_vsi], new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005070
5071 /* Notify the base of the switch tree connected to
5072 * the link. Floating VEBs are not notified.
5073 */
5074 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
5075 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
5076 else
5077 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
5078
5079 if (pf->vf)
5080 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005081
5082 if (pf->flags & I40E_FLAG_PTP)
5083 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005084}
5085
5086/**
5087 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
5088 * @pf: board private structure
5089 *
5090 * Set the per-queue flags to request a check for stuck queues in the irq
5091 * clean functions, then force interrupts to be sure the irq clean is called.
5092 **/
5093static void i40e_check_hang_subtask(struct i40e_pf *pf)
5094{
5095 int i, v;
5096
5097 /* If we're down or resetting, just bail */
5098 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
5099 return;
5100
5101 /* for each VSI/netdev
5102 * for each Tx queue
5103 * set the check flag
5104 * for each q_vector
5105 * force an interrupt
5106 */
Mitch Williams505682c2014-05-20 08:01:37 +00005107 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005108 struct i40e_vsi *vsi = pf->vsi[v];
5109 int armed = 0;
5110
5111 if (!pf->vsi[v] ||
5112 test_bit(__I40E_DOWN, &vsi->state) ||
5113 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
5114 continue;
5115
5116 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005117 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005118 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005119 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005120 armed++;
5121 }
5122
5123 if (armed) {
5124 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
5125 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
5126 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
5127 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
5128 } else {
5129 u16 vec = vsi->base_vector - 1;
5130 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
5131 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
5132 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
5133 wr32(&vsi->back->hw,
5134 I40E_PFINT_DYN_CTLN(vec), val);
5135 }
5136 i40e_flush(&vsi->back->hw);
5137 }
5138 }
5139}
5140
5141/**
5142 * i40e_watchdog_subtask - Check and bring link up
5143 * @pf: board private structure
5144 **/
5145static void i40e_watchdog_subtask(struct i40e_pf *pf)
5146{
5147 int i;
5148
5149 /* if interface is down do nothing */
5150 if (test_bit(__I40E_DOWN, &pf->state) ||
5151 test_bit(__I40E_CONFIG_BUSY, &pf->state))
5152 return;
5153
5154 /* Update the stats for active netdevs so the network stack
5155 * can look at updated numbers whenever it cares to
5156 */
Mitch Williams505682c2014-05-20 08:01:37 +00005157 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005158 if (pf->vsi[i] && pf->vsi[i]->netdev)
5159 i40e_update_stats(pf->vsi[i]);
5160
5161 /* Update the stats for the active switching components */
5162 for (i = 0; i < I40E_MAX_VEB; i++)
5163 if (pf->veb[i])
5164 i40e_update_veb_stats(pf->veb[i]);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005165
5166 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005167}
5168
5169/**
5170 * i40e_reset_subtask - Set up for resetting the device and driver
5171 * @pf: board private structure
5172 **/
5173static void i40e_reset_subtask(struct i40e_pf *pf)
5174{
5175 u32 reset_flags = 0;
5176
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005177 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005178 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
5179 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
5180 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
5181 }
5182 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
5183 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
5184 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5185 }
5186 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
5187 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
5188 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
5189 }
5190 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
5191 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
5192 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
5193 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005194 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
5195 reset_flags |= (1 << __I40E_DOWN_REQUESTED);
5196 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
5197 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005198
5199 /* If there's a recovery already waiting, it takes
5200 * precedence before starting a new reset sequence.
5201 */
5202 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
5203 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005204 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005205 }
5206
5207 /* If we're already down or resetting, just bail */
5208 if (reset_flags &&
5209 !test_bit(__I40E_DOWN, &pf->state) &&
5210 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
5211 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005212
5213unlock:
5214 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005215}
5216
5217/**
5218 * i40e_handle_link_event - Handle link event
5219 * @pf: board private structure
5220 * @e: event info posted on ARQ
5221 **/
5222static void i40e_handle_link_event(struct i40e_pf *pf,
5223 struct i40e_arq_event_info *e)
5224{
5225 struct i40e_hw *hw = &pf->hw;
5226 struct i40e_aqc_get_link_status *status =
5227 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
5228 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
5229
5230 /* save off old link status information */
5231 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
5232 sizeof(pf->hw.phy.link_info_old));
5233
5234 /* update link status */
5235 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
5236 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
5237 hw_link_info->link_info = status->link_info;
5238 hw_link_info->an_info = status->an_info;
5239 hw_link_info->ext_info = status->ext_info;
5240 hw_link_info->lse_enable =
5241 le16_to_cpu(status->command_flags) &
5242 I40E_AQ_LSE_ENABLE;
5243
5244 /* process the event */
5245 i40e_link_event(pf);
5246
5247 /* Do a new status request to re-enable LSE reporting
5248 * and load new status information into the hw struct,
5249 * then see if the status changed while processing the
5250 * initial event.
5251 */
5252 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
5253 i40e_link_event(pf);
5254}
5255
5256/**
5257 * i40e_clean_adminq_subtask - Clean the AdminQ rings
5258 * @pf: board private structure
5259 **/
5260static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
5261{
5262 struct i40e_arq_event_info event;
5263 struct i40e_hw *hw = &pf->hw;
5264 u16 pending, i = 0;
5265 i40e_status ret;
5266 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00005267 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005268 u32 val;
5269
5270 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
5271 return;
5272
Shannon Nelson86df2422014-05-20 08:01:35 +00005273 /* check for error indications */
5274 val = rd32(&pf->hw, pf->hw.aq.arq.len);
5275 oldval = val;
5276 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
5277 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
5278 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
5279 }
5280 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
5281 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
5282 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
5283 }
5284 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
5285 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
5286 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
5287 }
5288 if (oldval != val)
5289 wr32(&pf->hw, pf->hw.aq.arq.len, val);
5290
5291 val = rd32(&pf->hw, pf->hw.aq.asq.len);
5292 oldval = val;
5293 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
5294 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
5295 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
5296 }
5297 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
5298 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
5299 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
5300 }
5301 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
5302 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
5303 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
5304 }
5305 if (oldval != val)
5306 wr32(&pf->hw, pf->hw.aq.asq.len, val);
5307
Mitch Williams3197ce22013-11-28 06:39:39 +00005308 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005309 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
5310 if (!event.msg_buf)
5311 return;
5312
5313 do {
Mitch Williams2f019122013-11-28 06:39:33 +00005314 event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005315 ret = i40e_clean_arq_element(hw, &event, &pending);
5316 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
5317 dev_info(&pf->pdev->dev, "No ARQ event found\n");
5318 break;
5319 } else if (ret) {
5320 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
5321 break;
5322 }
5323
5324 opcode = le16_to_cpu(event.desc.opcode);
5325 switch (opcode) {
5326
5327 case i40e_aqc_opc_get_link_status:
5328 i40e_handle_link_event(pf, &event);
5329 break;
5330 case i40e_aqc_opc_send_msg_to_pf:
5331 ret = i40e_vc_process_vf_msg(pf,
5332 le16_to_cpu(event.desc.retval),
5333 le32_to_cpu(event.desc.cookie_high),
5334 le32_to_cpu(event.desc.cookie_low),
5335 event.msg_buf,
5336 event.msg_size);
5337 break;
5338 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005339 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005340#ifdef CONFIG_I40E_DCB
5341 rtnl_lock();
5342 ret = i40e_handle_lldp_event(pf, &event);
5343 rtnl_unlock();
5344#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005345 break;
5346 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005347 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005348 i40e_handle_lan_overflow_event(pf, &event);
5349 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00005350 case i40e_aqc_opc_send_msg_to_peer:
5351 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
5352 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005353 default:
5354 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00005355 "ARQ Error: Unknown event 0x%04x received\n",
5356 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005357 break;
5358 }
5359 } while (pending && (i++ < pf->adminq_work_limit));
5360
5361 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
5362 /* re-enable Admin queue interrupt cause */
5363 val = rd32(hw, I40E_PFINT_ICR0_ENA);
5364 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
5365 wr32(hw, I40E_PFINT_ICR0_ENA, val);
5366 i40e_flush(hw);
5367
5368 kfree(event.msg_buf);
5369}
5370
5371/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005372 * i40e_verify_eeprom - make sure eeprom is good to use
5373 * @pf: board private structure
5374 **/
5375static void i40e_verify_eeprom(struct i40e_pf *pf)
5376{
5377 int err;
5378
5379 err = i40e_diag_eeprom_test(&pf->hw);
5380 if (err) {
5381 /* retry in case of garbage read */
5382 err = i40e_diag_eeprom_test(&pf->hw);
5383 if (err) {
5384 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
5385 err);
5386 set_bit(__I40E_BAD_EEPROM, &pf->state);
5387 }
5388 }
5389
5390 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
5391 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
5392 clear_bit(__I40E_BAD_EEPROM, &pf->state);
5393 }
5394}
5395
5396/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005397 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
5398 * @veb: pointer to the VEB instance
5399 *
5400 * This is a recursive function that first builds the attached VSIs then
5401 * recurses in to build the next layer of VEB. We track the connections
5402 * through our own index numbers because the seid's from the HW could
5403 * change across the reset.
5404 **/
5405static int i40e_reconstitute_veb(struct i40e_veb *veb)
5406{
5407 struct i40e_vsi *ctl_vsi = NULL;
5408 struct i40e_pf *pf = veb->pf;
5409 int v, veb_idx;
5410 int ret;
5411
5412 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00005413 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005414 if (pf->vsi[v] &&
5415 pf->vsi[v]->veb_idx == veb->idx &&
5416 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
5417 ctl_vsi = pf->vsi[v];
5418 break;
5419 }
5420 }
5421 if (!ctl_vsi) {
5422 dev_info(&pf->pdev->dev,
5423 "missing owner VSI for veb_idx %d\n", veb->idx);
5424 ret = -ENOENT;
5425 goto end_reconstitute;
5426 }
5427 if (ctl_vsi != pf->vsi[pf->lan_vsi])
5428 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5429 ret = i40e_add_vsi(ctl_vsi);
5430 if (ret) {
5431 dev_info(&pf->pdev->dev,
5432 "rebuild of owner VSI failed: %d\n", ret);
5433 goto end_reconstitute;
5434 }
5435 i40e_vsi_reset_stats(ctl_vsi);
5436
5437 /* create the VEB in the switch and move the VSI onto the VEB */
5438 ret = i40e_add_veb(veb, ctl_vsi);
5439 if (ret)
5440 goto end_reconstitute;
5441
5442 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00005443 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005444 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
5445 continue;
5446
5447 if (pf->vsi[v]->veb_idx == veb->idx) {
5448 struct i40e_vsi *vsi = pf->vsi[v];
5449 vsi->uplink_seid = veb->seid;
5450 ret = i40e_add_vsi(vsi);
5451 if (ret) {
5452 dev_info(&pf->pdev->dev,
5453 "rebuild of vsi_idx %d failed: %d\n",
5454 v, ret);
5455 goto end_reconstitute;
5456 }
5457 i40e_vsi_reset_stats(vsi);
5458 }
5459 }
5460
5461 /* create any VEBs attached to this VEB - RECURSION */
5462 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
5463 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
5464 pf->veb[veb_idx]->uplink_seid = veb->seid;
5465 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
5466 if (ret)
5467 break;
5468 }
5469 }
5470
5471end_reconstitute:
5472 return ret;
5473}
5474
5475/**
5476 * i40e_get_capabilities - get info about the HW
5477 * @pf: the PF struct
5478 **/
5479static int i40e_get_capabilities(struct i40e_pf *pf)
5480{
5481 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
5482 u16 data_size;
5483 int buf_len;
5484 int err;
5485
5486 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
5487 do {
5488 cap_buf = kzalloc(buf_len, GFP_KERNEL);
5489 if (!cap_buf)
5490 return -ENOMEM;
5491
5492 /* this loads the data into the hw struct for us */
5493 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
5494 &data_size,
5495 i40e_aqc_opc_list_func_capabilities,
5496 NULL);
5497 /* data loaded, buffer no longer needed */
5498 kfree(cap_buf);
5499
5500 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
5501 /* retry with a larger buffer */
5502 buf_len = data_size;
5503 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
5504 dev_info(&pf->pdev->dev,
5505 "capability discovery failed: aq=%d\n",
5506 pf->hw.aq.asq_last_status);
5507 return -ENODEV;
5508 }
5509 } while (err);
5510
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00005511 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
5512 (pf->hw.aq.fw_maj_ver < 2)) {
5513 pf->hw.func_caps.num_msix_vectors++;
5514 pf->hw.func_caps.num_msix_vectors_vf++;
5515 }
5516
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005517 if (pf->hw.debug_mask & I40E_DEBUG_USER)
5518 dev_info(&pf->pdev->dev,
5519 "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",
5520 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
5521 pf->hw.func_caps.num_msix_vectors,
5522 pf->hw.func_caps.num_msix_vectors_vf,
5523 pf->hw.func_caps.fd_filters_guaranteed,
5524 pf->hw.func_caps.fd_filters_best_effort,
5525 pf->hw.func_caps.num_tx_qp,
5526 pf->hw.func_caps.num_vsis);
5527
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005528#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
5529 + pf->hw.func_caps.num_vfs)
5530 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
5531 dev_info(&pf->pdev->dev,
5532 "got num_vsis %d, setting num_vsis to %d\n",
5533 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
5534 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
5535 }
5536
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005537 return 0;
5538}
5539
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005540static int i40e_vsi_clear(struct i40e_vsi *vsi);
5541
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005542/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005543 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005544 * @pf: board private structure
5545 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005546static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005547{
5548 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005549 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005550
Jesse Brandeburg407e0632014-06-03 23:50:12 +00005551 /* quick workaround for an NVM issue that leaves a critical register
5552 * uninitialized
5553 */
5554 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
5555 static const u32 hkey[] = {
5556 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
5557 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
5558 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
5559 0x95b3a76d};
5560
5561 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
5562 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
5563 }
5564
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005565 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005566 return;
5567
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005568 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005569 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00005570 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005571 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005572 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005573 break;
5574 }
5575 }
5576
5577 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005578 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005579 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
5580 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005581 if (!vsi) {
5582 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005583 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
5584 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005585 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005586 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005587
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005588 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005589}
5590
5591/**
5592 * i40e_fdir_teardown - release the Flow Director resources
5593 * @pf: board private structure
5594 **/
5595static void i40e_fdir_teardown(struct i40e_pf *pf)
5596{
5597 int i;
5598
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005599 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00005600 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005601 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
5602 i40e_vsi_release(pf->vsi[i]);
5603 break;
5604 }
5605 }
5606}
5607
5608/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005609 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005610 * @pf: board private structure
5611 *
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005612 * Close up the VFs and other things in prep for pf Reset.
5613 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00005614static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005615{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005616 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00005617 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005618 u32 v;
5619
5620 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
5621 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00005622 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005623
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005624 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005625
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005626 /* quiesce the VSIs and their queues that are not already DOWN */
5627 i40e_pf_quiesce_all_vsi(pf);
5628
Mitch Williams505682c2014-05-20 08:01:37 +00005629 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005630 if (pf->vsi[v])
5631 pf->vsi[v]->seid = 0;
5632 }
5633
5634 i40e_shutdown_adminq(&pf->hw);
5635
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005636 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00005637 if (hw->hmc.hmc_obj) {
5638 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00005639 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00005640 dev_warn(&pf->pdev->dev,
5641 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005642 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005643}
5644
5645/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005646 * i40e_send_version - update firmware with driver version
5647 * @pf: PF struct
5648 */
5649static void i40e_send_version(struct i40e_pf *pf)
5650{
5651 struct i40e_driver_version dv;
5652
5653 dv.major_version = DRV_VERSION_MAJOR;
5654 dv.minor_version = DRV_VERSION_MINOR;
5655 dv.build_version = DRV_VERSION_BUILD;
5656 dv.subbuild_version = 0;
5657 strncpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
5658 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
5659}
5660
5661/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00005662 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005663 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005664 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005665 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005666static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005667{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005668 struct i40e_hw *hw = &pf->hw;
5669 i40e_status ret;
5670 u32 v;
5671
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005672 /* Now we wait for GRST to settle out.
5673 * We don't have to delete the VEBs or VSIs from the hw switch
5674 * because the reset will make them disappear.
5675 */
5676 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005677 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005678 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005679 goto end_core_reset;
5680 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005681 pf->pfr_count++;
5682
5683 if (test_bit(__I40E_DOWN, &pf->state))
5684 goto end_core_reset;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005685 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005686
5687 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
5688 ret = i40e_init_adminq(&pf->hw);
5689 if (ret) {
5690 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
5691 goto end_core_reset;
5692 }
5693
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005694 /* re-verify the eeprom if we just had an EMP reset */
5695 if (test_bit(__I40E_EMP_RESET_REQUESTED, &pf->state)) {
5696 clear_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
5697 i40e_verify_eeprom(pf);
5698 }
5699
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00005700 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005701 ret = i40e_get_capabilities(pf);
5702 if (ret) {
5703 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
5704 ret);
5705 goto end_core_reset;
5706 }
5707
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005708 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
5709 hw->func_caps.num_rx_qp,
5710 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
5711 if (ret) {
5712 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
5713 goto end_core_reset;
5714 }
5715 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
5716 if (ret) {
5717 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
5718 goto end_core_reset;
5719 }
5720
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005721#ifdef CONFIG_I40E_DCB
5722 ret = i40e_init_pf_dcb(pf);
5723 if (ret) {
5724 dev_info(&pf->pdev->dev, "init_pf_dcb failed: %d\n", ret);
5725 goto end_core_reset;
5726 }
5727#endif /* CONFIG_I40E_DCB */
5728
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005729 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005730 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005731 if (ret)
5732 goto end_core_reset;
5733
5734 /* Rebuild the VSIs and VEBs that existed before reset.
5735 * They are still in our local switch element arrays, so only
5736 * need to rebuild the switch model in the HW.
5737 *
5738 * If there were VEBs but the reconstitution failed, we'll try
5739 * try to recover minimal use by getting the basic PF VSI working.
5740 */
5741 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005742 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005743 /* find the one VEB connected to the MAC, and find orphans */
5744 for (v = 0; v < I40E_MAX_VEB; v++) {
5745 if (!pf->veb[v])
5746 continue;
5747
5748 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
5749 pf->veb[v]->uplink_seid == 0) {
5750 ret = i40e_reconstitute_veb(pf->veb[v]);
5751
5752 if (!ret)
5753 continue;
5754
5755 /* If Main VEB failed, we're in deep doodoo,
5756 * so give up rebuilding the switch and set up
5757 * for minimal rebuild of PF VSI.
5758 * If orphan failed, we'll report the error
5759 * but try to keep going.
5760 */
5761 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
5762 dev_info(&pf->pdev->dev,
5763 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
5764 ret);
5765 pf->vsi[pf->lan_vsi]->uplink_seid
5766 = pf->mac_seid;
5767 break;
5768 } else if (pf->veb[v]->uplink_seid == 0) {
5769 dev_info(&pf->pdev->dev,
5770 "rebuild of orphan VEB failed: %d\n",
5771 ret);
5772 }
5773 }
5774 }
5775 }
5776
5777 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
5778 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
5779 /* no VEB, so rebuild only the Main VSI */
5780 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
5781 if (ret) {
5782 dev_info(&pf->pdev->dev,
5783 "rebuild of Main VSI failed: %d\n", ret);
5784 goto end_core_reset;
5785 }
5786 }
5787
5788 /* reinit the misc interrupt */
5789 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5790 ret = i40e_setup_misc_vector(pf);
5791
5792 /* restart the VSIs that were rebuilt and running before the reset */
5793 i40e_pf_unquiesce_all_vsi(pf);
5794
Mitch Williams69f64b22014-02-13 03:48:46 -08005795 if (pf->num_alloc_vfs) {
5796 for (v = 0; v < pf->num_alloc_vfs; v++)
5797 i40e_reset_vf(&pf->vf[v], true);
5798 }
5799
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005800 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005801 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005802
5803end_core_reset:
5804 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5805}
5806
5807/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005808 * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
5809 * @pf: board private structure
5810 *
5811 * Close up the VFs and other things in prep for a Core Reset,
5812 * then get ready to rebuild the world.
5813 **/
5814static void i40e_handle_reset_warning(struct i40e_pf *pf)
5815{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00005816 i40e_prep_for_reset(pf);
5817 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005818}
5819
5820/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005821 * i40e_handle_mdd_event
5822 * @pf: pointer to the pf structure
5823 *
5824 * Called from the MDD irq handler to identify possibly malicious vfs
5825 **/
5826static void i40e_handle_mdd_event(struct i40e_pf *pf)
5827{
5828 struct i40e_hw *hw = &pf->hw;
5829 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00005830 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005831 struct i40e_vf *vf;
5832 u32 reg;
5833 int i;
5834
5835 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
5836 return;
5837
5838 /* find what triggered the MDD event */
5839 reg = rd32(hw, I40E_GL_MDET_TX);
5840 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00005841 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
5842 I40E_GL_MDET_TX_PF_NUM_SHIFT;
5843 u8 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
5844 I40E_GL_MDET_TX_VF_NUM_SHIFT;
5845 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT) >>
5846 I40E_GL_MDET_TX_EVENT_SHIFT;
5847 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
5848 I40E_GL_MDET_TX_QUEUE_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005849 dev_info(&pf->pdev->dev,
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00005850 "Malicious Driver Detection event 0x%02x on TX queue %d pf number 0x%02x vf number 0x%02x\n",
5851 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005852 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
5853 mdd_detected = true;
5854 }
5855 reg = rd32(hw, I40E_GL_MDET_RX);
5856 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00005857 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
5858 I40E_GL_MDET_RX_FUNCTION_SHIFT;
5859 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT) >>
5860 I40E_GL_MDET_RX_EVENT_SHIFT;
5861 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
5862 I40E_GL_MDET_RX_QUEUE_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005863 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005864 "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005865 event, queue, func);
5866 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
5867 mdd_detected = true;
5868 }
5869
Neerav Parikhdf430b12014-06-04 01:23:15 +00005870 if (mdd_detected) {
5871 reg = rd32(hw, I40E_PF_MDET_TX);
5872 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
5873 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
5874 dev_info(&pf->pdev->dev,
5875 "MDD TX event is for this function 0x%08x, requesting PF reset.\n",
5876 reg);
5877 pf_mdd_detected = true;
5878 }
5879 reg = rd32(hw, I40E_PF_MDET_RX);
5880 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
5881 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
5882 dev_info(&pf->pdev->dev,
5883 "MDD RX event is for this function 0x%08x, requesting PF reset.\n",
5884 reg);
5885 pf_mdd_detected = true;
5886 }
5887 /* Queue belongs to the PF, initiate a reset */
5888 if (pf_mdd_detected) {
5889 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5890 i40e_service_event_schedule(pf);
5891 }
5892 }
5893
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005894 /* see if one of the VFs needs its hand slapped */
5895 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
5896 vf = &(pf->vf[i]);
5897 reg = rd32(hw, I40E_VP_MDET_TX(i));
5898 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
5899 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
5900 vf->num_mdd_events++;
5901 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
5902 }
5903
5904 reg = rd32(hw, I40E_VP_MDET_RX(i));
5905 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
5906 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
5907 vf->num_mdd_events++;
5908 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
5909 }
5910
5911 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
5912 dev_info(&pf->pdev->dev,
5913 "Too many MDD events on VF %d, disabled\n", i);
5914 dev_info(&pf->pdev->dev,
5915 "Use PF Control I/F to re-enable the VF\n");
5916 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
5917 }
5918 }
5919
5920 /* re-enable mdd interrupt cause */
5921 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
5922 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
5923 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
5924 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
5925 i40e_flush(hw);
5926}
5927
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005928#ifdef CONFIG_I40E_VXLAN
5929/**
5930 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
5931 * @pf: board private structure
5932 **/
5933static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
5934{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005935 struct i40e_hw *hw = &pf->hw;
5936 i40e_status ret;
5937 u8 filter_index;
5938 __be16 port;
5939 int i;
5940
5941 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
5942 return;
5943
5944 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
5945
5946 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5947 if (pf->pending_vxlan_bitmap & (1 << i)) {
5948 pf->pending_vxlan_bitmap &= ~(1 << i);
5949 port = pf->vxlan_ports[i];
5950 ret = port ?
5951 i40e_aq_add_udp_tunnel(hw, ntohs(port),
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005952 I40E_AQC_TUNNEL_TYPE_VXLAN,
5953 &filter_index, NULL)
5954 : i40e_aq_del_udp_tunnel(hw, i, NULL);
5955
5956 if (ret) {
5957 dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
5958 port ? "adding" : "deleting",
5959 ntohs(port), port ? i : i);
5960
5961 pf->vxlan_ports[i] = 0;
5962 } else {
5963 dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
5964 port ? "Added" : "Deleted",
5965 ntohs(port), port ? i : filter_index);
5966 }
5967 }
5968 }
5969}
5970
5971#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005972/**
5973 * i40e_service_task - Run the driver's async subtasks
5974 * @work: pointer to work_struct containing our data
5975 **/
5976static void i40e_service_task(struct work_struct *work)
5977{
5978 struct i40e_pf *pf = container_of(work,
5979 struct i40e_pf,
5980 service_task);
5981 unsigned long start_time = jiffies;
5982
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00005983 /* don't bother with service tasks if a reset is in progress */
5984 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
5985 i40e_service_event_complete(pf);
5986 return;
5987 }
5988
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005989 i40e_reset_subtask(pf);
5990 i40e_handle_mdd_event(pf);
5991 i40e_vc_process_vflr_event(pf);
5992 i40e_watchdog_subtask(pf);
5993 i40e_fdir_reinit_subtask(pf);
5994 i40e_check_hang_subtask(pf);
5995 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005996#ifdef CONFIG_I40E_VXLAN
5997 i40e_sync_vxlan_filters_subtask(pf);
5998#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005999 i40e_clean_adminq_subtask(pf);
6000
6001 i40e_service_event_complete(pf);
6002
6003 /* If the tasks have taken longer than one timer cycle or there
6004 * is more work to be done, reschedule the service task now
6005 * rather than wait for the timer to tick again.
6006 */
6007 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
6008 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
6009 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
6010 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
6011 i40e_service_event_schedule(pf);
6012}
6013
6014/**
6015 * i40e_service_timer - timer callback
6016 * @data: pointer to PF struct
6017 **/
6018static void i40e_service_timer(unsigned long data)
6019{
6020 struct i40e_pf *pf = (struct i40e_pf *)data;
6021
6022 mod_timer(&pf->service_timer,
6023 round_jiffies(jiffies + pf->service_timer_period));
6024 i40e_service_event_schedule(pf);
6025}
6026
6027/**
6028 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
6029 * @vsi: the VSI being configured
6030 **/
6031static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
6032{
6033 struct i40e_pf *pf = vsi->back;
6034
6035 switch (vsi->type) {
6036 case I40E_VSI_MAIN:
6037 vsi->alloc_queue_pairs = pf->num_lan_qps;
6038 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6039 I40E_REQ_DESCRIPTOR_MULTIPLE);
6040 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6041 vsi->num_q_vectors = pf->num_lan_msix;
6042 else
6043 vsi->num_q_vectors = 1;
6044
6045 break;
6046
6047 case I40E_VSI_FDIR:
6048 vsi->alloc_queue_pairs = 1;
6049 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
6050 I40E_REQ_DESCRIPTOR_MULTIPLE);
6051 vsi->num_q_vectors = 1;
6052 break;
6053
6054 case I40E_VSI_VMDQ2:
6055 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
6056 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6057 I40E_REQ_DESCRIPTOR_MULTIPLE);
6058 vsi->num_q_vectors = pf->num_vmdq_msix;
6059 break;
6060
6061 case I40E_VSI_SRIOV:
6062 vsi->alloc_queue_pairs = pf->num_vf_qps;
6063 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6064 I40E_REQ_DESCRIPTOR_MULTIPLE);
6065 break;
6066
6067 default:
6068 WARN_ON(1);
6069 return -ENODATA;
6070 }
6071
6072 return 0;
6073}
6074
6075/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006076 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
6077 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006078 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006079 *
6080 * On error: returns error code (negative)
6081 * On success: returns 0
6082 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006083static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006084{
6085 int size;
6086 int ret = 0;
6087
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00006088 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006089 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
6090 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
6091 if (!vsi->tx_rings)
6092 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006093 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
6094
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006095 if (alloc_qvectors) {
6096 /* allocate memory for q_vector pointers */
6097 size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
6098 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
6099 if (!vsi->q_vectors) {
6100 ret = -ENOMEM;
6101 goto err_vectors;
6102 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006103 }
6104 return ret;
6105
6106err_vectors:
6107 kfree(vsi->tx_rings);
6108 return ret;
6109}
6110
6111/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006112 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
6113 * @pf: board private structure
6114 * @type: type of VSI
6115 *
6116 * On error: returns error code (negative)
6117 * On success: returns vsi index in PF (positive)
6118 **/
6119static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
6120{
6121 int ret = -ENODEV;
6122 struct i40e_vsi *vsi;
6123 int vsi_idx;
6124 int i;
6125
6126 /* Need to protect the allocation of the VSIs at the PF level */
6127 mutex_lock(&pf->switch_mutex);
6128
6129 /* VSI list may be fragmented if VSI creation/destruction has
6130 * been happening. We can afford to do a quick scan to look
6131 * for any free VSIs in the list.
6132 *
6133 * find next empty vsi slot, looping back around if necessary
6134 */
6135 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00006136 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006137 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00006138 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006139 i = 0;
6140 while (i < pf->next_vsi && pf->vsi[i])
6141 i++;
6142 }
6143
Mitch Williams505682c2014-05-20 08:01:37 +00006144 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006145 vsi_idx = i; /* Found one! */
6146 } else {
6147 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00006148 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006149 }
6150 pf->next_vsi = ++i;
6151
6152 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
6153 if (!vsi) {
6154 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00006155 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006156 }
6157 vsi->type = type;
6158 vsi->back = pf;
6159 set_bit(__I40E_DOWN, &vsi->state);
6160 vsi->flags = 0;
6161 vsi->idx = vsi_idx;
6162 vsi->rx_itr_setting = pf->rx_itr_default;
6163 vsi->tx_itr_setting = pf->tx_itr_default;
6164 vsi->netdev_registered = false;
6165 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
6166 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00006167 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006168
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006169 ret = i40e_set_num_rings_in_vsi(vsi);
6170 if (ret)
6171 goto err_rings;
6172
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006173 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006174 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006175 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00006176
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006177 /* Setup default MSIX irq handler for VSI */
6178 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
6179
6180 pf->vsi[vsi_idx] = vsi;
6181 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00006182 goto unlock_pf;
6183
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006184err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00006185 pf->next_vsi = i - 1;
6186 kfree(vsi);
6187unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006188 mutex_unlock(&pf->switch_mutex);
6189 return ret;
6190}
6191
6192/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006193 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
6194 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006195 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006196 *
6197 * On error: returns error code (negative)
6198 * On success: returns 0
6199 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006200static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006201{
6202 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006203 if (free_qvectors) {
6204 kfree(vsi->q_vectors);
6205 vsi->q_vectors = NULL;
6206 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006207 kfree(vsi->tx_rings);
6208 vsi->tx_rings = NULL;
6209 vsi->rx_rings = NULL;
6210}
6211
6212/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006213 * i40e_vsi_clear - Deallocate the VSI provided
6214 * @vsi: the VSI being un-configured
6215 **/
6216static int i40e_vsi_clear(struct i40e_vsi *vsi)
6217{
6218 struct i40e_pf *pf;
6219
6220 if (!vsi)
6221 return 0;
6222
6223 if (!vsi->back)
6224 goto free_vsi;
6225 pf = vsi->back;
6226
6227 mutex_lock(&pf->switch_mutex);
6228 if (!pf->vsi[vsi->idx]) {
6229 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
6230 vsi->idx, vsi->idx, vsi, vsi->type);
6231 goto unlock_vsi;
6232 }
6233
6234 if (pf->vsi[vsi->idx] != vsi) {
6235 dev_err(&pf->pdev->dev,
6236 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
6237 pf->vsi[vsi->idx]->idx,
6238 pf->vsi[vsi->idx],
6239 pf->vsi[vsi->idx]->type,
6240 vsi->idx, vsi, vsi->type);
6241 goto unlock_vsi;
6242 }
6243
6244 /* updates the pf for this cleared vsi */
6245 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
6246 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
6247
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006248 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00006249
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006250 pf->vsi[vsi->idx] = NULL;
6251 if (vsi->idx < pf->next_vsi)
6252 pf->next_vsi = vsi->idx;
6253
6254unlock_vsi:
6255 mutex_unlock(&pf->switch_mutex);
6256free_vsi:
6257 kfree(vsi);
6258
6259 return 0;
6260}
6261
6262/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006263 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
6264 * @vsi: the VSI being cleaned
6265 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006266static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006267{
6268 int i;
6269
Greg Rose8e9dca52013-12-18 13:45:53 +00006270 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00006271 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00006272 kfree_rcu(vsi->tx_rings[i], rcu);
6273 vsi->tx_rings[i] = NULL;
6274 vsi->rx_rings[i] = NULL;
6275 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006276 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006277}
6278
6279/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006280 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
6281 * @vsi: the VSI being configured
6282 **/
6283static int i40e_alloc_rings(struct i40e_vsi *vsi)
6284{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00006285 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006286 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006287 int i;
6288
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006289 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00006290 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00006291 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006292 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
6293 if (!tx_ring)
6294 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006295
6296 tx_ring->queue_index = i;
6297 tx_ring->reg_idx = vsi->base_queue + i;
6298 tx_ring->ring_active = false;
6299 tx_ring->vsi = vsi;
6300 tx_ring->netdev = vsi->netdev;
6301 tx_ring->dev = &pf->pdev->dev;
6302 tx_ring->count = vsi->num_desc;
6303 tx_ring->size = 0;
6304 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006305 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006306
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006307 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006308 rx_ring->queue_index = i;
6309 rx_ring->reg_idx = vsi->base_queue + i;
6310 rx_ring->ring_active = false;
6311 rx_ring->vsi = vsi;
6312 rx_ring->netdev = vsi->netdev;
6313 rx_ring->dev = &pf->pdev->dev;
6314 rx_ring->count = vsi->num_desc;
6315 rx_ring->size = 0;
6316 rx_ring->dcb_tc = 0;
6317 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
6318 set_ring_16byte_desc_enabled(rx_ring);
6319 else
6320 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006321 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006322 }
6323
6324 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006325
6326err_out:
6327 i40e_vsi_clear_rings(vsi);
6328 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006329}
6330
6331/**
6332 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
6333 * @pf: board private structure
6334 * @vectors: the number of MSI-X vectors to request
6335 *
6336 * Returns the number of vectors reserved, or error
6337 **/
6338static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
6339{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006340 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
6341 I40E_MIN_MSIX, vectors);
6342 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006343 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006344 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006345 vectors = 0;
6346 }
6347
6348 return vectors;
6349}
6350
6351/**
6352 * i40e_init_msix - Setup the MSIX capability
6353 * @pf: board private structure
6354 *
6355 * Work with the OS to set up the MSIX vectors needed.
6356 *
6357 * Returns 0 on success, negative on failure
6358 **/
6359static int i40e_init_msix(struct i40e_pf *pf)
6360{
6361 i40e_status err = 0;
6362 struct i40e_hw *hw = &pf->hw;
6363 int v_budget, i;
6364 int vec;
6365
6366 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6367 return -ENODEV;
6368
6369 /* The number of vectors we'll request will be comprised of:
6370 * - Add 1 for "other" cause for Admin Queue events, etc.
6371 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006372 * - Queues being used for RSS.
6373 * We don't need as many as max_rss_size vectors.
6374 * use rss_size instead in the calculation since that
6375 * is governed by number of cpus in the system.
6376 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006377 * - The number of VMDq pairs
6378 * Once we count this up, try the request.
6379 *
6380 * If we can't get what we want, we'll simplify to nearly nothing
6381 * and try again. If that still fails, we punt.
6382 */
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006383 pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006384 pf->num_vmdq_msix = pf->num_vmdq_qps;
6385 v_budget = 1 + pf->num_lan_msix;
6386 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006387 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006388 v_budget++;
6389
6390 /* Scale down if necessary, and the rings will share vectors */
6391 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
6392
6393 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
6394 GFP_KERNEL);
6395 if (!pf->msix_entries)
6396 return -ENOMEM;
6397
6398 for (i = 0; i < v_budget; i++)
6399 pf->msix_entries[i].entry = i;
6400 vec = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006401
6402 if (vec != v_budget) {
6403 /* If we have limited resources, we will start with no vectors
6404 * for the special features and then allocate vectors to some
6405 * of these features based on the policy and at the end disable
6406 * the features that did not get any vectors.
6407 */
6408 pf->num_vmdq_msix = 0;
6409 }
6410
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006411 if (vec < I40E_MIN_MSIX) {
6412 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
6413 kfree(pf->msix_entries);
6414 pf->msix_entries = NULL;
6415 return -ENODEV;
6416
6417 } else if (vec == I40E_MIN_MSIX) {
6418 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006419 pf->num_vmdq_vsis = 0;
6420 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006421 pf->num_lan_qps = 1;
6422 pf->num_lan_msix = 1;
6423
6424 } else if (vec != v_budget) {
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006425 /* reserve the misc vector */
6426 vec--;
6427
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006428 /* Scale vector usage down */
6429 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006430 pf->num_vmdq_vsis = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006431
6432 /* partition out the remaining vectors */
6433 switch (vec) {
6434 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006435 pf->num_lan_msix = 1;
6436 break;
6437 case 3:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006438 pf->num_lan_msix = 2;
6439 break;
6440 default:
6441 pf->num_lan_msix = min_t(int, (vec / 2),
6442 pf->num_lan_qps);
6443 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
6444 I40E_DEFAULT_NUM_VMDQ_VSI);
6445 break;
6446 }
6447 }
6448
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006449 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
6450 (pf->num_vmdq_msix == 0)) {
6451 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
6452 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
6453 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006454 return err;
6455}
6456
6457/**
Greg Rose90e04072014-03-06 08:59:57 +00006458 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00006459 * @vsi: the VSI being configured
6460 * @v_idx: index of the vector in the vsi struct
6461 *
6462 * We allocate one q_vector. If allocation fails we return -ENOMEM.
6463 **/
Greg Rose90e04072014-03-06 08:59:57 +00006464static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00006465{
6466 struct i40e_q_vector *q_vector;
6467
6468 /* allocate q_vector */
6469 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
6470 if (!q_vector)
6471 return -ENOMEM;
6472
6473 q_vector->vsi = vsi;
6474 q_vector->v_idx = v_idx;
6475 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
6476 if (vsi->netdev)
6477 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00006478 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00006479
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00006480 q_vector->rx.latency_range = I40E_LOW_LATENCY;
6481 q_vector->tx.latency_range = I40E_LOW_LATENCY;
6482
Alexander Duyck493fb302013-09-28 07:01:44 +00006483 /* tie q_vector and vsi together */
6484 vsi->q_vectors[v_idx] = q_vector;
6485
6486 return 0;
6487}
6488
6489/**
Greg Rose90e04072014-03-06 08:59:57 +00006490 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006491 * @vsi: the VSI being configured
6492 *
6493 * We allocate one q_vector per queue interrupt. If allocation fails we
6494 * return -ENOMEM.
6495 **/
Greg Rose90e04072014-03-06 08:59:57 +00006496static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006497{
6498 struct i40e_pf *pf = vsi->back;
6499 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00006500 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006501
6502 /* if not MSIX, give the one vector only to the LAN VSI */
6503 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6504 num_q_vectors = vsi->num_q_vectors;
6505 else if (vsi == pf->vsi[pf->lan_vsi])
6506 num_q_vectors = 1;
6507 else
6508 return -EINVAL;
6509
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006510 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00006511 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00006512 if (err)
6513 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006514 }
6515
6516 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00006517
6518err_out:
6519 while (v_idx--)
6520 i40e_free_q_vector(vsi, v_idx);
6521
6522 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006523}
6524
6525/**
6526 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
6527 * @pf: board private structure to initialize
6528 **/
6529static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
6530{
6531 int err = 0;
6532
6533 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
6534 err = i40e_init_msix(pf);
6535 if (err) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006536 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
6537 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006538 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006539 I40E_FLAG_SRIOV_ENABLED |
6540 I40E_FLAG_FD_SB_ENABLED |
6541 I40E_FLAG_FD_ATR_ENABLED |
6542 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006543
6544 /* rework the queue expectations without MSIX */
6545 i40e_determine_queue_usage(pf);
6546 }
6547 }
6548
6549 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
6550 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006551 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006552 err = pci_enable_msi(pf->pdev);
6553 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00006554 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006555 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
6556 }
6557 }
6558
Shannon Nelson958a3e32013-09-28 07:13:28 +00006559 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006560 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00006561
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006562 /* track first vector for misc interrupts */
6563 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
6564}
6565
6566/**
6567 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
6568 * @pf: board private structure
6569 *
6570 * This sets up the handler for MSIX 0, which is used to manage the
6571 * non-queue interrupts, e.g. AdminQ and errors. This is not used
6572 * when in MSI or Legacy interrupt mode.
6573 **/
6574static int i40e_setup_misc_vector(struct i40e_pf *pf)
6575{
6576 struct i40e_hw *hw = &pf->hw;
6577 int err = 0;
6578
6579 /* Only request the irq if this is the first time through, and
6580 * not when we're rebuilding after a Reset
6581 */
6582 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6583 err = request_irq(pf->msix_entries[0].vector,
6584 i40e_intr, 0, pf->misc_int_name, pf);
6585 if (err) {
6586 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006587 "request_irq for %s failed: %d\n",
6588 pf->misc_int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006589 return -EFAULT;
6590 }
6591 }
6592
6593 i40e_enable_misc_int_causes(hw);
6594
6595 /* associate no queues to the misc vector */
6596 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
6597 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
6598
6599 i40e_flush(hw);
6600
6601 i40e_irq_dynamic_enable_icr0(pf);
6602
6603 return err;
6604}
6605
6606/**
6607 * i40e_config_rss - Prepare for RSS if used
6608 * @pf: board private structure
6609 **/
6610static int i40e_config_rss(struct i40e_pf *pf)
6611{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006612 /* Set of random keys generated using kernel random number generator */
6613 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
6614 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
6615 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
6616 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00006617 struct i40e_hw *hw = &pf->hw;
6618 u32 lut = 0;
6619 int i, j;
6620 u64 hena;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00006621 u32 reg_val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006622
6623 /* Fill out hash function seed */
6624 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
6625 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
6626
6627 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
6628 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
6629 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00006630 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006631 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
6632 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
6633
Carolyn Wybornye157ea32014-06-03 23:50:22 +00006634 /* Check capability and Set table size and register per hw expectation*/
6635 reg_val = rd32(hw, I40E_PFQF_CTL_0);
6636 if (hw->func_caps.rss_table_size == 512) {
6637 reg_val |= I40E_PFQF_CTL_0_HASHLUTSIZE_512;
6638 pf->rss_table_size = 512;
6639 } else {
6640 pf->rss_table_size = 128;
6641 reg_val &= ~I40E_PFQF_CTL_0_HASHLUTSIZE_512;
6642 }
6643 wr32(hw, I40E_PFQF_CTL_0, reg_val);
6644
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006645 /* Populate the LUT with max no. of queues in round robin fashion */
Carolyn Wybornye157ea32014-06-03 23:50:22 +00006646 for (i = 0, j = 0; i < pf->rss_table_size; i++, j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006647
6648 /* The assumption is that lan qp count will be the highest
6649 * qp count for any PF VSI that needs RSS.
6650 * If multiple VSIs need RSS support, all the qp counts
6651 * for those VSIs should be a power of 2 for RSS to work.
6652 * If LAN VSI is the only consumer for RSS then this requirement
6653 * is not necessary.
6654 */
6655 if (j == pf->rss_size)
6656 j = 0;
6657 /* lut = 4-byte sliding window of 4 lut entries */
6658 lut = (lut << 8) | (j &
6659 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
6660 /* On i = 3, we have 4 entries in lut; write to the register */
6661 if ((i & 3) == 3)
6662 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
6663 }
6664 i40e_flush(hw);
6665
6666 return 0;
6667}
6668
6669/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006670 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
6671 * @pf: board private structure
6672 * @queue_count: the requested queue count for rss.
6673 *
6674 * returns 0 if rss is not enabled, if enabled returns the final rss queue
6675 * count which may be different from the requested queue count.
6676 **/
6677int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
6678{
6679 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
6680 return 0;
6681
6682 queue_count = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006683
6684 if (queue_count != pf->rss_size) {
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006685 i40e_prep_for_reset(pf);
6686
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006687 pf->rss_size = queue_count;
6688
6689 i40e_reset_and_rebuild(pf, true);
6690 i40e_config_rss(pf);
6691 }
6692 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
6693 return pf->rss_size;
6694}
6695
6696/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006697 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
6698 * @pf: board private structure to initialize
6699 *
6700 * i40e_sw_init initializes the Adapter private data structure.
6701 * Fields are initialized based on PCI device information and
6702 * OS network device settings (MTU size).
6703 **/
6704static int i40e_sw_init(struct i40e_pf *pf)
6705{
6706 int err = 0;
6707 int size;
6708
6709 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
6710 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00006711 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006712 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
6713 if (I40E_DEBUG_USER & debug)
6714 pf->hw.debug_mask = debug;
6715 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
6716 I40E_DEFAULT_MSG_ENABLE);
6717 }
6718
6719 /* Set default capability flags */
6720 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
6721 I40E_FLAG_MSI_ENABLED |
6722 I40E_FLAG_MSIX_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006723 I40E_FLAG_RX_1BUF_ENABLED;
6724
Mitch Williamsca99eb92014-04-04 04:43:07 +00006725 /* Set default ITR */
6726 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
6727 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
6728
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006729 /* Depending on PF configurations, it is possible that the RSS
6730 * maximum might end up larger than the available queues
6731 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006732 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006733 pf->rss_size_max = min_t(int, pf->rss_size_max,
6734 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006735 if (pf->hw.func_caps.rss) {
6736 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00006737 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006738 } else {
6739 pf->rss_size = 1;
6740 }
6741
Catherine Sullivan2050bc62013-12-18 13:46:03 +00006742 /* MFP mode enabled */
6743 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
6744 pf->flags |= I40E_FLAG_MFP_ENABLED;
6745 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
6746 }
6747
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006748 /* FW/NVM is not yet fixed in this regard */
6749 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
6750 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
6751 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6752 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00006753 /* Setup a counter for fd_atr per pf */
6754 pf->fd_atr_cnt_idx = I40E_FD_ATR_STAT_IDX(pf->hw.pf_id);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006755 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006756 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00006757 /* Setup a counter for fd_sb per pf */
6758 pf->fd_sb_cnt_idx = I40E_FD_SB_STAT_IDX(pf->hw.pf_id);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006759 } else {
6760 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00006761 "Flow Director Sideband mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006762 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006763 pf->fdir_pf_filter_count =
6764 pf->hw.func_caps.fd_filters_guaranteed;
6765 pf->hw.fdir_shared_filter_count =
6766 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006767 }
6768
6769 if (pf->hw.func_caps.vmdq) {
6770 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
6771 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
6772 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
6773 }
6774
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006775#ifdef CONFIG_PCI_IOV
6776 if (pf->hw.func_caps.num_vfs) {
6777 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
6778 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
6779 pf->num_req_vfs = min_t(int,
6780 pf->hw.func_caps.num_vfs,
6781 I40E_MAX_VF_COUNT);
6782 }
6783#endif /* CONFIG_PCI_IOV */
6784 pf->eeprom_version = 0xDEAD;
6785 pf->lan_veb = I40E_NO_VEB;
6786 pf->lan_vsi = I40E_NO_VSI;
6787
6788 /* set up queue assignment tracking */
6789 size = sizeof(struct i40e_lump_tracking)
6790 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
6791 pf->qp_pile = kzalloc(size, GFP_KERNEL);
6792 if (!pf->qp_pile) {
6793 err = -ENOMEM;
6794 goto sw_init_done;
6795 }
6796 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
6797 pf->qp_pile->search_hint = 0;
6798
6799 /* set up vector assignment tracking */
6800 size = sizeof(struct i40e_lump_tracking)
6801 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
6802 pf->irq_pile = kzalloc(size, GFP_KERNEL);
6803 if (!pf->irq_pile) {
6804 kfree(pf->qp_pile);
6805 err = -ENOMEM;
6806 goto sw_init_done;
6807 }
6808 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
6809 pf->irq_pile->search_hint = 0;
6810
6811 mutex_init(&pf->switch_mutex);
6812
6813sw_init_done:
6814 return err;
6815}
6816
6817/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006818 * i40e_set_ntuple - set the ntuple feature flag and take action
6819 * @pf: board private structure to initialize
6820 * @features: the feature set that the stack is suggesting
6821 *
6822 * returns a bool to indicate if reset needs to happen
6823 **/
6824bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
6825{
6826 bool need_reset = false;
6827
6828 /* Check if Flow Director n-tuple support was enabled or disabled. If
6829 * the state changed, we need to reset.
6830 */
6831 if (features & NETIF_F_NTUPLE) {
6832 /* Enable filters and mark for reset */
6833 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
6834 need_reset = true;
6835 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
6836 } else {
6837 /* turn off filters, mark for reset and clear SW filter list */
6838 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
6839 need_reset = true;
6840 i40e_fdir_filter_exit(pf);
6841 }
6842 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6843 /* if ATR was disabled it can be re-enabled. */
6844 if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
6845 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6846 }
6847 return need_reset;
6848}
6849
6850/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006851 * i40e_set_features - set the netdev feature flags
6852 * @netdev: ptr to the netdev being adjusted
6853 * @features: the feature set that the stack is suggesting
6854 **/
6855static int i40e_set_features(struct net_device *netdev,
6856 netdev_features_t features)
6857{
6858 struct i40e_netdev_priv *np = netdev_priv(netdev);
6859 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006860 struct i40e_pf *pf = vsi->back;
6861 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006862
6863 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6864 i40e_vlan_stripping_enable(vsi);
6865 else
6866 i40e_vlan_stripping_disable(vsi);
6867
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006868 need_reset = i40e_set_ntuple(pf, features);
6869
6870 if (need_reset)
6871 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
6872
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006873 return 0;
6874}
6875
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006876#ifdef CONFIG_I40E_VXLAN
6877/**
6878 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
6879 * @pf: board private structure
6880 * @port: The UDP port to look up
6881 *
6882 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
6883 **/
6884static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
6885{
6886 u8 i;
6887
6888 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6889 if (pf->vxlan_ports[i] == port)
6890 return i;
6891 }
6892
6893 return i;
6894}
6895
6896/**
6897 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
6898 * @netdev: This physical port's netdev
6899 * @sa_family: Socket Family that VXLAN is notifying us about
6900 * @port: New UDP port number that VXLAN started listening to
6901 **/
6902static void i40e_add_vxlan_port(struct net_device *netdev,
6903 sa_family_t sa_family, __be16 port)
6904{
6905 struct i40e_netdev_priv *np = netdev_priv(netdev);
6906 struct i40e_vsi *vsi = np->vsi;
6907 struct i40e_pf *pf = vsi->back;
6908 u8 next_idx;
6909 u8 idx;
6910
6911 if (sa_family == AF_INET6)
6912 return;
6913
6914 idx = i40e_get_vxlan_port_idx(pf, port);
6915
6916 /* Check if port already exists */
6917 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6918 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
6919 return;
6920 }
6921
6922 /* Now check if there is space to add the new port */
6923 next_idx = i40e_get_vxlan_port_idx(pf, 0);
6924
6925 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6926 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
6927 ntohs(port));
6928 return;
6929 }
6930
6931 /* New port: add it and mark its index in the bitmap */
6932 pf->vxlan_ports[next_idx] = port;
6933 pf->pending_vxlan_bitmap |= (1 << next_idx);
6934
6935 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6936}
6937
6938/**
6939 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
6940 * @netdev: This physical port's netdev
6941 * @sa_family: Socket Family that VXLAN is notifying us about
6942 * @port: UDP port number that VXLAN stopped listening to
6943 **/
6944static void i40e_del_vxlan_port(struct net_device *netdev,
6945 sa_family_t sa_family, __be16 port)
6946{
6947 struct i40e_netdev_priv *np = netdev_priv(netdev);
6948 struct i40e_vsi *vsi = np->vsi;
6949 struct i40e_pf *pf = vsi->back;
6950 u8 idx;
6951
6952 if (sa_family == AF_INET6)
6953 return;
6954
6955 idx = i40e_get_vxlan_port_idx(pf, port);
6956
6957 /* Check if port already exists */
6958 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6959 /* if port exists, set it to 0 (mark for deletion)
6960 * and make it pending
6961 */
6962 pf->vxlan_ports[idx] = 0;
6963
6964 pf->pending_vxlan_bitmap |= (1 << idx);
6965
6966 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6967 } else {
6968 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6969 ntohs(port));
6970 }
6971}
6972
6973#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006974#ifdef HAVE_FDB_OPS
6975#ifdef USE_CONST_DEV_UC_CHAR
6976static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
6977 struct net_device *dev,
6978 const unsigned char *addr,
6979 u16 flags)
6980#else
6981static int i40e_ndo_fdb_add(struct ndmsg *ndm,
6982 struct net_device *dev,
6983 unsigned char *addr,
6984 u16 flags)
6985#endif
6986{
6987 struct i40e_netdev_priv *np = netdev_priv(dev);
6988 struct i40e_pf *pf = np->vsi->back;
6989 int err = 0;
6990
6991 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
6992 return -EOPNOTSUPP;
6993
6994 /* Hardware does not support aging addresses so if a
6995 * ndm_state is given only allow permanent addresses
6996 */
6997 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6998 netdev_info(dev, "FDB only supports static addresses\n");
6999 return -EINVAL;
7000 }
7001
7002 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
7003 err = dev_uc_add_excl(dev, addr);
7004 else if (is_multicast_ether_addr(addr))
7005 err = dev_mc_add_excl(dev, addr);
7006 else
7007 err = -EINVAL;
7008
7009 /* Only return duplicate errors if NLM_F_EXCL is set */
7010 if (err == -EEXIST && !(flags & NLM_F_EXCL))
7011 err = 0;
7012
7013 return err;
7014}
7015
7016#ifndef USE_DEFAULT_FDB_DEL_DUMP
7017#ifdef USE_CONST_DEV_UC_CHAR
7018static int i40e_ndo_fdb_del(struct ndmsg *ndm,
7019 struct net_device *dev,
7020 const unsigned char *addr)
7021#else
7022static int i40e_ndo_fdb_del(struct ndmsg *ndm,
7023 struct net_device *dev,
7024 unsigned char *addr)
7025#endif
7026{
7027 struct i40e_netdev_priv *np = netdev_priv(dev);
7028 struct i40e_pf *pf = np->vsi->back;
7029 int err = -EOPNOTSUPP;
7030
7031 if (ndm->ndm_state & NUD_PERMANENT) {
7032 netdev_info(dev, "FDB only supports static addresses\n");
7033 return -EINVAL;
7034 }
7035
7036 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
7037 if (is_unicast_ether_addr(addr))
7038 err = dev_uc_del(dev, addr);
7039 else if (is_multicast_ether_addr(addr))
7040 err = dev_mc_del(dev, addr);
7041 else
7042 err = -EINVAL;
7043 }
7044
7045 return err;
7046}
7047
7048static int i40e_ndo_fdb_dump(struct sk_buff *skb,
7049 struct netlink_callback *cb,
7050 struct net_device *dev,
7051 int idx)
7052{
7053 struct i40e_netdev_priv *np = netdev_priv(dev);
7054 struct i40e_pf *pf = np->vsi->back;
7055
7056 if (pf->flags & I40E_FLAG_SRIOV_ENABLED)
7057 idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
7058
7059 return idx;
7060}
7061
7062#endif /* USE_DEFAULT_FDB_DEL_DUMP */
7063#endif /* HAVE_FDB_OPS */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007064static const struct net_device_ops i40e_netdev_ops = {
7065 .ndo_open = i40e_open,
7066 .ndo_stop = i40e_close,
7067 .ndo_start_xmit = i40e_lan_xmit_frame,
7068 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
7069 .ndo_set_rx_mode = i40e_set_rx_mode,
7070 .ndo_validate_addr = eth_validate_addr,
7071 .ndo_set_mac_address = i40e_set_mac,
7072 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00007073 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007074 .ndo_tx_timeout = i40e_tx_timeout,
7075 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
7076 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
7077#ifdef CONFIG_NET_POLL_CONTROLLER
7078 .ndo_poll_controller = i40e_netpoll,
7079#endif
7080 .ndo_setup_tc = i40e_setup_tc,
7081 .ndo_set_features = i40e_set_features,
7082 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
7083 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04007084 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007085 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00007086 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Mitch Williamsc674d122014-05-20 08:01:40 +00007087 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofck,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007088#ifdef CONFIG_I40E_VXLAN
7089 .ndo_add_vxlan_port = i40e_add_vxlan_port,
7090 .ndo_del_vxlan_port = i40e_del_vxlan_port,
7091#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00007092#ifdef HAVE_FDB_OPS
7093 .ndo_fdb_add = i40e_ndo_fdb_add,
7094#ifndef USE_DEFAULT_FDB_DEL_DUMP
7095 .ndo_fdb_del = i40e_ndo_fdb_del,
7096 .ndo_fdb_dump = i40e_ndo_fdb_dump,
7097#endif
7098#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007099};
7100
7101/**
7102 * i40e_config_netdev - Setup the netdev flags
7103 * @vsi: the VSI being configured
7104 *
7105 * Returns 0 on success, negative value on failure
7106 **/
7107static int i40e_config_netdev(struct i40e_vsi *vsi)
7108{
Greg Rose1a103702013-11-28 06:42:39 +00007109 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007110 struct i40e_pf *pf = vsi->back;
7111 struct i40e_hw *hw = &pf->hw;
7112 struct i40e_netdev_priv *np;
7113 struct net_device *netdev;
7114 u8 mac_addr[ETH_ALEN];
7115 int etherdev_size;
7116
7117 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007118 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007119 if (!netdev)
7120 return -ENOMEM;
7121
7122 vsi->netdev = netdev;
7123 np = netdev_priv(netdev);
7124 np->vsi = vsi;
7125
Or Gerlitzd70e9412014-03-18 10:36:45 +02007126 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007127 NETIF_F_GSO_UDP_TUNNEL |
Or Gerlitzd70e9412014-03-18 10:36:45 +02007128 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007129
7130 netdev->features = NETIF_F_SG |
7131 NETIF_F_IP_CSUM |
7132 NETIF_F_SCTP_CSUM |
7133 NETIF_F_HIGHDMA |
7134 NETIF_F_GSO_UDP_TUNNEL |
7135 NETIF_F_HW_VLAN_CTAG_TX |
7136 NETIF_F_HW_VLAN_CTAG_RX |
7137 NETIF_F_HW_VLAN_CTAG_FILTER |
7138 NETIF_F_IPV6_CSUM |
7139 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00007140 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007141 NETIF_F_TSO6 |
7142 NETIF_F_RXCSUM |
7143 NETIF_F_RXHASH |
7144 0;
7145
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00007146 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
7147 netdev->features |= NETIF_F_NTUPLE;
7148
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007149 /* copy netdev features into list of user selectable features */
7150 netdev->hw_features |= netdev->features;
7151
7152 if (vsi->type == I40E_VSI_MAIN) {
7153 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00007154 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Greg Rose8c27d422014-05-22 06:31:56 +00007155 /* The following two steps are necessary to prevent reception
7156 * of tagged packets - by default the NVM loads a MAC-VLAN
7157 * filter that will accept any tagged packet. This is to
7158 * prevent that during normal operations until a specific
7159 * VLAN tag filter has been set.
7160 */
7161 i40e_rm_default_mac_filter(vsi, mac_addr);
7162 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007163 } else {
7164 /* relate the VSI_VMDQ name to the VSI_MAIN name */
7165 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
7166 pf->vsi[pf->lan_vsi]->netdev->name);
7167 random_ether_addr(mac_addr);
7168 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
7169 }
Greg Rose1a103702013-11-28 06:42:39 +00007170 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007171
Greg Rose9a173902014-05-22 06:32:02 +00007172 ether_addr_copy(netdev->dev_addr, mac_addr);
7173 ether_addr_copy(netdev->perm_addr, mac_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007174 /* vlan gets same features (except vlan offload)
7175 * after any tweaks for specific VSI types
7176 */
7177 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
7178 NETIF_F_HW_VLAN_CTAG_RX |
7179 NETIF_F_HW_VLAN_CTAG_FILTER);
7180 netdev->priv_flags |= IFF_UNICAST_FLT;
7181 netdev->priv_flags |= IFF_SUPP_NOFCS;
7182 /* Setup netdev TC information */
7183 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
7184
7185 netdev->netdev_ops = &i40e_netdev_ops;
7186 netdev->watchdog_timeo = 5 * HZ;
7187 i40e_set_ethtool_ops(netdev);
7188
7189 return 0;
7190}
7191
7192/**
7193 * i40e_vsi_delete - Delete a VSI from the switch
7194 * @vsi: the VSI being removed
7195 *
7196 * Returns 0 on success, negative value on failure
7197 **/
7198static void i40e_vsi_delete(struct i40e_vsi *vsi)
7199{
7200 /* remove default VSI is not allowed */
7201 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
7202 return;
7203
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007204 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007205}
7206
7207/**
7208 * i40e_add_vsi - Add a VSI to the switch
7209 * @vsi: the VSI being configured
7210 *
7211 * This initializes a VSI context depending on the VSI type to be added and
7212 * passes it down to the add_vsi aq command.
7213 **/
7214static int i40e_add_vsi(struct i40e_vsi *vsi)
7215{
7216 int ret = -ENODEV;
7217 struct i40e_mac_filter *f, *ftmp;
7218 struct i40e_pf *pf = vsi->back;
7219 struct i40e_hw *hw = &pf->hw;
7220 struct i40e_vsi_context ctxt;
7221 u8 enabled_tc = 0x1; /* TC0 enabled */
7222 int f_count = 0;
7223
7224 memset(&ctxt, 0, sizeof(ctxt));
7225 switch (vsi->type) {
7226 case I40E_VSI_MAIN:
7227 /* The PF's main VSI is already setup as part of the
7228 * device initialization, so we'll not bother with
7229 * the add_vsi call, but we will retrieve the current
7230 * VSI context.
7231 */
7232 ctxt.seid = pf->main_vsi_seid;
7233 ctxt.pf_num = pf->hw.pf_id;
7234 ctxt.vf_num = 0;
7235 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
7236 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
7237 if (ret) {
7238 dev_info(&pf->pdev->dev,
7239 "couldn't get pf vsi config, err %d, aq_err %d\n",
7240 ret, pf->hw.aq.asq_last_status);
7241 return -ENOENT;
7242 }
7243 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
7244 vsi->info.valid_sections = 0;
7245
7246 vsi->seid = ctxt.seid;
7247 vsi->id = ctxt.vsi_number;
7248
7249 enabled_tc = i40e_pf_get_tc_map(pf);
7250
7251 /* MFP mode setup queue map and update VSI */
7252 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7253 memset(&ctxt, 0, sizeof(ctxt));
7254 ctxt.seid = pf->main_vsi_seid;
7255 ctxt.pf_num = pf->hw.pf_id;
7256 ctxt.vf_num = 0;
7257 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
7258 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7259 if (ret) {
7260 dev_info(&pf->pdev->dev,
7261 "update vsi failed, aq_err=%d\n",
7262 pf->hw.aq.asq_last_status);
7263 ret = -ENOENT;
7264 goto err;
7265 }
7266 /* update the local VSI info queue map */
7267 i40e_vsi_update_queue_map(vsi, &ctxt);
7268 vsi->info.valid_sections = 0;
7269 } else {
7270 /* Default/Main VSI is only enabled for TC0
7271 * reconfigure it to enable all TCs that are
7272 * available on the port in SFP mode.
7273 */
7274 ret = i40e_vsi_config_tc(vsi, enabled_tc);
7275 if (ret) {
7276 dev_info(&pf->pdev->dev,
7277 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
7278 enabled_tc, ret,
7279 pf->hw.aq.asq_last_status);
7280 ret = -ENOENT;
7281 }
7282 }
7283 break;
7284
7285 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007286 ctxt.pf_num = hw->pf_id;
7287 ctxt.vf_num = 0;
7288 ctxt.uplink_seid = vsi->uplink_seid;
7289 ctxt.connection_type = 0x1; /* regular data port */
7290 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007291 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007292 break;
7293
7294 case I40E_VSI_VMDQ2:
7295 ctxt.pf_num = hw->pf_id;
7296 ctxt.vf_num = 0;
7297 ctxt.uplink_seid = vsi->uplink_seid;
7298 ctxt.connection_type = 0x1; /* regular data port */
7299 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
7300
7301 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7302
7303 /* This VSI is connected to VEB so the switch_id
7304 * should be set to zero by default.
7305 */
7306 ctxt.info.switch_id = 0;
7307 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
7308 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7309
7310 /* Setup the VSI tx/rx queue map for TC0 only for now */
7311 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
7312 break;
7313
7314 case I40E_VSI_SRIOV:
7315 ctxt.pf_num = hw->pf_id;
7316 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
7317 ctxt.uplink_seid = vsi->uplink_seid;
7318 ctxt.connection_type = 0x1; /* regular data port */
7319 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
7320
7321 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7322
7323 /* This VSI is connected to VEB so the switch_id
7324 * should be set to zero by default.
7325 */
7326 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7327
7328 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
7329 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00007330 if (pf->vf[vsi->vf_id].spoofchk) {
7331 ctxt.info.valid_sections |=
7332 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
7333 ctxt.info.sec_flags |=
7334 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
7335 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
7336 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007337 /* Setup the VSI tx/rx queue map for TC0 only for now */
7338 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
7339 break;
7340
7341 default:
7342 return -ENODEV;
7343 }
7344
7345 if (vsi->type != I40E_VSI_MAIN) {
7346 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
7347 if (ret) {
7348 dev_info(&vsi->back->pdev->dev,
7349 "add vsi failed, aq_err=%d\n",
7350 vsi->back->hw.aq.asq_last_status);
7351 ret = -ENOENT;
7352 goto err;
7353 }
7354 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
7355 vsi->info.valid_sections = 0;
7356 vsi->seid = ctxt.seid;
7357 vsi->id = ctxt.vsi_number;
7358 }
7359
7360 /* If macvlan filters already exist, force them to get loaded */
7361 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
7362 f->changed = true;
7363 f_count++;
7364 }
7365 if (f_count) {
7366 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
7367 pf->flags |= I40E_FLAG_FILTER_SYNC;
7368 }
7369
7370 /* Update VSI BW information */
7371 ret = i40e_vsi_get_bw_info(vsi);
7372 if (ret) {
7373 dev_info(&pf->pdev->dev,
7374 "couldn't get vsi bw info, err %d, aq_err %d\n",
7375 ret, pf->hw.aq.asq_last_status);
7376 /* VSI is already added so not tearing that up */
7377 ret = 0;
7378 }
7379
7380err:
7381 return ret;
7382}
7383
7384/**
7385 * i40e_vsi_release - Delete a VSI and free its resources
7386 * @vsi: the VSI being removed
7387 *
7388 * Returns 0 on success or < 0 on error
7389 **/
7390int i40e_vsi_release(struct i40e_vsi *vsi)
7391{
7392 struct i40e_mac_filter *f, *ftmp;
7393 struct i40e_veb *veb = NULL;
7394 struct i40e_pf *pf;
7395 u16 uplink_seid;
7396 int i, n;
7397
7398 pf = vsi->back;
7399
7400 /* release of a VEB-owner or last VSI is not allowed */
7401 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
7402 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
7403 vsi->seid, vsi->uplink_seid);
7404 return -ENODEV;
7405 }
7406 if (vsi == pf->vsi[pf->lan_vsi] &&
7407 !test_bit(__I40E_DOWN, &pf->state)) {
7408 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
7409 return -ENODEV;
7410 }
7411
7412 uplink_seid = vsi->uplink_seid;
7413 if (vsi->type != I40E_VSI_SRIOV) {
7414 if (vsi->netdev_registered) {
7415 vsi->netdev_registered = false;
7416 if (vsi->netdev) {
7417 /* results in a call to i40e_close() */
7418 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007419 }
7420 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00007421 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007422 }
7423 i40e_vsi_disable_irq(vsi);
7424 }
7425
7426 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
7427 i40e_del_filter(vsi, f->macaddr, f->vlan,
7428 f->is_vf, f->is_netdev);
7429 i40e_sync_vsi_filters(vsi);
7430
7431 i40e_vsi_delete(vsi);
7432 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00007433 if (vsi->netdev) {
7434 free_netdev(vsi->netdev);
7435 vsi->netdev = NULL;
7436 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007437 i40e_vsi_clear_rings(vsi);
7438 i40e_vsi_clear(vsi);
7439
7440 /* If this was the last thing on the VEB, except for the
7441 * controlling VSI, remove the VEB, which puts the controlling
7442 * VSI onto the next level down in the switch.
7443 *
7444 * Well, okay, there's one more exception here: don't remove
7445 * the orphan VEBs yet. We'll wait for an explicit remove request
7446 * from up the network stack.
7447 */
Mitch Williams505682c2014-05-20 08:01:37 +00007448 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007449 if (pf->vsi[i] &&
7450 pf->vsi[i]->uplink_seid == uplink_seid &&
7451 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7452 n++; /* count the VSIs */
7453 }
7454 }
7455 for (i = 0; i < I40E_MAX_VEB; i++) {
7456 if (!pf->veb[i])
7457 continue;
7458 if (pf->veb[i]->uplink_seid == uplink_seid)
7459 n++; /* count the VEBs */
7460 if (pf->veb[i]->seid == uplink_seid)
7461 veb = pf->veb[i];
7462 }
7463 if (n == 0 && veb && veb->uplink_seid != 0)
7464 i40e_veb_release(veb);
7465
7466 return 0;
7467}
7468
7469/**
7470 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
7471 * @vsi: ptr to the VSI
7472 *
7473 * This should only be called after i40e_vsi_mem_alloc() which allocates the
7474 * corresponding SW VSI structure and initializes num_queue_pairs for the
7475 * newly allocated VSI.
7476 *
7477 * Returns 0 on success or negative on failure
7478 **/
7479static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
7480{
7481 int ret = -ENOENT;
7482 struct i40e_pf *pf = vsi->back;
7483
Alexander Duyck493fb302013-09-28 07:01:44 +00007484 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007485 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
7486 vsi->seid);
7487 return -EEXIST;
7488 }
7489
7490 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007491 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007492 vsi->seid, vsi->base_vector);
7493 return -EEXIST;
7494 }
7495
Greg Rose90e04072014-03-06 08:59:57 +00007496 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007497 if (ret) {
7498 dev_info(&pf->pdev->dev,
7499 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
7500 vsi->num_q_vectors, vsi->seid, ret);
7501 vsi->num_q_vectors = 0;
7502 goto vector_setup_out;
7503 }
7504
Shannon Nelson958a3e32013-09-28 07:13:28 +00007505 if (vsi->num_q_vectors)
7506 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
7507 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007508 if (vsi->base_vector < 0) {
7509 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007510 "failed to get queue tracking for VSI %d, err=%d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007511 vsi->seid, vsi->base_vector);
7512 i40e_vsi_free_q_vectors(vsi);
7513 ret = -ENOENT;
7514 goto vector_setup_out;
7515 }
7516
7517vector_setup_out:
7518 return ret;
7519}
7520
7521/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007522 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
7523 * @vsi: pointer to the vsi.
7524 *
7525 * This re-allocates a vsi's queue resources.
7526 *
7527 * Returns pointer to the successfully allocated and configured VSI sw struct
7528 * on success, otherwise returns NULL on failure.
7529 **/
7530static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
7531{
7532 struct i40e_pf *pf = vsi->back;
7533 u8 enabled_tc;
7534 int ret;
7535
7536 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7537 i40e_vsi_clear_rings(vsi);
7538
7539 i40e_vsi_free_arrays(vsi, false);
7540 i40e_set_num_rings_in_vsi(vsi);
7541 ret = i40e_vsi_alloc_arrays(vsi, false);
7542 if (ret)
7543 goto err_vsi;
7544
7545 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
7546 if (ret < 0) {
7547 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7548 vsi->seid, ret);
7549 goto err_vsi;
7550 }
7551 vsi->base_queue = ret;
7552
7553 /* Update the FW view of the VSI. Force a reset of TC and queue
7554 * layout configurations.
7555 */
7556 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7557 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7558 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7559 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7560
7561 /* assign it some queues */
7562 ret = i40e_alloc_rings(vsi);
7563 if (ret)
7564 goto err_rings;
7565
7566 /* map all of the rings to the q_vectors */
7567 i40e_vsi_map_rings_to_vectors(vsi);
7568 return vsi;
7569
7570err_rings:
7571 i40e_vsi_free_q_vectors(vsi);
7572 if (vsi->netdev_registered) {
7573 vsi->netdev_registered = false;
7574 unregister_netdev(vsi->netdev);
7575 free_netdev(vsi->netdev);
7576 vsi->netdev = NULL;
7577 }
7578 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7579err_vsi:
7580 i40e_vsi_clear(vsi);
7581 return NULL;
7582}
7583
7584/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007585 * i40e_vsi_setup - Set up a VSI by a given type
7586 * @pf: board private structure
7587 * @type: VSI type
7588 * @uplink_seid: the switch element to link to
7589 * @param1: usage depends upon VSI type. For VF types, indicates VF id
7590 *
7591 * This allocates the sw VSI structure and its queue resources, then add a VSI
7592 * to the identified VEB.
7593 *
7594 * Returns pointer to the successfully allocated and configure VSI sw struct on
7595 * success, otherwise returns NULL on failure.
7596 **/
7597struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
7598 u16 uplink_seid, u32 param1)
7599{
7600 struct i40e_vsi *vsi = NULL;
7601 struct i40e_veb *veb = NULL;
7602 int ret, i;
7603 int v_idx;
7604
7605 /* The requested uplink_seid must be either
7606 * - the PF's port seid
7607 * no VEB is needed because this is the PF
7608 * or this is a Flow Director special case VSI
7609 * - seid of an existing VEB
7610 * - seid of a VSI that owns an existing VEB
7611 * - seid of a VSI that doesn't own a VEB
7612 * a new VEB is created and the VSI becomes the owner
7613 * - seid of the PF VSI, which is what creates the first VEB
7614 * this is a special case of the previous
7615 *
7616 * Find which uplink_seid we were given and create a new VEB if needed
7617 */
7618 for (i = 0; i < I40E_MAX_VEB; i++) {
7619 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
7620 veb = pf->veb[i];
7621 break;
7622 }
7623 }
7624
7625 if (!veb && uplink_seid != pf->mac_seid) {
7626
Mitch Williams505682c2014-05-20 08:01:37 +00007627 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007628 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
7629 vsi = pf->vsi[i];
7630 break;
7631 }
7632 }
7633 if (!vsi) {
7634 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
7635 uplink_seid);
7636 return NULL;
7637 }
7638
7639 if (vsi->uplink_seid == pf->mac_seid)
7640 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
7641 vsi->tc_config.enabled_tc);
7642 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
7643 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
7644 vsi->tc_config.enabled_tc);
7645
7646 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
7647 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
7648 veb = pf->veb[i];
7649 }
7650 if (!veb) {
7651 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
7652 return NULL;
7653 }
7654
7655 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7656 uplink_seid = veb->seid;
7657 }
7658
7659 /* get vsi sw struct */
7660 v_idx = i40e_vsi_mem_alloc(pf, type);
7661 if (v_idx < 0)
7662 goto err_alloc;
7663 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007664 if (!vsi)
7665 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007666 vsi->type = type;
7667 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
7668
7669 if (type == I40E_VSI_MAIN)
7670 pf->lan_vsi = v_idx;
7671 else if (type == I40E_VSI_SRIOV)
7672 vsi->vf_id = param1;
7673 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007674 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
7675 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007676 if (ret < 0) {
7677 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7678 vsi->seid, ret);
7679 goto err_vsi;
7680 }
7681 vsi->base_queue = ret;
7682
7683 /* get a VSI from the hardware */
7684 vsi->uplink_seid = uplink_seid;
7685 ret = i40e_add_vsi(vsi);
7686 if (ret)
7687 goto err_vsi;
7688
7689 switch (vsi->type) {
7690 /* setup the netdev if needed */
7691 case I40E_VSI_MAIN:
7692 case I40E_VSI_VMDQ2:
7693 ret = i40e_config_netdev(vsi);
7694 if (ret)
7695 goto err_netdev;
7696 ret = register_netdev(vsi->netdev);
7697 if (ret)
7698 goto err_netdev;
7699 vsi->netdev_registered = true;
7700 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007701#ifdef CONFIG_I40E_DCB
7702 /* Setup DCB netlink interface */
7703 i40e_dcbnl_setup(vsi);
7704#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007705 /* fall through */
7706
7707 case I40E_VSI_FDIR:
7708 /* set up vectors and rings if needed */
7709 ret = i40e_vsi_setup_vectors(vsi);
7710 if (ret)
7711 goto err_msix;
7712
7713 ret = i40e_alloc_rings(vsi);
7714 if (ret)
7715 goto err_rings;
7716
7717 /* map all of the rings to the q_vectors */
7718 i40e_vsi_map_rings_to_vectors(vsi);
7719
7720 i40e_vsi_reset_stats(vsi);
7721 break;
7722
7723 default:
7724 /* no netdev or rings for the other VSI types */
7725 break;
7726 }
7727
7728 return vsi;
7729
7730err_rings:
7731 i40e_vsi_free_q_vectors(vsi);
7732err_msix:
7733 if (vsi->netdev_registered) {
7734 vsi->netdev_registered = false;
7735 unregister_netdev(vsi->netdev);
7736 free_netdev(vsi->netdev);
7737 vsi->netdev = NULL;
7738 }
7739err_netdev:
7740 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7741err_vsi:
7742 i40e_vsi_clear(vsi);
7743err_alloc:
7744 return NULL;
7745}
7746
7747/**
7748 * i40e_veb_get_bw_info - Query VEB BW information
7749 * @veb: the veb to query
7750 *
7751 * Query the Tx scheduler BW configuration data for given VEB
7752 **/
7753static int i40e_veb_get_bw_info(struct i40e_veb *veb)
7754{
7755 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
7756 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
7757 struct i40e_pf *pf = veb->pf;
7758 struct i40e_hw *hw = &pf->hw;
7759 u32 tc_bw_max;
7760 int ret = 0;
7761 int i;
7762
7763 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
7764 &bw_data, NULL);
7765 if (ret) {
7766 dev_info(&pf->pdev->dev,
7767 "query veb bw config failed, aq_err=%d\n",
7768 hw->aq.asq_last_status);
7769 goto out;
7770 }
7771
7772 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
7773 &ets_data, NULL);
7774 if (ret) {
7775 dev_info(&pf->pdev->dev,
7776 "query veb bw ets config failed, aq_err=%d\n",
7777 hw->aq.asq_last_status);
7778 goto out;
7779 }
7780
7781 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
7782 veb->bw_max_quanta = ets_data.tc_bw_max;
7783 veb->is_abs_credits = bw_data.absolute_credits_enable;
7784 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
7785 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
7786 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7787 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
7788 veb->bw_tc_limit_credits[i] =
7789 le16_to_cpu(bw_data.tc_bw_limits[i]);
7790 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
7791 }
7792
7793out:
7794 return ret;
7795}
7796
7797/**
7798 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
7799 * @pf: board private structure
7800 *
7801 * On error: returns error code (negative)
7802 * On success: returns vsi index in PF (positive)
7803 **/
7804static int i40e_veb_mem_alloc(struct i40e_pf *pf)
7805{
7806 int ret = -ENOENT;
7807 struct i40e_veb *veb;
7808 int i;
7809
7810 /* Need to protect the allocation of switch elements at the PF level */
7811 mutex_lock(&pf->switch_mutex);
7812
7813 /* VEB list may be fragmented if VEB creation/destruction has
7814 * been happening. We can afford to do a quick scan to look
7815 * for any free slots in the list.
7816 *
7817 * find next empty veb slot, looping back around if necessary
7818 */
7819 i = 0;
7820 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
7821 i++;
7822 if (i >= I40E_MAX_VEB) {
7823 ret = -ENOMEM;
7824 goto err_alloc_veb; /* out of VEB slots! */
7825 }
7826
7827 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
7828 if (!veb) {
7829 ret = -ENOMEM;
7830 goto err_alloc_veb;
7831 }
7832 veb->pf = pf;
7833 veb->idx = i;
7834 veb->enabled_tc = 1;
7835
7836 pf->veb[i] = veb;
7837 ret = i;
7838err_alloc_veb:
7839 mutex_unlock(&pf->switch_mutex);
7840 return ret;
7841}
7842
7843/**
7844 * i40e_switch_branch_release - Delete a branch of the switch tree
7845 * @branch: where to start deleting
7846 *
7847 * This uses recursion to find the tips of the branch to be
7848 * removed, deleting until we get back to and can delete this VEB.
7849 **/
7850static void i40e_switch_branch_release(struct i40e_veb *branch)
7851{
7852 struct i40e_pf *pf = branch->pf;
7853 u16 branch_seid = branch->seid;
7854 u16 veb_idx = branch->idx;
7855 int i;
7856
7857 /* release any VEBs on this VEB - RECURSION */
7858 for (i = 0; i < I40E_MAX_VEB; i++) {
7859 if (!pf->veb[i])
7860 continue;
7861 if (pf->veb[i]->uplink_seid == branch->seid)
7862 i40e_switch_branch_release(pf->veb[i]);
7863 }
7864
7865 /* Release the VSIs on this VEB, but not the owner VSI.
7866 *
7867 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
7868 * the VEB itself, so don't use (*branch) after this loop.
7869 */
Mitch Williams505682c2014-05-20 08:01:37 +00007870 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007871 if (!pf->vsi[i])
7872 continue;
7873 if (pf->vsi[i]->uplink_seid == branch_seid &&
7874 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7875 i40e_vsi_release(pf->vsi[i]);
7876 }
7877 }
7878
7879 /* There's one corner case where the VEB might not have been
7880 * removed, so double check it here and remove it if needed.
7881 * This case happens if the veb was created from the debugfs
7882 * commands and no VSIs were added to it.
7883 */
7884 if (pf->veb[veb_idx])
7885 i40e_veb_release(pf->veb[veb_idx]);
7886}
7887
7888/**
7889 * i40e_veb_clear - remove veb struct
7890 * @veb: the veb to remove
7891 **/
7892static void i40e_veb_clear(struct i40e_veb *veb)
7893{
7894 if (!veb)
7895 return;
7896
7897 if (veb->pf) {
7898 struct i40e_pf *pf = veb->pf;
7899
7900 mutex_lock(&pf->switch_mutex);
7901 if (pf->veb[veb->idx] == veb)
7902 pf->veb[veb->idx] = NULL;
7903 mutex_unlock(&pf->switch_mutex);
7904 }
7905
7906 kfree(veb);
7907}
7908
7909/**
7910 * i40e_veb_release - Delete a VEB and free its resources
7911 * @veb: the VEB being removed
7912 **/
7913void i40e_veb_release(struct i40e_veb *veb)
7914{
7915 struct i40e_vsi *vsi = NULL;
7916 struct i40e_pf *pf;
7917 int i, n = 0;
7918
7919 pf = veb->pf;
7920
7921 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +00007922 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007923 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
7924 n++;
7925 vsi = pf->vsi[i];
7926 }
7927 }
7928 if (n != 1) {
7929 dev_info(&pf->pdev->dev,
7930 "can't remove VEB %d with %d VSIs left\n",
7931 veb->seid, n);
7932 return;
7933 }
7934
7935 /* move the remaining VSI to uplink veb */
7936 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
7937 if (veb->uplink_seid) {
7938 vsi->uplink_seid = veb->uplink_seid;
7939 if (veb->uplink_seid == pf->mac_seid)
7940 vsi->veb_idx = I40E_NO_VEB;
7941 else
7942 vsi->veb_idx = veb->veb_idx;
7943 } else {
7944 /* floating VEB */
7945 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
7946 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
7947 }
7948
7949 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
7950 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007951}
7952
7953/**
7954 * i40e_add_veb - create the VEB in the switch
7955 * @veb: the VEB to be instantiated
7956 * @vsi: the controlling VSI
7957 **/
7958static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
7959{
Greg Rose56747262013-11-28 06:39:37 +00007960 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00007961 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007962 int ret;
7963
7964 /* get a VEB from the hardware */
7965 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00007966 veb->enabled_tc, is_default,
7967 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007968 if (ret) {
7969 dev_info(&veb->pf->pdev->dev,
7970 "couldn't add VEB, err %d, aq_err %d\n",
7971 ret, veb->pf->hw.aq.asq_last_status);
7972 return -EPERM;
7973 }
7974
7975 /* get statistics counter */
7976 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
7977 &veb->stats_idx, NULL, NULL, NULL);
7978 if (ret) {
7979 dev_info(&veb->pf->pdev->dev,
7980 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
7981 ret, veb->pf->hw.aq.asq_last_status);
7982 return -EPERM;
7983 }
7984 ret = i40e_veb_get_bw_info(veb);
7985 if (ret) {
7986 dev_info(&veb->pf->pdev->dev,
7987 "couldn't get VEB bw info, err %d, aq_err %d\n",
7988 ret, veb->pf->hw.aq.asq_last_status);
7989 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
7990 return -ENOENT;
7991 }
7992
7993 vsi->uplink_seid = veb->seid;
7994 vsi->veb_idx = veb->idx;
7995 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7996
7997 return 0;
7998}
7999
8000/**
8001 * i40e_veb_setup - Set up a VEB
8002 * @pf: board private structure
8003 * @flags: VEB setup flags
8004 * @uplink_seid: the switch element to link to
8005 * @vsi_seid: the initial VSI seid
8006 * @enabled_tc: Enabled TC bit-map
8007 *
8008 * This allocates the sw VEB structure and links it into the switch
8009 * It is possible and legal for this to be a duplicate of an already
8010 * existing VEB. It is also possible for both uplink and vsi seids
8011 * to be zero, in order to create a floating VEB.
8012 *
8013 * Returns pointer to the successfully allocated VEB sw struct on
8014 * success, otherwise returns NULL on failure.
8015 **/
8016struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
8017 u16 uplink_seid, u16 vsi_seid,
8018 u8 enabled_tc)
8019{
8020 struct i40e_veb *veb, *uplink_veb = NULL;
8021 int vsi_idx, veb_idx;
8022 int ret;
8023
8024 /* if one seid is 0, the other must be 0 to create a floating relay */
8025 if ((uplink_seid == 0 || vsi_seid == 0) &&
8026 (uplink_seid + vsi_seid != 0)) {
8027 dev_info(&pf->pdev->dev,
8028 "one, not both seid's are 0: uplink=%d vsi=%d\n",
8029 uplink_seid, vsi_seid);
8030 return NULL;
8031 }
8032
8033 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +00008034 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008035 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
8036 break;
Mitch Williams505682c2014-05-20 08:01:37 +00008037 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008038 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
8039 vsi_seid);
8040 return NULL;
8041 }
8042
8043 if (uplink_seid && uplink_seid != pf->mac_seid) {
8044 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
8045 if (pf->veb[veb_idx] &&
8046 pf->veb[veb_idx]->seid == uplink_seid) {
8047 uplink_veb = pf->veb[veb_idx];
8048 break;
8049 }
8050 }
8051 if (!uplink_veb) {
8052 dev_info(&pf->pdev->dev,
8053 "uplink seid %d not found\n", uplink_seid);
8054 return NULL;
8055 }
8056 }
8057
8058 /* get veb sw struct */
8059 veb_idx = i40e_veb_mem_alloc(pf);
8060 if (veb_idx < 0)
8061 goto err_alloc;
8062 veb = pf->veb[veb_idx];
8063 veb->flags = flags;
8064 veb->uplink_seid = uplink_seid;
8065 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
8066 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
8067
8068 /* create the VEB in the switch */
8069 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
8070 if (ret)
8071 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +00008072 if (vsi_idx == pf->lan_vsi)
8073 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008074
8075 return veb;
8076
8077err_veb:
8078 i40e_veb_clear(veb);
8079err_alloc:
8080 return NULL;
8081}
8082
8083/**
8084 * i40e_setup_pf_switch_element - set pf vars based on switch type
8085 * @pf: board private structure
8086 * @ele: element we are building info from
8087 * @num_reported: total number of elements
8088 * @printconfig: should we print the contents
8089 *
8090 * helper function to assist in extracting a few useful SEID values.
8091 **/
8092static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
8093 struct i40e_aqc_switch_config_element_resp *ele,
8094 u16 num_reported, bool printconfig)
8095{
8096 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
8097 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
8098 u8 element_type = ele->element_type;
8099 u16 seid = le16_to_cpu(ele->seid);
8100
8101 if (printconfig)
8102 dev_info(&pf->pdev->dev,
8103 "type=%d seid=%d uplink=%d downlink=%d\n",
8104 element_type, seid, uplink_seid, downlink_seid);
8105
8106 switch (element_type) {
8107 case I40E_SWITCH_ELEMENT_TYPE_MAC:
8108 pf->mac_seid = seid;
8109 break;
8110 case I40E_SWITCH_ELEMENT_TYPE_VEB:
8111 /* Main VEB? */
8112 if (uplink_seid != pf->mac_seid)
8113 break;
8114 if (pf->lan_veb == I40E_NO_VEB) {
8115 int v;
8116
8117 /* find existing or else empty VEB */
8118 for (v = 0; v < I40E_MAX_VEB; v++) {
8119 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
8120 pf->lan_veb = v;
8121 break;
8122 }
8123 }
8124 if (pf->lan_veb == I40E_NO_VEB) {
8125 v = i40e_veb_mem_alloc(pf);
8126 if (v < 0)
8127 break;
8128 pf->lan_veb = v;
8129 }
8130 }
8131
8132 pf->veb[pf->lan_veb]->seid = seid;
8133 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
8134 pf->veb[pf->lan_veb]->pf = pf;
8135 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
8136 break;
8137 case I40E_SWITCH_ELEMENT_TYPE_VSI:
8138 if (num_reported != 1)
8139 break;
8140 /* This is immediately after a reset so we can assume this is
8141 * the PF's VSI
8142 */
8143 pf->mac_seid = uplink_seid;
8144 pf->pf_seid = downlink_seid;
8145 pf->main_vsi_seid = seid;
8146 if (printconfig)
8147 dev_info(&pf->pdev->dev,
8148 "pf_seid=%d main_vsi_seid=%d\n",
8149 pf->pf_seid, pf->main_vsi_seid);
8150 break;
8151 case I40E_SWITCH_ELEMENT_TYPE_PF:
8152 case I40E_SWITCH_ELEMENT_TYPE_VF:
8153 case I40E_SWITCH_ELEMENT_TYPE_EMP:
8154 case I40E_SWITCH_ELEMENT_TYPE_BMC:
8155 case I40E_SWITCH_ELEMENT_TYPE_PE:
8156 case I40E_SWITCH_ELEMENT_TYPE_PA:
8157 /* ignore these for now */
8158 break;
8159 default:
8160 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
8161 element_type, seid);
8162 break;
8163 }
8164}
8165
8166/**
8167 * i40e_fetch_switch_configuration - Get switch config from firmware
8168 * @pf: board private structure
8169 * @printconfig: should we print the contents
8170 *
8171 * Get the current switch configuration from the device and
8172 * extract a few useful SEID values.
8173 **/
8174int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
8175{
8176 struct i40e_aqc_get_switch_config_resp *sw_config;
8177 u16 next_seid = 0;
8178 int ret = 0;
8179 u8 *aq_buf;
8180 int i;
8181
8182 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
8183 if (!aq_buf)
8184 return -ENOMEM;
8185
8186 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
8187 do {
8188 u16 num_reported, num_total;
8189
8190 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
8191 I40E_AQ_LARGE_BUF,
8192 &next_seid, NULL);
8193 if (ret) {
8194 dev_info(&pf->pdev->dev,
8195 "get switch config failed %d aq_err=%x\n",
8196 ret, pf->hw.aq.asq_last_status);
8197 kfree(aq_buf);
8198 return -ENOENT;
8199 }
8200
8201 num_reported = le16_to_cpu(sw_config->header.num_reported);
8202 num_total = le16_to_cpu(sw_config->header.num_total);
8203
8204 if (printconfig)
8205 dev_info(&pf->pdev->dev,
8206 "header: %d reported %d total\n",
8207 num_reported, num_total);
8208
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008209 for (i = 0; i < num_reported; i++) {
8210 struct i40e_aqc_switch_config_element_resp *ele =
8211 &sw_config->element[i];
8212
8213 i40e_setup_pf_switch_element(pf, ele, num_reported,
8214 printconfig);
8215 }
8216 } while (next_seid != 0);
8217
8218 kfree(aq_buf);
8219 return ret;
8220}
8221
8222/**
8223 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
8224 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008225 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008226 *
8227 * Returns 0 on success, negative value on failure
8228 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008229static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008230{
Jesse Brandeburg895106a2013-11-26 10:49:16 +00008231 u32 rxfc = 0, txfc = 0, rxfc_reg;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008232 int ret;
8233
8234 /* find out what's out there already */
8235 ret = i40e_fetch_switch_configuration(pf, false);
8236 if (ret) {
8237 dev_info(&pf->pdev->dev,
8238 "couldn't fetch switch config, err %d, aq_err %d\n",
8239 ret, pf->hw.aq.asq_last_status);
8240 return ret;
8241 }
8242 i40e_pf_reset_stats(pf);
8243
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008244 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008245 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008246 struct i40e_vsi *vsi = NULL;
8247 u16 uplink_seid;
8248
8249 /* Set up the PF VSI associated with the PF's main VSI
8250 * that is already in the HW switch
8251 */
8252 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
8253 uplink_seid = pf->veb[pf->lan_veb]->seid;
8254 else
8255 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008256 if (pf->lan_vsi == I40E_NO_VSI)
8257 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
8258 else if (reinit)
8259 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008260 if (!vsi) {
8261 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
8262 i40e_fdir_teardown(pf);
8263 return -EAGAIN;
8264 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008265 } else {
8266 /* force a reset of TC and queue layout configurations */
8267 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
8268 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
8269 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
8270 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
8271 }
8272 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
8273
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008274 i40e_fdir_sb_setup(pf);
8275
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008276 /* Setup static PF queue filter control settings */
8277 ret = i40e_setup_pf_filter_control(pf);
8278 if (ret) {
8279 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
8280 ret);
8281 /* Failure here should not stop continuing other steps */
8282 }
8283
8284 /* enable RSS in the HW, even for only one queue, as the stack can use
8285 * the hash
8286 */
8287 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
8288 i40e_config_rss(pf);
8289
8290 /* fill in link information and enable LSE reporting */
8291 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
8292 i40e_link_event(pf);
8293
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008294 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008295 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
8296 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008297 /* requested_mode is set in probe or by ethtool */
8298 if (!pf->fc_autoneg_status)
8299 goto no_autoneg;
8300
8301 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
8302 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008303 pf->hw.fc.current_mode = I40E_FC_FULL;
8304 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
8305 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
8306 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
8307 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
8308 else
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008309 pf->hw.fc.current_mode = I40E_FC_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008310
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008311 /* sync the flow control settings with the auto-neg values */
8312 switch (pf->hw.fc.current_mode) {
8313 case I40E_FC_FULL:
8314 txfc = 1;
8315 rxfc = 1;
8316 break;
8317 case I40E_FC_TX_PAUSE:
8318 txfc = 1;
8319 rxfc = 0;
8320 break;
8321 case I40E_FC_RX_PAUSE:
8322 txfc = 0;
8323 rxfc = 1;
8324 break;
8325 case I40E_FC_NONE:
8326 case I40E_FC_DEFAULT:
8327 txfc = 0;
8328 rxfc = 0;
8329 break;
8330 case I40E_FC_PFC:
8331 /* TBD */
8332 break;
8333 /* no default case, we have to handle all possibilities here */
8334 }
8335
8336 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
8337
8338 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8339 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
8340 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
8341
8342 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
8343
8344 goto fc_complete;
8345
8346no_autoneg:
8347 /* disable L2 flow control, user can turn it on if they wish */
8348 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
8349 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8350 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
8351
8352fc_complete:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008353 i40e_ptp_init(pf);
8354
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008355 return ret;
8356}
8357
8358/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008359 * i40e_determine_queue_usage - Work out queue distribution
8360 * @pf: board private structure
8361 **/
8362static void i40e_determine_queue_usage(struct i40e_pf *pf)
8363{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008364 int queues_left;
8365
8366 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008367
8368 /* Find the max queues to be put into basic use. We'll always be
8369 * using TC0, whether or not DCB is running, and TC0 will get the
8370 * big RSS set.
8371 */
8372 queues_left = pf->hw.func_caps.num_tx_qp;
8373
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008374 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +00008375 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008376 /* one qp for PF, no queues for anything else */
8377 queues_left = 0;
8378 pf->rss_size = pf->num_lan_qps = 1;
8379
8380 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008381 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
8382 I40E_FLAG_FD_SB_ENABLED |
8383 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008384 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008385 I40E_FLAG_SRIOV_ENABLED |
8386 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +00008387 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
8388 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +00008389 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008390 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +00008391 /* one qp for PF */
8392 pf->rss_size = pf->num_lan_qps = 1;
8393 queues_left -= pf->num_lan_qps;
8394
8395 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
8396 I40E_FLAG_FD_SB_ENABLED |
8397 I40E_FLAG_FD_ATR_ENABLED |
8398 I40E_FLAG_DCB_ENABLED |
8399 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008400 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008401 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008402 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008403 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008404 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008405 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
8406 }
8407 pf->num_lan_qps = pf->rss_size_max;
8408 queues_left -= pf->num_lan_qps;
8409 }
8410
8411 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8412 if (queues_left > 1) {
8413 queues_left -= 1; /* save 1 queue for FD */
8414 } else {
8415 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8416 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
8417 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008418 }
8419
8420 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
8421 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008422 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
8423 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008424 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
8425 }
8426
8427 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8428 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
8429 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
8430 (queues_left / pf->num_vmdq_qps));
8431 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
8432 }
8433
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008434 pf->queues_left = queues_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008435}
8436
8437/**
8438 * i40e_setup_pf_filter_control - Setup PF static filter control
8439 * @pf: PF to be setup
8440 *
8441 * i40e_setup_pf_filter_control sets up a pf's initial filter control
8442 * settings. If PE/FCoE are enabled then it will also set the per PF
8443 * based filter sizes required for them. It also enables Flow director,
8444 * ethertype and macvlan type filter settings for the pf.
8445 *
8446 * Returns 0 on success, negative on failure
8447 **/
8448static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
8449{
8450 struct i40e_filter_control_settings *settings = &pf->filter_settings;
8451
8452 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
8453
8454 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008455 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008456 settings->enable_fdir = true;
8457
8458 /* Ethtype and MACVLAN filters enabled for PF */
8459 settings->enable_ethtype = true;
8460 settings->enable_macvlan = true;
8461
8462 if (i40e_set_filter_control(&pf->hw, settings))
8463 return -ENOENT;
8464
8465 return 0;
8466}
8467
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008468#define INFO_STRING_LEN 255
8469static void i40e_print_features(struct i40e_pf *pf)
8470{
8471 struct i40e_hw *hw = &pf->hw;
8472 char *buf, *string;
8473
8474 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
8475 if (!string) {
8476 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
8477 return;
8478 }
8479
8480 buf = string;
8481
8482 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
8483#ifdef CONFIG_PCI_IOV
8484 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
8485#endif
8486 buf += sprintf(buf, "VSIs: %d QP: %d ", pf->hw.func_caps.num_vsis,
8487 pf->vsi[pf->lan_vsi]->num_queue_pairs);
8488
8489 if (pf->flags & I40E_FLAG_RSS_ENABLED)
8490 buf += sprintf(buf, "RSS ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008491 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00008492 buf += sprintf(buf, "FD_ATR ");
8493 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8494 buf += sprintf(buf, "FD_SB ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008495 buf += sprintf(buf, "NTUPLE ");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00008496 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008497 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008498 buf += sprintf(buf, "DCB ");
8499 if (pf->flags & I40E_FLAG_PTP)
8500 buf += sprintf(buf, "PTP ");
8501
8502 BUG_ON(buf > (string + INFO_STRING_LEN));
8503 dev_info(&pf->pdev->dev, "%s\n", string);
8504 kfree(string);
8505}
8506
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008507/**
8508 * i40e_probe - Device initialization routine
8509 * @pdev: PCI device information struct
8510 * @ent: entry in i40e_pci_tbl
8511 *
8512 * i40e_probe initializes a pf identified by a pci_dev structure.
8513 * The OS initialization, configuring of the pf private structure,
8514 * and a hardware reset occur.
8515 *
8516 * Returns 0 on success, negative on failure
8517 **/
8518static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8519{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008520 struct i40e_pf *pf;
8521 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008522 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008523 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008524 int err = 0;
8525 u32 len;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008526 u32 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008527
8528 err = pci_enable_device_mem(pdev);
8529 if (err)
8530 return err;
8531
8532 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +00008533 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00008534 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00008535 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
8536 if (err) {
8537 dev_err(&pdev->dev,
8538 "DMA configuration failed: 0x%x\n", err);
8539 goto err_dma;
8540 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008541 }
8542
8543 /* set up pci connections */
8544 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
8545 IORESOURCE_MEM), i40e_driver_name);
8546 if (err) {
8547 dev_info(&pdev->dev,
8548 "pci_request_selected_regions failed %d\n", err);
8549 goto err_pci_reg;
8550 }
8551
8552 pci_enable_pcie_error_reporting(pdev);
8553 pci_set_master(pdev);
8554
8555 /* Now that we have a PCI connection, we need to do the
8556 * low level device setup. This is primarily setting up
8557 * the Admin Queue structures and then querying for the
8558 * device's current profile information.
8559 */
8560 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
8561 if (!pf) {
8562 err = -ENOMEM;
8563 goto err_pf_alloc;
8564 }
8565 pf->next_vsi = 0;
8566 pf->pdev = pdev;
8567 set_bit(__I40E_DOWN, &pf->state);
8568
8569 hw = &pf->hw;
8570 hw->back = pf;
8571 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
8572 pci_resource_len(pdev, 0));
8573 if (!hw->hw_addr) {
8574 err = -EIO;
8575 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
8576 (unsigned int)pci_resource_start(pdev, 0),
8577 (unsigned int)pci_resource_len(pdev, 0), err);
8578 goto err_ioremap;
8579 }
8580 hw->vendor_id = pdev->vendor;
8581 hw->device_id = pdev->device;
8582 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
8583 hw->subsystem_vendor_id = pdev->subsystem_vendor;
8584 hw->subsystem_device_id = pdev->subsystem_device;
8585 hw->bus.device = PCI_SLOT(pdev->devfn);
8586 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008587 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008588
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008589 /* do a special CORER for clearing PXE mode once at init */
8590 if (hw->revision_id == 0 &&
8591 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
8592 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
8593 i40e_flush(hw);
8594 msleep(200);
8595 pf->corer_count++;
8596
8597 i40e_clear_pxe_mode(hw);
8598 }
8599
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008600 /* Reset here to make sure all is clean and to define PF 'n' */
8601 err = i40e_pf_reset(hw);
8602 if (err) {
8603 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
8604 goto err_pf_reset;
8605 }
8606 pf->pfr_count++;
8607
8608 hw->aq.num_arq_entries = I40E_AQ_LEN;
8609 hw->aq.num_asq_entries = I40E_AQ_LEN;
8610 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8611 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8612 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
8613 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
8614 "%s-pf%d:misc",
8615 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
8616
8617 err = i40e_init_shared_code(hw);
8618 if (err) {
8619 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
8620 goto err_pf_reset;
8621 }
8622
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008623 /* set up a default setting for link flow control */
8624 pf->hw.fc.requested_mode = I40E_FC_NONE;
8625
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008626 err = i40e_init_adminq(hw);
8627 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
8628 if (err) {
8629 dev_info(&pdev->dev,
8630 "init_adminq failed: %d expecting API %02x.%02x\n",
8631 err,
8632 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
8633 goto err_pf_reset;
8634 }
8635
Shannon Nelson278b6f62014-06-04 01:41:03 +00008636 if (hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
8637 dev_info(&pdev->dev,
8638 "Note: FW API version %02x.%02x newer than expected %02x.%02x, recommend driver update.\n",
8639 hw->aq.api_maj_ver, hw->aq.api_min_ver,
8640 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
8641
8642 if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
8643 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR-1))
8644 dev_info(&pdev->dev,
8645 "Note: FW API version %02x.%02x older than expected %02x.%02x, recommend nvm update.\n",
8646 hw->aq.api_maj_ver, hw->aq.api_min_ver,
8647 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
8648
8649
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008650 i40e_verify_eeprom(pf);
8651
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +00008652 /* Rev 0 hardware was never productized */
8653 if (hw->revision_id < 1)
8654 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");
8655
Shannon Nelson6ff4ef82013-12-21 05:44:49 +00008656 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008657 err = i40e_get_capabilities(pf);
8658 if (err)
8659 goto err_adminq_setup;
8660
8661 err = i40e_sw_init(pf);
8662 if (err) {
8663 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
8664 goto err_sw_init;
8665 }
8666
8667 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
8668 hw->func_caps.num_rx_qp,
8669 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
8670 if (err) {
8671 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
8672 goto err_init_lan_hmc;
8673 }
8674
8675 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
8676 if (err) {
8677 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
8678 err = -ENOENT;
8679 goto err_configure_lan_hmc;
8680 }
8681
8682 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00008683 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008684 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
8685 err = -EIO;
8686 goto err_mac_addr;
8687 }
8688 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +00008689 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008690
8691 pci_set_drvdata(pdev, pf);
8692 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008693#ifdef CONFIG_I40E_DCB
8694 err = i40e_init_pf_dcb(pf);
8695 if (err) {
8696 dev_info(&pdev->dev, "init_pf_dcb failed: %d\n", err);
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008697 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Neerav Parikh014269f2014-04-01 07:11:48 +00008698 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008699 }
8700#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008701
8702 /* set up periodic task facility */
8703 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
8704 pf->service_timer_period = HZ;
8705
8706 INIT_WORK(&pf->service_task, i40e_service_task);
8707 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
8708 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
8709 pf->link_check_timeout = jiffies;
8710
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008711 /* WoL defaults to disabled */
8712 pf->wol_en = false;
8713 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
8714
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008715 /* set up the main switch operations */
8716 i40e_determine_queue_usage(pf);
8717 i40e_init_interrupt_scheme(pf);
8718
Mitch Williams505682c2014-05-20 08:01:37 +00008719 /* The number of VSIs reported by the FW is the minimum guaranteed
8720 * to us; HW supports far more and we share the remaining pool with
8721 * the other PFs. We allocate space for more than the guarantee with
8722 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008723 */
Mitch Williams505682c2014-05-20 08:01:37 +00008724 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
8725 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
8726 else
8727 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
8728
8729 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
8730 len = sizeof(struct i40e_vsi *) * pf->num_alloc_vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008731 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00008732 if (!pf->vsi) {
8733 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008734 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00008735 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008736
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008737 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008738 if (err) {
8739 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
8740 goto err_vsis;
8741 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008742 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +00008743 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008744 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
8745 i40e_vsi_open(pf->vsi[i]);
8746 break;
8747 }
8748 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008749
8750 /* The main driver is (mostly) up and happy. We need to set this state
8751 * before setting up the misc vector or we get a race and the vector
8752 * ends up disabled forever.
8753 */
8754 clear_bit(__I40E_DOWN, &pf->state);
8755
8756 /* In case of MSIX we are going to setup the misc vector right here
8757 * to handle admin queue events etc. In case of legacy and MSI
8758 * the misc functionality and queue processing is combined in
8759 * the same vector and that gets setup at open.
8760 */
8761 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8762 err = i40e_setup_misc_vector(pf);
8763 if (err) {
8764 dev_info(&pdev->dev,
8765 "setup of misc vector failed: %d\n", err);
8766 goto err_vsis;
8767 }
8768 }
8769
Greg Rosedf805f62014-04-04 04:43:16 +00008770#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008771 /* prep for VF support */
8772 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008773 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8774 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008775 u32 val;
8776
8777 /* disable link interrupts for VFs */
8778 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
8779 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
8780 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
8781 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -08008782
8783 if (pci_num_vf(pdev)) {
8784 dev_info(&pdev->dev,
8785 "Active VFs found, allocating resources.\n");
8786 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
8787 if (err)
8788 dev_info(&pdev->dev,
8789 "Error %d allocating resources for existing VFs\n",
8790 err);
8791 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008792 }
Greg Rosedf805f62014-04-04 04:43:16 +00008793#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008794
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008795 pfs_found++;
8796
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008797 i40e_dbg_pf_init(pf);
8798
8799 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00008800 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008801
8802 /* since everything's happy, start the service_task timer */
8803 mod_timer(&pf->service_timer,
8804 round_jiffies(jiffies + pf->service_timer_period));
8805
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008806 /* Get the negotiated link width and speed from PCI config space */
8807 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
8808
8809 i40e_set_pci_config_data(hw, link_status);
8810
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008811 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008812 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
8813 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
8814 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
8815 "Unknown"),
8816 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
8817 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
8818 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
8819 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
8820 "Unknown"));
8821
8822 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
8823 hw->bus.speed < i40e_bus_speed_8000) {
8824 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
8825 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
8826 }
8827
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008828 /* print a string summarizing features */
8829 i40e_print_features(pf);
8830
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008831 return 0;
8832
8833 /* Unwind what we've done if something failed in the setup */
8834err_vsis:
8835 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008836 i40e_clear_interrupt_scheme(pf);
8837 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +00008838err_switch_setup:
8839 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008840 del_timer_sync(&pf->service_timer);
8841err_mac_addr:
8842err_configure_lan_hmc:
8843 (void)i40e_shutdown_lan_hmc(hw);
8844err_init_lan_hmc:
8845 kfree(pf->qp_pile);
8846 kfree(pf->irq_pile);
8847err_sw_init:
8848err_adminq_setup:
8849 (void)i40e_shutdown_adminq(hw);
8850err_pf_reset:
8851 iounmap(hw->hw_addr);
8852err_ioremap:
8853 kfree(pf);
8854err_pf_alloc:
8855 pci_disable_pcie_error_reporting(pdev);
8856 pci_release_selected_regions(pdev,
8857 pci_select_bars(pdev, IORESOURCE_MEM));
8858err_pci_reg:
8859err_dma:
8860 pci_disable_device(pdev);
8861 return err;
8862}
8863
8864/**
8865 * i40e_remove - Device removal routine
8866 * @pdev: PCI device information struct
8867 *
8868 * i40e_remove is called by the PCI subsystem to alert the driver
8869 * that is should release a PCI device. This could be caused by a
8870 * Hot-Plug event, or because the driver is going to be removed from
8871 * memory.
8872 **/
8873static void i40e_remove(struct pci_dev *pdev)
8874{
8875 struct i40e_pf *pf = pci_get_drvdata(pdev);
8876 i40e_status ret_code;
8877 u32 reg;
8878 int i;
8879
8880 i40e_dbg_pf_exit(pf);
8881
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008882 i40e_ptp_stop(pf);
8883
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008884 /* no more scheduling of any task */
8885 set_bit(__I40E_DOWN, &pf->state);
8886 del_timer_sync(&pf->service_timer);
8887 cancel_work_sync(&pf->service_task);
8888
Mitch Williamseb2d80b2014-02-13 03:48:48 -08008889 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
8890 i40e_free_vfs(pf);
8891 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
8892 }
8893
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008894 i40e_fdir_teardown(pf);
8895
8896 /* If there is a switch structure or any orphans, remove them.
8897 * This will leave only the PF's VSI remaining.
8898 */
8899 for (i = 0; i < I40E_MAX_VEB; i++) {
8900 if (!pf->veb[i])
8901 continue;
8902
8903 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
8904 pf->veb[i]->uplink_seid == 0)
8905 i40e_switch_branch_release(pf->veb[i]);
8906 }
8907
8908 /* Now we can shutdown the PF's VSI, just before we kill
8909 * adminq and hmc.
8910 */
8911 if (pf->vsi[pf->lan_vsi])
8912 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
8913
8914 i40e_stop_misc_vector(pf);
8915 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8916 synchronize_irq(pf->msix_entries[0].vector);
8917 free_irq(pf->msix_entries[0].vector, pf);
8918 }
8919
8920 /* shutdown and destroy the HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00008921 if (pf->hw.hmc.hmc_obj) {
8922 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
8923 if (ret_code)
8924 dev_warn(&pdev->dev,
8925 "Failed to destroy the HMC resources: %d\n",
8926 ret_code);
8927 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008928
8929 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008930 ret_code = i40e_shutdown_adminq(&pf->hw);
8931 if (ret_code)
8932 dev_warn(&pdev->dev,
8933 "Failed to destroy the Admin Queue resources: %d\n",
8934 ret_code);
8935
8936 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
8937 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00008938 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008939 if (pf->vsi[i]) {
8940 i40e_vsi_clear_rings(pf->vsi[i]);
8941 i40e_vsi_clear(pf->vsi[i]);
8942 pf->vsi[i] = NULL;
8943 }
8944 }
8945
8946 for (i = 0; i < I40E_MAX_VEB; i++) {
8947 kfree(pf->veb[i]);
8948 pf->veb[i] = NULL;
8949 }
8950
8951 kfree(pf->qp_pile);
8952 kfree(pf->irq_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008953 kfree(pf->vsi);
8954
8955 /* force a PF reset to clean anything leftover */
8956 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
8957 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
8958 i40e_flush(&pf->hw);
8959
8960 iounmap(pf->hw.hw_addr);
8961 kfree(pf);
8962 pci_release_selected_regions(pdev,
8963 pci_select_bars(pdev, IORESOURCE_MEM));
8964
8965 pci_disable_pcie_error_reporting(pdev);
8966 pci_disable_device(pdev);
8967}
8968
8969/**
8970 * i40e_pci_error_detected - warning that something funky happened in PCI land
8971 * @pdev: PCI device information struct
8972 *
8973 * Called to warn that something happened and the error handling steps
8974 * are in progress. Allows the driver to quiesce things, be ready for
8975 * remediation.
8976 **/
8977static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
8978 enum pci_channel_state error)
8979{
8980 struct i40e_pf *pf = pci_get_drvdata(pdev);
8981
8982 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
8983
8984 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008985 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
8986 rtnl_lock();
8987 i40e_prep_for_reset(pf);
8988 rtnl_unlock();
8989 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008990
8991 /* Request a slot reset */
8992 return PCI_ERS_RESULT_NEED_RESET;
8993}
8994
8995/**
8996 * i40e_pci_error_slot_reset - a PCI slot reset just happened
8997 * @pdev: PCI device information struct
8998 *
8999 * Called to find if the driver can work with the device now that
9000 * the pci slot has been reset. If a basic connection seems good
9001 * (registers are readable and have sane content) then return a
9002 * happy little PCI_ERS_RESULT_xxx.
9003 **/
9004static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
9005{
9006 struct i40e_pf *pf = pci_get_drvdata(pdev);
9007 pci_ers_result_t result;
9008 int err;
9009 u32 reg;
9010
9011 dev_info(&pdev->dev, "%s\n", __func__);
9012 if (pci_enable_device_mem(pdev)) {
9013 dev_info(&pdev->dev,
9014 "Cannot re-enable PCI device after reset.\n");
9015 result = PCI_ERS_RESULT_DISCONNECT;
9016 } else {
9017 pci_set_master(pdev);
9018 pci_restore_state(pdev);
9019 pci_save_state(pdev);
9020 pci_wake_from_d3(pdev, false);
9021
9022 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9023 if (reg == 0)
9024 result = PCI_ERS_RESULT_RECOVERED;
9025 else
9026 result = PCI_ERS_RESULT_DISCONNECT;
9027 }
9028
9029 err = pci_cleanup_aer_uncorrect_error_status(pdev);
9030 if (err) {
9031 dev_info(&pdev->dev,
9032 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
9033 err);
9034 /* non-fatal, continue */
9035 }
9036
9037 return result;
9038}
9039
9040/**
9041 * i40e_pci_error_resume - restart operations after PCI error recovery
9042 * @pdev: PCI device information struct
9043 *
9044 * Called to allow the driver to bring things back up after PCI error
9045 * and/or reset recovery has finished.
9046 **/
9047static void i40e_pci_error_resume(struct pci_dev *pdev)
9048{
9049 struct i40e_pf *pf = pci_get_drvdata(pdev);
9050
9051 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00009052 if (test_bit(__I40E_SUSPENDED, &pf->state))
9053 return;
9054
9055 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009056 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00009057 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009058}
9059
Shannon Nelson9007bcc2013-11-26 10:49:23 +00009060/**
9061 * i40e_shutdown - PCI callback for shutting down
9062 * @pdev: PCI device information struct
9063 **/
9064static void i40e_shutdown(struct pci_dev *pdev)
9065{
9066 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00009067 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00009068
9069 set_bit(__I40E_SUSPENDED, &pf->state);
9070 set_bit(__I40E_DOWN, &pf->state);
9071 rtnl_lock();
9072 i40e_prep_for_reset(pf);
9073 rtnl_unlock();
9074
Shannon Nelson8e2773a2013-11-28 06:39:22 +00009075 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
9076 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
9077
Shannon Nelson9007bcc2013-11-26 10:49:23 +00009078 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +00009079 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00009080 pci_set_power_state(pdev, PCI_D3hot);
9081 }
9082}
9083
9084#ifdef CONFIG_PM
9085/**
9086 * i40e_suspend - PCI callback for moving to D3
9087 * @pdev: PCI device information struct
9088 **/
9089static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
9090{
9091 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00009092 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00009093
9094 set_bit(__I40E_SUSPENDED, &pf->state);
9095 set_bit(__I40E_DOWN, &pf->state);
9096 rtnl_lock();
9097 i40e_prep_for_reset(pf);
9098 rtnl_unlock();
9099
Shannon Nelson8e2773a2013-11-28 06:39:22 +00009100 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
9101 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
9102
9103 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00009104 pci_set_power_state(pdev, PCI_D3hot);
9105
9106 return 0;
9107}
9108
9109/**
9110 * i40e_resume - PCI callback for waking up from D3
9111 * @pdev: PCI device information struct
9112 **/
9113static int i40e_resume(struct pci_dev *pdev)
9114{
9115 struct i40e_pf *pf = pci_get_drvdata(pdev);
9116 u32 err;
9117
9118 pci_set_power_state(pdev, PCI_D0);
9119 pci_restore_state(pdev);
9120 /* pci_restore_state() clears dev->state_saves, so
9121 * call pci_save_state() again to restore it.
9122 */
9123 pci_save_state(pdev);
9124
9125 err = pci_enable_device_mem(pdev);
9126 if (err) {
9127 dev_err(&pdev->dev,
9128 "%s: Cannot enable PCI device from suspend\n",
9129 __func__);
9130 return err;
9131 }
9132 pci_set_master(pdev);
9133
9134 /* no wakeup events while running */
9135 pci_wake_from_d3(pdev, false);
9136
9137 /* handling the reset will rebuild the device state */
9138 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
9139 clear_bit(__I40E_DOWN, &pf->state);
9140 rtnl_lock();
9141 i40e_reset_and_rebuild(pf, false);
9142 rtnl_unlock();
9143 }
9144
9145 return 0;
9146}
9147
9148#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009149static const struct pci_error_handlers i40e_err_handler = {
9150 .error_detected = i40e_pci_error_detected,
9151 .slot_reset = i40e_pci_error_slot_reset,
9152 .resume = i40e_pci_error_resume,
9153};
9154
9155static struct pci_driver i40e_driver = {
9156 .name = i40e_driver_name,
9157 .id_table = i40e_pci_tbl,
9158 .probe = i40e_probe,
9159 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +00009160#ifdef CONFIG_PM
9161 .suspend = i40e_suspend,
9162 .resume = i40e_resume,
9163#endif
9164 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009165 .err_handler = &i40e_err_handler,
9166 .sriov_configure = i40e_pci_sriov_configure,
9167};
9168
9169/**
9170 * i40e_init_module - Driver registration routine
9171 *
9172 * i40e_init_module is the first routine called when the driver is
9173 * loaded. All it does is register with the PCI subsystem.
9174 **/
9175static int __init i40e_init_module(void)
9176{
9177 pr_info("%s: %s - version %s\n", i40e_driver_name,
9178 i40e_driver_string, i40e_driver_version_str);
9179 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
9180 i40e_dbg_init();
9181 return pci_register_driver(&i40e_driver);
9182}
9183module_init(i40e_init_module);
9184
9185/**
9186 * i40e_exit_module - Driver exit cleanup routine
9187 *
9188 * i40e_exit_module is called just before the driver is removed
9189 * from memory.
9190 **/
9191static void __exit i40e_exit_module(void)
9192{
9193 pci_unregister_driver(&i40e_driver);
9194 i40e_dbg_exit();
9195}
9196module_exit(i40e_exit_module);