blob: 296b3d203ad9f5875b53f99151ac72668c0da1b7 [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 Sullivanf8320902014-05-22 06:32:33 +000042#define DRV_VERSION_BUILD 10
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");
Akeem G Abodunrine108b0e2014-02-13 03:48:43 -0800307 set_bit(__I40E_DOWN, &vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000308 i40e_down(vsi);
309 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{
447 memset(&pf->stats, 0, sizeof(pf->stats));
448 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
449 pf->stat_offsets_loaded = false;
450}
451
452/**
453 * i40e_stat_update48 - read and update a 48 bit stat from the chip
454 * @hw: ptr to the hardware info
455 * @hireg: the high 32 bit reg to read
456 * @loreg: the low 32 bit reg to read
457 * @offset_loaded: has the initial offset been loaded yet
458 * @offset: ptr to current offset value
459 * @stat: ptr to the stat
460 *
461 * Since the device stats are not reset at PFReset, they likely will not
462 * be zeroed when the driver starts. We'll save the first values read
463 * and use them as offsets to be subtracted from the raw values in order
464 * to report stats that count from zero. In the process, we also manage
465 * the potential roll-over.
466 **/
467static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
468 bool offset_loaded, u64 *offset, u64 *stat)
469{
470 u64 new_data;
471
Shannon Nelsonab600852014-01-17 15:36:39 -0800472 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000473 new_data = rd32(hw, loreg);
474 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
475 } else {
476 new_data = rd64(hw, loreg);
477 }
478 if (!offset_loaded)
479 *offset = new_data;
480 if (likely(new_data >= *offset))
481 *stat = new_data - *offset;
482 else
483 *stat = (new_data + ((u64)1 << 48)) - *offset;
484 *stat &= 0xFFFFFFFFFFFFULL;
485}
486
487/**
488 * i40e_stat_update32 - read and update a 32 bit stat from the chip
489 * @hw: ptr to the hardware info
490 * @reg: the hw reg to read
491 * @offset_loaded: has the initial offset been loaded yet
492 * @offset: ptr to current offset value
493 * @stat: ptr to the stat
494 **/
495static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
496 bool offset_loaded, u64 *offset, u64 *stat)
497{
498 u32 new_data;
499
500 new_data = rd32(hw, reg);
501 if (!offset_loaded)
502 *offset = new_data;
503 if (likely(new_data >= *offset))
504 *stat = (u32)(new_data - *offset);
505 else
506 *stat = (u32)((new_data + ((u64)1 << 32)) - *offset);
507}
508
509/**
510 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
511 * @vsi: the VSI to be updated
512 **/
513void i40e_update_eth_stats(struct i40e_vsi *vsi)
514{
515 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
516 struct i40e_pf *pf = vsi->back;
517 struct i40e_hw *hw = &pf->hw;
518 struct i40e_eth_stats *oes;
519 struct i40e_eth_stats *es; /* device's eth stats */
520
521 es = &vsi->eth_stats;
522 oes = &vsi->eth_stats_offsets;
523
524 /* Gather up the stats that the hw collects */
525 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
526 vsi->stat_offsets_loaded,
527 &oes->tx_errors, &es->tx_errors);
528 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
529 vsi->stat_offsets_loaded,
530 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000531 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
532 vsi->stat_offsets_loaded,
533 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
534 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
535 vsi->stat_offsets_loaded,
536 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000537
538 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
539 I40E_GLV_GORCL(stat_idx),
540 vsi->stat_offsets_loaded,
541 &oes->rx_bytes, &es->rx_bytes);
542 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
543 I40E_GLV_UPRCL(stat_idx),
544 vsi->stat_offsets_loaded,
545 &oes->rx_unicast, &es->rx_unicast);
546 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
547 I40E_GLV_MPRCL(stat_idx),
548 vsi->stat_offsets_loaded,
549 &oes->rx_multicast, &es->rx_multicast);
550 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
551 I40E_GLV_BPRCL(stat_idx),
552 vsi->stat_offsets_loaded,
553 &oes->rx_broadcast, &es->rx_broadcast);
554
555 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
556 I40E_GLV_GOTCL(stat_idx),
557 vsi->stat_offsets_loaded,
558 &oes->tx_bytes, &es->tx_bytes);
559 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
560 I40E_GLV_UPTCL(stat_idx),
561 vsi->stat_offsets_loaded,
562 &oes->tx_unicast, &es->tx_unicast);
563 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
564 I40E_GLV_MPTCL(stat_idx),
565 vsi->stat_offsets_loaded,
566 &oes->tx_multicast, &es->tx_multicast);
567 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
568 I40E_GLV_BPTCL(stat_idx),
569 vsi->stat_offsets_loaded,
570 &oes->tx_broadcast, &es->tx_broadcast);
571 vsi->stat_offsets_loaded = true;
572}
573
574/**
575 * i40e_update_veb_stats - Update Switch component statistics
576 * @veb: the VEB being updated
577 **/
578static void i40e_update_veb_stats(struct i40e_veb *veb)
579{
580 struct i40e_pf *pf = veb->pf;
581 struct i40e_hw *hw = &pf->hw;
582 struct i40e_eth_stats *oes;
583 struct i40e_eth_stats *es; /* device's eth stats */
584 int idx = 0;
585
586 idx = veb->stats_idx;
587 es = &veb->stats;
588 oes = &veb->stats_offsets;
589
590 /* Gather up the stats that the hw collects */
591 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
592 veb->stat_offsets_loaded,
593 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000594 if (hw->revision_id > 0)
595 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
596 veb->stat_offsets_loaded,
597 &oes->rx_unknown_protocol,
598 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000599 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
600 veb->stat_offsets_loaded,
601 &oes->rx_bytes, &es->rx_bytes);
602 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
603 veb->stat_offsets_loaded,
604 &oes->rx_unicast, &es->rx_unicast);
605 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
606 veb->stat_offsets_loaded,
607 &oes->rx_multicast, &es->rx_multicast);
608 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
609 veb->stat_offsets_loaded,
610 &oes->rx_broadcast, &es->rx_broadcast);
611
612 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
613 veb->stat_offsets_loaded,
614 &oes->tx_bytes, &es->tx_bytes);
615 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
616 veb->stat_offsets_loaded,
617 &oes->tx_unicast, &es->tx_unicast);
618 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
619 veb->stat_offsets_loaded,
620 &oes->tx_multicast, &es->tx_multicast);
621 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
622 veb->stat_offsets_loaded,
623 &oes->tx_broadcast, &es->tx_broadcast);
624 veb->stat_offsets_loaded = true;
625}
626
627/**
628 * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
629 * @pf: the corresponding PF
630 *
631 * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
632 **/
633static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
634{
635 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
636 struct i40e_hw_port_stats *nsd = &pf->stats;
637 struct i40e_hw *hw = &pf->hw;
638 u64 xoff = 0;
639 u16 i, v;
640
641 if ((hw->fc.current_mode != I40E_FC_FULL) &&
642 (hw->fc.current_mode != I40E_FC_RX_PAUSE))
643 return;
644
645 xoff = nsd->link_xoff_rx;
646 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
647 pf->stat_offsets_loaded,
648 &osd->link_xoff_rx, &nsd->link_xoff_rx);
649
650 /* No new LFC xoff rx */
651 if (!(nsd->link_xoff_rx - xoff))
652 return;
653
654 /* Clear the __I40E_HANG_CHECK_ARMED bit for all Tx rings */
Mitch Williams505682c2014-05-20 08:01:37 +0000655 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000656 struct i40e_vsi *vsi = pf->vsi[v];
657
Mitch Williamsddfda802014-05-10 04:49:10 +0000658 if (!vsi || !vsi->tx_rings[0])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000659 continue;
660
661 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000662 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000663 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
664 }
665 }
666}
667
668/**
669 * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
670 * @pf: the corresponding PF
671 *
672 * Update the Rx XOFF counter (PAUSE frames) in PFC mode
673 **/
674static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
675{
676 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
677 struct i40e_hw_port_stats *nsd = &pf->stats;
678 bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
679 struct i40e_dcbx_config *dcb_cfg;
680 struct i40e_hw *hw = &pf->hw;
681 u16 i, v;
682 u8 tc;
683
684 dcb_cfg = &hw->local_dcbx_config;
685
686 /* See if DCB enabled with PFC TC */
687 if (!(pf->flags & I40E_FLAG_DCB_ENABLED) ||
688 !(dcb_cfg->pfc.pfcenable)) {
689 i40e_update_link_xoff_rx(pf);
690 return;
691 }
692
693 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
694 u64 prio_xoff = nsd->priority_xoff_rx[i];
695 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
696 pf->stat_offsets_loaded,
697 &osd->priority_xoff_rx[i],
698 &nsd->priority_xoff_rx[i]);
699
700 /* No new PFC xoff rx */
701 if (!(nsd->priority_xoff_rx[i] - prio_xoff))
702 continue;
703 /* Get the TC for given priority */
704 tc = dcb_cfg->etscfg.prioritytable[i];
705 xoff[tc] = true;
706 }
707
708 /* Clear the __I40E_HANG_CHECK_ARMED bit for Tx rings */
Mitch Williams505682c2014-05-20 08:01:37 +0000709 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000710 struct i40e_vsi *vsi = pf->vsi[v];
711
Mitch Williamsddfda802014-05-10 04:49:10 +0000712 if (!vsi || !vsi->tx_rings[0])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000713 continue;
714
715 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000716 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000717
718 tc = ring->dcb_tc;
719 if (xoff[tc])
720 clear_bit(__I40E_HANG_CHECK_ARMED,
721 &ring->state);
722 }
723 }
724}
725
726/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000727 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000728 * @vsi: the VSI to be updated
729 *
730 * There are a few instances where we store the same stat in a
731 * couple of different structs. This is partly because we have
732 * the netdev stats that need to be filled out, which is slightly
733 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000734 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000735 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000736static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000737{
738 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000739 struct rtnl_link_stats64 *ons;
740 struct rtnl_link_stats64 *ns; /* netdev stats */
741 struct i40e_eth_stats *oes;
742 struct i40e_eth_stats *es; /* device's eth stats */
743 u32 tx_restart, tx_busy;
744 u32 rx_page, rx_buf;
745 u64 rx_p, rx_b;
746 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000747 u16 q;
748
749 if (test_bit(__I40E_DOWN, &vsi->state) ||
750 test_bit(__I40E_CONFIG_BUSY, &pf->state))
751 return;
752
753 ns = i40e_get_vsi_stats_struct(vsi);
754 ons = &vsi->net_stats_offsets;
755 es = &vsi->eth_stats;
756 oes = &vsi->eth_stats_offsets;
757
758 /* Gather up the netdev and vsi stats that the driver collects
759 * on the fly during packet processing
760 */
761 rx_b = rx_p = 0;
762 tx_b = tx_p = 0;
763 tx_restart = tx_busy = 0;
764 rx_page = 0;
765 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000766 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000767 for (q = 0; q < vsi->num_queue_pairs; q++) {
768 struct i40e_ring *p;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000769 u64 bytes, packets;
770 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000771
Alexander Duyck980e9b12013-09-28 06:01:03 +0000772 /* locate Tx ring */
773 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000774
Alexander Duyck980e9b12013-09-28 06:01:03 +0000775 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700776 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000777 packets = p->stats.packets;
778 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700779 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000780 tx_b += bytes;
781 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000782 tx_restart += p->tx_stats.restart_queue;
783 tx_busy += p->tx_stats.tx_busy;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000784
785 /* Rx queue is part of the same block as Tx queue */
786 p = &p[1];
787 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 rx_b += bytes;
793 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000794 rx_buf += p->rx_stats.alloc_buff_failed;
795 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000796 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000797 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000798 vsi->tx_restart = tx_restart;
799 vsi->tx_busy = tx_busy;
800 vsi->rx_page_failed = rx_page;
801 vsi->rx_buf_failed = rx_buf;
802
803 ns->rx_packets = rx_p;
804 ns->rx_bytes = rx_b;
805 ns->tx_packets = tx_p;
806 ns->tx_bytes = tx_b;
807
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000808 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000809 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000810 ons->tx_errors = oes->tx_errors;
811 ns->tx_errors = es->tx_errors;
812 ons->multicast = oes->rx_multicast;
813 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000814 ons->rx_dropped = oes->rx_discards;
815 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000816 ons->tx_dropped = oes->tx_discards;
817 ns->tx_dropped = es->tx_discards;
818
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000819 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000820 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000821 ns->rx_crc_errors = pf->stats.crc_errors;
822 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
823 ns->rx_length_errors = pf->stats.rx_length_errors;
824 }
825}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000826
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000827/**
828 * i40e_update_pf_stats - Update the pf statistics counters.
829 * @pf: the PF to be updated
830 **/
831static void i40e_update_pf_stats(struct i40e_pf *pf)
832{
833 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
834 struct i40e_hw_port_stats *nsd = &pf->stats;
835 struct i40e_hw *hw = &pf->hw;
836 u32 val;
837 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000838
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000839 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
840 I40E_GLPRT_GORCL(hw->port),
841 pf->stat_offsets_loaded,
842 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
843 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
844 I40E_GLPRT_GOTCL(hw->port),
845 pf->stat_offsets_loaded,
846 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
847 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
848 pf->stat_offsets_loaded,
849 &osd->eth.rx_discards,
850 &nsd->eth.rx_discards);
851 i40e_stat_update32(hw, I40E_GLPRT_TDPC(hw->port),
852 pf->stat_offsets_loaded,
853 &osd->eth.tx_discards,
854 &nsd->eth.tx_discards);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000855
Shannon Nelson532d2832014-04-23 04:50:09 +0000856 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
857 I40E_GLPRT_UPRCL(hw->port),
858 pf->stat_offsets_loaded,
859 &osd->eth.rx_unicast,
860 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000861 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
862 I40E_GLPRT_MPRCL(hw->port),
863 pf->stat_offsets_loaded,
864 &osd->eth.rx_multicast,
865 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000866 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
867 I40E_GLPRT_BPRCL(hw->port),
868 pf->stat_offsets_loaded,
869 &osd->eth.rx_broadcast,
870 &nsd->eth.rx_broadcast);
871 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
872 I40E_GLPRT_UPTCL(hw->port),
873 pf->stat_offsets_loaded,
874 &osd->eth.tx_unicast,
875 &nsd->eth.tx_unicast);
876 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
877 I40E_GLPRT_MPTCL(hw->port),
878 pf->stat_offsets_loaded,
879 &osd->eth.tx_multicast,
880 &nsd->eth.tx_multicast);
881 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
882 I40E_GLPRT_BPTCL(hw->port),
883 pf->stat_offsets_loaded,
884 &osd->eth.tx_broadcast,
885 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000886
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000887 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
888 pf->stat_offsets_loaded,
889 &osd->tx_dropped_link_down,
890 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000891
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000892 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
893 pf->stat_offsets_loaded,
894 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000895
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000896 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
897 pf->stat_offsets_loaded,
898 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000899
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000900 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
901 pf->stat_offsets_loaded,
902 &osd->mac_local_faults,
903 &nsd->mac_local_faults);
904 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
905 pf->stat_offsets_loaded,
906 &osd->mac_remote_faults,
907 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000908
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000909 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
910 pf->stat_offsets_loaded,
911 &osd->rx_length_errors,
912 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000913
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000914 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
915 pf->stat_offsets_loaded,
916 &osd->link_xon_rx, &nsd->link_xon_rx);
917 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
918 pf->stat_offsets_loaded,
919 &osd->link_xon_tx, &nsd->link_xon_tx);
920 i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
921 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
922 pf->stat_offsets_loaded,
923 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000924
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000925 for (i = 0; i < 8; i++) {
926 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000927 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000928 &osd->priority_xon_rx[i],
929 &nsd->priority_xon_rx[i]);
930 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000931 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000932 &osd->priority_xon_tx[i],
933 &nsd->priority_xon_tx[i]);
934 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000935 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000936 &osd->priority_xoff_tx[i],
937 &nsd->priority_xoff_tx[i]);
938 i40e_stat_update32(hw,
939 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000940 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000941 &osd->priority_xon_2_xoff[i],
942 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000943 }
944
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000945 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
946 I40E_GLPRT_PRC64L(hw->port),
947 pf->stat_offsets_loaded,
948 &osd->rx_size_64, &nsd->rx_size_64);
949 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
950 I40E_GLPRT_PRC127L(hw->port),
951 pf->stat_offsets_loaded,
952 &osd->rx_size_127, &nsd->rx_size_127);
953 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
954 I40E_GLPRT_PRC255L(hw->port),
955 pf->stat_offsets_loaded,
956 &osd->rx_size_255, &nsd->rx_size_255);
957 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
958 I40E_GLPRT_PRC511L(hw->port),
959 pf->stat_offsets_loaded,
960 &osd->rx_size_511, &nsd->rx_size_511);
961 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
962 I40E_GLPRT_PRC1023L(hw->port),
963 pf->stat_offsets_loaded,
964 &osd->rx_size_1023, &nsd->rx_size_1023);
965 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
966 I40E_GLPRT_PRC1522L(hw->port),
967 pf->stat_offsets_loaded,
968 &osd->rx_size_1522, &nsd->rx_size_1522);
969 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
970 I40E_GLPRT_PRC9522L(hw->port),
971 pf->stat_offsets_loaded,
972 &osd->rx_size_big, &nsd->rx_size_big);
973
974 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
975 I40E_GLPRT_PTC64L(hw->port),
976 pf->stat_offsets_loaded,
977 &osd->tx_size_64, &nsd->tx_size_64);
978 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
979 I40E_GLPRT_PTC127L(hw->port),
980 pf->stat_offsets_loaded,
981 &osd->tx_size_127, &nsd->tx_size_127);
982 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
983 I40E_GLPRT_PTC255L(hw->port),
984 pf->stat_offsets_loaded,
985 &osd->tx_size_255, &nsd->tx_size_255);
986 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
987 I40E_GLPRT_PTC511L(hw->port),
988 pf->stat_offsets_loaded,
989 &osd->tx_size_511, &nsd->tx_size_511);
990 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
991 I40E_GLPRT_PTC1023L(hw->port),
992 pf->stat_offsets_loaded,
993 &osd->tx_size_1023, &nsd->tx_size_1023);
994 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
995 I40E_GLPRT_PTC1522L(hw->port),
996 pf->stat_offsets_loaded,
997 &osd->tx_size_1522, &nsd->tx_size_1522);
998 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
999 I40E_GLPRT_PTC9522L(hw->port),
1000 pf->stat_offsets_loaded,
1001 &osd->tx_size_big, &nsd->tx_size_big);
1002
1003 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1004 pf->stat_offsets_loaded,
1005 &osd->rx_undersize, &nsd->rx_undersize);
1006 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1007 pf->stat_offsets_loaded,
1008 &osd->rx_fragments, &nsd->rx_fragments);
1009 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1010 pf->stat_offsets_loaded,
1011 &osd->rx_oversize, &nsd->rx_oversize);
1012 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1013 pf->stat_offsets_loaded,
1014 &osd->rx_jabber, &nsd->rx_jabber);
1015
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001016 /* FDIR stats */
1017 i40e_stat_update32(hw, I40E_GLQF_PCNT(pf->fd_atr_cnt_idx),
1018 pf->stat_offsets_loaded,
1019 &osd->fd_atr_match, &nsd->fd_atr_match);
1020 i40e_stat_update32(hw, I40E_GLQF_PCNT(pf->fd_sb_cnt_idx),
1021 pf->stat_offsets_loaded,
1022 &osd->fd_sb_match, &nsd->fd_sb_match);
1023
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001024 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1025 nsd->tx_lpi_status =
1026 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1027 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1028 nsd->rx_lpi_status =
1029 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1030 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1031 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1032 pf->stat_offsets_loaded,
1033 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1034 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1035 pf->stat_offsets_loaded,
1036 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1037
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001038 pf->stat_offsets_loaded = true;
1039}
1040
1041/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001042 * i40e_update_stats - Update the various statistics counters.
1043 * @vsi: the VSI to be updated
1044 *
1045 * Update the various stats for this VSI and its related entities.
1046 **/
1047void i40e_update_stats(struct i40e_vsi *vsi)
1048{
1049 struct i40e_pf *pf = vsi->back;
1050
1051 if (vsi == pf->vsi[pf->lan_vsi])
1052 i40e_update_pf_stats(pf);
1053
1054 i40e_update_vsi_stats(vsi);
1055}
1056
1057/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001058 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1059 * @vsi: the VSI to be searched
1060 * @macaddr: the MAC address
1061 * @vlan: the vlan
1062 * @is_vf: make sure its a vf filter, else doesn't matter
1063 * @is_netdev: make sure its a netdev filter, else doesn't matter
1064 *
1065 * Returns ptr to the filter object or NULL
1066 **/
1067static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1068 u8 *macaddr, s16 vlan,
1069 bool is_vf, bool is_netdev)
1070{
1071 struct i40e_mac_filter *f;
1072
1073 if (!vsi || !macaddr)
1074 return NULL;
1075
1076 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1077 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1078 (vlan == f->vlan) &&
1079 (!is_vf || f->is_vf) &&
1080 (!is_netdev || f->is_netdev))
1081 return f;
1082 }
1083 return NULL;
1084}
1085
1086/**
1087 * i40e_find_mac - Find a mac addr in the macvlan filters list
1088 * @vsi: the VSI to be searched
1089 * @macaddr: the MAC address we are searching for
1090 * @is_vf: make sure its a vf filter, else doesn't matter
1091 * @is_netdev: make sure its a netdev filter, else doesn't matter
1092 *
1093 * Returns the first filter with the provided MAC address or NULL if
1094 * MAC address was not found
1095 **/
1096struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1097 bool is_vf, bool is_netdev)
1098{
1099 struct i40e_mac_filter *f;
1100
1101 if (!vsi || !macaddr)
1102 return NULL;
1103
1104 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1105 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1106 (!is_vf || f->is_vf) &&
1107 (!is_netdev || f->is_netdev))
1108 return f;
1109 }
1110 return NULL;
1111}
1112
1113/**
1114 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1115 * @vsi: the VSI to be searched
1116 *
1117 * Returns true if VSI is in vlan mode or false otherwise
1118 **/
1119bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1120{
1121 struct i40e_mac_filter *f;
1122
1123 /* Only -1 for all the filters denotes not in vlan mode
1124 * so we have to go through all the list in order to make sure
1125 */
1126 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1127 if (f->vlan >= 0)
1128 return true;
1129 }
1130
1131 return false;
1132}
1133
1134/**
1135 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1136 * @vsi: the VSI to be searched
1137 * @macaddr: the mac address to be filtered
1138 * @is_vf: true if it is a vf
1139 * @is_netdev: true if it is a netdev
1140 *
1141 * Goes through all the macvlan filters and adds a
1142 * macvlan filter for each unique vlan that already exists
1143 *
1144 * Returns first filter found on success, else NULL
1145 **/
1146struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1147 bool is_vf, bool is_netdev)
1148{
1149 struct i40e_mac_filter *f;
1150
1151 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1152 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1153 is_vf, is_netdev)) {
1154 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001155 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001156 return NULL;
1157 }
1158 }
1159
1160 return list_first_entry_or_null(&vsi->mac_filter_list,
1161 struct i40e_mac_filter, list);
1162}
1163
1164/**
Greg Rose8c27d422014-05-22 06:31:56 +00001165 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1166 * @vsi: the PF Main VSI - inappropriate for any other VSI
1167 * @macaddr: the MAC address
1168 **/
1169static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1170{
1171 struct i40e_aqc_remove_macvlan_element_data element;
1172 struct i40e_pf *pf = vsi->back;
1173 i40e_status aq_ret;
1174
1175 /* Only appropriate for the PF main VSI */
1176 if (vsi->type != I40E_VSI_MAIN)
1177 return;
1178
1179 ether_addr_copy(element.mac_addr, macaddr);
1180 element.vlan_tag = 0;
1181 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1182 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1183 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1184 if (aq_ret)
1185 dev_err(&pf->pdev->dev, "Could not remove default MAC-VLAN\n");
1186}
1187
1188/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001189 * i40e_add_filter - Add a mac/vlan filter to the VSI
1190 * @vsi: the VSI to be searched
1191 * @macaddr: the MAC address
1192 * @vlan: the vlan
1193 * @is_vf: make sure its a vf filter, else doesn't matter
1194 * @is_netdev: make sure its a netdev filter, else doesn't matter
1195 *
1196 * Returns ptr to the filter object or NULL when no memory available.
1197 **/
1198struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1199 u8 *macaddr, s16 vlan,
1200 bool is_vf, bool is_netdev)
1201{
1202 struct i40e_mac_filter *f;
1203
1204 if (!vsi || !macaddr)
1205 return NULL;
1206
1207 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1208 if (!f) {
1209 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1210 if (!f)
1211 goto add_filter_out;
1212
Greg Rose9a173902014-05-22 06:32:02 +00001213 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001214 f->vlan = vlan;
1215 f->changed = true;
1216
1217 INIT_LIST_HEAD(&f->list);
1218 list_add(&f->list, &vsi->mac_filter_list);
1219 }
1220
1221 /* increment counter and add a new flag if needed */
1222 if (is_vf) {
1223 if (!f->is_vf) {
1224 f->is_vf = true;
1225 f->counter++;
1226 }
1227 } else if (is_netdev) {
1228 if (!f->is_netdev) {
1229 f->is_netdev = true;
1230 f->counter++;
1231 }
1232 } else {
1233 f->counter++;
1234 }
1235
1236 /* changed tells sync_filters_subtask to
1237 * push the filter down to the firmware
1238 */
1239 if (f->changed) {
1240 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1241 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1242 }
1243
1244add_filter_out:
1245 return f;
1246}
1247
1248/**
1249 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1250 * @vsi: the VSI to be searched
1251 * @macaddr: the MAC address
1252 * @vlan: the vlan
1253 * @is_vf: make sure it's a vf filter, else doesn't matter
1254 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1255 **/
1256void i40e_del_filter(struct i40e_vsi *vsi,
1257 u8 *macaddr, s16 vlan,
1258 bool is_vf, bool is_netdev)
1259{
1260 struct i40e_mac_filter *f;
1261
1262 if (!vsi || !macaddr)
1263 return;
1264
1265 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1266 if (!f || f->counter == 0)
1267 return;
1268
1269 if (is_vf) {
1270 if (f->is_vf) {
1271 f->is_vf = false;
1272 f->counter--;
1273 }
1274 } else if (is_netdev) {
1275 if (f->is_netdev) {
1276 f->is_netdev = false;
1277 f->counter--;
1278 }
1279 } else {
1280 /* make sure we don't remove a filter in use by vf or netdev */
1281 int min_f = 0;
1282 min_f += (f->is_vf ? 1 : 0);
1283 min_f += (f->is_netdev ? 1 : 0);
1284
1285 if (f->counter > min_f)
1286 f->counter--;
1287 }
1288
1289 /* counter == 0 tells sync_filters_subtask to
1290 * remove the filter from the firmware's list
1291 */
1292 if (f->counter == 0) {
1293 f->changed = true;
1294 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1295 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1296 }
1297}
1298
1299/**
1300 * i40e_set_mac - NDO callback to set mac address
1301 * @netdev: network interface device structure
1302 * @p: pointer to an address structure
1303 *
1304 * Returns 0 on success, negative on failure
1305 **/
1306static int i40e_set_mac(struct net_device *netdev, void *p)
1307{
1308 struct i40e_netdev_priv *np = netdev_priv(netdev);
1309 struct i40e_vsi *vsi = np->vsi;
1310 struct sockaddr *addr = p;
1311 struct i40e_mac_filter *f;
1312
1313 if (!is_valid_ether_addr(addr->sa_data))
1314 return -EADDRNOTAVAIL;
1315
1316 netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
1317
1318 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
1319 return 0;
1320
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001321 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1322 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1323 return -EADDRNOTAVAIL;
1324
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001325 if (vsi->type == I40E_VSI_MAIN) {
1326 i40e_status ret;
1327 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1328 I40E_AQC_WRITE_TYPE_LAA_ONLY,
1329 addr->sa_data, NULL);
1330 if (ret) {
1331 netdev_info(netdev,
1332 "Addr change for Main VSI failed: %d\n",
1333 ret);
1334 return -EADDRNOTAVAIL;
1335 }
1336
Greg Rose9a173902014-05-22 06:32:02 +00001337 ether_addr_copy(vsi->back->hw.mac.addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001338 }
1339
1340 /* In order to be sure to not drop any packets, add the new address
1341 * then delete the old one.
1342 */
1343 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
1344 if (!f)
1345 return -ENOMEM;
1346
1347 i40e_sync_vsi_filters(vsi);
1348 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
1349 i40e_sync_vsi_filters(vsi);
1350
Greg Rose9a173902014-05-22 06:32:02 +00001351 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001352
1353 return 0;
1354}
1355
1356/**
1357 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1358 * @vsi: the VSI being setup
1359 * @ctxt: VSI context structure
1360 * @enabled_tc: Enabled TCs bitmap
1361 * @is_add: True if called before Add VSI
1362 *
1363 * Setup VSI queue mapping for enabled traffic classes.
1364 **/
1365static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1366 struct i40e_vsi_context *ctxt,
1367 u8 enabled_tc,
1368 bool is_add)
1369{
1370 struct i40e_pf *pf = vsi->back;
1371 u16 sections = 0;
1372 u8 netdev_tc = 0;
1373 u16 numtc = 0;
1374 u16 qcount;
1375 u8 offset;
1376 u16 qmap;
1377 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001378 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001379
1380 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1381 offset = 0;
1382
1383 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1384 /* Find numtc from enabled TC bitmap */
1385 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1386 if (enabled_tc & (1 << i)) /* TC is enabled */
1387 numtc++;
1388 }
1389 if (!numtc) {
1390 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1391 numtc = 1;
1392 }
1393 } else {
1394 /* At least TC0 is enabled in case of non-DCB case */
1395 numtc = 1;
1396 }
1397
1398 vsi->tc_config.numtc = numtc;
1399 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001400 /* Number of queues per enabled TC */
Anjali Singhai Jaineb051af2014-05-20 08:01:46 +00001401 num_tc_qps = vsi->alloc_queue_pairs/numtc;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001402 num_tc_qps = min_t(int, num_tc_qps, I40E_MAX_QUEUES_PER_TC);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001403
1404 /* Setup queue offset/count for all TCs for given VSI */
1405 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1406 /* See if the given TC is enabled for the given VSI */
1407 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1408 int pow, num_qps;
1409
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001410 switch (vsi->type) {
1411 case I40E_VSI_MAIN:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001412 qcount = min_t(int, pf->rss_size, num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001413 break;
1414 case I40E_VSI_FDIR:
1415 case I40E_VSI_SRIOV:
1416 case I40E_VSI_VMDQ2:
1417 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001418 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001419 WARN_ON(i != 0);
1420 break;
1421 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001422 vsi->tc_config.tc_info[i].qoffset = offset;
1423 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001424
1425 /* find the power-of-2 of the number of queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001426 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001427 pow = 0;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001428 while (num_qps && ((1 << pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001429 pow++;
1430 num_qps >>= 1;
1431 }
1432
1433 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1434 qmap =
1435 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1436 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1437
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001438 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001439 } else {
1440 /* TC is not enabled so set the offset to
1441 * default queue and allocate one queue
1442 * for the given TC.
1443 */
1444 vsi->tc_config.tc_info[i].qoffset = 0;
1445 vsi->tc_config.tc_info[i].qcount = 1;
1446 vsi->tc_config.tc_info[i].netdev_tc = 0;
1447
1448 qmap = 0;
1449 }
1450 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1451 }
1452
1453 /* Set actual Tx/Rx queue pairs */
1454 vsi->num_queue_pairs = offset;
1455
1456 /* Scheduler section valid can only be set for ADD VSI */
1457 if (is_add) {
1458 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1459
1460 ctxt->info.up_enable_bits = enabled_tc;
1461 }
1462 if (vsi->type == I40E_VSI_SRIOV) {
1463 ctxt->info.mapping_flags |=
1464 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1465 for (i = 0; i < vsi->num_queue_pairs; i++)
1466 ctxt->info.queue_mapping[i] =
1467 cpu_to_le16(vsi->base_queue + i);
1468 } else {
1469 ctxt->info.mapping_flags |=
1470 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1471 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1472 }
1473 ctxt->info.valid_sections |= cpu_to_le16(sections);
1474}
1475
1476/**
1477 * i40e_set_rx_mode - NDO callback to set the netdev filters
1478 * @netdev: network interface device structure
1479 **/
1480static void i40e_set_rx_mode(struct net_device *netdev)
1481{
1482 struct i40e_netdev_priv *np = netdev_priv(netdev);
1483 struct i40e_mac_filter *f, *ftmp;
1484 struct i40e_vsi *vsi = np->vsi;
1485 struct netdev_hw_addr *uca;
1486 struct netdev_hw_addr *mca;
1487 struct netdev_hw_addr *ha;
1488
1489 /* add addr if not already in the filter list */
1490 netdev_for_each_uc_addr(uca, netdev) {
1491 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1492 if (i40e_is_vsi_in_vlan(vsi))
1493 i40e_put_mac_in_vlan(vsi, uca->addr,
1494 false, true);
1495 else
1496 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1497 false, true);
1498 }
1499 }
1500
1501 netdev_for_each_mc_addr(mca, netdev) {
1502 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1503 if (i40e_is_vsi_in_vlan(vsi))
1504 i40e_put_mac_in_vlan(vsi, mca->addr,
1505 false, true);
1506 else
1507 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1508 false, true);
1509 }
1510 }
1511
1512 /* remove filter if not in netdev list */
1513 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1514 bool found = false;
1515
1516 if (!f->is_netdev)
1517 continue;
1518
1519 if (is_multicast_ether_addr(f->macaddr)) {
1520 netdev_for_each_mc_addr(mca, netdev) {
1521 if (ether_addr_equal(mca->addr, f->macaddr)) {
1522 found = true;
1523 break;
1524 }
1525 }
1526 } else {
1527 netdev_for_each_uc_addr(uca, netdev) {
1528 if (ether_addr_equal(uca->addr, f->macaddr)) {
1529 found = true;
1530 break;
1531 }
1532 }
1533
1534 for_each_dev_addr(netdev, ha) {
1535 if (ether_addr_equal(ha->addr, f->macaddr)) {
1536 found = true;
1537 break;
1538 }
1539 }
1540 }
1541 if (!found)
1542 i40e_del_filter(
1543 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1544 }
1545
1546 /* check for other flag changes */
1547 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1548 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1549 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1550 }
1551}
1552
1553/**
1554 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1555 * @vsi: ptr to the VSI
1556 *
1557 * Push any outstanding VSI filter changes through the AdminQ.
1558 *
1559 * Returns 0 or error value
1560 **/
1561int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1562{
1563 struct i40e_mac_filter *f, *ftmp;
1564 bool promisc_forced_on = false;
1565 bool add_happened = false;
1566 int filter_list_len = 0;
1567 u32 changed_flags = 0;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001568 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001569 struct i40e_pf *pf;
1570 int num_add = 0;
1571 int num_del = 0;
1572 u16 cmd_flags;
1573
1574 /* empty array typed pointers, kcalloc later */
1575 struct i40e_aqc_add_macvlan_element_data *add_list;
1576 struct i40e_aqc_remove_macvlan_element_data *del_list;
1577
1578 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1579 usleep_range(1000, 2000);
1580 pf = vsi->back;
1581
1582 if (vsi->netdev) {
1583 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1584 vsi->current_netdev_flags = vsi->netdev->flags;
1585 }
1586
1587 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1588 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1589
1590 filter_list_len = pf->hw.aq.asq_buf_size /
1591 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1592 del_list = kcalloc(filter_list_len,
1593 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1594 GFP_KERNEL);
1595 if (!del_list)
1596 return -ENOMEM;
1597
1598 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1599 if (!f->changed)
1600 continue;
1601
1602 if (f->counter != 0)
1603 continue;
1604 f->changed = false;
1605 cmd_flags = 0;
1606
1607 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001608 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001609 del_list[num_del].vlan_tag =
1610 cpu_to_le16((u16)(f->vlan ==
1611 I40E_VLAN_ANY ? 0 : f->vlan));
1612
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001613 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1614 del_list[num_del].flags = cmd_flags;
1615 num_del++;
1616
1617 /* unlink from filter list */
1618 list_del(&f->list);
1619 kfree(f);
1620
1621 /* flush a full buffer */
1622 if (num_del == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001623 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001624 vsi->seid, del_list, num_del,
1625 NULL);
1626 num_del = 0;
1627 memset(del_list, 0, sizeof(*del_list));
1628
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001629 if (aq_ret &&
1630 pf->hw.aq.asq_last_status !=
1631 I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001632 dev_info(&pf->pdev->dev,
1633 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001634 aq_ret,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001635 pf->hw.aq.asq_last_status);
1636 }
1637 }
1638 if (num_del) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001639 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001640 del_list, num_del, NULL);
1641 num_del = 0;
1642
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001643 if (aq_ret &&
1644 pf->hw.aq.asq_last_status != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001645 dev_info(&pf->pdev->dev,
1646 "ignoring delete macvlan error, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001647 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001648 }
1649
1650 kfree(del_list);
1651 del_list = NULL;
1652
1653 /* do all the adds now */
1654 filter_list_len = pf->hw.aq.asq_buf_size /
1655 sizeof(struct i40e_aqc_add_macvlan_element_data),
1656 add_list = kcalloc(filter_list_len,
1657 sizeof(struct i40e_aqc_add_macvlan_element_data),
1658 GFP_KERNEL);
1659 if (!add_list)
1660 return -ENOMEM;
1661
1662 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1663 if (!f->changed)
1664 continue;
1665
1666 if (f->counter == 0)
1667 continue;
1668 f->changed = false;
1669 add_happened = true;
1670 cmd_flags = 0;
1671
1672 /* add to add array */
Greg Rose9a173902014-05-22 06:32:02 +00001673 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001674 add_list[num_add].vlan_tag =
1675 cpu_to_le16(
1676 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1677 add_list[num_add].queue_number = 0;
1678
1679 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001680 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1681 num_add++;
1682
1683 /* flush a full buffer */
1684 if (num_add == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001685 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1686 add_list, num_add,
1687 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001688 num_add = 0;
1689
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001690 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001691 break;
1692 memset(add_list, 0, sizeof(*add_list));
1693 }
1694 }
1695 if (num_add) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001696 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1697 add_list, num_add, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001698 num_add = 0;
1699 }
1700 kfree(add_list);
1701 add_list = NULL;
1702
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001703 if (add_happened && (!aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001704 /* do nothing */;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001705 } else if (add_happened && (aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001706 dev_info(&pf->pdev->dev,
1707 "add filter failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001708 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001709 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1710 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1711 &vsi->state)) {
1712 promisc_forced_on = true;
1713 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1714 &vsi->state);
1715 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1716 }
1717 }
1718 }
1719
1720 /* check for changes in promiscuous modes */
1721 if (changed_flags & IFF_ALLMULTI) {
1722 bool cur_multipromisc;
1723 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001724 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1725 vsi->seid,
1726 cur_multipromisc,
1727 NULL);
1728 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001729 dev_info(&pf->pdev->dev,
1730 "set multi promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001731 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001732 }
1733 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1734 bool cur_promisc;
1735 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1736 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1737 &vsi->state));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001738 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1739 vsi->seid,
1740 cur_promisc, NULL);
1741 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001742 dev_info(&pf->pdev->dev,
1743 "set uni promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001744 aq_ret, pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00001745 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1746 vsi->seid,
1747 cur_promisc, NULL);
1748 if (aq_ret)
1749 dev_info(&pf->pdev->dev,
1750 "set brdcast promisc failed, err %d, aq_err %d\n",
1751 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001752 }
1753
1754 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1755 return 0;
1756}
1757
1758/**
1759 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1760 * @pf: board private structure
1761 **/
1762static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1763{
1764 int v;
1765
1766 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1767 return;
1768 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1769
Mitch Williams505682c2014-05-20 08:01:37 +00001770 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001771 if (pf->vsi[v] &&
1772 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1773 i40e_sync_vsi_filters(pf->vsi[v]);
1774 }
1775}
1776
1777/**
1778 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1779 * @netdev: network interface device structure
1780 * @new_mtu: new value for maximum frame size
1781 *
1782 * Returns 0 on success, negative on failure
1783 **/
1784static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1785{
1786 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00001787 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001788 struct i40e_vsi *vsi = np->vsi;
1789
1790 /* MTU < 68 is an error and causes problems on some kernels */
1791 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1792 return -EINVAL;
1793
1794 netdev_info(netdev, "changing MTU from %d to %d\n",
1795 netdev->mtu, new_mtu);
1796 netdev->mtu = new_mtu;
1797 if (netif_running(netdev))
1798 i40e_vsi_reinit_locked(vsi);
1799
1800 return 0;
1801}
1802
1803/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001804 * i40e_ioctl - Access the hwtstamp interface
1805 * @netdev: network interface device structure
1806 * @ifr: interface request data
1807 * @cmd: ioctl command
1808 **/
1809int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1810{
1811 struct i40e_netdev_priv *np = netdev_priv(netdev);
1812 struct i40e_pf *pf = np->vsi->back;
1813
1814 switch (cmd) {
1815 case SIOCGHWTSTAMP:
1816 return i40e_ptp_get_ts_config(pf, ifr);
1817 case SIOCSHWTSTAMP:
1818 return i40e_ptp_set_ts_config(pf, ifr);
1819 default:
1820 return -EOPNOTSUPP;
1821 }
1822}
1823
1824/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001825 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1826 * @vsi: the vsi being adjusted
1827 **/
1828void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1829{
1830 struct i40e_vsi_context ctxt;
1831 i40e_status ret;
1832
1833 if ((vsi->info.valid_sections &
1834 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1835 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1836 return; /* already enabled */
1837
1838 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1839 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1840 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1841
1842 ctxt.seid = vsi->seid;
1843 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1844 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1845 if (ret) {
1846 dev_info(&vsi->back->pdev->dev,
1847 "%s: update vsi failed, aq_err=%d\n",
1848 __func__, vsi->back->hw.aq.asq_last_status);
1849 }
1850}
1851
1852/**
1853 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1854 * @vsi: the vsi being adjusted
1855 **/
1856void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
1857{
1858 struct i40e_vsi_context ctxt;
1859 i40e_status ret;
1860
1861 if ((vsi->info.valid_sections &
1862 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1863 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
1864 I40E_AQ_VSI_PVLAN_EMOD_MASK))
1865 return; /* already disabled */
1866
1867 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1868 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1869 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
1870
1871 ctxt.seid = vsi->seid;
1872 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1873 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1874 if (ret) {
1875 dev_info(&vsi->back->pdev->dev,
1876 "%s: update vsi failed, aq_err=%d\n",
1877 __func__, vsi->back->hw.aq.asq_last_status);
1878 }
1879}
1880
1881/**
1882 * i40e_vlan_rx_register - Setup or shutdown vlan offload
1883 * @netdev: network interface to be adjusted
1884 * @features: netdev features to test if VLAN offload is enabled or not
1885 **/
1886static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
1887{
1888 struct i40e_netdev_priv *np = netdev_priv(netdev);
1889 struct i40e_vsi *vsi = np->vsi;
1890
1891 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1892 i40e_vlan_stripping_enable(vsi);
1893 else
1894 i40e_vlan_stripping_disable(vsi);
1895}
1896
1897/**
1898 * i40e_vsi_add_vlan - Add vsi membership for given vlan
1899 * @vsi: the vsi being configured
1900 * @vid: vlan id to be added (0 = untagged only , -1 = any)
1901 **/
1902int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
1903{
1904 struct i40e_mac_filter *f, *add_f;
1905 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001906
1907 is_vf = (vsi->type == I40E_VSI_SRIOV);
1908 is_netdev = !!(vsi->netdev);
1909
1910 if (is_netdev) {
1911 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
1912 is_vf, is_netdev);
1913 if (!add_f) {
1914 dev_info(&vsi->back->pdev->dev,
1915 "Could not add vlan filter %d for %pM\n",
1916 vid, vsi->netdev->dev_addr);
1917 return -ENOMEM;
1918 }
1919 }
1920
1921 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1922 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1923 if (!add_f) {
1924 dev_info(&vsi->back->pdev->dev,
1925 "Could not add vlan filter %d for %pM\n",
1926 vid, f->macaddr);
1927 return -ENOMEM;
1928 }
1929 }
1930
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001931 /* Now if we add a vlan tag, make sure to check if it is the first
1932 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
1933 * with 0, so we now accept untagged and specified tagged traffic
1934 * (and not any taged and untagged)
1935 */
1936 if (vid > 0) {
1937 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
1938 I40E_VLAN_ANY,
1939 is_vf, is_netdev)) {
1940 i40e_del_filter(vsi, vsi->netdev->dev_addr,
1941 I40E_VLAN_ANY, is_vf, is_netdev);
1942 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
1943 is_vf, is_netdev);
1944 if (!add_f) {
1945 dev_info(&vsi->back->pdev->dev,
1946 "Could not add filter 0 for %pM\n",
1947 vsi->netdev->dev_addr);
1948 return -ENOMEM;
1949 }
1950 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08001951 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001952
Greg Rose8d82a7c2014-01-13 16:13:04 -08001953 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
1954 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001955 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1956 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1957 is_vf, is_netdev)) {
1958 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1959 is_vf, is_netdev);
1960 add_f = i40e_add_filter(vsi, f->macaddr,
1961 0, is_vf, is_netdev);
1962 if (!add_f) {
1963 dev_info(&vsi->back->pdev->dev,
1964 "Could not add filter 0 for %pM\n",
1965 f->macaddr);
1966 return -ENOMEM;
1967 }
1968 }
1969 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001970 }
1971
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001972 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1973 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1974 return 0;
1975
1976 return i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001977}
1978
1979/**
1980 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
1981 * @vsi: the vsi being configured
1982 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001983 *
1984 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001985 **/
1986int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
1987{
1988 struct net_device *netdev = vsi->netdev;
1989 struct i40e_mac_filter *f, *add_f;
1990 bool is_vf, is_netdev;
1991 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001992
1993 is_vf = (vsi->type == I40E_VSI_SRIOV);
1994 is_netdev = !!(netdev);
1995
1996 if (is_netdev)
1997 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
1998
1999 list_for_each_entry(f, &vsi->mac_filter_list, list)
2000 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2001
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002002 /* go through all the filters for this VSI and if there is only
2003 * vid == 0 it means there are no other filters, so vid 0 must
2004 * be replaced with -1. This signifies that we should from now
2005 * on accept any traffic (with any tag present, or untagged)
2006 */
2007 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2008 if (is_netdev) {
2009 if (f->vlan &&
2010 ether_addr_equal(netdev->dev_addr, f->macaddr))
2011 filter_count++;
2012 }
2013
2014 if (f->vlan)
2015 filter_count++;
2016 }
2017
2018 if (!filter_count && is_netdev) {
2019 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2020 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2021 is_vf, is_netdev);
2022 if (!f) {
2023 dev_info(&vsi->back->pdev->dev,
2024 "Could not add filter %d for %pM\n",
2025 I40E_VLAN_ANY, netdev->dev_addr);
2026 return -ENOMEM;
2027 }
2028 }
2029
2030 if (!filter_count) {
2031 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2032 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2033 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2034 is_vf, is_netdev);
2035 if (!add_f) {
2036 dev_info(&vsi->back->pdev->dev,
2037 "Could not add filter %d for %pM\n",
2038 I40E_VLAN_ANY, f->macaddr);
2039 return -ENOMEM;
2040 }
2041 }
2042 }
2043
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002044 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2045 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2046 return 0;
2047
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002048 return i40e_sync_vsi_filters(vsi);
2049}
2050
2051/**
2052 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2053 * @netdev: network interface to be adjusted
2054 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002055 *
2056 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002057 **/
2058static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2059 __always_unused __be16 proto, u16 vid)
2060{
2061 struct i40e_netdev_priv *np = netdev_priv(netdev);
2062 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002063 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002064
2065 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002066 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002067
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002068 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2069
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002070 /* If the network stack called us with vid = 0 then
2071 * it is asking to receive priority tagged packets with
2072 * vlan id 0. Our HW receives them by default when configured
2073 * to receive untagged packets so there is no need to add an
2074 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002075 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002076 if (vid)
2077 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002078
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002079 if (!ret && (vid < VLAN_N_VID))
2080 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002081
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002082 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002083}
2084
2085/**
2086 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2087 * @netdev: network interface to be adjusted
2088 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002089 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002090 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002091 **/
2092static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2093 __always_unused __be16 proto, u16 vid)
2094{
2095 struct i40e_netdev_priv *np = netdev_priv(netdev);
2096 struct i40e_vsi *vsi = np->vsi;
2097
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002098 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2099
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002100 /* return code is ignored as there is nothing a user
2101 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002102 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002103 */
2104 i40e_vsi_kill_vlan(vsi, vid);
2105
2106 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002107
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002108 return 0;
2109}
2110
2111/**
2112 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2113 * @vsi: the vsi being brought back up
2114 **/
2115static void i40e_restore_vlan(struct i40e_vsi *vsi)
2116{
2117 u16 vid;
2118
2119 if (!vsi->netdev)
2120 return;
2121
2122 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2123
2124 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2125 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2126 vid);
2127}
2128
2129/**
2130 * i40e_vsi_add_pvid - Add pvid for the VSI
2131 * @vsi: the vsi being adjusted
2132 * @vid: the vlan id to set as a PVID
2133 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002134int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002135{
2136 struct i40e_vsi_context ctxt;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002137 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002138
2139 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2140 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002141 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2142 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002143 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002144
2145 ctxt.seid = vsi->seid;
2146 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002147 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2148 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002149 dev_info(&vsi->back->pdev->dev,
2150 "%s: update vsi failed, aq_err=%d\n",
2151 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002152 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002153 }
2154
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002155 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002156}
2157
2158/**
2159 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2160 * @vsi: the vsi being adjusted
2161 *
2162 * Just use the vlan_rx_register() service to put it back to normal
2163 **/
2164void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2165{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002166 i40e_vlan_stripping_disable(vsi);
2167
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002168 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002169}
2170
2171/**
2172 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2173 * @vsi: ptr to the VSI
2174 *
2175 * If this function returns with an error, then it's possible one or
2176 * more of the rings is populated (while the rest are not). It is the
2177 * callers duty to clean those orphaned rings.
2178 *
2179 * Return 0 on success, negative on failure
2180 **/
2181static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2182{
2183 int i, err = 0;
2184
2185 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002186 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002187
2188 return err;
2189}
2190
2191/**
2192 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2193 * @vsi: ptr to the VSI
2194 *
2195 * Free VSI's transmit software resources
2196 **/
2197static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2198{
2199 int i;
2200
Greg Rose8e9dca52013-12-18 13:45:53 +00002201 if (!vsi->tx_rings)
2202 return;
2203
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002204 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002205 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002206 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002207}
2208
2209/**
2210 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2211 * @vsi: ptr to the VSI
2212 *
2213 * If this function returns with an error, then it's possible one or
2214 * more of the rings is populated (while the rest are not). It is the
2215 * callers duty to clean those orphaned rings.
2216 *
2217 * Return 0 on success, negative on failure
2218 **/
2219static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2220{
2221 int i, err = 0;
2222
2223 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002224 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002225 return err;
2226}
2227
2228/**
2229 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2230 * @vsi: ptr to the VSI
2231 *
2232 * Free all receive software resources
2233 **/
2234static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2235{
2236 int i;
2237
Greg Rose8e9dca52013-12-18 13:45:53 +00002238 if (!vsi->rx_rings)
2239 return;
2240
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002241 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002242 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002243 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002244}
2245
2246/**
2247 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2248 * @ring: The Tx ring to configure
2249 *
2250 * Configure the Tx descriptor ring in the HMC context.
2251 **/
2252static int i40e_configure_tx_ring(struct i40e_ring *ring)
2253{
2254 struct i40e_vsi *vsi = ring->vsi;
2255 u16 pf_q = vsi->base_queue + ring->queue_index;
2256 struct i40e_hw *hw = &vsi->back->hw;
2257 struct i40e_hmc_obj_txq tx_ctx;
2258 i40e_status err = 0;
2259 u32 qtx_ctl = 0;
2260
2261 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002262 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002263 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2264 ring->atr_count = 0;
2265 } else {
2266 ring->atr_sample_rate = 0;
2267 }
2268
2269 /* initialize XPS */
2270 if (ring->q_vector && ring->netdev &&
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08002271 vsi->tc_config.numtc <= 1 &&
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002272 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
2273 netif_set_xps_queue(ring->netdev,
2274 &ring->q_vector->affinity_mask,
2275 ring->queue_index);
2276
2277 /* clear the context structure first */
2278 memset(&tx_ctx, 0, sizeof(tx_ctx));
2279
2280 tx_ctx.new_context = 1;
2281 tx_ctx.base = (ring->dma / 128);
2282 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002283 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2284 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002285 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002286 /* FDIR VSI tx ring can still use RS bit and writebacks */
2287 if (vsi->type != I40E_VSI_FDIR)
2288 tx_ctx.head_wb_ena = 1;
2289 tx_ctx.head_wb_addr = ring->dma +
2290 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002291
2292 /* As part of VSI creation/update, FW allocates certain
2293 * Tx arbitration queue sets for each TC enabled for
2294 * the VSI. The FW returns the handles to these queue
2295 * sets as part of the response buffer to Add VSI,
2296 * Update VSI, etc. AQ commands. It is expected that
2297 * these queue set handles be associated with the Tx
2298 * queues by the driver as part of the TX queue context
2299 * initialization. This has to be done regardless of
2300 * DCB as by default everything is mapped to TC0.
2301 */
2302 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2303 tx_ctx.rdylist_act = 0;
2304
2305 /* clear the context in the HMC */
2306 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2307 if (err) {
2308 dev_info(&vsi->back->pdev->dev,
2309 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2310 ring->queue_index, pf_q, err);
2311 return -ENOMEM;
2312 }
2313
2314 /* set the context in the HMC */
2315 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2316 if (err) {
2317 dev_info(&vsi->back->pdev->dev,
2318 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2319 ring->queue_index, pf_q, err);
2320 return -ENOMEM;
2321 }
2322
2323 /* Now associate this queue with this PCI function */
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002324 if (vsi->type == I40E_VSI_VMDQ2)
2325 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
2326 else
2327 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +00002328 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2329 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002330 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2331 i40e_flush(hw);
2332
2333 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2334
2335 /* cache tail off for easier writes later */
2336 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2337
2338 return 0;
2339}
2340
2341/**
2342 * i40e_configure_rx_ring - Configure a receive ring context
2343 * @ring: The Rx ring to configure
2344 *
2345 * Configure the Rx descriptor ring in the HMC context.
2346 **/
2347static int i40e_configure_rx_ring(struct i40e_ring *ring)
2348{
2349 struct i40e_vsi *vsi = ring->vsi;
2350 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2351 u16 pf_q = vsi->base_queue + ring->queue_index;
2352 struct i40e_hw *hw = &vsi->back->hw;
2353 struct i40e_hmc_obj_rxq rx_ctx;
2354 i40e_status err = 0;
2355
2356 ring->state = 0;
2357
2358 /* clear the context structure first */
2359 memset(&rx_ctx, 0, sizeof(rx_ctx));
2360
2361 ring->rx_buf_len = vsi->rx_buf_len;
2362 ring->rx_hdr_len = vsi->rx_hdr_len;
2363
2364 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2365 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2366
2367 rx_ctx.base = (ring->dma / 128);
2368 rx_ctx.qlen = ring->count;
2369
2370 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2371 set_ring_16byte_desc_enabled(ring);
2372 rx_ctx.dsize = 0;
2373 } else {
2374 rx_ctx.dsize = 1;
2375 }
2376
2377 rx_ctx.dtype = vsi->dtype;
2378 if (vsi->dtype) {
2379 set_ring_ps_enabled(ring);
2380 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2381 I40E_RX_SPLIT_IP |
2382 I40E_RX_SPLIT_TCP_UDP |
2383 I40E_RX_SPLIT_SCTP;
2384 } else {
2385 rx_ctx.hsplit_0 = 0;
2386 }
2387
2388 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2389 (chain_len * ring->rx_buf_len));
2390 rx_ctx.tphrdesc_ena = 1;
2391 rx_ctx.tphwdesc_ena = 1;
2392 rx_ctx.tphdata_ena = 1;
2393 rx_ctx.tphhead_ena = 1;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002394 if (hw->revision_id == 0)
2395 rx_ctx.lrxqthresh = 0;
2396 else
2397 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002398 rx_ctx.crcstrip = 1;
2399 rx_ctx.l2tsel = 1;
2400 rx_ctx.showiv = 1;
Catherine Sullivanacb36762014-03-06 09:02:30 +00002401 /* set the prefena field to 1 because the manual says to */
2402 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002403
2404 /* clear the context in the HMC */
2405 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2406 if (err) {
2407 dev_info(&vsi->back->pdev->dev,
2408 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2409 ring->queue_index, pf_q, err);
2410 return -ENOMEM;
2411 }
2412
2413 /* set the context in the HMC */
2414 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2415 if (err) {
2416 dev_info(&vsi->back->pdev->dev,
2417 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2418 ring->queue_index, pf_q, err);
2419 return -ENOMEM;
2420 }
2421
2422 /* cache tail for quicker writes, and clear the reg before use */
2423 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2424 writel(0, ring->tail);
2425
2426 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
2427
2428 return 0;
2429}
2430
2431/**
2432 * i40e_vsi_configure_tx - Configure the VSI for Tx
2433 * @vsi: VSI structure describing this set of rings and resources
2434 *
2435 * Configure the Tx VSI for operation.
2436 **/
2437static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2438{
2439 int err = 0;
2440 u16 i;
2441
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002442 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2443 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002444
2445 return err;
2446}
2447
2448/**
2449 * i40e_vsi_configure_rx - Configure the VSI for Rx
2450 * @vsi: the VSI being configured
2451 *
2452 * Configure the Rx VSI for operation.
2453 **/
2454static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2455{
2456 int err = 0;
2457 u16 i;
2458
2459 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2460 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2461 + ETH_FCS_LEN + VLAN_HLEN;
2462 else
2463 vsi->max_frame = I40E_RXBUFFER_2048;
2464
2465 /* figure out correct receive buffer length */
2466 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2467 I40E_FLAG_RX_PS_ENABLED)) {
2468 case I40E_FLAG_RX_1BUF_ENABLED:
2469 vsi->rx_hdr_len = 0;
2470 vsi->rx_buf_len = vsi->max_frame;
2471 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2472 break;
2473 case I40E_FLAG_RX_PS_ENABLED:
2474 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2475 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2476 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2477 break;
2478 default:
2479 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2480 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2481 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2482 break;
2483 }
2484
2485 /* round up for the chip's needs */
2486 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2487 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2488 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2489 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2490
2491 /* set up individual rings */
2492 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002493 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002494
2495 return err;
2496}
2497
2498/**
2499 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2500 * @vsi: ptr to the VSI
2501 **/
2502static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2503{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002504 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002505 u16 qoffset, qcount;
2506 int i, n;
2507
2508 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2509 return;
2510
2511 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2512 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2513 continue;
2514
2515 qoffset = vsi->tc_config.tc_info[n].qoffset;
2516 qcount = vsi->tc_config.tc_info[n].qcount;
2517 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002518 rx_ring = vsi->rx_rings[i];
2519 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002520 rx_ring->dcb_tc = n;
2521 tx_ring->dcb_tc = n;
2522 }
2523 }
2524}
2525
2526/**
2527 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2528 * @vsi: ptr to the VSI
2529 **/
2530static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2531{
2532 if (vsi->netdev)
2533 i40e_set_rx_mode(vsi->netdev);
2534}
2535
2536/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002537 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
2538 * @vsi: Pointer to the targeted VSI
2539 *
2540 * This function replays the hlist on the hw where all the SB Flow Director
2541 * filters were saved.
2542 **/
2543static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
2544{
2545 struct i40e_fdir_filter *filter;
2546 struct i40e_pf *pf = vsi->back;
2547 struct hlist_node *node;
2548
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002549 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2550 return;
2551
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002552 hlist_for_each_entry_safe(filter, node,
2553 &pf->fdir_filter_list, fdir_node) {
2554 i40e_add_del_fdir(vsi, filter, true);
2555 }
2556}
2557
2558/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002559 * i40e_vsi_configure - Set up the VSI for action
2560 * @vsi: the VSI being configured
2561 **/
2562static int i40e_vsi_configure(struct i40e_vsi *vsi)
2563{
2564 int err;
2565
2566 i40e_set_vsi_rx_mode(vsi);
2567 i40e_restore_vlan(vsi);
2568 i40e_vsi_config_dcb_rings(vsi);
2569 err = i40e_vsi_configure_tx(vsi);
2570 if (!err)
2571 err = i40e_vsi_configure_rx(vsi);
2572
2573 return err;
2574}
2575
2576/**
2577 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2578 * @vsi: the VSI being configured
2579 **/
2580static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2581{
2582 struct i40e_pf *pf = vsi->back;
2583 struct i40e_q_vector *q_vector;
2584 struct i40e_hw *hw = &pf->hw;
2585 u16 vector;
2586 int i, q;
2587 u32 val;
2588 u32 qp;
2589
2590 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2591 * and PFINT_LNKLSTn registers, e.g.:
2592 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2593 */
2594 qp = vsi->base_queue;
2595 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002596 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2597 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002598 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2599 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2600 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2601 q_vector->rx.itr);
2602 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2603 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2604 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2605 q_vector->tx.itr);
2606
2607 /* Linked list for the queuepairs assigned to this vector */
2608 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2609 for (q = 0; q < q_vector->num_ringpairs; q++) {
2610 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2611 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2612 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2613 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2614 (I40E_QUEUE_TYPE_TX
2615 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2616
2617 wr32(hw, I40E_QINT_RQCTL(qp), val);
2618
2619 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2620 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2621 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2622 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2623 (I40E_QUEUE_TYPE_RX
2624 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2625
2626 /* Terminate the linked list */
2627 if (q == (q_vector->num_ringpairs - 1))
2628 val |= (I40E_QUEUE_END_OF_LIST
2629 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2630
2631 wr32(hw, I40E_QINT_TQCTL(qp), val);
2632 qp++;
2633 }
2634 }
2635
2636 i40e_flush(hw);
2637}
2638
2639/**
2640 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2641 * @hw: ptr to the hardware info
2642 **/
2643static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2644{
2645 u32 val;
2646
2647 /* clear things first */
2648 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2649 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2650
2651 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2652 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2653 I40E_PFINT_ICR0_ENA_GRST_MASK |
2654 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2655 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002656 I40E_PFINT_ICR0_ENA_TIMESYNC_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002657 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2658 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2659 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2660
2661 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2662
2663 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002664 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2665 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002666
2667 /* OTHER_ITR_IDX = 0 */
2668 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2669}
2670
2671/**
2672 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2673 * @vsi: the VSI being configured
2674 **/
2675static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2676{
Alexander Duyck493fb302013-09-28 07:01:44 +00002677 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002678 struct i40e_pf *pf = vsi->back;
2679 struct i40e_hw *hw = &pf->hw;
2680 u32 val;
2681
2682 /* set the ITR configuration */
2683 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2684 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2685 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2686 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2687 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2688 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2689
2690 i40e_enable_misc_int_causes(hw);
2691
2692 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2693 wr32(hw, I40E_PFINT_LNKLST0, 0);
2694
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00002695 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002696 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2697 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2698 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2699
2700 wr32(hw, I40E_QINT_RQCTL(0), val);
2701
2702 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2703 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2704 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2705
2706 wr32(hw, I40E_QINT_TQCTL(0), val);
2707 i40e_flush(hw);
2708}
2709
2710/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00002711 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2712 * @pf: board private structure
2713 **/
2714void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2715{
2716 struct i40e_hw *hw = &pf->hw;
2717
2718 wr32(hw, I40E_PFINT_DYN_CTL0,
2719 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2720 i40e_flush(hw);
2721}
2722
2723/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002724 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2725 * @pf: board private structure
2726 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002727void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002728{
2729 struct i40e_hw *hw = &pf->hw;
2730 u32 val;
2731
2732 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2733 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2734 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2735
2736 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2737 i40e_flush(hw);
2738}
2739
2740/**
2741 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2742 * @vsi: pointer to a vsi
2743 * @vector: enable a particular Hw Interrupt vector
2744 **/
2745void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2746{
2747 struct i40e_pf *pf = vsi->back;
2748 struct i40e_hw *hw = &pf->hw;
2749 u32 val;
2750
2751 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2752 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2753 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2754 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002755 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002756}
2757
2758/**
2759 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2760 * @irq: interrupt number
2761 * @data: pointer to a q_vector
2762 **/
2763static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2764{
2765 struct i40e_q_vector *q_vector = data;
2766
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002767 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002768 return IRQ_HANDLED;
2769
2770 napi_schedule(&q_vector->napi);
2771
2772 return IRQ_HANDLED;
2773}
2774
2775/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002776 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2777 * @vsi: the VSI being configured
2778 * @basename: name for the vector
2779 *
2780 * Allocates MSI-X vectors and requests interrupts from the kernel.
2781 **/
2782static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2783{
2784 int q_vectors = vsi->num_q_vectors;
2785 struct i40e_pf *pf = vsi->back;
2786 int base = vsi->base_vector;
2787 int rx_int_idx = 0;
2788 int tx_int_idx = 0;
2789 int vector, err;
2790
2791 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00002792 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002793
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002794 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002795 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2796 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2797 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002798 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002799 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2800 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002801 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002802 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2803 "%s-%s-%d", basename, "tx", tx_int_idx++);
2804 } else {
2805 /* skip this unused q_vector */
2806 continue;
2807 }
2808 err = request_irq(pf->msix_entries[base + vector].vector,
2809 vsi->irq_handler,
2810 0,
2811 q_vector->name,
2812 q_vector);
2813 if (err) {
2814 dev_info(&pf->pdev->dev,
2815 "%s: request_irq failed, error: %d\n",
2816 __func__, err);
2817 goto free_queue_irqs;
2818 }
2819 /* assign the mask for this irq */
2820 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2821 &q_vector->affinity_mask);
2822 }
2823
Shannon Nelson63741842014-04-23 04:50:16 +00002824 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002825 return 0;
2826
2827free_queue_irqs:
2828 while (vector) {
2829 vector--;
2830 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2831 NULL);
2832 free_irq(pf->msix_entries[base + vector].vector,
2833 &(vsi->q_vectors[vector]));
2834 }
2835 return err;
2836}
2837
2838/**
2839 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2840 * @vsi: the VSI being un-configured
2841 **/
2842static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2843{
2844 struct i40e_pf *pf = vsi->back;
2845 struct i40e_hw *hw = &pf->hw;
2846 int base = vsi->base_vector;
2847 int i;
2848
2849 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002850 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2851 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002852 }
2853
2854 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2855 for (i = vsi->base_vector;
2856 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2857 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2858
2859 i40e_flush(hw);
2860 for (i = 0; i < vsi->num_q_vectors; i++)
2861 synchronize_irq(pf->msix_entries[i + base].vector);
2862 } else {
2863 /* Legacy and MSI mode - this stops all interrupt handling */
2864 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2865 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2866 i40e_flush(hw);
2867 synchronize_irq(pf->pdev->irq);
2868 }
2869}
2870
2871/**
2872 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2873 * @vsi: the VSI being configured
2874 **/
2875static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2876{
2877 struct i40e_pf *pf = vsi->back;
2878 int i;
2879
2880 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2881 for (i = vsi->base_vector;
2882 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2883 i40e_irq_dynamic_enable(vsi, i);
2884 } else {
2885 i40e_irq_dynamic_enable_icr0(pf);
2886 }
2887
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002888 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002889 return 0;
2890}
2891
2892/**
2893 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2894 * @pf: board private structure
2895 **/
2896static void i40e_stop_misc_vector(struct i40e_pf *pf)
2897{
2898 /* Disable ICR 0 */
2899 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2900 i40e_flush(&pf->hw);
2901}
2902
2903/**
2904 * i40e_intr - MSI/Legacy and non-queue interrupt handler
2905 * @irq: interrupt number
2906 * @data: pointer to a q_vector
2907 *
2908 * This is the handler used for all MSI/Legacy interrupts, and deals
2909 * with both queue and non-queue interrupts. This is also used in
2910 * MSIX mode to handle the non-queue interrupts.
2911 **/
2912static irqreturn_t i40e_intr(int irq, void *data)
2913{
2914 struct i40e_pf *pf = (struct i40e_pf *)data;
2915 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002916 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002917 u32 icr0, icr0_remaining;
2918 u32 val, ena_mask;
2919
2920 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002921 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002922
Shannon Nelson116a57d2013-09-28 07:13:59 +00002923 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2924 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002925 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002926
Shannon Nelsoncd92e722013-11-16 10:00:44 +00002927 /* if interrupt but no bits showing, must be SWINT */
2928 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2929 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2930 pf->sw_int_count++;
2931
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002932 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2933 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2934
2935 /* temporarily disable queue cause for NAPI processing */
2936 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2937 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2938 wr32(hw, I40E_QINT_RQCTL(0), qval);
2939
2940 qval = rd32(hw, I40E_QINT_TQCTL(0));
2941 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2942 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002943
2944 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00002945 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002946 }
2947
2948 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2949 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2950 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2951 }
2952
2953 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2954 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2955 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2956 }
2957
2958 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2959 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2960 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2961 }
2962
2963 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2964 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2965 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2966 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2967 val = rd32(hw, I40E_GLGEN_RSTAT);
2968 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2969 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002970 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002971 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002972 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002973 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002974 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002975 pf->empr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002976 set_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
2977 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002978 }
2979
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002980 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2981 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
2982 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2983 }
2984
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002985 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
2986 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
2987
2988 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07002989 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002990 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002991 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002992 }
2993
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002994 /* If a critical error is pending we have no choice but to reset the
2995 * device.
2996 * Report and mask out any remaining unexpected interrupts.
2997 */
2998 icr0_remaining = icr0 & ena_mask;
2999 if (icr0_remaining) {
3000 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3001 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003002 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003003 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003004 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003005 dev_info(&pf->pdev->dev, "device will be reset\n");
3006 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3007 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003008 }
3009 ena_mask &= ~icr0_remaining;
3010 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003011 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003012
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003013enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003014 /* re-enable interrupt causes */
3015 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003016 if (!test_bit(__I40E_DOWN, &pf->state)) {
3017 i40e_service_event_schedule(pf);
3018 i40e_irq_dynamic_enable_icr0(pf);
3019 }
3020
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003021 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003022}
3023
3024/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003025 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3026 * @tx_ring: tx ring to clean
3027 * @budget: how many cleans we're allowed
3028 *
3029 * Returns true if there's any budget left (e.g. the clean is finished)
3030 **/
3031static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3032{
3033 struct i40e_vsi *vsi = tx_ring->vsi;
3034 u16 i = tx_ring->next_to_clean;
3035 struct i40e_tx_buffer *tx_buf;
3036 struct i40e_tx_desc *tx_desc;
3037
3038 tx_buf = &tx_ring->tx_bi[i];
3039 tx_desc = I40E_TX_DESC(tx_ring, i);
3040 i -= tx_ring->count;
3041
3042 do {
3043 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3044
3045 /* if next_to_watch is not set then there is no work pending */
3046 if (!eop_desc)
3047 break;
3048
3049 /* prevent any other reads prior to eop_desc */
3050 read_barrier_depends();
3051
3052 /* if the descriptor isn't done, no work yet to do */
3053 if (!(eop_desc->cmd_type_offset_bsz &
3054 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3055 break;
3056
3057 /* clear next_to_watch to prevent false hangs */
3058 tx_buf->next_to_watch = NULL;
3059
3060 /* unmap skb header data */
3061 dma_unmap_single(tx_ring->dev,
3062 dma_unmap_addr(tx_buf, dma),
3063 dma_unmap_len(tx_buf, len),
3064 DMA_TO_DEVICE);
3065
3066 dma_unmap_len_set(tx_buf, len, 0);
3067
3068
3069 /* move to the next desc and buffer to clean */
3070 tx_buf++;
3071 tx_desc++;
3072 i++;
3073 if (unlikely(!i)) {
3074 i -= tx_ring->count;
3075 tx_buf = tx_ring->tx_bi;
3076 tx_desc = I40E_TX_DESC(tx_ring, 0);
3077 }
3078
3079 /* update budget accounting */
3080 budget--;
3081 } while (likely(budget));
3082
3083 i += tx_ring->count;
3084 tx_ring->next_to_clean = i;
3085
3086 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
3087 i40e_irq_dynamic_enable(vsi,
3088 tx_ring->q_vector->v_idx + vsi->base_vector);
3089 }
3090 return budget > 0;
3091}
3092
3093/**
3094 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3095 * @irq: interrupt number
3096 * @data: pointer to a q_vector
3097 **/
3098static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3099{
3100 struct i40e_q_vector *q_vector = data;
3101 struct i40e_vsi *vsi;
3102
3103 if (!q_vector->tx.ring)
3104 return IRQ_HANDLED;
3105
3106 vsi = q_vector->tx.ring->vsi;
3107 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3108
3109 return IRQ_HANDLED;
3110}
3111
3112/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003113 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003114 * @vsi: the VSI being configured
3115 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003116 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003117 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003118static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003119{
Alexander Duyck493fb302013-09-28 07:01:44 +00003120 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003121 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3122 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003123
3124 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003125 tx_ring->next = q_vector->tx.ring;
3126 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003127 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003128
3129 rx_ring->q_vector = q_vector;
3130 rx_ring->next = q_vector->rx.ring;
3131 q_vector->rx.ring = rx_ring;
3132 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003133}
3134
3135/**
3136 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3137 * @vsi: the VSI being configured
3138 *
3139 * This function maps descriptor rings to the queue-specific vectors
3140 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3141 * one vector per queue pair, but on a constrained vector budget, we
3142 * group the queue pairs as "efficiently" as possible.
3143 **/
3144static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3145{
3146 int qp_remaining = vsi->num_queue_pairs;
3147 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003148 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003149 int v_start = 0;
3150 int qp_idx = 0;
3151
3152 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3153 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003154 * It is also important to go through all the vectors available to be
3155 * sure that if we don't use all the vectors, that the remaining vectors
3156 * are cleared. This is especially important when decreasing the
3157 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003158 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003159 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003160 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3161
3162 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3163
3164 q_vector->num_ringpairs = num_ringpairs;
3165
3166 q_vector->rx.count = 0;
3167 q_vector->tx.count = 0;
3168 q_vector->rx.ring = NULL;
3169 q_vector->tx.ring = NULL;
3170
3171 while (num_ringpairs--) {
3172 map_vector_to_qp(vsi, v_start, qp_idx);
3173 qp_idx++;
3174 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003175 }
3176 }
3177}
3178
3179/**
3180 * i40e_vsi_request_irq - Request IRQ from the OS
3181 * @vsi: the VSI being configured
3182 * @basename: name for the vector
3183 **/
3184static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3185{
3186 struct i40e_pf *pf = vsi->back;
3187 int err;
3188
3189 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3190 err = i40e_vsi_request_irq_msix(vsi, basename);
3191 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3192 err = request_irq(pf->pdev->irq, i40e_intr, 0,
3193 pf->misc_int_name, pf);
3194 else
3195 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
3196 pf->misc_int_name, pf);
3197
3198 if (err)
3199 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3200
3201 return err;
3202}
3203
3204#ifdef CONFIG_NET_POLL_CONTROLLER
3205/**
3206 * i40e_netpoll - A Polling 'interrupt'handler
3207 * @netdev: network interface device structure
3208 *
3209 * This is used by netconsole to send skbs without having to re-enable
3210 * interrupts. It's not called while the normal interrupt routine is executing.
3211 **/
3212static void i40e_netpoll(struct net_device *netdev)
3213{
3214 struct i40e_netdev_priv *np = netdev_priv(netdev);
3215 struct i40e_vsi *vsi = np->vsi;
3216 struct i40e_pf *pf = vsi->back;
3217 int i;
3218
3219 /* if interface is down do nothing */
3220 if (test_bit(__I40E_DOWN, &vsi->state))
3221 return;
3222
3223 pf->flags |= I40E_FLAG_IN_NETPOLL;
3224 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3225 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003226 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003227 } else {
3228 i40e_intr(pf->pdev->irq, netdev);
3229 }
3230 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3231}
3232#endif
3233
3234/**
Neerav Parikh23527302014-06-03 23:50:15 +00003235 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3236 * @pf: the PF being configured
3237 * @pf_q: the PF queue
3238 * @enable: enable or disable state of the queue
3239 *
3240 * This routine will wait for the given Tx queue of the PF to reach the
3241 * enabled or disabled state.
3242 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3243 * multiple retries; else will return 0 in case of success.
3244 **/
3245static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3246{
3247 int i;
3248 u32 tx_reg;
3249
3250 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3251 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3252 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3253 break;
3254
3255 udelay(10);
3256 }
3257 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3258 return -ETIMEDOUT;
3259
3260 return 0;
3261}
3262
3263/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003264 * i40e_vsi_control_tx - Start or stop a VSI's rings
3265 * @vsi: the VSI being configured
3266 * @enable: start or stop the rings
3267 **/
3268static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3269{
3270 struct i40e_pf *pf = vsi->back;
3271 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003272 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003273 u32 tx_reg;
3274
3275 pf_q = vsi->base_queue;
3276 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499a2014-04-23 04:50:03 +00003277
3278 /* warn the TX unit of coming changes */
3279 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3280 if (!enable)
3281 udelay(10);
3282
Mitch Williams6c5ef622014-02-20 19:29:16 -08003283 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003284 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003285 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3286 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3287 break;
3288 usleep_range(1000, 2000);
3289 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003290 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003291 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003292 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003293
3294 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003295 if (enable) {
3296 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003297 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003298 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003299 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003300 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003301
3302 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3303
3304 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003305 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3306 if (ret) {
3307 dev_info(&pf->pdev->dev,
3308 "%s: VSI seid %d Tx ring %d %sable timeout\n",
3309 __func__, vsi->seid, pf_q,
3310 (enable ? "en" : "dis"));
3311 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003312 }
3313 }
3314
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003315 if (hw->revision_id == 0)
3316 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003317 return ret;
3318}
3319
3320/**
3321 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3322 * @pf: the PF being configured
3323 * @pf_q: the PF queue
3324 * @enable: enable or disable state of the queue
3325 *
3326 * This routine will wait for the given Rx queue of the PF to reach the
3327 * enabled or disabled state.
3328 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3329 * multiple retries; else will return 0 in case of success.
3330 **/
3331static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3332{
3333 int i;
3334 u32 rx_reg;
3335
3336 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3337 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3338 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3339 break;
3340
3341 udelay(10);
3342 }
3343 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3344 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003345
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003346 return 0;
3347}
3348
3349/**
3350 * i40e_vsi_control_rx - Start or stop a VSI's rings
3351 * @vsi: the VSI being configured
3352 * @enable: start or stop the rings
3353 **/
3354static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3355{
3356 struct i40e_pf *pf = vsi->back;
3357 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003358 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003359 u32 rx_reg;
3360
3361 pf_q = vsi->base_queue;
3362 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003363 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003364 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003365 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3366 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3367 break;
3368 usleep_range(1000, 2000);
3369 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003370
Catherine Sullivan7c122002014-03-14 07:32:29 +00003371 /* Skip if the queue is already in the requested state */
3372 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3373 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003374
3375 /* turn on/off the queue */
3376 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003377 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003378 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003379 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003380 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3381
3382 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003383 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3384 if (ret) {
3385 dev_info(&pf->pdev->dev,
3386 "%s: VSI seid %d Rx ring %d %sable timeout\n",
3387 __func__, vsi->seid, pf_q,
3388 (enable ? "en" : "dis"));
3389 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003390 }
3391 }
3392
Neerav Parikh23527302014-06-03 23:50:15 +00003393 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003394}
3395
3396/**
3397 * i40e_vsi_control_rings - Start or stop a VSI's rings
3398 * @vsi: the VSI being configured
3399 * @enable: start or stop the rings
3400 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003401int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003402{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003403 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003404
3405 /* do rx first for enable and last for disable */
3406 if (request) {
3407 ret = i40e_vsi_control_rx(vsi, request);
3408 if (ret)
3409 return ret;
3410 ret = i40e_vsi_control_tx(vsi, request);
3411 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003412 /* Ignore return value, we need to shutdown whatever we can */
3413 i40e_vsi_control_tx(vsi, request);
3414 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003415 }
3416
3417 return ret;
3418}
3419
3420/**
3421 * i40e_vsi_free_irq - Free the irq association with the OS
3422 * @vsi: the VSI being configured
3423 **/
3424static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3425{
3426 struct i40e_pf *pf = vsi->back;
3427 struct i40e_hw *hw = &pf->hw;
3428 int base = vsi->base_vector;
3429 u32 val, qp;
3430 int i;
3431
3432 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3433 if (!vsi->q_vectors)
3434 return;
3435
Shannon Nelson63741842014-04-23 04:50:16 +00003436 if (!vsi->irqs_ready)
3437 return;
3438
3439 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003440 for (i = 0; i < vsi->num_q_vectors; i++) {
3441 u16 vector = i + base;
3442
3443 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003444 if (!vsi->q_vectors[i] ||
3445 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003446 continue;
3447
3448 /* clear the affinity_mask in the IRQ descriptor */
3449 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3450 NULL);
3451 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003452 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003453
3454 /* Tear down the interrupt queue link list
3455 *
3456 * We know that they come in pairs and always
3457 * the Rx first, then the Tx. To clear the
3458 * link list, stick the EOL value into the
3459 * next_q field of the registers.
3460 */
3461 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3462 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3463 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3464 val |= I40E_QUEUE_END_OF_LIST
3465 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3466 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3467
3468 while (qp != I40E_QUEUE_END_OF_LIST) {
3469 u32 next;
3470
3471 val = rd32(hw, I40E_QINT_RQCTL(qp));
3472
3473 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3474 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3475 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3476 I40E_QINT_RQCTL_INTEVENT_MASK);
3477
3478 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3479 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3480
3481 wr32(hw, I40E_QINT_RQCTL(qp), val);
3482
3483 val = rd32(hw, I40E_QINT_TQCTL(qp));
3484
3485 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3486 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3487
3488 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3489 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3490 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3491 I40E_QINT_TQCTL_INTEVENT_MASK);
3492
3493 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3494 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3495
3496 wr32(hw, I40E_QINT_TQCTL(qp), val);
3497 qp = next;
3498 }
3499 }
3500 } else {
3501 free_irq(pf->pdev->irq, pf);
3502
3503 val = rd32(hw, I40E_PFINT_LNKLST0);
3504 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3505 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3506 val |= I40E_QUEUE_END_OF_LIST
3507 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3508 wr32(hw, I40E_PFINT_LNKLST0, val);
3509
3510 val = rd32(hw, I40E_QINT_RQCTL(qp));
3511 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3512 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3513 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3514 I40E_QINT_RQCTL_INTEVENT_MASK);
3515
3516 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3517 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3518
3519 wr32(hw, I40E_QINT_RQCTL(qp), val);
3520
3521 val = rd32(hw, I40E_QINT_TQCTL(qp));
3522
3523 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3524 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3525 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3526 I40E_QINT_TQCTL_INTEVENT_MASK);
3527
3528 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3529 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3530
3531 wr32(hw, I40E_QINT_TQCTL(qp), val);
3532 }
3533}
3534
3535/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003536 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3537 * @vsi: the VSI being configured
3538 * @v_idx: Index of vector to be freed
3539 *
3540 * This function frees the memory allocated to the q_vector. In addition if
3541 * NAPI is enabled it will delete any references to the NAPI struct prior
3542 * to freeing the q_vector.
3543 **/
3544static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3545{
3546 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003547 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003548
3549 if (!q_vector)
3550 return;
3551
3552 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003553 i40e_for_each_ring(ring, q_vector->tx)
3554 ring->q_vector = NULL;
3555
3556 i40e_for_each_ring(ring, q_vector->rx)
3557 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003558
3559 /* only VSI w/ an associated netdev is set up w/ NAPI */
3560 if (vsi->netdev)
3561 netif_napi_del(&q_vector->napi);
3562
3563 vsi->q_vectors[v_idx] = NULL;
3564
3565 kfree_rcu(q_vector, rcu);
3566}
3567
3568/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003569 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3570 * @vsi: the VSI being un-configured
3571 *
3572 * This frees the memory allocated to the q_vectors and
3573 * deletes references to the NAPI struct.
3574 **/
3575static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3576{
3577 int v_idx;
3578
Alexander Duyck493fb302013-09-28 07:01:44 +00003579 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3580 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003581}
3582
3583/**
3584 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3585 * @pf: board private structure
3586 **/
3587static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3588{
3589 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3590 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3591 pci_disable_msix(pf->pdev);
3592 kfree(pf->msix_entries);
3593 pf->msix_entries = NULL;
3594 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3595 pci_disable_msi(pf->pdev);
3596 }
3597 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3598}
3599
3600/**
3601 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3602 * @pf: board private structure
3603 *
3604 * We go through and clear interrupt specific resources and reset the structure
3605 * to pre-load conditions
3606 **/
3607static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3608{
3609 int i;
3610
3611 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00003612 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003613 if (pf->vsi[i])
3614 i40e_vsi_free_q_vectors(pf->vsi[i]);
3615 i40e_reset_interrupt_capability(pf);
3616}
3617
3618/**
3619 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3620 * @vsi: the VSI being configured
3621 **/
3622static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3623{
3624 int q_idx;
3625
3626 if (!vsi->netdev)
3627 return;
3628
3629 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003630 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003631}
3632
3633/**
3634 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3635 * @vsi: the VSI being configured
3636 **/
3637static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3638{
3639 int q_idx;
3640
3641 if (!vsi->netdev)
3642 return;
3643
3644 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003645 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003646}
3647
3648/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003649 * i40e_vsi_close - Shut down a VSI
3650 * @vsi: the vsi to be quelled
3651 **/
3652static void i40e_vsi_close(struct i40e_vsi *vsi)
3653{
3654 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
3655 i40e_down(vsi);
3656 i40e_vsi_free_irq(vsi);
3657 i40e_vsi_free_tx_resources(vsi);
3658 i40e_vsi_free_rx_resources(vsi);
3659}
3660
3661/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003662 * i40e_quiesce_vsi - Pause a given VSI
3663 * @vsi: the VSI being paused
3664 **/
3665static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3666{
3667 if (test_bit(__I40E_DOWN, &vsi->state))
3668 return;
3669
3670 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3671 if (vsi->netdev && netif_running(vsi->netdev)) {
3672 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3673 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003674 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003675 }
3676}
3677
3678/**
3679 * i40e_unquiesce_vsi - Resume a given VSI
3680 * @vsi: the VSI being resumed
3681 **/
3682static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3683{
3684 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3685 return;
3686
3687 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3688 if (vsi->netdev && netif_running(vsi->netdev))
3689 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3690 else
Shannon Nelson8276f752014-03-14 07:32:27 +00003691 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003692}
3693
3694/**
3695 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3696 * @pf: the PF
3697 **/
3698static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3699{
3700 int v;
3701
Mitch Williams505682c2014-05-20 08:01:37 +00003702 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003703 if (pf->vsi[v])
3704 i40e_quiesce_vsi(pf->vsi[v]);
3705 }
3706}
3707
3708/**
3709 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3710 * @pf: the PF
3711 **/
3712static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3713{
3714 int v;
3715
Mitch Williams505682c2014-05-20 08:01:37 +00003716 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003717 if (pf->vsi[v])
3718 i40e_unquiesce_vsi(pf->vsi[v]);
3719 }
3720}
3721
3722/**
3723 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3724 * @dcbcfg: the corresponding DCBx configuration structure
3725 *
3726 * Return the number of TCs from given DCBx configuration
3727 **/
3728static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3729{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003730 u8 num_tc = 0;
3731 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003732
3733 /* Scan the ETS Config Priority Table to find
3734 * traffic class enabled for a given priority
3735 * and use the traffic class index to get the
3736 * number of traffic classes enabled
3737 */
3738 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3739 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3740 num_tc = dcbcfg->etscfg.prioritytable[i];
3741 }
3742
3743 /* Traffic class index starts from zero so
3744 * increment to return the actual count
3745 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003746 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003747}
3748
3749/**
3750 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3751 * @dcbcfg: the corresponding DCBx configuration structure
3752 *
3753 * Query the current DCB configuration and return the number of
3754 * traffic classes enabled from the given DCBX config
3755 **/
3756static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3757{
3758 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3759 u8 enabled_tc = 1;
3760 u8 i;
3761
3762 for (i = 0; i < num_tc; i++)
3763 enabled_tc |= 1 << i;
3764
3765 return enabled_tc;
3766}
3767
3768/**
3769 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3770 * @pf: PF being queried
3771 *
3772 * Return number of traffic classes enabled for the given PF
3773 **/
3774static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3775{
3776 struct i40e_hw *hw = &pf->hw;
3777 u8 i, enabled_tc;
3778 u8 num_tc = 0;
3779 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3780
3781 /* If DCB is not enabled then always in single TC */
3782 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3783 return 1;
3784
3785 /* MFP mode return count of enabled TCs for this PF */
3786 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3787 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3788 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3789 if (enabled_tc & (1 << i))
3790 num_tc++;
3791 }
3792 return num_tc;
3793 }
3794
3795 /* SFP mode will be enabled for all TCs on port */
3796 return i40e_dcb_get_num_tc(dcbcfg);
3797}
3798
3799/**
3800 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3801 * @pf: PF being queried
3802 *
3803 * Return a bitmap for first enabled traffic class for this PF.
3804 **/
3805static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3806{
3807 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3808 u8 i = 0;
3809
3810 if (!enabled_tc)
3811 return 0x1; /* TC0 */
3812
3813 /* Find the first enabled TC */
3814 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3815 if (enabled_tc & (1 << i))
3816 break;
3817 }
3818
3819 return 1 << i;
3820}
3821
3822/**
3823 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3824 * @pf: PF being queried
3825 *
3826 * Return a bitmap for enabled traffic classes for this PF.
3827 **/
3828static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3829{
3830 /* If DCB is not enabled for this PF then just return default TC */
3831 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3832 return i40e_pf_get_default_tc(pf);
3833
3834 /* MFP mode will have enabled TCs set by FW */
3835 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3836 return pf->hw.func_caps.enabled_tcmap;
3837
3838 /* SFP mode we want PF to be enabled for all TCs */
3839 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3840}
3841
3842/**
3843 * i40e_vsi_get_bw_info - Query VSI BW Information
3844 * @vsi: the VSI being queried
3845 *
3846 * Returns 0 on success, negative value on failure
3847 **/
3848static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3849{
3850 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3851 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3852 struct i40e_pf *pf = vsi->back;
3853 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003854 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003855 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003856 int i;
3857
3858 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003859 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3860 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003861 dev_info(&pf->pdev->dev,
3862 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003863 aq_ret, pf->hw.aq.asq_last_status);
3864 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003865 }
3866
3867 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003868 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
Jesse Brandeburg6838b532014-01-14 00:49:52 -08003869 NULL);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003870 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003871 dev_info(&pf->pdev->dev,
3872 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003873 aq_ret, pf->hw.aq.asq_last_status);
3874 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003875 }
3876
3877 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3878 dev_info(&pf->pdev->dev,
3879 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3880 bw_config.tc_valid_bits,
3881 bw_ets_config.tc_valid_bits);
3882 /* Still continuing */
3883 }
3884
3885 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3886 vsi->bw_max_quanta = bw_config.max_bw;
3887 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3888 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3889 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3890 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3891 vsi->bw_ets_limit_credits[i] =
3892 le16_to_cpu(bw_ets_config.credits[i]);
3893 /* 3 bits out of 4 for each TC */
3894 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3895 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003896
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003897 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003898}
3899
3900/**
3901 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3902 * @vsi: the VSI being configured
3903 * @enabled_tc: TC bitmap
3904 * @bw_credits: BW shared credits per TC
3905 *
3906 * Returns 0 on success, negative value on failure
3907 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003908static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003909 u8 *bw_share)
3910{
3911 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003912 i40e_status aq_ret;
3913 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003914
3915 bw_data.tc_valid_bits = enabled_tc;
3916 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3917 bw_data.tc_bw_credits[i] = bw_share[i];
3918
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003919 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3920 NULL);
3921 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003922 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00003923 "AQ command Config VSI BW allocation per TC failed = %d\n",
3924 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003925 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003926 }
3927
3928 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3929 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3930
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003931 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003932}
3933
3934/**
3935 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3936 * @vsi: the VSI being configured
3937 * @enabled_tc: TC map to be enabled
3938 *
3939 **/
3940static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3941{
3942 struct net_device *netdev = vsi->netdev;
3943 struct i40e_pf *pf = vsi->back;
3944 struct i40e_hw *hw = &pf->hw;
3945 u8 netdev_tc = 0;
3946 int i;
3947 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3948
3949 if (!netdev)
3950 return;
3951
3952 if (!enabled_tc) {
3953 netdev_reset_tc(netdev);
3954 return;
3955 }
3956
3957 /* Set up actual enabled TCs on the VSI */
3958 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3959 return;
3960
3961 /* set per TC queues for the VSI */
3962 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3963 /* Only set TC queues for enabled tcs
3964 *
3965 * e.g. For a VSI that has TC0 and TC3 enabled the
3966 * enabled_tc bitmap would be 0x00001001; the driver
3967 * will set the numtc for netdev as 2 that will be
3968 * referenced by the netdev layer as TC 0 and 1.
3969 */
3970 if (vsi->tc_config.enabled_tc & (1 << i))
3971 netdev_set_tc_queue(netdev,
3972 vsi->tc_config.tc_info[i].netdev_tc,
3973 vsi->tc_config.tc_info[i].qcount,
3974 vsi->tc_config.tc_info[i].qoffset);
3975 }
3976
3977 /* Assign UP2TC map for the VSI */
3978 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3979 /* Get the actual TC# for the UP */
3980 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3981 /* Get the mapped netdev TC# for the UP */
3982 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3983 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3984 }
3985}
3986
3987/**
3988 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3989 * @vsi: the VSI being configured
3990 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3991 **/
3992static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3993 struct i40e_vsi_context *ctxt)
3994{
3995 /* copy just the sections touched not the entire info
3996 * since not all sections are valid as returned by
3997 * update vsi params
3998 */
3999 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4000 memcpy(&vsi->info.queue_mapping,
4001 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4002 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4003 sizeof(vsi->info.tc_mapping));
4004}
4005
4006/**
4007 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4008 * @vsi: VSI to be configured
4009 * @enabled_tc: TC bitmap
4010 *
4011 * This configures a particular VSI for TCs that are mapped to the
4012 * given TC bitmap. It uses default bandwidth share for TCs across
4013 * VSIs to configure TC for a particular VSI.
4014 *
4015 * NOTE:
4016 * It is expected that the VSI queues have been quisced before calling
4017 * this function.
4018 **/
4019static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4020{
4021 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4022 struct i40e_vsi_context ctxt;
4023 int ret = 0;
4024 int i;
4025
4026 /* Check if enabled_tc is same as existing or new TCs */
4027 if (vsi->tc_config.enabled_tc == enabled_tc)
4028 return ret;
4029
4030 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4031 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4032 if (enabled_tc & (1 << i))
4033 bw_share[i] = 1;
4034 }
4035
4036 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4037 if (ret) {
4038 dev_info(&vsi->back->pdev->dev,
4039 "Failed configuring TC map %d for VSI %d\n",
4040 enabled_tc, vsi->seid);
4041 goto out;
4042 }
4043
4044 /* Update Queue Pairs Mapping for currently enabled UPs */
4045 ctxt.seid = vsi->seid;
4046 ctxt.pf_num = vsi->back->hw.pf_id;
4047 ctxt.vf_num = 0;
4048 ctxt.uplink_seid = vsi->uplink_seid;
4049 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
4050 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4051
4052 /* Update the VSI after updating the VSI queue-mapping information */
4053 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4054 if (ret) {
4055 dev_info(&vsi->back->pdev->dev,
4056 "update vsi failed, aq_err=%d\n",
4057 vsi->back->hw.aq.asq_last_status);
4058 goto out;
4059 }
4060 /* update the local VSI info with updated queue map */
4061 i40e_vsi_update_queue_map(vsi, &ctxt);
4062 vsi->info.valid_sections = 0;
4063
4064 /* Update current VSI BW information */
4065 ret = i40e_vsi_get_bw_info(vsi);
4066 if (ret) {
4067 dev_info(&vsi->back->pdev->dev,
4068 "Failed updating vsi bw info, aq_err=%d\n",
4069 vsi->back->hw.aq.asq_last_status);
4070 goto out;
4071 }
4072
4073 /* Update the netdev TC setup */
4074 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4075out:
4076 return ret;
4077}
4078
4079/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004080 * i40e_veb_config_tc - Configure TCs for given VEB
4081 * @veb: given VEB
4082 * @enabled_tc: TC bitmap
4083 *
4084 * Configures given TC bitmap for VEB (switching) element
4085 **/
4086int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4087{
4088 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4089 struct i40e_pf *pf = veb->pf;
4090 int ret = 0;
4091 int i;
4092
4093 /* No TCs or already enabled TCs just return */
4094 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4095 return ret;
4096
4097 bw_data.tc_valid_bits = enabled_tc;
4098 /* bw_data.absolute_credits is not set (relative) */
4099
4100 /* Enable ETS TCs with equal BW Share for now */
4101 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4102 if (enabled_tc & (1 << i))
4103 bw_data.tc_bw_share_credits[i] = 1;
4104 }
4105
4106 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4107 &bw_data, NULL);
4108 if (ret) {
4109 dev_info(&pf->pdev->dev,
4110 "veb bw config failed, aq_err=%d\n",
4111 pf->hw.aq.asq_last_status);
4112 goto out;
4113 }
4114
4115 /* Update the BW information */
4116 ret = i40e_veb_get_bw_info(veb);
4117 if (ret) {
4118 dev_info(&pf->pdev->dev,
4119 "Failed getting veb bw config, aq_err=%d\n",
4120 pf->hw.aq.asq_last_status);
4121 }
4122
4123out:
4124 return ret;
4125}
4126
4127#ifdef CONFIG_I40E_DCB
4128/**
4129 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4130 * @pf: PF struct
4131 *
4132 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4133 * the caller would've quiesce all the VSIs before calling
4134 * this function
4135 **/
4136static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4137{
4138 u8 tc_map = 0;
4139 int ret;
4140 u8 v;
4141
4142 /* Enable the TCs available on PF to all VEBs */
4143 tc_map = i40e_pf_get_tc_map(pf);
4144 for (v = 0; v < I40E_MAX_VEB; v++) {
4145 if (!pf->veb[v])
4146 continue;
4147 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4148 if (ret) {
4149 dev_info(&pf->pdev->dev,
4150 "Failed configuring TC for VEB seid=%d\n",
4151 pf->veb[v]->seid);
4152 /* Will try to configure as many components */
4153 }
4154 }
4155
4156 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004157 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004158 if (!pf->vsi[v])
4159 continue;
4160
4161 /* - Enable all TCs for the LAN VSI
4162 * - For all others keep them at TC0 for now
4163 */
4164 if (v == pf->lan_vsi)
4165 tc_map = i40e_pf_get_tc_map(pf);
4166 else
4167 tc_map = i40e_pf_get_default_tc(pf);
4168
4169 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4170 if (ret) {
4171 dev_info(&pf->pdev->dev,
4172 "Failed configuring TC for VSI seid=%d\n",
4173 pf->vsi[v]->seid);
4174 /* Will try to configure as many components */
4175 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004176 /* Re-configure VSI vectors based on updated TC map */
4177 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004178 if (pf->vsi[v]->netdev)
4179 i40e_dcbnl_set_all(pf->vsi[v]);
4180 }
4181 }
4182}
4183
4184/**
4185 * i40e_init_pf_dcb - Initialize DCB configuration
4186 * @pf: PF being configured
4187 *
4188 * Query the current DCB configuration and cache it
4189 * in the hardware structure
4190 **/
4191static int i40e_init_pf_dcb(struct i40e_pf *pf)
4192{
4193 struct i40e_hw *hw = &pf->hw;
4194 int err = 0;
4195
4196 if (pf->hw.func_caps.npar_enable)
4197 goto out;
4198
4199 /* Get the initial DCB configuration */
4200 err = i40e_init_dcb(hw);
4201 if (!err) {
4202 /* Device/Function is not DCBX capable */
4203 if ((!hw->func_caps.dcb) ||
4204 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4205 dev_info(&pf->pdev->dev,
4206 "DCBX offload is not supported or is disabled for this PF.\n");
4207
4208 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4209 goto out;
4210
4211 } else {
4212 /* When status is not DISABLED then DCBX in FW */
4213 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4214 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004215
4216 pf->flags |= I40E_FLAG_DCB_CAPABLE;
4217 /* Enable DCB tagging only when more than one TC */
4218 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
4219 pf->flags |= I40E_FLAG_DCB_ENABLED;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004220 }
Neerav Parikh014269f2014-04-01 07:11:48 +00004221 } else {
4222 dev_info(&pf->pdev->dev, "AQ Querying DCB configuration failed: %d\n",
4223 pf->hw.aq.asq_last_status);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004224 }
4225
4226out:
4227 return err;
4228}
4229#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004230#define SPEED_SIZE 14
4231#define FC_SIZE 8
4232/**
4233 * i40e_print_link_message - print link up or down
4234 * @vsi: the VSI for which link needs a message
4235 */
4236static void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
4237{
4238 char speed[SPEED_SIZE] = "Unknown";
4239 char fc[FC_SIZE] = "RX/TX";
4240
4241 if (!isup) {
4242 netdev_info(vsi->netdev, "NIC Link is Down\n");
4243 return;
4244 }
4245
4246 switch (vsi->back->hw.phy.link_info.link_speed) {
4247 case I40E_LINK_SPEED_40GB:
4248 strncpy(speed, "40 Gbps", SPEED_SIZE);
4249 break;
4250 case I40E_LINK_SPEED_10GB:
4251 strncpy(speed, "10 Gbps", SPEED_SIZE);
4252 break;
4253 case I40E_LINK_SPEED_1GB:
4254 strncpy(speed, "1000 Mbps", SPEED_SIZE);
4255 break;
4256 default:
4257 break;
4258 }
4259
4260 switch (vsi->back->hw.fc.current_mode) {
4261 case I40E_FC_FULL:
4262 strncpy(fc, "RX/TX", FC_SIZE);
4263 break;
4264 case I40E_FC_TX_PAUSE:
4265 strncpy(fc, "TX", FC_SIZE);
4266 break;
4267 case I40E_FC_RX_PAUSE:
4268 strncpy(fc, "RX", FC_SIZE);
4269 break;
4270 default:
4271 strncpy(fc, "None", FC_SIZE);
4272 break;
4273 }
4274
4275 netdev_info(vsi->netdev, "NIC Link is Up %s Full Duplex, Flow Control: %s\n",
4276 speed, fc);
4277}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004278
4279/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004280 * i40e_up_complete - Finish the last steps of bringing up a connection
4281 * @vsi: the VSI being configured
4282 **/
4283static int i40e_up_complete(struct i40e_vsi *vsi)
4284{
4285 struct i40e_pf *pf = vsi->back;
4286 int err;
4287
4288 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4289 i40e_vsi_configure_msix(vsi);
4290 else
4291 i40e_configure_msi_and_legacy(vsi);
4292
4293 /* start rings */
4294 err = i40e_vsi_control_rings(vsi, true);
4295 if (err)
4296 return err;
4297
4298 clear_bit(__I40E_DOWN, &vsi->state);
4299 i40e_napi_enable_all(vsi);
4300 i40e_vsi_enable_irq(vsi);
4301
4302 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4303 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004304 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004305 netif_tx_start_all_queues(vsi->netdev);
4306 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004307 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004308 i40e_print_link_message(vsi, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004309 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004310
4311 /* replay FDIR SB filters */
4312 if (vsi->type == I40E_VSI_FDIR)
4313 i40e_fdir_filter_restore(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004314 i40e_service_event_schedule(pf);
4315
4316 return 0;
4317}
4318
4319/**
4320 * i40e_vsi_reinit_locked - Reset the VSI
4321 * @vsi: the VSI being configured
4322 *
4323 * Rebuild the ring structs after some configuration
4324 * has changed, e.g. MTU size.
4325 **/
4326static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4327{
4328 struct i40e_pf *pf = vsi->back;
4329
4330 WARN_ON(in_interrupt());
4331 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4332 usleep_range(1000, 2000);
4333 i40e_down(vsi);
4334
4335 /* Give a VF some time to respond to the reset. The
4336 * two second wait is based upon the watchdog cycle in
4337 * the VF driver.
4338 */
4339 if (vsi->type == I40E_VSI_SRIOV)
4340 msleep(2000);
4341 i40e_up(vsi);
4342 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
4343}
4344
4345/**
4346 * i40e_up - Bring the connection back up after being down
4347 * @vsi: the VSI being configured
4348 **/
4349int i40e_up(struct i40e_vsi *vsi)
4350{
4351 int err;
4352
4353 err = i40e_vsi_configure(vsi);
4354 if (!err)
4355 err = i40e_up_complete(vsi);
4356
4357 return err;
4358}
4359
4360/**
4361 * i40e_down - Shutdown the connection processing
4362 * @vsi: the VSI being stopped
4363 **/
4364void i40e_down(struct i40e_vsi *vsi)
4365{
4366 int i;
4367
4368 /* It is assumed that the caller of this function
4369 * sets the vsi->state __I40E_DOWN bit.
4370 */
4371 if (vsi->netdev) {
4372 netif_carrier_off(vsi->netdev);
4373 netif_tx_disable(vsi->netdev);
4374 }
4375 i40e_vsi_disable_irq(vsi);
4376 i40e_vsi_control_rings(vsi, false);
4377 i40e_napi_disable_all(vsi);
4378
4379 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004380 i40e_clean_tx_ring(vsi->tx_rings[i]);
4381 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004382 }
4383}
4384
4385/**
4386 * i40e_setup_tc - configure multiple traffic classes
4387 * @netdev: net device to configure
4388 * @tc: number of traffic classes to enable
4389 **/
4390static int i40e_setup_tc(struct net_device *netdev, u8 tc)
4391{
4392 struct i40e_netdev_priv *np = netdev_priv(netdev);
4393 struct i40e_vsi *vsi = np->vsi;
4394 struct i40e_pf *pf = vsi->back;
4395 u8 enabled_tc = 0;
4396 int ret = -EINVAL;
4397 int i;
4398
4399 /* Check if DCB enabled to continue */
4400 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
4401 netdev_info(netdev, "DCB is not enabled for adapter\n");
4402 goto exit;
4403 }
4404
4405 /* Check if MFP enabled */
4406 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4407 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
4408 goto exit;
4409 }
4410
4411 /* Check whether tc count is within enabled limit */
4412 if (tc > i40e_pf_get_num_tc(pf)) {
4413 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
4414 goto exit;
4415 }
4416
4417 /* Generate TC map for number of tc requested */
4418 for (i = 0; i < tc; i++)
4419 enabled_tc |= (1 << i);
4420
4421 /* Requesting same TC configuration as already enabled */
4422 if (enabled_tc == vsi->tc_config.enabled_tc)
4423 return 0;
4424
4425 /* Quiesce VSI queues */
4426 i40e_quiesce_vsi(vsi);
4427
4428 /* Configure VSI for enabled TCs */
4429 ret = i40e_vsi_config_tc(vsi, enabled_tc);
4430 if (ret) {
4431 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
4432 vsi->seid);
4433 goto exit;
4434 }
4435
4436 /* Unquiesce VSI */
4437 i40e_unquiesce_vsi(vsi);
4438
4439exit:
4440 return ret;
4441}
4442
4443/**
4444 * i40e_open - Called when a network interface is made active
4445 * @netdev: network interface device structure
4446 *
4447 * The open entry point is called when a network interface is made
4448 * active by the system (IFF_UP). At this point all resources needed
4449 * for transmit and receive operations are allocated, the interrupt
4450 * handler is registered with the OS, the netdev watchdog subtask is
4451 * enabled, and the stack is notified that the interface is ready.
4452 *
4453 * Returns 0 on success, negative value on failure
4454 **/
4455static int i40e_open(struct net_device *netdev)
4456{
4457 struct i40e_netdev_priv *np = netdev_priv(netdev);
4458 struct i40e_vsi *vsi = np->vsi;
4459 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004460 int err;
4461
Shannon Nelson4eb3f762014-03-06 08:59:58 +00004462 /* disallow open during test or if eeprom is broken */
4463 if (test_bit(__I40E_TESTING, &pf->state) ||
4464 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004465 return -EBUSY;
4466
4467 netif_carrier_off(netdev);
4468
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004469 err = i40e_vsi_open(vsi);
4470 if (err)
4471 return err;
4472
Jesse Brandeburg059dab62014-04-01 09:07:20 +00004473 /* configure global TSO hardware offload settings */
4474 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
4475 TCP_FLAG_FIN) >> 16);
4476 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
4477 TCP_FLAG_FIN |
4478 TCP_FLAG_CWR) >> 16);
4479 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
4480
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004481#ifdef CONFIG_I40E_VXLAN
4482 vxlan_get_rx_port(netdev);
4483#endif
4484
4485 return 0;
4486}
4487
4488/**
4489 * i40e_vsi_open -
4490 * @vsi: the VSI to open
4491 *
4492 * Finish initialization of the VSI.
4493 *
4494 * Returns 0 on success, negative value on failure
4495 **/
4496int i40e_vsi_open(struct i40e_vsi *vsi)
4497{
4498 struct i40e_pf *pf = vsi->back;
4499 char int_name[IFNAMSIZ];
4500 int err;
4501
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004502 /* allocate descriptors */
4503 err = i40e_vsi_setup_tx_resources(vsi);
4504 if (err)
4505 goto err_setup_tx;
4506 err = i40e_vsi_setup_rx_resources(vsi);
4507 if (err)
4508 goto err_setup_rx;
4509
4510 err = i40e_vsi_configure(vsi);
4511 if (err)
4512 goto err_setup_rx;
4513
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004514 if (vsi->netdev) {
4515 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4516 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
4517 err = i40e_vsi_request_irq(vsi, int_name);
4518 if (err)
4519 goto err_setup_rx;
4520
4521 /* Notify the stack of the actual queue counts. */
4522 err = netif_set_real_num_tx_queues(vsi->netdev,
4523 vsi->num_queue_pairs);
4524 if (err)
4525 goto err_set_queues;
4526
4527 err = netif_set_real_num_rx_queues(vsi->netdev,
4528 vsi->num_queue_pairs);
4529 if (err)
4530 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00004531
4532 } else if (vsi->type == I40E_VSI_FDIR) {
4533 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4534 dev_driver_string(&pf->pdev->dev));
4535 err = i40e_vsi_request_irq(vsi, int_name);
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004536 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00004537 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004538 goto err_setup_rx;
4539 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004540
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004541 err = i40e_up_complete(vsi);
4542 if (err)
4543 goto err_up_complete;
4544
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004545 return 0;
4546
4547err_up_complete:
4548 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004549err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004550 i40e_vsi_free_irq(vsi);
4551err_setup_rx:
4552 i40e_vsi_free_rx_resources(vsi);
4553err_setup_tx:
4554 i40e_vsi_free_tx_resources(vsi);
4555 if (vsi == pf->vsi[pf->lan_vsi])
4556 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4557
4558 return err;
4559}
4560
4561/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00004562 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
4563 * @pf: Pointer to pf
4564 *
4565 * This function destroys the hlist where all the Flow Director
4566 * filters were saved.
4567 **/
4568static void i40e_fdir_filter_exit(struct i40e_pf *pf)
4569{
4570 struct i40e_fdir_filter *filter;
4571 struct hlist_node *node2;
4572
4573 hlist_for_each_entry_safe(filter, node2,
4574 &pf->fdir_filter_list, fdir_node) {
4575 hlist_del(&filter->fdir_node);
4576 kfree(filter);
4577 }
4578 pf->fdir_pf_active_filters = 0;
4579}
4580
4581/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004582 * i40e_close - Disables a network interface
4583 * @netdev: network interface device structure
4584 *
4585 * The close entry point is called when an interface is de-activated
4586 * by the OS. The hardware is still under the driver's control, but
4587 * this netdev interface is disabled.
4588 *
4589 * Returns 0, this is not allowed to fail
4590 **/
4591static int i40e_close(struct net_device *netdev)
4592{
4593 struct i40e_netdev_priv *np = netdev_priv(netdev);
4594 struct i40e_vsi *vsi = np->vsi;
4595
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004596 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004597
4598 return 0;
4599}
4600
4601/**
4602 * i40e_do_reset - Start a PF or Core Reset sequence
4603 * @pf: board private structure
4604 * @reset_flags: which reset is requested
4605 *
4606 * The essential difference in resets is that the PF Reset
4607 * doesn't clear the packet buffers, doesn't reset the PE
4608 * firmware, and doesn't bother the other PFs on the chip.
4609 **/
4610void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4611{
4612 u32 val;
4613
4614 WARN_ON(in_interrupt());
4615
Mitch Williams263fc482014-04-23 04:50:11 +00004616 if (i40e_check_asq_alive(&pf->hw))
4617 i40e_vc_notify_reset(pf);
4618
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004619 /* do the biggest reset indicated */
4620 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4621
4622 /* Request a Global Reset
4623 *
4624 * This will start the chip's countdown to the actual full
4625 * chip reset event, and a warning interrupt to be sent
4626 * to all PFs, including the requestor. Our handler
4627 * for the warning interrupt will deal with the shutdown
4628 * and recovery of the switch setup.
4629 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004630 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004631 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4632 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4633 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4634
4635 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4636
4637 /* Request a Core Reset
4638 *
4639 * Same as Global Reset, except does *not* include the MAC/PHY
4640 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004641 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004642 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4643 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4644 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4645 i40e_flush(&pf->hw);
4646
Shannon Nelson7823fe32013-11-16 10:00:45 +00004647 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4648
4649 /* Request a Firmware Reset
4650 *
4651 * Same as Global reset, plus restarting the
4652 * embedded firmware engine.
4653 */
4654 /* enable EMP Reset */
4655 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4656 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4657 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4658
4659 /* force the reset */
4660 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4661 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4662 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4663 i40e_flush(&pf->hw);
4664
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004665 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4666
4667 /* Request a PF Reset
4668 *
4669 * Resets only the PF-specific registers
4670 *
4671 * This goes directly to the tear-down and rebuild of
4672 * the switch, since we need to do all the recovery as
4673 * for the Core Reset.
4674 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004675 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004676 i40e_handle_reset_warning(pf);
4677
4678 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4679 int v;
4680
4681 /* Find the VSI(s) that requested a re-init */
4682 dev_info(&pf->pdev->dev,
4683 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00004684 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004685 struct i40e_vsi *vsi = pf->vsi[v];
4686 if (vsi != NULL &&
4687 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4688 i40e_vsi_reinit_locked(pf->vsi[v]);
4689 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4690 }
4691 }
4692
4693 /* no further action needed, so return now */
4694 return;
4695 } else {
4696 dev_info(&pf->pdev->dev,
4697 "bad reset request 0x%08x\n", reset_flags);
4698 return;
4699 }
4700}
4701
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004702#ifdef CONFIG_I40E_DCB
4703/**
4704 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
4705 * @pf: board private structure
4706 * @old_cfg: current DCB config
4707 * @new_cfg: new DCB config
4708 **/
4709bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
4710 struct i40e_dcbx_config *old_cfg,
4711 struct i40e_dcbx_config *new_cfg)
4712{
4713 bool need_reconfig = false;
4714
4715 /* Check if ETS configuration has changed */
4716 if (memcmp(&new_cfg->etscfg,
4717 &old_cfg->etscfg,
4718 sizeof(new_cfg->etscfg))) {
4719 /* If Priority Table has changed reconfig is needed */
4720 if (memcmp(&new_cfg->etscfg.prioritytable,
4721 &old_cfg->etscfg.prioritytable,
4722 sizeof(new_cfg->etscfg.prioritytable))) {
4723 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004724 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004725 }
4726
4727 if (memcmp(&new_cfg->etscfg.tcbwtable,
4728 &old_cfg->etscfg.tcbwtable,
4729 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004730 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004731
4732 if (memcmp(&new_cfg->etscfg.tsatable,
4733 &old_cfg->etscfg.tsatable,
4734 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004735 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004736 }
4737
4738 /* Check if PFC configuration has changed */
4739 if (memcmp(&new_cfg->pfc,
4740 &old_cfg->pfc,
4741 sizeof(new_cfg->pfc))) {
4742 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004743 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004744 }
4745
4746 /* Check if APP Table has changed */
4747 if (memcmp(&new_cfg->app,
4748 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05004749 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004750 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004751 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05004752 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004753
4754 return need_reconfig;
4755}
4756
4757/**
4758 * i40e_handle_lldp_event - Handle LLDP Change MIB event
4759 * @pf: board private structure
4760 * @e: event info posted on ARQ
4761 **/
4762static int i40e_handle_lldp_event(struct i40e_pf *pf,
4763 struct i40e_arq_event_info *e)
4764{
4765 struct i40e_aqc_lldp_get_mib *mib =
4766 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
4767 struct i40e_hw *hw = &pf->hw;
4768 struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
4769 struct i40e_dcbx_config tmp_dcbx_cfg;
4770 bool need_reconfig = false;
4771 int ret = 0;
4772 u8 type;
4773
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004774 /* Not DCB capable or capability disabled */
4775 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
4776 return ret;
4777
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004778 /* Ignore if event is not for Nearest Bridge */
4779 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
4780 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
4781 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
4782 return ret;
4783
4784 /* Check MIB Type and return if event for Remote MIB update */
4785 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
4786 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
4787 /* Update the remote cached instance and return */
4788 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
4789 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
4790 &hw->remote_dcbx_config);
4791 goto exit;
4792 }
4793
4794 /* Convert/store the DCBX data from LLDPDU temporarily */
4795 memset(&tmp_dcbx_cfg, 0, sizeof(tmp_dcbx_cfg));
4796 ret = i40e_lldp_to_dcb_config(e->msg_buf, &tmp_dcbx_cfg);
4797 if (ret) {
4798 /* Error in LLDPDU parsing return */
4799 dev_info(&pf->pdev->dev, "Failed parsing LLDPDU from event buffer\n");
4800 goto exit;
4801 }
4802
4803 /* No change detected in DCBX configs */
4804 if (!memcmp(&tmp_dcbx_cfg, dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004805 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004806 goto exit;
4807 }
4808
4809 need_reconfig = i40e_dcb_need_reconfig(pf, dcbx_cfg, &tmp_dcbx_cfg);
4810
4811 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg);
4812
4813 /* Overwrite the new configuration */
4814 *dcbx_cfg = tmp_dcbx_cfg;
4815
4816 if (!need_reconfig)
4817 goto exit;
4818
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004819 /* Enable DCB tagging only when more than one TC */
4820 if (i40e_dcb_get_num_tc(dcbx_cfg) > 1)
4821 pf->flags |= I40E_FLAG_DCB_ENABLED;
4822 else
4823 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
4824
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004825 /* Reconfiguration needed quiesce all VSIs */
4826 i40e_pf_quiesce_all_vsi(pf);
4827
4828 /* Changes in configuration update VEB/VSI */
4829 i40e_dcb_reconfigure(pf);
4830
4831 i40e_pf_unquiesce_all_vsi(pf);
4832exit:
4833 return ret;
4834}
4835#endif /* CONFIG_I40E_DCB */
4836
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004837/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004838 * i40e_do_reset_safe - Protected reset path for userland calls.
4839 * @pf: board private structure
4840 * @reset_flags: which reset is requested
4841 *
4842 **/
4843void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4844{
4845 rtnl_lock();
4846 i40e_do_reset(pf, reset_flags);
4847 rtnl_unlock();
4848}
4849
4850/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004851 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4852 * @pf: board private structure
4853 * @e: event info posted on ARQ
4854 *
4855 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4856 * and VF queues
4857 **/
4858static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4859 struct i40e_arq_event_info *e)
4860{
4861 struct i40e_aqc_lan_overflow *data =
4862 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4863 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4864 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4865 struct i40e_hw *hw = &pf->hw;
4866 struct i40e_vf *vf;
4867 u16 vf_id;
4868
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004869 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
4870 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004871
4872 /* Queue belongs to VF, find the VF and issue VF reset */
4873 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4874 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4875 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4876 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4877 vf_id -= hw->func_caps.vf_base_id;
4878 vf = &pf->vf[vf_id];
4879 i40e_vc_notify_vf_reset(vf);
4880 /* Allow VF to process pending reset notification */
4881 msleep(20);
4882 i40e_reset_vf(vf, false);
4883 }
4884}
4885
4886/**
4887 * i40e_service_event_complete - Finish up the service event
4888 * @pf: board private structure
4889 **/
4890static void i40e_service_event_complete(struct i40e_pf *pf)
4891{
4892 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4893
4894 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01004895 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004896 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4897}
4898
4899/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004900 * i40e_get_current_fd_count - Get the count of FD filters programmed in the HW
4901 * @pf: board private structure
4902 **/
4903int i40e_get_current_fd_count(struct i40e_pf *pf)
4904{
4905 int val, fcnt_prog;
4906 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
4907 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
4908 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
4909 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
4910 return fcnt_prog;
4911}
4912
4913/**
4914 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
4915 * @pf: board private structure
4916 **/
4917void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
4918{
4919 u32 fcnt_prog, fcnt_avail;
4920
4921 /* Check if, FD SB or ATR was auto disabled and if there is enough room
4922 * to re-enable
4923 */
4924 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4925 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4926 return;
4927 fcnt_prog = i40e_get_current_fd_count(pf);
Anjali Singhai Jain89132782014-04-09 05:59:01 +00004928 fcnt_avail = i40e_get_fd_cnt_all(pf);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004929 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) {
4930 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
4931 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
4932 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
4933 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
4934 }
4935 }
4936 /* Wait for some more space to be available to turn on ATR */
4937 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
4938 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4939 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
4940 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
4941 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
4942 }
4943 }
4944}
4945
4946/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004947 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4948 * @pf: board private structure
4949 **/
4950static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4951{
4952 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4953 return;
4954
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004955 /* if interface is down do nothing */
4956 if (test_bit(__I40E_DOWN, &pf->state))
4957 return;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004958 i40e_fdir_check_and_reenable(pf);
4959
4960 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4961 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4962 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004963}
4964
4965/**
4966 * i40e_vsi_link_event - notify VSI of a link event
4967 * @vsi: vsi to be notified
4968 * @link_up: link up or down
4969 **/
4970static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4971{
4972 if (!vsi)
4973 return;
4974
4975 switch (vsi->type) {
4976 case I40E_VSI_MAIN:
4977 if (!vsi->netdev || !vsi->netdev_registered)
4978 break;
4979
4980 if (link_up) {
4981 netif_carrier_on(vsi->netdev);
4982 netif_tx_wake_all_queues(vsi->netdev);
4983 } else {
4984 netif_carrier_off(vsi->netdev);
4985 netif_tx_stop_all_queues(vsi->netdev);
4986 }
4987 break;
4988
4989 case I40E_VSI_SRIOV:
4990 break;
4991
4992 case I40E_VSI_VMDQ2:
4993 case I40E_VSI_CTRL:
4994 case I40E_VSI_MIRROR:
4995 default:
4996 /* there is no notification for other VSIs */
4997 break;
4998 }
4999}
5000
5001/**
5002 * i40e_veb_link_event - notify elements on the veb of a link event
5003 * @veb: veb to be notified
5004 * @link_up: link up or down
5005 **/
5006static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
5007{
5008 struct i40e_pf *pf;
5009 int i;
5010
5011 if (!veb || !veb->pf)
5012 return;
5013 pf = veb->pf;
5014
5015 /* depth first... */
5016 for (i = 0; i < I40E_MAX_VEB; i++)
5017 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
5018 i40e_veb_link_event(pf->veb[i], link_up);
5019
5020 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00005021 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005022 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
5023 i40e_vsi_link_event(pf->vsi[i], link_up);
5024}
5025
5026/**
5027 * i40e_link_event - Update netif_carrier status
5028 * @pf: board private structure
5029 **/
5030static void i40e_link_event(struct i40e_pf *pf)
5031{
5032 bool new_link, old_link;
5033
5034 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
5035 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
5036
5037 if (new_link == old_link)
5038 return;
Anjali Singhai6d779b42013-09-28 06:00:02 +00005039 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005040 i40e_print_link_message(pf->vsi[pf->lan_vsi], new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005041
5042 /* Notify the base of the switch tree connected to
5043 * the link. Floating VEBs are not notified.
5044 */
5045 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
5046 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
5047 else
5048 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
5049
5050 if (pf->vf)
5051 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005052
5053 if (pf->flags & I40E_FLAG_PTP)
5054 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005055}
5056
5057/**
5058 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
5059 * @pf: board private structure
5060 *
5061 * Set the per-queue flags to request a check for stuck queues in the irq
5062 * clean functions, then force interrupts to be sure the irq clean is called.
5063 **/
5064static void i40e_check_hang_subtask(struct i40e_pf *pf)
5065{
5066 int i, v;
5067
5068 /* If we're down or resetting, just bail */
5069 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
5070 return;
5071
5072 /* for each VSI/netdev
5073 * for each Tx queue
5074 * set the check flag
5075 * for each q_vector
5076 * force an interrupt
5077 */
Mitch Williams505682c2014-05-20 08:01:37 +00005078 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005079 struct i40e_vsi *vsi = pf->vsi[v];
5080 int armed = 0;
5081
5082 if (!pf->vsi[v] ||
5083 test_bit(__I40E_DOWN, &vsi->state) ||
5084 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
5085 continue;
5086
5087 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005088 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005089 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005090 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005091 armed++;
5092 }
5093
5094 if (armed) {
5095 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
5096 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
5097 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
5098 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
5099 } else {
5100 u16 vec = vsi->base_vector - 1;
5101 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
5102 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
5103 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
5104 wr32(&vsi->back->hw,
5105 I40E_PFINT_DYN_CTLN(vec), val);
5106 }
5107 i40e_flush(&vsi->back->hw);
5108 }
5109 }
5110}
5111
5112/**
5113 * i40e_watchdog_subtask - Check and bring link up
5114 * @pf: board private structure
5115 **/
5116static void i40e_watchdog_subtask(struct i40e_pf *pf)
5117{
5118 int i;
5119
5120 /* if interface is down do nothing */
5121 if (test_bit(__I40E_DOWN, &pf->state) ||
5122 test_bit(__I40E_CONFIG_BUSY, &pf->state))
5123 return;
5124
5125 /* Update the stats for active netdevs so the network stack
5126 * can look at updated numbers whenever it cares to
5127 */
Mitch Williams505682c2014-05-20 08:01:37 +00005128 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005129 if (pf->vsi[i] && pf->vsi[i]->netdev)
5130 i40e_update_stats(pf->vsi[i]);
5131
5132 /* Update the stats for the active switching components */
5133 for (i = 0; i < I40E_MAX_VEB; i++)
5134 if (pf->veb[i])
5135 i40e_update_veb_stats(pf->veb[i]);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005136
5137 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005138}
5139
5140/**
5141 * i40e_reset_subtask - Set up for resetting the device and driver
5142 * @pf: board private structure
5143 **/
5144static void i40e_reset_subtask(struct i40e_pf *pf)
5145{
5146 u32 reset_flags = 0;
5147
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005148 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005149 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
5150 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
5151 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
5152 }
5153 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
5154 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
5155 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5156 }
5157 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
5158 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
5159 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
5160 }
5161 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
5162 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
5163 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
5164 }
5165
5166 /* If there's a recovery already waiting, it takes
5167 * precedence before starting a new reset sequence.
5168 */
5169 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
5170 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005171 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005172 }
5173
5174 /* If we're already down or resetting, just bail */
5175 if (reset_flags &&
5176 !test_bit(__I40E_DOWN, &pf->state) &&
5177 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
5178 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005179
5180unlock:
5181 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005182}
5183
5184/**
5185 * i40e_handle_link_event - Handle link event
5186 * @pf: board private structure
5187 * @e: event info posted on ARQ
5188 **/
5189static void i40e_handle_link_event(struct i40e_pf *pf,
5190 struct i40e_arq_event_info *e)
5191{
5192 struct i40e_hw *hw = &pf->hw;
5193 struct i40e_aqc_get_link_status *status =
5194 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
5195 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
5196
5197 /* save off old link status information */
5198 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
5199 sizeof(pf->hw.phy.link_info_old));
5200
5201 /* update link status */
5202 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
5203 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
5204 hw_link_info->link_info = status->link_info;
5205 hw_link_info->an_info = status->an_info;
5206 hw_link_info->ext_info = status->ext_info;
5207 hw_link_info->lse_enable =
5208 le16_to_cpu(status->command_flags) &
5209 I40E_AQ_LSE_ENABLE;
5210
5211 /* process the event */
5212 i40e_link_event(pf);
5213
5214 /* Do a new status request to re-enable LSE reporting
5215 * and load new status information into the hw struct,
5216 * then see if the status changed while processing the
5217 * initial event.
5218 */
5219 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
5220 i40e_link_event(pf);
5221}
5222
5223/**
5224 * i40e_clean_adminq_subtask - Clean the AdminQ rings
5225 * @pf: board private structure
5226 **/
5227static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
5228{
5229 struct i40e_arq_event_info event;
5230 struct i40e_hw *hw = &pf->hw;
5231 u16 pending, i = 0;
5232 i40e_status ret;
5233 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00005234 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005235 u32 val;
5236
5237 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
5238 return;
5239
Shannon Nelson86df2422014-05-20 08:01:35 +00005240 /* check for error indications */
5241 val = rd32(&pf->hw, pf->hw.aq.arq.len);
5242 oldval = val;
5243 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
5244 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
5245 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
5246 }
5247 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
5248 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
5249 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
5250 }
5251 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
5252 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
5253 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
5254 }
5255 if (oldval != val)
5256 wr32(&pf->hw, pf->hw.aq.arq.len, val);
5257
5258 val = rd32(&pf->hw, pf->hw.aq.asq.len);
5259 oldval = val;
5260 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
5261 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
5262 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
5263 }
5264 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
5265 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
5266 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
5267 }
5268 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
5269 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
5270 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
5271 }
5272 if (oldval != val)
5273 wr32(&pf->hw, pf->hw.aq.asq.len, val);
5274
Mitch Williams3197ce22013-11-28 06:39:39 +00005275 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005276 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
5277 if (!event.msg_buf)
5278 return;
5279
5280 do {
Mitch Williams2f019122013-11-28 06:39:33 +00005281 event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005282 ret = i40e_clean_arq_element(hw, &event, &pending);
5283 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
5284 dev_info(&pf->pdev->dev, "No ARQ event found\n");
5285 break;
5286 } else if (ret) {
5287 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
5288 break;
5289 }
5290
5291 opcode = le16_to_cpu(event.desc.opcode);
5292 switch (opcode) {
5293
5294 case i40e_aqc_opc_get_link_status:
5295 i40e_handle_link_event(pf, &event);
5296 break;
5297 case i40e_aqc_opc_send_msg_to_pf:
5298 ret = i40e_vc_process_vf_msg(pf,
5299 le16_to_cpu(event.desc.retval),
5300 le32_to_cpu(event.desc.cookie_high),
5301 le32_to_cpu(event.desc.cookie_low),
5302 event.msg_buf,
5303 event.msg_size);
5304 break;
5305 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005306 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005307#ifdef CONFIG_I40E_DCB
5308 rtnl_lock();
5309 ret = i40e_handle_lldp_event(pf, &event);
5310 rtnl_unlock();
5311#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005312 break;
5313 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005314 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005315 i40e_handle_lan_overflow_event(pf, &event);
5316 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00005317 case i40e_aqc_opc_send_msg_to_peer:
5318 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
5319 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005320 default:
5321 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00005322 "ARQ Error: Unknown event 0x%04x received\n",
5323 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005324 break;
5325 }
5326 } while (pending && (i++ < pf->adminq_work_limit));
5327
5328 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
5329 /* re-enable Admin queue interrupt cause */
5330 val = rd32(hw, I40E_PFINT_ICR0_ENA);
5331 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
5332 wr32(hw, I40E_PFINT_ICR0_ENA, val);
5333 i40e_flush(hw);
5334
5335 kfree(event.msg_buf);
5336}
5337
5338/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005339 * i40e_verify_eeprom - make sure eeprom is good to use
5340 * @pf: board private structure
5341 **/
5342static void i40e_verify_eeprom(struct i40e_pf *pf)
5343{
5344 int err;
5345
5346 err = i40e_diag_eeprom_test(&pf->hw);
5347 if (err) {
5348 /* retry in case of garbage read */
5349 err = i40e_diag_eeprom_test(&pf->hw);
5350 if (err) {
5351 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
5352 err);
5353 set_bit(__I40E_BAD_EEPROM, &pf->state);
5354 }
5355 }
5356
5357 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
5358 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
5359 clear_bit(__I40E_BAD_EEPROM, &pf->state);
5360 }
5361}
5362
5363/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005364 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
5365 * @veb: pointer to the VEB instance
5366 *
5367 * This is a recursive function that first builds the attached VSIs then
5368 * recurses in to build the next layer of VEB. We track the connections
5369 * through our own index numbers because the seid's from the HW could
5370 * change across the reset.
5371 **/
5372static int i40e_reconstitute_veb(struct i40e_veb *veb)
5373{
5374 struct i40e_vsi *ctl_vsi = NULL;
5375 struct i40e_pf *pf = veb->pf;
5376 int v, veb_idx;
5377 int ret;
5378
5379 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00005380 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005381 if (pf->vsi[v] &&
5382 pf->vsi[v]->veb_idx == veb->idx &&
5383 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
5384 ctl_vsi = pf->vsi[v];
5385 break;
5386 }
5387 }
5388 if (!ctl_vsi) {
5389 dev_info(&pf->pdev->dev,
5390 "missing owner VSI for veb_idx %d\n", veb->idx);
5391 ret = -ENOENT;
5392 goto end_reconstitute;
5393 }
5394 if (ctl_vsi != pf->vsi[pf->lan_vsi])
5395 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5396 ret = i40e_add_vsi(ctl_vsi);
5397 if (ret) {
5398 dev_info(&pf->pdev->dev,
5399 "rebuild of owner VSI failed: %d\n", ret);
5400 goto end_reconstitute;
5401 }
5402 i40e_vsi_reset_stats(ctl_vsi);
5403
5404 /* create the VEB in the switch and move the VSI onto the VEB */
5405 ret = i40e_add_veb(veb, ctl_vsi);
5406 if (ret)
5407 goto end_reconstitute;
5408
5409 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00005410 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005411 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
5412 continue;
5413
5414 if (pf->vsi[v]->veb_idx == veb->idx) {
5415 struct i40e_vsi *vsi = pf->vsi[v];
5416 vsi->uplink_seid = veb->seid;
5417 ret = i40e_add_vsi(vsi);
5418 if (ret) {
5419 dev_info(&pf->pdev->dev,
5420 "rebuild of vsi_idx %d failed: %d\n",
5421 v, ret);
5422 goto end_reconstitute;
5423 }
5424 i40e_vsi_reset_stats(vsi);
5425 }
5426 }
5427
5428 /* create any VEBs attached to this VEB - RECURSION */
5429 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
5430 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
5431 pf->veb[veb_idx]->uplink_seid = veb->seid;
5432 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
5433 if (ret)
5434 break;
5435 }
5436 }
5437
5438end_reconstitute:
5439 return ret;
5440}
5441
5442/**
5443 * i40e_get_capabilities - get info about the HW
5444 * @pf: the PF struct
5445 **/
5446static int i40e_get_capabilities(struct i40e_pf *pf)
5447{
5448 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
5449 u16 data_size;
5450 int buf_len;
5451 int err;
5452
5453 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
5454 do {
5455 cap_buf = kzalloc(buf_len, GFP_KERNEL);
5456 if (!cap_buf)
5457 return -ENOMEM;
5458
5459 /* this loads the data into the hw struct for us */
5460 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
5461 &data_size,
5462 i40e_aqc_opc_list_func_capabilities,
5463 NULL);
5464 /* data loaded, buffer no longer needed */
5465 kfree(cap_buf);
5466
5467 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
5468 /* retry with a larger buffer */
5469 buf_len = data_size;
5470 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
5471 dev_info(&pf->pdev->dev,
5472 "capability discovery failed: aq=%d\n",
5473 pf->hw.aq.asq_last_status);
5474 return -ENODEV;
5475 }
5476 } while (err);
5477
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00005478 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
5479 (pf->hw.aq.fw_maj_ver < 2)) {
5480 pf->hw.func_caps.num_msix_vectors++;
5481 pf->hw.func_caps.num_msix_vectors_vf++;
5482 }
5483
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005484 if (pf->hw.debug_mask & I40E_DEBUG_USER)
5485 dev_info(&pf->pdev->dev,
5486 "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",
5487 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
5488 pf->hw.func_caps.num_msix_vectors,
5489 pf->hw.func_caps.num_msix_vectors_vf,
5490 pf->hw.func_caps.fd_filters_guaranteed,
5491 pf->hw.func_caps.fd_filters_best_effort,
5492 pf->hw.func_caps.num_tx_qp,
5493 pf->hw.func_caps.num_vsis);
5494
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005495#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
5496 + pf->hw.func_caps.num_vfs)
5497 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
5498 dev_info(&pf->pdev->dev,
5499 "got num_vsis %d, setting num_vsis to %d\n",
5500 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
5501 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
5502 }
5503
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005504 return 0;
5505}
5506
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005507static int i40e_vsi_clear(struct i40e_vsi *vsi);
5508
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005509/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005510 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005511 * @pf: board private structure
5512 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005513static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005514{
5515 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005516 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005517
Jesse Brandeburg407e0632014-06-03 23:50:12 +00005518 /* quick workaround for an NVM issue that leaves a critical register
5519 * uninitialized
5520 */
5521 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
5522 static const u32 hkey[] = {
5523 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
5524 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
5525 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
5526 0x95b3a76d};
5527
5528 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
5529 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
5530 }
5531
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005532 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005533 return;
5534
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005535 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005536 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00005537 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005538 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005539 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005540 break;
5541 }
5542 }
5543
5544 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005545 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005546 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
5547 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005548 if (!vsi) {
5549 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005550 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
5551 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005552 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005553 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005554
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005555 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005556}
5557
5558/**
5559 * i40e_fdir_teardown - release the Flow Director resources
5560 * @pf: board private structure
5561 **/
5562static void i40e_fdir_teardown(struct i40e_pf *pf)
5563{
5564 int i;
5565
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005566 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00005567 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005568 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
5569 i40e_vsi_release(pf->vsi[i]);
5570 break;
5571 }
5572 }
5573}
5574
5575/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005576 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005577 * @pf: board private structure
5578 *
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005579 * Close up the VFs and other things in prep for pf Reset.
5580 **/
5581static int i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005582{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005583 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00005584 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005585 u32 v;
5586
5587 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
5588 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005589 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005590
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005591 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005592
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005593 /* quiesce the VSIs and their queues that are not already DOWN */
5594 i40e_pf_quiesce_all_vsi(pf);
5595
Mitch Williams505682c2014-05-20 08:01:37 +00005596 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005597 if (pf->vsi[v])
5598 pf->vsi[v]->seid = 0;
5599 }
5600
5601 i40e_shutdown_adminq(&pf->hw);
5602
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005603 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00005604 if (hw->hmc.hmc_obj) {
5605 ret = i40e_shutdown_lan_hmc(hw);
5606 if (ret) {
5607 dev_warn(&pf->pdev->dev,
5608 "shutdown_lan_hmc failed: %d\n", ret);
5609 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5610 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005611 }
5612 return ret;
5613}
5614
5615/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005616 * i40e_send_version - update firmware with driver version
5617 * @pf: PF struct
5618 */
5619static void i40e_send_version(struct i40e_pf *pf)
5620{
5621 struct i40e_driver_version dv;
5622
5623 dv.major_version = DRV_VERSION_MAJOR;
5624 dv.minor_version = DRV_VERSION_MINOR;
5625 dv.build_version = DRV_VERSION_BUILD;
5626 dv.subbuild_version = 0;
5627 strncpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
5628 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
5629}
5630
5631/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00005632 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005633 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005634 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005635 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005636static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005637{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005638 struct i40e_hw *hw = &pf->hw;
5639 i40e_status ret;
5640 u32 v;
5641
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005642 /* Now we wait for GRST to settle out.
5643 * We don't have to delete the VEBs or VSIs from the hw switch
5644 * because the reset will make them disappear.
5645 */
5646 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005647 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005648 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005649 goto end_core_reset;
5650 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005651 pf->pfr_count++;
5652
5653 if (test_bit(__I40E_DOWN, &pf->state))
5654 goto end_core_reset;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005655 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005656
5657 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
5658 ret = i40e_init_adminq(&pf->hw);
5659 if (ret) {
5660 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
5661 goto end_core_reset;
5662 }
5663
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005664 /* re-verify the eeprom if we just had an EMP reset */
5665 if (test_bit(__I40E_EMP_RESET_REQUESTED, &pf->state)) {
5666 clear_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
5667 i40e_verify_eeprom(pf);
5668 }
5669
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00005670 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005671 ret = i40e_get_capabilities(pf);
5672 if (ret) {
5673 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
5674 ret);
5675 goto end_core_reset;
5676 }
5677
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005678 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
5679 hw->func_caps.num_rx_qp,
5680 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
5681 if (ret) {
5682 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
5683 goto end_core_reset;
5684 }
5685 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
5686 if (ret) {
5687 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
5688 goto end_core_reset;
5689 }
5690
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005691#ifdef CONFIG_I40E_DCB
5692 ret = i40e_init_pf_dcb(pf);
5693 if (ret) {
5694 dev_info(&pf->pdev->dev, "init_pf_dcb failed: %d\n", ret);
5695 goto end_core_reset;
5696 }
5697#endif /* CONFIG_I40E_DCB */
5698
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005699 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005700 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005701 if (ret)
5702 goto end_core_reset;
5703
5704 /* Rebuild the VSIs and VEBs that existed before reset.
5705 * They are still in our local switch element arrays, so only
5706 * need to rebuild the switch model in the HW.
5707 *
5708 * If there were VEBs but the reconstitution failed, we'll try
5709 * try to recover minimal use by getting the basic PF VSI working.
5710 */
5711 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005712 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005713 /* find the one VEB connected to the MAC, and find orphans */
5714 for (v = 0; v < I40E_MAX_VEB; v++) {
5715 if (!pf->veb[v])
5716 continue;
5717
5718 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
5719 pf->veb[v]->uplink_seid == 0) {
5720 ret = i40e_reconstitute_veb(pf->veb[v]);
5721
5722 if (!ret)
5723 continue;
5724
5725 /* If Main VEB failed, we're in deep doodoo,
5726 * so give up rebuilding the switch and set up
5727 * for minimal rebuild of PF VSI.
5728 * If orphan failed, we'll report the error
5729 * but try to keep going.
5730 */
5731 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
5732 dev_info(&pf->pdev->dev,
5733 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
5734 ret);
5735 pf->vsi[pf->lan_vsi]->uplink_seid
5736 = pf->mac_seid;
5737 break;
5738 } else if (pf->veb[v]->uplink_seid == 0) {
5739 dev_info(&pf->pdev->dev,
5740 "rebuild of orphan VEB failed: %d\n",
5741 ret);
5742 }
5743 }
5744 }
5745 }
5746
5747 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
5748 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
5749 /* no VEB, so rebuild only the Main VSI */
5750 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
5751 if (ret) {
5752 dev_info(&pf->pdev->dev,
5753 "rebuild of Main VSI failed: %d\n", ret);
5754 goto end_core_reset;
5755 }
5756 }
5757
5758 /* reinit the misc interrupt */
5759 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5760 ret = i40e_setup_misc_vector(pf);
5761
5762 /* restart the VSIs that were rebuilt and running before the reset */
5763 i40e_pf_unquiesce_all_vsi(pf);
5764
Mitch Williams69f64b22014-02-13 03:48:46 -08005765 if (pf->num_alloc_vfs) {
5766 for (v = 0; v < pf->num_alloc_vfs; v++)
5767 i40e_reset_vf(&pf->vf[v], true);
5768 }
5769
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005770 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005771 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005772
5773end_core_reset:
5774 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5775}
5776
5777/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005778 * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
5779 * @pf: board private structure
5780 *
5781 * Close up the VFs and other things in prep for a Core Reset,
5782 * then get ready to rebuild the world.
5783 **/
5784static void i40e_handle_reset_warning(struct i40e_pf *pf)
5785{
5786 i40e_status ret;
5787
5788 ret = i40e_prep_for_reset(pf);
5789 if (!ret)
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005790 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005791}
5792
5793/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005794 * i40e_handle_mdd_event
5795 * @pf: pointer to the pf structure
5796 *
5797 * Called from the MDD irq handler to identify possibly malicious vfs
5798 **/
5799static void i40e_handle_mdd_event(struct i40e_pf *pf)
5800{
5801 struct i40e_hw *hw = &pf->hw;
5802 bool mdd_detected = false;
5803 struct i40e_vf *vf;
5804 u32 reg;
5805 int i;
5806
5807 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
5808 return;
5809
5810 /* find what triggered the MDD event */
5811 reg = rd32(hw, I40E_GL_MDET_TX);
5812 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00005813 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
5814 I40E_GL_MDET_TX_PF_NUM_SHIFT;
5815 u8 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
5816 I40E_GL_MDET_TX_VF_NUM_SHIFT;
5817 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT) >>
5818 I40E_GL_MDET_TX_EVENT_SHIFT;
5819 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
5820 I40E_GL_MDET_TX_QUEUE_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005821 dev_info(&pf->pdev->dev,
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00005822 "Malicious Driver Detection event 0x%02x on TX queue %d pf number 0x%02x vf number 0x%02x\n",
5823 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005824 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
5825 mdd_detected = true;
5826 }
5827 reg = rd32(hw, I40E_GL_MDET_RX);
5828 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00005829 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
5830 I40E_GL_MDET_RX_FUNCTION_SHIFT;
5831 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT) >>
5832 I40E_GL_MDET_RX_EVENT_SHIFT;
5833 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
5834 I40E_GL_MDET_RX_QUEUE_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005835 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005836 "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005837 event, queue, func);
5838 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
5839 mdd_detected = true;
5840 }
5841
5842 /* see if one of the VFs needs its hand slapped */
5843 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
5844 vf = &(pf->vf[i]);
5845 reg = rd32(hw, I40E_VP_MDET_TX(i));
5846 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
5847 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
5848 vf->num_mdd_events++;
5849 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
5850 }
5851
5852 reg = rd32(hw, I40E_VP_MDET_RX(i));
5853 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
5854 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
5855 vf->num_mdd_events++;
5856 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
5857 }
5858
5859 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
5860 dev_info(&pf->pdev->dev,
5861 "Too many MDD events on VF %d, disabled\n", i);
5862 dev_info(&pf->pdev->dev,
5863 "Use PF Control I/F to re-enable the VF\n");
5864 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
5865 }
5866 }
5867
5868 /* re-enable mdd interrupt cause */
5869 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
5870 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
5871 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
5872 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
5873 i40e_flush(hw);
5874}
5875
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005876#ifdef CONFIG_I40E_VXLAN
5877/**
5878 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
5879 * @pf: board private structure
5880 **/
5881static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
5882{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005883 struct i40e_hw *hw = &pf->hw;
5884 i40e_status ret;
5885 u8 filter_index;
5886 __be16 port;
5887 int i;
5888
5889 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
5890 return;
5891
5892 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
5893
5894 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5895 if (pf->pending_vxlan_bitmap & (1 << i)) {
5896 pf->pending_vxlan_bitmap &= ~(1 << i);
5897 port = pf->vxlan_ports[i];
5898 ret = port ?
5899 i40e_aq_add_udp_tunnel(hw, ntohs(port),
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005900 I40E_AQC_TUNNEL_TYPE_VXLAN,
5901 &filter_index, NULL)
5902 : i40e_aq_del_udp_tunnel(hw, i, NULL);
5903
5904 if (ret) {
5905 dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
5906 port ? "adding" : "deleting",
5907 ntohs(port), port ? i : i);
5908
5909 pf->vxlan_ports[i] = 0;
5910 } else {
5911 dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
5912 port ? "Added" : "Deleted",
5913 ntohs(port), port ? i : filter_index);
5914 }
5915 }
5916 }
5917}
5918
5919#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005920/**
5921 * i40e_service_task - Run the driver's async subtasks
5922 * @work: pointer to work_struct containing our data
5923 **/
5924static void i40e_service_task(struct work_struct *work)
5925{
5926 struct i40e_pf *pf = container_of(work,
5927 struct i40e_pf,
5928 service_task);
5929 unsigned long start_time = jiffies;
5930
5931 i40e_reset_subtask(pf);
5932 i40e_handle_mdd_event(pf);
5933 i40e_vc_process_vflr_event(pf);
5934 i40e_watchdog_subtask(pf);
5935 i40e_fdir_reinit_subtask(pf);
5936 i40e_check_hang_subtask(pf);
5937 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005938#ifdef CONFIG_I40E_VXLAN
5939 i40e_sync_vxlan_filters_subtask(pf);
5940#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005941 i40e_clean_adminq_subtask(pf);
5942
5943 i40e_service_event_complete(pf);
5944
5945 /* If the tasks have taken longer than one timer cycle or there
5946 * is more work to be done, reschedule the service task now
5947 * rather than wait for the timer to tick again.
5948 */
5949 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
5950 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
5951 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
5952 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
5953 i40e_service_event_schedule(pf);
5954}
5955
5956/**
5957 * i40e_service_timer - timer callback
5958 * @data: pointer to PF struct
5959 **/
5960static void i40e_service_timer(unsigned long data)
5961{
5962 struct i40e_pf *pf = (struct i40e_pf *)data;
5963
5964 mod_timer(&pf->service_timer,
5965 round_jiffies(jiffies + pf->service_timer_period));
5966 i40e_service_event_schedule(pf);
5967}
5968
5969/**
5970 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5971 * @vsi: the VSI being configured
5972 **/
5973static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5974{
5975 struct i40e_pf *pf = vsi->back;
5976
5977 switch (vsi->type) {
5978 case I40E_VSI_MAIN:
5979 vsi->alloc_queue_pairs = pf->num_lan_qps;
5980 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5981 I40E_REQ_DESCRIPTOR_MULTIPLE);
5982 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5983 vsi->num_q_vectors = pf->num_lan_msix;
5984 else
5985 vsi->num_q_vectors = 1;
5986
5987 break;
5988
5989 case I40E_VSI_FDIR:
5990 vsi->alloc_queue_pairs = 1;
5991 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5992 I40E_REQ_DESCRIPTOR_MULTIPLE);
5993 vsi->num_q_vectors = 1;
5994 break;
5995
5996 case I40E_VSI_VMDQ2:
5997 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5998 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5999 I40E_REQ_DESCRIPTOR_MULTIPLE);
6000 vsi->num_q_vectors = pf->num_vmdq_msix;
6001 break;
6002
6003 case I40E_VSI_SRIOV:
6004 vsi->alloc_queue_pairs = pf->num_vf_qps;
6005 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6006 I40E_REQ_DESCRIPTOR_MULTIPLE);
6007 break;
6008
6009 default:
6010 WARN_ON(1);
6011 return -ENODATA;
6012 }
6013
6014 return 0;
6015}
6016
6017/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006018 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
6019 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006020 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006021 *
6022 * On error: returns error code (negative)
6023 * On success: returns 0
6024 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006025static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006026{
6027 int size;
6028 int ret = 0;
6029
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00006030 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006031 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
6032 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
6033 if (!vsi->tx_rings)
6034 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006035 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
6036
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006037 if (alloc_qvectors) {
6038 /* allocate memory for q_vector pointers */
6039 size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
6040 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
6041 if (!vsi->q_vectors) {
6042 ret = -ENOMEM;
6043 goto err_vectors;
6044 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006045 }
6046 return ret;
6047
6048err_vectors:
6049 kfree(vsi->tx_rings);
6050 return ret;
6051}
6052
6053/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006054 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
6055 * @pf: board private structure
6056 * @type: type of VSI
6057 *
6058 * On error: returns error code (negative)
6059 * On success: returns vsi index in PF (positive)
6060 **/
6061static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
6062{
6063 int ret = -ENODEV;
6064 struct i40e_vsi *vsi;
6065 int vsi_idx;
6066 int i;
6067
6068 /* Need to protect the allocation of the VSIs at the PF level */
6069 mutex_lock(&pf->switch_mutex);
6070
6071 /* VSI list may be fragmented if VSI creation/destruction has
6072 * been happening. We can afford to do a quick scan to look
6073 * for any free VSIs in the list.
6074 *
6075 * find next empty vsi slot, looping back around if necessary
6076 */
6077 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00006078 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006079 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00006080 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006081 i = 0;
6082 while (i < pf->next_vsi && pf->vsi[i])
6083 i++;
6084 }
6085
Mitch Williams505682c2014-05-20 08:01:37 +00006086 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006087 vsi_idx = i; /* Found one! */
6088 } else {
6089 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00006090 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006091 }
6092 pf->next_vsi = ++i;
6093
6094 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
6095 if (!vsi) {
6096 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00006097 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006098 }
6099 vsi->type = type;
6100 vsi->back = pf;
6101 set_bit(__I40E_DOWN, &vsi->state);
6102 vsi->flags = 0;
6103 vsi->idx = vsi_idx;
6104 vsi->rx_itr_setting = pf->rx_itr_default;
6105 vsi->tx_itr_setting = pf->tx_itr_default;
6106 vsi->netdev_registered = false;
6107 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
6108 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00006109 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006110
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006111 ret = i40e_set_num_rings_in_vsi(vsi);
6112 if (ret)
6113 goto err_rings;
6114
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006115 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006116 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006117 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00006118
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006119 /* Setup default MSIX irq handler for VSI */
6120 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
6121
6122 pf->vsi[vsi_idx] = vsi;
6123 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00006124 goto unlock_pf;
6125
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006126err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00006127 pf->next_vsi = i - 1;
6128 kfree(vsi);
6129unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006130 mutex_unlock(&pf->switch_mutex);
6131 return ret;
6132}
6133
6134/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006135 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
6136 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006137 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006138 *
6139 * On error: returns error code (negative)
6140 * On success: returns 0
6141 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006142static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006143{
6144 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006145 if (free_qvectors) {
6146 kfree(vsi->q_vectors);
6147 vsi->q_vectors = NULL;
6148 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006149 kfree(vsi->tx_rings);
6150 vsi->tx_rings = NULL;
6151 vsi->rx_rings = NULL;
6152}
6153
6154/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006155 * i40e_vsi_clear - Deallocate the VSI provided
6156 * @vsi: the VSI being un-configured
6157 **/
6158static int i40e_vsi_clear(struct i40e_vsi *vsi)
6159{
6160 struct i40e_pf *pf;
6161
6162 if (!vsi)
6163 return 0;
6164
6165 if (!vsi->back)
6166 goto free_vsi;
6167 pf = vsi->back;
6168
6169 mutex_lock(&pf->switch_mutex);
6170 if (!pf->vsi[vsi->idx]) {
6171 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
6172 vsi->idx, vsi->idx, vsi, vsi->type);
6173 goto unlock_vsi;
6174 }
6175
6176 if (pf->vsi[vsi->idx] != vsi) {
6177 dev_err(&pf->pdev->dev,
6178 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
6179 pf->vsi[vsi->idx]->idx,
6180 pf->vsi[vsi->idx],
6181 pf->vsi[vsi->idx]->type,
6182 vsi->idx, vsi, vsi->type);
6183 goto unlock_vsi;
6184 }
6185
6186 /* updates the pf for this cleared vsi */
6187 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
6188 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
6189
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006190 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00006191
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006192 pf->vsi[vsi->idx] = NULL;
6193 if (vsi->idx < pf->next_vsi)
6194 pf->next_vsi = vsi->idx;
6195
6196unlock_vsi:
6197 mutex_unlock(&pf->switch_mutex);
6198free_vsi:
6199 kfree(vsi);
6200
6201 return 0;
6202}
6203
6204/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006205 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
6206 * @vsi: the VSI being cleaned
6207 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006208static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006209{
6210 int i;
6211
Greg Rose8e9dca52013-12-18 13:45:53 +00006212 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00006213 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00006214 kfree_rcu(vsi->tx_rings[i], rcu);
6215 vsi->tx_rings[i] = NULL;
6216 vsi->rx_rings[i] = NULL;
6217 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006218 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006219}
6220
6221/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006222 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
6223 * @vsi: the VSI being configured
6224 **/
6225static int i40e_alloc_rings(struct i40e_vsi *vsi)
6226{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00006227 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006228 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006229 int i;
6230
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006231 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00006232 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00006233 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006234 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
6235 if (!tx_ring)
6236 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006237
6238 tx_ring->queue_index = i;
6239 tx_ring->reg_idx = vsi->base_queue + i;
6240 tx_ring->ring_active = false;
6241 tx_ring->vsi = vsi;
6242 tx_ring->netdev = vsi->netdev;
6243 tx_ring->dev = &pf->pdev->dev;
6244 tx_ring->count = vsi->num_desc;
6245 tx_ring->size = 0;
6246 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006247 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006248
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006249 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006250 rx_ring->queue_index = i;
6251 rx_ring->reg_idx = vsi->base_queue + i;
6252 rx_ring->ring_active = false;
6253 rx_ring->vsi = vsi;
6254 rx_ring->netdev = vsi->netdev;
6255 rx_ring->dev = &pf->pdev->dev;
6256 rx_ring->count = vsi->num_desc;
6257 rx_ring->size = 0;
6258 rx_ring->dcb_tc = 0;
6259 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
6260 set_ring_16byte_desc_enabled(rx_ring);
6261 else
6262 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006263 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006264 }
6265
6266 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006267
6268err_out:
6269 i40e_vsi_clear_rings(vsi);
6270 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006271}
6272
6273/**
6274 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
6275 * @pf: board private structure
6276 * @vectors: the number of MSI-X vectors to request
6277 *
6278 * Returns the number of vectors reserved, or error
6279 **/
6280static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
6281{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006282 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
6283 I40E_MIN_MSIX, vectors);
6284 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006285 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006286 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006287 vectors = 0;
6288 }
6289
6290 return vectors;
6291}
6292
6293/**
6294 * i40e_init_msix - Setup the MSIX capability
6295 * @pf: board private structure
6296 *
6297 * Work with the OS to set up the MSIX vectors needed.
6298 *
6299 * Returns 0 on success, negative on failure
6300 **/
6301static int i40e_init_msix(struct i40e_pf *pf)
6302{
6303 i40e_status err = 0;
6304 struct i40e_hw *hw = &pf->hw;
6305 int v_budget, i;
6306 int vec;
6307
6308 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6309 return -ENODEV;
6310
6311 /* The number of vectors we'll request will be comprised of:
6312 * - Add 1 for "other" cause for Admin Queue events, etc.
6313 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006314 * - Queues being used for RSS.
6315 * We don't need as many as max_rss_size vectors.
6316 * use rss_size instead in the calculation since that
6317 * is governed by number of cpus in the system.
6318 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006319 * - The number of VMDq pairs
6320 * Once we count this up, try the request.
6321 *
6322 * If we can't get what we want, we'll simplify to nearly nothing
6323 * and try again. If that still fails, we punt.
6324 */
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006325 pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006326 pf->num_vmdq_msix = pf->num_vmdq_qps;
6327 v_budget = 1 + pf->num_lan_msix;
6328 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006329 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006330 v_budget++;
6331
6332 /* Scale down if necessary, and the rings will share vectors */
6333 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
6334
6335 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
6336 GFP_KERNEL);
6337 if (!pf->msix_entries)
6338 return -ENOMEM;
6339
6340 for (i = 0; i < v_budget; i++)
6341 pf->msix_entries[i].entry = i;
6342 vec = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006343
6344 if (vec != v_budget) {
6345 /* If we have limited resources, we will start with no vectors
6346 * for the special features and then allocate vectors to some
6347 * of these features based on the policy and at the end disable
6348 * the features that did not get any vectors.
6349 */
6350 pf->num_vmdq_msix = 0;
6351 }
6352
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006353 if (vec < I40E_MIN_MSIX) {
6354 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
6355 kfree(pf->msix_entries);
6356 pf->msix_entries = NULL;
6357 return -ENODEV;
6358
6359 } else if (vec == I40E_MIN_MSIX) {
6360 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006361 pf->num_vmdq_vsis = 0;
6362 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006363 pf->num_lan_qps = 1;
6364 pf->num_lan_msix = 1;
6365
6366 } else if (vec != v_budget) {
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006367 /* reserve the misc vector */
6368 vec--;
6369
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006370 /* Scale vector usage down */
6371 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006372 pf->num_vmdq_vsis = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006373
6374 /* partition out the remaining vectors */
6375 switch (vec) {
6376 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006377 pf->num_lan_msix = 1;
6378 break;
6379 case 3:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006380 pf->num_lan_msix = 2;
6381 break;
6382 default:
6383 pf->num_lan_msix = min_t(int, (vec / 2),
6384 pf->num_lan_qps);
6385 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
6386 I40E_DEFAULT_NUM_VMDQ_VSI);
6387 break;
6388 }
6389 }
6390
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006391 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
6392 (pf->num_vmdq_msix == 0)) {
6393 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
6394 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
6395 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006396 return err;
6397}
6398
6399/**
Greg Rose90e04072014-03-06 08:59:57 +00006400 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00006401 * @vsi: the VSI being configured
6402 * @v_idx: index of the vector in the vsi struct
6403 *
6404 * We allocate one q_vector. If allocation fails we return -ENOMEM.
6405 **/
Greg Rose90e04072014-03-06 08:59:57 +00006406static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00006407{
6408 struct i40e_q_vector *q_vector;
6409
6410 /* allocate q_vector */
6411 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
6412 if (!q_vector)
6413 return -ENOMEM;
6414
6415 q_vector->vsi = vsi;
6416 q_vector->v_idx = v_idx;
6417 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
6418 if (vsi->netdev)
6419 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00006420 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00006421
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00006422 q_vector->rx.latency_range = I40E_LOW_LATENCY;
6423 q_vector->tx.latency_range = I40E_LOW_LATENCY;
6424
Alexander Duyck493fb302013-09-28 07:01:44 +00006425 /* tie q_vector and vsi together */
6426 vsi->q_vectors[v_idx] = q_vector;
6427
6428 return 0;
6429}
6430
6431/**
Greg Rose90e04072014-03-06 08:59:57 +00006432 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006433 * @vsi: the VSI being configured
6434 *
6435 * We allocate one q_vector per queue interrupt. If allocation fails we
6436 * return -ENOMEM.
6437 **/
Greg Rose90e04072014-03-06 08:59:57 +00006438static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006439{
6440 struct i40e_pf *pf = vsi->back;
6441 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00006442 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006443
6444 /* if not MSIX, give the one vector only to the LAN VSI */
6445 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6446 num_q_vectors = vsi->num_q_vectors;
6447 else if (vsi == pf->vsi[pf->lan_vsi])
6448 num_q_vectors = 1;
6449 else
6450 return -EINVAL;
6451
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006452 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00006453 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00006454 if (err)
6455 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006456 }
6457
6458 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00006459
6460err_out:
6461 while (v_idx--)
6462 i40e_free_q_vector(vsi, v_idx);
6463
6464 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006465}
6466
6467/**
6468 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
6469 * @pf: board private structure to initialize
6470 **/
6471static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
6472{
6473 int err = 0;
6474
6475 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
6476 err = i40e_init_msix(pf);
6477 if (err) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006478 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
6479 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006480 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006481 I40E_FLAG_SRIOV_ENABLED |
6482 I40E_FLAG_FD_SB_ENABLED |
6483 I40E_FLAG_FD_ATR_ENABLED |
6484 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006485
6486 /* rework the queue expectations without MSIX */
6487 i40e_determine_queue_usage(pf);
6488 }
6489 }
6490
6491 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
6492 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006493 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006494 err = pci_enable_msi(pf->pdev);
6495 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00006496 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006497 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
6498 }
6499 }
6500
Shannon Nelson958a3e32013-09-28 07:13:28 +00006501 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006502 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00006503
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006504 /* track first vector for misc interrupts */
6505 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
6506}
6507
6508/**
6509 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
6510 * @pf: board private structure
6511 *
6512 * This sets up the handler for MSIX 0, which is used to manage the
6513 * non-queue interrupts, e.g. AdminQ and errors. This is not used
6514 * when in MSI or Legacy interrupt mode.
6515 **/
6516static int i40e_setup_misc_vector(struct i40e_pf *pf)
6517{
6518 struct i40e_hw *hw = &pf->hw;
6519 int err = 0;
6520
6521 /* Only request the irq if this is the first time through, and
6522 * not when we're rebuilding after a Reset
6523 */
6524 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6525 err = request_irq(pf->msix_entries[0].vector,
6526 i40e_intr, 0, pf->misc_int_name, pf);
6527 if (err) {
6528 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006529 "request_irq for %s failed: %d\n",
6530 pf->misc_int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006531 return -EFAULT;
6532 }
6533 }
6534
6535 i40e_enable_misc_int_causes(hw);
6536
6537 /* associate no queues to the misc vector */
6538 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
6539 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
6540
6541 i40e_flush(hw);
6542
6543 i40e_irq_dynamic_enable_icr0(pf);
6544
6545 return err;
6546}
6547
6548/**
6549 * i40e_config_rss - Prepare for RSS if used
6550 * @pf: board private structure
6551 **/
6552static int i40e_config_rss(struct i40e_pf *pf)
6553{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006554 /* Set of random keys generated using kernel random number generator */
6555 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
6556 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
6557 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
6558 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00006559 struct i40e_hw *hw = &pf->hw;
6560 u32 lut = 0;
6561 int i, j;
6562 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006563
6564 /* Fill out hash function seed */
6565 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
6566 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
6567
6568 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
6569 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
6570 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00006571 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006572 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
6573 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
6574
6575 /* Populate the LUT with max no. of queues in round robin fashion */
6576 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
6577
6578 /* The assumption is that lan qp count will be the highest
6579 * qp count for any PF VSI that needs RSS.
6580 * If multiple VSIs need RSS support, all the qp counts
6581 * for those VSIs should be a power of 2 for RSS to work.
6582 * If LAN VSI is the only consumer for RSS then this requirement
6583 * is not necessary.
6584 */
6585 if (j == pf->rss_size)
6586 j = 0;
6587 /* lut = 4-byte sliding window of 4 lut entries */
6588 lut = (lut << 8) | (j &
6589 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
6590 /* On i = 3, we have 4 entries in lut; write to the register */
6591 if ((i & 3) == 3)
6592 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
6593 }
6594 i40e_flush(hw);
6595
6596 return 0;
6597}
6598
6599/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006600 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
6601 * @pf: board private structure
6602 * @queue_count: the requested queue count for rss.
6603 *
6604 * returns 0 if rss is not enabled, if enabled returns the final rss queue
6605 * count which may be different from the requested queue count.
6606 **/
6607int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
6608{
6609 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
6610 return 0;
6611
6612 queue_count = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006613
6614 if (queue_count != pf->rss_size) {
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006615 i40e_prep_for_reset(pf);
6616
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006617 pf->rss_size = queue_count;
6618
6619 i40e_reset_and_rebuild(pf, true);
6620 i40e_config_rss(pf);
6621 }
6622 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
6623 return pf->rss_size;
6624}
6625
6626/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006627 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
6628 * @pf: board private structure to initialize
6629 *
6630 * i40e_sw_init initializes the Adapter private data structure.
6631 * Fields are initialized based on PCI device information and
6632 * OS network device settings (MTU size).
6633 **/
6634static int i40e_sw_init(struct i40e_pf *pf)
6635{
6636 int err = 0;
6637 int size;
6638
6639 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
6640 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00006641 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006642 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
6643 if (I40E_DEBUG_USER & debug)
6644 pf->hw.debug_mask = debug;
6645 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
6646 I40E_DEFAULT_MSG_ENABLE);
6647 }
6648
6649 /* Set default capability flags */
6650 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
6651 I40E_FLAG_MSI_ENABLED |
6652 I40E_FLAG_MSIX_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006653 I40E_FLAG_RX_1BUF_ENABLED;
6654
Mitch Williamsca99eb92014-04-04 04:43:07 +00006655 /* Set default ITR */
6656 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
6657 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
6658
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006659 /* Depending on PF configurations, it is possible that the RSS
6660 * maximum might end up larger than the available queues
6661 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006662 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006663 pf->rss_size_max = min_t(int, pf->rss_size_max,
6664 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006665 if (pf->hw.func_caps.rss) {
6666 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00006667 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006668 } else {
6669 pf->rss_size = 1;
6670 }
6671
Catherine Sullivan2050bc62013-12-18 13:46:03 +00006672 /* MFP mode enabled */
6673 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
6674 pf->flags |= I40E_FLAG_MFP_ENABLED;
6675 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
6676 }
6677
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006678 /* FW/NVM is not yet fixed in this regard */
6679 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
6680 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
6681 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6682 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00006683 /* Setup a counter for fd_atr per pf */
6684 pf->fd_atr_cnt_idx = I40E_FD_ATR_STAT_IDX(pf->hw.pf_id);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006685 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006686 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00006687 /* Setup a counter for fd_sb per pf */
6688 pf->fd_sb_cnt_idx = I40E_FD_SB_STAT_IDX(pf->hw.pf_id);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006689 } else {
6690 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00006691 "Flow Director Sideband mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006692 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006693 pf->fdir_pf_filter_count =
6694 pf->hw.func_caps.fd_filters_guaranteed;
6695 pf->hw.fdir_shared_filter_count =
6696 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006697 }
6698
6699 if (pf->hw.func_caps.vmdq) {
6700 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
6701 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
6702 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
6703 }
6704
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006705#ifdef CONFIG_PCI_IOV
6706 if (pf->hw.func_caps.num_vfs) {
6707 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
6708 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
6709 pf->num_req_vfs = min_t(int,
6710 pf->hw.func_caps.num_vfs,
6711 I40E_MAX_VF_COUNT);
6712 }
6713#endif /* CONFIG_PCI_IOV */
6714 pf->eeprom_version = 0xDEAD;
6715 pf->lan_veb = I40E_NO_VEB;
6716 pf->lan_vsi = I40E_NO_VSI;
6717
6718 /* set up queue assignment tracking */
6719 size = sizeof(struct i40e_lump_tracking)
6720 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
6721 pf->qp_pile = kzalloc(size, GFP_KERNEL);
6722 if (!pf->qp_pile) {
6723 err = -ENOMEM;
6724 goto sw_init_done;
6725 }
6726 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
6727 pf->qp_pile->search_hint = 0;
6728
6729 /* set up vector assignment tracking */
6730 size = sizeof(struct i40e_lump_tracking)
6731 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
6732 pf->irq_pile = kzalloc(size, GFP_KERNEL);
6733 if (!pf->irq_pile) {
6734 kfree(pf->qp_pile);
6735 err = -ENOMEM;
6736 goto sw_init_done;
6737 }
6738 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
6739 pf->irq_pile->search_hint = 0;
6740
6741 mutex_init(&pf->switch_mutex);
6742
6743sw_init_done:
6744 return err;
6745}
6746
6747/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006748 * i40e_set_ntuple - set the ntuple feature flag and take action
6749 * @pf: board private structure to initialize
6750 * @features: the feature set that the stack is suggesting
6751 *
6752 * returns a bool to indicate if reset needs to happen
6753 **/
6754bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
6755{
6756 bool need_reset = false;
6757
6758 /* Check if Flow Director n-tuple support was enabled or disabled. If
6759 * the state changed, we need to reset.
6760 */
6761 if (features & NETIF_F_NTUPLE) {
6762 /* Enable filters and mark for reset */
6763 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
6764 need_reset = true;
6765 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
6766 } else {
6767 /* turn off filters, mark for reset and clear SW filter list */
6768 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
6769 need_reset = true;
6770 i40e_fdir_filter_exit(pf);
6771 }
6772 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6773 /* if ATR was disabled it can be re-enabled. */
6774 if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
6775 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6776 }
6777 return need_reset;
6778}
6779
6780/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006781 * i40e_set_features - set the netdev feature flags
6782 * @netdev: ptr to the netdev being adjusted
6783 * @features: the feature set that the stack is suggesting
6784 **/
6785static int i40e_set_features(struct net_device *netdev,
6786 netdev_features_t features)
6787{
6788 struct i40e_netdev_priv *np = netdev_priv(netdev);
6789 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006790 struct i40e_pf *pf = vsi->back;
6791 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006792
6793 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6794 i40e_vlan_stripping_enable(vsi);
6795 else
6796 i40e_vlan_stripping_disable(vsi);
6797
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006798 need_reset = i40e_set_ntuple(pf, features);
6799
6800 if (need_reset)
6801 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
6802
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006803 return 0;
6804}
6805
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006806#ifdef CONFIG_I40E_VXLAN
6807/**
6808 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
6809 * @pf: board private structure
6810 * @port: The UDP port to look up
6811 *
6812 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
6813 **/
6814static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
6815{
6816 u8 i;
6817
6818 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6819 if (pf->vxlan_ports[i] == port)
6820 return i;
6821 }
6822
6823 return i;
6824}
6825
6826/**
6827 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
6828 * @netdev: This physical port's netdev
6829 * @sa_family: Socket Family that VXLAN is notifying us about
6830 * @port: New UDP port number that VXLAN started listening to
6831 **/
6832static void i40e_add_vxlan_port(struct net_device *netdev,
6833 sa_family_t sa_family, __be16 port)
6834{
6835 struct i40e_netdev_priv *np = netdev_priv(netdev);
6836 struct i40e_vsi *vsi = np->vsi;
6837 struct i40e_pf *pf = vsi->back;
6838 u8 next_idx;
6839 u8 idx;
6840
6841 if (sa_family == AF_INET6)
6842 return;
6843
6844 idx = i40e_get_vxlan_port_idx(pf, port);
6845
6846 /* Check if port already exists */
6847 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6848 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
6849 return;
6850 }
6851
6852 /* Now check if there is space to add the new port */
6853 next_idx = i40e_get_vxlan_port_idx(pf, 0);
6854
6855 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6856 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
6857 ntohs(port));
6858 return;
6859 }
6860
6861 /* New port: add it and mark its index in the bitmap */
6862 pf->vxlan_ports[next_idx] = port;
6863 pf->pending_vxlan_bitmap |= (1 << next_idx);
6864
6865 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6866}
6867
6868/**
6869 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
6870 * @netdev: This physical port's netdev
6871 * @sa_family: Socket Family that VXLAN is notifying us about
6872 * @port: UDP port number that VXLAN stopped listening to
6873 **/
6874static void i40e_del_vxlan_port(struct net_device *netdev,
6875 sa_family_t sa_family, __be16 port)
6876{
6877 struct i40e_netdev_priv *np = netdev_priv(netdev);
6878 struct i40e_vsi *vsi = np->vsi;
6879 struct i40e_pf *pf = vsi->back;
6880 u8 idx;
6881
6882 if (sa_family == AF_INET6)
6883 return;
6884
6885 idx = i40e_get_vxlan_port_idx(pf, port);
6886
6887 /* Check if port already exists */
6888 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6889 /* if port exists, set it to 0 (mark for deletion)
6890 * and make it pending
6891 */
6892 pf->vxlan_ports[idx] = 0;
6893
6894 pf->pending_vxlan_bitmap |= (1 << idx);
6895
6896 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6897 } else {
6898 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6899 ntohs(port));
6900 }
6901}
6902
6903#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006904#ifdef HAVE_FDB_OPS
6905#ifdef USE_CONST_DEV_UC_CHAR
6906static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
6907 struct net_device *dev,
6908 const unsigned char *addr,
6909 u16 flags)
6910#else
6911static int i40e_ndo_fdb_add(struct ndmsg *ndm,
6912 struct net_device *dev,
6913 unsigned char *addr,
6914 u16 flags)
6915#endif
6916{
6917 struct i40e_netdev_priv *np = netdev_priv(dev);
6918 struct i40e_pf *pf = np->vsi->back;
6919 int err = 0;
6920
6921 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
6922 return -EOPNOTSUPP;
6923
6924 /* Hardware does not support aging addresses so if a
6925 * ndm_state is given only allow permanent addresses
6926 */
6927 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6928 netdev_info(dev, "FDB only supports static addresses\n");
6929 return -EINVAL;
6930 }
6931
6932 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
6933 err = dev_uc_add_excl(dev, addr);
6934 else if (is_multicast_ether_addr(addr))
6935 err = dev_mc_add_excl(dev, addr);
6936 else
6937 err = -EINVAL;
6938
6939 /* Only return duplicate errors if NLM_F_EXCL is set */
6940 if (err == -EEXIST && !(flags & NLM_F_EXCL))
6941 err = 0;
6942
6943 return err;
6944}
6945
6946#ifndef USE_DEFAULT_FDB_DEL_DUMP
6947#ifdef USE_CONST_DEV_UC_CHAR
6948static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6949 struct net_device *dev,
6950 const unsigned char *addr)
6951#else
6952static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6953 struct net_device *dev,
6954 unsigned char *addr)
6955#endif
6956{
6957 struct i40e_netdev_priv *np = netdev_priv(dev);
6958 struct i40e_pf *pf = np->vsi->back;
6959 int err = -EOPNOTSUPP;
6960
6961 if (ndm->ndm_state & NUD_PERMANENT) {
6962 netdev_info(dev, "FDB only supports static addresses\n");
6963 return -EINVAL;
6964 }
6965
6966 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
6967 if (is_unicast_ether_addr(addr))
6968 err = dev_uc_del(dev, addr);
6969 else if (is_multicast_ether_addr(addr))
6970 err = dev_mc_del(dev, addr);
6971 else
6972 err = -EINVAL;
6973 }
6974
6975 return err;
6976}
6977
6978static int i40e_ndo_fdb_dump(struct sk_buff *skb,
6979 struct netlink_callback *cb,
6980 struct net_device *dev,
6981 int idx)
6982{
6983 struct i40e_netdev_priv *np = netdev_priv(dev);
6984 struct i40e_pf *pf = np->vsi->back;
6985
6986 if (pf->flags & I40E_FLAG_SRIOV_ENABLED)
6987 idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
6988
6989 return idx;
6990}
6991
6992#endif /* USE_DEFAULT_FDB_DEL_DUMP */
6993#endif /* HAVE_FDB_OPS */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006994static const struct net_device_ops i40e_netdev_ops = {
6995 .ndo_open = i40e_open,
6996 .ndo_stop = i40e_close,
6997 .ndo_start_xmit = i40e_lan_xmit_frame,
6998 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
6999 .ndo_set_rx_mode = i40e_set_rx_mode,
7000 .ndo_validate_addr = eth_validate_addr,
7001 .ndo_set_mac_address = i40e_set_mac,
7002 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00007003 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007004 .ndo_tx_timeout = i40e_tx_timeout,
7005 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
7006 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
7007#ifdef CONFIG_NET_POLL_CONTROLLER
7008 .ndo_poll_controller = i40e_netpoll,
7009#endif
7010 .ndo_setup_tc = i40e_setup_tc,
7011 .ndo_set_features = i40e_set_features,
7012 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
7013 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04007014 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007015 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00007016 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Mitch Williamsc674d122014-05-20 08:01:40 +00007017 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofck,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007018#ifdef CONFIG_I40E_VXLAN
7019 .ndo_add_vxlan_port = i40e_add_vxlan_port,
7020 .ndo_del_vxlan_port = i40e_del_vxlan_port,
7021#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00007022#ifdef HAVE_FDB_OPS
7023 .ndo_fdb_add = i40e_ndo_fdb_add,
7024#ifndef USE_DEFAULT_FDB_DEL_DUMP
7025 .ndo_fdb_del = i40e_ndo_fdb_del,
7026 .ndo_fdb_dump = i40e_ndo_fdb_dump,
7027#endif
7028#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007029};
7030
7031/**
7032 * i40e_config_netdev - Setup the netdev flags
7033 * @vsi: the VSI being configured
7034 *
7035 * Returns 0 on success, negative value on failure
7036 **/
7037static int i40e_config_netdev(struct i40e_vsi *vsi)
7038{
Greg Rose1a103702013-11-28 06:42:39 +00007039 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007040 struct i40e_pf *pf = vsi->back;
7041 struct i40e_hw *hw = &pf->hw;
7042 struct i40e_netdev_priv *np;
7043 struct net_device *netdev;
7044 u8 mac_addr[ETH_ALEN];
7045 int etherdev_size;
7046
7047 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007048 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007049 if (!netdev)
7050 return -ENOMEM;
7051
7052 vsi->netdev = netdev;
7053 np = netdev_priv(netdev);
7054 np->vsi = vsi;
7055
Or Gerlitzd70e9412014-03-18 10:36:45 +02007056 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007057 NETIF_F_GSO_UDP_TUNNEL |
Or Gerlitzd70e9412014-03-18 10:36:45 +02007058 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007059
7060 netdev->features = NETIF_F_SG |
7061 NETIF_F_IP_CSUM |
7062 NETIF_F_SCTP_CSUM |
7063 NETIF_F_HIGHDMA |
7064 NETIF_F_GSO_UDP_TUNNEL |
7065 NETIF_F_HW_VLAN_CTAG_TX |
7066 NETIF_F_HW_VLAN_CTAG_RX |
7067 NETIF_F_HW_VLAN_CTAG_FILTER |
7068 NETIF_F_IPV6_CSUM |
7069 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00007070 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007071 NETIF_F_TSO6 |
7072 NETIF_F_RXCSUM |
7073 NETIF_F_RXHASH |
7074 0;
7075
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00007076 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
7077 netdev->features |= NETIF_F_NTUPLE;
7078
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007079 /* copy netdev features into list of user selectable features */
7080 netdev->hw_features |= netdev->features;
7081
7082 if (vsi->type == I40E_VSI_MAIN) {
7083 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00007084 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Greg Rose8c27d422014-05-22 06:31:56 +00007085 /* The following two steps are necessary to prevent reception
7086 * of tagged packets - by default the NVM loads a MAC-VLAN
7087 * filter that will accept any tagged packet. This is to
7088 * prevent that during normal operations until a specific
7089 * VLAN tag filter has been set.
7090 */
7091 i40e_rm_default_mac_filter(vsi, mac_addr);
7092 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007093 } else {
7094 /* relate the VSI_VMDQ name to the VSI_MAIN name */
7095 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
7096 pf->vsi[pf->lan_vsi]->netdev->name);
7097 random_ether_addr(mac_addr);
7098 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
7099 }
Greg Rose1a103702013-11-28 06:42:39 +00007100 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007101
Greg Rose9a173902014-05-22 06:32:02 +00007102 ether_addr_copy(netdev->dev_addr, mac_addr);
7103 ether_addr_copy(netdev->perm_addr, mac_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007104 /* vlan gets same features (except vlan offload)
7105 * after any tweaks for specific VSI types
7106 */
7107 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
7108 NETIF_F_HW_VLAN_CTAG_RX |
7109 NETIF_F_HW_VLAN_CTAG_FILTER);
7110 netdev->priv_flags |= IFF_UNICAST_FLT;
7111 netdev->priv_flags |= IFF_SUPP_NOFCS;
7112 /* Setup netdev TC information */
7113 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
7114
7115 netdev->netdev_ops = &i40e_netdev_ops;
7116 netdev->watchdog_timeo = 5 * HZ;
7117 i40e_set_ethtool_ops(netdev);
7118
7119 return 0;
7120}
7121
7122/**
7123 * i40e_vsi_delete - Delete a VSI from the switch
7124 * @vsi: the VSI being removed
7125 *
7126 * Returns 0 on success, negative value on failure
7127 **/
7128static void i40e_vsi_delete(struct i40e_vsi *vsi)
7129{
7130 /* remove default VSI is not allowed */
7131 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
7132 return;
7133
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007134 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007135}
7136
7137/**
7138 * i40e_add_vsi - Add a VSI to the switch
7139 * @vsi: the VSI being configured
7140 *
7141 * This initializes a VSI context depending on the VSI type to be added and
7142 * passes it down to the add_vsi aq command.
7143 **/
7144static int i40e_add_vsi(struct i40e_vsi *vsi)
7145{
7146 int ret = -ENODEV;
7147 struct i40e_mac_filter *f, *ftmp;
7148 struct i40e_pf *pf = vsi->back;
7149 struct i40e_hw *hw = &pf->hw;
7150 struct i40e_vsi_context ctxt;
7151 u8 enabled_tc = 0x1; /* TC0 enabled */
7152 int f_count = 0;
7153
7154 memset(&ctxt, 0, sizeof(ctxt));
7155 switch (vsi->type) {
7156 case I40E_VSI_MAIN:
7157 /* The PF's main VSI is already setup as part of the
7158 * device initialization, so we'll not bother with
7159 * the add_vsi call, but we will retrieve the current
7160 * VSI context.
7161 */
7162 ctxt.seid = pf->main_vsi_seid;
7163 ctxt.pf_num = pf->hw.pf_id;
7164 ctxt.vf_num = 0;
7165 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
7166 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
7167 if (ret) {
7168 dev_info(&pf->pdev->dev,
7169 "couldn't get pf vsi config, err %d, aq_err %d\n",
7170 ret, pf->hw.aq.asq_last_status);
7171 return -ENOENT;
7172 }
7173 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
7174 vsi->info.valid_sections = 0;
7175
7176 vsi->seid = ctxt.seid;
7177 vsi->id = ctxt.vsi_number;
7178
7179 enabled_tc = i40e_pf_get_tc_map(pf);
7180
7181 /* MFP mode setup queue map and update VSI */
7182 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7183 memset(&ctxt, 0, sizeof(ctxt));
7184 ctxt.seid = pf->main_vsi_seid;
7185 ctxt.pf_num = pf->hw.pf_id;
7186 ctxt.vf_num = 0;
7187 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
7188 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7189 if (ret) {
7190 dev_info(&pf->pdev->dev,
7191 "update vsi failed, aq_err=%d\n",
7192 pf->hw.aq.asq_last_status);
7193 ret = -ENOENT;
7194 goto err;
7195 }
7196 /* update the local VSI info queue map */
7197 i40e_vsi_update_queue_map(vsi, &ctxt);
7198 vsi->info.valid_sections = 0;
7199 } else {
7200 /* Default/Main VSI is only enabled for TC0
7201 * reconfigure it to enable all TCs that are
7202 * available on the port in SFP mode.
7203 */
7204 ret = i40e_vsi_config_tc(vsi, enabled_tc);
7205 if (ret) {
7206 dev_info(&pf->pdev->dev,
7207 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
7208 enabled_tc, ret,
7209 pf->hw.aq.asq_last_status);
7210 ret = -ENOENT;
7211 }
7212 }
7213 break;
7214
7215 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007216 ctxt.pf_num = hw->pf_id;
7217 ctxt.vf_num = 0;
7218 ctxt.uplink_seid = vsi->uplink_seid;
7219 ctxt.connection_type = 0x1; /* regular data port */
7220 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007221 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007222 break;
7223
7224 case I40E_VSI_VMDQ2:
7225 ctxt.pf_num = hw->pf_id;
7226 ctxt.vf_num = 0;
7227 ctxt.uplink_seid = vsi->uplink_seid;
7228 ctxt.connection_type = 0x1; /* regular data port */
7229 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
7230
7231 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7232
7233 /* This VSI is connected to VEB so the switch_id
7234 * should be set to zero by default.
7235 */
7236 ctxt.info.switch_id = 0;
7237 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
7238 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7239
7240 /* Setup the VSI tx/rx queue map for TC0 only for now */
7241 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
7242 break;
7243
7244 case I40E_VSI_SRIOV:
7245 ctxt.pf_num = hw->pf_id;
7246 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
7247 ctxt.uplink_seid = vsi->uplink_seid;
7248 ctxt.connection_type = 0x1; /* regular data port */
7249 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
7250
7251 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7252
7253 /* This VSI is connected to VEB so the switch_id
7254 * should be set to zero by default.
7255 */
7256 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7257
7258 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
7259 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00007260 if (pf->vf[vsi->vf_id].spoofchk) {
7261 ctxt.info.valid_sections |=
7262 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
7263 ctxt.info.sec_flags |=
7264 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
7265 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
7266 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007267 /* Setup the VSI tx/rx queue map for TC0 only for now */
7268 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
7269 break;
7270
7271 default:
7272 return -ENODEV;
7273 }
7274
7275 if (vsi->type != I40E_VSI_MAIN) {
7276 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
7277 if (ret) {
7278 dev_info(&vsi->back->pdev->dev,
7279 "add vsi failed, aq_err=%d\n",
7280 vsi->back->hw.aq.asq_last_status);
7281 ret = -ENOENT;
7282 goto err;
7283 }
7284 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
7285 vsi->info.valid_sections = 0;
7286 vsi->seid = ctxt.seid;
7287 vsi->id = ctxt.vsi_number;
7288 }
7289
7290 /* If macvlan filters already exist, force them to get loaded */
7291 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
7292 f->changed = true;
7293 f_count++;
7294 }
7295 if (f_count) {
7296 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
7297 pf->flags |= I40E_FLAG_FILTER_SYNC;
7298 }
7299
7300 /* Update VSI BW information */
7301 ret = i40e_vsi_get_bw_info(vsi);
7302 if (ret) {
7303 dev_info(&pf->pdev->dev,
7304 "couldn't get vsi bw info, err %d, aq_err %d\n",
7305 ret, pf->hw.aq.asq_last_status);
7306 /* VSI is already added so not tearing that up */
7307 ret = 0;
7308 }
7309
7310err:
7311 return ret;
7312}
7313
7314/**
7315 * i40e_vsi_release - Delete a VSI and free its resources
7316 * @vsi: the VSI being removed
7317 *
7318 * Returns 0 on success or < 0 on error
7319 **/
7320int i40e_vsi_release(struct i40e_vsi *vsi)
7321{
7322 struct i40e_mac_filter *f, *ftmp;
7323 struct i40e_veb *veb = NULL;
7324 struct i40e_pf *pf;
7325 u16 uplink_seid;
7326 int i, n;
7327
7328 pf = vsi->back;
7329
7330 /* release of a VEB-owner or last VSI is not allowed */
7331 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
7332 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
7333 vsi->seid, vsi->uplink_seid);
7334 return -ENODEV;
7335 }
7336 if (vsi == pf->vsi[pf->lan_vsi] &&
7337 !test_bit(__I40E_DOWN, &pf->state)) {
7338 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
7339 return -ENODEV;
7340 }
7341
7342 uplink_seid = vsi->uplink_seid;
7343 if (vsi->type != I40E_VSI_SRIOV) {
7344 if (vsi->netdev_registered) {
7345 vsi->netdev_registered = false;
7346 if (vsi->netdev) {
7347 /* results in a call to i40e_close() */
7348 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007349 }
7350 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00007351 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007352 }
7353 i40e_vsi_disable_irq(vsi);
7354 }
7355
7356 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
7357 i40e_del_filter(vsi, f->macaddr, f->vlan,
7358 f->is_vf, f->is_netdev);
7359 i40e_sync_vsi_filters(vsi);
7360
7361 i40e_vsi_delete(vsi);
7362 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00007363 if (vsi->netdev) {
7364 free_netdev(vsi->netdev);
7365 vsi->netdev = NULL;
7366 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007367 i40e_vsi_clear_rings(vsi);
7368 i40e_vsi_clear(vsi);
7369
7370 /* If this was the last thing on the VEB, except for the
7371 * controlling VSI, remove the VEB, which puts the controlling
7372 * VSI onto the next level down in the switch.
7373 *
7374 * Well, okay, there's one more exception here: don't remove
7375 * the orphan VEBs yet. We'll wait for an explicit remove request
7376 * from up the network stack.
7377 */
Mitch Williams505682c2014-05-20 08:01:37 +00007378 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007379 if (pf->vsi[i] &&
7380 pf->vsi[i]->uplink_seid == uplink_seid &&
7381 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7382 n++; /* count the VSIs */
7383 }
7384 }
7385 for (i = 0; i < I40E_MAX_VEB; i++) {
7386 if (!pf->veb[i])
7387 continue;
7388 if (pf->veb[i]->uplink_seid == uplink_seid)
7389 n++; /* count the VEBs */
7390 if (pf->veb[i]->seid == uplink_seid)
7391 veb = pf->veb[i];
7392 }
7393 if (n == 0 && veb && veb->uplink_seid != 0)
7394 i40e_veb_release(veb);
7395
7396 return 0;
7397}
7398
7399/**
7400 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
7401 * @vsi: ptr to the VSI
7402 *
7403 * This should only be called after i40e_vsi_mem_alloc() which allocates the
7404 * corresponding SW VSI structure and initializes num_queue_pairs for the
7405 * newly allocated VSI.
7406 *
7407 * Returns 0 on success or negative on failure
7408 **/
7409static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
7410{
7411 int ret = -ENOENT;
7412 struct i40e_pf *pf = vsi->back;
7413
Alexander Duyck493fb302013-09-28 07:01:44 +00007414 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007415 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
7416 vsi->seid);
7417 return -EEXIST;
7418 }
7419
7420 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007421 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007422 vsi->seid, vsi->base_vector);
7423 return -EEXIST;
7424 }
7425
Greg Rose90e04072014-03-06 08:59:57 +00007426 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007427 if (ret) {
7428 dev_info(&pf->pdev->dev,
7429 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
7430 vsi->num_q_vectors, vsi->seid, ret);
7431 vsi->num_q_vectors = 0;
7432 goto vector_setup_out;
7433 }
7434
Shannon Nelson958a3e32013-09-28 07:13:28 +00007435 if (vsi->num_q_vectors)
7436 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
7437 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007438 if (vsi->base_vector < 0) {
7439 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007440 "failed to get queue tracking for VSI %d, err=%d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007441 vsi->seid, vsi->base_vector);
7442 i40e_vsi_free_q_vectors(vsi);
7443 ret = -ENOENT;
7444 goto vector_setup_out;
7445 }
7446
7447vector_setup_out:
7448 return ret;
7449}
7450
7451/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007452 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
7453 * @vsi: pointer to the vsi.
7454 *
7455 * This re-allocates a vsi's queue resources.
7456 *
7457 * Returns pointer to the successfully allocated and configured VSI sw struct
7458 * on success, otherwise returns NULL on failure.
7459 **/
7460static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
7461{
7462 struct i40e_pf *pf = vsi->back;
7463 u8 enabled_tc;
7464 int ret;
7465
7466 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7467 i40e_vsi_clear_rings(vsi);
7468
7469 i40e_vsi_free_arrays(vsi, false);
7470 i40e_set_num_rings_in_vsi(vsi);
7471 ret = i40e_vsi_alloc_arrays(vsi, false);
7472 if (ret)
7473 goto err_vsi;
7474
7475 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
7476 if (ret < 0) {
7477 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7478 vsi->seid, ret);
7479 goto err_vsi;
7480 }
7481 vsi->base_queue = ret;
7482
7483 /* Update the FW view of the VSI. Force a reset of TC and queue
7484 * layout configurations.
7485 */
7486 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7487 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7488 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7489 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7490
7491 /* assign it some queues */
7492 ret = i40e_alloc_rings(vsi);
7493 if (ret)
7494 goto err_rings;
7495
7496 /* map all of the rings to the q_vectors */
7497 i40e_vsi_map_rings_to_vectors(vsi);
7498 return vsi;
7499
7500err_rings:
7501 i40e_vsi_free_q_vectors(vsi);
7502 if (vsi->netdev_registered) {
7503 vsi->netdev_registered = false;
7504 unregister_netdev(vsi->netdev);
7505 free_netdev(vsi->netdev);
7506 vsi->netdev = NULL;
7507 }
7508 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7509err_vsi:
7510 i40e_vsi_clear(vsi);
7511 return NULL;
7512}
7513
7514/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007515 * i40e_vsi_setup - Set up a VSI by a given type
7516 * @pf: board private structure
7517 * @type: VSI type
7518 * @uplink_seid: the switch element to link to
7519 * @param1: usage depends upon VSI type. For VF types, indicates VF id
7520 *
7521 * This allocates the sw VSI structure and its queue resources, then add a VSI
7522 * to the identified VEB.
7523 *
7524 * Returns pointer to the successfully allocated and configure VSI sw struct on
7525 * success, otherwise returns NULL on failure.
7526 **/
7527struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
7528 u16 uplink_seid, u32 param1)
7529{
7530 struct i40e_vsi *vsi = NULL;
7531 struct i40e_veb *veb = NULL;
7532 int ret, i;
7533 int v_idx;
7534
7535 /* The requested uplink_seid must be either
7536 * - the PF's port seid
7537 * no VEB is needed because this is the PF
7538 * or this is a Flow Director special case VSI
7539 * - seid of an existing VEB
7540 * - seid of a VSI that owns an existing VEB
7541 * - seid of a VSI that doesn't own a VEB
7542 * a new VEB is created and the VSI becomes the owner
7543 * - seid of the PF VSI, which is what creates the first VEB
7544 * this is a special case of the previous
7545 *
7546 * Find which uplink_seid we were given and create a new VEB if needed
7547 */
7548 for (i = 0; i < I40E_MAX_VEB; i++) {
7549 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
7550 veb = pf->veb[i];
7551 break;
7552 }
7553 }
7554
7555 if (!veb && uplink_seid != pf->mac_seid) {
7556
Mitch Williams505682c2014-05-20 08:01:37 +00007557 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007558 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
7559 vsi = pf->vsi[i];
7560 break;
7561 }
7562 }
7563 if (!vsi) {
7564 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
7565 uplink_seid);
7566 return NULL;
7567 }
7568
7569 if (vsi->uplink_seid == pf->mac_seid)
7570 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
7571 vsi->tc_config.enabled_tc);
7572 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
7573 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
7574 vsi->tc_config.enabled_tc);
7575
7576 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
7577 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
7578 veb = pf->veb[i];
7579 }
7580 if (!veb) {
7581 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
7582 return NULL;
7583 }
7584
7585 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7586 uplink_seid = veb->seid;
7587 }
7588
7589 /* get vsi sw struct */
7590 v_idx = i40e_vsi_mem_alloc(pf, type);
7591 if (v_idx < 0)
7592 goto err_alloc;
7593 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007594 if (!vsi)
7595 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007596 vsi->type = type;
7597 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
7598
7599 if (type == I40E_VSI_MAIN)
7600 pf->lan_vsi = v_idx;
7601 else if (type == I40E_VSI_SRIOV)
7602 vsi->vf_id = param1;
7603 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007604 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
7605 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007606 if (ret < 0) {
7607 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7608 vsi->seid, ret);
7609 goto err_vsi;
7610 }
7611 vsi->base_queue = ret;
7612
7613 /* get a VSI from the hardware */
7614 vsi->uplink_seid = uplink_seid;
7615 ret = i40e_add_vsi(vsi);
7616 if (ret)
7617 goto err_vsi;
7618
7619 switch (vsi->type) {
7620 /* setup the netdev if needed */
7621 case I40E_VSI_MAIN:
7622 case I40E_VSI_VMDQ2:
7623 ret = i40e_config_netdev(vsi);
7624 if (ret)
7625 goto err_netdev;
7626 ret = register_netdev(vsi->netdev);
7627 if (ret)
7628 goto err_netdev;
7629 vsi->netdev_registered = true;
7630 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007631#ifdef CONFIG_I40E_DCB
7632 /* Setup DCB netlink interface */
7633 i40e_dcbnl_setup(vsi);
7634#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007635 /* fall through */
7636
7637 case I40E_VSI_FDIR:
7638 /* set up vectors and rings if needed */
7639 ret = i40e_vsi_setup_vectors(vsi);
7640 if (ret)
7641 goto err_msix;
7642
7643 ret = i40e_alloc_rings(vsi);
7644 if (ret)
7645 goto err_rings;
7646
7647 /* map all of the rings to the q_vectors */
7648 i40e_vsi_map_rings_to_vectors(vsi);
7649
7650 i40e_vsi_reset_stats(vsi);
7651 break;
7652
7653 default:
7654 /* no netdev or rings for the other VSI types */
7655 break;
7656 }
7657
7658 return vsi;
7659
7660err_rings:
7661 i40e_vsi_free_q_vectors(vsi);
7662err_msix:
7663 if (vsi->netdev_registered) {
7664 vsi->netdev_registered = false;
7665 unregister_netdev(vsi->netdev);
7666 free_netdev(vsi->netdev);
7667 vsi->netdev = NULL;
7668 }
7669err_netdev:
7670 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7671err_vsi:
7672 i40e_vsi_clear(vsi);
7673err_alloc:
7674 return NULL;
7675}
7676
7677/**
7678 * i40e_veb_get_bw_info - Query VEB BW information
7679 * @veb: the veb to query
7680 *
7681 * Query the Tx scheduler BW configuration data for given VEB
7682 **/
7683static int i40e_veb_get_bw_info(struct i40e_veb *veb)
7684{
7685 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
7686 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
7687 struct i40e_pf *pf = veb->pf;
7688 struct i40e_hw *hw = &pf->hw;
7689 u32 tc_bw_max;
7690 int ret = 0;
7691 int i;
7692
7693 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
7694 &bw_data, NULL);
7695 if (ret) {
7696 dev_info(&pf->pdev->dev,
7697 "query veb bw config failed, aq_err=%d\n",
7698 hw->aq.asq_last_status);
7699 goto out;
7700 }
7701
7702 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
7703 &ets_data, NULL);
7704 if (ret) {
7705 dev_info(&pf->pdev->dev,
7706 "query veb bw ets config failed, aq_err=%d\n",
7707 hw->aq.asq_last_status);
7708 goto out;
7709 }
7710
7711 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
7712 veb->bw_max_quanta = ets_data.tc_bw_max;
7713 veb->is_abs_credits = bw_data.absolute_credits_enable;
7714 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
7715 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
7716 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7717 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
7718 veb->bw_tc_limit_credits[i] =
7719 le16_to_cpu(bw_data.tc_bw_limits[i]);
7720 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
7721 }
7722
7723out:
7724 return ret;
7725}
7726
7727/**
7728 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
7729 * @pf: board private structure
7730 *
7731 * On error: returns error code (negative)
7732 * On success: returns vsi index in PF (positive)
7733 **/
7734static int i40e_veb_mem_alloc(struct i40e_pf *pf)
7735{
7736 int ret = -ENOENT;
7737 struct i40e_veb *veb;
7738 int i;
7739
7740 /* Need to protect the allocation of switch elements at the PF level */
7741 mutex_lock(&pf->switch_mutex);
7742
7743 /* VEB list may be fragmented if VEB creation/destruction has
7744 * been happening. We can afford to do a quick scan to look
7745 * for any free slots in the list.
7746 *
7747 * find next empty veb slot, looping back around if necessary
7748 */
7749 i = 0;
7750 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
7751 i++;
7752 if (i >= I40E_MAX_VEB) {
7753 ret = -ENOMEM;
7754 goto err_alloc_veb; /* out of VEB slots! */
7755 }
7756
7757 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
7758 if (!veb) {
7759 ret = -ENOMEM;
7760 goto err_alloc_veb;
7761 }
7762 veb->pf = pf;
7763 veb->idx = i;
7764 veb->enabled_tc = 1;
7765
7766 pf->veb[i] = veb;
7767 ret = i;
7768err_alloc_veb:
7769 mutex_unlock(&pf->switch_mutex);
7770 return ret;
7771}
7772
7773/**
7774 * i40e_switch_branch_release - Delete a branch of the switch tree
7775 * @branch: where to start deleting
7776 *
7777 * This uses recursion to find the tips of the branch to be
7778 * removed, deleting until we get back to and can delete this VEB.
7779 **/
7780static void i40e_switch_branch_release(struct i40e_veb *branch)
7781{
7782 struct i40e_pf *pf = branch->pf;
7783 u16 branch_seid = branch->seid;
7784 u16 veb_idx = branch->idx;
7785 int i;
7786
7787 /* release any VEBs on this VEB - RECURSION */
7788 for (i = 0; i < I40E_MAX_VEB; i++) {
7789 if (!pf->veb[i])
7790 continue;
7791 if (pf->veb[i]->uplink_seid == branch->seid)
7792 i40e_switch_branch_release(pf->veb[i]);
7793 }
7794
7795 /* Release the VSIs on this VEB, but not the owner VSI.
7796 *
7797 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
7798 * the VEB itself, so don't use (*branch) after this loop.
7799 */
Mitch Williams505682c2014-05-20 08:01:37 +00007800 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007801 if (!pf->vsi[i])
7802 continue;
7803 if (pf->vsi[i]->uplink_seid == branch_seid &&
7804 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7805 i40e_vsi_release(pf->vsi[i]);
7806 }
7807 }
7808
7809 /* There's one corner case where the VEB might not have been
7810 * removed, so double check it here and remove it if needed.
7811 * This case happens if the veb was created from the debugfs
7812 * commands and no VSIs were added to it.
7813 */
7814 if (pf->veb[veb_idx])
7815 i40e_veb_release(pf->veb[veb_idx]);
7816}
7817
7818/**
7819 * i40e_veb_clear - remove veb struct
7820 * @veb: the veb to remove
7821 **/
7822static void i40e_veb_clear(struct i40e_veb *veb)
7823{
7824 if (!veb)
7825 return;
7826
7827 if (veb->pf) {
7828 struct i40e_pf *pf = veb->pf;
7829
7830 mutex_lock(&pf->switch_mutex);
7831 if (pf->veb[veb->idx] == veb)
7832 pf->veb[veb->idx] = NULL;
7833 mutex_unlock(&pf->switch_mutex);
7834 }
7835
7836 kfree(veb);
7837}
7838
7839/**
7840 * i40e_veb_release - Delete a VEB and free its resources
7841 * @veb: the VEB being removed
7842 **/
7843void i40e_veb_release(struct i40e_veb *veb)
7844{
7845 struct i40e_vsi *vsi = NULL;
7846 struct i40e_pf *pf;
7847 int i, n = 0;
7848
7849 pf = veb->pf;
7850
7851 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +00007852 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007853 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
7854 n++;
7855 vsi = pf->vsi[i];
7856 }
7857 }
7858 if (n != 1) {
7859 dev_info(&pf->pdev->dev,
7860 "can't remove VEB %d with %d VSIs left\n",
7861 veb->seid, n);
7862 return;
7863 }
7864
7865 /* move the remaining VSI to uplink veb */
7866 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
7867 if (veb->uplink_seid) {
7868 vsi->uplink_seid = veb->uplink_seid;
7869 if (veb->uplink_seid == pf->mac_seid)
7870 vsi->veb_idx = I40E_NO_VEB;
7871 else
7872 vsi->veb_idx = veb->veb_idx;
7873 } else {
7874 /* floating VEB */
7875 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
7876 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
7877 }
7878
7879 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
7880 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007881}
7882
7883/**
7884 * i40e_add_veb - create the VEB in the switch
7885 * @veb: the VEB to be instantiated
7886 * @vsi: the controlling VSI
7887 **/
7888static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
7889{
Greg Rose56747262013-11-28 06:39:37 +00007890 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00007891 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007892 int ret;
7893
7894 /* get a VEB from the hardware */
7895 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00007896 veb->enabled_tc, is_default,
7897 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007898 if (ret) {
7899 dev_info(&veb->pf->pdev->dev,
7900 "couldn't add VEB, err %d, aq_err %d\n",
7901 ret, veb->pf->hw.aq.asq_last_status);
7902 return -EPERM;
7903 }
7904
7905 /* get statistics counter */
7906 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
7907 &veb->stats_idx, NULL, NULL, NULL);
7908 if (ret) {
7909 dev_info(&veb->pf->pdev->dev,
7910 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
7911 ret, veb->pf->hw.aq.asq_last_status);
7912 return -EPERM;
7913 }
7914 ret = i40e_veb_get_bw_info(veb);
7915 if (ret) {
7916 dev_info(&veb->pf->pdev->dev,
7917 "couldn't get VEB bw info, err %d, aq_err %d\n",
7918 ret, veb->pf->hw.aq.asq_last_status);
7919 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
7920 return -ENOENT;
7921 }
7922
7923 vsi->uplink_seid = veb->seid;
7924 vsi->veb_idx = veb->idx;
7925 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7926
7927 return 0;
7928}
7929
7930/**
7931 * i40e_veb_setup - Set up a VEB
7932 * @pf: board private structure
7933 * @flags: VEB setup flags
7934 * @uplink_seid: the switch element to link to
7935 * @vsi_seid: the initial VSI seid
7936 * @enabled_tc: Enabled TC bit-map
7937 *
7938 * This allocates the sw VEB structure and links it into the switch
7939 * It is possible and legal for this to be a duplicate of an already
7940 * existing VEB. It is also possible for both uplink and vsi seids
7941 * to be zero, in order to create a floating VEB.
7942 *
7943 * Returns pointer to the successfully allocated VEB sw struct on
7944 * success, otherwise returns NULL on failure.
7945 **/
7946struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
7947 u16 uplink_seid, u16 vsi_seid,
7948 u8 enabled_tc)
7949{
7950 struct i40e_veb *veb, *uplink_veb = NULL;
7951 int vsi_idx, veb_idx;
7952 int ret;
7953
7954 /* if one seid is 0, the other must be 0 to create a floating relay */
7955 if ((uplink_seid == 0 || vsi_seid == 0) &&
7956 (uplink_seid + vsi_seid != 0)) {
7957 dev_info(&pf->pdev->dev,
7958 "one, not both seid's are 0: uplink=%d vsi=%d\n",
7959 uplink_seid, vsi_seid);
7960 return NULL;
7961 }
7962
7963 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +00007964 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007965 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
7966 break;
Mitch Williams505682c2014-05-20 08:01:37 +00007967 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007968 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
7969 vsi_seid);
7970 return NULL;
7971 }
7972
7973 if (uplink_seid && uplink_seid != pf->mac_seid) {
7974 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
7975 if (pf->veb[veb_idx] &&
7976 pf->veb[veb_idx]->seid == uplink_seid) {
7977 uplink_veb = pf->veb[veb_idx];
7978 break;
7979 }
7980 }
7981 if (!uplink_veb) {
7982 dev_info(&pf->pdev->dev,
7983 "uplink seid %d not found\n", uplink_seid);
7984 return NULL;
7985 }
7986 }
7987
7988 /* get veb sw struct */
7989 veb_idx = i40e_veb_mem_alloc(pf);
7990 if (veb_idx < 0)
7991 goto err_alloc;
7992 veb = pf->veb[veb_idx];
7993 veb->flags = flags;
7994 veb->uplink_seid = uplink_seid;
7995 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
7996 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
7997
7998 /* create the VEB in the switch */
7999 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
8000 if (ret)
8001 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +00008002 if (vsi_idx == pf->lan_vsi)
8003 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008004
8005 return veb;
8006
8007err_veb:
8008 i40e_veb_clear(veb);
8009err_alloc:
8010 return NULL;
8011}
8012
8013/**
8014 * i40e_setup_pf_switch_element - set pf vars based on switch type
8015 * @pf: board private structure
8016 * @ele: element we are building info from
8017 * @num_reported: total number of elements
8018 * @printconfig: should we print the contents
8019 *
8020 * helper function to assist in extracting a few useful SEID values.
8021 **/
8022static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
8023 struct i40e_aqc_switch_config_element_resp *ele,
8024 u16 num_reported, bool printconfig)
8025{
8026 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
8027 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
8028 u8 element_type = ele->element_type;
8029 u16 seid = le16_to_cpu(ele->seid);
8030
8031 if (printconfig)
8032 dev_info(&pf->pdev->dev,
8033 "type=%d seid=%d uplink=%d downlink=%d\n",
8034 element_type, seid, uplink_seid, downlink_seid);
8035
8036 switch (element_type) {
8037 case I40E_SWITCH_ELEMENT_TYPE_MAC:
8038 pf->mac_seid = seid;
8039 break;
8040 case I40E_SWITCH_ELEMENT_TYPE_VEB:
8041 /* Main VEB? */
8042 if (uplink_seid != pf->mac_seid)
8043 break;
8044 if (pf->lan_veb == I40E_NO_VEB) {
8045 int v;
8046
8047 /* find existing or else empty VEB */
8048 for (v = 0; v < I40E_MAX_VEB; v++) {
8049 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
8050 pf->lan_veb = v;
8051 break;
8052 }
8053 }
8054 if (pf->lan_veb == I40E_NO_VEB) {
8055 v = i40e_veb_mem_alloc(pf);
8056 if (v < 0)
8057 break;
8058 pf->lan_veb = v;
8059 }
8060 }
8061
8062 pf->veb[pf->lan_veb]->seid = seid;
8063 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
8064 pf->veb[pf->lan_veb]->pf = pf;
8065 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
8066 break;
8067 case I40E_SWITCH_ELEMENT_TYPE_VSI:
8068 if (num_reported != 1)
8069 break;
8070 /* This is immediately after a reset so we can assume this is
8071 * the PF's VSI
8072 */
8073 pf->mac_seid = uplink_seid;
8074 pf->pf_seid = downlink_seid;
8075 pf->main_vsi_seid = seid;
8076 if (printconfig)
8077 dev_info(&pf->pdev->dev,
8078 "pf_seid=%d main_vsi_seid=%d\n",
8079 pf->pf_seid, pf->main_vsi_seid);
8080 break;
8081 case I40E_SWITCH_ELEMENT_TYPE_PF:
8082 case I40E_SWITCH_ELEMENT_TYPE_VF:
8083 case I40E_SWITCH_ELEMENT_TYPE_EMP:
8084 case I40E_SWITCH_ELEMENT_TYPE_BMC:
8085 case I40E_SWITCH_ELEMENT_TYPE_PE:
8086 case I40E_SWITCH_ELEMENT_TYPE_PA:
8087 /* ignore these for now */
8088 break;
8089 default:
8090 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
8091 element_type, seid);
8092 break;
8093 }
8094}
8095
8096/**
8097 * i40e_fetch_switch_configuration - Get switch config from firmware
8098 * @pf: board private structure
8099 * @printconfig: should we print the contents
8100 *
8101 * Get the current switch configuration from the device and
8102 * extract a few useful SEID values.
8103 **/
8104int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
8105{
8106 struct i40e_aqc_get_switch_config_resp *sw_config;
8107 u16 next_seid = 0;
8108 int ret = 0;
8109 u8 *aq_buf;
8110 int i;
8111
8112 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
8113 if (!aq_buf)
8114 return -ENOMEM;
8115
8116 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
8117 do {
8118 u16 num_reported, num_total;
8119
8120 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
8121 I40E_AQ_LARGE_BUF,
8122 &next_seid, NULL);
8123 if (ret) {
8124 dev_info(&pf->pdev->dev,
8125 "get switch config failed %d aq_err=%x\n",
8126 ret, pf->hw.aq.asq_last_status);
8127 kfree(aq_buf);
8128 return -ENOENT;
8129 }
8130
8131 num_reported = le16_to_cpu(sw_config->header.num_reported);
8132 num_total = le16_to_cpu(sw_config->header.num_total);
8133
8134 if (printconfig)
8135 dev_info(&pf->pdev->dev,
8136 "header: %d reported %d total\n",
8137 num_reported, num_total);
8138
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008139 for (i = 0; i < num_reported; i++) {
8140 struct i40e_aqc_switch_config_element_resp *ele =
8141 &sw_config->element[i];
8142
8143 i40e_setup_pf_switch_element(pf, ele, num_reported,
8144 printconfig);
8145 }
8146 } while (next_seid != 0);
8147
8148 kfree(aq_buf);
8149 return ret;
8150}
8151
8152/**
8153 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
8154 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008155 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008156 *
8157 * Returns 0 on success, negative value on failure
8158 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008159static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008160{
Jesse Brandeburg895106a2013-11-26 10:49:16 +00008161 u32 rxfc = 0, txfc = 0, rxfc_reg;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008162 int ret;
8163
8164 /* find out what's out there already */
8165 ret = i40e_fetch_switch_configuration(pf, false);
8166 if (ret) {
8167 dev_info(&pf->pdev->dev,
8168 "couldn't fetch switch config, err %d, aq_err %d\n",
8169 ret, pf->hw.aq.asq_last_status);
8170 return ret;
8171 }
8172 i40e_pf_reset_stats(pf);
8173
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008174 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008175 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008176 struct i40e_vsi *vsi = NULL;
8177 u16 uplink_seid;
8178
8179 /* Set up the PF VSI associated with the PF's main VSI
8180 * that is already in the HW switch
8181 */
8182 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
8183 uplink_seid = pf->veb[pf->lan_veb]->seid;
8184 else
8185 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008186 if (pf->lan_vsi == I40E_NO_VSI)
8187 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
8188 else if (reinit)
8189 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008190 if (!vsi) {
8191 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
8192 i40e_fdir_teardown(pf);
8193 return -EAGAIN;
8194 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008195 } else {
8196 /* force a reset of TC and queue layout configurations */
8197 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
8198 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
8199 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
8200 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
8201 }
8202 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
8203
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008204 i40e_fdir_sb_setup(pf);
8205
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008206 /* Setup static PF queue filter control settings */
8207 ret = i40e_setup_pf_filter_control(pf);
8208 if (ret) {
8209 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
8210 ret);
8211 /* Failure here should not stop continuing other steps */
8212 }
8213
8214 /* enable RSS in the HW, even for only one queue, as the stack can use
8215 * the hash
8216 */
8217 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
8218 i40e_config_rss(pf);
8219
8220 /* fill in link information and enable LSE reporting */
8221 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
8222 i40e_link_event(pf);
8223
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008224 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008225 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
8226 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008227 /* requested_mode is set in probe or by ethtool */
8228 if (!pf->fc_autoneg_status)
8229 goto no_autoneg;
8230
8231 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
8232 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008233 pf->hw.fc.current_mode = I40E_FC_FULL;
8234 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
8235 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
8236 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
8237 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
8238 else
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008239 pf->hw.fc.current_mode = I40E_FC_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008240
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008241 /* sync the flow control settings with the auto-neg values */
8242 switch (pf->hw.fc.current_mode) {
8243 case I40E_FC_FULL:
8244 txfc = 1;
8245 rxfc = 1;
8246 break;
8247 case I40E_FC_TX_PAUSE:
8248 txfc = 1;
8249 rxfc = 0;
8250 break;
8251 case I40E_FC_RX_PAUSE:
8252 txfc = 0;
8253 rxfc = 1;
8254 break;
8255 case I40E_FC_NONE:
8256 case I40E_FC_DEFAULT:
8257 txfc = 0;
8258 rxfc = 0;
8259 break;
8260 case I40E_FC_PFC:
8261 /* TBD */
8262 break;
8263 /* no default case, we have to handle all possibilities here */
8264 }
8265
8266 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
8267
8268 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8269 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
8270 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
8271
8272 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
8273
8274 goto fc_complete;
8275
8276no_autoneg:
8277 /* disable L2 flow control, user can turn it on if they wish */
8278 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
8279 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8280 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
8281
8282fc_complete:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008283 i40e_ptp_init(pf);
8284
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008285 return ret;
8286}
8287
8288/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008289 * i40e_determine_queue_usage - Work out queue distribution
8290 * @pf: board private structure
8291 **/
8292static void i40e_determine_queue_usage(struct i40e_pf *pf)
8293{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008294 int queues_left;
8295
8296 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008297
8298 /* Find the max queues to be put into basic use. We'll always be
8299 * using TC0, whether or not DCB is running, and TC0 will get the
8300 * big RSS set.
8301 */
8302 queues_left = pf->hw.func_caps.num_tx_qp;
8303
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008304 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +00008305 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008306 /* one qp for PF, no queues for anything else */
8307 queues_left = 0;
8308 pf->rss_size = pf->num_lan_qps = 1;
8309
8310 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008311 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
8312 I40E_FLAG_FD_SB_ENABLED |
8313 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008314 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008315 I40E_FLAG_SRIOV_ENABLED |
8316 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +00008317 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
8318 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +00008319 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008320 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +00008321 /* one qp for PF */
8322 pf->rss_size = pf->num_lan_qps = 1;
8323 queues_left -= pf->num_lan_qps;
8324
8325 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
8326 I40E_FLAG_FD_SB_ENABLED |
8327 I40E_FLAG_FD_ATR_ENABLED |
8328 I40E_FLAG_DCB_ENABLED |
8329 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008330 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008331 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008332 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008333 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008334 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008335 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
8336 }
8337 pf->num_lan_qps = pf->rss_size_max;
8338 queues_left -= pf->num_lan_qps;
8339 }
8340
8341 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8342 if (queues_left > 1) {
8343 queues_left -= 1; /* save 1 queue for FD */
8344 } else {
8345 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8346 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
8347 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008348 }
8349
8350 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
8351 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008352 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
8353 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008354 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
8355 }
8356
8357 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8358 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
8359 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
8360 (queues_left / pf->num_vmdq_qps));
8361 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
8362 }
8363
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008364 pf->queues_left = queues_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008365}
8366
8367/**
8368 * i40e_setup_pf_filter_control - Setup PF static filter control
8369 * @pf: PF to be setup
8370 *
8371 * i40e_setup_pf_filter_control sets up a pf's initial filter control
8372 * settings. If PE/FCoE are enabled then it will also set the per PF
8373 * based filter sizes required for them. It also enables Flow director,
8374 * ethertype and macvlan type filter settings for the pf.
8375 *
8376 * Returns 0 on success, negative on failure
8377 **/
8378static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
8379{
8380 struct i40e_filter_control_settings *settings = &pf->filter_settings;
8381
8382 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
8383
8384 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008385 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008386 settings->enable_fdir = true;
8387
8388 /* Ethtype and MACVLAN filters enabled for PF */
8389 settings->enable_ethtype = true;
8390 settings->enable_macvlan = true;
8391
8392 if (i40e_set_filter_control(&pf->hw, settings))
8393 return -ENOENT;
8394
8395 return 0;
8396}
8397
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008398#define INFO_STRING_LEN 255
8399static void i40e_print_features(struct i40e_pf *pf)
8400{
8401 struct i40e_hw *hw = &pf->hw;
8402 char *buf, *string;
8403
8404 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
8405 if (!string) {
8406 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
8407 return;
8408 }
8409
8410 buf = string;
8411
8412 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
8413#ifdef CONFIG_PCI_IOV
8414 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
8415#endif
8416 buf += sprintf(buf, "VSIs: %d QP: %d ", pf->hw.func_caps.num_vsis,
8417 pf->vsi[pf->lan_vsi]->num_queue_pairs);
8418
8419 if (pf->flags & I40E_FLAG_RSS_ENABLED)
8420 buf += sprintf(buf, "RSS ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008421 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00008422 buf += sprintf(buf, "FD_ATR ");
8423 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8424 buf += sprintf(buf, "FD_SB ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008425 buf += sprintf(buf, "NTUPLE ");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00008426 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008427 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008428 buf += sprintf(buf, "DCB ");
8429 if (pf->flags & I40E_FLAG_PTP)
8430 buf += sprintf(buf, "PTP ");
8431
8432 BUG_ON(buf > (string + INFO_STRING_LEN));
8433 dev_info(&pf->pdev->dev, "%s\n", string);
8434 kfree(string);
8435}
8436
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008437/**
8438 * i40e_probe - Device initialization routine
8439 * @pdev: PCI device information struct
8440 * @ent: entry in i40e_pci_tbl
8441 *
8442 * i40e_probe initializes a pf identified by a pci_dev structure.
8443 * The OS initialization, configuring of the pf private structure,
8444 * and a hardware reset occur.
8445 *
8446 * Returns 0 on success, negative on failure
8447 **/
8448static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8449{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008450 struct i40e_pf *pf;
8451 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008452 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008453 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008454 int err = 0;
8455 u32 len;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008456 u32 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008457
8458 err = pci_enable_device_mem(pdev);
8459 if (err)
8460 return err;
8461
8462 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +00008463 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00008464 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00008465 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
8466 if (err) {
8467 dev_err(&pdev->dev,
8468 "DMA configuration failed: 0x%x\n", err);
8469 goto err_dma;
8470 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008471 }
8472
8473 /* set up pci connections */
8474 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
8475 IORESOURCE_MEM), i40e_driver_name);
8476 if (err) {
8477 dev_info(&pdev->dev,
8478 "pci_request_selected_regions failed %d\n", err);
8479 goto err_pci_reg;
8480 }
8481
8482 pci_enable_pcie_error_reporting(pdev);
8483 pci_set_master(pdev);
8484
8485 /* Now that we have a PCI connection, we need to do the
8486 * low level device setup. This is primarily setting up
8487 * the Admin Queue structures and then querying for the
8488 * device's current profile information.
8489 */
8490 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
8491 if (!pf) {
8492 err = -ENOMEM;
8493 goto err_pf_alloc;
8494 }
8495 pf->next_vsi = 0;
8496 pf->pdev = pdev;
8497 set_bit(__I40E_DOWN, &pf->state);
8498
8499 hw = &pf->hw;
8500 hw->back = pf;
8501 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
8502 pci_resource_len(pdev, 0));
8503 if (!hw->hw_addr) {
8504 err = -EIO;
8505 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
8506 (unsigned int)pci_resource_start(pdev, 0),
8507 (unsigned int)pci_resource_len(pdev, 0), err);
8508 goto err_ioremap;
8509 }
8510 hw->vendor_id = pdev->vendor;
8511 hw->device_id = pdev->device;
8512 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
8513 hw->subsystem_vendor_id = pdev->subsystem_vendor;
8514 hw->subsystem_device_id = pdev->subsystem_device;
8515 hw->bus.device = PCI_SLOT(pdev->devfn);
8516 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008517 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008518
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008519 /* do a special CORER for clearing PXE mode once at init */
8520 if (hw->revision_id == 0 &&
8521 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
8522 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
8523 i40e_flush(hw);
8524 msleep(200);
8525 pf->corer_count++;
8526
8527 i40e_clear_pxe_mode(hw);
8528 }
8529
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008530 /* Reset here to make sure all is clean and to define PF 'n' */
8531 err = i40e_pf_reset(hw);
8532 if (err) {
8533 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
8534 goto err_pf_reset;
8535 }
8536 pf->pfr_count++;
8537
8538 hw->aq.num_arq_entries = I40E_AQ_LEN;
8539 hw->aq.num_asq_entries = I40E_AQ_LEN;
8540 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8541 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8542 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
8543 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
8544 "%s-pf%d:misc",
8545 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
8546
8547 err = i40e_init_shared_code(hw);
8548 if (err) {
8549 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
8550 goto err_pf_reset;
8551 }
8552
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008553 /* set up a default setting for link flow control */
8554 pf->hw.fc.requested_mode = I40E_FC_NONE;
8555
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008556 err = i40e_init_adminq(hw);
8557 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
8558 if (err) {
8559 dev_info(&pdev->dev,
8560 "init_adminq failed: %d expecting API %02x.%02x\n",
8561 err,
8562 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
8563 goto err_pf_reset;
8564 }
8565
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008566 i40e_verify_eeprom(pf);
8567
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +00008568 /* Rev 0 hardware was never productized */
8569 if (hw->revision_id < 1)
8570 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");
8571
Shannon Nelson6ff4ef82013-12-21 05:44:49 +00008572 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008573 err = i40e_get_capabilities(pf);
8574 if (err)
8575 goto err_adminq_setup;
8576
8577 err = i40e_sw_init(pf);
8578 if (err) {
8579 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
8580 goto err_sw_init;
8581 }
8582
8583 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
8584 hw->func_caps.num_rx_qp,
8585 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
8586 if (err) {
8587 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
8588 goto err_init_lan_hmc;
8589 }
8590
8591 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
8592 if (err) {
8593 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
8594 err = -ENOENT;
8595 goto err_configure_lan_hmc;
8596 }
8597
8598 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00008599 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008600 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
8601 err = -EIO;
8602 goto err_mac_addr;
8603 }
8604 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +00008605 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008606
8607 pci_set_drvdata(pdev, pf);
8608 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008609#ifdef CONFIG_I40E_DCB
8610 err = i40e_init_pf_dcb(pf);
8611 if (err) {
8612 dev_info(&pdev->dev, "init_pf_dcb failed: %d\n", err);
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008613 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Neerav Parikh014269f2014-04-01 07:11:48 +00008614 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008615 }
8616#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008617
8618 /* set up periodic task facility */
8619 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
8620 pf->service_timer_period = HZ;
8621
8622 INIT_WORK(&pf->service_task, i40e_service_task);
8623 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
8624 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
8625 pf->link_check_timeout = jiffies;
8626
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008627 /* WoL defaults to disabled */
8628 pf->wol_en = false;
8629 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
8630
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008631 /* set up the main switch operations */
8632 i40e_determine_queue_usage(pf);
8633 i40e_init_interrupt_scheme(pf);
8634
Mitch Williams505682c2014-05-20 08:01:37 +00008635 /* The number of VSIs reported by the FW is the minimum guaranteed
8636 * to us; HW supports far more and we share the remaining pool with
8637 * the other PFs. We allocate space for more than the guarantee with
8638 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008639 */
Mitch Williams505682c2014-05-20 08:01:37 +00008640 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
8641 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
8642 else
8643 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
8644
8645 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
8646 len = sizeof(struct i40e_vsi *) * pf->num_alloc_vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008647 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00008648 if (!pf->vsi) {
8649 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008650 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00008651 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008652
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008653 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008654 if (err) {
8655 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
8656 goto err_vsis;
8657 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008658 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +00008659 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008660 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
8661 i40e_vsi_open(pf->vsi[i]);
8662 break;
8663 }
8664 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008665
8666 /* The main driver is (mostly) up and happy. We need to set this state
8667 * before setting up the misc vector or we get a race and the vector
8668 * ends up disabled forever.
8669 */
8670 clear_bit(__I40E_DOWN, &pf->state);
8671
8672 /* In case of MSIX we are going to setup the misc vector right here
8673 * to handle admin queue events etc. In case of legacy and MSI
8674 * the misc functionality and queue processing is combined in
8675 * the same vector and that gets setup at open.
8676 */
8677 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8678 err = i40e_setup_misc_vector(pf);
8679 if (err) {
8680 dev_info(&pdev->dev,
8681 "setup of misc vector failed: %d\n", err);
8682 goto err_vsis;
8683 }
8684 }
8685
Greg Rosedf805f62014-04-04 04:43:16 +00008686#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008687 /* prep for VF support */
8688 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008689 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8690 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008691 u32 val;
8692
8693 /* disable link interrupts for VFs */
8694 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
8695 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
8696 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
8697 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -08008698
8699 if (pci_num_vf(pdev)) {
8700 dev_info(&pdev->dev,
8701 "Active VFs found, allocating resources.\n");
8702 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
8703 if (err)
8704 dev_info(&pdev->dev,
8705 "Error %d allocating resources for existing VFs\n",
8706 err);
8707 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008708 }
Greg Rosedf805f62014-04-04 04:43:16 +00008709#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008710
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008711 pfs_found++;
8712
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008713 i40e_dbg_pf_init(pf);
8714
8715 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00008716 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008717
8718 /* since everything's happy, start the service_task timer */
8719 mod_timer(&pf->service_timer,
8720 round_jiffies(jiffies + pf->service_timer_period));
8721
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008722 /* Get the negotiated link width and speed from PCI config space */
8723 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
8724
8725 i40e_set_pci_config_data(hw, link_status);
8726
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008727 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008728 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
8729 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
8730 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
8731 "Unknown"),
8732 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
8733 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
8734 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
8735 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
8736 "Unknown"));
8737
8738 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
8739 hw->bus.speed < i40e_bus_speed_8000) {
8740 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
8741 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
8742 }
8743
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008744 /* print a string summarizing features */
8745 i40e_print_features(pf);
8746
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008747 return 0;
8748
8749 /* Unwind what we've done if something failed in the setup */
8750err_vsis:
8751 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008752 i40e_clear_interrupt_scheme(pf);
8753 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +00008754err_switch_setup:
8755 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008756 del_timer_sync(&pf->service_timer);
8757err_mac_addr:
8758err_configure_lan_hmc:
8759 (void)i40e_shutdown_lan_hmc(hw);
8760err_init_lan_hmc:
8761 kfree(pf->qp_pile);
8762 kfree(pf->irq_pile);
8763err_sw_init:
8764err_adminq_setup:
8765 (void)i40e_shutdown_adminq(hw);
8766err_pf_reset:
8767 iounmap(hw->hw_addr);
8768err_ioremap:
8769 kfree(pf);
8770err_pf_alloc:
8771 pci_disable_pcie_error_reporting(pdev);
8772 pci_release_selected_regions(pdev,
8773 pci_select_bars(pdev, IORESOURCE_MEM));
8774err_pci_reg:
8775err_dma:
8776 pci_disable_device(pdev);
8777 return err;
8778}
8779
8780/**
8781 * i40e_remove - Device removal routine
8782 * @pdev: PCI device information struct
8783 *
8784 * i40e_remove is called by the PCI subsystem to alert the driver
8785 * that is should release a PCI device. This could be caused by a
8786 * Hot-Plug event, or because the driver is going to be removed from
8787 * memory.
8788 **/
8789static void i40e_remove(struct pci_dev *pdev)
8790{
8791 struct i40e_pf *pf = pci_get_drvdata(pdev);
8792 i40e_status ret_code;
8793 u32 reg;
8794 int i;
8795
8796 i40e_dbg_pf_exit(pf);
8797
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008798 i40e_ptp_stop(pf);
8799
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008800 /* no more scheduling of any task */
8801 set_bit(__I40E_DOWN, &pf->state);
8802 del_timer_sync(&pf->service_timer);
8803 cancel_work_sync(&pf->service_task);
8804
Mitch Williamseb2d80b2014-02-13 03:48:48 -08008805 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
8806 i40e_free_vfs(pf);
8807 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
8808 }
8809
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008810 i40e_fdir_teardown(pf);
8811
8812 /* If there is a switch structure or any orphans, remove them.
8813 * This will leave only the PF's VSI remaining.
8814 */
8815 for (i = 0; i < I40E_MAX_VEB; i++) {
8816 if (!pf->veb[i])
8817 continue;
8818
8819 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
8820 pf->veb[i]->uplink_seid == 0)
8821 i40e_switch_branch_release(pf->veb[i]);
8822 }
8823
8824 /* Now we can shutdown the PF's VSI, just before we kill
8825 * adminq and hmc.
8826 */
8827 if (pf->vsi[pf->lan_vsi])
8828 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
8829
8830 i40e_stop_misc_vector(pf);
8831 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8832 synchronize_irq(pf->msix_entries[0].vector);
8833 free_irq(pf->msix_entries[0].vector, pf);
8834 }
8835
8836 /* shutdown and destroy the HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00008837 if (pf->hw.hmc.hmc_obj) {
8838 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
8839 if (ret_code)
8840 dev_warn(&pdev->dev,
8841 "Failed to destroy the HMC resources: %d\n",
8842 ret_code);
8843 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008844
8845 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008846 ret_code = i40e_shutdown_adminq(&pf->hw);
8847 if (ret_code)
8848 dev_warn(&pdev->dev,
8849 "Failed to destroy the Admin Queue resources: %d\n",
8850 ret_code);
8851
8852 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
8853 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00008854 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008855 if (pf->vsi[i]) {
8856 i40e_vsi_clear_rings(pf->vsi[i]);
8857 i40e_vsi_clear(pf->vsi[i]);
8858 pf->vsi[i] = NULL;
8859 }
8860 }
8861
8862 for (i = 0; i < I40E_MAX_VEB; i++) {
8863 kfree(pf->veb[i]);
8864 pf->veb[i] = NULL;
8865 }
8866
8867 kfree(pf->qp_pile);
8868 kfree(pf->irq_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008869 kfree(pf->vsi);
8870
8871 /* force a PF reset to clean anything leftover */
8872 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
8873 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
8874 i40e_flush(&pf->hw);
8875
8876 iounmap(pf->hw.hw_addr);
8877 kfree(pf);
8878 pci_release_selected_regions(pdev,
8879 pci_select_bars(pdev, IORESOURCE_MEM));
8880
8881 pci_disable_pcie_error_reporting(pdev);
8882 pci_disable_device(pdev);
8883}
8884
8885/**
8886 * i40e_pci_error_detected - warning that something funky happened in PCI land
8887 * @pdev: PCI device information struct
8888 *
8889 * Called to warn that something happened and the error handling steps
8890 * are in progress. Allows the driver to quiesce things, be ready for
8891 * remediation.
8892 **/
8893static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
8894 enum pci_channel_state error)
8895{
8896 struct i40e_pf *pf = pci_get_drvdata(pdev);
8897
8898 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
8899
8900 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008901 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
8902 rtnl_lock();
8903 i40e_prep_for_reset(pf);
8904 rtnl_unlock();
8905 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008906
8907 /* Request a slot reset */
8908 return PCI_ERS_RESULT_NEED_RESET;
8909}
8910
8911/**
8912 * i40e_pci_error_slot_reset - a PCI slot reset just happened
8913 * @pdev: PCI device information struct
8914 *
8915 * Called to find if the driver can work with the device now that
8916 * the pci slot has been reset. If a basic connection seems good
8917 * (registers are readable and have sane content) then return a
8918 * happy little PCI_ERS_RESULT_xxx.
8919 **/
8920static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
8921{
8922 struct i40e_pf *pf = pci_get_drvdata(pdev);
8923 pci_ers_result_t result;
8924 int err;
8925 u32 reg;
8926
8927 dev_info(&pdev->dev, "%s\n", __func__);
8928 if (pci_enable_device_mem(pdev)) {
8929 dev_info(&pdev->dev,
8930 "Cannot re-enable PCI device after reset.\n");
8931 result = PCI_ERS_RESULT_DISCONNECT;
8932 } else {
8933 pci_set_master(pdev);
8934 pci_restore_state(pdev);
8935 pci_save_state(pdev);
8936 pci_wake_from_d3(pdev, false);
8937
8938 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8939 if (reg == 0)
8940 result = PCI_ERS_RESULT_RECOVERED;
8941 else
8942 result = PCI_ERS_RESULT_DISCONNECT;
8943 }
8944
8945 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8946 if (err) {
8947 dev_info(&pdev->dev,
8948 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8949 err);
8950 /* non-fatal, continue */
8951 }
8952
8953 return result;
8954}
8955
8956/**
8957 * i40e_pci_error_resume - restart operations after PCI error recovery
8958 * @pdev: PCI device information struct
8959 *
8960 * Called to allow the driver to bring things back up after PCI error
8961 * and/or reset recovery has finished.
8962 **/
8963static void i40e_pci_error_resume(struct pci_dev *pdev)
8964{
8965 struct i40e_pf *pf = pci_get_drvdata(pdev);
8966
8967 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008968 if (test_bit(__I40E_SUSPENDED, &pf->state))
8969 return;
8970
8971 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008972 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008973 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008974}
8975
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008976/**
8977 * i40e_shutdown - PCI callback for shutting down
8978 * @pdev: PCI device information struct
8979 **/
8980static void i40e_shutdown(struct pci_dev *pdev)
8981{
8982 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008983 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008984
8985 set_bit(__I40E_SUSPENDED, &pf->state);
8986 set_bit(__I40E_DOWN, &pf->state);
8987 rtnl_lock();
8988 i40e_prep_for_reset(pf);
8989 rtnl_unlock();
8990
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008991 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8992 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8993
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008994 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008995 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008996 pci_set_power_state(pdev, PCI_D3hot);
8997 }
8998}
8999
9000#ifdef CONFIG_PM
9001/**
9002 * i40e_suspend - PCI callback for moving to D3
9003 * @pdev: PCI device information struct
9004 **/
9005static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
9006{
9007 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00009008 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00009009
9010 set_bit(__I40E_SUSPENDED, &pf->state);
9011 set_bit(__I40E_DOWN, &pf->state);
9012 rtnl_lock();
9013 i40e_prep_for_reset(pf);
9014 rtnl_unlock();
9015
Shannon Nelson8e2773a2013-11-28 06:39:22 +00009016 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
9017 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
9018
9019 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00009020 pci_set_power_state(pdev, PCI_D3hot);
9021
9022 return 0;
9023}
9024
9025/**
9026 * i40e_resume - PCI callback for waking up from D3
9027 * @pdev: PCI device information struct
9028 **/
9029static int i40e_resume(struct pci_dev *pdev)
9030{
9031 struct i40e_pf *pf = pci_get_drvdata(pdev);
9032 u32 err;
9033
9034 pci_set_power_state(pdev, PCI_D0);
9035 pci_restore_state(pdev);
9036 /* pci_restore_state() clears dev->state_saves, so
9037 * call pci_save_state() again to restore it.
9038 */
9039 pci_save_state(pdev);
9040
9041 err = pci_enable_device_mem(pdev);
9042 if (err) {
9043 dev_err(&pdev->dev,
9044 "%s: Cannot enable PCI device from suspend\n",
9045 __func__);
9046 return err;
9047 }
9048 pci_set_master(pdev);
9049
9050 /* no wakeup events while running */
9051 pci_wake_from_d3(pdev, false);
9052
9053 /* handling the reset will rebuild the device state */
9054 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
9055 clear_bit(__I40E_DOWN, &pf->state);
9056 rtnl_lock();
9057 i40e_reset_and_rebuild(pf, false);
9058 rtnl_unlock();
9059 }
9060
9061 return 0;
9062}
9063
9064#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009065static const struct pci_error_handlers i40e_err_handler = {
9066 .error_detected = i40e_pci_error_detected,
9067 .slot_reset = i40e_pci_error_slot_reset,
9068 .resume = i40e_pci_error_resume,
9069};
9070
9071static struct pci_driver i40e_driver = {
9072 .name = i40e_driver_name,
9073 .id_table = i40e_pci_tbl,
9074 .probe = i40e_probe,
9075 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +00009076#ifdef CONFIG_PM
9077 .suspend = i40e_suspend,
9078 .resume = i40e_resume,
9079#endif
9080 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009081 .err_handler = &i40e_err_handler,
9082 .sriov_configure = i40e_pci_sriov_configure,
9083};
9084
9085/**
9086 * i40e_init_module - Driver registration routine
9087 *
9088 * i40e_init_module is the first routine called when the driver is
9089 * loaded. All it does is register with the PCI subsystem.
9090 **/
9091static int __init i40e_init_module(void)
9092{
9093 pr_info("%s: %s - version %s\n", i40e_driver_name,
9094 i40e_driver_string, i40e_driver_version_str);
9095 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
9096 i40e_dbg_init();
9097 return pci_register_driver(&i40e_driver);
9098}
9099module_init(i40e_init_module);
9100
9101/**
9102 * i40e_exit_module - Driver exit cleanup routine
9103 *
9104 * i40e_exit_module is called just before the driver is removed
9105 * from memory.
9106 **/
9107static void __exit i40e_exit_module(void)
9108{
9109 pci_unregister_driver(&i40e_driver);
9110 i40e_dbg_exit();
9111}
9112module_exit(i40e_exit_module);