blob: 676bebb97f6eb823f026d78d62632f71e8d0efd9 [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
Shannon Nelsone8607ef2014-05-20 08:01:47 +000042#define DRV_VERSION_BUILD 7
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);
Shannon Nelson532d2832014-04-23 04:50:09 +0000855
856 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
1016 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1017 nsd->tx_lpi_status =
1018 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1019 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1020 nsd->rx_lpi_status =
1021 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1022 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1023 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1024 pf->stat_offsets_loaded,
1025 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1026 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1027 pf->stat_offsets_loaded,
1028 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1029
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001030 pf->stat_offsets_loaded = true;
1031}
1032
1033/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001034 * i40e_update_stats - Update the various statistics counters.
1035 * @vsi: the VSI to be updated
1036 *
1037 * Update the various stats for this VSI and its related entities.
1038 **/
1039void i40e_update_stats(struct i40e_vsi *vsi)
1040{
1041 struct i40e_pf *pf = vsi->back;
1042
1043 if (vsi == pf->vsi[pf->lan_vsi])
1044 i40e_update_pf_stats(pf);
1045
1046 i40e_update_vsi_stats(vsi);
1047}
1048
1049/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001050 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1051 * @vsi: the VSI to be searched
1052 * @macaddr: the MAC address
1053 * @vlan: the vlan
1054 * @is_vf: make sure its a vf filter, else doesn't matter
1055 * @is_netdev: make sure its a netdev filter, else doesn't matter
1056 *
1057 * Returns ptr to the filter object or NULL
1058 **/
1059static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1060 u8 *macaddr, s16 vlan,
1061 bool is_vf, bool is_netdev)
1062{
1063 struct i40e_mac_filter *f;
1064
1065 if (!vsi || !macaddr)
1066 return NULL;
1067
1068 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1069 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1070 (vlan == f->vlan) &&
1071 (!is_vf || f->is_vf) &&
1072 (!is_netdev || f->is_netdev))
1073 return f;
1074 }
1075 return NULL;
1076}
1077
1078/**
1079 * i40e_find_mac - Find a mac addr in the macvlan filters list
1080 * @vsi: the VSI to be searched
1081 * @macaddr: the MAC address we are searching for
1082 * @is_vf: make sure its a vf filter, else doesn't matter
1083 * @is_netdev: make sure its a netdev filter, else doesn't matter
1084 *
1085 * Returns the first filter with the provided MAC address or NULL if
1086 * MAC address was not found
1087 **/
1088struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1089 bool is_vf, bool is_netdev)
1090{
1091 struct i40e_mac_filter *f;
1092
1093 if (!vsi || !macaddr)
1094 return NULL;
1095
1096 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1097 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1098 (!is_vf || f->is_vf) &&
1099 (!is_netdev || f->is_netdev))
1100 return f;
1101 }
1102 return NULL;
1103}
1104
1105/**
1106 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1107 * @vsi: the VSI to be searched
1108 *
1109 * Returns true if VSI is in vlan mode or false otherwise
1110 **/
1111bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1112{
1113 struct i40e_mac_filter *f;
1114
1115 /* Only -1 for all the filters denotes not in vlan mode
1116 * so we have to go through all the list in order to make sure
1117 */
1118 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1119 if (f->vlan >= 0)
1120 return true;
1121 }
1122
1123 return false;
1124}
1125
1126/**
1127 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1128 * @vsi: the VSI to be searched
1129 * @macaddr: the mac address to be filtered
1130 * @is_vf: true if it is a vf
1131 * @is_netdev: true if it is a netdev
1132 *
1133 * Goes through all the macvlan filters and adds a
1134 * macvlan filter for each unique vlan that already exists
1135 *
1136 * Returns first filter found on success, else NULL
1137 **/
1138struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1139 bool is_vf, bool is_netdev)
1140{
1141 struct i40e_mac_filter *f;
1142
1143 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1144 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1145 is_vf, is_netdev)) {
1146 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001147 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001148 return NULL;
1149 }
1150 }
1151
1152 return list_first_entry_or_null(&vsi->mac_filter_list,
1153 struct i40e_mac_filter, list);
1154}
1155
1156/**
1157 * i40e_add_filter - Add a mac/vlan filter to the VSI
1158 * @vsi: the VSI to be searched
1159 * @macaddr: the MAC address
1160 * @vlan: the vlan
1161 * @is_vf: make sure its a vf filter, else doesn't matter
1162 * @is_netdev: make sure its a netdev filter, else doesn't matter
1163 *
1164 * Returns ptr to the filter object or NULL when no memory available.
1165 **/
1166struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1167 u8 *macaddr, s16 vlan,
1168 bool is_vf, bool is_netdev)
1169{
1170 struct i40e_mac_filter *f;
1171
1172 if (!vsi || !macaddr)
1173 return NULL;
1174
1175 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1176 if (!f) {
1177 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1178 if (!f)
1179 goto add_filter_out;
1180
1181 memcpy(f->macaddr, macaddr, ETH_ALEN);
1182 f->vlan = vlan;
1183 f->changed = true;
1184
1185 INIT_LIST_HEAD(&f->list);
1186 list_add(&f->list, &vsi->mac_filter_list);
1187 }
1188
1189 /* increment counter and add a new flag if needed */
1190 if (is_vf) {
1191 if (!f->is_vf) {
1192 f->is_vf = true;
1193 f->counter++;
1194 }
1195 } else if (is_netdev) {
1196 if (!f->is_netdev) {
1197 f->is_netdev = true;
1198 f->counter++;
1199 }
1200 } else {
1201 f->counter++;
1202 }
1203
1204 /* changed tells sync_filters_subtask to
1205 * push the filter down to the firmware
1206 */
1207 if (f->changed) {
1208 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1209 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1210 }
1211
1212add_filter_out:
1213 return f;
1214}
1215
1216/**
1217 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1218 * @vsi: the VSI to be searched
1219 * @macaddr: the MAC address
1220 * @vlan: the vlan
1221 * @is_vf: make sure it's a vf filter, else doesn't matter
1222 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1223 **/
1224void i40e_del_filter(struct i40e_vsi *vsi,
1225 u8 *macaddr, s16 vlan,
1226 bool is_vf, bool is_netdev)
1227{
1228 struct i40e_mac_filter *f;
1229
1230 if (!vsi || !macaddr)
1231 return;
1232
1233 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1234 if (!f || f->counter == 0)
1235 return;
1236
1237 if (is_vf) {
1238 if (f->is_vf) {
1239 f->is_vf = false;
1240 f->counter--;
1241 }
1242 } else if (is_netdev) {
1243 if (f->is_netdev) {
1244 f->is_netdev = false;
1245 f->counter--;
1246 }
1247 } else {
1248 /* make sure we don't remove a filter in use by vf or netdev */
1249 int min_f = 0;
1250 min_f += (f->is_vf ? 1 : 0);
1251 min_f += (f->is_netdev ? 1 : 0);
1252
1253 if (f->counter > min_f)
1254 f->counter--;
1255 }
1256
1257 /* counter == 0 tells sync_filters_subtask to
1258 * remove the filter from the firmware's list
1259 */
1260 if (f->counter == 0) {
1261 f->changed = true;
1262 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1263 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1264 }
1265}
1266
1267/**
1268 * i40e_set_mac - NDO callback to set mac address
1269 * @netdev: network interface device structure
1270 * @p: pointer to an address structure
1271 *
1272 * Returns 0 on success, negative on failure
1273 **/
1274static int i40e_set_mac(struct net_device *netdev, void *p)
1275{
1276 struct i40e_netdev_priv *np = netdev_priv(netdev);
1277 struct i40e_vsi *vsi = np->vsi;
1278 struct sockaddr *addr = p;
1279 struct i40e_mac_filter *f;
1280
1281 if (!is_valid_ether_addr(addr->sa_data))
1282 return -EADDRNOTAVAIL;
1283
1284 netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
1285
1286 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
1287 return 0;
1288
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001289 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1290 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1291 return -EADDRNOTAVAIL;
1292
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001293 if (vsi->type == I40E_VSI_MAIN) {
1294 i40e_status ret;
1295 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1296 I40E_AQC_WRITE_TYPE_LAA_ONLY,
1297 addr->sa_data, NULL);
1298 if (ret) {
1299 netdev_info(netdev,
1300 "Addr change for Main VSI failed: %d\n",
1301 ret);
1302 return -EADDRNOTAVAIL;
1303 }
1304
1305 memcpy(vsi->back->hw.mac.addr, addr->sa_data, netdev->addr_len);
1306 }
1307
1308 /* In order to be sure to not drop any packets, add the new address
1309 * then delete the old one.
1310 */
1311 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
1312 if (!f)
1313 return -ENOMEM;
1314
1315 i40e_sync_vsi_filters(vsi);
1316 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
1317 i40e_sync_vsi_filters(vsi);
1318
1319 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1320
1321 return 0;
1322}
1323
1324/**
1325 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1326 * @vsi: the VSI being setup
1327 * @ctxt: VSI context structure
1328 * @enabled_tc: Enabled TCs bitmap
1329 * @is_add: True if called before Add VSI
1330 *
1331 * Setup VSI queue mapping for enabled traffic classes.
1332 **/
1333static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1334 struct i40e_vsi_context *ctxt,
1335 u8 enabled_tc,
1336 bool is_add)
1337{
1338 struct i40e_pf *pf = vsi->back;
1339 u16 sections = 0;
1340 u8 netdev_tc = 0;
1341 u16 numtc = 0;
1342 u16 qcount;
1343 u8 offset;
1344 u16 qmap;
1345 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001346 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001347
1348 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1349 offset = 0;
1350
1351 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1352 /* Find numtc from enabled TC bitmap */
1353 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1354 if (enabled_tc & (1 << i)) /* TC is enabled */
1355 numtc++;
1356 }
1357 if (!numtc) {
1358 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1359 numtc = 1;
1360 }
1361 } else {
1362 /* At least TC0 is enabled in case of non-DCB case */
1363 numtc = 1;
1364 }
1365
1366 vsi->tc_config.numtc = numtc;
1367 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001368 /* Number of queues per enabled TC */
Anjali Singhai Jaineb051af2014-05-20 08:01:46 +00001369 num_tc_qps = vsi->alloc_queue_pairs/numtc;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001370 num_tc_qps = min_t(int, num_tc_qps, I40E_MAX_QUEUES_PER_TC);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001371
1372 /* Setup queue offset/count for all TCs for given VSI */
1373 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1374 /* See if the given TC is enabled for the given VSI */
1375 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1376 int pow, num_qps;
1377
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001378 switch (vsi->type) {
1379 case I40E_VSI_MAIN:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001380 qcount = min_t(int, pf->rss_size, num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001381 break;
1382 case I40E_VSI_FDIR:
1383 case I40E_VSI_SRIOV:
1384 case I40E_VSI_VMDQ2:
1385 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001386 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001387 WARN_ON(i != 0);
1388 break;
1389 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001390 vsi->tc_config.tc_info[i].qoffset = offset;
1391 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001392
1393 /* find the power-of-2 of the number of queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001394 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001395 pow = 0;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001396 while (num_qps && ((1 << pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001397 pow++;
1398 num_qps >>= 1;
1399 }
1400
1401 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1402 qmap =
1403 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1404 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1405
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001406 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001407 } else {
1408 /* TC is not enabled so set the offset to
1409 * default queue and allocate one queue
1410 * for the given TC.
1411 */
1412 vsi->tc_config.tc_info[i].qoffset = 0;
1413 vsi->tc_config.tc_info[i].qcount = 1;
1414 vsi->tc_config.tc_info[i].netdev_tc = 0;
1415
1416 qmap = 0;
1417 }
1418 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1419 }
1420
1421 /* Set actual Tx/Rx queue pairs */
1422 vsi->num_queue_pairs = offset;
1423
1424 /* Scheduler section valid can only be set for ADD VSI */
1425 if (is_add) {
1426 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1427
1428 ctxt->info.up_enable_bits = enabled_tc;
1429 }
1430 if (vsi->type == I40E_VSI_SRIOV) {
1431 ctxt->info.mapping_flags |=
1432 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1433 for (i = 0; i < vsi->num_queue_pairs; i++)
1434 ctxt->info.queue_mapping[i] =
1435 cpu_to_le16(vsi->base_queue + i);
1436 } else {
1437 ctxt->info.mapping_flags |=
1438 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1439 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1440 }
1441 ctxt->info.valid_sections |= cpu_to_le16(sections);
1442}
1443
1444/**
1445 * i40e_set_rx_mode - NDO callback to set the netdev filters
1446 * @netdev: network interface device structure
1447 **/
1448static void i40e_set_rx_mode(struct net_device *netdev)
1449{
1450 struct i40e_netdev_priv *np = netdev_priv(netdev);
1451 struct i40e_mac_filter *f, *ftmp;
1452 struct i40e_vsi *vsi = np->vsi;
1453 struct netdev_hw_addr *uca;
1454 struct netdev_hw_addr *mca;
1455 struct netdev_hw_addr *ha;
1456
1457 /* add addr if not already in the filter list */
1458 netdev_for_each_uc_addr(uca, netdev) {
1459 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1460 if (i40e_is_vsi_in_vlan(vsi))
1461 i40e_put_mac_in_vlan(vsi, uca->addr,
1462 false, true);
1463 else
1464 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1465 false, true);
1466 }
1467 }
1468
1469 netdev_for_each_mc_addr(mca, netdev) {
1470 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1471 if (i40e_is_vsi_in_vlan(vsi))
1472 i40e_put_mac_in_vlan(vsi, mca->addr,
1473 false, true);
1474 else
1475 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1476 false, true);
1477 }
1478 }
1479
1480 /* remove filter if not in netdev list */
1481 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1482 bool found = false;
1483
1484 if (!f->is_netdev)
1485 continue;
1486
1487 if (is_multicast_ether_addr(f->macaddr)) {
1488 netdev_for_each_mc_addr(mca, netdev) {
1489 if (ether_addr_equal(mca->addr, f->macaddr)) {
1490 found = true;
1491 break;
1492 }
1493 }
1494 } else {
1495 netdev_for_each_uc_addr(uca, netdev) {
1496 if (ether_addr_equal(uca->addr, f->macaddr)) {
1497 found = true;
1498 break;
1499 }
1500 }
1501
1502 for_each_dev_addr(netdev, ha) {
1503 if (ether_addr_equal(ha->addr, f->macaddr)) {
1504 found = true;
1505 break;
1506 }
1507 }
1508 }
1509 if (!found)
1510 i40e_del_filter(
1511 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1512 }
1513
1514 /* check for other flag changes */
1515 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1516 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1517 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1518 }
1519}
1520
1521/**
1522 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1523 * @vsi: ptr to the VSI
1524 *
1525 * Push any outstanding VSI filter changes through the AdminQ.
1526 *
1527 * Returns 0 or error value
1528 **/
1529int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1530{
1531 struct i40e_mac_filter *f, *ftmp;
1532 bool promisc_forced_on = false;
1533 bool add_happened = false;
1534 int filter_list_len = 0;
1535 u32 changed_flags = 0;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001536 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001537 struct i40e_pf *pf;
1538 int num_add = 0;
1539 int num_del = 0;
1540 u16 cmd_flags;
1541
1542 /* empty array typed pointers, kcalloc later */
1543 struct i40e_aqc_add_macvlan_element_data *add_list;
1544 struct i40e_aqc_remove_macvlan_element_data *del_list;
1545
1546 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1547 usleep_range(1000, 2000);
1548 pf = vsi->back;
1549
1550 if (vsi->netdev) {
1551 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1552 vsi->current_netdev_flags = vsi->netdev->flags;
1553 }
1554
1555 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1556 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1557
1558 filter_list_len = pf->hw.aq.asq_buf_size /
1559 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1560 del_list = kcalloc(filter_list_len,
1561 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1562 GFP_KERNEL);
1563 if (!del_list)
1564 return -ENOMEM;
1565
1566 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1567 if (!f->changed)
1568 continue;
1569
1570 if (f->counter != 0)
1571 continue;
1572 f->changed = false;
1573 cmd_flags = 0;
1574
1575 /* add to delete list */
1576 memcpy(del_list[num_del].mac_addr,
1577 f->macaddr, ETH_ALEN);
1578 del_list[num_del].vlan_tag =
1579 cpu_to_le16((u16)(f->vlan ==
1580 I40E_VLAN_ANY ? 0 : f->vlan));
1581
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001582 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1583 del_list[num_del].flags = cmd_flags;
1584 num_del++;
1585
1586 /* unlink from filter list */
1587 list_del(&f->list);
1588 kfree(f);
1589
1590 /* flush a full buffer */
1591 if (num_del == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001592 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001593 vsi->seid, del_list, num_del,
1594 NULL);
1595 num_del = 0;
1596 memset(del_list, 0, sizeof(*del_list));
1597
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001598 if (aq_ret &&
1599 pf->hw.aq.asq_last_status !=
1600 I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001601 dev_info(&pf->pdev->dev,
1602 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001603 aq_ret,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001604 pf->hw.aq.asq_last_status);
1605 }
1606 }
1607 if (num_del) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001608 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001609 del_list, num_del, NULL);
1610 num_del = 0;
1611
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001612 if (aq_ret &&
1613 pf->hw.aq.asq_last_status != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001614 dev_info(&pf->pdev->dev,
1615 "ignoring delete macvlan error, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001616 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001617 }
1618
1619 kfree(del_list);
1620 del_list = NULL;
1621
1622 /* do all the adds now */
1623 filter_list_len = pf->hw.aq.asq_buf_size /
1624 sizeof(struct i40e_aqc_add_macvlan_element_data),
1625 add_list = kcalloc(filter_list_len,
1626 sizeof(struct i40e_aqc_add_macvlan_element_data),
1627 GFP_KERNEL);
1628 if (!add_list)
1629 return -ENOMEM;
1630
1631 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1632 if (!f->changed)
1633 continue;
1634
1635 if (f->counter == 0)
1636 continue;
1637 f->changed = false;
1638 add_happened = true;
1639 cmd_flags = 0;
1640
1641 /* add to add array */
1642 memcpy(add_list[num_add].mac_addr,
1643 f->macaddr, ETH_ALEN);
1644 add_list[num_add].vlan_tag =
1645 cpu_to_le16(
1646 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1647 add_list[num_add].queue_number = 0;
1648
1649 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001650 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1651 num_add++;
1652
1653 /* flush a full buffer */
1654 if (num_add == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001655 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1656 add_list, num_add,
1657 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001658 num_add = 0;
1659
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001660 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001661 break;
1662 memset(add_list, 0, sizeof(*add_list));
1663 }
1664 }
1665 if (num_add) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001666 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1667 add_list, num_add, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001668 num_add = 0;
1669 }
1670 kfree(add_list);
1671 add_list = NULL;
1672
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001673 if (add_happened && (!aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001674 /* do nothing */;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001675 } else if (add_happened && (aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001676 dev_info(&pf->pdev->dev,
1677 "add filter failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001678 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001679 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1680 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1681 &vsi->state)) {
1682 promisc_forced_on = true;
1683 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1684 &vsi->state);
1685 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1686 }
1687 }
1688 }
1689
1690 /* check for changes in promiscuous modes */
1691 if (changed_flags & IFF_ALLMULTI) {
1692 bool cur_multipromisc;
1693 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001694 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1695 vsi->seid,
1696 cur_multipromisc,
1697 NULL);
1698 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001699 dev_info(&pf->pdev->dev,
1700 "set multi promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001701 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001702 }
1703 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1704 bool cur_promisc;
1705 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1706 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1707 &vsi->state));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001708 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1709 vsi->seid,
1710 cur_promisc, NULL);
1711 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001712 dev_info(&pf->pdev->dev,
1713 "set uni promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001714 aq_ret, pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00001715 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1716 vsi->seid,
1717 cur_promisc, NULL);
1718 if (aq_ret)
1719 dev_info(&pf->pdev->dev,
1720 "set brdcast promisc failed, err %d, aq_err %d\n",
1721 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001722 }
1723
1724 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1725 return 0;
1726}
1727
1728/**
1729 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1730 * @pf: board private structure
1731 **/
1732static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1733{
1734 int v;
1735
1736 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1737 return;
1738 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1739
Mitch Williams505682c2014-05-20 08:01:37 +00001740 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001741 if (pf->vsi[v] &&
1742 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1743 i40e_sync_vsi_filters(pf->vsi[v]);
1744 }
1745}
1746
1747/**
1748 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1749 * @netdev: network interface device structure
1750 * @new_mtu: new value for maximum frame size
1751 *
1752 * Returns 0 on success, negative on failure
1753 **/
1754static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1755{
1756 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00001757 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001758 struct i40e_vsi *vsi = np->vsi;
1759
1760 /* MTU < 68 is an error and causes problems on some kernels */
1761 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1762 return -EINVAL;
1763
1764 netdev_info(netdev, "changing MTU from %d to %d\n",
1765 netdev->mtu, new_mtu);
1766 netdev->mtu = new_mtu;
1767 if (netif_running(netdev))
1768 i40e_vsi_reinit_locked(vsi);
1769
1770 return 0;
1771}
1772
1773/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001774 * i40e_ioctl - Access the hwtstamp interface
1775 * @netdev: network interface device structure
1776 * @ifr: interface request data
1777 * @cmd: ioctl command
1778 **/
1779int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1780{
1781 struct i40e_netdev_priv *np = netdev_priv(netdev);
1782 struct i40e_pf *pf = np->vsi->back;
1783
1784 switch (cmd) {
1785 case SIOCGHWTSTAMP:
1786 return i40e_ptp_get_ts_config(pf, ifr);
1787 case SIOCSHWTSTAMP:
1788 return i40e_ptp_set_ts_config(pf, ifr);
1789 default:
1790 return -EOPNOTSUPP;
1791 }
1792}
1793
1794/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001795 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1796 * @vsi: the vsi being adjusted
1797 **/
1798void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1799{
1800 struct i40e_vsi_context ctxt;
1801 i40e_status ret;
1802
1803 if ((vsi->info.valid_sections &
1804 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1805 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1806 return; /* already enabled */
1807
1808 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1809 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1810 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1811
1812 ctxt.seid = vsi->seid;
1813 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1814 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1815 if (ret) {
1816 dev_info(&vsi->back->pdev->dev,
1817 "%s: update vsi failed, aq_err=%d\n",
1818 __func__, vsi->back->hw.aq.asq_last_status);
1819 }
1820}
1821
1822/**
1823 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1824 * @vsi: the vsi being adjusted
1825 **/
1826void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
1827{
1828 struct i40e_vsi_context ctxt;
1829 i40e_status ret;
1830
1831 if ((vsi->info.valid_sections &
1832 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1833 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
1834 I40E_AQ_VSI_PVLAN_EMOD_MASK))
1835 return; /* already disabled */
1836
1837 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1838 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1839 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
1840
1841 ctxt.seid = vsi->seid;
1842 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1843 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1844 if (ret) {
1845 dev_info(&vsi->back->pdev->dev,
1846 "%s: update vsi failed, aq_err=%d\n",
1847 __func__, vsi->back->hw.aq.asq_last_status);
1848 }
1849}
1850
1851/**
1852 * i40e_vlan_rx_register - Setup or shutdown vlan offload
1853 * @netdev: network interface to be adjusted
1854 * @features: netdev features to test if VLAN offload is enabled or not
1855 **/
1856static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
1857{
1858 struct i40e_netdev_priv *np = netdev_priv(netdev);
1859 struct i40e_vsi *vsi = np->vsi;
1860
1861 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1862 i40e_vlan_stripping_enable(vsi);
1863 else
1864 i40e_vlan_stripping_disable(vsi);
1865}
1866
1867/**
1868 * i40e_vsi_add_vlan - Add vsi membership for given vlan
1869 * @vsi: the vsi being configured
1870 * @vid: vlan id to be added (0 = untagged only , -1 = any)
1871 **/
1872int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
1873{
1874 struct i40e_mac_filter *f, *add_f;
1875 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001876
1877 is_vf = (vsi->type == I40E_VSI_SRIOV);
1878 is_netdev = !!(vsi->netdev);
1879
1880 if (is_netdev) {
1881 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
1882 is_vf, is_netdev);
1883 if (!add_f) {
1884 dev_info(&vsi->back->pdev->dev,
1885 "Could not add vlan filter %d for %pM\n",
1886 vid, vsi->netdev->dev_addr);
1887 return -ENOMEM;
1888 }
1889 }
1890
1891 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1892 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1893 if (!add_f) {
1894 dev_info(&vsi->back->pdev->dev,
1895 "Could not add vlan filter %d for %pM\n",
1896 vid, f->macaddr);
1897 return -ENOMEM;
1898 }
1899 }
1900
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001901 /* Now if we add a vlan tag, make sure to check if it is the first
1902 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
1903 * with 0, so we now accept untagged and specified tagged traffic
1904 * (and not any taged and untagged)
1905 */
1906 if (vid > 0) {
1907 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
1908 I40E_VLAN_ANY,
1909 is_vf, is_netdev)) {
1910 i40e_del_filter(vsi, vsi->netdev->dev_addr,
1911 I40E_VLAN_ANY, is_vf, is_netdev);
1912 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
1913 is_vf, is_netdev);
1914 if (!add_f) {
1915 dev_info(&vsi->back->pdev->dev,
1916 "Could not add filter 0 for %pM\n",
1917 vsi->netdev->dev_addr);
1918 return -ENOMEM;
1919 }
1920 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08001921 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001922
Greg Rose8d82a7c2014-01-13 16:13:04 -08001923 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
1924 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001925 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1926 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1927 is_vf, is_netdev)) {
1928 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1929 is_vf, is_netdev);
1930 add_f = i40e_add_filter(vsi, f->macaddr,
1931 0, is_vf, is_netdev);
1932 if (!add_f) {
1933 dev_info(&vsi->back->pdev->dev,
1934 "Could not add filter 0 for %pM\n",
1935 f->macaddr);
1936 return -ENOMEM;
1937 }
1938 }
1939 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001940 }
1941
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001942 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1943 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1944 return 0;
1945
1946 return i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001947}
1948
1949/**
1950 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
1951 * @vsi: the vsi being configured
1952 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001953 *
1954 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001955 **/
1956int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
1957{
1958 struct net_device *netdev = vsi->netdev;
1959 struct i40e_mac_filter *f, *add_f;
1960 bool is_vf, is_netdev;
1961 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001962
1963 is_vf = (vsi->type == I40E_VSI_SRIOV);
1964 is_netdev = !!(netdev);
1965
1966 if (is_netdev)
1967 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
1968
1969 list_for_each_entry(f, &vsi->mac_filter_list, list)
1970 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1971
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001972 /* go through all the filters for this VSI and if there is only
1973 * vid == 0 it means there are no other filters, so vid 0 must
1974 * be replaced with -1. This signifies that we should from now
1975 * on accept any traffic (with any tag present, or untagged)
1976 */
1977 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1978 if (is_netdev) {
1979 if (f->vlan &&
1980 ether_addr_equal(netdev->dev_addr, f->macaddr))
1981 filter_count++;
1982 }
1983
1984 if (f->vlan)
1985 filter_count++;
1986 }
1987
1988 if (!filter_count && is_netdev) {
1989 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
1990 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1991 is_vf, is_netdev);
1992 if (!f) {
1993 dev_info(&vsi->back->pdev->dev,
1994 "Could not add filter %d for %pM\n",
1995 I40E_VLAN_ANY, netdev->dev_addr);
1996 return -ENOMEM;
1997 }
1998 }
1999
2000 if (!filter_count) {
2001 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2002 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2003 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2004 is_vf, is_netdev);
2005 if (!add_f) {
2006 dev_info(&vsi->back->pdev->dev,
2007 "Could not add filter %d for %pM\n",
2008 I40E_VLAN_ANY, f->macaddr);
2009 return -ENOMEM;
2010 }
2011 }
2012 }
2013
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002014 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2015 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2016 return 0;
2017
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002018 return i40e_sync_vsi_filters(vsi);
2019}
2020
2021/**
2022 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2023 * @netdev: network interface to be adjusted
2024 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002025 *
2026 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002027 **/
2028static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2029 __always_unused __be16 proto, u16 vid)
2030{
2031 struct i40e_netdev_priv *np = netdev_priv(netdev);
2032 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002033 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002034
2035 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002036 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002037
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002038 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2039
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002040 /* If the network stack called us with vid = 0 then
2041 * it is asking to receive priority tagged packets with
2042 * vlan id 0. Our HW receives them by default when configured
2043 * to receive untagged packets so there is no need to add an
2044 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002045 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002046 if (vid)
2047 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002048
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002049 if (!ret && (vid < VLAN_N_VID))
2050 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002051
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002052 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002053}
2054
2055/**
2056 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2057 * @netdev: network interface to be adjusted
2058 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002059 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002060 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002061 **/
2062static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2063 __always_unused __be16 proto, u16 vid)
2064{
2065 struct i40e_netdev_priv *np = netdev_priv(netdev);
2066 struct i40e_vsi *vsi = np->vsi;
2067
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002068 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2069
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002070 /* return code is ignored as there is nothing a user
2071 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002072 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002073 */
2074 i40e_vsi_kill_vlan(vsi, vid);
2075
2076 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002077
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002078 return 0;
2079}
2080
2081/**
2082 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2083 * @vsi: the vsi being brought back up
2084 **/
2085static void i40e_restore_vlan(struct i40e_vsi *vsi)
2086{
2087 u16 vid;
2088
2089 if (!vsi->netdev)
2090 return;
2091
2092 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2093
2094 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2095 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2096 vid);
2097}
2098
2099/**
2100 * i40e_vsi_add_pvid - Add pvid for the VSI
2101 * @vsi: the vsi being adjusted
2102 * @vid: the vlan id to set as a PVID
2103 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002104int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002105{
2106 struct i40e_vsi_context ctxt;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002107 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002108
2109 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2110 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002111 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2112 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002113 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002114
2115 ctxt.seid = vsi->seid;
2116 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002117 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2118 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002119 dev_info(&vsi->back->pdev->dev,
2120 "%s: update vsi failed, aq_err=%d\n",
2121 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002122 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002123 }
2124
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002125 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002126}
2127
2128/**
2129 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2130 * @vsi: the vsi being adjusted
2131 *
2132 * Just use the vlan_rx_register() service to put it back to normal
2133 **/
2134void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2135{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002136 i40e_vlan_stripping_disable(vsi);
2137
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002138 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002139}
2140
2141/**
2142 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2143 * @vsi: ptr to the VSI
2144 *
2145 * If this function returns with an error, then it's possible one or
2146 * more of the rings is populated (while the rest are not). It is the
2147 * callers duty to clean those orphaned rings.
2148 *
2149 * Return 0 on success, negative on failure
2150 **/
2151static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2152{
2153 int i, err = 0;
2154
2155 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002156 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002157
2158 return err;
2159}
2160
2161/**
2162 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2163 * @vsi: ptr to the VSI
2164 *
2165 * Free VSI's transmit software resources
2166 **/
2167static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2168{
2169 int i;
2170
Greg Rose8e9dca52013-12-18 13:45:53 +00002171 if (!vsi->tx_rings)
2172 return;
2173
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002174 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002175 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002176 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002177}
2178
2179/**
2180 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2181 * @vsi: ptr to the VSI
2182 *
2183 * If this function returns with an error, then it's possible one or
2184 * more of the rings is populated (while the rest are not). It is the
2185 * callers duty to clean those orphaned rings.
2186 *
2187 * Return 0 on success, negative on failure
2188 **/
2189static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2190{
2191 int i, err = 0;
2192
2193 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002194 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002195 return err;
2196}
2197
2198/**
2199 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2200 * @vsi: ptr to the VSI
2201 *
2202 * Free all receive software resources
2203 **/
2204static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2205{
2206 int i;
2207
Greg Rose8e9dca52013-12-18 13:45:53 +00002208 if (!vsi->rx_rings)
2209 return;
2210
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002211 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002212 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002213 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002214}
2215
2216/**
2217 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2218 * @ring: The Tx ring to configure
2219 *
2220 * Configure the Tx descriptor ring in the HMC context.
2221 **/
2222static int i40e_configure_tx_ring(struct i40e_ring *ring)
2223{
2224 struct i40e_vsi *vsi = ring->vsi;
2225 u16 pf_q = vsi->base_queue + ring->queue_index;
2226 struct i40e_hw *hw = &vsi->back->hw;
2227 struct i40e_hmc_obj_txq tx_ctx;
2228 i40e_status err = 0;
2229 u32 qtx_ctl = 0;
2230
2231 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002232 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002233 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2234 ring->atr_count = 0;
2235 } else {
2236 ring->atr_sample_rate = 0;
2237 }
2238
2239 /* initialize XPS */
2240 if (ring->q_vector && ring->netdev &&
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08002241 vsi->tc_config.numtc <= 1 &&
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002242 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
2243 netif_set_xps_queue(ring->netdev,
2244 &ring->q_vector->affinity_mask,
2245 ring->queue_index);
2246
2247 /* clear the context structure first */
2248 memset(&tx_ctx, 0, sizeof(tx_ctx));
2249
2250 tx_ctx.new_context = 1;
2251 tx_ctx.base = (ring->dma / 128);
2252 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002253 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2254 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002255 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002256 /* FDIR VSI tx ring can still use RS bit and writebacks */
2257 if (vsi->type != I40E_VSI_FDIR)
2258 tx_ctx.head_wb_ena = 1;
2259 tx_ctx.head_wb_addr = ring->dma +
2260 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002261
2262 /* As part of VSI creation/update, FW allocates certain
2263 * Tx arbitration queue sets for each TC enabled for
2264 * the VSI. The FW returns the handles to these queue
2265 * sets as part of the response buffer to Add VSI,
2266 * Update VSI, etc. AQ commands. It is expected that
2267 * these queue set handles be associated with the Tx
2268 * queues by the driver as part of the TX queue context
2269 * initialization. This has to be done regardless of
2270 * DCB as by default everything is mapped to TC0.
2271 */
2272 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2273 tx_ctx.rdylist_act = 0;
2274
2275 /* clear the context in the HMC */
2276 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2277 if (err) {
2278 dev_info(&vsi->back->pdev->dev,
2279 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2280 ring->queue_index, pf_q, err);
2281 return -ENOMEM;
2282 }
2283
2284 /* set the context in the HMC */
2285 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2286 if (err) {
2287 dev_info(&vsi->back->pdev->dev,
2288 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2289 ring->queue_index, pf_q, err);
2290 return -ENOMEM;
2291 }
2292
2293 /* Now associate this queue with this PCI function */
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002294 if (vsi->type == I40E_VSI_VMDQ2)
2295 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
2296 else
2297 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +00002298 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2299 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002300 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2301 i40e_flush(hw);
2302
2303 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2304
2305 /* cache tail off for easier writes later */
2306 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2307
2308 return 0;
2309}
2310
2311/**
2312 * i40e_configure_rx_ring - Configure a receive ring context
2313 * @ring: The Rx ring to configure
2314 *
2315 * Configure the Rx descriptor ring in the HMC context.
2316 **/
2317static int i40e_configure_rx_ring(struct i40e_ring *ring)
2318{
2319 struct i40e_vsi *vsi = ring->vsi;
2320 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2321 u16 pf_q = vsi->base_queue + ring->queue_index;
2322 struct i40e_hw *hw = &vsi->back->hw;
2323 struct i40e_hmc_obj_rxq rx_ctx;
2324 i40e_status err = 0;
2325
2326 ring->state = 0;
2327
2328 /* clear the context structure first */
2329 memset(&rx_ctx, 0, sizeof(rx_ctx));
2330
2331 ring->rx_buf_len = vsi->rx_buf_len;
2332 ring->rx_hdr_len = vsi->rx_hdr_len;
2333
2334 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2335 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2336
2337 rx_ctx.base = (ring->dma / 128);
2338 rx_ctx.qlen = ring->count;
2339
2340 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2341 set_ring_16byte_desc_enabled(ring);
2342 rx_ctx.dsize = 0;
2343 } else {
2344 rx_ctx.dsize = 1;
2345 }
2346
2347 rx_ctx.dtype = vsi->dtype;
2348 if (vsi->dtype) {
2349 set_ring_ps_enabled(ring);
2350 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2351 I40E_RX_SPLIT_IP |
2352 I40E_RX_SPLIT_TCP_UDP |
2353 I40E_RX_SPLIT_SCTP;
2354 } else {
2355 rx_ctx.hsplit_0 = 0;
2356 }
2357
2358 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2359 (chain_len * ring->rx_buf_len));
2360 rx_ctx.tphrdesc_ena = 1;
2361 rx_ctx.tphwdesc_ena = 1;
2362 rx_ctx.tphdata_ena = 1;
2363 rx_ctx.tphhead_ena = 1;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002364 if (hw->revision_id == 0)
2365 rx_ctx.lrxqthresh = 0;
2366 else
2367 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002368 rx_ctx.crcstrip = 1;
2369 rx_ctx.l2tsel = 1;
2370 rx_ctx.showiv = 1;
Catherine Sullivanacb36762014-03-06 09:02:30 +00002371 /* set the prefena field to 1 because the manual says to */
2372 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002373
2374 /* clear the context in the HMC */
2375 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2376 if (err) {
2377 dev_info(&vsi->back->pdev->dev,
2378 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2379 ring->queue_index, pf_q, err);
2380 return -ENOMEM;
2381 }
2382
2383 /* set the context in the HMC */
2384 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2385 if (err) {
2386 dev_info(&vsi->back->pdev->dev,
2387 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2388 ring->queue_index, pf_q, err);
2389 return -ENOMEM;
2390 }
2391
2392 /* cache tail for quicker writes, and clear the reg before use */
2393 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2394 writel(0, ring->tail);
2395
2396 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
2397
2398 return 0;
2399}
2400
2401/**
2402 * i40e_vsi_configure_tx - Configure the VSI for Tx
2403 * @vsi: VSI structure describing this set of rings and resources
2404 *
2405 * Configure the Tx VSI for operation.
2406 **/
2407static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2408{
2409 int err = 0;
2410 u16 i;
2411
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002412 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2413 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002414
2415 return err;
2416}
2417
2418/**
2419 * i40e_vsi_configure_rx - Configure the VSI for Rx
2420 * @vsi: the VSI being configured
2421 *
2422 * Configure the Rx VSI for operation.
2423 **/
2424static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2425{
2426 int err = 0;
2427 u16 i;
2428
2429 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2430 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2431 + ETH_FCS_LEN + VLAN_HLEN;
2432 else
2433 vsi->max_frame = I40E_RXBUFFER_2048;
2434
2435 /* figure out correct receive buffer length */
2436 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2437 I40E_FLAG_RX_PS_ENABLED)) {
2438 case I40E_FLAG_RX_1BUF_ENABLED:
2439 vsi->rx_hdr_len = 0;
2440 vsi->rx_buf_len = vsi->max_frame;
2441 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2442 break;
2443 case I40E_FLAG_RX_PS_ENABLED:
2444 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2445 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2446 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2447 break;
2448 default:
2449 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2450 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2451 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2452 break;
2453 }
2454
2455 /* round up for the chip's needs */
2456 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2457 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2458 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2459 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2460
2461 /* set up individual rings */
2462 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002463 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002464
2465 return err;
2466}
2467
2468/**
2469 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2470 * @vsi: ptr to the VSI
2471 **/
2472static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2473{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002474 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002475 u16 qoffset, qcount;
2476 int i, n;
2477
2478 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2479 return;
2480
2481 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2482 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2483 continue;
2484
2485 qoffset = vsi->tc_config.tc_info[n].qoffset;
2486 qcount = vsi->tc_config.tc_info[n].qcount;
2487 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002488 rx_ring = vsi->rx_rings[i];
2489 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002490 rx_ring->dcb_tc = n;
2491 tx_ring->dcb_tc = n;
2492 }
2493 }
2494}
2495
2496/**
2497 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2498 * @vsi: ptr to the VSI
2499 **/
2500static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2501{
2502 if (vsi->netdev)
2503 i40e_set_rx_mode(vsi->netdev);
2504}
2505
2506/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002507 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
2508 * @vsi: Pointer to the targeted VSI
2509 *
2510 * This function replays the hlist on the hw where all the SB Flow Director
2511 * filters were saved.
2512 **/
2513static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
2514{
2515 struct i40e_fdir_filter *filter;
2516 struct i40e_pf *pf = vsi->back;
2517 struct hlist_node *node;
2518
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002519 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2520 return;
2521
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002522 hlist_for_each_entry_safe(filter, node,
2523 &pf->fdir_filter_list, fdir_node) {
2524 i40e_add_del_fdir(vsi, filter, true);
2525 }
2526}
2527
2528/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002529 * i40e_vsi_configure - Set up the VSI for action
2530 * @vsi: the VSI being configured
2531 **/
2532static int i40e_vsi_configure(struct i40e_vsi *vsi)
2533{
2534 int err;
2535
2536 i40e_set_vsi_rx_mode(vsi);
2537 i40e_restore_vlan(vsi);
2538 i40e_vsi_config_dcb_rings(vsi);
2539 err = i40e_vsi_configure_tx(vsi);
2540 if (!err)
2541 err = i40e_vsi_configure_rx(vsi);
2542
2543 return err;
2544}
2545
2546/**
2547 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2548 * @vsi: the VSI being configured
2549 **/
2550static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2551{
2552 struct i40e_pf *pf = vsi->back;
2553 struct i40e_q_vector *q_vector;
2554 struct i40e_hw *hw = &pf->hw;
2555 u16 vector;
2556 int i, q;
2557 u32 val;
2558 u32 qp;
2559
2560 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2561 * and PFINT_LNKLSTn registers, e.g.:
2562 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2563 */
2564 qp = vsi->base_queue;
2565 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002566 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2567 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002568 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2569 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2570 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2571 q_vector->rx.itr);
2572 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2573 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2574 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2575 q_vector->tx.itr);
2576
2577 /* Linked list for the queuepairs assigned to this vector */
2578 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2579 for (q = 0; q < q_vector->num_ringpairs; q++) {
2580 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2581 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2582 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2583 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2584 (I40E_QUEUE_TYPE_TX
2585 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2586
2587 wr32(hw, I40E_QINT_RQCTL(qp), val);
2588
2589 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2590 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2591 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2592 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2593 (I40E_QUEUE_TYPE_RX
2594 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2595
2596 /* Terminate the linked list */
2597 if (q == (q_vector->num_ringpairs - 1))
2598 val |= (I40E_QUEUE_END_OF_LIST
2599 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2600
2601 wr32(hw, I40E_QINT_TQCTL(qp), val);
2602 qp++;
2603 }
2604 }
2605
2606 i40e_flush(hw);
2607}
2608
2609/**
2610 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2611 * @hw: ptr to the hardware info
2612 **/
2613static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2614{
2615 u32 val;
2616
2617 /* clear things first */
2618 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2619 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2620
2621 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2622 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2623 I40E_PFINT_ICR0_ENA_GRST_MASK |
2624 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2625 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002626 I40E_PFINT_ICR0_ENA_TIMESYNC_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002627 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2628 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2629 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2630
2631 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2632
2633 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002634 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2635 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002636
2637 /* OTHER_ITR_IDX = 0 */
2638 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2639}
2640
2641/**
2642 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2643 * @vsi: the VSI being configured
2644 **/
2645static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2646{
Alexander Duyck493fb302013-09-28 07:01:44 +00002647 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002648 struct i40e_pf *pf = vsi->back;
2649 struct i40e_hw *hw = &pf->hw;
2650 u32 val;
2651
2652 /* set the ITR configuration */
2653 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2654 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2655 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2656 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2657 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2658 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2659
2660 i40e_enable_misc_int_causes(hw);
2661
2662 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2663 wr32(hw, I40E_PFINT_LNKLST0, 0);
2664
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00002665 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002666 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2667 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2668 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2669
2670 wr32(hw, I40E_QINT_RQCTL(0), val);
2671
2672 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2673 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2674 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2675
2676 wr32(hw, I40E_QINT_TQCTL(0), val);
2677 i40e_flush(hw);
2678}
2679
2680/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00002681 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2682 * @pf: board private structure
2683 **/
2684void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2685{
2686 struct i40e_hw *hw = &pf->hw;
2687
2688 wr32(hw, I40E_PFINT_DYN_CTL0,
2689 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2690 i40e_flush(hw);
2691}
2692
2693/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002694 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2695 * @pf: board private structure
2696 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002697void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002698{
2699 struct i40e_hw *hw = &pf->hw;
2700 u32 val;
2701
2702 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2703 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2704 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2705
2706 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2707 i40e_flush(hw);
2708}
2709
2710/**
2711 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2712 * @vsi: pointer to a vsi
2713 * @vector: enable a particular Hw Interrupt vector
2714 **/
2715void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2716{
2717 struct i40e_pf *pf = vsi->back;
2718 struct i40e_hw *hw = &pf->hw;
2719 u32 val;
2720
2721 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2722 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2723 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2724 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002725 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002726}
2727
2728/**
2729 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2730 * @irq: interrupt number
2731 * @data: pointer to a q_vector
2732 **/
2733static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2734{
2735 struct i40e_q_vector *q_vector = data;
2736
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002737 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002738 return IRQ_HANDLED;
2739
2740 napi_schedule(&q_vector->napi);
2741
2742 return IRQ_HANDLED;
2743}
2744
2745/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002746 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2747 * @vsi: the VSI being configured
2748 * @basename: name for the vector
2749 *
2750 * Allocates MSI-X vectors and requests interrupts from the kernel.
2751 **/
2752static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2753{
2754 int q_vectors = vsi->num_q_vectors;
2755 struct i40e_pf *pf = vsi->back;
2756 int base = vsi->base_vector;
2757 int rx_int_idx = 0;
2758 int tx_int_idx = 0;
2759 int vector, err;
2760
2761 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00002762 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002763
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002764 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002765 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2766 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2767 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002768 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002769 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2770 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002771 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002772 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2773 "%s-%s-%d", basename, "tx", tx_int_idx++);
2774 } else {
2775 /* skip this unused q_vector */
2776 continue;
2777 }
2778 err = request_irq(pf->msix_entries[base + vector].vector,
2779 vsi->irq_handler,
2780 0,
2781 q_vector->name,
2782 q_vector);
2783 if (err) {
2784 dev_info(&pf->pdev->dev,
2785 "%s: request_irq failed, error: %d\n",
2786 __func__, err);
2787 goto free_queue_irqs;
2788 }
2789 /* assign the mask for this irq */
2790 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2791 &q_vector->affinity_mask);
2792 }
2793
Shannon Nelson63741842014-04-23 04:50:16 +00002794 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002795 return 0;
2796
2797free_queue_irqs:
2798 while (vector) {
2799 vector--;
2800 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2801 NULL);
2802 free_irq(pf->msix_entries[base + vector].vector,
2803 &(vsi->q_vectors[vector]));
2804 }
2805 return err;
2806}
2807
2808/**
2809 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2810 * @vsi: the VSI being un-configured
2811 **/
2812static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2813{
2814 struct i40e_pf *pf = vsi->back;
2815 struct i40e_hw *hw = &pf->hw;
2816 int base = vsi->base_vector;
2817 int i;
2818
2819 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002820 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2821 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002822 }
2823
2824 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2825 for (i = vsi->base_vector;
2826 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2827 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2828
2829 i40e_flush(hw);
2830 for (i = 0; i < vsi->num_q_vectors; i++)
2831 synchronize_irq(pf->msix_entries[i + base].vector);
2832 } else {
2833 /* Legacy and MSI mode - this stops all interrupt handling */
2834 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2835 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2836 i40e_flush(hw);
2837 synchronize_irq(pf->pdev->irq);
2838 }
2839}
2840
2841/**
2842 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2843 * @vsi: the VSI being configured
2844 **/
2845static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2846{
2847 struct i40e_pf *pf = vsi->back;
2848 int i;
2849
2850 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2851 for (i = vsi->base_vector;
2852 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2853 i40e_irq_dynamic_enable(vsi, i);
2854 } else {
2855 i40e_irq_dynamic_enable_icr0(pf);
2856 }
2857
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002858 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002859 return 0;
2860}
2861
2862/**
2863 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2864 * @pf: board private structure
2865 **/
2866static void i40e_stop_misc_vector(struct i40e_pf *pf)
2867{
2868 /* Disable ICR 0 */
2869 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2870 i40e_flush(&pf->hw);
2871}
2872
2873/**
2874 * i40e_intr - MSI/Legacy and non-queue interrupt handler
2875 * @irq: interrupt number
2876 * @data: pointer to a q_vector
2877 *
2878 * This is the handler used for all MSI/Legacy interrupts, and deals
2879 * with both queue and non-queue interrupts. This is also used in
2880 * MSIX mode to handle the non-queue interrupts.
2881 **/
2882static irqreturn_t i40e_intr(int irq, void *data)
2883{
2884 struct i40e_pf *pf = (struct i40e_pf *)data;
2885 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002886 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002887 u32 icr0, icr0_remaining;
2888 u32 val, ena_mask;
2889
2890 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002891 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002892
Shannon Nelson116a57d2013-09-28 07:13:59 +00002893 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2894 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002895 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002896
Shannon Nelsoncd92e722013-11-16 10:00:44 +00002897 /* if interrupt but no bits showing, must be SWINT */
2898 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2899 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2900 pf->sw_int_count++;
2901
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002902 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2903 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2904
2905 /* temporarily disable queue cause for NAPI processing */
2906 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2907 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2908 wr32(hw, I40E_QINT_RQCTL(0), qval);
2909
2910 qval = rd32(hw, I40E_QINT_TQCTL(0));
2911 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2912 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002913
2914 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00002915 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002916 }
2917
2918 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2919 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2920 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2921 }
2922
2923 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2924 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2925 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2926 }
2927
2928 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2929 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2930 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2931 }
2932
2933 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2934 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2935 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2936 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2937 val = rd32(hw, I40E_GLGEN_RSTAT);
2938 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2939 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002940 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002941 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002942 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002943 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002944 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002945 pf->empr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002946 set_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
2947 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002948 }
2949
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002950 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2951 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
2952 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2953 }
2954
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002955 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
2956 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
2957
2958 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07002959 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002960 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002961 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002962 }
2963
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002964 /* If a critical error is pending we have no choice but to reset the
2965 * device.
2966 * Report and mask out any remaining unexpected interrupts.
2967 */
2968 icr0_remaining = icr0 & ena_mask;
2969 if (icr0_remaining) {
2970 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
2971 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002972 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002973 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00002974 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002975 dev_info(&pf->pdev->dev, "device will be reset\n");
2976 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
2977 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002978 }
2979 ena_mask &= ~icr0_remaining;
2980 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002981 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002982
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002983enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002984 /* re-enable interrupt causes */
2985 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002986 if (!test_bit(__I40E_DOWN, &pf->state)) {
2987 i40e_service_event_schedule(pf);
2988 i40e_irq_dynamic_enable_icr0(pf);
2989 }
2990
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002991 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002992}
2993
2994/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08002995 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
2996 * @tx_ring: tx ring to clean
2997 * @budget: how many cleans we're allowed
2998 *
2999 * Returns true if there's any budget left (e.g. the clean is finished)
3000 **/
3001static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3002{
3003 struct i40e_vsi *vsi = tx_ring->vsi;
3004 u16 i = tx_ring->next_to_clean;
3005 struct i40e_tx_buffer *tx_buf;
3006 struct i40e_tx_desc *tx_desc;
3007
3008 tx_buf = &tx_ring->tx_bi[i];
3009 tx_desc = I40E_TX_DESC(tx_ring, i);
3010 i -= tx_ring->count;
3011
3012 do {
3013 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3014
3015 /* if next_to_watch is not set then there is no work pending */
3016 if (!eop_desc)
3017 break;
3018
3019 /* prevent any other reads prior to eop_desc */
3020 read_barrier_depends();
3021
3022 /* if the descriptor isn't done, no work yet to do */
3023 if (!(eop_desc->cmd_type_offset_bsz &
3024 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3025 break;
3026
3027 /* clear next_to_watch to prevent false hangs */
3028 tx_buf->next_to_watch = NULL;
3029
3030 /* unmap skb header data */
3031 dma_unmap_single(tx_ring->dev,
3032 dma_unmap_addr(tx_buf, dma),
3033 dma_unmap_len(tx_buf, len),
3034 DMA_TO_DEVICE);
3035
3036 dma_unmap_len_set(tx_buf, len, 0);
3037
3038
3039 /* move to the next desc and buffer to clean */
3040 tx_buf++;
3041 tx_desc++;
3042 i++;
3043 if (unlikely(!i)) {
3044 i -= tx_ring->count;
3045 tx_buf = tx_ring->tx_bi;
3046 tx_desc = I40E_TX_DESC(tx_ring, 0);
3047 }
3048
3049 /* update budget accounting */
3050 budget--;
3051 } while (likely(budget));
3052
3053 i += tx_ring->count;
3054 tx_ring->next_to_clean = i;
3055
3056 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
3057 i40e_irq_dynamic_enable(vsi,
3058 tx_ring->q_vector->v_idx + vsi->base_vector);
3059 }
3060 return budget > 0;
3061}
3062
3063/**
3064 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3065 * @irq: interrupt number
3066 * @data: pointer to a q_vector
3067 **/
3068static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3069{
3070 struct i40e_q_vector *q_vector = data;
3071 struct i40e_vsi *vsi;
3072
3073 if (!q_vector->tx.ring)
3074 return IRQ_HANDLED;
3075
3076 vsi = q_vector->tx.ring->vsi;
3077 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3078
3079 return IRQ_HANDLED;
3080}
3081
3082/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003083 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003084 * @vsi: the VSI being configured
3085 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003086 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003087 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003088static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003089{
Alexander Duyck493fb302013-09-28 07:01:44 +00003090 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003091 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3092 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003093
3094 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003095 tx_ring->next = q_vector->tx.ring;
3096 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003097 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003098
3099 rx_ring->q_vector = q_vector;
3100 rx_ring->next = q_vector->rx.ring;
3101 q_vector->rx.ring = rx_ring;
3102 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003103}
3104
3105/**
3106 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3107 * @vsi: the VSI being configured
3108 *
3109 * This function maps descriptor rings to the queue-specific vectors
3110 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3111 * one vector per queue pair, but on a constrained vector budget, we
3112 * group the queue pairs as "efficiently" as possible.
3113 **/
3114static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3115{
3116 int qp_remaining = vsi->num_queue_pairs;
3117 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003118 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003119 int v_start = 0;
3120 int qp_idx = 0;
3121
3122 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3123 * group them so there are multiple queues per vector.
3124 */
3125 for (; v_start < q_vectors && qp_remaining; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003126 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3127
3128 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3129
3130 q_vector->num_ringpairs = num_ringpairs;
3131
3132 q_vector->rx.count = 0;
3133 q_vector->tx.count = 0;
3134 q_vector->rx.ring = NULL;
3135 q_vector->tx.ring = NULL;
3136
3137 while (num_ringpairs--) {
3138 map_vector_to_qp(vsi, v_start, qp_idx);
3139 qp_idx++;
3140 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003141 }
3142 }
3143}
3144
3145/**
3146 * i40e_vsi_request_irq - Request IRQ from the OS
3147 * @vsi: the VSI being configured
3148 * @basename: name for the vector
3149 **/
3150static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3151{
3152 struct i40e_pf *pf = vsi->back;
3153 int err;
3154
3155 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3156 err = i40e_vsi_request_irq_msix(vsi, basename);
3157 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3158 err = request_irq(pf->pdev->irq, i40e_intr, 0,
3159 pf->misc_int_name, pf);
3160 else
3161 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
3162 pf->misc_int_name, pf);
3163
3164 if (err)
3165 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3166
3167 return err;
3168}
3169
3170#ifdef CONFIG_NET_POLL_CONTROLLER
3171/**
3172 * i40e_netpoll - A Polling 'interrupt'handler
3173 * @netdev: network interface device structure
3174 *
3175 * This is used by netconsole to send skbs without having to re-enable
3176 * interrupts. It's not called while the normal interrupt routine is executing.
3177 **/
3178static void i40e_netpoll(struct net_device *netdev)
3179{
3180 struct i40e_netdev_priv *np = netdev_priv(netdev);
3181 struct i40e_vsi *vsi = np->vsi;
3182 struct i40e_pf *pf = vsi->back;
3183 int i;
3184
3185 /* if interface is down do nothing */
3186 if (test_bit(__I40E_DOWN, &vsi->state))
3187 return;
3188
3189 pf->flags |= I40E_FLAG_IN_NETPOLL;
3190 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3191 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003192 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003193 } else {
3194 i40e_intr(pf->pdev->irq, netdev);
3195 }
3196 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3197}
3198#endif
3199
3200/**
3201 * i40e_vsi_control_tx - Start or stop a VSI's rings
3202 * @vsi: the VSI being configured
3203 * @enable: start or stop the rings
3204 **/
3205static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3206{
3207 struct i40e_pf *pf = vsi->back;
3208 struct i40e_hw *hw = &pf->hw;
3209 int i, j, pf_q;
3210 u32 tx_reg;
3211
3212 pf_q = vsi->base_queue;
3213 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499a2014-04-23 04:50:03 +00003214
3215 /* warn the TX unit of coming changes */
3216 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3217 if (!enable)
3218 udelay(10);
3219
Mitch Williams6c5ef622014-02-20 19:29:16 -08003220 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003221 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003222 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3223 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3224 break;
3225 usleep_range(1000, 2000);
3226 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003227 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003228 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003229 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003230
3231 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003232 if (enable) {
3233 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003234 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003235 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003236 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003237 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003238
3239 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3240
3241 /* wait for the change to finish */
3242 for (j = 0; j < 10; j++) {
3243 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Catherine Sullivan7c122002014-03-14 07:32:29 +00003244 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3245 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003246
3247 udelay(10);
3248 }
3249 if (j >= 10) {
3250 dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
3251 pf_q, (enable ? "en" : "dis"));
3252 return -ETIMEDOUT;
3253 }
3254 }
3255
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003256 if (hw->revision_id == 0)
3257 mdelay(50);
3258
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003259 return 0;
3260}
3261
3262/**
3263 * i40e_vsi_control_rx - Start or stop a VSI's rings
3264 * @vsi: the VSI being configured
3265 * @enable: start or stop the rings
3266 **/
3267static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3268{
3269 struct i40e_pf *pf = vsi->back;
3270 struct i40e_hw *hw = &pf->hw;
3271 int i, j, pf_q;
3272 u32 rx_reg;
3273
3274 pf_q = vsi->base_queue;
3275 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003276 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003277 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003278 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3279 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3280 break;
3281 usleep_range(1000, 2000);
3282 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003283
Catherine Sullivan7c122002014-03-14 07:32:29 +00003284 /* Skip if the queue is already in the requested state */
3285 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3286 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003287
3288 /* turn on/off the queue */
3289 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003290 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003291 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003292 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003293 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3294
3295 /* wait for the change to finish */
3296 for (j = 0; j < 10; j++) {
3297 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3298
Catherine Sullivan7c122002014-03-14 07:32:29 +00003299 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3300 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003301
3302 udelay(10);
3303 }
3304 if (j >= 10) {
3305 dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3306 pf_q, (enable ? "en" : "dis"));
3307 return -ETIMEDOUT;
3308 }
3309 }
3310
3311 return 0;
3312}
3313
3314/**
3315 * i40e_vsi_control_rings - Start or stop a VSI's rings
3316 * @vsi: the VSI being configured
3317 * @enable: start or stop the rings
3318 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003319int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003320{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003321 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003322
3323 /* do rx first for enable and last for disable */
3324 if (request) {
3325 ret = i40e_vsi_control_rx(vsi, request);
3326 if (ret)
3327 return ret;
3328 ret = i40e_vsi_control_tx(vsi, request);
3329 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003330 /* Ignore return value, we need to shutdown whatever we can */
3331 i40e_vsi_control_tx(vsi, request);
3332 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003333 }
3334
3335 return ret;
3336}
3337
3338/**
3339 * i40e_vsi_free_irq - Free the irq association with the OS
3340 * @vsi: the VSI being configured
3341 **/
3342static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3343{
3344 struct i40e_pf *pf = vsi->back;
3345 struct i40e_hw *hw = &pf->hw;
3346 int base = vsi->base_vector;
3347 u32 val, qp;
3348 int i;
3349
3350 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3351 if (!vsi->q_vectors)
3352 return;
3353
Shannon Nelson63741842014-04-23 04:50:16 +00003354 if (!vsi->irqs_ready)
3355 return;
3356
3357 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003358 for (i = 0; i < vsi->num_q_vectors; i++) {
3359 u16 vector = i + base;
3360
3361 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003362 if (!vsi->q_vectors[i] ||
3363 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003364 continue;
3365
3366 /* clear the affinity_mask in the IRQ descriptor */
3367 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3368 NULL);
3369 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003370 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003371
3372 /* Tear down the interrupt queue link list
3373 *
3374 * We know that they come in pairs and always
3375 * the Rx first, then the Tx. To clear the
3376 * link list, stick the EOL value into the
3377 * next_q field of the registers.
3378 */
3379 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3380 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3381 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3382 val |= I40E_QUEUE_END_OF_LIST
3383 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3384 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3385
3386 while (qp != I40E_QUEUE_END_OF_LIST) {
3387 u32 next;
3388
3389 val = rd32(hw, I40E_QINT_RQCTL(qp));
3390
3391 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3392 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3393 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3394 I40E_QINT_RQCTL_INTEVENT_MASK);
3395
3396 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3397 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3398
3399 wr32(hw, I40E_QINT_RQCTL(qp), val);
3400
3401 val = rd32(hw, I40E_QINT_TQCTL(qp));
3402
3403 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3404 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3405
3406 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3407 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3408 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3409 I40E_QINT_TQCTL_INTEVENT_MASK);
3410
3411 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3412 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3413
3414 wr32(hw, I40E_QINT_TQCTL(qp), val);
3415 qp = next;
3416 }
3417 }
3418 } else {
3419 free_irq(pf->pdev->irq, pf);
3420
3421 val = rd32(hw, I40E_PFINT_LNKLST0);
3422 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3423 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3424 val |= I40E_QUEUE_END_OF_LIST
3425 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3426 wr32(hw, I40E_PFINT_LNKLST0, val);
3427
3428 val = rd32(hw, I40E_QINT_RQCTL(qp));
3429 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3430 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3431 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3432 I40E_QINT_RQCTL_INTEVENT_MASK);
3433
3434 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3435 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3436
3437 wr32(hw, I40E_QINT_RQCTL(qp), val);
3438
3439 val = rd32(hw, I40E_QINT_TQCTL(qp));
3440
3441 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3442 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3443 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3444 I40E_QINT_TQCTL_INTEVENT_MASK);
3445
3446 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3447 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3448
3449 wr32(hw, I40E_QINT_TQCTL(qp), val);
3450 }
3451}
3452
3453/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003454 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3455 * @vsi: the VSI being configured
3456 * @v_idx: Index of vector to be freed
3457 *
3458 * This function frees the memory allocated to the q_vector. In addition if
3459 * NAPI is enabled it will delete any references to the NAPI struct prior
3460 * to freeing the q_vector.
3461 **/
3462static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3463{
3464 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003465 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003466
3467 if (!q_vector)
3468 return;
3469
3470 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003471 i40e_for_each_ring(ring, q_vector->tx)
3472 ring->q_vector = NULL;
3473
3474 i40e_for_each_ring(ring, q_vector->rx)
3475 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003476
3477 /* only VSI w/ an associated netdev is set up w/ NAPI */
3478 if (vsi->netdev)
3479 netif_napi_del(&q_vector->napi);
3480
3481 vsi->q_vectors[v_idx] = NULL;
3482
3483 kfree_rcu(q_vector, rcu);
3484}
3485
3486/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003487 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3488 * @vsi: the VSI being un-configured
3489 *
3490 * This frees the memory allocated to the q_vectors and
3491 * deletes references to the NAPI struct.
3492 **/
3493static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3494{
3495 int v_idx;
3496
Alexander Duyck493fb302013-09-28 07:01:44 +00003497 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3498 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003499}
3500
3501/**
3502 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3503 * @pf: board private structure
3504 **/
3505static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3506{
3507 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3508 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3509 pci_disable_msix(pf->pdev);
3510 kfree(pf->msix_entries);
3511 pf->msix_entries = NULL;
3512 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3513 pci_disable_msi(pf->pdev);
3514 }
3515 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3516}
3517
3518/**
3519 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3520 * @pf: board private structure
3521 *
3522 * We go through and clear interrupt specific resources and reset the structure
3523 * to pre-load conditions
3524 **/
3525static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3526{
3527 int i;
3528
3529 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00003530 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003531 if (pf->vsi[i])
3532 i40e_vsi_free_q_vectors(pf->vsi[i]);
3533 i40e_reset_interrupt_capability(pf);
3534}
3535
3536/**
3537 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3538 * @vsi: the VSI being configured
3539 **/
3540static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3541{
3542 int q_idx;
3543
3544 if (!vsi->netdev)
3545 return;
3546
3547 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003548 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003549}
3550
3551/**
3552 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3553 * @vsi: the VSI being configured
3554 **/
3555static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3556{
3557 int q_idx;
3558
3559 if (!vsi->netdev)
3560 return;
3561
3562 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003563 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003564}
3565
3566/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003567 * i40e_vsi_close - Shut down a VSI
3568 * @vsi: the vsi to be quelled
3569 **/
3570static void i40e_vsi_close(struct i40e_vsi *vsi)
3571{
3572 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
3573 i40e_down(vsi);
3574 i40e_vsi_free_irq(vsi);
3575 i40e_vsi_free_tx_resources(vsi);
3576 i40e_vsi_free_rx_resources(vsi);
3577}
3578
3579/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003580 * i40e_quiesce_vsi - Pause a given VSI
3581 * @vsi: the VSI being paused
3582 **/
3583static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3584{
3585 if (test_bit(__I40E_DOWN, &vsi->state))
3586 return;
3587
3588 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3589 if (vsi->netdev && netif_running(vsi->netdev)) {
3590 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3591 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003592 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003593 }
3594}
3595
3596/**
3597 * i40e_unquiesce_vsi - Resume a given VSI
3598 * @vsi: the VSI being resumed
3599 **/
3600static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3601{
3602 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3603 return;
3604
3605 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3606 if (vsi->netdev && netif_running(vsi->netdev))
3607 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3608 else
Shannon Nelson8276f752014-03-14 07:32:27 +00003609 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003610}
3611
3612/**
3613 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3614 * @pf: the PF
3615 **/
3616static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3617{
3618 int v;
3619
Mitch Williams505682c2014-05-20 08:01:37 +00003620 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003621 if (pf->vsi[v])
3622 i40e_quiesce_vsi(pf->vsi[v]);
3623 }
3624}
3625
3626/**
3627 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3628 * @pf: the PF
3629 **/
3630static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3631{
3632 int v;
3633
Mitch Williams505682c2014-05-20 08:01:37 +00003634 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003635 if (pf->vsi[v])
3636 i40e_unquiesce_vsi(pf->vsi[v]);
3637 }
3638}
3639
3640/**
3641 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3642 * @dcbcfg: the corresponding DCBx configuration structure
3643 *
3644 * Return the number of TCs from given DCBx configuration
3645 **/
3646static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3647{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003648 u8 num_tc = 0;
3649 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003650
3651 /* Scan the ETS Config Priority Table to find
3652 * traffic class enabled for a given priority
3653 * and use the traffic class index to get the
3654 * number of traffic classes enabled
3655 */
3656 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3657 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3658 num_tc = dcbcfg->etscfg.prioritytable[i];
3659 }
3660
3661 /* Traffic class index starts from zero so
3662 * increment to return the actual count
3663 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003664 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003665}
3666
3667/**
3668 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3669 * @dcbcfg: the corresponding DCBx configuration structure
3670 *
3671 * Query the current DCB configuration and return the number of
3672 * traffic classes enabled from the given DCBX config
3673 **/
3674static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3675{
3676 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3677 u8 enabled_tc = 1;
3678 u8 i;
3679
3680 for (i = 0; i < num_tc; i++)
3681 enabled_tc |= 1 << i;
3682
3683 return enabled_tc;
3684}
3685
3686/**
3687 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3688 * @pf: PF being queried
3689 *
3690 * Return number of traffic classes enabled for the given PF
3691 **/
3692static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3693{
3694 struct i40e_hw *hw = &pf->hw;
3695 u8 i, enabled_tc;
3696 u8 num_tc = 0;
3697 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3698
3699 /* If DCB is not enabled then always in single TC */
3700 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3701 return 1;
3702
3703 /* MFP mode return count of enabled TCs for this PF */
3704 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3705 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3706 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3707 if (enabled_tc & (1 << i))
3708 num_tc++;
3709 }
3710 return num_tc;
3711 }
3712
3713 /* SFP mode will be enabled for all TCs on port */
3714 return i40e_dcb_get_num_tc(dcbcfg);
3715}
3716
3717/**
3718 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3719 * @pf: PF being queried
3720 *
3721 * Return a bitmap for first enabled traffic class for this PF.
3722 **/
3723static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3724{
3725 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3726 u8 i = 0;
3727
3728 if (!enabled_tc)
3729 return 0x1; /* TC0 */
3730
3731 /* Find the first enabled TC */
3732 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3733 if (enabled_tc & (1 << i))
3734 break;
3735 }
3736
3737 return 1 << i;
3738}
3739
3740/**
3741 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3742 * @pf: PF being queried
3743 *
3744 * Return a bitmap for enabled traffic classes for this PF.
3745 **/
3746static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3747{
3748 /* If DCB is not enabled for this PF then just return default TC */
3749 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3750 return i40e_pf_get_default_tc(pf);
3751
3752 /* MFP mode will have enabled TCs set by FW */
3753 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3754 return pf->hw.func_caps.enabled_tcmap;
3755
3756 /* SFP mode we want PF to be enabled for all TCs */
3757 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3758}
3759
3760/**
3761 * i40e_vsi_get_bw_info - Query VSI BW Information
3762 * @vsi: the VSI being queried
3763 *
3764 * Returns 0 on success, negative value on failure
3765 **/
3766static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3767{
3768 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3769 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3770 struct i40e_pf *pf = vsi->back;
3771 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003772 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003773 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003774 int i;
3775
3776 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003777 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3778 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003779 dev_info(&pf->pdev->dev,
3780 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003781 aq_ret, pf->hw.aq.asq_last_status);
3782 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003783 }
3784
3785 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003786 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
Jesse Brandeburg6838b532014-01-14 00:49:52 -08003787 NULL);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003788 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003789 dev_info(&pf->pdev->dev,
3790 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003791 aq_ret, pf->hw.aq.asq_last_status);
3792 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003793 }
3794
3795 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3796 dev_info(&pf->pdev->dev,
3797 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3798 bw_config.tc_valid_bits,
3799 bw_ets_config.tc_valid_bits);
3800 /* Still continuing */
3801 }
3802
3803 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3804 vsi->bw_max_quanta = bw_config.max_bw;
3805 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3806 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3807 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3808 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3809 vsi->bw_ets_limit_credits[i] =
3810 le16_to_cpu(bw_ets_config.credits[i]);
3811 /* 3 bits out of 4 for each TC */
3812 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3813 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003814
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003815 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003816}
3817
3818/**
3819 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3820 * @vsi: the VSI being configured
3821 * @enabled_tc: TC bitmap
3822 * @bw_credits: BW shared credits per TC
3823 *
3824 * Returns 0 on success, negative value on failure
3825 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003826static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003827 u8 *bw_share)
3828{
3829 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003830 i40e_status aq_ret;
3831 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003832
3833 bw_data.tc_valid_bits = enabled_tc;
3834 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3835 bw_data.tc_bw_credits[i] = bw_share[i];
3836
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003837 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3838 NULL);
3839 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003840 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00003841 "AQ command Config VSI BW allocation per TC failed = %d\n",
3842 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003843 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003844 }
3845
3846 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3847 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3848
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003849 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003850}
3851
3852/**
3853 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3854 * @vsi: the VSI being configured
3855 * @enabled_tc: TC map to be enabled
3856 *
3857 **/
3858static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3859{
3860 struct net_device *netdev = vsi->netdev;
3861 struct i40e_pf *pf = vsi->back;
3862 struct i40e_hw *hw = &pf->hw;
3863 u8 netdev_tc = 0;
3864 int i;
3865 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3866
3867 if (!netdev)
3868 return;
3869
3870 if (!enabled_tc) {
3871 netdev_reset_tc(netdev);
3872 return;
3873 }
3874
3875 /* Set up actual enabled TCs on the VSI */
3876 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3877 return;
3878
3879 /* set per TC queues for the VSI */
3880 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3881 /* Only set TC queues for enabled tcs
3882 *
3883 * e.g. For a VSI that has TC0 and TC3 enabled the
3884 * enabled_tc bitmap would be 0x00001001; the driver
3885 * will set the numtc for netdev as 2 that will be
3886 * referenced by the netdev layer as TC 0 and 1.
3887 */
3888 if (vsi->tc_config.enabled_tc & (1 << i))
3889 netdev_set_tc_queue(netdev,
3890 vsi->tc_config.tc_info[i].netdev_tc,
3891 vsi->tc_config.tc_info[i].qcount,
3892 vsi->tc_config.tc_info[i].qoffset);
3893 }
3894
3895 /* Assign UP2TC map for the VSI */
3896 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3897 /* Get the actual TC# for the UP */
3898 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3899 /* Get the mapped netdev TC# for the UP */
3900 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3901 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3902 }
3903}
3904
3905/**
3906 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3907 * @vsi: the VSI being configured
3908 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3909 **/
3910static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3911 struct i40e_vsi_context *ctxt)
3912{
3913 /* copy just the sections touched not the entire info
3914 * since not all sections are valid as returned by
3915 * update vsi params
3916 */
3917 vsi->info.mapping_flags = ctxt->info.mapping_flags;
3918 memcpy(&vsi->info.queue_mapping,
3919 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3920 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3921 sizeof(vsi->info.tc_mapping));
3922}
3923
3924/**
3925 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3926 * @vsi: VSI to be configured
3927 * @enabled_tc: TC bitmap
3928 *
3929 * This configures a particular VSI for TCs that are mapped to the
3930 * given TC bitmap. It uses default bandwidth share for TCs across
3931 * VSIs to configure TC for a particular VSI.
3932 *
3933 * NOTE:
3934 * It is expected that the VSI queues have been quisced before calling
3935 * this function.
3936 **/
3937static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3938{
3939 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3940 struct i40e_vsi_context ctxt;
3941 int ret = 0;
3942 int i;
3943
3944 /* Check if enabled_tc is same as existing or new TCs */
3945 if (vsi->tc_config.enabled_tc == enabled_tc)
3946 return ret;
3947
3948 /* Enable ETS TCs with equal BW Share for now across all VSIs */
3949 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3950 if (enabled_tc & (1 << i))
3951 bw_share[i] = 1;
3952 }
3953
3954 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3955 if (ret) {
3956 dev_info(&vsi->back->pdev->dev,
3957 "Failed configuring TC map %d for VSI %d\n",
3958 enabled_tc, vsi->seid);
3959 goto out;
3960 }
3961
3962 /* Update Queue Pairs Mapping for currently enabled UPs */
3963 ctxt.seid = vsi->seid;
3964 ctxt.pf_num = vsi->back->hw.pf_id;
3965 ctxt.vf_num = 0;
3966 ctxt.uplink_seid = vsi->uplink_seid;
3967 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3968 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3969
3970 /* Update the VSI after updating the VSI queue-mapping information */
3971 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3972 if (ret) {
3973 dev_info(&vsi->back->pdev->dev,
3974 "update vsi failed, aq_err=%d\n",
3975 vsi->back->hw.aq.asq_last_status);
3976 goto out;
3977 }
3978 /* update the local VSI info with updated queue map */
3979 i40e_vsi_update_queue_map(vsi, &ctxt);
3980 vsi->info.valid_sections = 0;
3981
3982 /* Update current VSI BW information */
3983 ret = i40e_vsi_get_bw_info(vsi);
3984 if (ret) {
3985 dev_info(&vsi->back->pdev->dev,
3986 "Failed updating vsi bw info, aq_err=%d\n",
3987 vsi->back->hw.aq.asq_last_status);
3988 goto out;
3989 }
3990
3991 /* Update the netdev TC setup */
3992 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
3993out:
3994 return ret;
3995}
3996
3997/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08003998 * i40e_veb_config_tc - Configure TCs for given VEB
3999 * @veb: given VEB
4000 * @enabled_tc: TC bitmap
4001 *
4002 * Configures given TC bitmap for VEB (switching) element
4003 **/
4004int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4005{
4006 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4007 struct i40e_pf *pf = veb->pf;
4008 int ret = 0;
4009 int i;
4010
4011 /* No TCs or already enabled TCs just return */
4012 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4013 return ret;
4014
4015 bw_data.tc_valid_bits = enabled_tc;
4016 /* bw_data.absolute_credits is not set (relative) */
4017
4018 /* Enable ETS TCs with equal BW Share for now */
4019 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4020 if (enabled_tc & (1 << i))
4021 bw_data.tc_bw_share_credits[i] = 1;
4022 }
4023
4024 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4025 &bw_data, NULL);
4026 if (ret) {
4027 dev_info(&pf->pdev->dev,
4028 "veb bw config failed, aq_err=%d\n",
4029 pf->hw.aq.asq_last_status);
4030 goto out;
4031 }
4032
4033 /* Update the BW information */
4034 ret = i40e_veb_get_bw_info(veb);
4035 if (ret) {
4036 dev_info(&pf->pdev->dev,
4037 "Failed getting veb bw config, aq_err=%d\n",
4038 pf->hw.aq.asq_last_status);
4039 }
4040
4041out:
4042 return ret;
4043}
4044
4045#ifdef CONFIG_I40E_DCB
4046/**
4047 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4048 * @pf: PF struct
4049 *
4050 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4051 * the caller would've quiesce all the VSIs before calling
4052 * this function
4053 **/
4054static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4055{
4056 u8 tc_map = 0;
4057 int ret;
4058 u8 v;
4059
4060 /* Enable the TCs available on PF to all VEBs */
4061 tc_map = i40e_pf_get_tc_map(pf);
4062 for (v = 0; v < I40E_MAX_VEB; v++) {
4063 if (!pf->veb[v])
4064 continue;
4065 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4066 if (ret) {
4067 dev_info(&pf->pdev->dev,
4068 "Failed configuring TC for VEB seid=%d\n",
4069 pf->veb[v]->seid);
4070 /* Will try to configure as many components */
4071 }
4072 }
4073
4074 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004075 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004076 if (!pf->vsi[v])
4077 continue;
4078
4079 /* - Enable all TCs for the LAN VSI
4080 * - For all others keep them at TC0 for now
4081 */
4082 if (v == pf->lan_vsi)
4083 tc_map = i40e_pf_get_tc_map(pf);
4084 else
4085 tc_map = i40e_pf_get_default_tc(pf);
4086
4087 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4088 if (ret) {
4089 dev_info(&pf->pdev->dev,
4090 "Failed configuring TC for VSI seid=%d\n",
4091 pf->vsi[v]->seid);
4092 /* Will try to configure as many components */
4093 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004094 /* Re-configure VSI vectors based on updated TC map */
4095 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004096 if (pf->vsi[v]->netdev)
4097 i40e_dcbnl_set_all(pf->vsi[v]);
4098 }
4099 }
4100}
4101
4102/**
4103 * i40e_init_pf_dcb - Initialize DCB configuration
4104 * @pf: PF being configured
4105 *
4106 * Query the current DCB configuration and cache it
4107 * in the hardware structure
4108 **/
4109static int i40e_init_pf_dcb(struct i40e_pf *pf)
4110{
4111 struct i40e_hw *hw = &pf->hw;
4112 int err = 0;
4113
4114 if (pf->hw.func_caps.npar_enable)
4115 goto out;
4116
4117 /* Get the initial DCB configuration */
4118 err = i40e_init_dcb(hw);
4119 if (!err) {
4120 /* Device/Function is not DCBX capable */
4121 if ((!hw->func_caps.dcb) ||
4122 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4123 dev_info(&pf->pdev->dev,
4124 "DCBX offload is not supported or is disabled for this PF.\n");
4125
4126 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4127 goto out;
4128
4129 } else {
4130 /* When status is not DISABLED then DCBX in FW */
4131 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4132 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004133
4134 pf->flags |= I40E_FLAG_DCB_CAPABLE;
4135 /* Enable DCB tagging only when more than one TC */
4136 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
4137 pf->flags |= I40E_FLAG_DCB_ENABLED;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004138 }
Neerav Parikh014269f2014-04-01 07:11:48 +00004139 } else {
4140 dev_info(&pf->pdev->dev, "AQ Querying DCB configuration failed: %d\n",
4141 pf->hw.aq.asq_last_status);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004142 }
4143
4144out:
4145 return err;
4146}
4147#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004148#define SPEED_SIZE 14
4149#define FC_SIZE 8
4150/**
4151 * i40e_print_link_message - print link up or down
4152 * @vsi: the VSI for which link needs a message
4153 */
4154static void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
4155{
4156 char speed[SPEED_SIZE] = "Unknown";
4157 char fc[FC_SIZE] = "RX/TX";
4158
4159 if (!isup) {
4160 netdev_info(vsi->netdev, "NIC Link is Down\n");
4161 return;
4162 }
4163
4164 switch (vsi->back->hw.phy.link_info.link_speed) {
4165 case I40E_LINK_SPEED_40GB:
4166 strncpy(speed, "40 Gbps", SPEED_SIZE);
4167 break;
4168 case I40E_LINK_SPEED_10GB:
4169 strncpy(speed, "10 Gbps", SPEED_SIZE);
4170 break;
4171 case I40E_LINK_SPEED_1GB:
4172 strncpy(speed, "1000 Mbps", SPEED_SIZE);
4173 break;
4174 default:
4175 break;
4176 }
4177
4178 switch (vsi->back->hw.fc.current_mode) {
4179 case I40E_FC_FULL:
4180 strncpy(fc, "RX/TX", FC_SIZE);
4181 break;
4182 case I40E_FC_TX_PAUSE:
4183 strncpy(fc, "TX", FC_SIZE);
4184 break;
4185 case I40E_FC_RX_PAUSE:
4186 strncpy(fc, "RX", FC_SIZE);
4187 break;
4188 default:
4189 strncpy(fc, "None", FC_SIZE);
4190 break;
4191 }
4192
4193 netdev_info(vsi->netdev, "NIC Link is Up %s Full Duplex, Flow Control: %s\n",
4194 speed, fc);
4195}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004196
4197/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004198 * i40e_up_complete - Finish the last steps of bringing up a connection
4199 * @vsi: the VSI being configured
4200 **/
4201static int i40e_up_complete(struct i40e_vsi *vsi)
4202{
4203 struct i40e_pf *pf = vsi->back;
4204 int err;
4205
4206 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4207 i40e_vsi_configure_msix(vsi);
4208 else
4209 i40e_configure_msi_and_legacy(vsi);
4210
4211 /* start rings */
4212 err = i40e_vsi_control_rings(vsi, true);
4213 if (err)
4214 return err;
4215
4216 clear_bit(__I40E_DOWN, &vsi->state);
4217 i40e_napi_enable_all(vsi);
4218 i40e_vsi_enable_irq(vsi);
4219
4220 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4221 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004222 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004223 netif_tx_start_all_queues(vsi->netdev);
4224 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004225 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004226 i40e_print_link_message(vsi, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004227 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004228
4229 /* replay FDIR SB filters */
4230 if (vsi->type == I40E_VSI_FDIR)
4231 i40e_fdir_filter_restore(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004232 i40e_service_event_schedule(pf);
4233
4234 return 0;
4235}
4236
4237/**
4238 * i40e_vsi_reinit_locked - Reset the VSI
4239 * @vsi: the VSI being configured
4240 *
4241 * Rebuild the ring structs after some configuration
4242 * has changed, e.g. MTU size.
4243 **/
4244static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4245{
4246 struct i40e_pf *pf = vsi->back;
4247
4248 WARN_ON(in_interrupt());
4249 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4250 usleep_range(1000, 2000);
4251 i40e_down(vsi);
4252
4253 /* Give a VF some time to respond to the reset. The
4254 * two second wait is based upon the watchdog cycle in
4255 * the VF driver.
4256 */
4257 if (vsi->type == I40E_VSI_SRIOV)
4258 msleep(2000);
4259 i40e_up(vsi);
4260 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
4261}
4262
4263/**
4264 * i40e_up - Bring the connection back up after being down
4265 * @vsi: the VSI being configured
4266 **/
4267int i40e_up(struct i40e_vsi *vsi)
4268{
4269 int err;
4270
4271 err = i40e_vsi_configure(vsi);
4272 if (!err)
4273 err = i40e_up_complete(vsi);
4274
4275 return err;
4276}
4277
4278/**
4279 * i40e_down - Shutdown the connection processing
4280 * @vsi: the VSI being stopped
4281 **/
4282void i40e_down(struct i40e_vsi *vsi)
4283{
4284 int i;
4285
4286 /* It is assumed that the caller of this function
4287 * sets the vsi->state __I40E_DOWN bit.
4288 */
4289 if (vsi->netdev) {
4290 netif_carrier_off(vsi->netdev);
4291 netif_tx_disable(vsi->netdev);
4292 }
4293 i40e_vsi_disable_irq(vsi);
4294 i40e_vsi_control_rings(vsi, false);
4295 i40e_napi_disable_all(vsi);
4296
4297 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004298 i40e_clean_tx_ring(vsi->tx_rings[i]);
4299 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004300 }
4301}
4302
4303/**
4304 * i40e_setup_tc - configure multiple traffic classes
4305 * @netdev: net device to configure
4306 * @tc: number of traffic classes to enable
4307 **/
4308static int i40e_setup_tc(struct net_device *netdev, u8 tc)
4309{
4310 struct i40e_netdev_priv *np = netdev_priv(netdev);
4311 struct i40e_vsi *vsi = np->vsi;
4312 struct i40e_pf *pf = vsi->back;
4313 u8 enabled_tc = 0;
4314 int ret = -EINVAL;
4315 int i;
4316
4317 /* Check if DCB enabled to continue */
4318 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
4319 netdev_info(netdev, "DCB is not enabled for adapter\n");
4320 goto exit;
4321 }
4322
4323 /* Check if MFP enabled */
4324 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4325 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
4326 goto exit;
4327 }
4328
4329 /* Check whether tc count is within enabled limit */
4330 if (tc > i40e_pf_get_num_tc(pf)) {
4331 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
4332 goto exit;
4333 }
4334
4335 /* Generate TC map for number of tc requested */
4336 for (i = 0; i < tc; i++)
4337 enabled_tc |= (1 << i);
4338
4339 /* Requesting same TC configuration as already enabled */
4340 if (enabled_tc == vsi->tc_config.enabled_tc)
4341 return 0;
4342
4343 /* Quiesce VSI queues */
4344 i40e_quiesce_vsi(vsi);
4345
4346 /* Configure VSI for enabled TCs */
4347 ret = i40e_vsi_config_tc(vsi, enabled_tc);
4348 if (ret) {
4349 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
4350 vsi->seid);
4351 goto exit;
4352 }
4353
4354 /* Unquiesce VSI */
4355 i40e_unquiesce_vsi(vsi);
4356
4357exit:
4358 return ret;
4359}
4360
4361/**
4362 * i40e_open - Called when a network interface is made active
4363 * @netdev: network interface device structure
4364 *
4365 * The open entry point is called when a network interface is made
4366 * active by the system (IFF_UP). At this point all resources needed
4367 * for transmit and receive operations are allocated, the interrupt
4368 * handler is registered with the OS, the netdev watchdog subtask is
4369 * enabled, and the stack is notified that the interface is ready.
4370 *
4371 * Returns 0 on success, negative value on failure
4372 **/
4373static int i40e_open(struct net_device *netdev)
4374{
4375 struct i40e_netdev_priv *np = netdev_priv(netdev);
4376 struct i40e_vsi *vsi = np->vsi;
4377 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004378 int err;
4379
Shannon Nelson4eb3f762014-03-06 08:59:58 +00004380 /* disallow open during test or if eeprom is broken */
4381 if (test_bit(__I40E_TESTING, &pf->state) ||
4382 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004383 return -EBUSY;
4384
4385 netif_carrier_off(netdev);
4386
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004387 err = i40e_vsi_open(vsi);
4388 if (err)
4389 return err;
4390
Jesse Brandeburg059dab62014-04-01 09:07:20 +00004391 /* configure global TSO hardware offload settings */
4392 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
4393 TCP_FLAG_FIN) >> 16);
4394 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
4395 TCP_FLAG_FIN |
4396 TCP_FLAG_CWR) >> 16);
4397 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
4398
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004399#ifdef CONFIG_I40E_VXLAN
4400 vxlan_get_rx_port(netdev);
4401#endif
4402
4403 return 0;
4404}
4405
4406/**
4407 * i40e_vsi_open -
4408 * @vsi: the VSI to open
4409 *
4410 * Finish initialization of the VSI.
4411 *
4412 * Returns 0 on success, negative value on failure
4413 **/
4414int i40e_vsi_open(struct i40e_vsi *vsi)
4415{
4416 struct i40e_pf *pf = vsi->back;
4417 char int_name[IFNAMSIZ];
4418 int err;
4419
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004420 /* allocate descriptors */
4421 err = i40e_vsi_setup_tx_resources(vsi);
4422 if (err)
4423 goto err_setup_tx;
4424 err = i40e_vsi_setup_rx_resources(vsi);
4425 if (err)
4426 goto err_setup_rx;
4427
4428 err = i40e_vsi_configure(vsi);
4429 if (err)
4430 goto err_setup_rx;
4431
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004432 if (vsi->netdev) {
4433 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4434 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
4435 err = i40e_vsi_request_irq(vsi, int_name);
4436 if (err)
4437 goto err_setup_rx;
4438
4439 /* Notify the stack of the actual queue counts. */
4440 err = netif_set_real_num_tx_queues(vsi->netdev,
4441 vsi->num_queue_pairs);
4442 if (err)
4443 goto err_set_queues;
4444
4445 err = netif_set_real_num_rx_queues(vsi->netdev,
4446 vsi->num_queue_pairs);
4447 if (err)
4448 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00004449
4450 } else if (vsi->type == I40E_VSI_FDIR) {
4451 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4452 dev_driver_string(&pf->pdev->dev));
4453 err = i40e_vsi_request_irq(vsi, int_name);
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004454 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00004455 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004456 goto err_setup_rx;
4457 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004458
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004459 err = i40e_up_complete(vsi);
4460 if (err)
4461 goto err_up_complete;
4462
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004463 return 0;
4464
4465err_up_complete:
4466 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004467err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004468 i40e_vsi_free_irq(vsi);
4469err_setup_rx:
4470 i40e_vsi_free_rx_resources(vsi);
4471err_setup_tx:
4472 i40e_vsi_free_tx_resources(vsi);
4473 if (vsi == pf->vsi[pf->lan_vsi])
4474 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4475
4476 return err;
4477}
4478
4479/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00004480 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
4481 * @pf: Pointer to pf
4482 *
4483 * This function destroys the hlist where all the Flow Director
4484 * filters were saved.
4485 **/
4486static void i40e_fdir_filter_exit(struct i40e_pf *pf)
4487{
4488 struct i40e_fdir_filter *filter;
4489 struct hlist_node *node2;
4490
4491 hlist_for_each_entry_safe(filter, node2,
4492 &pf->fdir_filter_list, fdir_node) {
4493 hlist_del(&filter->fdir_node);
4494 kfree(filter);
4495 }
4496 pf->fdir_pf_active_filters = 0;
4497}
4498
4499/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004500 * i40e_close - Disables a network interface
4501 * @netdev: network interface device structure
4502 *
4503 * The close entry point is called when an interface is de-activated
4504 * by the OS. The hardware is still under the driver's control, but
4505 * this netdev interface is disabled.
4506 *
4507 * Returns 0, this is not allowed to fail
4508 **/
4509static int i40e_close(struct net_device *netdev)
4510{
4511 struct i40e_netdev_priv *np = netdev_priv(netdev);
4512 struct i40e_vsi *vsi = np->vsi;
4513
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004514 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004515
4516 return 0;
4517}
4518
4519/**
4520 * i40e_do_reset - Start a PF or Core Reset sequence
4521 * @pf: board private structure
4522 * @reset_flags: which reset is requested
4523 *
4524 * The essential difference in resets is that the PF Reset
4525 * doesn't clear the packet buffers, doesn't reset the PE
4526 * firmware, and doesn't bother the other PFs on the chip.
4527 **/
4528void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4529{
4530 u32 val;
4531
4532 WARN_ON(in_interrupt());
4533
Mitch Williams263fc482014-04-23 04:50:11 +00004534 if (i40e_check_asq_alive(&pf->hw))
4535 i40e_vc_notify_reset(pf);
4536
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004537 /* do the biggest reset indicated */
4538 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4539
4540 /* Request a Global Reset
4541 *
4542 * This will start the chip's countdown to the actual full
4543 * chip reset event, and a warning interrupt to be sent
4544 * to all PFs, including the requestor. Our handler
4545 * for the warning interrupt will deal with the shutdown
4546 * and recovery of the switch setup.
4547 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004548 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004549 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4550 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4551 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4552
4553 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4554
4555 /* Request a Core Reset
4556 *
4557 * Same as Global Reset, except does *not* include the MAC/PHY
4558 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004559 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004560 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4561 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4562 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4563 i40e_flush(&pf->hw);
4564
Shannon Nelson7823fe32013-11-16 10:00:45 +00004565 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4566
4567 /* Request a Firmware Reset
4568 *
4569 * Same as Global reset, plus restarting the
4570 * embedded firmware engine.
4571 */
4572 /* enable EMP Reset */
4573 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4574 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4575 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4576
4577 /* force the reset */
4578 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4579 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4580 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4581 i40e_flush(&pf->hw);
4582
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004583 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4584
4585 /* Request a PF Reset
4586 *
4587 * Resets only the PF-specific registers
4588 *
4589 * This goes directly to the tear-down and rebuild of
4590 * the switch, since we need to do all the recovery as
4591 * for the Core Reset.
4592 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004593 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004594 i40e_handle_reset_warning(pf);
4595
4596 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4597 int v;
4598
4599 /* Find the VSI(s) that requested a re-init */
4600 dev_info(&pf->pdev->dev,
4601 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00004602 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004603 struct i40e_vsi *vsi = pf->vsi[v];
4604 if (vsi != NULL &&
4605 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4606 i40e_vsi_reinit_locked(pf->vsi[v]);
4607 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4608 }
4609 }
4610
4611 /* no further action needed, so return now */
4612 return;
4613 } else {
4614 dev_info(&pf->pdev->dev,
4615 "bad reset request 0x%08x\n", reset_flags);
4616 return;
4617 }
4618}
4619
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004620#ifdef CONFIG_I40E_DCB
4621/**
4622 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
4623 * @pf: board private structure
4624 * @old_cfg: current DCB config
4625 * @new_cfg: new DCB config
4626 **/
4627bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
4628 struct i40e_dcbx_config *old_cfg,
4629 struct i40e_dcbx_config *new_cfg)
4630{
4631 bool need_reconfig = false;
4632
4633 /* Check if ETS configuration has changed */
4634 if (memcmp(&new_cfg->etscfg,
4635 &old_cfg->etscfg,
4636 sizeof(new_cfg->etscfg))) {
4637 /* If Priority Table has changed reconfig is needed */
4638 if (memcmp(&new_cfg->etscfg.prioritytable,
4639 &old_cfg->etscfg.prioritytable,
4640 sizeof(new_cfg->etscfg.prioritytable))) {
4641 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004642 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004643 }
4644
4645 if (memcmp(&new_cfg->etscfg.tcbwtable,
4646 &old_cfg->etscfg.tcbwtable,
4647 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004648 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004649
4650 if (memcmp(&new_cfg->etscfg.tsatable,
4651 &old_cfg->etscfg.tsatable,
4652 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004653 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004654 }
4655
4656 /* Check if PFC configuration has changed */
4657 if (memcmp(&new_cfg->pfc,
4658 &old_cfg->pfc,
4659 sizeof(new_cfg->pfc))) {
4660 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004661 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004662 }
4663
4664 /* Check if APP Table has changed */
4665 if (memcmp(&new_cfg->app,
4666 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05004667 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004668 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004669 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05004670 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004671
4672 return need_reconfig;
4673}
4674
4675/**
4676 * i40e_handle_lldp_event - Handle LLDP Change MIB event
4677 * @pf: board private structure
4678 * @e: event info posted on ARQ
4679 **/
4680static int i40e_handle_lldp_event(struct i40e_pf *pf,
4681 struct i40e_arq_event_info *e)
4682{
4683 struct i40e_aqc_lldp_get_mib *mib =
4684 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
4685 struct i40e_hw *hw = &pf->hw;
4686 struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
4687 struct i40e_dcbx_config tmp_dcbx_cfg;
4688 bool need_reconfig = false;
4689 int ret = 0;
4690 u8 type;
4691
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004692 /* Not DCB capable or capability disabled */
4693 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
4694 return ret;
4695
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004696 /* Ignore if event is not for Nearest Bridge */
4697 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
4698 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
4699 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
4700 return ret;
4701
4702 /* Check MIB Type and return if event for Remote MIB update */
4703 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
4704 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
4705 /* Update the remote cached instance and return */
4706 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
4707 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
4708 &hw->remote_dcbx_config);
4709 goto exit;
4710 }
4711
4712 /* Convert/store the DCBX data from LLDPDU temporarily */
4713 memset(&tmp_dcbx_cfg, 0, sizeof(tmp_dcbx_cfg));
4714 ret = i40e_lldp_to_dcb_config(e->msg_buf, &tmp_dcbx_cfg);
4715 if (ret) {
4716 /* Error in LLDPDU parsing return */
4717 dev_info(&pf->pdev->dev, "Failed parsing LLDPDU from event buffer\n");
4718 goto exit;
4719 }
4720
4721 /* No change detected in DCBX configs */
4722 if (!memcmp(&tmp_dcbx_cfg, dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004723 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004724 goto exit;
4725 }
4726
4727 need_reconfig = i40e_dcb_need_reconfig(pf, dcbx_cfg, &tmp_dcbx_cfg);
4728
4729 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg);
4730
4731 /* Overwrite the new configuration */
4732 *dcbx_cfg = tmp_dcbx_cfg;
4733
4734 if (!need_reconfig)
4735 goto exit;
4736
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004737 /* Enable DCB tagging only when more than one TC */
4738 if (i40e_dcb_get_num_tc(dcbx_cfg) > 1)
4739 pf->flags |= I40E_FLAG_DCB_ENABLED;
4740 else
4741 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
4742
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004743 /* Reconfiguration needed quiesce all VSIs */
4744 i40e_pf_quiesce_all_vsi(pf);
4745
4746 /* Changes in configuration update VEB/VSI */
4747 i40e_dcb_reconfigure(pf);
4748
4749 i40e_pf_unquiesce_all_vsi(pf);
4750exit:
4751 return ret;
4752}
4753#endif /* CONFIG_I40E_DCB */
4754
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004755/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004756 * i40e_do_reset_safe - Protected reset path for userland calls.
4757 * @pf: board private structure
4758 * @reset_flags: which reset is requested
4759 *
4760 **/
4761void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4762{
4763 rtnl_lock();
4764 i40e_do_reset(pf, reset_flags);
4765 rtnl_unlock();
4766}
4767
4768/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004769 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4770 * @pf: board private structure
4771 * @e: event info posted on ARQ
4772 *
4773 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4774 * and VF queues
4775 **/
4776static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4777 struct i40e_arq_event_info *e)
4778{
4779 struct i40e_aqc_lan_overflow *data =
4780 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4781 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4782 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4783 struct i40e_hw *hw = &pf->hw;
4784 struct i40e_vf *vf;
4785 u16 vf_id;
4786
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004787 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
4788 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004789
4790 /* Queue belongs to VF, find the VF and issue VF reset */
4791 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4792 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4793 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4794 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4795 vf_id -= hw->func_caps.vf_base_id;
4796 vf = &pf->vf[vf_id];
4797 i40e_vc_notify_vf_reset(vf);
4798 /* Allow VF to process pending reset notification */
4799 msleep(20);
4800 i40e_reset_vf(vf, false);
4801 }
4802}
4803
4804/**
4805 * i40e_service_event_complete - Finish up the service event
4806 * @pf: board private structure
4807 **/
4808static void i40e_service_event_complete(struct i40e_pf *pf)
4809{
4810 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4811
4812 /* flush memory to make sure state is correct before next watchog */
4813 smp_mb__before_clear_bit();
4814 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4815}
4816
4817/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004818 * i40e_get_current_fd_count - Get the count of FD filters programmed in the HW
4819 * @pf: board private structure
4820 **/
4821int i40e_get_current_fd_count(struct i40e_pf *pf)
4822{
4823 int val, fcnt_prog;
4824 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
4825 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
4826 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
4827 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
4828 return fcnt_prog;
4829}
4830
4831/**
4832 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
4833 * @pf: board private structure
4834 **/
4835void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
4836{
4837 u32 fcnt_prog, fcnt_avail;
4838
4839 /* Check if, FD SB or ATR was auto disabled and if there is enough room
4840 * to re-enable
4841 */
4842 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4843 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4844 return;
4845 fcnt_prog = i40e_get_current_fd_count(pf);
Anjali Singhai Jain89132782014-04-09 05:59:01 +00004846 fcnt_avail = i40e_get_fd_cnt_all(pf);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004847 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) {
4848 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
4849 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
4850 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
4851 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
4852 }
4853 }
4854 /* Wait for some more space to be available to turn on ATR */
4855 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
4856 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4857 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
4858 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
4859 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
4860 }
4861 }
4862}
4863
4864/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004865 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4866 * @pf: board private structure
4867 **/
4868static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4869{
4870 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4871 return;
4872
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004873 /* if interface is down do nothing */
4874 if (test_bit(__I40E_DOWN, &pf->state))
4875 return;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004876 i40e_fdir_check_and_reenable(pf);
4877
4878 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4879 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4880 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004881}
4882
4883/**
4884 * i40e_vsi_link_event - notify VSI of a link event
4885 * @vsi: vsi to be notified
4886 * @link_up: link up or down
4887 **/
4888static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4889{
4890 if (!vsi)
4891 return;
4892
4893 switch (vsi->type) {
4894 case I40E_VSI_MAIN:
4895 if (!vsi->netdev || !vsi->netdev_registered)
4896 break;
4897
4898 if (link_up) {
4899 netif_carrier_on(vsi->netdev);
4900 netif_tx_wake_all_queues(vsi->netdev);
4901 } else {
4902 netif_carrier_off(vsi->netdev);
4903 netif_tx_stop_all_queues(vsi->netdev);
4904 }
4905 break;
4906
4907 case I40E_VSI_SRIOV:
4908 break;
4909
4910 case I40E_VSI_VMDQ2:
4911 case I40E_VSI_CTRL:
4912 case I40E_VSI_MIRROR:
4913 default:
4914 /* there is no notification for other VSIs */
4915 break;
4916 }
4917}
4918
4919/**
4920 * i40e_veb_link_event - notify elements on the veb of a link event
4921 * @veb: veb to be notified
4922 * @link_up: link up or down
4923 **/
4924static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4925{
4926 struct i40e_pf *pf;
4927 int i;
4928
4929 if (!veb || !veb->pf)
4930 return;
4931 pf = veb->pf;
4932
4933 /* depth first... */
4934 for (i = 0; i < I40E_MAX_VEB; i++)
4935 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4936 i40e_veb_link_event(pf->veb[i], link_up);
4937
4938 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00004939 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004940 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4941 i40e_vsi_link_event(pf->vsi[i], link_up);
4942}
4943
4944/**
4945 * i40e_link_event - Update netif_carrier status
4946 * @pf: board private structure
4947 **/
4948static void i40e_link_event(struct i40e_pf *pf)
4949{
4950 bool new_link, old_link;
4951
4952 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4953 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4954
4955 if (new_link == old_link)
4956 return;
Anjali Singhai6d779b42013-09-28 06:00:02 +00004957 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004958 i40e_print_link_message(pf->vsi[pf->lan_vsi], new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004959
4960 /* Notify the base of the switch tree connected to
4961 * the link. Floating VEBs are not notified.
4962 */
4963 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4964 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4965 else
4966 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4967
4968 if (pf->vf)
4969 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004970
4971 if (pf->flags & I40E_FLAG_PTP)
4972 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004973}
4974
4975/**
4976 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
4977 * @pf: board private structure
4978 *
4979 * Set the per-queue flags to request a check for stuck queues in the irq
4980 * clean functions, then force interrupts to be sure the irq clean is called.
4981 **/
4982static void i40e_check_hang_subtask(struct i40e_pf *pf)
4983{
4984 int i, v;
4985
4986 /* If we're down or resetting, just bail */
4987 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
4988 return;
4989
4990 /* for each VSI/netdev
4991 * for each Tx queue
4992 * set the check flag
4993 * for each q_vector
4994 * force an interrupt
4995 */
Mitch Williams505682c2014-05-20 08:01:37 +00004996 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004997 struct i40e_vsi *vsi = pf->vsi[v];
4998 int armed = 0;
4999
5000 if (!pf->vsi[v] ||
5001 test_bit(__I40E_DOWN, &vsi->state) ||
5002 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
5003 continue;
5004
5005 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005006 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005007 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005008 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005009 armed++;
5010 }
5011
5012 if (armed) {
5013 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
5014 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
5015 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
5016 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
5017 } else {
5018 u16 vec = vsi->base_vector - 1;
5019 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
5020 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
5021 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
5022 wr32(&vsi->back->hw,
5023 I40E_PFINT_DYN_CTLN(vec), val);
5024 }
5025 i40e_flush(&vsi->back->hw);
5026 }
5027 }
5028}
5029
5030/**
5031 * i40e_watchdog_subtask - Check and bring link up
5032 * @pf: board private structure
5033 **/
5034static void i40e_watchdog_subtask(struct i40e_pf *pf)
5035{
5036 int i;
5037
5038 /* if interface is down do nothing */
5039 if (test_bit(__I40E_DOWN, &pf->state) ||
5040 test_bit(__I40E_CONFIG_BUSY, &pf->state))
5041 return;
5042
5043 /* Update the stats for active netdevs so the network stack
5044 * can look at updated numbers whenever it cares to
5045 */
Mitch Williams505682c2014-05-20 08:01:37 +00005046 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005047 if (pf->vsi[i] && pf->vsi[i]->netdev)
5048 i40e_update_stats(pf->vsi[i]);
5049
5050 /* Update the stats for the active switching components */
5051 for (i = 0; i < I40E_MAX_VEB; i++)
5052 if (pf->veb[i])
5053 i40e_update_veb_stats(pf->veb[i]);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005054
5055 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005056}
5057
5058/**
5059 * i40e_reset_subtask - Set up for resetting the device and driver
5060 * @pf: board private structure
5061 **/
5062static void i40e_reset_subtask(struct i40e_pf *pf)
5063{
5064 u32 reset_flags = 0;
5065
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005066 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005067 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
5068 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
5069 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
5070 }
5071 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
5072 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
5073 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5074 }
5075 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
5076 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
5077 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
5078 }
5079 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
5080 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
5081 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
5082 }
5083
5084 /* If there's a recovery already waiting, it takes
5085 * precedence before starting a new reset sequence.
5086 */
5087 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
5088 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005089 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005090 }
5091
5092 /* If we're already down or resetting, just bail */
5093 if (reset_flags &&
5094 !test_bit(__I40E_DOWN, &pf->state) &&
5095 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
5096 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005097
5098unlock:
5099 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005100}
5101
5102/**
5103 * i40e_handle_link_event - Handle link event
5104 * @pf: board private structure
5105 * @e: event info posted on ARQ
5106 **/
5107static void i40e_handle_link_event(struct i40e_pf *pf,
5108 struct i40e_arq_event_info *e)
5109{
5110 struct i40e_hw *hw = &pf->hw;
5111 struct i40e_aqc_get_link_status *status =
5112 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
5113 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
5114
5115 /* save off old link status information */
5116 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
5117 sizeof(pf->hw.phy.link_info_old));
5118
5119 /* update link status */
5120 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
5121 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
5122 hw_link_info->link_info = status->link_info;
5123 hw_link_info->an_info = status->an_info;
5124 hw_link_info->ext_info = status->ext_info;
5125 hw_link_info->lse_enable =
5126 le16_to_cpu(status->command_flags) &
5127 I40E_AQ_LSE_ENABLE;
5128
5129 /* process the event */
5130 i40e_link_event(pf);
5131
5132 /* Do a new status request to re-enable LSE reporting
5133 * and load new status information into the hw struct,
5134 * then see if the status changed while processing the
5135 * initial event.
5136 */
5137 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
5138 i40e_link_event(pf);
5139}
5140
5141/**
5142 * i40e_clean_adminq_subtask - Clean the AdminQ rings
5143 * @pf: board private structure
5144 **/
5145static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
5146{
5147 struct i40e_arq_event_info event;
5148 struct i40e_hw *hw = &pf->hw;
5149 u16 pending, i = 0;
5150 i40e_status ret;
5151 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00005152 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005153 u32 val;
5154
5155 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
5156 return;
5157
Shannon Nelson86df2422014-05-20 08:01:35 +00005158 /* check for error indications */
5159 val = rd32(&pf->hw, pf->hw.aq.arq.len);
5160 oldval = val;
5161 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
5162 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
5163 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
5164 }
5165 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
5166 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
5167 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
5168 }
5169 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
5170 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
5171 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
5172 }
5173 if (oldval != val)
5174 wr32(&pf->hw, pf->hw.aq.arq.len, val);
5175
5176 val = rd32(&pf->hw, pf->hw.aq.asq.len);
5177 oldval = val;
5178 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
5179 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
5180 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
5181 }
5182 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
5183 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
5184 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
5185 }
5186 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
5187 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
5188 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
5189 }
5190 if (oldval != val)
5191 wr32(&pf->hw, pf->hw.aq.asq.len, val);
5192
Mitch Williams3197ce22013-11-28 06:39:39 +00005193 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005194 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
5195 if (!event.msg_buf)
5196 return;
5197
5198 do {
Mitch Williams2f019122013-11-28 06:39:33 +00005199 event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005200 ret = i40e_clean_arq_element(hw, &event, &pending);
5201 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
5202 dev_info(&pf->pdev->dev, "No ARQ event found\n");
5203 break;
5204 } else if (ret) {
5205 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
5206 break;
5207 }
5208
5209 opcode = le16_to_cpu(event.desc.opcode);
5210 switch (opcode) {
5211
5212 case i40e_aqc_opc_get_link_status:
5213 i40e_handle_link_event(pf, &event);
5214 break;
5215 case i40e_aqc_opc_send_msg_to_pf:
5216 ret = i40e_vc_process_vf_msg(pf,
5217 le16_to_cpu(event.desc.retval),
5218 le32_to_cpu(event.desc.cookie_high),
5219 le32_to_cpu(event.desc.cookie_low),
5220 event.msg_buf,
5221 event.msg_size);
5222 break;
5223 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005224 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005225#ifdef CONFIG_I40E_DCB
5226 rtnl_lock();
5227 ret = i40e_handle_lldp_event(pf, &event);
5228 rtnl_unlock();
5229#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005230 break;
5231 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005232 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005233 i40e_handle_lan_overflow_event(pf, &event);
5234 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00005235 case i40e_aqc_opc_send_msg_to_peer:
5236 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
5237 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005238 default:
5239 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00005240 "ARQ Error: Unknown event 0x%04x received\n",
5241 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005242 break;
5243 }
5244 } while (pending && (i++ < pf->adminq_work_limit));
5245
5246 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
5247 /* re-enable Admin queue interrupt cause */
5248 val = rd32(hw, I40E_PFINT_ICR0_ENA);
5249 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
5250 wr32(hw, I40E_PFINT_ICR0_ENA, val);
5251 i40e_flush(hw);
5252
5253 kfree(event.msg_buf);
5254}
5255
5256/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005257 * i40e_verify_eeprom - make sure eeprom is good to use
5258 * @pf: board private structure
5259 **/
5260static void i40e_verify_eeprom(struct i40e_pf *pf)
5261{
5262 int err;
5263
5264 err = i40e_diag_eeprom_test(&pf->hw);
5265 if (err) {
5266 /* retry in case of garbage read */
5267 err = i40e_diag_eeprom_test(&pf->hw);
5268 if (err) {
5269 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
5270 err);
5271 set_bit(__I40E_BAD_EEPROM, &pf->state);
5272 }
5273 }
5274
5275 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
5276 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
5277 clear_bit(__I40E_BAD_EEPROM, &pf->state);
5278 }
5279}
5280
5281/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005282 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
5283 * @veb: pointer to the VEB instance
5284 *
5285 * This is a recursive function that first builds the attached VSIs then
5286 * recurses in to build the next layer of VEB. We track the connections
5287 * through our own index numbers because the seid's from the HW could
5288 * change across the reset.
5289 **/
5290static int i40e_reconstitute_veb(struct i40e_veb *veb)
5291{
5292 struct i40e_vsi *ctl_vsi = NULL;
5293 struct i40e_pf *pf = veb->pf;
5294 int v, veb_idx;
5295 int ret;
5296
5297 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00005298 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005299 if (pf->vsi[v] &&
5300 pf->vsi[v]->veb_idx == veb->idx &&
5301 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
5302 ctl_vsi = pf->vsi[v];
5303 break;
5304 }
5305 }
5306 if (!ctl_vsi) {
5307 dev_info(&pf->pdev->dev,
5308 "missing owner VSI for veb_idx %d\n", veb->idx);
5309 ret = -ENOENT;
5310 goto end_reconstitute;
5311 }
5312 if (ctl_vsi != pf->vsi[pf->lan_vsi])
5313 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5314 ret = i40e_add_vsi(ctl_vsi);
5315 if (ret) {
5316 dev_info(&pf->pdev->dev,
5317 "rebuild of owner VSI failed: %d\n", ret);
5318 goto end_reconstitute;
5319 }
5320 i40e_vsi_reset_stats(ctl_vsi);
5321
5322 /* create the VEB in the switch and move the VSI onto the VEB */
5323 ret = i40e_add_veb(veb, ctl_vsi);
5324 if (ret)
5325 goto end_reconstitute;
5326
5327 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00005328 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005329 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
5330 continue;
5331
5332 if (pf->vsi[v]->veb_idx == veb->idx) {
5333 struct i40e_vsi *vsi = pf->vsi[v];
5334 vsi->uplink_seid = veb->seid;
5335 ret = i40e_add_vsi(vsi);
5336 if (ret) {
5337 dev_info(&pf->pdev->dev,
5338 "rebuild of vsi_idx %d failed: %d\n",
5339 v, ret);
5340 goto end_reconstitute;
5341 }
5342 i40e_vsi_reset_stats(vsi);
5343 }
5344 }
5345
5346 /* create any VEBs attached to this VEB - RECURSION */
5347 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
5348 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
5349 pf->veb[veb_idx]->uplink_seid = veb->seid;
5350 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
5351 if (ret)
5352 break;
5353 }
5354 }
5355
5356end_reconstitute:
5357 return ret;
5358}
5359
5360/**
5361 * i40e_get_capabilities - get info about the HW
5362 * @pf: the PF struct
5363 **/
5364static int i40e_get_capabilities(struct i40e_pf *pf)
5365{
5366 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
5367 u16 data_size;
5368 int buf_len;
5369 int err;
5370
5371 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
5372 do {
5373 cap_buf = kzalloc(buf_len, GFP_KERNEL);
5374 if (!cap_buf)
5375 return -ENOMEM;
5376
5377 /* this loads the data into the hw struct for us */
5378 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
5379 &data_size,
5380 i40e_aqc_opc_list_func_capabilities,
5381 NULL);
5382 /* data loaded, buffer no longer needed */
5383 kfree(cap_buf);
5384
5385 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
5386 /* retry with a larger buffer */
5387 buf_len = data_size;
5388 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
5389 dev_info(&pf->pdev->dev,
5390 "capability discovery failed: aq=%d\n",
5391 pf->hw.aq.asq_last_status);
5392 return -ENODEV;
5393 }
5394 } while (err);
5395
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00005396 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
5397 (pf->hw.aq.fw_maj_ver < 2)) {
5398 pf->hw.func_caps.num_msix_vectors++;
5399 pf->hw.func_caps.num_msix_vectors_vf++;
5400 }
5401
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005402 if (pf->hw.debug_mask & I40E_DEBUG_USER)
5403 dev_info(&pf->pdev->dev,
5404 "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",
5405 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
5406 pf->hw.func_caps.num_msix_vectors,
5407 pf->hw.func_caps.num_msix_vectors_vf,
5408 pf->hw.func_caps.fd_filters_guaranteed,
5409 pf->hw.func_caps.fd_filters_best_effort,
5410 pf->hw.func_caps.num_tx_qp,
5411 pf->hw.func_caps.num_vsis);
5412
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005413#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
5414 + pf->hw.func_caps.num_vfs)
5415 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
5416 dev_info(&pf->pdev->dev,
5417 "got num_vsis %d, setting num_vsis to %d\n",
5418 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
5419 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
5420 }
5421
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005422 return 0;
5423}
5424
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005425static int i40e_vsi_clear(struct i40e_vsi *vsi);
5426
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005427/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005428 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005429 * @pf: board private structure
5430 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005431static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005432{
5433 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005434 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005435
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005436 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005437 return;
5438
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005439 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005440 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00005441 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005442 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005443 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005444 break;
5445 }
5446 }
5447
5448 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005449 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005450 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
5451 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005452 if (!vsi) {
5453 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005454 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
5455 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005456 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005457 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005458
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005459 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005460}
5461
5462/**
5463 * i40e_fdir_teardown - release the Flow Director resources
5464 * @pf: board private structure
5465 **/
5466static void i40e_fdir_teardown(struct i40e_pf *pf)
5467{
5468 int i;
5469
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005470 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00005471 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005472 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
5473 i40e_vsi_release(pf->vsi[i]);
5474 break;
5475 }
5476 }
5477}
5478
5479/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005480 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005481 * @pf: board private structure
5482 *
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005483 * Close up the VFs and other things in prep for pf Reset.
5484 **/
5485static int i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005486{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005487 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00005488 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005489 u32 v;
5490
5491 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
5492 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005493 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005494
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005495 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005496
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005497 /* quiesce the VSIs and their queues that are not already DOWN */
5498 i40e_pf_quiesce_all_vsi(pf);
5499
Mitch Williams505682c2014-05-20 08:01:37 +00005500 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005501 if (pf->vsi[v])
5502 pf->vsi[v]->seid = 0;
5503 }
5504
5505 i40e_shutdown_adminq(&pf->hw);
5506
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005507 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00005508 if (hw->hmc.hmc_obj) {
5509 ret = i40e_shutdown_lan_hmc(hw);
5510 if (ret) {
5511 dev_warn(&pf->pdev->dev,
5512 "shutdown_lan_hmc failed: %d\n", ret);
5513 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5514 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005515 }
5516 return ret;
5517}
5518
5519/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005520 * i40e_send_version - update firmware with driver version
5521 * @pf: PF struct
5522 */
5523static void i40e_send_version(struct i40e_pf *pf)
5524{
5525 struct i40e_driver_version dv;
5526
5527 dv.major_version = DRV_VERSION_MAJOR;
5528 dv.minor_version = DRV_VERSION_MINOR;
5529 dv.build_version = DRV_VERSION_BUILD;
5530 dv.subbuild_version = 0;
5531 strncpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
5532 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
5533}
5534
5535/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00005536 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005537 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005538 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005539 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005540static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005541{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005542 struct i40e_hw *hw = &pf->hw;
5543 i40e_status ret;
5544 u32 v;
5545
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005546 /* Now we wait for GRST to settle out.
5547 * We don't have to delete the VEBs or VSIs from the hw switch
5548 * because the reset will make them disappear.
5549 */
5550 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005551 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005552 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005553 goto end_core_reset;
5554 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005555 pf->pfr_count++;
5556
5557 if (test_bit(__I40E_DOWN, &pf->state))
5558 goto end_core_reset;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005559 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005560
5561 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
5562 ret = i40e_init_adminq(&pf->hw);
5563 if (ret) {
5564 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
5565 goto end_core_reset;
5566 }
5567
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005568 /* re-verify the eeprom if we just had an EMP reset */
5569 if (test_bit(__I40E_EMP_RESET_REQUESTED, &pf->state)) {
5570 clear_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
5571 i40e_verify_eeprom(pf);
5572 }
5573
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00005574 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005575 ret = i40e_get_capabilities(pf);
5576 if (ret) {
5577 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
5578 ret);
5579 goto end_core_reset;
5580 }
5581
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005582 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
5583 hw->func_caps.num_rx_qp,
5584 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
5585 if (ret) {
5586 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
5587 goto end_core_reset;
5588 }
5589 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
5590 if (ret) {
5591 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
5592 goto end_core_reset;
5593 }
5594
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005595#ifdef CONFIG_I40E_DCB
5596 ret = i40e_init_pf_dcb(pf);
5597 if (ret) {
5598 dev_info(&pf->pdev->dev, "init_pf_dcb failed: %d\n", ret);
5599 goto end_core_reset;
5600 }
5601#endif /* CONFIG_I40E_DCB */
5602
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005603 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005604 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005605 if (ret)
5606 goto end_core_reset;
5607
5608 /* Rebuild the VSIs and VEBs that existed before reset.
5609 * They are still in our local switch element arrays, so only
5610 * need to rebuild the switch model in the HW.
5611 *
5612 * If there were VEBs but the reconstitution failed, we'll try
5613 * try to recover minimal use by getting the basic PF VSI working.
5614 */
5615 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005616 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005617 /* find the one VEB connected to the MAC, and find orphans */
5618 for (v = 0; v < I40E_MAX_VEB; v++) {
5619 if (!pf->veb[v])
5620 continue;
5621
5622 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
5623 pf->veb[v]->uplink_seid == 0) {
5624 ret = i40e_reconstitute_veb(pf->veb[v]);
5625
5626 if (!ret)
5627 continue;
5628
5629 /* If Main VEB failed, we're in deep doodoo,
5630 * so give up rebuilding the switch and set up
5631 * for minimal rebuild of PF VSI.
5632 * If orphan failed, we'll report the error
5633 * but try to keep going.
5634 */
5635 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
5636 dev_info(&pf->pdev->dev,
5637 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
5638 ret);
5639 pf->vsi[pf->lan_vsi]->uplink_seid
5640 = pf->mac_seid;
5641 break;
5642 } else if (pf->veb[v]->uplink_seid == 0) {
5643 dev_info(&pf->pdev->dev,
5644 "rebuild of orphan VEB failed: %d\n",
5645 ret);
5646 }
5647 }
5648 }
5649 }
5650
5651 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
5652 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
5653 /* no VEB, so rebuild only the Main VSI */
5654 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
5655 if (ret) {
5656 dev_info(&pf->pdev->dev,
5657 "rebuild of Main VSI failed: %d\n", ret);
5658 goto end_core_reset;
5659 }
5660 }
5661
5662 /* reinit the misc interrupt */
5663 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5664 ret = i40e_setup_misc_vector(pf);
5665
5666 /* restart the VSIs that were rebuilt and running before the reset */
5667 i40e_pf_unquiesce_all_vsi(pf);
5668
Mitch Williams69f64b22014-02-13 03:48:46 -08005669 if (pf->num_alloc_vfs) {
5670 for (v = 0; v < pf->num_alloc_vfs; v++)
5671 i40e_reset_vf(&pf->vf[v], true);
5672 }
5673
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005674 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005675 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005676
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005677end_core_reset:
5678 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5679}
5680
5681/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005682 * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
5683 * @pf: board private structure
5684 *
5685 * Close up the VFs and other things in prep for a Core Reset,
5686 * then get ready to rebuild the world.
5687 **/
5688static void i40e_handle_reset_warning(struct i40e_pf *pf)
5689{
5690 i40e_status ret;
5691
5692 ret = i40e_prep_for_reset(pf);
5693 if (!ret)
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005694 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005695}
5696
5697/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005698 * i40e_handle_mdd_event
5699 * @pf: pointer to the pf structure
5700 *
5701 * Called from the MDD irq handler to identify possibly malicious vfs
5702 **/
5703static void i40e_handle_mdd_event(struct i40e_pf *pf)
5704{
5705 struct i40e_hw *hw = &pf->hw;
5706 bool mdd_detected = false;
5707 struct i40e_vf *vf;
5708 u32 reg;
5709 int i;
5710
5711 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
5712 return;
5713
5714 /* find what triggered the MDD event */
5715 reg = rd32(hw, I40E_GL_MDET_TX);
5716 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
5717 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
5718 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
5719 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
5720 >> I40E_GL_MDET_TX_EVENT_SHIFT;
5721 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
5722 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
5723 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005724 "Malicious Driver Detection event 0x%02x on TX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005725 event, queue, func);
5726 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
5727 mdd_detected = true;
5728 }
5729 reg = rd32(hw, I40E_GL_MDET_RX);
5730 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
5731 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
5732 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
5733 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
5734 >> I40E_GL_MDET_RX_EVENT_SHIFT;
5735 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
5736 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
5737 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005738 "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005739 event, queue, func);
5740 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
5741 mdd_detected = true;
5742 }
5743
5744 /* see if one of the VFs needs its hand slapped */
5745 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
5746 vf = &(pf->vf[i]);
5747 reg = rd32(hw, I40E_VP_MDET_TX(i));
5748 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
5749 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
5750 vf->num_mdd_events++;
5751 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
5752 }
5753
5754 reg = rd32(hw, I40E_VP_MDET_RX(i));
5755 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
5756 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
5757 vf->num_mdd_events++;
5758 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
5759 }
5760
5761 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
5762 dev_info(&pf->pdev->dev,
5763 "Too many MDD events on VF %d, disabled\n", i);
5764 dev_info(&pf->pdev->dev,
5765 "Use PF Control I/F to re-enable the VF\n");
5766 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
5767 }
5768 }
5769
5770 /* re-enable mdd interrupt cause */
5771 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
5772 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
5773 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
5774 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
5775 i40e_flush(hw);
5776}
5777
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005778#ifdef CONFIG_I40E_VXLAN
5779/**
5780 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
5781 * @pf: board private structure
5782 **/
5783static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
5784{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005785 struct i40e_hw *hw = &pf->hw;
5786 i40e_status ret;
5787 u8 filter_index;
5788 __be16 port;
5789 int i;
5790
5791 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
5792 return;
5793
5794 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
5795
5796 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5797 if (pf->pending_vxlan_bitmap & (1 << i)) {
5798 pf->pending_vxlan_bitmap &= ~(1 << i);
5799 port = pf->vxlan_ports[i];
5800 ret = port ?
5801 i40e_aq_add_udp_tunnel(hw, ntohs(port),
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005802 I40E_AQC_TUNNEL_TYPE_VXLAN,
5803 &filter_index, NULL)
5804 : i40e_aq_del_udp_tunnel(hw, i, NULL);
5805
5806 if (ret) {
5807 dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
5808 port ? "adding" : "deleting",
5809 ntohs(port), port ? i : i);
5810
5811 pf->vxlan_ports[i] = 0;
5812 } else {
5813 dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
5814 port ? "Added" : "Deleted",
5815 ntohs(port), port ? i : filter_index);
5816 }
5817 }
5818 }
5819}
5820
5821#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005822/**
5823 * i40e_service_task - Run the driver's async subtasks
5824 * @work: pointer to work_struct containing our data
5825 **/
5826static void i40e_service_task(struct work_struct *work)
5827{
5828 struct i40e_pf *pf = container_of(work,
5829 struct i40e_pf,
5830 service_task);
5831 unsigned long start_time = jiffies;
5832
5833 i40e_reset_subtask(pf);
5834 i40e_handle_mdd_event(pf);
5835 i40e_vc_process_vflr_event(pf);
5836 i40e_watchdog_subtask(pf);
5837 i40e_fdir_reinit_subtask(pf);
5838 i40e_check_hang_subtask(pf);
5839 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005840#ifdef CONFIG_I40E_VXLAN
5841 i40e_sync_vxlan_filters_subtask(pf);
5842#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005843 i40e_clean_adminq_subtask(pf);
5844
5845 i40e_service_event_complete(pf);
5846
5847 /* If the tasks have taken longer than one timer cycle or there
5848 * is more work to be done, reschedule the service task now
5849 * rather than wait for the timer to tick again.
5850 */
5851 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
5852 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
5853 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
5854 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
5855 i40e_service_event_schedule(pf);
5856}
5857
5858/**
5859 * i40e_service_timer - timer callback
5860 * @data: pointer to PF struct
5861 **/
5862static void i40e_service_timer(unsigned long data)
5863{
5864 struct i40e_pf *pf = (struct i40e_pf *)data;
5865
5866 mod_timer(&pf->service_timer,
5867 round_jiffies(jiffies + pf->service_timer_period));
5868 i40e_service_event_schedule(pf);
5869}
5870
5871/**
5872 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5873 * @vsi: the VSI being configured
5874 **/
5875static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5876{
5877 struct i40e_pf *pf = vsi->back;
5878
5879 switch (vsi->type) {
5880 case I40E_VSI_MAIN:
5881 vsi->alloc_queue_pairs = pf->num_lan_qps;
5882 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5883 I40E_REQ_DESCRIPTOR_MULTIPLE);
5884 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5885 vsi->num_q_vectors = pf->num_lan_msix;
5886 else
5887 vsi->num_q_vectors = 1;
5888
5889 break;
5890
5891 case I40E_VSI_FDIR:
5892 vsi->alloc_queue_pairs = 1;
5893 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5894 I40E_REQ_DESCRIPTOR_MULTIPLE);
5895 vsi->num_q_vectors = 1;
5896 break;
5897
5898 case I40E_VSI_VMDQ2:
5899 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5900 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5901 I40E_REQ_DESCRIPTOR_MULTIPLE);
5902 vsi->num_q_vectors = pf->num_vmdq_msix;
5903 break;
5904
5905 case I40E_VSI_SRIOV:
5906 vsi->alloc_queue_pairs = pf->num_vf_qps;
5907 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5908 I40E_REQ_DESCRIPTOR_MULTIPLE);
5909 break;
5910
5911 default:
5912 WARN_ON(1);
5913 return -ENODATA;
5914 }
5915
5916 return 0;
5917}
5918
5919/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005920 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
5921 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005922 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005923 *
5924 * On error: returns error code (negative)
5925 * On success: returns 0
5926 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005927static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005928{
5929 int size;
5930 int ret = 0;
5931
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005932 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005933 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
5934 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
5935 if (!vsi->tx_rings)
5936 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005937 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
5938
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005939 if (alloc_qvectors) {
5940 /* allocate memory for q_vector pointers */
5941 size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
5942 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
5943 if (!vsi->q_vectors) {
5944 ret = -ENOMEM;
5945 goto err_vectors;
5946 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005947 }
5948 return ret;
5949
5950err_vectors:
5951 kfree(vsi->tx_rings);
5952 return ret;
5953}
5954
5955/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005956 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
5957 * @pf: board private structure
5958 * @type: type of VSI
5959 *
5960 * On error: returns error code (negative)
5961 * On success: returns vsi index in PF (positive)
5962 **/
5963static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
5964{
5965 int ret = -ENODEV;
5966 struct i40e_vsi *vsi;
5967 int vsi_idx;
5968 int i;
5969
5970 /* Need to protect the allocation of the VSIs at the PF level */
5971 mutex_lock(&pf->switch_mutex);
5972
5973 /* VSI list may be fragmented if VSI creation/destruction has
5974 * been happening. We can afford to do a quick scan to look
5975 * for any free VSIs in the list.
5976 *
5977 * find next empty vsi slot, looping back around if necessary
5978 */
5979 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00005980 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005981 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00005982 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005983 i = 0;
5984 while (i < pf->next_vsi && pf->vsi[i])
5985 i++;
5986 }
5987
Mitch Williams505682c2014-05-20 08:01:37 +00005988 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005989 vsi_idx = i; /* Found one! */
5990 } else {
5991 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00005992 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005993 }
5994 pf->next_vsi = ++i;
5995
5996 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
5997 if (!vsi) {
5998 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00005999 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006000 }
6001 vsi->type = type;
6002 vsi->back = pf;
6003 set_bit(__I40E_DOWN, &vsi->state);
6004 vsi->flags = 0;
6005 vsi->idx = vsi_idx;
6006 vsi->rx_itr_setting = pf->rx_itr_default;
6007 vsi->tx_itr_setting = pf->tx_itr_default;
6008 vsi->netdev_registered = false;
6009 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
6010 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00006011 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006012
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006013 ret = i40e_set_num_rings_in_vsi(vsi);
6014 if (ret)
6015 goto err_rings;
6016
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006017 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006018 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006019 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00006020
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006021 /* Setup default MSIX irq handler for VSI */
6022 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
6023
6024 pf->vsi[vsi_idx] = vsi;
6025 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00006026 goto unlock_pf;
6027
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006028err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00006029 pf->next_vsi = i - 1;
6030 kfree(vsi);
6031unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006032 mutex_unlock(&pf->switch_mutex);
6033 return ret;
6034}
6035
6036/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006037 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
6038 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006039 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006040 *
6041 * On error: returns error code (negative)
6042 * On success: returns 0
6043 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006044static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006045{
6046 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006047 if (free_qvectors) {
6048 kfree(vsi->q_vectors);
6049 vsi->q_vectors = NULL;
6050 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006051 kfree(vsi->tx_rings);
6052 vsi->tx_rings = NULL;
6053 vsi->rx_rings = NULL;
6054}
6055
6056/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006057 * i40e_vsi_clear - Deallocate the VSI provided
6058 * @vsi: the VSI being un-configured
6059 **/
6060static int i40e_vsi_clear(struct i40e_vsi *vsi)
6061{
6062 struct i40e_pf *pf;
6063
6064 if (!vsi)
6065 return 0;
6066
6067 if (!vsi->back)
6068 goto free_vsi;
6069 pf = vsi->back;
6070
6071 mutex_lock(&pf->switch_mutex);
6072 if (!pf->vsi[vsi->idx]) {
6073 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
6074 vsi->idx, vsi->idx, vsi, vsi->type);
6075 goto unlock_vsi;
6076 }
6077
6078 if (pf->vsi[vsi->idx] != vsi) {
6079 dev_err(&pf->pdev->dev,
6080 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
6081 pf->vsi[vsi->idx]->idx,
6082 pf->vsi[vsi->idx],
6083 pf->vsi[vsi->idx]->type,
6084 vsi->idx, vsi, vsi->type);
6085 goto unlock_vsi;
6086 }
6087
6088 /* updates the pf for this cleared vsi */
6089 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
6090 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
6091
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006092 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00006093
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006094 pf->vsi[vsi->idx] = NULL;
6095 if (vsi->idx < pf->next_vsi)
6096 pf->next_vsi = vsi->idx;
6097
6098unlock_vsi:
6099 mutex_unlock(&pf->switch_mutex);
6100free_vsi:
6101 kfree(vsi);
6102
6103 return 0;
6104}
6105
6106/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006107 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
6108 * @vsi: the VSI being cleaned
6109 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006110static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006111{
6112 int i;
6113
Greg Rose8e9dca52013-12-18 13:45:53 +00006114 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00006115 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00006116 kfree_rcu(vsi->tx_rings[i], rcu);
6117 vsi->tx_rings[i] = NULL;
6118 vsi->rx_rings[i] = NULL;
6119 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006120 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006121}
6122
6123/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006124 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
6125 * @vsi: the VSI being configured
6126 **/
6127static int i40e_alloc_rings(struct i40e_vsi *vsi)
6128{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00006129 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006130 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006131 int i;
6132
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006133 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00006134 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00006135 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006136 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
6137 if (!tx_ring)
6138 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006139
6140 tx_ring->queue_index = i;
6141 tx_ring->reg_idx = vsi->base_queue + i;
6142 tx_ring->ring_active = false;
6143 tx_ring->vsi = vsi;
6144 tx_ring->netdev = vsi->netdev;
6145 tx_ring->dev = &pf->pdev->dev;
6146 tx_ring->count = vsi->num_desc;
6147 tx_ring->size = 0;
6148 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006149 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006150
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006151 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006152 rx_ring->queue_index = i;
6153 rx_ring->reg_idx = vsi->base_queue + i;
6154 rx_ring->ring_active = false;
6155 rx_ring->vsi = vsi;
6156 rx_ring->netdev = vsi->netdev;
6157 rx_ring->dev = &pf->pdev->dev;
6158 rx_ring->count = vsi->num_desc;
6159 rx_ring->size = 0;
6160 rx_ring->dcb_tc = 0;
6161 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
6162 set_ring_16byte_desc_enabled(rx_ring);
6163 else
6164 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006165 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006166 }
6167
6168 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006169
6170err_out:
6171 i40e_vsi_clear_rings(vsi);
6172 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006173}
6174
6175/**
6176 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
6177 * @pf: board private structure
6178 * @vectors: the number of MSI-X vectors to request
6179 *
6180 * Returns the number of vectors reserved, or error
6181 **/
6182static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
6183{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006184 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
6185 I40E_MIN_MSIX, vectors);
6186 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006187 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006188 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006189 vectors = 0;
6190 }
6191
6192 return vectors;
6193}
6194
6195/**
6196 * i40e_init_msix - Setup the MSIX capability
6197 * @pf: board private structure
6198 *
6199 * Work with the OS to set up the MSIX vectors needed.
6200 *
6201 * Returns 0 on success, negative on failure
6202 **/
6203static int i40e_init_msix(struct i40e_pf *pf)
6204{
6205 i40e_status err = 0;
6206 struct i40e_hw *hw = &pf->hw;
6207 int v_budget, i;
6208 int vec;
6209
6210 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6211 return -ENODEV;
6212
6213 /* The number of vectors we'll request will be comprised of:
6214 * - Add 1 for "other" cause for Admin Queue events, etc.
6215 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006216 * - Queues being used for RSS.
6217 * We don't need as many as max_rss_size vectors.
6218 * use rss_size instead in the calculation since that
6219 * is governed by number of cpus in the system.
6220 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006221 * - The number of VMDq pairs
6222 * Once we count this up, try the request.
6223 *
6224 * If we can't get what we want, we'll simplify to nearly nothing
6225 * and try again. If that still fails, we punt.
6226 */
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006227 pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006228 pf->num_vmdq_msix = pf->num_vmdq_qps;
6229 v_budget = 1 + pf->num_lan_msix;
6230 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006231 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006232 v_budget++;
6233
6234 /* Scale down if necessary, and the rings will share vectors */
6235 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
6236
6237 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
6238 GFP_KERNEL);
6239 if (!pf->msix_entries)
6240 return -ENOMEM;
6241
6242 for (i = 0; i < v_budget; i++)
6243 pf->msix_entries[i].entry = i;
6244 vec = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006245
6246 if (vec != v_budget) {
6247 /* If we have limited resources, we will start with no vectors
6248 * for the special features and then allocate vectors to some
6249 * of these features based on the policy and at the end disable
6250 * the features that did not get any vectors.
6251 */
6252 pf->num_vmdq_msix = 0;
6253 }
6254
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006255 if (vec < I40E_MIN_MSIX) {
6256 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
6257 kfree(pf->msix_entries);
6258 pf->msix_entries = NULL;
6259 return -ENODEV;
6260
6261 } else if (vec == I40E_MIN_MSIX) {
6262 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006263 pf->num_vmdq_vsis = 0;
6264 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006265 pf->num_lan_qps = 1;
6266 pf->num_lan_msix = 1;
6267
6268 } else if (vec != v_budget) {
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006269 /* reserve the misc vector */
6270 vec--;
6271
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006272 /* Scale vector usage down */
6273 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006274 pf->num_vmdq_vsis = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006275
6276 /* partition out the remaining vectors */
6277 switch (vec) {
6278 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006279 pf->num_lan_msix = 1;
6280 break;
6281 case 3:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006282 pf->num_lan_msix = 2;
6283 break;
6284 default:
6285 pf->num_lan_msix = min_t(int, (vec / 2),
6286 pf->num_lan_qps);
6287 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
6288 I40E_DEFAULT_NUM_VMDQ_VSI);
6289 break;
6290 }
6291 }
6292
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006293 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
6294 (pf->num_vmdq_msix == 0)) {
6295 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
6296 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
6297 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006298 return err;
6299}
6300
6301/**
Greg Rose90e04072014-03-06 08:59:57 +00006302 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00006303 * @vsi: the VSI being configured
6304 * @v_idx: index of the vector in the vsi struct
6305 *
6306 * We allocate one q_vector. If allocation fails we return -ENOMEM.
6307 **/
Greg Rose90e04072014-03-06 08:59:57 +00006308static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00006309{
6310 struct i40e_q_vector *q_vector;
6311
6312 /* allocate q_vector */
6313 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
6314 if (!q_vector)
6315 return -ENOMEM;
6316
6317 q_vector->vsi = vsi;
6318 q_vector->v_idx = v_idx;
6319 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
6320 if (vsi->netdev)
6321 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00006322 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00006323
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00006324 q_vector->rx.latency_range = I40E_LOW_LATENCY;
6325 q_vector->tx.latency_range = I40E_LOW_LATENCY;
6326
Alexander Duyck493fb302013-09-28 07:01:44 +00006327 /* tie q_vector and vsi together */
6328 vsi->q_vectors[v_idx] = q_vector;
6329
6330 return 0;
6331}
6332
6333/**
Greg Rose90e04072014-03-06 08:59:57 +00006334 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006335 * @vsi: the VSI being configured
6336 *
6337 * We allocate one q_vector per queue interrupt. If allocation fails we
6338 * return -ENOMEM.
6339 **/
Greg Rose90e04072014-03-06 08:59:57 +00006340static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006341{
6342 struct i40e_pf *pf = vsi->back;
6343 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00006344 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006345
6346 /* if not MSIX, give the one vector only to the LAN VSI */
6347 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6348 num_q_vectors = vsi->num_q_vectors;
6349 else if (vsi == pf->vsi[pf->lan_vsi])
6350 num_q_vectors = 1;
6351 else
6352 return -EINVAL;
6353
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006354 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00006355 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00006356 if (err)
6357 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006358 }
6359
6360 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00006361
6362err_out:
6363 while (v_idx--)
6364 i40e_free_q_vector(vsi, v_idx);
6365
6366 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006367}
6368
6369/**
6370 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
6371 * @pf: board private structure to initialize
6372 **/
6373static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
6374{
6375 int err = 0;
6376
6377 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
6378 err = i40e_init_msix(pf);
6379 if (err) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006380 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
6381 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006382 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006383 I40E_FLAG_SRIOV_ENABLED |
6384 I40E_FLAG_FD_SB_ENABLED |
6385 I40E_FLAG_FD_ATR_ENABLED |
6386 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006387
6388 /* rework the queue expectations without MSIX */
6389 i40e_determine_queue_usage(pf);
6390 }
6391 }
6392
6393 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
6394 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006395 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006396 err = pci_enable_msi(pf->pdev);
6397 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00006398 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006399 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
6400 }
6401 }
6402
Shannon Nelson958a3e32013-09-28 07:13:28 +00006403 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006404 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00006405
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006406 /* track first vector for misc interrupts */
6407 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
6408}
6409
6410/**
6411 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
6412 * @pf: board private structure
6413 *
6414 * This sets up the handler for MSIX 0, which is used to manage the
6415 * non-queue interrupts, e.g. AdminQ and errors. This is not used
6416 * when in MSI or Legacy interrupt mode.
6417 **/
6418static int i40e_setup_misc_vector(struct i40e_pf *pf)
6419{
6420 struct i40e_hw *hw = &pf->hw;
6421 int err = 0;
6422
6423 /* Only request the irq if this is the first time through, and
6424 * not when we're rebuilding after a Reset
6425 */
6426 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6427 err = request_irq(pf->msix_entries[0].vector,
6428 i40e_intr, 0, pf->misc_int_name, pf);
6429 if (err) {
6430 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006431 "request_irq for %s failed: %d\n",
6432 pf->misc_int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006433 return -EFAULT;
6434 }
6435 }
6436
6437 i40e_enable_misc_int_causes(hw);
6438
6439 /* associate no queues to the misc vector */
6440 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
6441 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
6442
6443 i40e_flush(hw);
6444
6445 i40e_irq_dynamic_enable_icr0(pf);
6446
6447 return err;
6448}
6449
6450/**
6451 * i40e_config_rss - Prepare for RSS if used
6452 * @pf: board private structure
6453 **/
6454static int i40e_config_rss(struct i40e_pf *pf)
6455{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006456 /* Set of random keys generated using kernel random number generator */
6457 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
6458 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
6459 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
6460 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00006461 struct i40e_hw *hw = &pf->hw;
6462 u32 lut = 0;
6463 int i, j;
6464 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006465
6466 /* Fill out hash function seed */
6467 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
6468 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
6469
6470 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
6471 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
6472 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00006473 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006474 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
6475 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
6476
6477 /* Populate the LUT with max no. of queues in round robin fashion */
6478 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
6479
6480 /* The assumption is that lan qp count will be the highest
6481 * qp count for any PF VSI that needs RSS.
6482 * If multiple VSIs need RSS support, all the qp counts
6483 * for those VSIs should be a power of 2 for RSS to work.
6484 * If LAN VSI is the only consumer for RSS then this requirement
6485 * is not necessary.
6486 */
6487 if (j == pf->rss_size)
6488 j = 0;
6489 /* lut = 4-byte sliding window of 4 lut entries */
6490 lut = (lut << 8) | (j &
6491 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
6492 /* On i = 3, we have 4 entries in lut; write to the register */
6493 if ((i & 3) == 3)
6494 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
6495 }
6496 i40e_flush(hw);
6497
6498 return 0;
6499}
6500
6501/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006502 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
6503 * @pf: board private structure
6504 * @queue_count: the requested queue count for rss.
6505 *
6506 * returns 0 if rss is not enabled, if enabled returns the final rss queue
6507 * count which may be different from the requested queue count.
6508 **/
6509int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
6510{
6511 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
6512 return 0;
6513
6514 queue_count = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006515
6516 if (queue_count != pf->rss_size) {
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006517 i40e_prep_for_reset(pf);
6518
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006519 pf->rss_size = queue_count;
6520
6521 i40e_reset_and_rebuild(pf, true);
6522 i40e_config_rss(pf);
6523 }
6524 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
6525 return pf->rss_size;
6526}
6527
6528/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006529 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
6530 * @pf: board private structure to initialize
6531 *
6532 * i40e_sw_init initializes the Adapter private data structure.
6533 * Fields are initialized based on PCI device information and
6534 * OS network device settings (MTU size).
6535 **/
6536static int i40e_sw_init(struct i40e_pf *pf)
6537{
6538 int err = 0;
6539 int size;
6540
6541 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
6542 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00006543 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006544 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
6545 if (I40E_DEBUG_USER & debug)
6546 pf->hw.debug_mask = debug;
6547 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
6548 I40E_DEFAULT_MSG_ENABLE);
6549 }
6550
6551 /* Set default capability flags */
6552 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
6553 I40E_FLAG_MSI_ENABLED |
6554 I40E_FLAG_MSIX_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006555 I40E_FLAG_RX_1BUF_ENABLED;
6556
Mitch Williamsca99eb92014-04-04 04:43:07 +00006557 /* Set default ITR */
6558 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
6559 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
6560
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006561 /* Depending on PF configurations, it is possible that the RSS
6562 * maximum might end up larger than the available queues
6563 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006564 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006565 pf->rss_size_max = min_t(int, pf->rss_size_max,
6566 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006567 if (pf->hw.func_caps.rss) {
6568 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00006569 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006570 } else {
6571 pf->rss_size = 1;
6572 }
6573
Catherine Sullivan2050bc62013-12-18 13:46:03 +00006574 /* MFP mode enabled */
6575 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
6576 pf->flags |= I40E_FLAG_MFP_ENABLED;
6577 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
6578 }
6579
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006580 /* FW/NVM is not yet fixed in this regard */
6581 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
6582 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
6583 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6584 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006585 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006586 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006587 } else {
6588 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00006589 "Flow Director Sideband mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006590 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006591 pf->fdir_pf_filter_count =
6592 pf->hw.func_caps.fd_filters_guaranteed;
6593 pf->hw.fdir_shared_filter_count =
6594 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006595 }
6596
6597 if (pf->hw.func_caps.vmdq) {
6598 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
6599 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
6600 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
6601 }
6602
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006603#ifdef CONFIG_PCI_IOV
6604 if (pf->hw.func_caps.num_vfs) {
6605 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
6606 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
6607 pf->num_req_vfs = min_t(int,
6608 pf->hw.func_caps.num_vfs,
6609 I40E_MAX_VF_COUNT);
6610 }
6611#endif /* CONFIG_PCI_IOV */
6612 pf->eeprom_version = 0xDEAD;
6613 pf->lan_veb = I40E_NO_VEB;
6614 pf->lan_vsi = I40E_NO_VSI;
6615
6616 /* set up queue assignment tracking */
6617 size = sizeof(struct i40e_lump_tracking)
6618 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
6619 pf->qp_pile = kzalloc(size, GFP_KERNEL);
6620 if (!pf->qp_pile) {
6621 err = -ENOMEM;
6622 goto sw_init_done;
6623 }
6624 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
6625 pf->qp_pile->search_hint = 0;
6626
6627 /* set up vector assignment tracking */
6628 size = sizeof(struct i40e_lump_tracking)
6629 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
6630 pf->irq_pile = kzalloc(size, GFP_KERNEL);
6631 if (!pf->irq_pile) {
6632 kfree(pf->qp_pile);
6633 err = -ENOMEM;
6634 goto sw_init_done;
6635 }
6636 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
6637 pf->irq_pile->search_hint = 0;
6638
6639 mutex_init(&pf->switch_mutex);
6640
6641sw_init_done:
6642 return err;
6643}
6644
6645/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006646 * i40e_set_ntuple - set the ntuple feature flag and take action
6647 * @pf: board private structure to initialize
6648 * @features: the feature set that the stack is suggesting
6649 *
6650 * returns a bool to indicate if reset needs to happen
6651 **/
6652bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
6653{
6654 bool need_reset = false;
6655
6656 /* Check if Flow Director n-tuple support was enabled or disabled. If
6657 * the state changed, we need to reset.
6658 */
6659 if (features & NETIF_F_NTUPLE) {
6660 /* Enable filters and mark for reset */
6661 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
6662 need_reset = true;
6663 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
6664 } else {
6665 /* turn off filters, mark for reset and clear SW filter list */
6666 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
6667 need_reset = true;
6668 i40e_fdir_filter_exit(pf);
6669 }
6670 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6671 /* if ATR was disabled it can be re-enabled. */
6672 if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
6673 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6674 }
6675 return need_reset;
6676}
6677
6678/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006679 * i40e_set_features - set the netdev feature flags
6680 * @netdev: ptr to the netdev being adjusted
6681 * @features: the feature set that the stack is suggesting
6682 **/
6683static int i40e_set_features(struct net_device *netdev,
6684 netdev_features_t features)
6685{
6686 struct i40e_netdev_priv *np = netdev_priv(netdev);
6687 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006688 struct i40e_pf *pf = vsi->back;
6689 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006690
6691 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6692 i40e_vlan_stripping_enable(vsi);
6693 else
6694 i40e_vlan_stripping_disable(vsi);
6695
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006696 need_reset = i40e_set_ntuple(pf, features);
6697
6698 if (need_reset)
6699 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
6700
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006701 return 0;
6702}
6703
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006704#ifdef CONFIG_I40E_VXLAN
6705/**
6706 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
6707 * @pf: board private structure
6708 * @port: The UDP port to look up
6709 *
6710 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
6711 **/
6712static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
6713{
6714 u8 i;
6715
6716 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6717 if (pf->vxlan_ports[i] == port)
6718 return i;
6719 }
6720
6721 return i;
6722}
6723
6724/**
6725 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
6726 * @netdev: This physical port's netdev
6727 * @sa_family: Socket Family that VXLAN is notifying us about
6728 * @port: New UDP port number that VXLAN started listening to
6729 **/
6730static void i40e_add_vxlan_port(struct net_device *netdev,
6731 sa_family_t sa_family, __be16 port)
6732{
6733 struct i40e_netdev_priv *np = netdev_priv(netdev);
6734 struct i40e_vsi *vsi = np->vsi;
6735 struct i40e_pf *pf = vsi->back;
6736 u8 next_idx;
6737 u8 idx;
6738
6739 if (sa_family == AF_INET6)
6740 return;
6741
6742 idx = i40e_get_vxlan_port_idx(pf, port);
6743
6744 /* Check if port already exists */
6745 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6746 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
6747 return;
6748 }
6749
6750 /* Now check if there is space to add the new port */
6751 next_idx = i40e_get_vxlan_port_idx(pf, 0);
6752
6753 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6754 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
6755 ntohs(port));
6756 return;
6757 }
6758
6759 /* New port: add it and mark its index in the bitmap */
6760 pf->vxlan_ports[next_idx] = port;
6761 pf->pending_vxlan_bitmap |= (1 << next_idx);
6762
6763 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6764}
6765
6766/**
6767 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
6768 * @netdev: This physical port's netdev
6769 * @sa_family: Socket Family that VXLAN is notifying us about
6770 * @port: UDP port number that VXLAN stopped listening to
6771 **/
6772static void i40e_del_vxlan_port(struct net_device *netdev,
6773 sa_family_t sa_family, __be16 port)
6774{
6775 struct i40e_netdev_priv *np = netdev_priv(netdev);
6776 struct i40e_vsi *vsi = np->vsi;
6777 struct i40e_pf *pf = vsi->back;
6778 u8 idx;
6779
6780 if (sa_family == AF_INET6)
6781 return;
6782
6783 idx = i40e_get_vxlan_port_idx(pf, port);
6784
6785 /* Check if port already exists */
6786 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6787 /* if port exists, set it to 0 (mark for deletion)
6788 * and make it pending
6789 */
6790 pf->vxlan_ports[idx] = 0;
6791
6792 pf->pending_vxlan_bitmap |= (1 << idx);
6793
6794 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6795 } else {
6796 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6797 ntohs(port));
6798 }
6799}
6800
6801#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006802#ifdef HAVE_FDB_OPS
6803#ifdef USE_CONST_DEV_UC_CHAR
6804static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
6805 struct net_device *dev,
6806 const unsigned char *addr,
6807 u16 flags)
6808#else
6809static int i40e_ndo_fdb_add(struct ndmsg *ndm,
6810 struct net_device *dev,
6811 unsigned char *addr,
6812 u16 flags)
6813#endif
6814{
6815 struct i40e_netdev_priv *np = netdev_priv(dev);
6816 struct i40e_pf *pf = np->vsi->back;
6817 int err = 0;
6818
6819 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
6820 return -EOPNOTSUPP;
6821
6822 /* Hardware does not support aging addresses so if a
6823 * ndm_state is given only allow permanent addresses
6824 */
6825 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6826 netdev_info(dev, "FDB only supports static addresses\n");
6827 return -EINVAL;
6828 }
6829
6830 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
6831 err = dev_uc_add_excl(dev, addr);
6832 else if (is_multicast_ether_addr(addr))
6833 err = dev_mc_add_excl(dev, addr);
6834 else
6835 err = -EINVAL;
6836
6837 /* Only return duplicate errors if NLM_F_EXCL is set */
6838 if (err == -EEXIST && !(flags & NLM_F_EXCL))
6839 err = 0;
6840
6841 return err;
6842}
6843
6844#ifndef USE_DEFAULT_FDB_DEL_DUMP
6845#ifdef USE_CONST_DEV_UC_CHAR
6846static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6847 struct net_device *dev,
6848 const unsigned char *addr)
6849#else
6850static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6851 struct net_device *dev,
6852 unsigned char *addr)
6853#endif
6854{
6855 struct i40e_netdev_priv *np = netdev_priv(dev);
6856 struct i40e_pf *pf = np->vsi->back;
6857 int err = -EOPNOTSUPP;
6858
6859 if (ndm->ndm_state & NUD_PERMANENT) {
6860 netdev_info(dev, "FDB only supports static addresses\n");
6861 return -EINVAL;
6862 }
6863
6864 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
6865 if (is_unicast_ether_addr(addr))
6866 err = dev_uc_del(dev, addr);
6867 else if (is_multicast_ether_addr(addr))
6868 err = dev_mc_del(dev, addr);
6869 else
6870 err = -EINVAL;
6871 }
6872
6873 return err;
6874}
6875
6876static int i40e_ndo_fdb_dump(struct sk_buff *skb,
6877 struct netlink_callback *cb,
6878 struct net_device *dev,
6879 int idx)
6880{
6881 struct i40e_netdev_priv *np = netdev_priv(dev);
6882 struct i40e_pf *pf = np->vsi->back;
6883
6884 if (pf->flags & I40E_FLAG_SRIOV_ENABLED)
6885 idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
6886
6887 return idx;
6888}
6889
6890#endif /* USE_DEFAULT_FDB_DEL_DUMP */
6891#endif /* HAVE_FDB_OPS */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006892static const struct net_device_ops i40e_netdev_ops = {
6893 .ndo_open = i40e_open,
6894 .ndo_stop = i40e_close,
6895 .ndo_start_xmit = i40e_lan_xmit_frame,
6896 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
6897 .ndo_set_rx_mode = i40e_set_rx_mode,
6898 .ndo_validate_addr = eth_validate_addr,
6899 .ndo_set_mac_address = i40e_set_mac,
6900 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006901 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006902 .ndo_tx_timeout = i40e_tx_timeout,
6903 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
6904 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
6905#ifdef CONFIG_NET_POLL_CONTROLLER
6906 .ndo_poll_controller = i40e_netpoll,
6907#endif
6908 .ndo_setup_tc = i40e_setup_tc,
6909 .ndo_set_features = i40e_set_features,
6910 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
6911 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04006912 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006913 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00006914 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Mitch Williamsc674d122014-05-20 08:01:40 +00006915 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofck,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006916#ifdef CONFIG_I40E_VXLAN
6917 .ndo_add_vxlan_port = i40e_add_vxlan_port,
6918 .ndo_del_vxlan_port = i40e_del_vxlan_port,
6919#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006920#ifdef HAVE_FDB_OPS
6921 .ndo_fdb_add = i40e_ndo_fdb_add,
6922#ifndef USE_DEFAULT_FDB_DEL_DUMP
6923 .ndo_fdb_del = i40e_ndo_fdb_del,
6924 .ndo_fdb_dump = i40e_ndo_fdb_dump,
6925#endif
6926#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006927};
6928
6929/**
6930 * i40e_config_netdev - Setup the netdev flags
6931 * @vsi: the VSI being configured
6932 *
6933 * Returns 0 on success, negative value on failure
6934 **/
6935static int i40e_config_netdev(struct i40e_vsi *vsi)
6936{
Greg Rose1a103702013-11-28 06:42:39 +00006937 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006938 struct i40e_pf *pf = vsi->back;
6939 struct i40e_hw *hw = &pf->hw;
6940 struct i40e_netdev_priv *np;
6941 struct net_device *netdev;
6942 u8 mac_addr[ETH_ALEN];
6943 int etherdev_size;
6944
6945 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006946 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006947 if (!netdev)
6948 return -ENOMEM;
6949
6950 vsi->netdev = netdev;
6951 np = netdev_priv(netdev);
6952 np->vsi = vsi;
6953
Or Gerlitzd70e9412014-03-18 10:36:45 +02006954 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006955 NETIF_F_GSO_UDP_TUNNEL |
Or Gerlitzd70e9412014-03-18 10:36:45 +02006956 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006957
6958 netdev->features = NETIF_F_SG |
6959 NETIF_F_IP_CSUM |
6960 NETIF_F_SCTP_CSUM |
6961 NETIF_F_HIGHDMA |
6962 NETIF_F_GSO_UDP_TUNNEL |
6963 NETIF_F_HW_VLAN_CTAG_TX |
6964 NETIF_F_HW_VLAN_CTAG_RX |
6965 NETIF_F_HW_VLAN_CTAG_FILTER |
6966 NETIF_F_IPV6_CSUM |
6967 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00006968 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006969 NETIF_F_TSO6 |
6970 NETIF_F_RXCSUM |
6971 NETIF_F_RXHASH |
6972 0;
6973
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00006974 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
6975 netdev->features |= NETIF_F_NTUPLE;
6976
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006977 /* copy netdev features into list of user selectable features */
6978 netdev->hw_features |= netdev->features;
6979
6980 if (vsi->type == I40E_VSI_MAIN) {
6981 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
6982 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
6983 } else {
6984 /* relate the VSI_VMDQ name to the VSI_MAIN name */
6985 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
6986 pf->vsi[pf->lan_vsi]->netdev->name);
6987 random_ether_addr(mac_addr);
6988 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
6989 }
Greg Rose1a103702013-11-28 06:42:39 +00006990 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006991
6992 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
6993 memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
6994 /* vlan gets same features (except vlan offload)
6995 * after any tweaks for specific VSI types
6996 */
6997 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
6998 NETIF_F_HW_VLAN_CTAG_RX |
6999 NETIF_F_HW_VLAN_CTAG_FILTER);
7000 netdev->priv_flags |= IFF_UNICAST_FLT;
7001 netdev->priv_flags |= IFF_SUPP_NOFCS;
7002 /* Setup netdev TC information */
7003 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
7004
7005 netdev->netdev_ops = &i40e_netdev_ops;
7006 netdev->watchdog_timeo = 5 * HZ;
7007 i40e_set_ethtool_ops(netdev);
7008
7009 return 0;
7010}
7011
7012/**
7013 * i40e_vsi_delete - Delete a VSI from the switch
7014 * @vsi: the VSI being removed
7015 *
7016 * Returns 0 on success, negative value on failure
7017 **/
7018static void i40e_vsi_delete(struct i40e_vsi *vsi)
7019{
7020 /* remove default VSI is not allowed */
7021 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
7022 return;
7023
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007024 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007025}
7026
7027/**
7028 * i40e_add_vsi - Add a VSI to the switch
7029 * @vsi: the VSI being configured
7030 *
7031 * This initializes a VSI context depending on the VSI type to be added and
7032 * passes it down to the add_vsi aq command.
7033 **/
7034static int i40e_add_vsi(struct i40e_vsi *vsi)
7035{
7036 int ret = -ENODEV;
7037 struct i40e_mac_filter *f, *ftmp;
7038 struct i40e_pf *pf = vsi->back;
7039 struct i40e_hw *hw = &pf->hw;
7040 struct i40e_vsi_context ctxt;
7041 u8 enabled_tc = 0x1; /* TC0 enabled */
7042 int f_count = 0;
7043
7044 memset(&ctxt, 0, sizeof(ctxt));
7045 switch (vsi->type) {
7046 case I40E_VSI_MAIN:
7047 /* The PF's main VSI is already setup as part of the
7048 * device initialization, so we'll not bother with
7049 * the add_vsi call, but we will retrieve the current
7050 * VSI context.
7051 */
7052 ctxt.seid = pf->main_vsi_seid;
7053 ctxt.pf_num = pf->hw.pf_id;
7054 ctxt.vf_num = 0;
7055 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
7056 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
7057 if (ret) {
7058 dev_info(&pf->pdev->dev,
7059 "couldn't get pf vsi config, err %d, aq_err %d\n",
7060 ret, pf->hw.aq.asq_last_status);
7061 return -ENOENT;
7062 }
7063 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
7064 vsi->info.valid_sections = 0;
7065
7066 vsi->seid = ctxt.seid;
7067 vsi->id = ctxt.vsi_number;
7068
7069 enabled_tc = i40e_pf_get_tc_map(pf);
7070
7071 /* MFP mode setup queue map and update VSI */
7072 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7073 memset(&ctxt, 0, sizeof(ctxt));
7074 ctxt.seid = pf->main_vsi_seid;
7075 ctxt.pf_num = pf->hw.pf_id;
7076 ctxt.vf_num = 0;
7077 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
7078 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7079 if (ret) {
7080 dev_info(&pf->pdev->dev,
7081 "update vsi failed, aq_err=%d\n",
7082 pf->hw.aq.asq_last_status);
7083 ret = -ENOENT;
7084 goto err;
7085 }
7086 /* update the local VSI info queue map */
7087 i40e_vsi_update_queue_map(vsi, &ctxt);
7088 vsi->info.valid_sections = 0;
7089 } else {
7090 /* Default/Main VSI is only enabled for TC0
7091 * reconfigure it to enable all TCs that are
7092 * available on the port in SFP mode.
7093 */
7094 ret = i40e_vsi_config_tc(vsi, enabled_tc);
7095 if (ret) {
7096 dev_info(&pf->pdev->dev,
7097 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
7098 enabled_tc, ret,
7099 pf->hw.aq.asq_last_status);
7100 ret = -ENOENT;
7101 }
7102 }
7103 break;
7104
7105 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007106 ctxt.pf_num = hw->pf_id;
7107 ctxt.vf_num = 0;
7108 ctxt.uplink_seid = vsi->uplink_seid;
7109 ctxt.connection_type = 0x1; /* regular data port */
7110 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007111 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007112 break;
7113
7114 case I40E_VSI_VMDQ2:
7115 ctxt.pf_num = hw->pf_id;
7116 ctxt.vf_num = 0;
7117 ctxt.uplink_seid = vsi->uplink_seid;
7118 ctxt.connection_type = 0x1; /* regular data port */
7119 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
7120
7121 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7122
7123 /* This VSI is connected to VEB so the switch_id
7124 * should be set to zero by default.
7125 */
7126 ctxt.info.switch_id = 0;
7127 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
7128 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7129
7130 /* Setup the VSI tx/rx queue map for TC0 only for now */
7131 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
7132 break;
7133
7134 case I40E_VSI_SRIOV:
7135 ctxt.pf_num = hw->pf_id;
7136 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
7137 ctxt.uplink_seid = vsi->uplink_seid;
7138 ctxt.connection_type = 0x1; /* regular data port */
7139 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
7140
7141 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7142
7143 /* This VSI is connected to VEB so the switch_id
7144 * should be set to zero by default.
7145 */
7146 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7147
7148 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
7149 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00007150 if (pf->vf[vsi->vf_id].spoofchk) {
7151 ctxt.info.valid_sections |=
7152 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
7153 ctxt.info.sec_flags |=
7154 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
7155 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
7156 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007157 /* Setup the VSI tx/rx queue map for TC0 only for now */
7158 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
7159 break;
7160
7161 default:
7162 return -ENODEV;
7163 }
7164
7165 if (vsi->type != I40E_VSI_MAIN) {
7166 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
7167 if (ret) {
7168 dev_info(&vsi->back->pdev->dev,
7169 "add vsi failed, aq_err=%d\n",
7170 vsi->back->hw.aq.asq_last_status);
7171 ret = -ENOENT;
7172 goto err;
7173 }
7174 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
7175 vsi->info.valid_sections = 0;
7176 vsi->seid = ctxt.seid;
7177 vsi->id = ctxt.vsi_number;
7178 }
7179
7180 /* If macvlan filters already exist, force them to get loaded */
7181 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
7182 f->changed = true;
7183 f_count++;
7184 }
7185 if (f_count) {
7186 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
7187 pf->flags |= I40E_FLAG_FILTER_SYNC;
7188 }
7189
7190 /* Update VSI BW information */
7191 ret = i40e_vsi_get_bw_info(vsi);
7192 if (ret) {
7193 dev_info(&pf->pdev->dev,
7194 "couldn't get vsi bw info, err %d, aq_err %d\n",
7195 ret, pf->hw.aq.asq_last_status);
7196 /* VSI is already added so not tearing that up */
7197 ret = 0;
7198 }
7199
7200err:
7201 return ret;
7202}
7203
7204/**
7205 * i40e_vsi_release - Delete a VSI and free its resources
7206 * @vsi: the VSI being removed
7207 *
7208 * Returns 0 on success or < 0 on error
7209 **/
7210int i40e_vsi_release(struct i40e_vsi *vsi)
7211{
7212 struct i40e_mac_filter *f, *ftmp;
7213 struct i40e_veb *veb = NULL;
7214 struct i40e_pf *pf;
7215 u16 uplink_seid;
7216 int i, n;
7217
7218 pf = vsi->back;
7219
7220 /* release of a VEB-owner or last VSI is not allowed */
7221 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
7222 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
7223 vsi->seid, vsi->uplink_seid);
7224 return -ENODEV;
7225 }
7226 if (vsi == pf->vsi[pf->lan_vsi] &&
7227 !test_bit(__I40E_DOWN, &pf->state)) {
7228 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
7229 return -ENODEV;
7230 }
7231
7232 uplink_seid = vsi->uplink_seid;
7233 if (vsi->type != I40E_VSI_SRIOV) {
7234 if (vsi->netdev_registered) {
7235 vsi->netdev_registered = false;
7236 if (vsi->netdev) {
7237 /* results in a call to i40e_close() */
7238 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007239 }
7240 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00007241 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007242 }
7243 i40e_vsi_disable_irq(vsi);
7244 }
7245
7246 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
7247 i40e_del_filter(vsi, f->macaddr, f->vlan,
7248 f->is_vf, f->is_netdev);
7249 i40e_sync_vsi_filters(vsi);
7250
7251 i40e_vsi_delete(vsi);
7252 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00007253 if (vsi->netdev) {
7254 free_netdev(vsi->netdev);
7255 vsi->netdev = NULL;
7256 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007257 i40e_vsi_clear_rings(vsi);
7258 i40e_vsi_clear(vsi);
7259
7260 /* If this was the last thing on the VEB, except for the
7261 * controlling VSI, remove the VEB, which puts the controlling
7262 * VSI onto the next level down in the switch.
7263 *
7264 * Well, okay, there's one more exception here: don't remove
7265 * the orphan VEBs yet. We'll wait for an explicit remove request
7266 * from up the network stack.
7267 */
Mitch Williams505682c2014-05-20 08:01:37 +00007268 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007269 if (pf->vsi[i] &&
7270 pf->vsi[i]->uplink_seid == uplink_seid &&
7271 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7272 n++; /* count the VSIs */
7273 }
7274 }
7275 for (i = 0; i < I40E_MAX_VEB; i++) {
7276 if (!pf->veb[i])
7277 continue;
7278 if (pf->veb[i]->uplink_seid == uplink_seid)
7279 n++; /* count the VEBs */
7280 if (pf->veb[i]->seid == uplink_seid)
7281 veb = pf->veb[i];
7282 }
7283 if (n == 0 && veb && veb->uplink_seid != 0)
7284 i40e_veb_release(veb);
7285
7286 return 0;
7287}
7288
7289/**
7290 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
7291 * @vsi: ptr to the VSI
7292 *
7293 * This should only be called after i40e_vsi_mem_alloc() which allocates the
7294 * corresponding SW VSI structure and initializes num_queue_pairs for the
7295 * newly allocated VSI.
7296 *
7297 * Returns 0 on success or negative on failure
7298 **/
7299static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
7300{
7301 int ret = -ENOENT;
7302 struct i40e_pf *pf = vsi->back;
7303
Alexander Duyck493fb302013-09-28 07:01:44 +00007304 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007305 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
7306 vsi->seid);
7307 return -EEXIST;
7308 }
7309
7310 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007311 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007312 vsi->seid, vsi->base_vector);
7313 return -EEXIST;
7314 }
7315
Greg Rose90e04072014-03-06 08:59:57 +00007316 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007317 if (ret) {
7318 dev_info(&pf->pdev->dev,
7319 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
7320 vsi->num_q_vectors, vsi->seid, ret);
7321 vsi->num_q_vectors = 0;
7322 goto vector_setup_out;
7323 }
7324
Shannon Nelson958a3e32013-09-28 07:13:28 +00007325 if (vsi->num_q_vectors)
7326 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
7327 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007328 if (vsi->base_vector < 0) {
7329 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007330 "failed to get queue tracking for VSI %d, err=%d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007331 vsi->seid, vsi->base_vector);
7332 i40e_vsi_free_q_vectors(vsi);
7333 ret = -ENOENT;
7334 goto vector_setup_out;
7335 }
7336
7337vector_setup_out:
7338 return ret;
7339}
7340
7341/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007342 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
7343 * @vsi: pointer to the vsi.
7344 *
7345 * This re-allocates a vsi's queue resources.
7346 *
7347 * Returns pointer to the successfully allocated and configured VSI sw struct
7348 * on success, otherwise returns NULL on failure.
7349 **/
7350static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
7351{
7352 struct i40e_pf *pf = vsi->back;
7353 u8 enabled_tc;
7354 int ret;
7355
7356 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7357 i40e_vsi_clear_rings(vsi);
7358
7359 i40e_vsi_free_arrays(vsi, false);
7360 i40e_set_num_rings_in_vsi(vsi);
7361 ret = i40e_vsi_alloc_arrays(vsi, false);
7362 if (ret)
7363 goto err_vsi;
7364
7365 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
7366 if (ret < 0) {
7367 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7368 vsi->seid, ret);
7369 goto err_vsi;
7370 }
7371 vsi->base_queue = ret;
7372
7373 /* Update the FW view of the VSI. Force a reset of TC and queue
7374 * layout configurations.
7375 */
7376 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7377 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7378 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7379 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7380
7381 /* assign it some queues */
7382 ret = i40e_alloc_rings(vsi);
7383 if (ret)
7384 goto err_rings;
7385
7386 /* map all of the rings to the q_vectors */
7387 i40e_vsi_map_rings_to_vectors(vsi);
7388 return vsi;
7389
7390err_rings:
7391 i40e_vsi_free_q_vectors(vsi);
7392 if (vsi->netdev_registered) {
7393 vsi->netdev_registered = false;
7394 unregister_netdev(vsi->netdev);
7395 free_netdev(vsi->netdev);
7396 vsi->netdev = NULL;
7397 }
7398 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7399err_vsi:
7400 i40e_vsi_clear(vsi);
7401 return NULL;
7402}
7403
7404/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007405 * i40e_vsi_setup - Set up a VSI by a given type
7406 * @pf: board private structure
7407 * @type: VSI type
7408 * @uplink_seid: the switch element to link to
7409 * @param1: usage depends upon VSI type. For VF types, indicates VF id
7410 *
7411 * This allocates the sw VSI structure and its queue resources, then add a VSI
7412 * to the identified VEB.
7413 *
7414 * Returns pointer to the successfully allocated and configure VSI sw struct on
7415 * success, otherwise returns NULL on failure.
7416 **/
7417struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
7418 u16 uplink_seid, u32 param1)
7419{
7420 struct i40e_vsi *vsi = NULL;
7421 struct i40e_veb *veb = NULL;
7422 int ret, i;
7423 int v_idx;
7424
7425 /* The requested uplink_seid must be either
7426 * - the PF's port seid
7427 * no VEB is needed because this is the PF
7428 * or this is a Flow Director special case VSI
7429 * - seid of an existing VEB
7430 * - seid of a VSI that owns an existing VEB
7431 * - seid of a VSI that doesn't own a VEB
7432 * a new VEB is created and the VSI becomes the owner
7433 * - seid of the PF VSI, which is what creates the first VEB
7434 * this is a special case of the previous
7435 *
7436 * Find which uplink_seid we were given and create a new VEB if needed
7437 */
7438 for (i = 0; i < I40E_MAX_VEB; i++) {
7439 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
7440 veb = pf->veb[i];
7441 break;
7442 }
7443 }
7444
7445 if (!veb && uplink_seid != pf->mac_seid) {
7446
Mitch Williams505682c2014-05-20 08:01:37 +00007447 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007448 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
7449 vsi = pf->vsi[i];
7450 break;
7451 }
7452 }
7453 if (!vsi) {
7454 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
7455 uplink_seid);
7456 return NULL;
7457 }
7458
7459 if (vsi->uplink_seid == pf->mac_seid)
7460 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
7461 vsi->tc_config.enabled_tc);
7462 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
7463 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
7464 vsi->tc_config.enabled_tc);
7465
7466 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
7467 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
7468 veb = pf->veb[i];
7469 }
7470 if (!veb) {
7471 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
7472 return NULL;
7473 }
7474
7475 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7476 uplink_seid = veb->seid;
7477 }
7478
7479 /* get vsi sw struct */
7480 v_idx = i40e_vsi_mem_alloc(pf, type);
7481 if (v_idx < 0)
7482 goto err_alloc;
7483 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007484 if (!vsi)
7485 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007486 vsi->type = type;
7487 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
7488
7489 if (type == I40E_VSI_MAIN)
7490 pf->lan_vsi = v_idx;
7491 else if (type == I40E_VSI_SRIOV)
7492 vsi->vf_id = param1;
7493 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007494 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
7495 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007496 if (ret < 0) {
7497 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7498 vsi->seid, ret);
7499 goto err_vsi;
7500 }
7501 vsi->base_queue = ret;
7502
7503 /* get a VSI from the hardware */
7504 vsi->uplink_seid = uplink_seid;
7505 ret = i40e_add_vsi(vsi);
7506 if (ret)
7507 goto err_vsi;
7508
7509 switch (vsi->type) {
7510 /* setup the netdev if needed */
7511 case I40E_VSI_MAIN:
7512 case I40E_VSI_VMDQ2:
7513 ret = i40e_config_netdev(vsi);
7514 if (ret)
7515 goto err_netdev;
7516 ret = register_netdev(vsi->netdev);
7517 if (ret)
7518 goto err_netdev;
7519 vsi->netdev_registered = true;
7520 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007521#ifdef CONFIG_I40E_DCB
7522 /* Setup DCB netlink interface */
7523 i40e_dcbnl_setup(vsi);
7524#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007525 /* fall through */
7526
7527 case I40E_VSI_FDIR:
7528 /* set up vectors and rings if needed */
7529 ret = i40e_vsi_setup_vectors(vsi);
7530 if (ret)
7531 goto err_msix;
7532
7533 ret = i40e_alloc_rings(vsi);
7534 if (ret)
7535 goto err_rings;
7536
7537 /* map all of the rings to the q_vectors */
7538 i40e_vsi_map_rings_to_vectors(vsi);
7539
7540 i40e_vsi_reset_stats(vsi);
7541 break;
7542
7543 default:
7544 /* no netdev or rings for the other VSI types */
7545 break;
7546 }
7547
7548 return vsi;
7549
7550err_rings:
7551 i40e_vsi_free_q_vectors(vsi);
7552err_msix:
7553 if (vsi->netdev_registered) {
7554 vsi->netdev_registered = false;
7555 unregister_netdev(vsi->netdev);
7556 free_netdev(vsi->netdev);
7557 vsi->netdev = NULL;
7558 }
7559err_netdev:
7560 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7561err_vsi:
7562 i40e_vsi_clear(vsi);
7563err_alloc:
7564 return NULL;
7565}
7566
7567/**
7568 * i40e_veb_get_bw_info - Query VEB BW information
7569 * @veb: the veb to query
7570 *
7571 * Query the Tx scheduler BW configuration data for given VEB
7572 **/
7573static int i40e_veb_get_bw_info(struct i40e_veb *veb)
7574{
7575 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
7576 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
7577 struct i40e_pf *pf = veb->pf;
7578 struct i40e_hw *hw = &pf->hw;
7579 u32 tc_bw_max;
7580 int ret = 0;
7581 int i;
7582
7583 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
7584 &bw_data, NULL);
7585 if (ret) {
7586 dev_info(&pf->pdev->dev,
7587 "query veb bw config failed, aq_err=%d\n",
7588 hw->aq.asq_last_status);
7589 goto out;
7590 }
7591
7592 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
7593 &ets_data, NULL);
7594 if (ret) {
7595 dev_info(&pf->pdev->dev,
7596 "query veb bw ets config failed, aq_err=%d\n",
7597 hw->aq.asq_last_status);
7598 goto out;
7599 }
7600
7601 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
7602 veb->bw_max_quanta = ets_data.tc_bw_max;
7603 veb->is_abs_credits = bw_data.absolute_credits_enable;
7604 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
7605 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
7606 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7607 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
7608 veb->bw_tc_limit_credits[i] =
7609 le16_to_cpu(bw_data.tc_bw_limits[i]);
7610 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
7611 }
7612
7613out:
7614 return ret;
7615}
7616
7617/**
7618 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
7619 * @pf: board private structure
7620 *
7621 * On error: returns error code (negative)
7622 * On success: returns vsi index in PF (positive)
7623 **/
7624static int i40e_veb_mem_alloc(struct i40e_pf *pf)
7625{
7626 int ret = -ENOENT;
7627 struct i40e_veb *veb;
7628 int i;
7629
7630 /* Need to protect the allocation of switch elements at the PF level */
7631 mutex_lock(&pf->switch_mutex);
7632
7633 /* VEB list may be fragmented if VEB creation/destruction has
7634 * been happening. We can afford to do a quick scan to look
7635 * for any free slots in the list.
7636 *
7637 * find next empty veb slot, looping back around if necessary
7638 */
7639 i = 0;
7640 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
7641 i++;
7642 if (i >= I40E_MAX_VEB) {
7643 ret = -ENOMEM;
7644 goto err_alloc_veb; /* out of VEB slots! */
7645 }
7646
7647 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
7648 if (!veb) {
7649 ret = -ENOMEM;
7650 goto err_alloc_veb;
7651 }
7652 veb->pf = pf;
7653 veb->idx = i;
7654 veb->enabled_tc = 1;
7655
7656 pf->veb[i] = veb;
7657 ret = i;
7658err_alloc_veb:
7659 mutex_unlock(&pf->switch_mutex);
7660 return ret;
7661}
7662
7663/**
7664 * i40e_switch_branch_release - Delete a branch of the switch tree
7665 * @branch: where to start deleting
7666 *
7667 * This uses recursion to find the tips of the branch to be
7668 * removed, deleting until we get back to and can delete this VEB.
7669 **/
7670static void i40e_switch_branch_release(struct i40e_veb *branch)
7671{
7672 struct i40e_pf *pf = branch->pf;
7673 u16 branch_seid = branch->seid;
7674 u16 veb_idx = branch->idx;
7675 int i;
7676
7677 /* release any VEBs on this VEB - RECURSION */
7678 for (i = 0; i < I40E_MAX_VEB; i++) {
7679 if (!pf->veb[i])
7680 continue;
7681 if (pf->veb[i]->uplink_seid == branch->seid)
7682 i40e_switch_branch_release(pf->veb[i]);
7683 }
7684
7685 /* Release the VSIs on this VEB, but not the owner VSI.
7686 *
7687 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
7688 * the VEB itself, so don't use (*branch) after this loop.
7689 */
Mitch Williams505682c2014-05-20 08:01:37 +00007690 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007691 if (!pf->vsi[i])
7692 continue;
7693 if (pf->vsi[i]->uplink_seid == branch_seid &&
7694 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7695 i40e_vsi_release(pf->vsi[i]);
7696 }
7697 }
7698
7699 /* There's one corner case where the VEB might not have been
7700 * removed, so double check it here and remove it if needed.
7701 * This case happens if the veb was created from the debugfs
7702 * commands and no VSIs were added to it.
7703 */
7704 if (pf->veb[veb_idx])
7705 i40e_veb_release(pf->veb[veb_idx]);
7706}
7707
7708/**
7709 * i40e_veb_clear - remove veb struct
7710 * @veb: the veb to remove
7711 **/
7712static void i40e_veb_clear(struct i40e_veb *veb)
7713{
7714 if (!veb)
7715 return;
7716
7717 if (veb->pf) {
7718 struct i40e_pf *pf = veb->pf;
7719
7720 mutex_lock(&pf->switch_mutex);
7721 if (pf->veb[veb->idx] == veb)
7722 pf->veb[veb->idx] = NULL;
7723 mutex_unlock(&pf->switch_mutex);
7724 }
7725
7726 kfree(veb);
7727}
7728
7729/**
7730 * i40e_veb_release - Delete a VEB and free its resources
7731 * @veb: the VEB being removed
7732 **/
7733void i40e_veb_release(struct i40e_veb *veb)
7734{
7735 struct i40e_vsi *vsi = NULL;
7736 struct i40e_pf *pf;
7737 int i, n = 0;
7738
7739 pf = veb->pf;
7740
7741 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +00007742 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007743 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
7744 n++;
7745 vsi = pf->vsi[i];
7746 }
7747 }
7748 if (n != 1) {
7749 dev_info(&pf->pdev->dev,
7750 "can't remove VEB %d with %d VSIs left\n",
7751 veb->seid, n);
7752 return;
7753 }
7754
7755 /* move the remaining VSI to uplink veb */
7756 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
7757 if (veb->uplink_seid) {
7758 vsi->uplink_seid = veb->uplink_seid;
7759 if (veb->uplink_seid == pf->mac_seid)
7760 vsi->veb_idx = I40E_NO_VEB;
7761 else
7762 vsi->veb_idx = veb->veb_idx;
7763 } else {
7764 /* floating VEB */
7765 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
7766 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
7767 }
7768
7769 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
7770 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007771}
7772
7773/**
7774 * i40e_add_veb - create the VEB in the switch
7775 * @veb: the VEB to be instantiated
7776 * @vsi: the controlling VSI
7777 **/
7778static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
7779{
Greg Rose56747262013-11-28 06:39:37 +00007780 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00007781 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007782 int ret;
7783
7784 /* get a VEB from the hardware */
7785 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00007786 veb->enabled_tc, is_default,
7787 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007788 if (ret) {
7789 dev_info(&veb->pf->pdev->dev,
7790 "couldn't add VEB, err %d, aq_err %d\n",
7791 ret, veb->pf->hw.aq.asq_last_status);
7792 return -EPERM;
7793 }
7794
7795 /* get statistics counter */
7796 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
7797 &veb->stats_idx, NULL, NULL, NULL);
7798 if (ret) {
7799 dev_info(&veb->pf->pdev->dev,
7800 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
7801 ret, veb->pf->hw.aq.asq_last_status);
7802 return -EPERM;
7803 }
7804 ret = i40e_veb_get_bw_info(veb);
7805 if (ret) {
7806 dev_info(&veb->pf->pdev->dev,
7807 "couldn't get VEB bw info, err %d, aq_err %d\n",
7808 ret, veb->pf->hw.aq.asq_last_status);
7809 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
7810 return -ENOENT;
7811 }
7812
7813 vsi->uplink_seid = veb->seid;
7814 vsi->veb_idx = veb->idx;
7815 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7816
7817 return 0;
7818}
7819
7820/**
7821 * i40e_veb_setup - Set up a VEB
7822 * @pf: board private structure
7823 * @flags: VEB setup flags
7824 * @uplink_seid: the switch element to link to
7825 * @vsi_seid: the initial VSI seid
7826 * @enabled_tc: Enabled TC bit-map
7827 *
7828 * This allocates the sw VEB structure and links it into the switch
7829 * It is possible and legal for this to be a duplicate of an already
7830 * existing VEB. It is also possible for both uplink and vsi seids
7831 * to be zero, in order to create a floating VEB.
7832 *
7833 * Returns pointer to the successfully allocated VEB sw struct on
7834 * success, otherwise returns NULL on failure.
7835 **/
7836struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
7837 u16 uplink_seid, u16 vsi_seid,
7838 u8 enabled_tc)
7839{
7840 struct i40e_veb *veb, *uplink_veb = NULL;
7841 int vsi_idx, veb_idx;
7842 int ret;
7843
7844 /* if one seid is 0, the other must be 0 to create a floating relay */
7845 if ((uplink_seid == 0 || vsi_seid == 0) &&
7846 (uplink_seid + vsi_seid != 0)) {
7847 dev_info(&pf->pdev->dev,
7848 "one, not both seid's are 0: uplink=%d vsi=%d\n",
7849 uplink_seid, vsi_seid);
7850 return NULL;
7851 }
7852
7853 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +00007854 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007855 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
7856 break;
Mitch Williams505682c2014-05-20 08:01:37 +00007857 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007858 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
7859 vsi_seid);
7860 return NULL;
7861 }
7862
7863 if (uplink_seid && uplink_seid != pf->mac_seid) {
7864 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
7865 if (pf->veb[veb_idx] &&
7866 pf->veb[veb_idx]->seid == uplink_seid) {
7867 uplink_veb = pf->veb[veb_idx];
7868 break;
7869 }
7870 }
7871 if (!uplink_veb) {
7872 dev_info(&pf->pdev->dev,
7873 "uplink seid %d not found\n", uplink_seid);
7874 return NULL;
7875 }
7876 }
7877
7878 /* get veb sw struct */
7879 veb_idx = i40e_veb_mem_alloc(pf);
7880 if (veb_idx < 0)
7881 goto err_alloc;
7882 veb = pf->veb[veb_idx];
7883 veb->flags = flags;
7884 veb->uplink_seid = uplink_seid;
7885 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
7886 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
7887
7888 /* create the VEB in the switch */
7889 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
7890 if (ret)
7891 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +00007892 if (vsi_idx == pf->lan_vsi)
7893 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007894
7895 return veb;
7896
7897err_veb:
7898 i40e_veb_clear(veb);
7899err_alloc:
7900 return NULL;
7901}
7902
7903/**
7904 * i40e_setup_pf_switch_element - set pf vars based on switch type
7905 * @pf: board private structure
7906 * @ele: element we are building info from
7907 * @num_reported: total number of elements
7908 * @printconfig: should we print the contents
7909 *
7910 * helper function to assist in extracting a few useful SEID values.
7911 **/
7912static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
7913 struct i40e_aqc_switch_config_element_resp *ele,
7914 u16 num_reported, bool printconfig)
7915{
7916 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
7917 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
7918 u8 element_type = ele->element_type;
7919 u16 seid = le16_to_cpu(ele->seid);
7920
7921 if (printconfig)
7922 dev_info(&pf->pdev->dev,
7923 "type=%d seid=%d uplink=%d downlink=%d\n",
7924 element_type, seid, uplink_seid, downlink_seid);
7925
7926 switch (element_type) {
7927 case I40E_SWITCH_ELEMENT_TYPE_MAC:
7928 pf->mac_seid = seid;
7929 break;
7930 case I40E_SWITCH_ELEMENT_TYPE_VEB:
7931 /* Main VEB? */
7932 if (uplink_seid != pf->mac_seid)
7933 break;
7934 if (pf->lan_veb == I40E_NO_VEB) {
7935 int v;
7936
7937 /* find existing or else empty VEB */
7938 for (v = 0; v < I40E_MAX_VEB; v++) {
7939 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
7940 pf->lan_veb = v;
7941 break;
7942 }
7943 }
7944 if (pf->lan_veb == I40E_NO_VEB) {
7945 v = i40e_veb_mem_alloc(pf);
7946 if (v < 0)
7947 break;
7948 pf->lan_veb = v;
7949 }
7950 }
7951
7952 pf->veb[pf->lan_veb]->seid = seid;
7953 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
7954 pf->veb[pf->lan_veb]->pf = pf;
7955 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
7956 break;
7957 case I40E_SWITCH_ELEMENT_TYPE_VSI:
7958 if (num_reported != 1)
7959 break;
7960 /* This is immediately after a reset so we can assume this is
7961 * the PF's VSI
7962 */
7963 pf->mac_seid = uplink_seid;
7964 pf->pf_seid = downlink_seid;
7965 pf->main_vsi_seid = seid;
7966 if (printconfig)
7967 dev_info(&pf->pdev->dev,
7968 "pf_seid=%d main_vsi_seid=%d\n",
7969 pf->pf_seid, pf->main_vsi_seid);
7970 break;
7971 case I40E_SWITCH_ELEMENT_TYPE_PF:
7972 case I40E_SWITCH_ELEMENT_TYPE_VF:
7973 case I40E_SWITCH_ELEMENT_TYPE_EMP:
7974 case I40E_SWITCH_ELEMENT_TYPE_BMC:
7975 case I40E_SWITCH_ELEMENT_TYPE_PE:
7976 case I40E_SWITCH_ELEMENT_TYPE_PA:
7977 /* ignore these for now */
7978 break;
7979 default:
7980 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
7981 element_type, seid);
7982 break;
7983 }
7984}
7985
7986/**
7987 * i40e_fetch_switch_configuration - Get switch config from firmware
7988 * @pf: board private structure
7989 * @printconfig: should we print the contents
7990 *
7991 * Get the current switch configuration from the device and
7992 * extract a few useful SEID values.
7993 **/
7994int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
7995{
7996 struct i40e_aqc_get_switch_config_resp *sw_config;
7997 u16 next_seid = 0;
7998 int ret = 0;
7999 u8 *aq_buf;
8000 int i;
8001
8002 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
8003 if (!aq_buf)
8004 return -ENOMEM;
8005
8006 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
8007 do {
8008 u16 num_reported, num_total;
8009
8010 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
8011 I40E_AQ_LARGE_BUF,
8012 &next_seid, NULL);
8013 if (ret) {
8014 dev_info(&pf->pdev->dev,
8015 "get switch config failed %d aq_err=%x\n",
8016 ret, pf->hw.aq.asq_last_status);
8017 kfree(aq_buf);
8018 return -ENOENT;
8019 }
8020
8021 num_reported = le16_to_cpu(sw_config->header.num_reported);
8022 num_total = le16_to_cpu(sw_config->header.num_total);
8023
8024 if (printconfig)
8025 dev_info(&pf->pdev->dev,
8026 "header: %d reported %d total\n",
8027 num_reported, num_total);
8028
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008029 for (i = 0; i < num_reported; i++) {
8030 struct i40e_aqc_switch_config_element_resp *ele =
8031 &sw_config->element[i];
8032
8033 i40e_setup_pf_switch_element(pf, ele, num_reported,
8034 printconfig);
8035 }
8036 } while (next_seid != 0);
8037
8038 kfree(aq_buf);
8039 return ret;
8040}
8041
8042/**
8043 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
8044 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008045 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008046 *
8047 * Returns 0 on success, negative value on failure
8048 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008049static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008050{
Jesse Brandeburg895106a2013-11-26 10:49:16 +00008051 u32 rxfc = 0, txfc = 0, rxfc_reg;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008052 int ret;
8053
8054 /* find out what's out there already */
8055 ret = i40e_fetch_switch_configuration(pf, false);
8056 if (ret) {
8057 dev_info(&pf->pdev->dev,
8058 "couldn't fetch switch config, err %d, aq_err %d\n",
8059 ret, pf->hw.aq.asq_last_status);
8060 return ret;
8061 }
8062 i40e_pf_reset_stats(pf);
8063
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008064 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008065 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008066 struct i40e_vsi *vsi = NULL;
8067 u16 uplink_seid;
8068
8069 /* Set up the PF VSI associated with the PF's main VSI
8070 * that is already in the HW switch
8071 */
8072 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
8073 uplink_seid = pf->veb[pf->lan_veb]->seid;
8074 else
8075 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008076 if (pf->lan_vsi == I40E_NO_VSI)
8077 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
8078 else if (reinit)
8079 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008080 if (!vsi) {
8081 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
8082 i40e_fdir_teardown(pf);
8083 return -EAGAIN;
8084 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008085 } else {
8086 /* force a reset of TC and queue layout configurations */
8087 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
8088 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
8089 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
8090 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
8091 }
8092 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
8093
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008094 i40e_fdir_sb_setup(pf);
8095
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008096 /* Setup static PF queue filter control settings */
8097 ret = i40e_setup_pf_filter_control(pf);
8098 if (ret) {
8099 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
8100 ret);
8101 /* Failure here should not stop continuing other steps */
8102 }
8103
8104 /* enable RSS in the HW, even for only one queue, as the stack can use
8105 * the hash
8106 */
8107 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
8108 i40e_config_rss(pf);
8109
8110 /* fill in link information and enable LSE reporting */
8111 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
8112 i40e_link_event(pf);
8113
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008114 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008115 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
8116 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008117 /* requested_mode is set in probe or by ethtool */
8118 if (!pf->fc_autoneg_status)
8119 goto no_autoneg;
8120
8121 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
8122 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008123 pf->hw.fc.current_mode = I40E_FC_FULL;
8124 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
8125 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
8126 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
8127 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
8128 else
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008129 pf->hw.fc.current_mode = I40E_FC_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008130
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008131 /* sync the flow control settings with the auto-neg values */
8132 switch (pf->hw.fc.current_mode) {
8133 case I40E_FC_FULL:
8134 txfc = 1;
8135 rxfc = 1;
8136 break;
8137 case I40E_FC_TX_PAUSE:
8138 txfc = 1;
8139 rxfc = 0;
8140 break;
8141 case I40E_FC_RX_PAUSE:
8142 txfc = 0;
8143 rxfc = 1;
8144 break;
8145 case I40E_FC_NONE:
8146 case I40E_FC_DEFAULT:
8147 txfc = 0;
8148 rxfc = 0;
8149 break;
8150 case I40E_FC_PFC:
8151 /* TBD */
8152 break;
8153 /* no default case, we have to handle all possibilities here */
8154 }
8155
8156 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
8157
8158 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8159 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
8160 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
8161
8162 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
8163
8164 goto fc_complete;
8165
8166no_autoneg:
8167 /* disable L2 flow control, user can turn it on if they wish */
8168 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
8169 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8170 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
8171
8172fc_complete:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008173 i40e_ptp_init(pf);
8174
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008175 return ret;
8176}
8177
8178/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008179 * i40e_determine_queue_usage - Work out queue distribution
8180 * @pf: board private structure
8181 **/
8182static void i40e_determine_queue_usage(struct i40e_pf *pf)
8183{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008184 int queues_left;
8185
8186 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008187
8188 /* Find the max queues to be put into basic use. We'll always be
8189 * using TC0, whether or not DCB is running, and TC0 will get the
8190 * big RSS set.
8191 */
8192 queues_left = pf->hw.func_caps.num_tx_qp;
8193
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008194 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +00008195 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008196 /* one qp for PF, no queues for anything else */
8197 queues_left = 0;
8198 pf->rss_size = pf->num_lan_qps = 1;
8199
8200 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008201 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
8202 I40E_FLAG_FD_SB_ENABLED |
8203 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008204 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008205 I40E_FLAG_SRIOV_ENABLED |
8206 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +00008207 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
8208 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +00008209 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008210 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +00008211 /* one qp for PF */
8212 pf->rss_size = pf->num_lan_qps = 1;
8213 queues_left -= pf->num_lan_qps;
8214
8215 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
8216 I40E_FLAG_FD_SB_ENABLED |
8217 I40E_FLAG_FD_ATR_ENABLED |
8218 I40E_FLAG_DCB_ENABLED |
8219 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008220 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008221 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008222 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008223 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008224 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008225 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
8226 }
8227 pf->num_lan_qps = pf->rss_size_max;
8228 queues_left -= pf->num_lan_qps;
8229 }
8230
8231 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8232 if (queues_left > 1) {
8233 queues_left -= 1; /* save 1 queue for FD */
8234 } else {
8235 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8236 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
8237 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008238 }
8239
8240 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
8241 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008242 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
8243 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008244 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
8245 }
8246
8247 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8248 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
8249 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
8250 (queues_left / pf->num_vmdq_qps));
8251 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
8252 }
8253
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008254 pf->queues_left = queues_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008255}
8256
8257/**
8258 * i40e_setup_pf_filter_control - Setup PF static filter control
8259 * @pf: PF to be setup
8260 *
8261 * i40e_setup_pf_filter_control sets up a pf's initial filter control
8262 * settings. If PE/FCoE are enabled then it will also set the per PF
8263 * based filter sizes required for them. It also enables Flow director,
8264 * ethertype and macvlan type filter settings for the pf.
8265 *
8266 * Returns 0 on success, negative on failure
8267 **/
8268static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
8269{
8270 struct i40e_filter_control_settings *settings = &pf->filter_settings;
8271
8272 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
8273
8274 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008275 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008276 settings->enable_fdir = true;
8277
8278 /* Ethtype and MACVLAN filters enabled for PF */
8279 settings->enable_ethtype = true;
8280 settings->enable_macvlan = true;
8281
8282 if (i40e_set_filter_control(&pf->hw, settings))
8283 return -ENOENT;
8284
8285 return 0;
8286}
8287
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008288#define INFO_STRING_LEN 255
8289static void i40e_print_features(struct i40e_pf *pf)
8290{
8291 struct i40e_hw *hw = &pf->hw;
8292 char *buf, *string;
8293
8294 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
8295 if (!string) {
8296 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
8297 return;
8298 }
8299
8300 buf = string;
8301
8302 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
8303#ifdef CONFIG_PCI_IOV
8304 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
8305#endif
8306 buf += sprintf(buf, "VSIs: %d QP: %d ", pf->hw.func_caps.num_vsis,
8307 pf->vsi[pf->lan_vsi]->num_queue_pairs);
8308
8309 if (pf->flags & I40E_FLAG_RSS_ENABLED)
8310 buf += sprintf(buf, "RSS ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008311 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00008312 buf += sprintf(buf, "FD_ATR ");
8313 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8314 buf += sprintf(buf, "FD_SB ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008315 buf += sprintf(buf, "NTUPLE ");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00008316 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008317 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008318 buf += sprintf(buf, "DCB ");
8319 if (pf->flags & I40E_FLAG_PTP)
8320 buf += sprintf(buf, "PTP ");
8321
8322 BUG_ON(buf > (string + INFO_STRING_LEN));
8323 dev_info(&pf->pdev->dev, "%s\n", string);
8324 kfree(string);
8325}
8326
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008327/**
8328 * i40e_probe - Device initialization routine
8329 * @pdev: PCI device information struct
8330 * @ent: entry in i40e_pci_tbl
8331 *
8332 * i40e_probe initializes a pf identified by a pci_dev structure.
8333 * The OS initialization, configuring of the pf private structure,
8334 * and a hardware reset occur.
8335 *
8336 * Returns 0 on success, negative on failure
8337 **/
8338static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8339{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008340 struct i40e_pf *pf;
8341 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008342 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008343 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008344 int err = 0;
8345 u32 len;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008346 u32 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008347
8348 err = pci_enable_device_mem(pdev);
8349 if (err)
8350 return err;
8351
8352 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +00008353 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00008354 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00008355 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
8356 if (err) {
8357 dev_err(&pdev->dev,
8358 "DMA configuration failed: 0x%x\n", err);
8359 goto err_dma;
8360 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008361 }
8362
8363 /* set up pci connections */
8364 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
8365 IORESOURCE_MEM), i40e_driver_name);
8366 if (err) {
8367 dev_info(&pdev->dev,
8368 "pci_request_selected_regions failed %d\n", err);
8369 goto err_pci_reg;
8370 }
8371
8372 pci_enable_pcie_error_reporting(pdev);
8373 pci_set_master(pdev);
8374
8375 /* Now that we have a PCI connection, we need to do the
8376 * low level device setup. This is primarily setting up
8377 * the Admin Queue structures and then querying for the
8378 * device's current profile information.
8379 */
8380 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
8381 if (!pf) {
8382 err = -ENOMEM;
8383 goto err_pf_alloc;
8384 }
8385 pf->next_vsi = 0;
8386 pf->pdev = pdev;
8387 set_bit(__I40E_DOWN, &pf->state);
8388
8389 hw = &pf->hw;
8390 hw->back = pf;
8391 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
8392 pci_resource_len(pdev, 0));
8393 if (!hw->hw_addr) {
8394 err = -EIO;
8395 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
8396 (unsigned int)pci_resource_start(pdev, 0),
8397 (unsigned int)pci_resource_len(pdev, 0), err);
8398 goto err_ioremap;
8399 }
8400 hw->vendor_id = pdev->vendor;
8401 hw->device_id = pdev->device;
8402 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
8403 hw->subsystem_vendor_id = pdev->subsystem_vendor;
8404 hw->subsystem_device_id = pdev->subsystem_device;
8405 hw->bus.device = PCI_SLOT(pdev->devfn);
8406 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008407 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008408
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008409 /* do a special CORER for clearing PXE mode once at init */
8410 if (hw->revision_id == 0 &&
8411 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
8412 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
8413 i40e_flush(hw);
8414 msleep(200);
8415 pf->corer_count++;
8416
8417 i40e_clear_pxe_mode(hw);
8418 }
8419
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008420 /* Reset here to make sure all is clean and to define PF 'n' */
8421 err = i40e_pf_reset(hw);
8422 if (err) {
8423 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
8424 goto err_pf_reset;
8425 }
8426 pf->pfr_count++;
8427
8428 hw->aq.num_arq_entries = I40E_AQ_LEN;
8429 hw->aq.num_asq_entries = I40E_AQ_LEN;
8430 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8431 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8432 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
8433 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
8434 "%s-pf%d:misc",
8435 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
8436
8437 err = i40e_init_shared_code(hw);
8438 if (err) {
8439 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
8440 goto err_pf_reset;
8441 }
8442
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008443 /* set up a default setting for link flow control */
8444 pf->hw.fc.requested_mode = I40E_FC_NONE;
8445
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008446 err = i40e_init_adminq(hw);
8447 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
8448 if (err) {
8449 dev_info(&pdev->dev,
8450 "init_adminq failed: %d expecting API %02x.%02x\n",
8451 err,
8452 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
8453 goto err_pf_reset;
8454 }
8455
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008456 i40e_verify_eeprom(pf);
8457
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +00008458 /* Rev 0 hardware was never productized */
8459 if (hw->revision_id < 1)
8460 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");
8461
Shannon Nelson6ff4ef82013-12-21 05:44:49 +00008462 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008463 err = i40e_get_capabilities(pf);
8464 if (err)
8465 goto err_adminq_setup;
8466
8467 err = i40e_sw_init(pf);
8468 if (err) {
8469 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
8470 goto err_sw_init;
8471 }
8472
8473 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
8474 hw->func_caps.num_rx_qp,
8475 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
8476 if (err) {
8477 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
8478 goto err_init_lan_hmc;
8479 }
8480
8481 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
8482 if (err) {
8483 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
8484 err = -ENOENT;
8485 goto err_configure_lan_hmc;
8486 }
8487
8488 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00008489 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008490 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
8491 err = -EIO;
8492 goto err_mac_addr;
8493 }
8494 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
8495 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
8496
8497 pci_set_drvdata(pdev, pf);
8498 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008499#ifdef CONFIG_I40E_DCB
8500 err = i40e_init_pf_dcb(pf);
8501 if (err) {
8502 dev_info(&pdev->dev, "init_pf_dcb failed: %d\n", err);
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008503 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Neerav Parikh014269f2014-04-01 07:11:48 +00008504 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008505 }
8506#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008507
8508 /* set up periodic task facility */
8509 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
8510 pf->service_timer_period = HZ;
8511
8512 INIT_WORK(&pf->service_task, i40e_service_task);
8513 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
8514 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
8515 pf->link_check_timeout = jiffies;
8516
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008517 /* WoL defaults to disabled */
8518 pf->wol_en = false;
8519 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
8520
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008521 /* set up the main switch operations */
8522 i40e_determine_queue_usage(pf);
8523 i40e_init_interrupt_scheme(pf);
8524
Mitch Williams505682c2014-05-20 08:01:37 +00008525 /* The number of VSIs reported by the FW is the minimum guaranteed
8526 * to us; HW supports far more and we share the remaining pool with
8527 * the other PFs. We allocate space for more than the guarantee with
8528 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008529 */
Mitch Williams505682c2014-05-20 08:01:37 +00008530 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
8531 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
8532 else
8533 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
8534
8535 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
8536 len = sizeof(struct i40e_vsi *) * pf->num_alloc_vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008537 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00008538 if (!pf->vsi) {
8539 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008540 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00008541 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008542
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008543 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008544 if (err) {
8545 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
8546 goto err_vsis;
8547 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008548 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +00008549 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008550 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
8551 i40e_vsi_open(pf->vsi[i]);
8552 break;
8553 }
8554 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008555
8556 /* The main driver is (mostly) up and happy. We need to set this state
8557 * before setting up the misc vector or we get a race and the vector
8558 * ends up disabled forever.
8559 */
8560 clear_bit(__I40E_DOWN, &pf->state);
8561
8562 /* In case of MSIX we are going to setup the misc vector right here
8563 * to handle admin queue events etc. In case of legacy and MSI
8564 * the misc functionality and queue processing is combined in
8565 * the same vector and that gets setup at open.
8566 */
8567 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8568 err = i40e_setup_misc_vector(pf);
8569 if (err) {
8570 dev_info(&pdev->dev,
8571 "setup of misc vector failed: %d\n", err);
8572 goto err_vsis;
8573 }
8574 }
8575
Greg Rosedf805f62014-04-04 04:43:16 +00008576#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008577 /* prep for VF support */
8578 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008579 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8580 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008581 u32 val;
8582
8583 /* disable link interrupts for VFs */
8584 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
8585 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
8586 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
8587 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -08008588
8589 if (pci_num_vf(pdev)) {
8590 dev_info(&pdev->dev,
8591 "Active VFs found, allocating resources.\n");
8592 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
8593 if (err)
8594 dev_info(&pdev->dev,
8595 "Error %d allocating resources for existing VFs\n",
8596 err);
8597 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008598 }
Greg Rosedf805f62014-04-04 04:43:16 +00008599#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008600
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008601 pfs_found++;
8602
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008603 i40e_dbg_pf_init(pf);
8604
8605 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00008606 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008607
8608 /* since everything's happy, start the service_task timer */
8609 mod_timer(&pf->service_timer,
8610 round_jiffies(jiffies + pf->service_timer_period));
8611
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008612 /* Get the negotiated link width and speed from PCI config space */
8613 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
8614
8615 i40e_set_pci_config_data(hw, link_status);
8616
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008617 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008618 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
8619 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
8620 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
8621 "Unknown"),
8622 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
8623 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
8624 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
8625 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
8626 "Unknown"));
8627
8628 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
8629 hw->bus.speed < i40e_bus_speed_8000) {
8630 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
8631 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
8632 }
8633
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008634 /* print a string summarizing features */
8635 i40e_print_features(pf);
8636
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008637 return 0;
8638
8639 /* Unwind what we've done if something failed in the setup */
8640err_vsis:
8641 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008642 i40e_clear_interrupt_scheme(pf);
8643 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +00008644err_switch_setup:
8645 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008646 del_timer_sync(&pf->service_timer);
8647err_mac_addr:
8648err_configure_lan_hmc:
8649 (void)i40e_shutdown_lan_hmc(hw);
8650err_init_lan_hmc:
8651 kfree(pf->qp_pile);
8652 kfree(pf->irq_pile);
8653err_sw_init:
8654err_adminq_setup:
8655 (void)i40e_shutdown_adminq(hw);
8656err_pf_reset:
8657 iounmap(hw->hw_addr);
8658err_ioremap:
8659 kfree(pf);
8660err_pf_alloc:
8661 pci_disable_pcie_error_reporting(pdev);
8662 pci_release_selected_regions(pdev,
8663 pci_select_bars(pdev, IORESOURCE_MEM));
8664err_pci_reg:
8665err_dma:
8666 pci_disable_device(pdev);
8667 return err;
8668}
8669
8670/**
8671 * i40e_remove - Device removal routine
8672 * @pdev: PCI device information struct
8673 *
8674 * i40e_remove is called by the PCI subsystem to alert the driver
8675 * that is should release a PCI device. This could be caused by a
8676 * Hot-Plug event, or because the driver is going to be removed from
8677 * memory.
8678 **/
8679static void i40e_remove(struct pci_dev *pdev)
8680{
8681 struct i40e_pf *pf = pci_get_drvdata(pdev);
8682 i40e_status ret_code;
8683 u32 reg;
8684 int i;
8685
8686 i40e_dbg_pf_exit(pf);
8687
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008688 i40e_ptp_stop(pf);
8689
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008690 /* no more scheduling of any task */
8691 set_bit(__I40E_DOWN, &pf->state);
8692 del_timer_sync(&pf->service_timer);
8693 cancel_work_sync(&pf->service_task);
8694
Mitch Williamseb2d80b2014-02-13 03:48:48 -08008695 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
8696 i40e_free_vfs(pf);
8697 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
8698 }
8699
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008700 i40e_fdir_teardown(pf);
8701
8702 /* If there is a switch structure or any orphans, remove them.
8703 * This will leave only the PF's VSI remaining.
8704 */
8705 for (i = 0; i < I40E_MAX_VEB; i++) {
8706 if (!pf->veb[i])
8707 continue;
8708
8709 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
8710 pf->veb[i]->uplink_seid == 0)
8711 i40e_switch_branch_release(pf->veb[i]);
8712 }
8713
8714 /* Now we can shutdown the PF's VSI, just before we kill
8715 * adminq and hmc.
8716 */
8717 if (pf->vsi[pf->lan_vsi])
8718 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
8719
8720 i40e_stop_misc_vector(pf);
8721 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8722 synchronize_irq(pf->msix_entries[0].vector);
8723 free_irq(pf->msix_entries[0].vector, pf);
8724 }
8725
8726 /* shutdown and destroy the HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00008727 if (pf->hw.hmc.hmc_obj) {
8728 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
8729 if (ret_code)
8730 dev_warn(&pdev->dev,
8731 "Failed to destroy the HMC resources: %d\n",
8732 ret_code);
8733 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008734
8735 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008736 ret_code = i40e_shutdown_adminq(&pf->hw);
8737 if (ret_code)
8738 dev_warn(&pdev->dev,
8739 "Failed to destroy the Admin Queue resources: %d\n",
8740 ret_code);
8741
8742 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
8743 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00008744 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008745 if (pf->vsi[i]) {
8746 i40e_vsi_clear_rings(pf->vsi[i]);
8747 i40e_vsi_clear(pf->vsi[i]);
8748 pf->vsi[i] = NULL;
8749 }
8750 }
8751
8752 for (i = 0; i < I40E_MAX_VEB; i++) {
8753 kfree(pf->veb[i]);
8754 pf->veb[i] = NULL;
8755 }
8756
8757 kfree(pf->qp_pile);
8758 kfree(pf->irq_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008759 kfree(pf->vsi);
8760
8761 /* force a PF reset to clean anything leftover */
8762 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
8763 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
8764 i40e_flush(&pf->hw);
8765
8766 iounmap(pf->hw.hw_addr);
8767 kfree(pf);
8768 pci_release_selected_regions(pdev,
8769 pci_select_bars(pdev, IORESOURCE_MEM));
8770
8771 pci_disable_pcie_error_reporting(pdev);
8772 pci_disable_device(pdev);
8773}
8774
8775/**
8776 * i40e_pci_error_detected - warning that something funky happened in PCI land
8777 * @pdev: PCI device information struct
8778 *
8779 * Called to warn that something happened and the error handling steps
8780 * are in progress. Allows the driver to quiesce things, be ready for
8781 * remediation.
8782 **/
8783static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
8784 enum pci_channel_state error)
8785{
8786 struct i40e_pf *pf = pci_get_drvdata(pdev);
8787
8788 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
8789
8790 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008791 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
8792 rtnl_lock();
8793 i40e_prep_for_reset(pf);
8794 rtnl_unlock();
8795 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008796
8797 /* Request a slot reset */
8798 return PCI_ERS_RESULT_NEED_RESET;
8799}
8800
8801/**
8802 * i40e_pci_error_slot_reset - a PCI slot reset just happened
8803 * @pdev: PCI device information struct
8804 *
8805 * Called to find if the driver can work with the device now that
8806 * the pci slot has been reset. If a basic connection seems good
8807 * (registers are readable and have sane content) then return a
8808 * happy little PCI_ERS_RESULT_xxx.
8809 **/
8810static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
8811{
8812 struct i40e_pf *pf = pci_get_drvdata(pdev);
8813 pci_ers_result_t result;
8814 int err;
8815 u32 reg;
8816
8817 dev_info(&pdev->dev, "%s\n", __func__);
8818 if (pci_enable_device_mem(pdev)) {
8819 dev_info(&pdev->dev,
8820 "Cannot re-enable PCI device after reset.\n");
8821 result = PCI_ERS_RESULT_DISCONNECT;
8822 } else {
8823 pci_set_master(pdev);
8824 pci_restore_state(pdev);
8825 pci_save_state(pdev);
8826 pci_wake_from_d3(pdev, false);
8827
8828 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8829 if (reg == 0)
8830 result = PCI_ERS_RESULT_RECOVERED;
8831 else
8832 result = PCI_ERS_RESULT_DISCONNECT;
8833 }
8834
8835 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8836 if (err) {
8837 dev_info(&pdev->dev,
8838 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8839 err);
8840 /* non-fatal, continue */
8841 }
8842
8843 return result;
8844}
8845
8846/**
8847 * i40e_pci_error_resume - restart operations after PCI error recovery
8848 * @pdev: PCI device information struct
8849 *
8850 * Called to allow the driver to bring things back up after PCI error
8851 * and/or reset recovery has finished.
8852 **/
8853static void i40e_pci_error_resume(struct pci_dev *pdev)
8854{
8855 struct i40e_pf *pf = pci_get_drvdata(pdev);
8856
8857 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008858 if (test_bit(__I40E_SUSPENDED, &pf->state))
8859 return;
8860
8861 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008862 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008863 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008864}
8865
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008866/**
8867 * i40e_shutdown - PCI callback for shutting down
8868 * @pdev: PCI device information struct
8869 **/
8870static void i40e_shutdown(struct pci_dev *pdev)
8871{
8872 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008873 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008874
8875 set_bit(__I40E_SUSPENDED, &pf->state);
8876 set_bit(__I40E_DOWN, &pf->state);
8877 rtnl_lock();
8878 i40e_prep_for_reset(pf);
8879 rtnl_unlock();
8880
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008881 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8882 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8883
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008884 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008885 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008886 pci_set_power_state(pdev, PCI_D3hot);
8887 }
8888}
8889
8890#ifdef CONFIG_PM
8891/**
8892 * i40e_suspend - PCI callback for moving to D3
8893 * @pdev: PCI device information struct
8894 **/
8895static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
8896{
8897 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008898 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008899
8900 set_bit(__I40E_SUSPENDED, &pf->state);
8901 set_bit(__I40E_DOWN, &pf->state);
8902 rtnl_lock();
8903 i40e_prep_for_reset(pf);
8904 rtnl_unlock();
8905
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008906 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8907 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8908
8909 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008910 pci_set_power_state(pdev, PCI_D3hot);
8911
8912 return 0;
8913}
8914
8915/**
8916 * i40e_resume - PCI callback for waking up from D3
8917 * @pdev: PCI device information struct
8918 **/
8919static int i40e_resume(struct pci_dev *pdev)
8920{
8921 struct i40e_pf *pf = pci_get_drvdata(pdev);
8922 u32 err;
8923
8924 pci_set_power_state(pdev, PCI_D0);
8925 pci_restore_state(pdev);
8926 /* pci_restore_state() clears dev->state_saves, so
8927 * call pci_save_state() again to restore it.
8928 */
8929 pci_save_state(pdev);
8930
8931 err = pci_enable_device_mem(pdev);
8932 if (err) {
8933 dev_err(&pdev->dev,
8934 "%s: Cannot enable PCI device from suspend\n",
8935 __func__);
8936 return err;
8937 }
8938 pci_set_master(pdev);
8939
8940 /* no wakeup events while running */
8941 pci_wake_from_d3(pdev, false);
8942
8943 /* handling the reset will rebuild the device state */
8944 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
8945 clear_bit(__I40E_DOWN, &pf->state);
8946 rtnl_lock();
8947 i40e_reset_and_rebuild(pf, false);
8948 rtnl_unlock();
8949 }
8950
8951 return 0;
8952}
8953
8954#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008955static const struct pci_error_handlers i40e_err_handler = {
8956 .error_detected = i40e_pci_error_detected,
8957 .slot_reset = i40e_pci_error_slot_reset,
8958 .resume = i40e_pci_error_resume,
8959};
8960
8961static struct pci_driver i40e_driver = {
8962 .name = i40e_driver_name,
8963 .id_table = i40e_pci_tbl,
8964 .probe = i40e_probe,
8965 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008966#ifdef CONFIG_PM
8967 .suspend = i40e_suspend,
8968 .resume = i40e_resume,
8969#endif
8970 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008971 .err_handler = &i40e_err_handler,
8972 .sriov_configure = i40e_pci_sriov_configure,
8973};
8974
8975/**
8976 * i40e_init_module - Driver registration routine
8977 *
8978 * i40e_init_module is the first routine called when the driver is
8979 * loaded. All it does is register with the PCI subsystem.
8980 **/
8981static int __init i40e_init_module(void)
8982{
8983 pr_info("%s: %s - version %s\n", i40e_driver_name,
8984 i40e_driver_string, i40e_driver_version_str);
8985 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
8986 i40e_dbg_init();
8987 return pci_register_driver(&i40e_driver);
8988}
8989module_init(i40e_init_module);
8990
8991/**
8992 * i40e_exit_module - Driver exit cleanup routine
8993 *
8994 * i40e_exit_module is called just before the driver is removed
8995 * from memory.
8996 **/
8997static void __exit i40e_exit_module(void)
8998{
8999 pci_unregister_driver(&i40e_driver);
9000 i40e_dbg_exit();
9001}
9002module_exit(i40e_exit_module);