blob: 853eb2f7e558d02d46b09c111e16d7fd6a1042dd [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Catherine Sullivane8278452015-02-06 08:52:08 +00004 * Copyright(c) 2013 - 2015 Intel Corporation.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
27/* Local includes */
28#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000029#include "i40e_diag.h"
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000030#ifdef CONFIG_I40E_VXLAN
31#include <net/vxlan.h>
32#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000033
34const char i40e_driver_name[] = "i40e";
35static const char i40e_driver_string[] =
36 "Intel(R) Ethernet Connection XL710 Network Driver";
37
38#define DRV_KERN "-k"
39
Catherine Sullivane8e724d2014-07-10 07:58:26 +000040#define DRV_VERSION_MAJOR 1
Catherine Sullivan42d255c2015-03-31 00:45:06 -070041#define DRV_VERSION_MINOR 3
Catherine Sullivan2aea6dc2015-04-07 19:45:42 -040042#define DRV_VERSION_BUILD 2
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000043#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
44 __stringify(DRV_VERSION_MINOR) "." \
45 __stringify(DRV_VERSION_BUILD) DRV_KERN
46const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080047static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000048
49/* a bit of forward declarations */
50static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
51static void i40e_handle_reset_warning(struct i40e_pf *pf);
52static int i40e_add_vsi(struct i40e_vsi *vsi);
53static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000054static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000055static int i40e_setup_misc_vector(struct i40e_pf *pf);
56static void i40e_determine_queue_usage(struct i40e_pf *pf);
57static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080058static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080059static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000060
61/* i40e_pci_tbl - PCI Device ID Table
62 *
63 * Last entry must be all 0s
64 *
65 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
66 * Class, Class Mask, private data (not used) }
67 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020068static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080069 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080070 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
71 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_A), 0},
72 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
73 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080074 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
75 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
76 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000077 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Jesse Brandeburgae24b402015-03-27 00:12:09 -070078 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000079 /* required last entry */
80 {0, }
81};
82MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
83
84#define I40E_MAX_VF_COUNT 128
85static int debug = -1;
86module_param(debug, int, 0);
87MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
88
89MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
90MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
91MODULE_LICENSE("GPL");
92MODULE_VERSION(DRV_VERSION);
93
94/**
95 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
96 * @hw: pointer to the HW structure
97 * @mem: ptr to mem struct to fill out
98 * @size: size of memory requested
99 * @alignment: what to align the allocation to
100 **/
101int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
102 u64 size, u32 alignment)
103{
104 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
105
106 mem->size = ALIGN(size, alignment);
107 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
108 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000109 if (!mem->va)
110 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000111
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000112 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000113}
114
115/**
116 * i40e_free_dma_mem_d - OS specific memory free for shared code
117 * @hw: pointer to the HW structure
118 * @mem: ptr to mem struct to free
119 **/
120int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
121{
122 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
123
124 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
125 mem->va = NULL;
126 mem->pa = 0;
127 mem->size = 0;
128
129 return 0;
130}
131
132/**
133 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
134 * @hw: pointer to the HW structure
135 * @mem: ptr to mem struct to fill out
136 * @size: size of memory requested
137 **/
138int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
139 u32 size)
140{
141 mem->size = size;
142 mem->va = kzalloc(size, GFP_KERNEL);
143
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000144 if (!mem->va)
145 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000146
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000147 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000148}
149
150/**
151 * i40e_free_virt_mem_d - OS specific memory free for shared code
152 * @hw: pointer to the HW structure
153 * @mem: ptr to mem struct to free
154 **/
155int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
156{
157 /* it's ok to kfree a NULL pointer */
158 kfree(mem->va);
159 mem->va = NULL;
160 mem->size = 0;
161
162 return 0;
163}
164
165/**
166 * i40e_get_lump - find a lump of free generic resource
167 * @pf: board private structure
168 * @pile: the pile of resource to search
169 * @needed: the number of items needed
170 * @id: an owner id to stick on the items assigned
171 *
172 * Returns the base item index of the lump, or negative for error
173 *
174 * The search_hint trick and lack of advanced fit-finding only work
175 * because we're highly likely to have all the same size lump requests.
176 * Linear search time and any fragmentation should be minimal.
177 **/
178static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
179 u16 needed, u16 id)
180{
181 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000182 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000183
184 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
185 dev_info(&pf->pdev->dev,
186 "param err: pile=%p needed=%d id=0x%04x\n",
187 pile, needed, id);
188 return -EINVAL;
189 }
190
191 /* start the linear search with an imperfect hint */
192 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000193 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000194 /* skip already allocated entries */
195 if (pile->list[i] & I40E_PILE_VALID_BIT) {
196 i++;
197 continue;
198 }
199
200 /* do we have enough in this lump? */
201 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
202 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
203 break;
204 }
205
206 if (j == needed) {
207 /* there was enough, so assign it to the requestor */
208 for (j = 0; j < needed; j++)
209 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
210 ret = i;
211 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000212 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000213 } else {
214 /* not enough, so skip over it and continue looking */
215 i += j;
216 }
217 }
218
219 return ret;
220}
221
222/**
223 * i40e_put_lump - return a lump of generic resource
224 * @pile: the pile of resource to search
225 * @index: the base item index
226 * @id: the owner id of the items assigned
227 *
228 * Returns the count of items in the lump
229 **/
230static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
231{
232 int valid_id = (id | I40E_PILE_VALID_BIT);
233 int count = 0;
234 int i;
235
236 if (!pile || index >= pile->num_entries)
237 return -EINVAL;
238
239 for (i = index;
240 i < pile->num_entries && pile->list[i] == valid_id;
241 i++) {
242 pile->list[i] = 0;
243 count++;
244 }
245
246 if (count && index < pile->search_hint)
247 pile->search_hint = index;
248
249 return count;
250}
251
252/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700253 * i40e_find_vsi_from_id - searches for the vsi with the given id
254 * @pf - the pf structure to search for the vsi
255 * @id - id of the vsi it is searching for
256 **/
257struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
258{
259 int i;
260
261 for (i = 0; i < pf->num_alloc_vsi; i++)
262 if (pf->vsi[i] && (pf->vsi[i]->id == id))
263 return pf->vsi[i];
264
265 return NULL;
266}
267
268/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000269 * i40e_service_event_schedule - Schedule the service task to wake up
270 * @pf: board private structure
271 *
272 * If not already scheduled, this puts the task into the work queue
273 **/
274static void i40e_service_event_schedule(struct i40e_pf *pf)
275{
276 if (!test_bit(__I40E_DOWN, &pf->state) &&
277 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
278 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
279 schedule_work(&pf->service_task);
280}
281
282/**
283 * i40e_tx_timeout - Respond to a Tx Hang
284 * @netdev: network interface device structure
285 *
286 * If any port has noticed a Tx timeout, it is likely that the whole
287 * device is munged, not just the one netdev port, so go for the full
288 * reset.
289 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700290#ifdef I40E_FCOE
291void i40e_tx_timeout(struct net_device *netdev)
292#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000293static void i40e_tx_timeout(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -0700294#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000295{
296 struct i40e_netdev_priv *np = netdev_priv(netdev);
297 struct i40e_vsi *vsi = np->vsi;
298 struct i40e_pf *pf = vsi->back;
299
300 pf->tx_timeout_count++;
301
302 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Anjali Singhai Jain327fe042014-06-04 01:23:26 +0000303 pf->tx_timeout_recovery_level = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000304 pf->tx_timeout_last_recovery = jiffies;
305 netdev_info(netdev, "tx_timeout recovery level %d\n",
306 pf->tx_timeout_recovery_level);
307
308 switch (pf->tx_timeout_recovery_level) {
309 case 0:
310 /* disable and re-enable queues for the VSI */
311 if (in_interrupt()) {
312 set_bit(__I40E_REINIT_REQUESTED, &pf->state);
313 set_bit(__I40E_REINIT_REQUESTED, &vsi->state);
314 } else {
315 i40e_vsi_reinit_locked(vsi);
316 }
317 break;
318 case 1:
319 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
320 break;
321 case 2:
322 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
323 break;
324 case 3:
325 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
326 break;
327 default:
328 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Neerav Parikhb5d06f02014-06-03 23:50:17 +0000329 set_bit(__I40E_DOWN_REQUESTED, &pf->state);
330 set_bit(__I40E_DOWN_REQUESTED, &vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000331 break;
332 }
333 i40e_service_event_schedule(pf);
334 pf->tx_timeout_recovery_level++;
335}
336
337/**
338 * i40e_release_rx_desc - Store the new tail and head values
339 * @rx_ring: ring to bump
340 * @val: new head index
341 **/
342static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
343{
344 rx_ring->next_to_use = val;
345
346 /* Force memory writes to complete before letting h/w
347 * know there are new descriptors to fetch. (Only
348 * applicable for weak-ordered memory model archs,
349 * such as IA-64).
350 */
351 wmb();
352 writel(val, rx_ring->tail);
353}
354
355/**
356 * i40e_get_vsi_stats_struct - Get System Network Statistics
357 * @vsi: the VSI we care about
358 *
359 * Returns the address of the device statistics structure.
360 * The statistics are actually updated from the service task.
361 **/
362struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
363{
364 return &vsi->net_stats;
365}
366
367/**
368 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
369 * @netdev: network interface device structure
370 *
371 * Returns the address of the device statistics structure.
372 * The statistics are actually updated from the service task.
373 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700374#ifdef I40E_FCOE
375struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
376 struct net_device *netdev,
377 struct rtnl_link_stats64 *stats)
378#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000379static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
380 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000381 struct rtnl_link_stats64 *stats)
Vasu Dev38e00432014-08-01 13:27:03 -0700382#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000383{
384 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000385 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000386 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000387 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
388 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000389
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000390 if (test_bit(__I40E_DOWN, &vsi->state))
391 return stats;
392
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800393 if (!vsi->tx_rings)
394 return stats;
395
Alexander Duyck980e9b12013-09-28 06:01:03 +0000396 rcu_read_lock();
397 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000398 u64 bytes, packets;
399 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000400
Alexander Duyck980e9b12013-09-28 06:01:03 +0000401 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
402 if (!tx_ring)
403 continue;
404
405 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700406 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000407 packets = tx_ring->stats.packets;
408 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700409 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000410
411 stats->tx_packets += packets;
412 stats->tx_bytes += bytes;
413 rx_ring = &tx_ring[1];
414
415 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700416 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000417 packets = rx_ring->stats.packets;
418 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700419 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000420
421 stats->rx_packets += packets;
422 stats->rx_bytes += bytes;
423 }
424 rcu_read_unlock();
425
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000426 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000427 stats->multicast = vsi_stats->multicast;
428 stats->tx_errors = vsi_stats->tx_errors;
429 stats->tx_dropped = vsi_stats->tx_dropped;
430 stats->rx_errors = vsi_stats->rx_errors;
431 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
432 stats->rx_length_errors = vsi_stats->rx_length_errors;
433
434 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000435}
436
437/**
438 * i40e_vsi_reset_stats - Resets all stats of the given vsi
439 * @vsi: the VSI to have its stats reset
440 **/
441void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
442{
443 struct rtnl_link_stats64 *ns;
444 int i;
445
446 if (!vsi)
447 return;
448
449 ns = i40e_get_vsi_stats_struct(vsi);
450 memset(ns, 0, sizeof(*ns));
451 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
452 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
453 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000454 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000455 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +0000456 memset(&vsi->rx_rings[i]->stats, 0 ,
457 sizeof(vsi->rx_rings[i]->stats));
458 memset(&vsi->rx_rings[i]->rx_stats, 0 ,
459 sizeof(vsi->rx_rings[i]->rx_stats));
460 memset(&vsi->tx_rings[i]->stats, 0 ,
461 sizeof(vsi->tx_rings[i]->stats));
462 memset(&vsi->tx_rings[i]->tx_stats, 0,
463 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000464 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000465 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000466 vsi->stat_offsets_loaded = false;
467}
468
469/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000470 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000471 * @pf: the PF to be reset
472 **/
473void i40e_pf_reset_stats(struct i40e_pf *pf)
474{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000475 int i;
476
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000477 memset(&pf->stats, 0, sizeof(pf->stats));
478 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
479 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000480
481 for (i = 0; i < I40E_MAX_VEB; i++) {
482 if (pf->veb[i]) {
483 memset(&pf->veb[i]->stats, 0,
484 sizeof(pf->veb[i]->stats));
485 memset(&pf->veb[i]->stats_offsets, 0,
486 sizeof(pf->veb[i]->stats_offsets));
487 pf->veb[i]->stat_offsets_loaded = false;
488 }
489 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000490}
491
492/**
493 * i40e_stat_update48 - read and update a 48 bit stat from the chip
494 * @hw: ptr to the hardware info
495 * @hireg: the high 32 bit reg to read
496 * @loreg: the low 32 bit reg to read
497 * @offset_loaded: has the initial offset been loaded yet
498 * @offset: ptr to current offset value
499 * @stat: ptr to the stat
500 *
501 * Since the device stats are not reset at PFReset, they likely will not
502 * be zeroed when the driver starts. We'll save the first values read
503 * and use them as offsets to be subtracted from the raw values in order
504 * to report stats that count from zero. In the process, we also manage
505 * the potential roll-over.
506 **/
507static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
508 bool offset_loaded, u64 *offset, u64 *stat)
509{
510 u64 new_data;
511
Shannon Nelsonab600852014-01-17 15:36:39 -0800512 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000513 new_data = rd32(hw, loreg);
514 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
515 } else {
516 new_data = rd64(hw, loreg);
517 }
518 if (!offset_loaded)
519 *offset = new_data;
520 if (likely(new_data >= *offset))
521 *stat = new_data - *offset;
522 else
523 *stat = (new_data + ((u64)1 << 48)) - *offset;
524 *stat &= 0xFFFFFFFFFFFFULL;
525}
526
527/**
528 * i40e_stat_update32 - read and update a 32 bit stat from the chip
529 * @hw: ptr to the hardware info
530 * @reg: the hw reg to read
531 * @offset_loaded: has the initial offset been loaded yet
532 * @offset: ptr to current offset value
533 * @stat: ptr to the stat
534 **/
535static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
536 bool offset_loaded, u64 *offset, u64 *stat)
537{
538 u32 new_data;
539
540 new_data = rd32(hw, reg);
541 if (!offset_loaded)
542 *offset = new_data;
543 if (likely(new_data >= *offset))
544 *stat = (u32)(new_data - *offset);
545 else
546 *stat = (u32)((new_data + ((u64)1 << 32)) - *offset);
547}
548
549/**
550 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
551 * @vsi: the VSI to be updated
552 **/
553void i40e_update_eth_stats(struct i40e_vsi *vsi)
554{
555 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
556 struct i40e_pf *pf = vsi->back;
557 struct i40e_hw *hw = &pf->hw;
558 struct i40e_eth_stats *oes;
559 struct i40e_eth_stats *es; /* device's eth stats */
560
561 es = &vsi->eth_stats;
562 oes = &vsi->eth_stats_offsets;
563
564 /* Gather up the stats that the hw collects */
565 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
566 vsi->stat_offsets_loaded,
567 &oes->tx_errors, &es->tx_errors);
568 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
569 vsi->stat_offsets_loaded,
570 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000571 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
572 vsi->stat_offsets_loaded,
573 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
574 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
575 vsi->stat_offsets_loaded,
576 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000577
578 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
579 I40E_GLV_GORCL(stat_idx),
580 vsi->stat_offsets_loaded,
581 &oes->rx_bytes, &es->rx_bytes);
582 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
583 I40E_GLV_UPRCL(stat_idx),
584 vsi->stat_offsets_loaded,
585 &oes->rx_unicast, &es->rx_unicast);
586 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
587 I40E_GLV_MPRCL(stat_idx),
588 vsi->stat_offsets_loaded,
589 &oes->rx_multicast, &es->rx_multicast);
590 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
591 I40E_GLV_BPRCL(stat_idx),
592 vsi->stat_offsets_loaded,
593 &oes->rx_broadcast, &es->rx_broadcast);
594
595 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
596 I40E_GLV_GOTCL(stat_idx),
597 vsi->stat_offsets_loaded,
598 &oes->tx_bytes, &es->tx_bytes);
599 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
600 I40E_GLV_UPTCL(stat_idx),
601 vsi->stat_offsets_loaded,
602 &oes->tx_unicast, &es->tx_unicast);
603 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
604 I40E_GLV_MPTCL(stat_idx),
605 vsi->stat_offsets_loaded,
606 &oes->tx_multicast, &es->tx_multicast);
607 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
608 I40E_GLV_BPTCL(stat_idx),
609 vsi->stat_offsets_loaded,
610 &oes->tx_broadcast, &es->tx_broadcast);
611 vsi->stat_offsets_loaded = true;
612}
613
614/**
615 * i40e_update_veb_stats - Update Switch component statistics
616 * @veb: the VEB being updated
617 **/
618static void i40e_update_veb_stats(struct i40e_veb *veb)
619{
620 struct i40e_pf *pf = veb->pf;
621 struct i40e_hw *hw = &pf->hw;
622 struct i40e_eth_stats *oes;
623 struct i40e_eth_stats *es; /* device's eth stats */
624 int idx = 0;
625
626 idx = veb->stats_idx;
627 es = &veb->stats;
628 oes = &veb->stats_offsets;
629
630 /* Gather up the stats that the hw collects */
631 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
632 veb->stat_offsets_loaded,
633 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000634 if (hw->revision_id > 0)
635 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
636 veb->stat_offsets_loaded,
637 &oes->rx_unknown_protocol,
638 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000639 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
640 veb->stat_offsets_loaded,
641 &oes->rx_bytes, &es->rx_bytes);
642 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
643 veb->stat_offsets_loaded,
644 &oes->rx_unicast, &es->rx_unicast);
645 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
646 veb->stat_offsets_loaded,
647 &oes->rx_multicast, &es->rx_multicast);
648 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
649 veb->stat_offsets_loaded,
650 &oes->rx_broadcast, &es->rx_broadcast);
651
652 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
653 veb->stat_offsets_loaded,
654 &oes->tx_bytes, &es->tx_bytes);
655 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
656 veb->stat_offsets_loaded,
657 &oes->tx_unicast, &es->tx_unicast);
658 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
659 veb->stat_offsets_loaded,
660 &oes->tx_multicast, &es->tx_multicast);
661 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
662 veb->stat_offsets_loaded,
663 &oes->tx_broadcast, &es->tx_broadcast);
664 veb->stat_offsets_loaded = true;
665}
666
Vasu Dev38e00432014-08-01 13:27:03 -0700667#ifdef I40E_FCOE
668/**
669 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
670 * @vsi: the VSI that is capable of doing FCoE
671 **/
672static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
673{
674 struct i40e_pf *pf = vsi->back;
675 struct i40e_hw *hw = &pf->hw;
676 struct i40e_fcoe_stats *ofs;
677 struct i40e_fcoe_stats *fs; /* device's eth stats */
678 int idx;
679
680 if (vsi->type != I40E_VSI_FCOE)
681 return;
682
683 idx = (pf->pf_seid - I40E_BASE_PF_SEID) + I40E_FCOE_PF_STAT_OFFSET;
684 fs = &vsi->fcoe_stats;
685 ofs = &vsi->fcoe_stats_offsets;
686
687 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
688 vsi->fcoe_stat_offsets_loaded,
689 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
690 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
691 vsi->fcoe_stat_offsets_loaded,
692 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
693 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
694 vsi->fcoe_stat_offsets_loaded,
695 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
696 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
697 vsi->fcoe_stat_offsets_loaded,
698 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
699 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
700 vsi->fcoe_stat_offsets_loaded,
701 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
702 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
703 vsi->fcoe_stat_offsets_loaded,
704 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
705 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
706 vsi->fcoe_stat_offsets_loaded,
707 &ofs->fcoe_last_error, &fs->fcoe_last_error);
708 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
709 vsi->fcoe_stat_offsets_loaded,
710 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
711
712 vsi->fcoe_stat_offsets_loaded = true;
713}
714
715#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000716/**
717 * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
718 * @pf: the corresponding PF
719 *
720 * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
721 **/
722static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
723{
724 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
725 struct i40e_hw_port_stats *nsd = &pf->stats;
726 struct i40e_hw *hw = &pf->hw;
727 u64 xoff = 0;
728 u16 i, v;
729
730 if ((hw->fc.current_mode != I40E_FC_FULL) &&
731 (hw->fc.current_mode != I40E_FC_RX_PAUSE))
732 return;
733
734 xoff = nsd->link_xoff_rx;
735 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
736 pf->stat_offsets_loaded,
737 &osd->link_xoff_rx, &nsd->link_xoff_rx);
738
739 /* No new LFC xoff rx */
740 if (!(nsd->link_xoff_rx - xoff))
741 return;
742
743 /* Clear the __I40E_HANG_CHECK_ARMED bit for all Tx rings */
Mitch Williams505682c2014-05-20 08:01:37 +0000744 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000745 struct i40e_vsi *vsi = pf->vsi[v];
746
Mitch Williamsddfda802014-05-10 04:49:10 +0000747 if (!vsi || !vsi->tx_rings[0])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000748 continue;
749
750 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +0000751 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000752 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
753 }
754 }
755}
756
757/**
758 * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
759 * @pf: the corresponding PF
760 *
761 * Update the Rx XOFF counter (PAUSE frames) in PFC mode
762 **/
763static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
764{
765 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
766 struct i40e_hw_port_stats *nsd = &pf->stats;
767 bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
768 struct i40e_dcbx_config *dcb_cfg;
769 struct i40e_hw *hw = &pf->hw;
770 u16 i, v;
771 u8 tc;
772
773 dcb_cfg = &hw->local_dcbx_config;
774
775 /* See if DCB enabled with PFC TC */
776 if (!(pf->flags & I40E_FLAG_DCB_ENABLED) ||
777 !(dcb_cfg->pfc.pfcenable)) {
778 i40e_update_link_xoff_rx(pf);
779 return;
780 }
781
782 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
783 u64 prio_xoff = nsd->priority_xoff_rx[i];
784 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
785 pf->stat_offsets_loaded,
786 &osd->priority_xoff_rx[i],
787 &nsd->priority_xoff_rx[i]);
788
789 /* No new PFC xoff rx */
790 if (!(nsd->priority_xoff_rx[i] - prio_xoff))
791 continue;
792 /* Get the TC for given priority */
793 tc = dcb_cfg->etscfg.prioritytable[i];
794 xoff[tc] = true;
795 }
796
797 /* Clear the __I40E_HANG_CHECK_ARMED bit for Tx rings */
Mitch Williams505682c2014-05-20 08:01:37 +0000798 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000799 struct i40e_vsi *vsi = pf->vsi[v];
800
Mitch Williamsddfda802014-05-10 04:49:10 +0000801 if (!vsi || !vsi->tx_rings[0])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000802 continue;
803
804 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +0000805 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000806
807 tc = ring->dcb_tc;
808 if (xoff[tc])
809 clear_bit(__I40E_HANG_CHECK_ARMED,
810 &ring->state);
811 }
812 }
813}
814
815/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000816 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000817 * @vsi: the VSI to be updated
818 *
819 * There are a few instances where we store the same stat in a
820 * couple of different structs. This is partly because we have
821 * the netdev stats that need to be filled out, which is slightly
822 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000823 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000824 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000825static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000826{
827 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000828 struct rtnl_link_stats64 *ons;
829 struct rtnl_link_stats64 *ns; /* netdev stats */
830 struct i40e_eth_stats *oes;
831 struct i40e_eth_stats *es; /* device's eth stats */
832 u32 tx_restart, tx_busy;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000833 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000834 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000835 u64 bytes, packets;
836 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000837 u64 rx_p, rx_b;
838 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000839 u16 q;
840
841 if (test_bit(__I40E_DOWN, &vsi->state) ||
842 test_bit(__I40E_CONFIG_BUSY, &pf->state))
843 return;
844
845 ns = i40e_get_vsi_stats_struct(vsi);
846 ons = &vsi->net_stats_offsets;
847 es = &vsi->eth_stats;
848 oes = &vsi->eth_stats_offsets;
849
850 /* Gather up the netdev and vsi stats that the driver collects
851 * on the fly during packet processing
852 */
853 rx_b = rx_p = 0;
854 tx_b = tx_p = 0;
855 tx_restart = tx_busy = 0;
856 rx_page = 0;
857 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000858 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000859 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000860 /* locate Tx ring */
861 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000862
Alexander Duyck980e9b12013-09-28 06:01:03 +0000863 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700864 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000865 packets = p->stats.packets;
866 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700867 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000868 tx_b += bytes;
869 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000870 tx_restart += p->tx_stats.restart_queue;
871 tx_busy += p->tx_stats.tx_busy;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000872
873 /* Rx queue is part of the same block as Tx queue */
874 p = &p[1];
875 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700876 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000877 packets = p->stats.packets;
878 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700879 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000880 rx_b += bytes;
881 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000882 rx_buf += p->rx_stats.alloc_buff_failed;
883 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000884 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000885 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000886 vsi->tx_restart = tx_restart;
887 vsi->tx_busy = tx_busy;
888 vsi->rx_page_failed = rx_page;
889 vsi->rx_buf_failed = rx_buf;
890
891 ns->rx_packets = rx_p;
892 ns->rx_bytes = rx_b;
893 ns->tx_packets = tx_p;
894 ns->tx_bytes = tx_b;
895
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000896 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000897 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000898 ons->tx_errors = oes->tx_errors;
899 ns->tx_errors = es->tx_errors;
900 ons->multicast = oes->rx_multicast;
901 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000902 ons->rx_dropped = oes->rx_discards;
903 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000904 ons->tx_dropped = oes->tx_discards;
905 ns->tx_dropped = es->tx_discards;
906
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000907 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000908 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000909 ns->rx_crc_errors = pf->stats.crc_errors;
910 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
911 ns->rx_length_errors = pf->stats.rx_length_errors;
912 }
913}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000914
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000915/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000916 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000917 * @pf: the PF to be updated
918 **/
919static void i40e_update_pf_stats(struct i40e_pf *pf)
920{
921 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
922 struct i40e_hw_port_stats *nsd = &pf->stats;
923 struct i40e_hw *hw = &pf->hw;
924 u32 val;
925 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000926
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000927 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
928 I40E_GLPRT_GORCL(hw->port),
929 pf->stat_offsets_loaded,
930 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
931 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
932 I40E_GLPRT_GOTCL(hw->port),
933 pf->stat_offsets_loaded,
934 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
935 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
936 pf->stat_offsets_loaded,
937 &osd->eth.rx_discards,
938 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000939 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
940 I40E_GLPRT_UPRCL(hw->port),
941 pf->stat_offsets_loaded,
942 &osd->eth.rx_unicast,
943 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000944 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
945 I40E_GLPRT_MPRCL(hw->port),
946 pf->stat_offsets_loaded,
947 &osd->eth.rx_multicast,
948 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000949 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
950 I40E_GLPRT_BPRCL(hw->port),
951 pf->stat_offsets_loaded,
952 &osd->eth.rx_broadcast,
953 &nsd->eth.rx_broadcast);
954 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
955 I40E_GLPRT_UPTCL(hw->port),
956 pf->stat_offsets_loaded,
957 &osd->eth.tx_unicast,
958 &nsd->eth.tx_unicast);
959 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
960 I40E_GLPRT_MPTCL(hw->port),
961 pf->stat_offsets_loaded,
962 &osd->eth.tx_multicast,
963 &nsd->eth.tx_multicast);
964 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
965 I40E_GLPRT_BPTCL(hw->port),
966 pf->stat_offsets_loaded,
967 &osd->eth.tx_broadcast,
968 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000969
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000970 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
971 pf->stat_offsets_loaded,
972 &osd->tx_dropped_link_down,
973 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000974
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000975 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
976 pf->stat_offsets_loaded,
977 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000978
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000979 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
980 pf->stat_offsets_loaded,
981 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000982
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000983 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
984 pf->stat_offsets_loaded,
985 &osd->mac_local_faults,
986 &nsd->mac_local_faults);
987 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
988 pf->stat_offsets_loaded,
989 &osd->mac_remote_faults,
990 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000991
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000992 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
993 pf->stat_offsets_loaded,
994 &osd->rx_length_errors,
995 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000996
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000997 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
998 pf->stat_offsets_loaded,
999 &osd->link_xon_rx, &nsd->link_xon_rx);
1000 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1001 pf->stat_offsets_loaded,
1002 &osd->link_xon_tx, &nsd->link_xon_tx);
1003 i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
1004 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1005 pf->stat_offsets_loaded,
1006 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001007
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001008 for (i = 0; i < 8; i++) {
1009 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001010 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001011 &osd->priority_xon_rx[i],
1012 &nsd->priority_xon_rx[i]);
1013 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001014 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001015 &osd->priority_xon_tx[i],
1016 &nsd->priority_xon_tx[i]);
1017 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001018 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001019 &osd->priority_xoff_tx[i],
1020 &nsd->priority_xoff_tx[i]);
1021 i40e_stat_update32(hw,
1022 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001023 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001024 &osd->priority_xon_2_xoff[i],
1025 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001026 }
1027
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001028 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1029 I40E_GLPRT_PRC64L(hw->port),
1030 pf->stat_offsets_loaded,
1031 &osd->rx_size_64, &nsd->rx_size_64);
1032 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1033 I40E_GLPRT_PRC127L(hw->port),
1034 pf->stat_offsets_loaded,
1035 &osd->rx_size_127, &nsd->rx_size_127);
1036 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1037 I40E_GLPRT_PRC255L(hw->port),
1038 pf->stat_offsets_loaded,
1039 &osd->rx_size_255, &nsd->rx_size_255);
1040 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1041 I40E_GLPRT_PRC511L(hw->port),
1042 pf->stat_offsets_loaded,
1043 &osd->rx_size_511, &nsd->rx_size_511);
1044 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1045 I40E_GLPRT_PRC1023L(hw->port),
1046 pf->stat_offsets_loaded,
1047 &osd->rx_size_1023, &nsd->rx_size_1023);
1048 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1049 I40E_GLPRT_PRC1522L(hw->port),
1050 pf->stat_offsets_loaded,
1051 &osd->rx_size_1522, &nsd->rx_size_1522);
1052 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1053 I40E_GLPRT_PRC9522L(hw->port),
1054 pf->stat_offsets_loaded,
1055 &osd->rx_size_big, &nsd->rx_size_big);
1056
1057 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1058 I40E_GLPRT_PTC64L(hw->port),
1059 pf->stat_offsets_loaded,
1060 &osd->tx_size_64, &nsd->tx_size_64);
1061 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1062 I40E_GLPRT_PTC127L(hw->port),
1063 pf->stat_offsets_loaded,
1064 &osd->tx_size_127, &nsd->tx_size_127);
1065 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1066 I40E_GLPRT_PTC255L(hw->port),
1067 pf->stat_offsets_loaded,
1068 &osd->tx_size_255, &nsd->tx_size_255);
1069 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1070 I40E_GLPRT_PTC511L(hw->port),
1071 pf->stat_offsets_loaded,
1072 &osd->tx_size_511, &nsd->tx_size_511);
1073 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1074 I40E_GLPRT_PTC1023L(hw->port),
1075 pf->stat_offsets_loaded,
1076 &osd->tx_size_1023, &nsd->tx_size_1023);
1077 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1078 I40E_GLPRT_PTC1522L(hw->port),
1079 pf->stat_offsets_loaded,
1080 &osd->tx_size_1522, &nsd->tx_size_1522);
1081 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1082 I40E_GLPRT_PTC9522L(hw->port),
1083 pf->stat_offsets_loaded,
1084 &osd->tx_size_big, &nsd->tx_size_big);
1085
1086 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1087 pf->stat_offsets_loaded,
1088 &osd->rx_undersize, &nsd->rx_undersize);
1089 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1090 pf->stat_offsets_loaded,
1091 &osd->rx_fragments, &nsd->rx_fragments);
1092 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1093 pf->stat_offsets_loaded,
1094 &osd->rx_oversize, &nsd->rx_oversize);
1095 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1096 pf->stat_offsets_loaded,
1097 &osd->rx_jabber, &nsd->rx_jabber);
1098
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001099 /* FDIR stats */
1100 i40e_stat_update32(hw, I40E_GLQF_PCNT(pf->fd_atr_cnt_idx),
1101 pf->stat_offsets_loaded,
1102 &osd->fd_atr_match, &nsd->fd_atr_match);
1103 i40e_stat_update32(hw, I40E_GLQF_PCNT(pf->fd_sb_cnt_idx),
1104 pf->stat_offsets_loaded,
1105 &osd->fd_sb_match, &nsd->fd_sb_match);
1106
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001107 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1108 nsd->tx_lpi_status =
1109 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1110 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1111 nsd->rx_lpi_status =
1112 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1113 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1114 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1115 pf->stat_offsets_loaded,
1116 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1117 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1118 pf->stat_offsets_loaded,
1119 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1120
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001121 pf->stat_offsets_loaded = true;
1122}
1123
1124/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001125 * i40e_update_stats - Update the various statistics counters.
1126 * @vsi: the VSI to be updated
1127 *
1128 * Update the various stats for this VSI and its related entities.
1129 **/
1130void i40e_update_stats(struct i40e_vsi *vsi)
1131{
1132 struct i40e_pf *pf = vsi->back;
1133
1134 if (vsi == pf->vsi[pf->lan_vsi])
1135 i40e_update_pf_stats(pf);
1136
1137 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001138#ifdef I40E_FCOE
1139 i40e_update_fcoe_stats(vsi);
1140#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001141}
1142
1143/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001144 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1145 * @vsi: the VSI to be searched
1146 * @macaddr: the MAC address
1147 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001148 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001149 * @is_netdev: make sure its a netdev filter, else doesn't matter
1150 *
1151 * Returns ptr to the filter object or NULL
1152 **/
1153static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1154 u8 *macaddr, s16 vlan,
1155 bool is_vf, bool is_netdev)
1156{
1157 struct i40e_mac_filter *f;
1158
1159 if (!vsi || !macaddr)
1160 return NULL;
1161
1162 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1163 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1164 (vlan == f->vlan) &&
1165 (!is_vf || f->is_vf) &&
1166 (!is_netdev || f->is_netdev))
1167 return f;
1168 }
1169 return NULL;
1170}
1171
1172/**
1173 * i40e_find_mac - Find a mac addr in the macvlan filters list
1174 * @vsi: the VSI to be searched
1175 * @macaddr: the MAC address we are searching for
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001176 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001177 * @is_netdev: make sure its a netdev filter, else doesn't matter
1178 *
1179 * Returns the first filter with the provided MAC address or NULL if
1180 * MAC address was not found
1181 **/
1182struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1183 bool is_vf, bool is_netdev)
1184{
1185 struct i40e_mac_filter *f;
1186
1187 if (!vsi || !macaddr)
1188 return NULL;
1189
1190 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1191 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1192 (!is_vf || f->is_vf) &&
1193 (!is_netdev || f->is_netdev))
1194 return f;
1195 }
1196 return NULL;
1197}
1198
1199/**
1200 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1201 * @vsi: the VSI to be searched
1202 *
1203 * Returns true if VSI is in vlan mode or false otherwise
1204 **/
1205bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1206{
1207 struct i40e_mac_filter *f;
1208
1209 /* Only -1 for all the filters denotes not in vlan mode
1210 * so we have to go through all the list in order to make sure
1211 */
1212 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1213 if (f->vlan >= 0)
1214 return true;
1215 }
1216
1217 return false;
1218}
1219
1220/**
1221 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1222 * @vsi: the VSI to be searched
1223 * @macaddr: the mac address to be filtered
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001224 * @is_vf: true if it is a VF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001225 * @is_netdev: true if it is a netdev
1226 *
1227 * Goes through all the macvlan filters and adds a
1228 * macvlan filter for each unique vlan that already exists
1229 *
1230 * Returns first filter found on success, else NULL
1231 **/
1232struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1233 bool is_vf, bool is_netdev)
1234{
1235 struct i40e_mac_filter *f;
1236
1237 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1238 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1239 is_vf, is_netdev)) {
1240 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001241 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001242 return NULL;
1243 }
1244 }
1245
1246 return list_first_entry_or_null(&vsi->mac_filter_list,
1247 struct i40e_mac_filter, list);
1248}
1249
1250/**
Greg Rose8c27d422014-05-22 06:31:56 +00001251 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1252 * @vsi: the PF Main VSI - inappropriate for any other VSI
1253 * @macaddr: the MAC address
Shannon Nelson30650cc2014-07-29 04:01:50 +00001254 *
1255 * Some older firmware configurations set up a default promiscuous VLAN
1256 * filter that needs to be removed.
Greg Rose8c27d422014-05-22 06:31:56 +00001257 **/
Shannon Nelson30650cc2014-07-29 04:01:50 +00001258static int i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
Greg Rose8c27d422014-05-22 06:31:56 +00001259{
1260 struct i40e_aqc_remove_macvlan_element_data element;
1261 struct i40e_pf *pf = vsi->back;
1262 i40e_status aq_ret;
1263
1264 /* Only appropriate for the PF main VSI */
1265 if (vsi->type != I40E_VSI_MAIN)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001266 return -EINVAL;
Greg Rose8c27d422014-05-22 06:31:56 +00001267
Shannon Nelson30650cc2014-07-29 04:01:50 +00001268 memset(&element, 0, sizeof(element));
Greg Rose8c27d422014-05-22 06:31:56 +00001269 ether_addr_copy(element.mac_addr, macaddr);
1270 element.vlan_tag = 0;
1271 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1272 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1273 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1274 if (aq_ret)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001275 return -ENOENT;
1276
1277 return 0;
Greg Rose8c27d422014-05-22 06:31:56 +00001278}
1279
1280/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001281 * i40e_add_filter - Add a mac/vlan filter to the VSI
1282 * @vsi: the VSI to be searched
1283 * @macaddr: the MAC address
1284 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001285 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001286 * @is_netdev: make sure its a netdev filter, else doesn't matter
1287 *
1288 * Returns ptr to the filter object or NULL when no memory available.
1289 **/
1290struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1291 u8 *macaddr, s16 vlan,
1292 bool is_vf, bool is_netdev)
1293{
1294 struct i40e_mac_filter *f;
1295
1296 if (!vsi || !macaddr)
1297 return NULL;
1298
1299 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1300 if (!f) {
1301 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1302 if (!f)
1303 goto add_filter_out;
1304
Greg Rose9a173902014-05-22 06:32:02 +00001305 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001306 f->vlan = vlan;
1307 f->changed = true;
1308
1309 INIT_LIST_HEAD(&f->list);
1310 list_add(&f->list, &vsi->mac_filter_list);
1311 }
1312
1313 /* increment counter and add a new flag if needed */
1314 if (is_vf) {
1315 if (!f->is_vf) {
1316 f->is_vf = true;
1317 f->counter++;
1318 }
1319 } else if (is_netdev) {
1320 if (!f->is_netdev) {
1321 f->is_netdev = true;
1322 f->counter++;
1323 }
1324 } else {
1325 f->counter++;
1326 }
1327
1328 /* changed tells sync_filters_subtask to
1329 * push the filter down to the firmware
1330 */
1331 if (f->changed) {
1332 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1333 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1334 }
1335
1336add_filter_out:
1337 return f;
1338}
1339
1340/**
1341 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1342 * @vsi: the VSI to be searched
1343 * @macaddr: the MAC address
1344 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001345 * @is_vf: make sure it's a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001346 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1347 **/
1348void i40e_del_filter(struct i40e_vsi *vsi,
1349 u8 *macaddr, s16 vlan,
1350 bool is_vf, bool is_netdev)
1351{
1352 struct i40e_mac_filter *f;
1353
1354 if (!vsi || !macaddr)
1355 return;
1356
1357 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1358 if (!f || f->counter == 0)
1359 return;
1360
1361 if (is_vf) {
1362 if (f->is_vf) {
1363 f->is_vf = false;
1364 f->counter--;
1365 }
1366 } else if (is_netdev) {
1367 if (f->is_netdev) {
1368 f->is_netdev = false;
1369 f->counter--;
1370 }
1371 } else {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001372 /* make sure we don't remove a filter in use by VF or netdev */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001373 int min_f = 0;
1374 min_f += (f->is_vf ? 1 : 0);
1375 min_f += (f->is_netdev ? 1 : 0);
1376
1377 if (f->counter > min_f)
1378 f->counter--;
1379 }
1380
1381 /* counter == 0 tells sync_filters_subtask to
1382 * remove the filter from the firmware's list
1383 */
1384 if (f->counter == 0) {
1385 f->changed = true;
1386 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1387 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1388 }
1389}
1390
1391/**
1392 * i40e_set_mac - NDO callback to set mac address
1393 * @netdev: network interface device structure
1394 * @p: pointer to an address structure
1395 *
1396 * Returns 0 on success, negative on failure
1397 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001398#ifdef I40E_FCOE
1399int i40e_set_mac(struct net_device *netdev, void *p)
1400#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001401static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001402#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001403{
1404 struct i40e_netdev_priv *np = netdev_priv(netdev);
1405 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001406 struct i40e_pf *pf = vsi->back;
1407 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001408 struct sockaddr *addr = p;
1409 struct i40e_mac_filter *f;
1410
1411 if (!is_valid_ether_addr(addr->sa_data))
1412 return -EADDRNOTAVAIL;
1413
Shannon Nelson30650cc2014-07-29 04:01:50 +00001414 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1415 netdev_info(netdev, "already using mac address %pM\n",
1416 addr->sa_data);
1417 return 0;
1418 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001419
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001420 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1421 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1422 return -EADDRNOTAVAIL;
1423
Shannon Nelson30650cc2014-07-29 04:01:50 +00001424 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1425 netdev_info(netdev, "returning to hw mac address %pM\n",
1426 hw->mac.addr);
1427 else
1428 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1429
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001430 if (vsi->type == I40E_VSI_MAIN) {
1431 i40e_status ret;
1432 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001433 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001434 addr->sa_data, NULL);
1435 if (ret) {
1436 netdev_info(netdev,
1437 "Addr change for Main VSI failed: %d\n",
1438 ret);
1439 return -EADDRNOTAVAIL;
1440 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001441 }
1442
Shannon Nelson30650cc2014-07-29 04:01:50 +00001443 if (ether_addr_equal(netdev->dev_addr, hw->mac.addr)) {
1444 struct i40e_aqc_remove_macvlan_element_data element;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001445
Shannon Nelson30650cc2014-07-29 04:01:50 +00001446 memset(&element, 0, sizeof(element));
1447 ether_addr_copy(element.mac_addr, netdev->dev_addr);
1448 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1449 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1450 } else {
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001451 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1452 false, false);
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001453 }
1454
Shannon Nelson30650cc2014-07-29 04:01:50 +00001455 if (ether_addr_equal(addr->sa_data, hw->mac.addr)) {
1456 struct i40e_aqc_add_macvlan_element_data element;
1457
1458 memset(&element, 0, sizeof(element));
1459 ether_addr_copy(element.mac_addr, hw->mac.addr);
1460 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
1461 i40e_aq_add_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1462 } else {
1463 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY,
1464 false, false);
1465 if (f)
1466 f->is_laa = true;
1467 }
1468
1469 i40e_sync_vsi_filters(vsi);
1470 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001471
1472 return 0;
1473}
1474
1475/**
1476 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1477 * @vsi: the VSI being setup
1478 * @ctxt: VSI context structure
1479 * @enabled_tc: Enabled TCs bitmap
1480 * @is_add: True if called before Add VSI
1481 *
1482 * Setup VSI queue mapping for enabled traffic classes.
1483 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001484#ifdef I40E_FCOE
1485void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1486 struct i40e_vsi_context *ctxt,
1487 u8 enabled_tc,
1488 bool is_add)
1489#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001490static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1491 struct i40e_vsi_context *ctxt,
1492 u8 enabled_tc,
1493 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001494#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001495{
1496 struct i40e_pf *pf = vsi->back;
1497 u16 sections = 0;
1498 u8 netdev_tc = 0;
1499 u16 numtc = 0;
1500 u16 qcount;
1501 u8 offset;
1502 u16 qmap;
1503 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001504 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001505
1506 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1507 offset = 0;
1508
1509 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1510 /* Find numtc from enabled TC bitmap */
1511 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1512 if (enabled_tc & (1 << i)) /* TC is enabled */
1513 numtc++;
1514 }
1515 if (!numtc) {
1516 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1517 numtc = 1;
1518 }
1519 } else {
1520 /* At least TC0 is enabled in case of non-DCB case */
1521 numtc = 1;
1522 }
1523
1524 vsi->tc_config.numtc = numtc;
1525 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001526 /* Number of queues per enabled TC */
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001527 /* In MFP case we can have a much lower count of MSIx
1528 * vectors available and so we need to lower the used
1529 * q count.
1530 */
1531 qcount = min_t(int, vsi->alloc_queue_pairs, pf->num_lan_msix);
1532 num_tc_qps = qcount / numtc;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001533 num_tc_qps = min_t(int, num_tc_qps, I40E_MAX_QUEUES_PER_TC);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001534
1535 /* Setup queue offset/count for all TCs for given VSI */
1536 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1537 /* See if the given TC is enabled for the given VSI */
1538 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1539 int pow, num_qps;
1540
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001541 switch (vsi->type) {
1542 case I40E_VSI_MAIN:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001543 qcount = min_t(int, pf->rss_size, num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001544 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001545#ifdef I40E_FCOE
1546 case I40E_VSI_FCOE:
1547 qcount = num_tc_qps;
1548 break;
1549#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001550 case I40E_VSI_FDIR:
1551 case I40E_VSI_SRIOV:
1552 case I40E_VSI_VMDQ2:
1553 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001554 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001555 WARN_ON(i != 0);
1556 break;
1557 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001558 vsi->tc_config.tc_info[i].qoffset = offset;
1559 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001560
Shannon Nelson1e200e42015-02-27 09:15:24 +00001561 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001562 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001563 pow = 0;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001564 while (num_qps && ((1 << pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001565 pow++;
1566 num_qps >>= 1;
1567 }
1568
1569 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1570 qmap =
1571 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1572 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1573
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001574 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001575 } else {
1576 /* TC is not enabled so set the offset to
1577 * default queue and allocate one queue
1578 * for the given TC.
1579 */
1580 vsi->tc_config.tc_info[i].qoffset = 0;
1581 vsi->tc_config.tc_info[i].qcount = 1;
1582 vsi->tc_config.tc_info[i].netdev_tc = 0;
1583
1584 qmap = 0;
1585 }
1586 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1587 }
1588
1589 /* Set actual Tx/Rx queue pairs */
1590 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001591 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1592 if (vsi->req_queue_pairs > 0)
1593 vsi->num_queue_pairs = vsi->req_queue_pairs;
1594 else
1595 vsi->num_queue_pairs = pf->num_lan_msix;
1596 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001597
1598 /* Scheduler section valid can only be set for ADD VSI */
1599 if (is_add) {
1600 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1601
1602 ctxt->info.up_enable_bits = enabled_tc;
1603 }
1604 if (vsi->type == I40E_VSI_SRIOV) {
1605 ctxt->info.mapping_flags |=
1606 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1607 for (i = 0; i < vsi->num_queue_pairs; i++)
1608 ctxt->info.queue_mapping[i] =
1609 cpu_to_le16(vsi->base_queue + i);
1610 } else {
1611 ctxt->info.mapping_flags |=
1612 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1613 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1614 }
1615 ctxt->info.valid_sections |= cpu_to_le16(sections);
1616}
1617
1618/**
1619 * i40e_set_rx_mode - NDO callback to set the netdev filters
1620 * @netdev: network interface device structure
1621 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001622#ifdef I40E_FCOE
1623void i40e_set_rx_mode(struct net_device *netdev)
1624#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001625static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001626#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001627{
1628 struct i40e_netdev_priv *np = netdev_priv(netdev);
1629 struct i40e_mac_filter *f, *ftmp;
1630 struct i40e_vsi *vsi = np->vsi;
1631 struct netdev_hw_addr *uca;
1632 struct netdev_hw_addr *mca;
1633 struct netdev_hw_addr *ha;
1634
1635 /* add addr if not already in the filter list */
1636 netdev_for_each_uc_addr(uca, netdev) {
1637 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1638 if (i40e_is_vsi_in_vlan(vsi))
1639 i40e_put_mac_in_vlan(vsi, uca->addr,
1640 false, true);
1641 else
1642 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1643 false, true);
1644 }
1645 }
1646
1647 netdev_for_each_mc_addr(mca, netdev) {
1648 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1649 if (i40e_is_vsi_in_vlan(vsi))
1650 i40e_put_mac_in_vlan(vsi, mca->addr,
1651 false, true);
1652 else
1653 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1654 false, true);
1655 }
1656 }
1657
1658 /* remove filter if not in netdev list */
1659 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1660 bool found = false;
1661
1662 if (!f->is_netdev)
1663 continue;
1664
1665 if (is_multicast_ether_addr(f->macaddr)) {
1666 netdev_for_each_mc_addr(mca, netdev) {
1667 if (ether_addr_equal(mca->addr, f->macaddr)) {
1668 found = true;
1669 break;
1670 }
1671 }
1672 } else {
1673 netdev_for_each_uc_addr(uca, netdev) {
1674 if (ether_addr_equal(uca->addr, f->macaddr)) {
1675 found = true;
1676 break;
1677 }
1678 }
1679
1680 for_each_dev_addr(netdev, ha) {
1681 if (ether_addr_equal(ha->addr, f->macaddr)) {
1682 found = true;
1683 break;
1684 }
1685 }
1686 }
1687 if (!found)
1688 i40e_del_filter(
1689 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1690 }
1691
1692 /* check for other flag changes */
1693 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1694 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1695 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1696 }
1697}
1698
1699/**
1700 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1701 * @vsi: ptr to the VSI
1702 *
1703 * Push any outstanding VSI filter changes through the AdminQ.
1704 *
1705 * Returns 0 or error value
1706 **/
1707int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1708{
1709 struct i40e_mac_filter *f, *ftmp;
1710 bool promisc_forced_on = false;
1711 bool add_happened = false;
1712 int filter_list_len = 0;
1713 u32 changed_flags = 0;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001714 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001715 struct i40e_pf *pf;
1716 int num_add = 0;
1717 int num_del = 0;
1718 u16 cmd_flags;
1719
1720 /* empty array typed pointers, kcalloc later */
1721 struct i40e_aqc_add_macvlan_element_data *add_list;
1722 struct i40e_aqc_remove_macvlan_element_data *del_list;
1723
1724 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1725 usleep_range(1000, 2000);
1726 pf = vsi->back;
1727
1728 if (vsi->netdev) {
1729 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1730 vsi->current_netdev_flags = vsi->netdev->flags;
1731 }
1732
1733 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1734 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1735
1736 filter_list_len = pf->hw.aq.asq_buf_size /
1737 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1738 del_list = kcalloc(filter_list_len,
1739 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1740 GFP_KERNEL);
1741 if (!del_list)
1742 return -ENOMEM;
1743
1744 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1745 if (!f->changed)
1746 continue;
1747
1748 if (f->counter != 0)
1749 continue;
1750 f->changed = false;
1751 cmd_flags = 0;
1752
1753 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001754 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001755 del_list[num_del].vlan_tag =
1756 cpu_to_le16((u16)(f->vlan ==
1757 I40E_VLAN_ANY ? 0 : f->vlan));
1758
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001759 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1760 del_list[num_del].flags = cmd_flags;
1761 num_del++;
1762
1763 /* unlink from filter list */
1764 list_del(&f->list);
1765 kfree(f);
1766
1767 /* flush a full buffer */
1768 if (num_del == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001769 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001770 vsi->seid, del_list, num_del,
1771 NULL);
1772 num_del = 0;
1773 memset(del_list, 0, sizeof(*del_list));
1774
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001775 if (aq_ret &&
1776 pf->hw.aq.asq_last_status !=
1777 I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001778 dev_info(&pf->pdev->dev,
1779 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001780 aq_ret,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001781 pf->hw.aq.asq_last_status);
1782 }
1783 }
1784 if (num_del) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001785 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001786 del_list, num_del, NULL);
1787 num_del = 0;
1788
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001789 if (aq_ret &&
1790 pf->hw.aq.asq_last_status != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001791 dev_info(&pf->pdev->dev,
1792 "ignoring delete macvlan error, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001793 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001794 }
1795
1796 kfree(del_list);
1797 del_list = NULL;
1798
1799 /* do all the adds now */
1800 filter_list_len = pf->hw.aq.asq_buf_size /
1801 sizeof(struct i40e_aqc_add_macvlan_element_data),
1802 add_list = kcalloc(filter_list_len,
1803 sizeof(struct i40e_aqc_add_macvlan_element_data),
1804 GFP_KERNEL);
1805 if (!add_list)
1806 return -ENOMEM;
1807
1808 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1809 if (!f->changed)
1810 continue;
1811
1812 if (f->counter == 0)
1813 continue;
1814 f->changed = false;
1815 add_happened = true;
1816 cmd_flags = 0;
1817
1818 /* add to add array */
Greg Rose9a173902014-05-22 06:32:02 +00001819 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001820 add_list[num_add].vlan_tag =
1821 cpu_to_le16(
1822 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1823 add_list[num_add].queue_number = 0;
1824
1825 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001826 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1827 num_add++;
1828
1829 /* flush a full buffer */
1830 if (num_add == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001831 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1832 add_list, num_add,
1833 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001834 num_add = 0;
1835
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001836 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001837 break;
1838 memset(add_list, 0, sizeof(*add_list));
1839 }
1840 }
1841 if (num_add) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001842 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1843 add_list, num_add, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001844 num_add = 0;
1845 }
1846 kfree(add_list);
1847 add_list = NULL;
1848
Shannon Nelson30650cc2014-07-29 04:01:50 +00001849 if (add_happened && aq_ret &&
1850 pf->hw.aq.asq_last_status != I40E_AQ_RC_EINVAL) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001851 dev_info(&pf->pdev->dev,
1852 "add filter failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001853 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001854 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1855 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1856 &vsi->state)) {
1857 promisc_forced_on = true;
1858 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1859 &vsi->state);
1860 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1861 }
1862 }
1863 }
1864
1865 /* check for changes in promiscuous modes */
1866 if (changed_flags & IFF_ALLMULTI) {
1867 bool cur_multipromisc;
1868 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001869 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1870 vsi->seid,
1871 cur_multipromisc,
1872 NULL);
1873 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001874 dev_info(&pf->pdev->dev,
1875 "set multi promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001876 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001877 }
1878 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1879 bool cur_promisc;
1880 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1881 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1882 &vsi->state));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001883 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1884 vsi->seid,
1885 cur_promisc, NULL);
1886 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001887 dev_info(&pf->pdev->dev,
1888 "set uni promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001889 aq_ret, pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00001890 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1891 vsi->seid,
1892 cur_promisc, NULL);
1893 if (aq_ret)
1894 dev_info(&pf->pdev->dev,
1895 "set brdcast promisc failed, err %d, aq_err %d\n",
1896 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001897 }
1898
1899 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1900 return 0;
1901}
1902
1903/**
1904 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1905 * @pf: board private structure
1906 **/
1907static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1908{
1909 int v;
1910
1911 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1912 return;
1913 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1914
Mitch Williams505682c2014-05-20 08:01:37 +00001915 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001916 if (pf->vsi[v] &&
1917 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1918 i40e_sync_vsi_filters(pf->vsi[v]);
1919 }
1920}
1921
1922/**
1923 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1924 * @netdev: network interface device structure
1925 * @new_mtu: new value for maximum frame size
1926 *
1927 * Returns 0 on success, negative on failure
1928 **/
1929static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1930{
1931 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00001932 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001933 struct i40e_vsi *vsi = np->vsi;
1934
1935 /* MTU < 68 is an error and causes problems on some kernels */
1936 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1937 return -EINVAL;
1938
1939 netdev_info(netdev, "changing MTU from %d to %d\n",
1940 netdev->mtu, new_mtu);
1941 netdev->mtu = new_mtu;
1942 if (netif_running(netdev))
1943 i40e_vsi_reinit_locked(vsi);
1944
1945 return 0;
1946}
1947
1948/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001949 * i40e_ioctl - Access the hwtstamp interface
1950 * @netdev: network interface device structure
1951 * @ifr: interface request data
1952 * @cmd: ioctl command
1953 **/
1954int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1955{
1956 struct i40e_netdev_priv *np = netdev_priv(netdev);
1957 struct i40e_pf *pf = np->vsi->back;
1958
1959 switch (cmd) {
1960 case SIOCGHWTSTAMP:
1961 return i40e_ptp_get_ts_config(pf, ifr);
1962 case SIOCSHWTSTAMP:
1963 return i40e_ptp_set_ts_config(pf, ifr);
1964 default:
1965 return -EOPNOTSUPP;
1966 }
1967}
1968
1969/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001970 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1971 * @vsi: the vsi being adjusted
1972 **/
1973void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1974{
1975 struct i40e_vsi_context ctxt;
1976 i40e_status ret;
1977
1978 if ((vsi->info.valid_sections &
1979 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1980 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1981 return; /* already enabled */
1982
1983 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1984 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1985 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1986
1987 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07001988 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001989 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1990 if (ret) {
1991 dev_info(&vsi->back->pdev->dev,
1992 "%s: update vsi failed, aq_err=%d\n",
1993 __func__, vsi->back->hw.aq.asq_last_status);
1994 }
1995}
1996
1997/**
1998 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1999 * @vsi: the vsi being adjusted
2000 **/
2001void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2002{
2003 struct i40e_vsi_context ctxt;
2004 i40e_status ret;
2005
2006 if ((vsi->info.valid_sections &
2007 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2008 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2009 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2010 return; /* already disabled */
2011
2012 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2013 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2014 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2015
2016 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002017 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002018 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2019 if (ret) {
2020 dev_info(&vsi->back->pdev->dev,
2021 "%s: update vsi failed, aq_err=%d\n",
2022 __func__, vsi->back->hw.aq.asq_last_status);
2023 }
2024}
2025
2026/**
2027 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2028 * @netdev: network interface to be adjusted
2029 * @features: netdev features to test if VLAN offload is enabled or not
2030 **/
2031static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2032{
2033 struct i40e_netdev_priv *np = netdev_priv(netdev);
2034 struct i40e_vsi *vsi = np->vsi;
2035
2036 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2037 i40e_vlan_stripping_enable(vsi);
2038 else
2039 i40e_vlan_stripping_disable(vsi);
2040}
2041
2042/**
2043 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2044 * @vsi: the vsi being configured
2045 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2046 **/
2047int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2048{
2049 struct i40e_mac_filter *f, *add_f;
2050 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002051
2052 is_vf = (vsi->type == I40E_VSI_SRIOV);
2053 is_netdev = !!(vsi->netdev);
2054
2055 if (is_netdev) {
2056 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
2057 is_vf, is_netdev);
2058 if (!add_f) {
2059 dev_info(&vsi->back->pdev->dev,
2060 "Could not add vlan filter %d for %pM\n",
2061 vid, vsi->netdev->dev_addr);
2062 return -ENOMEM;
2063 }
2064 }
2065
2066 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2067 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2068 if (!add_f) {
2069 dev_info(&vsi->back->pdev->dev,
2070 "Could not add vlan filter %d for %pM\n",
2071 vid, f->macaddr);
2072 return -ENOMEM;
2073 }
2074 }
2075
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002076 /* Now if we add a vlan tag, make sure to check if it is the first
2077 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2078 * with 0, so we now accept untagged and specified tagged traffic
2079 * (and not any taged and untagged)
2080 */
2081 if (vid > 0) {
2082 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
2083 I40E_VLAN_ANY,
2084 is_vf, is_netdev)) {
2085 i40e_del_filter(vsi, vsi->netdev->dev_addr,
2086 I40E_VLAN_ANY, is_vf, is_netdev);
2087 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
2088 is_vf, is_netdev);
2089 if (!add_f) {
2090 dev_info(&vsi->back->pdev->dev,
2091 "Could not add filter 0 for %pM\n",
2092 vsi->netdev->dev_addr);
2093 return -ENOMEM;
2094 }
2095 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002096 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002097
Greg Rose8d82a7c2014-01-13 16:13:04 -08002098 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2099 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002100 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2101 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2102 is_vf, is_netdev)) {
2103 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2104 is_vf, is_netdev);
2105 add_f = i40e_add_filter(vsi, f->macaddr,
2106 0, is_vf, is_netdev);
2107 if (!add_f) {
2108 dev_info(&vsi->back->pdev->dev,
2109 "Could not add filter 0 for %pM\n",
2110 f->macaddr);
2111 return -ENOMEM;
2112 }
2113 }
2114 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002115 }
2116
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002117 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2118 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2119 return 0;
2120
2121 return i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002122}
2123
2124/**
2125 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2126 * @vsi: the vsi being configured
2127 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002128 *
2129 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002130 **/
2131int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
2132{
2133 struct net_device *netdev = vsi->netdev;
2134 struct i40e_mac_filter *f, *add_f;
2135 bool is_vf, is_netdev;
2136 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002137
2138 is_vf = (vsi->type == I40E_VSI_SRIOV);
2139 is_netdev = !!(netdev);
2140
2141 if (is_netdev)
2142 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
2143
2144 list_for_each_entry(f, &vsi->mac_filter_list, list)
2145 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2146
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002147 /* go through all the filters for this VSI and if there is only
2148 * vid == 0 it means there are no other filters, so vid 0 must
2149 * be replaced with -1. This signifies that we should from now
2150 * on accept any traffic (with any tag present, or untagged)
2151 */
2152 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2153 if (is_netdev) {
2154 if (f->vlan &&
2155 ether_addr_equal(netdev->dev_addr, f->macaddr))
2156 filter_count++;
2157 }
2158
2159 if (f->vlan)
2160 filter_count++;
2161 }
2162
2163 if (!filter_count && is_netdev) {
2164 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2165 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2166 is_vf, is_netdev);
2167 if (!f) {
2168 dev_info(&vsi->back->pdev->dev,
2169 "Could not add filter %d for %pM\n",
2170 I40E_VLAN_ANY, netdev->dev_addr);
2171 return -ENOMEM;
2172 }
2173 }
2174
2175 if (!filter_count) {
2176 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2177 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2178 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2179 is_vf, is_netdev);
2180 if (!add_f) {
2181 dev_info(&vsi->back->pdev->dev,
2182 "Could not add filter %d for %pM\n",
2183 I40E_VLAN_ANY, f->macaddr);
2184 return -ENOMEM;
2185 }
2186 }
2187 }
2188
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002189 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2190 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2191 return 0;
2192
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002193 return i40e_sync_vsi_filters(vsi);
2194}
2195
2196/**
2197 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2198 * @netdev: network interface to be adjusted
2199 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002200 *
2201 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002202 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002203#ifdef I40E_FCOE
2204int i40e_vlan_rx_add_vid(struct net_device *netdev,
2205 __always_unused __be16 proto, u16 vid)
2206#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002207static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2208 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002209#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002210{
2211 struct i40e_netdev_priv *np = netdev_priv(netdev);
2212 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002213 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002214
2215 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002216 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002217
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002218 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2219
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002220 /* If the network stack called us with vid = 0 then
2221 * it is asking to receive priority tagged packets with
2222 * vlan id 0. Our HW receives them by default when configured
2223 * to receive untagged packets so there is no need to add an
2224 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002225 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002226 if (vid)
2227 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002228
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002229 if (!ret && (vid < VLAN_N_VID))
2230 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002231
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002232 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002233}
2234
2235/**
2236 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2237 * @netdev: network interface to be adjusted
2238 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002239 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002240 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002241 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002242#ifdef I40E_FCOE
2243int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2244 __always_unused __be16 proto, u16 vid)
2245#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002246static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2247 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002248#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002249{
2250 struct i40e_netdev_priv *np = netdev_priv(netdev);
2251 struct i40e_vsi *vsi = np->vsi;
2252
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002253 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2254
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002255 /* return code is ignored as there is nothing a user
2256 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002257 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002258 */
2259 i40e_vsi_kill_vlan(vsi, vid);
2260
2261 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002262
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002263 return 0;
2264}
2265
2266/**
2267 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2268 * @vsi: the vsi being brought back up
2269 **/
2270static void i40e_restore_vlan(struct i40e_vsi *vsi)
2271{
2272 u16 vid;
2273
2274 if (!vsi->netdev)
2275 return;
2276
2277 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2278
2279 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2280 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2281 vid);
2282}
2283
2284/**
2285 * i40e_vsi_add_pvid - Add pvid for the VSI
2286 * @vsi: the vsi being adjusted
2287 * @vid: the vlan id to set as a PVID
2288 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002289int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002290{
2291 struct i40e_vsi_context ctxt;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002292 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002293
2294 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2295 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002296 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2297 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002298 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002299
2300 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002301 ctxt.info = vsi->info;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002302 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2303 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002304 dev_info(&vsi->back->pdev->dev,
2305 "%s: update vsi failed, aq_err=%d\n",
2306 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002307 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002308 }
2309
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002310 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002311}
2312
2313/**
2314 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2315 * @vsi: the vsi being adjusted
2316 *
2317 * Just use the vlan_rx_register() service to put it back to normal
2318 **/
2319void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2320{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002321 i40e_vlan_stripping_disable(vsi);
2322
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002323 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002324}
2325
2326/**
2327 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2328 * @vsi: ptr to the VSI
2329 *
2330 * If this function returns with an error, then it's possible one or
2331 * more of the rings is populated (while the rest are not). It is the
2332 * callers duty to clean those orphaned rings.
2333 *
2334 * Return 0 on success, negative on failure
2335 **/
2336static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2337{
2338 int i, err = 0;
2339
2340 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002341 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002342
2343 return err;
2344}
2345
2346/**
2347 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2348 * @vsi: ptr to the VSI
2349 *
2350 * Free VSI's transmit software resources
2351 **/
2352static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2353{
2354 int i;
2355
Greg Rose8e9dca52013-12-18 13:45:53 +00002356 if (!vsi->tx_rings)
2357 return;
2358
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002359 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002360 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002361 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002362}
2363
2364/**
2365 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2366 * @vsi: ptr to the VSI
2367 *
2368 * If this function returns with an error, then it's possible one or
2369 * more of the rings is populated (while the rest are not). It is the
2370 * callers duty to clean those orphaned rings.
2371 *
2372 * Return 0 on success, negative on failure
2373 **/
2374static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2375{
2376 int i, err = 0;
2377
2378 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002379 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002380#ifdef I40E_FCOE
2381 i40e_fcoe_setup_ddp_resources(vsi);
2382#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002383 return err;
2384}
2385
2386/**
2387 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2388 * @vsi: ptr to the VSI
2389 *
2390 * Free all receive software resources
2391 **/
2392static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2393{
2394 int i;
2395
Greg Rose8e9dca52013-12-18 13:45:53 +00002396 if (!vsi->rx_rings)
2397 return;
2398
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002399 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002400 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002401 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002402#ifdef I40E_FCOE
2403 i40e_fcoe_free_ddp_resources(vsi);
2404#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002405}
2406
2407/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002408 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2409 * @ring: The Tx ring to configure
2410 *
2411 * This enables/disables XPS for a given Tx descriptor ring
2412 * based on the TCs enabled for the VSI that ring belongs to.
2413 **/
2414static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2415{
2416 struct i40e_vsi *vsi = ring->vsi;
2417 cpumask_var_t mask;
2418
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002419 if (!ring->q_vector || !ring->netdev)
2420 return;
2421
2422 /* Single TC mode enable XPS */
2423 if (vsi->tc_config.numtc <= 1) {
2424 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002425 netif_set_xps_queue(ring->netdev,
2426 &ring->q_vector->affinity_mask,
2427 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002428 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2429 /* Disable XPS to allow selection based on TC */
2430 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2431 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2432 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002433 }
2434}
2435
2436/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002437 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2438 * @ring: The Tx ring to configure
2439 *
2440 * Configure the Tx descriptor ring in the HMC context.
2441 **/
2442static int i40e_configure_tx_ring(struct i40e_ring *ring)
2443{
2444 struct i40e_vsi *vsi = ring->vsi;
2445 u16 pf_q = vsi->base_queue + ring->queue_index;
2446 struct i40e_hw *hw = &vsi->back->hw;
2447 struct i40e_hmc_obj_txq tx_ctx;
2448 i40e_status err = 0;
2449 u32 qtx_ctl = 0;
2450
2451 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002452 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002453 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2454 ring->atr_count = 0;
2455 } else {
2456 ring->atr_sample_rate = 0;
2457 }
2458
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002459 /* configure XPS */
2460 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002461
2462 /* clear the context structure first */
2463 memset(&tx_ctx, 0, sizeof(tx_ctx));
2464
2465 tx_ctx.new_context = 1;
2466 tx_ctx.base = (ring->dma / 128);
2467 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002468 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2469 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002470#ifdef I40E_FCOE
2471 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2472#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002473 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002474 /* FDIR VSI tx ring can still use RS bit and writebacks */
2475 if (vsi->type != I40E_VSI_FDIR)
2476 tx_ctx.head_wb_ena = 1;
2477 tx_ctx.head_wb_addr = ring->dma +
2478 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002479
2480 /* As part of VSI creation/update, FW allocates certain
2481 * Tx arbitration queue sets for each TC enabled for
2482 * the VSI. The FW returns the handles to these queue
2483 * sets as part of the response buffer to Add VSI,
2484 * Update VSI, etc. AQ commands. It is expected that
2485 * these queue set handles be associated with the Tx
2486 * queues by the driver as part of the TX queue context
2487 * initialization. This has to be done regardless of
2488 * DCB as by default everything is mapped to TC0.
2489 */
2490 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2491 tx_ctx.rdylist_act = 0;
2492
2493 /* clear the context in the HMC */
2494 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2495 if (err) {
2496 dev_info(&vsi->back->pdev->dev,
2497 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2498 ring->queue_index, pf_q, err);
2499 return -ENOMEM;
2500 }
2501
2502 /* set the context in the HMC */
2503 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2504 if (err) {
2505 dev_info(&vsi->back->pdev->dev,
2506 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2507 ring->queue_index, pf_q, err);
2508 return -ENOMEM;
2509 }
2510
2511 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002512 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002513 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002514 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2515 I40E_QTX_CTL_VFVM_INDX_MASK;
2516 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002517 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002518 }
2519
Shannon Nelson13fd977492013-09-28 07:14:19 +00002520 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2521 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002522 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2523 i40e_flush(hw);
2524
2525 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2526
2527 /* cache tail off for easier writes later */
2528 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2529
2530 return 0;
2531}
2532
2533/**
2534 * i40e_configure_rx_ring - Configure a receive ring context
2535 * @ring: The Rx ring to configure
2536 *
2537 * Configure the Rx descriptor ring in the HMC context.
2538 **/
2539static int i40e_configure_rx_ring(struct i40e_ring *ring)
2540{
2541 struct i40e_vsi *vsi = ring->vsi;
2542 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2543 u16 pf_q = vsi->base_queue + ring->queue_index;
2544 struct i40e_hw *hw = &vsi->back->hw;
2545 struct i40e_hmc_obj_rxq rx_ctx;
2546 i40e_status err = 0;
2547
2548 ring->state = 0;
2549
2550 /* clear the context structure first */
2551 memset(&rx_ctx, 0, sizeof(rx_ctx));
2552
2553 ring->rx_buf_len = vsi->rx_buf_len;
2554 ring->rx_hdr_len = vsi->rx_hdr_len;
2555
2556 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2557 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2558
2559 rx_ctx.base = (ring->dma / 128);
2560 rx_ctx.qlen = ring->count;
2561
2562 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2563 set_ring_16byte_desc_enabled(ring);
2564 rx_ctx.dsize = 0;
2565 } else {
2566 rx_ctx.dsize = 1;
2567 }
2568
2569 rx_ctx.dtype = vsi->dtype;
2570 if (vsi->dtype) {
2571 set_ring_ps_enabled(ring);
2572 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2573 I40E_RX_SPLIT_IP |
2574 I40E_RX_SPLIT_TCP_UDP |
2575 I40E_RX_SPLIT_SCTP;
2576 } else {
2577 rx_ctx.hsplit_0 = 0;
2578 }
2579
2580 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2581 (chain_len * ring->rx_buf_len));
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002582 if (hw->revision_id == 0)
2583 rx_ctx.lrxqthresh = 0;
2584 else
2585 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002586 rx_ctx.crcstrip = 1;
2587 rx_ctx.l2tsel = 1;
2588 rx_ctx.showiv = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07002589#ifdef I40E_FCOE
2590 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2591#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002592 /* set the prefena field to 1 because the manual says to */
2593 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002594
2595 /* clear the context in the HMC */
2596 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2597 if (err) {
2598 dev_info(&vsi->back->pdev->dev,
2599 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2600 ring->queue_index, pf_q, err);
2601 return -ENOMEM;
2602 }
2603
2604 /* set the context in the HMC */
2605 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2606 if (err) {
2607 dev_info(&vsi->back->pdev->dev,
2608 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2609 ring->queue_index, pf_q, err);
2610 return -ENOMEM;
2611 }
2612
2613 /* cache tail for quicker writes, and clear the reg before use */
2614 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2615 writel(0, ring->tail);
2616
Mitch Williamsa132af22015-01-24 09:58:35 +00002617 if (ring_is_ps_enabled(ring)) {
2618 i40e_alloc_rx_headers(ring);
2619 i40e_alloc_rx_buffers_ps(ring, I40E_DESC_UNUSED(ring));
2620 } else {
2621 i40e_alloc_rx_buffers_1buf(ring, I40E_DESC_UNUSED(ring));
2622 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002623
2624 return 0;
2625}
2626
2627/**
2628 * i40e_vsi_configure_tx - Configure the VSI for Tx
2629 * @vsi: VSI structure describing this set of rings and resources
2630 *
2631 * Configure the Tx VSI for operation.
2632 **/
2633static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2634{
2635 int err = 0;
2636 u16 i;
2637
Alexander Duyck9f65e152013-09-28 06:00:58 +00002638 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2639 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002640
2641 return err;
2642}
2643
2644/**
2645 * i40e_vsi_configure_rx - Configure the VSI for Rx
2646 * @vsi: the VSI being configured
2647 *
2648 * Configure the Rx VSI for operation.
2649 **/
2650static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2651{
2652 int err = 0;
2653 u16 i;
2654
2655 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2656 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2657 + ETH_FCS_LEN + VLAN_HLEN;
2658 else
2659 vsi->max_frame = I40E_RXBUFFER_2048;
2660
2661 /* figure out correct receive buffer length */
2662 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2663 I40E_FLAG_RX_PS_ENABLED)) {
2664 case I40E_FLAG_RX_1BUF_ENABLED:
2665 vsi->rx_hdr_len = 0;
2666 vsi->rx_buf_len = vsi->max_frame;
2667 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2668 break;
2669 case I40E_FLAG_RX_PS_ENABLED:
2670 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2671 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2672 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2673 break;
2674 default:
2675 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2676 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2677 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2678 break;
2679 }
2680
Vasu Dev38e00432014-08-01 13:27:03 -07002681#ifdef I40E_FCOE
2682 /* setup rx buffer for FCoE */
2683 if ((vsi->type == I40E_VSI_FCOE) &&
2684 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
2685 vsi->rx_hdr_len = 0;
2686 vsi->rx_buf_len = I40E_RXBUFFER_3072;
2687 vsi->max_frame = I40E_RXBUFFER_3072;
2688 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2689 }
2690
2691#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002692 /* round up for the chip's needs */
2693 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2694 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2695 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2696 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2697
2698 /* set up individual rings */
2699 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002700 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002701
2702 return err;
2703}
2704
2705/**
2706 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2707 * @vsi: ptr to the VSI
2708 **/
2709static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2710{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002711 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002712 u16 qoffset, qcount;
2713 int i, n;
2714
Parikh, Neeravcd238a32015-02-21 06:43:37 +00002715 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2716 /* Reset the TC information */
2717 for (i = 0; i < vsi->num_queue_pairs; i++) {
2718 rx_ring = vsi->rx_rings[i];
2719 tx_ring = vsi->tx_rings[i];
2720 rx_ring->dcb_tc = 0;
2721 tx_ring->dcb_tc = 0;
2722 }
2723 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002724
2725 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2726 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2727 continue;
2728
2729 qoffset = vsi->tc_config.tc_info[n].qoffset;
2730 qcount = vsi->tc_config.tc_info[n].qcount;
2731 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002732 rx_ring = vsi->rx_rings[i];
2733 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002734 rx_ring->dcb_tc = n;
2735 tx_ring->dcb_tc = n;
2736 }
2737 }
2738}
2739
2740/**
2741 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2742 * @vsi: ptr to the VSI
2743 **/
2744static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2745{
2746 if (vsi->netdev)
2747 i40e_set_rx_mode(vsi->netdev);
2748}
2749
2750/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002751 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
2752 * @vsi: Pointer to the targeted VSI
2753 *
2754 * This function replays the hlist on the hw where all the SB Flow Director
2755 * filters were saved.
2756 **/
2757static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
2758{
2759 struct i40e_fdir_filter *filter;
2760 struct i40e_pf *pf = vsi->back;
2761 struct hlist_node *node;
2762
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002763 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2764 return;
2765
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002766 hlist_for_each_entry_safe(filter, node,
2767 &pf->fdir_filter_list, fdir_node) {
2768 i40e_add_del_fdir(vsi, filter, true);
2769 }
2770}
2771
2772/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002773 * i40e_vsi_configure - Set up the VSI for action
2774 * @vsi: the VSI being configured
2775 **/
2776static int i40e_vsi_configure(struct i40e_vsi *vsi)
2777{
2778 int err;
2779
2780 i40e_set_vsi_rx_mode(vsi);
2781 i40e_restore_vlan(vsi);
2782 i40e_vsi_config_dcb_rings(vsi);
2783 err = i40e_vsi_configure_tx(vsi);
2784 if (!err)
2785 err = i40e_vsi_configure_rx(vsi);
2786
2787 return err;
2788}
2789
2790/**
2791 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2792 * @vsi: the VSI being configured
2793 **/
2794static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2795{
2796 struct i40e_pf *pf = vsi->back;
2797 struct i40e_q_vector *q_vector;
2798 struct i40e_hw *hw = &pf->hw;
2799 u16 vector;
2800 int i, q;
2801 u32 val;
2802 u32 qp;
2803
2804 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2805 * and PFINT_LNKLSTn registers, e.g.:
2806 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2807 */
2808 qp = vsi->base_queue;
2809 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002810 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2811 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002812 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2813 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2814 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2815 q_vector->rx.itr);
2816 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2817 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2818 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2819 q_vector->tx.itr);
2820
2821 /* Linked list for the queuepairs assigned to this vector */
2822 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2823 for (q = 0; q < q_vector->num_ringpairs; q++) {
2824 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2825 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2826 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2827 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2828 (I40E_QUEUE_TYPE_TX
2829 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2830
2831 wr32(hw, I40E_QINT_RQCTL(qp), val);
2832
2833 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2834 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2835 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2836 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2837 (I40E_QUEUE_TYPE_RX
2838 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2839
2840 /* Terminate the linked list */
2841 if (q == (q_vector->num_ringpairs - 1))
2842 val |= (I40E_QUEUE_END_OF_LIST
2843 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2844
2845 wr32(hw, I40E_QINT_TQCTL(qp), val);
2846 qp++;
2847 }
2848 }
2849
2850 i40e_flush(hw);
2851}
2852
2853/**
2854 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2855 * @hw: ptr to the hardware info
2856 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00002857static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002858{
Jacob Kellerab437b52014-12-14 01:55:08 +00002859 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002860 u32 val;
2861
2862 /* clear things first */
2863 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2864 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2865
2866 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2867 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2868 I40E_PFINT_ICR0_ENA_GRST_MASK |
2869 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2870 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002871 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2872 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2873 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2874
Jacob Kellerab437b52014-12-14 01:55:08 +00002875 if (pf->flags & I40E_FLAG_PTP)
2876 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
2877
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002878 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2879
2880 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002881 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2882 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002883
2884 /* OTHER_ITR_IDX = 0 */
2885 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2886}
2887
2888/**
2889 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2890 * @vsi: the VSI being configured
2891 **/
2892static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2893{
Alexander Duyck493fb302013-09-28 07:01:44 +00002894 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002895 struct i40e_pf *pf = vsi->back;
2896 struct i40e_hw *hw = &pf->hw;
2897 u32 val;
2898
2899 /* set the ITR configuration */
2900 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2901 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2902 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2903 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2904 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2905 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2906
Jacob Kellerab437b52014-12-14 01:55:08 +00002907 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002908
2909 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2910 wr32(hw, I40E_PFINT_LNKLST0, 0);
2911
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00002912 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002913 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2914 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2915 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2916
2917 wr32(hw, I40E_QINT_RQCTL(0), val);
2918
2919 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2920 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2921 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2922
2923 wr32(hw, I40E_QINT_TQCTL(0), val);
2924 i40e_flush(hw);
2925}
2926
2927/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00002928 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2929 * @pf: board private structure
2930 **/
2931void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2932{
2933 struct i40e_hw *hw = &pf->hw;
2934
2935 wr32(hw, I40E_PFINT_DYN_CTL0,
2936 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2937 i40e_flush(hw);
2938}
2939
2940/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002941 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2942 * @pf: board private structure
2943 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002944void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002945{
2946 struct i40e_hw *hw = &pf->hw;
2947 u32 val;
2948
2949 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2950 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2951 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2952
2953 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2954 i40e_flush(hw);
2955}
2956
2957/**
2958 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2959 * @vsi: pointer to a vsi
2960 * @vector: enable a particular Hw Interrupt vector
2961 **/
2962void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2963{
2964 struct i40e_pf *pf = vsi->back;
2965 struct i40e_hw *hw = &pf->hw;
2966 u32 val;
2967
2968 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2969 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2970 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2971 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002972 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002973}
2974
2975/**
Carolyn Wyborny5c2cebd2014-06-04 01:23:18 +00002976 * i40e_irq_dynamic_disable - Disable default interrupt generation settings
2977 * @vsi: pointer to a vsi
Greg Rose03147772015-01-24 09:58:29 +00002978 * @vector: disable a particular Hw Interrupt vector
Carolyn Wyborny5c2cebd2014-06-04 01:23:18 +00002979 **/
2980void i40e_irq_dynamic_disable(struct i40e_vsi *vsi, int vector)
2981{
2982 struct i40e_pf *pf = vsi->back;
2983 struct i40e_hw *hw = &pf->hw;
2984 u32 val;
2985
2986 val = I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT;
2987 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
2988 i40e_flush(hw);
2989}
2990
2991/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002992 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2993 * @irq: interrupt number
2994 * @data: pointer to a q_vector
2995 **/
2996static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2997{
2998 struct i40e_q_vector *q_vector = data;
2999
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003000 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003001 return IRQ_HANDLED;
3002
3003 napi_schedule(&q_vector->napi);
3004
3005 return IRQ_HANDLED;
3006}
3007
3008/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003009 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3010 * @vsi: the VSI being configured
3011 * @basename: name for the vector
3012 *
3013 * Allocates MSI-X vectors and requests interrupts from the kernel.
3014 **/
3015static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3016{
3017 int q_vectors = vsi->num_q_vectors;
3018 struct i40e_pf *pf = vsi->back;
3019 int base = vsi->base_vector;
3020 int rx_int_idx = 0;
3021 int tx_int_idx = 0;
3022 int vector, err;
3023
3024 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003025 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003026
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003027 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003028 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3029 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3030 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003031 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003032 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3033 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003034 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003035 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3036 "%s-%s-%d", basename, "tx", tx_int_idx++);
3037 } else {
3038 /* skip this unused q_vector */
3039 continue;
3040 }
3041 err = request_irq(pf->msix_entries[base + vector].vector,
3042 vsi->irq_handler,
3043 0,
3044 q_vector->name,
3045 q_vector);
3046 if (err) {
3047 dev_info(&pf->pdev->dev,
3048 "%s: request_irq failed, error: %d\n",
3049 __func__, err);
3050 goto free_queue_irqs;
3051 }
3052 /* assign the mask for this irq */
3053 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3054 &q_vector->affinity_mask);
3055 }
3056
Shannon Nelson63741842014-04-23 04:50:16 +00003057 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003058 return 0;
3059
3060free_queue_irqs:
3061 while (vector) {
3062 vector--;
3063 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3064 NULL);
3065 free_irq(pf->msix_entries[base + vector].vector,
3066 &(vsi->q_vectors[vector]));
3067 }
3068 return err;
3069}
3070
3071/**
3072 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3073 * @vsi: the VSI being un-configured
3074 **/
3075static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3076{
3077 struct i40e_pf *pf = vsi->back;
3078 struct i40e_hw *hw = &pf->hw;
3079 int base = vsi->base_vector;
3080 int i;
3081
3082 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003083 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3084 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003085 }
3086
3087 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3088 for (i = vsi->base_vector;
3089 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3090 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3091
3092 i40e_flush(hw);
3093 for (i = 0; i < vsi->num_q_vectors; i++)
3094 synchronize_irq(pf->msix_entries[i + base].vector);
3095 } else {
3096 /* Legacy and MSI mode - this stops all interrupt handling */
3097 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3098 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3099 i40e_flush(hw);
3100 synchronize_irq(pf->pdev->irq);
3101 }
3102}
3103
3104/**
3105 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3106 * @vsi: the VSI being configured
3107 **/
3108static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3109{
3110 struct i40e_pf *pf = vsi->back;
3111 int i;
3112
3113 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3114 for (i = vsi->base_vector;
3115 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3116 i40e_irq_dynamic_enable(vsi, i);
3117 } else {
3118 i40e_irq_dynamic_enable_icr0(pf);
3119 }
3120
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003121 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003122 return 0;
3123}
3124
3125/**
3126 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3127 * @pf: board private structure
3128 **/
3129static void i40e_stop_misc_vector(struct i40e_pf *pf)
3130{
3131 /* Disable ICR 0 */
3132 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3133 i40e_flush(&pf->hw);
3134}
3135
3136/**
3137 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3138 * @irq: interrupt number
3139 * @data: pointer to a q_vector
3140 *
3141 * This is the handler used for all MSI/Legacy interrupts, and deals
3142 * with both queue and non-queue interrupts. This is also used in
3143 * MSIX mode to handle the non-queue interrupts.
3144 **/
3145static irqreturn_t i40e_intr(int irq, void *data)
3146{
3147 struct i40e_pf *pf = (struct i40e_pf *)data;
3148 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003149 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003150 u32 icr0, icr0_remaining;
3151 u32 val, ena_mask;
3152
3153 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003154 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003155
Shannon Nelson116a57d2013-09-28 07:13:59 +00003156 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3157 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003158 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003159
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003160 /* if interrupt but no bits showing, must be SWINT */
3161 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3162 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3163 pf->sw_int_count++;
3164
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003165 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3166 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
3167
3168 /* temporarily disable queue cause for NAPI processing */
3169 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
3170 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3171 wr32(hw, I40E_QINT_RQCTL(0), qval);
3172
3173 qval = rd32(hw, I40E_QINT_TQCTL(0));
3174 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3175 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003176
3177 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00003178 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003179 }
3180
3181 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3182 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3183 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
3184 }
3185
3186 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3187 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3188 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3189 }
3190
3191 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3192 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3193 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3194 }
3195
3196 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3197 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3198 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3199 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3200 val = rd32(hw, I40E_GLGEN_RSTAT);
3201 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3202 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003203 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003204 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003205 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003206 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003207 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003208 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003209 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003210 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003211 }
3212
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003213 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3214 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3215 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003216 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3217 rd32(hw, I40E_PFHMC_ERRORINFO),
3218 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003219 }
3220
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003221 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3222 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3223
3224 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003225 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003226 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003227 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003228 }
3229
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003230 /* If a critical error is pending we have no choice but to reset the
3231 * device.
3232 * Report and mask out any remaining unexpected interrupts.
3233 */
3234 icr0_remaining = icr0 & ena_mask;
3235 if (icr0_remaining) {
3236 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3237 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003238 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003239 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003240 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003241 dev_info(&pf->pdev->dev, "device will be reset\n");
3242 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3243 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003244 }
3245 ena_mask &= ~icr0_remaining;
3246 }
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003247 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003248
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003249enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003250 /* re-enable interrupt causes */
3251 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003252 if (!test_bit(__I40E_DOWN, &pf->state)) {
3253 i40e_service_event_schedule(pf);
3254 i40e_irq_dynamic_enable_icr0(pf);
3255 }
3256
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003257 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003258}
3259
3260/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003261 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3262 * @tx_ring: tx ring to clean
3263 * @budget: how many cleans we're allowed
3264 *
3265 * Returns true if there's any budget left (e.g. the clean is finished)
3266 **/
3267static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3268{
3269 struct i40e_vsi *vsi = tx_ring->vsi;
3270 u16 i = tx_ring->next_to_clean;
3271 struct i40e_tx_buffer *tx_buf;
3272 struct i40e_tx_desc *tx_desc;
3273
3274 tx_buf = &tx_ring->tx_bi[i];
3275 tx_desc = I40E_TX_DESC(tx_ring, i);
3276 i -= tx_ring->count;
3277
3278 do {
3279 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3280
3281 /* if next_to_watch is not set then there is no work pending */
3282 if (!eop_desc)
3283 break;
3284
3285 /* prevent any other reads prior to eop_desc */
3286 read_barrier_depends();
3287
3288 /* if the descriptor isn't done, no work yet to do */
3289 if (!(eop_desc->cmd_type_offset_bsz &
3290 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3291 break;
3292
3293 /* clear next_to_watch to prevent false hangs */
3294 tx_buf->next_to_watch = NULL;
3295
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003296 tx_desc->buffer_addr = 0;
3297 tx_desc->cmd_type_offset_bsz = 0;
3298 /* move past filter desc */
3299 tx_buf++;
3300 tx_desc++;
3301 i++;
3302 if (unlikely(!i)) {
3303 i -= tx_ring->count;
3304 tx_buf = tx_ring->tx_bi;
3305 tx_desc = I40E_TX_DESC(tx_ring, 0);
3306 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003307 /* unmap skb header data */
3308 dma_unmap_single(tx_ring->dev,
3309 dma_unmap_addr(tx_buf, dma),
3310 dma_unmap_len(tx_buf, len),
3311 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003312 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3313 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003314
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003315 tx_buf->raw_buf = NULL;
3316 tx_buf->tx_flags = 0;
3317 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003318 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003319 tx_desc->buffer_addr = 0;
3320 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003321
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003322 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003323 tx_buf++;
3324 tx_desc++;
3325 i++;
3326 if (unlikely(!i)) {
3327 i -= tx_ring->count;
3328 tx_buf = tx_ring->tx_bi;
3329 tx_desc = I40E_TX_DESC(tx_ring, 0);
3330 }
3331
3332 /* update budget accounting */
3333 budget--;
3334 } while (likely(budget));
3335
3336 i += tx_ring->count;
3337 tx_ring->next_to_clean = i;
3338
3339 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
3340 i40e_irq_dynamic_enable(vsi,
3341 tx_ring->q_vector->v_idx + vsi->base_vector);
3342 }
3343 return budget > 0;
3344}
3345
3346/**
3347 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3348 * @irq: interrupt number
3349 * @data: pointer to a q_vector
3350 **/
3351static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3352{
3353 struct i40e_q_vector *q_vector = data;
3354 struct i40e_vsi *vsi;
3355
3356 if (!q_vector->tx.ring)
3357 return IRQ_HANDLED;
3358
3359 vsi = q_vector->tx.ring->vsi;
3360 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3361
3362 return IRQ_HANDLED;
3363}
3364
3365/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003366 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003367 * @vsi: the VSI being configured
3368 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003369 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003370 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003371static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003372{
Alexander Duyck493fb302013-09-28 07:01:44 +00003373 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003374 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3375 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003376
3377 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003378 tx_ring->next = q_vector->tx.ring;
3379 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003380 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003381
3382 rx_ring->q_vector = q_vector;
3383 rx_ring->next = q_vector->rx.ring;
3384 q_vector->rx.ring = rx_ring;
3385 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003386}
3387
3388/**
3389 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3390 * @vsi: the VSI being configured
3391 *
3392 * This function maps descriptor rings to the queue-specific vectors
3393 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3394 * one vector per queue pair, but on a constrained vector budget, we
3395 * group the queue pairs as "efficiently" as possible.
3396 **/
3397static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3398{
3399 int qp_remaining = vsi->num_queue_pairs;
3400 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003401 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003402 int v_start = 0;
3403 int qp_idx = 0;
3404
3405 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3406 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003407 * It is also important to go through all the vectors available to be
3408 * sure that if we don't use all the vectors, that the remaining vectors
3409 * are cleared. This is especially important when decreasing the
3410 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003411 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003412 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003413 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3414
3415 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3416
3417 q_vector->num_ringpairs = num_ringpairs;
3418
3419 q_vector->rx.count = 0;
3420 q_vector->tx.count = 0;
3421 q_vector->rx.ring = NULL;
3422 q_vector->tx.ring = NULL;
3423
3424 while (num_ringpairs--) {
3425 map_vector_to_qp(vsi, v_start, qp_idx);
3426 qp_idx++;
3427 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003428 }
3429 }
3430}
3431
3432/**
3433 * i40e_vsi_request_irq - Request IRQ from the OS
3434 * @vsi: the VSI being configured
3435 * @basename: name for the vector
3436 **/
3437static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3438{
3439 struct i40e_pf *pf = vsi->back;
3440 int err;
3441
3442 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3443 err = i40e_vsi_request_irq_msix(vsi, basename);
3444 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3445 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003446 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003447 else
3448 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003449 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003450
3451 if (err)
3452 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3453
3454 return err;
3455}
3456
3457#ifdef CONFIG_NET_POLL_CONTROLLER
3458/**
3459 * i40e_netpoll - A Polling 'interrupt'handler
3460 * @netdev: network interface device structure
3461 *
3462 * This is used by netconsole to send skbs without having to re-enable
3463 * interrupts. It's not called while the normal interrupt routine is executing.
3464 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003465#ifdef I40E_FCOE
3466void i40e_netpoll(struct net_device *netdev)
3467#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003468static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003469#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003470{
3471 struct i40e_netdev_priv *np = netdev_priv(netdev);
3472 struct i40e_vsi *vsi = np->vsi;
3473 struct i40e_pf *pf = vsi->back;
3474 int i;
3475
3476 /* if interface is down do nothing */
3477 if (test_bit(__I40E_DOWN, &vsi->state))
3478 return;
3479
3480 pf->flags |= I40E_FLAG_IN_NETPOLL;
3481 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3482 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003483 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003484 } else {
3485 i40e_intr(pf->pdev->irq, netdev);
3486 }
3487 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3488}
3489#endif
3490
3491/**
Neerav Parikh23527302014-06-03 23:50:15 +00003492 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3493 * @pf: the PF being configured
3494 * @pf_q: the PF queue
3495 * @enable: enable or disable state of the queue
3496 *
3497 * This routine will wait for the given Tx queue of the PF to reach the
3498 * enabled or disabled state.
3499 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3500 * multiple retries; else will return 0 in case of success.
3501 **/
3502static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3503{
3504 int i;
3505 u32 tx_reg;
3506
3507 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3508 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3509 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3510 break;
3511
Neerav Parikhf98a2002014-09-13 07:40:44 +00003512 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003513 }
3514 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3515 return -ETIMEDOUT;
3516
3517 return 0;
3518}
3519
3520/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003521 * i40e_vsi_control_tx - Start or stop a VSI's rings
3522 * @vsi: the VSI being configured
3523 * @enable: start or stop the rings
3524 **/
3525static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3526{
3527 struct i40e_pf *pf = vsi->back;
3528 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003529 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003530 u32 tx_reg;
3531
3532 pf_q = vsi->base_queue;
3533 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003534
3535 /* warn the TX unit of coming changes */
3536 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3537 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003538 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00003539
Mitch Williams6c5ef622014-02-20 19:29:16 -08003540 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003541 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003542 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3543 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3544 break;
3545 usleep_range(1000, 2000);
3546 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003547 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c1220072014-03-14 07:32:29 +00003548 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003549 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003550
3551 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003552 if (enable) {
3553 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003554 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003555 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003556 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003557 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003558
3559 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003560 /* No waiting for the Tx queue to disable */
3561 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3562 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003563
3564 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003565 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3566 if (ret) {
3567 dev_info(&pf->pdev->dev,
3568 "%s: VSI seid %d Tx ring %d %sable timeout\n",
3569 __func__, vsi->seid, pf_q,
3570 (enable ? "en" : "dis"));
3571 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003572 }
3573 }
3574
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003575 if (hw->revision_id == 0)
3576 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003577 return ret;
3578}
3579
3580/**
3581 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3582 * @pf: the PF being configured
3583 * @pf_q: the PF queue
3584 * @enable: enable or disable state of the queue
3585 *
3586 * This routine will wait for the given Rx queue of the PF to reach the
3587 * enabled or disabled state.
3588 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3589 * multiple retries; else will return 0 in case of success.
3590 **/
3591static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3592{
3593 int i;
3594 u32 rx_reg;
3595
3596 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3597 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3598 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3599 break;
3600
Neerav Parikhf98a2002014-09-13 07:40:44 +00003601 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003602 }
3603 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3604 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003605
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003606 return 0;
3607}
3608
3609/**
3610 * i40e_vsi_control_rx - Start or stop a VSI's rings
3611 * @vsi: the VSI being configured
3612 * @enable: start or stop the rings
3613 **/
3614static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3615{
3616 struct i40e_pf *pf = vsi->back;
3617 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003618 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003619 u32 rx_reg;
3620
3621 pf_q = vsi->base_queue;
3622 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003623 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003624 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003625 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3626 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3627 break;
3628 usleep_range(1000, 2000);
3629 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003630
Catherine Sullivan7c1220072014-03-14 07:32:29 +00003631 /* Skip if the queue is already in the requested state */
3632 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3633 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003634
3635 /* turn on/off the queue */
3636 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003637 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003638 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003639 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003640 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3641
3642 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003643 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3644 if (ret) {
3645 dev_info(&pf->pdev->dev,
3646 "%s: VSI seid %d Rx ring %d %sable timeout\n",
3647 __func__, vsi->seid, pf_q,
3648 (enable ? "en" : "dis"));
3649 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003650 }
3651 }
3652
Neerav Parikh23527302014-06-03 23:50:15 +00003653 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003654}
3655
3656/**
3657 * i40e_vsi_control_rings - Start or stop a VSI's rings
3658 * @vsi: the VSI being configured
3659 * @enable: start or stop the rings
3660 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003661int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003662{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003663 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003664
3665 /* do rx first for enable and last for disable */
3666 if (request) {
3667 ret = i40e_vsi_control_rx(vsi, request);
3668 if (ret)
3669 return ret;
3670 ret = i40e_vsi_control_tx(vsi, request);
3671 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003672 /* Ignore return value, we need to shutdown whatever we can */
3673 i40e_vsi_control_tx(vsi, request);
3674 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003675 }
3676
3677 return ret;
3678}
3679
3680/**
3681 * i40e_vsi_free_irq - Free the irq association with the OS
3682 * @vsi: the VSI being configured
3683 **/
3684static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3685{
3686 struct i40e_pf *pf = vsi->back;
3687 struct i40e_hw *hw = &pf->hw;
3688 int base = vsi->base_vector;
3689 u32 val, qp;
3690 int i;
3691
3692 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3693 if (!vsi->q_vectors)
3694 return;
3695
Shannon Nelson63741842014-04-23 04:50:16 +00003696 if (!vsi->irqs_ready)
3697 return;
3698
3699 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003700 for (i = 0; i < vsi->num_q_vectors; i++) {
3701 u16 vector = i + base;
3702
3703 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003704 if (!vsi->q_vectors[i] ||
3705 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003706 continue;
3707
3708 /* clear the affinity_mask in the IRQ descriptor */
3709 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3710 NULL);
3711 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003712 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003713
3714 /* Tear down the interrupt queue link list
3715 *
3716 * We know that they come in pairs and always
3717 * the Rx first, then the Tx. To clear the
3718 * link list, stick the EOL value into the
3719 * next_q field of the registers.
3720 */
3721 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3722 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3723 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3724 val |= I40E_QUEUE_END_OF_LIST
3725 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3726 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3727
3728 while (qp != I40E_QUEUE_END_OF_LIST) {
3729 u32 next;
3730
3731 val = rd32(hw, I40E_QINT_RQCTL(qp));
3732
3733 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3734 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3735 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3736 I40E_QINT_RQCTL_INTEVENT_MASK);
3737
3738 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3739 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3740
3741 wr32(hw, I40E_QINT_RQCTL(qp), val);
3742
3743 val = rd32(hw, I40E_QINT_TQCTL(qp));
3744
3745 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3746 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3747
3748 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3749 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3750 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3751 I40E_QINT_TQCTL_INTEVENT_MASK);
3752
3753 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3754 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3755
3756 wr32(hw, I40E_QINT_TQCTL(qp), val);
3757 qp = next;
3758 }
3759 }
3760 } else {
3761 free_irq(pf->pdev->irq, pf);
3762
3763 val = rd32(hw, I40E_PFINT_LNKLST0);
3764 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3765 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3766 val |= I40E_QUEUE_END_OF_LIST
3767 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3768 wr32(hw, I40E_PFINT_LNKLST0, val);
3769
3770 val = rd32(hw, I40E_QINT_RQCTL(qp));
3771 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3772 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3773 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3774 I40E_QINT_RQCTL_INTEVENT_MASK);
3775
3776 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3777 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3778
3779 wr32(hw, I40E_QINT_RQCTL(qp), val);
3780
3781 val = rd32(hw, I40E_QINT_TQCTL(qp));
3782
3783 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3784 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3785 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3786 I40E_QINT_TQCTL_INTEVENT_MASK);
3787
3788 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3789 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3790
3791 wr32(hw, I40E_QINT_TQCTL(qp), val);
3792 }
3793}
3794
3795/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003796 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3797 * @vsi: the VSI being configured
3798 * @v_idx: Index of vector to be freed
3799 *
3800 * This function frees the memory allocated to the q_vector. In addition if
3801 * NAPI is enabled it will delete any references to the NAPI struct prior
3802 * to freeing the q_vector.
3803 **/
3804static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3805{
3806 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003807 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003808
3809 if (!q_vector)
3810 return;
3811
3812 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003813 i40e_for_each_ring(ring, q_vector->tx)
3814 ring->q_vector = NULL;
3815
3816 i40e_for_each_ring(ring, q_vector->rx)
3817 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003818
3819 /* only VSI w/ an associated netdev is set up w/ NAPI */
3820 if (vsi->netdev)
3821 netif_napi_del(&q_vector->napi);
3822
3823 vsi->q_vectors[v_idx] = NULL;
3824
3825 kfree_rcu(q_vector, rcu);
3826}
3827
3828/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003829 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3830 * @vsi: the VSI being un-configured
3831 *
3832 * This frees the memory allocated to the q_vectors and
3833 * deletes references to the NAPI struct.
3834 **/
3835static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3836{
3837 int v_idx;
3838
Alexander Duyck493fb302013-09-28 07:01:44 +00003839 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3840 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003841}
3842
3843/**
3844 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3845 * @pf: board private structure
3846 **/
3847static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3848{
3849 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3850 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3851 pci_disable_msix(pf->pdev);
3852 kfree(pf->msix_entries);
3853 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00003854 kfree(pf->irq_pile);
3855 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003856 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3857 pci_disable_msi(pf->pdev);
3858 }
3859 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3860}
3861
3862/**
3863 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3864 * @pf: board private structure
3865 *
3866 * We go through and clear interrupt specific resources and reset the structure
3867 * to pre-load conditions
3868 **/
3869static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3870{
3871 int i;
3872
Shannon Nelsone1477582015-02-21 06:44:33 +00003873 i40e_stop_misc_vector(pf);
3874 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3875 synchronize_irq(pf->msix_entries[0].vector);
3876 free_irq(pf->msix_entries[0].vector, pf);
3877 }
3878
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003879 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00003880 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003881 if (pf->vsi[i])
3882 i40e_vsi_free_q_vectors(pf->vsi[i]);
3883 i40e_reset_interrupt_capability(pf);
3884}
3885
3886/**
3887 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3888 * @vsi: the VSI being configured
3889 **/
3890static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3891{
3892 int q_idx;
3893
3894 if (!vsi->netdev)
3895 return;
3896
3897 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003898 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003899}
3900
3901/**
3902 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3903 * @vsi: the VSI being configured
3904 **/
3905static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3906{
3907 int q_idx;
3908
3909 if (!vsi->netdev)
3910 return;
3911
3912 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003913 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003914}
3915
3916/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003917 * i40e_vsi_close - Shut down a VSI
3918 * @vsi: the vsi to be quelled
3919 **/
3920static void i40e_vsi_close(struct i40e_vsi *vsi)
3921{
3922 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
3923 i40e_down(vsi);
3924 i40e_vsi_free_irq(vsi);
3925 i40e_vsi_free_tx_resources(vsi);
3926 i40e_vsi_free_rx_resources(vsi);
3927}
3928
3929/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003930 * i40e_quiesce_vsi - Pause a given VSI
3931 * @vsi: the VSI being paused
3932 **/
3933static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3934{
3935 if (test_bit(__I40E_DOWN, &vsi->state))
3936 return;
3937
Neerav Parikhd341b7a2014-11-12 00:18:51 +00003938 /* No need to disable FCoE VSI when Tx suspended */
3939 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
3940 vsi->type == I40E_VSI_FCOE) {
3941 dev_dbg(&vsi->back->pdev->dev,
3942 "%s: VSI seid %d skipping FCoE VSI disable\n",
3943 __func__, vsi->seid);
3944 return;
3945 }
3946
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003947 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3948 if (vsi->netdev && netif_running(vsi->netdev)) {
3949 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3950 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003951 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003952 }
3953}
3954
3955/**
3956 * i40e_unquiesce_vsi - Resume a given VSI
3957 * @vsi: the VSI being resumed
3958 **/
3959static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3960{
3961 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3962 return;
3963
3964 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3965 if (vsi->netdev && netif_running(vsi->netdev))
3966 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3967 else
Shannon Nelson8276f752014-03-14 07:32:27 +00003968 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003969}
3970
3971/**
3972 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3973 * @pf: the PF
3974 **/
3975static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3976{
3977 int v;
3978
Mitch Williams505682c2014-05-20 08:01:37 +00003979 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003980 if (pf->vsi[v])
3981 i40e_quiesce_vsi(pf->vsi[v]);
3982 }
3983}
3984
3985/**
3986 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3987 * @pf: the PF
3988 **/
3989static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3990{
3991 int v;
3992
Mitch Williams505682c2014-05-20 08:01:37 +00003993 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003994 if (pf->vsi[v])
3995 i40e_unquiesce_vsi(pf->vsi[v]);
3996 }
3997}
3998
Neerav Parikh69129dc2014-11-12 00:18:46 +00003999#ifdef CONFIG_I40E_DCB
4000/**
4001 * i40e_vsi_wait_txq_disabled - Wait for VSI's queues to be disabled
4002 * @vsi: the VSI being configured
4003 *
4004 * This function waits for the given VSI's Tx queues to be disabled.
4005 **/
4006static int i40e_vsi_wait_txq_disabled(struct i40e_vsi *vsi)
4007{
4008 struct i40e_pf *pf = vsi->back;
4009 int i, pf_q, ret;
4010
4011 pf_q = vsi->base_queue;
4012 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4013 /* Check and wait for the disable status of the queue */
4014 ret = i40e_pf_txq_wait(pf, pf_q, false);
4015 if (ret) {
4016 dev_info(&pf->pdev->dev,
4017 "%s: VSI seid %d Tx ring %d disable timeout\n",
4018 __func__, vsi->seid, pf_q);
4019 return ret;
4020 }
4021 }
4022
4023 return 0;
4024}
4025
4026/**
4027 * i40e_pf_wait_txq_disabled - Wait for all queues of PF VSIs to be disabled
4028 * @pf: the PF
4029 *
4030 * This function waits for the Tx queues to be in disabled state for all the
4031 * VSIs that are managed by this PF.
4032 **/
4033static int i40e_pf_wait_txq_disabled(struct i40e_pf *pf)
4034{
4035 int v, ret = 0;
4036
4037 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004038 /* No need to wait for FCoE VSI queues */
4039 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh69129dc2014-11-12 00:18:46 +00004040 ret = i40e_vsi_wait_txq_disabled(pf->vsi[v]);
4041 if (ret)
4042 break;
4043 }
4044 }
4045
4046 return ret;
4047}
4048
4049#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004050/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004051 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004052 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004053 *
4054 * Get TC map for ISCSI PF type that will include iSCSI TC
4055 * and LAN TC.
4056 **/
4057static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4058{
4059 struct i40e_dcb_app_priority_table app;
4060 struct i40e_hw *hw = &pf->hw;
4061 u8 enabled_tc = 1; /* TC0 is always enabled */
4062 u8 tc, i;
4063 /* Get the iSCSI APP TLV */
4064 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4065
4066 for (i = 0; i < dcbcfg->numapps; i++) {
4067 app = dcbcfg->app[i];
4068 if (app.selector == I40E_APP_SEL_TCPIP &&
4069 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4070 tc = dcbcfg->etscfg.prioritytable[app.priority];
4071 enabled_tc |= (1 << tc);
4072 break;
4073 }
4074 }
4075
4076 return enabled_tc;
4077}
4078
4079/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004080 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4081 * @dcbcfg: the corresponding DCBx configuration structure
4082 *
4083 * Return the number of TCs from given DCBx configuration
4084 **/
4085static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4086{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004087 u8 num_tc = 0;
4088 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004089
4090 /* Scan the ETS Config Priority Table to find
4091 * traffic class enabled for a given priority
4092 * and use the traffic class index to get the
4093 * number of traffic classes enabled
4094 */
4095 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4096 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
4097 num_tc = dcbcfg->etscfg.prioritytable[i];
4098 }
4099
4100 /* Traffic class index starts from zero so
4101 * increment to return the actual count
4102 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004103 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004104}
4105
4106/**
4107 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4108 * @dcbcfg: the corresponding DCBx configuration structure
4109 *
4110 * Query the current DCB configuration and return the number of
4111 * traffic classes enabled from the given DCBX config
4112 **/
4113static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4114{
4115 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4116 u8 enabled_tc = 1;
4117 u8 i;
4118
4119 for (i = 0; i < num_tc; i++)
4120 enabled_tc |= 1 << i;
4121
4122 return enabled_tc;
4123}
4124
4125/**
4126 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4127 * @pf: PF being queried
4128 *
4129 * Return number of traffic classes enabled for the given PF
4130 **/
4131static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4132{
4133 struct i40e_hw *hw = &pf->hw;
4134 u8 i, enabled_tc;
4135 u8 num_tc = 0;
4136 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4137
4138 /* If DCB is not enabled then always in single TC */
4139 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4140 return 1;
4141
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004142 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004143 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4144 return i40e_dcb_get_num_tc(dcbcfg);
4145
4146 /* MFP mode return count of enabled TCs for this PF */
4147 if (pf->hw.func_caps.iscsi)
4148 enabled_tc = i40e_get_iscsi_tc_map(pf);
4149 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004150 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004151
4152 /* At least have TC0 */
4153 enabled_tc = (enabled_tc ? enabled_tc : 0x1);
4154 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4155 if (enabled_tc & (1 << i))
4156 num_tc++;
4157 }
4158 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004159}
4160
4161/**
4162 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
4163 * @pf: PF being queried
4164 *
4165 * Return a bitmap for first enabled traffic class for this PF.
4166 **/
4167static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
4168{
4169 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
4170 u8 i = 0;
4171
4172 if (!enabled_tc)
4173 return 0x1; /* TC0 */
4174
4175 /* Find the first enabled TC */
4176 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4177 if (enabled_tc & (1 << i))
4178 break;
4179 }
4180
4181 return 1 << i;
4182}
4183
4184/**
4185 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4186 * @pf: PF being queried
4187 *
4188 * Return a bitmap for enabled traffic classes for this PF.
4189 **/
4190static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4191{
4192 /* If DCB is not enabled for this PF then just return default TC */
4193 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4194 return i40e_pf_get_default_tc(pf);
4195
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004196 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004197 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4198 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4199
Neerav Parikhfc51de92015-02-24 06:58:53 +00004200 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004201 if (pf->hw.func_caps.iscsi)
4202 return i40e_get_iscsi_tc_map(pf);
4203 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004204 return i40e_pf_get_default_tc(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004205}
4206
4207/**
4208 * i40e_vsi_get_bw_info - Query VSI BW Information
4209 * @vsi: the VSI being queried
4210 *
4211 * Returns 0 on success, negative value on failure
4212 **/
4213static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4214{
4215 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4216 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4217 struct i40e_pf *pf = vsi->back;
4218 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004219 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004220 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004221 int i;
4222
4223 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004224 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4225 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004226 dev_info(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004227 "couldn't get PF vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004228 aq_ret, pf->hw.aq.asq_last_status);
4229 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004230 }
4231
4232 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004233 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
Jesse Brandeburg6838b532014-01-14 00:49:52 -08004234 NULL);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004235 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004236 dev_info(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004237 "couldn't get PF vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004238 aq_ret, pf->hw.aq.asq_last_status);
4239 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004240 }
4241
4242 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4243 dev_info(&pf->pdev->dev,
4244 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4245 bw_config.tc_valid_bits,
4246 bw_ets_config.tc_valid_bits);
4247 /* Still continuing */
4248 }
4249
4250 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4251 vsi->bw_max_quanta = bw_config.max_bw;
4252 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4253 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4254 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4255 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4256 vsi->bw_ets_limit_credits[i] =
4257 le16_to_cpu(bw_ets_config.credits[i]);
4258 /* 3 bits out of 4 for each TC */
4259 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4260 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004261
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004262 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004263}
4264
4265/**
4266 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4267 * @vsi: the VSI being configured
4268 * @enabled_tc: TC bitmap
4269 * @bw_credits: BW shared credits per TC
4270 *
4271 * Returns 0 on success, negative value on failure
4272 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004273static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004274 u8 *bw_share)
4275{
4276 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004277 i40e_status aq_ret;
4278 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004279
4280 bw_data.tc_valid_bits = enabled_tc;
4281 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4282 bw_data.tc_bw_credits[i] = bw_share[i];
4283
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004284 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4285 NULL);
4286 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004287 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004288 "AQ command Config VSI BW allocation per TC failed = %d\n",
4289 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004290 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004291 }
4292
4293 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4294 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4295
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004296 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004297}
4298
4299/**
4300 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4301 * @vsi: the VSI being configured
4302 * @enabled_tc: TC map to be enabled
4303 *
4304 **/
4305static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4306{
4307 struct net_device *netdev = vsi->netdev;
4308 struct i40e_pf *pf = vsi->back;
4309 struct i40e_hw *hw = &pf->hw;
4310 u8 netdev_tc = 0;
4311 int i;
4312 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4313
4314 if (!netdev)
4315 return;
4316
4317 if (!enabled_tc) {
4318 netdev_reset_tc(netdev);
4319 return;
4320 }
4321
4322 /* Set up actual enabled TCs on the VSI */
4323 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4324 return;
4325
4326 /* set per TC queues for the VSI */
4327 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4328 /* Only set TC queues for enabled tcs
4329 *
4330 * e.g. For a VSI that has TC0 and TC3 enabled the
4331 * enabled_tc bitmap would be 0x00001001; the driver
4332 * will set the numtc for netdev as 2 that will be
4333 * referenced by the netdev layer as TC 0 and 1.
4334 */
4335 if (vsi->tc_config.enabled_tc & (1 << i))
4336 netdev_set_tc_queue(netdev,
4337 vsi->tc_config.tc_info[i].netdev_tc,
4338 vsi->tc_config.tc_info[i].qcount,
4339 vsi->tc_config.tc_info[i].qoffset);
4340 }
4341
4342 /* Assign UP2TC map for the VSI */
4343 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4344 /* Get the actual TC# for the UP */
4345 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4346 /* Get the mapped netdev TC# for the UP */
4347 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4348 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4349 }
4350}
4351
4352/**
4353 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4354 * @vsi: the VSI being configured
4355 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4356 **/
4357static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4358 struct i40e_vsi_context *ctxt)
4359{
4360 /* copy just the sections touched not the entire info
4361 * since not all sections are valid as returned by
4362 * update vsi params
4363 */
4364 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4365 memcpy(&vsi->info.queue_mapping,
4366 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4367 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4368 sizeof(vsi->info.tc_mapping));
4369}
4370
4371/**
4372 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4373 * @vsi: VSI to be configured
4374 * @enabled_tc: TC bitmap
4375 *
4376 * This configures a particular VSI for TCs that are mapped to the
4377 * given TC bitmap. It uses default bandwidth share for TCs across
4378 * VSIs to configure TC for a particular VSI.
4379 *
4380 * NOTE:
4381 * It is expected that the VSI queues have been quisced before calling
4382 * this function.
4383 **/
4384static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4385{
4386 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4387 struct i40e_vsi_context ctxt;
4388 int ret = 0;
4389 int i;
4390
4391 /* Check if enabled_tc is same as existing or new TCs */
4392 if (vsi->tc_config.enabled_tc == enabled_tc)
4393 return ret;
4394
4395 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4396 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4397 if (enabled_tc & (1 << i))
4398 bw_share[i] = 1;
4399 }
4400
4401 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4402 if (ret) {
4403 dev_info(&vsi->back->pdev->dev,
4404 "Failed configuring TC map %d for VSI %d\n",
4405 enabled_tc, vsi->seid);
4406 goto out;
4407 }
4408
4409 /* Update Queue Pairs Mapping for currently enabled UPs */
4410 ctxt.seid = vsi->seid;
4411 ctxt.pf_num = vsi->back->hw.pf_id;
4412 ctxt.vf_num = 0;
4413 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004414 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004415 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4416
4417 /* Update the VSI after updating the VSI queue-mapping information */
4418 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4419 if (ret) {
4420 dev_info(&vsi->back->pdev->dev,
4421 "update vsi failed, aq_err=%d\n",
4422 vsi->back->hw.aq.asq_last_status);
4423 goto out;
4424 }
4425 /* update the local VSI info with updated queue map */
4426 i40e_vsi_update_queue_map(vsi, &ctxt);
4427 vsi->info.valid_sections = 0;
4428
4429 /* Update current VSI BW information */
4430 ret = i40e_vsi_get_bw_info(vsi);
4431 if (ret) {
4432 dev_info(&vsi->back->pdev->dev,
4433 "Failed updating vsi bw info, aq_err=%d\n",
4434 vsi->back->hw.aq.asq_last_status);
4435 goto out;
4436 }
4437
4438 /* Update the netdev TC setup */
4439 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4440out:
4441 return ret;
4442}
4443
4444/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004445 * i40e_veb_config_tc - Configure TCs for given VEB
4446 * @veb: given VEB
4447 * @enabled_tc: TC bitmap
4448 *
4449 * Configures given TC bitmap for VEB (switching) element
4450 **/
4451int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4452{
4453 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4454 struct i40e_pf *pf = veb->pf;
4455 int ret = 0;
4456 int i;
4457
4458 /* No TCs or already enabled TCs just return */
4459 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4460 return ret;
4461
4462 bw_data.tc_valid_bits = enabled_tc;
4463 /* bw_data.absolute_credits is not set (relative) */
4464
4465 /* Enable ETS TCs with equal BW Share for now */
4466 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4467 if (enabled_tc & (1 << i))
4468 bw_data.tc_bw_share_credits[i] = 1;
4469 }
4470
4471 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4472 &bw_data, NULL);
4473 if (ret) {
4474 dev_info(&pf->pdev->dev,
4475 "veb bw config failed, aq_err=%d\n",
4476 pf->hw.aq.asq_last_status);
4477 goto out;
4478 }
4479
4480 /* Update the BW information */
4481 ret = i40e_veb_get_bw_info(veb);
4482 if (ret) {
4483 dev_info(&pf->pdev->dev,
4484 "Failed getting veb bw config, aq_err=%d\n",
4485 pf->hw.aq.asq_last_status);
4486 }
4487
4488out:
4489 return ret;
4490}
4491
4492#ifdef CONFIG_I40E_DCB
4493/**
4494 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4495 * @pf: PF struct
4496 *
4497 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4498 * the caller would've quiesce all the VSIs before calling
4499 * this function
4500 **/
4501static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4502{
4503 u8 tc_map = 0;
4504 int ret;
4505 u8 v;
4506
4507 /* Enable the TCs available on PF to all VEBs */
4508 tc_map = i40e_pf_get_tc_map(pf);
4509 for (v = 0; v < I40E_MAX_VEB; v++) {
4510 if (!pf->veb[v])
4511 continue;
4512 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4513 if (ret) {
4514 dev_info(&pf->pdev->dev,
4515 "Failed configuring TC for VEB seid=%d\n",
4516 pf->veb[v]->seid);
4517 /* Will try to configure as many components */
4518 }
4519 }
4520
4521 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004522 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004523 if (!pf->vsi[v])
4524 continue;
4525
4526 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07004527#ifdef I40E_FCOE
4528 * - For FCoE VSI only enable the TC configured
4529 * as per the APP TLV
4530#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004531 * - For all others keep them at TC0 for now
4532 */
4533 if (v == pf->lan_vsi)
4534 tc_map = i40e_pf_get_tc_map(pf);
4535 else
4536 tc_map = i40e_pf_get_default_tc(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07004537#ifdef I40E_FCOE
4538 if (pf->vsi[v]->type == I40E_VSI_FCOE)
4539 tc_map = i40e_get_fcoe_tc_map(pf);
4540#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004541
4542 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4543 if (ret) {
4544 dev_info(&pf->pdev->dev,
4545 "Failed configuring TC for VSI seid=%d\n",
4546 pf->vsi[v]->seid);
4547 /* Will try to configure as many components */
4548 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004549 /* Re-configure VSI vectors based on updated TC map */
4550 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004551 if (pf->vsi[v]->netdev)
4552 i40e_dcbnl_set_all(pf->vsi[v]);
4553 }
4554 }
4555}
4556
4557/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00004558 * i40e_resume_port_tx - Resume port Tx
4559 * @pf: PF struct
4560 *
4561 * Resume a port's Tx and issue a PF reset in case of failure to
4562 * resume.
4563 **/
4564static int i40e_resume_port_tx(struct i40e_pf *pf)
4565{
4566 struct i40e_hw *hw = &pf->hw;
4567 int ret;
4568
4569 ret = i40e_aq_resume_port_tx(hw, NULL);
4570 if (ret) {
4571 dev_info(&pf->pdev->dev,
4572 "AQ command Resume Port Tx failed = %d\n",
4573 pf->hw.aq.asq_last_status);
4574 /* Schedule PF reset to recover */
4575 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4576 i40e_service_event_schedule(pf);
4577 }
4578
4579 return ret;
4580}
4581
4582/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004583 * i40e_init_pf_dcb - Initialize DCB configuration
4584 * @pf: PF being configured
4585 *
4586 * Query the current DCB configuration and cache it
4587 * in the hardware structure
4588 **/
4589static int i40e_init_pf_dcb(struct i40e_pf *pf)
4590{
4591 struct i40e_hw *hw = &pf->hw;
4592 int err = 0;
4593
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00004594 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
4595 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
4596 (pf->hw.aq.fw_maj_ver < 4))
4597 goto out;
4598
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004599 /* Get the initial DCB configuration */
4600 err = i40e_init_dcb(hw);
4601 if (!err) {
4602 /* Device/Function is not DCBX capable */
4603 if ((!hw->func_caps.dcb) ||
4604 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4605 dev_info(&pf->pdev->dev,
4606 "DCBX offload is not supported or is disabled for this PF.\n");
4607
4608 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4609 goto out;
4610
4611 } else {
4612 /* When status is not DISABLED then DCBX in FW */
4613 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4614 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004615
4616 pf->flags |= I40E_FLAG_DCB_CAPABLE;
4617 /* Enable DCB tagging only when more than one TC */
4618 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
4619 pf->flags |= I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00004620 dev_dbg(&pf->pdev->dev,
4621 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004622 }
Neerav Parikh014269f2014-04-01 07:11:48 +00004623 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00004624 dev_info(&pf->pdev->dev,
4625 "AQ Querying DCB configuration failed: aq_err %d\n",
Neerav Parikh014269f2014-04-01 07:11:48 +00004626 pf->hw.aq.asq_last_status);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004627 }
4628
4629out:
4630 return err;
4631}
4632#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004633#define SPEED_SIZE 14
4634#define FC_SIZE 8
4635/**
4636 * i40e_print_link_message - print link up or down
4637 * @vsi: the VSI for which link needs a message
4638 */
4639static void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
4640{
4641 char speed[SPEED_SIZE] = "Unknown";
4642 char fc[FC_SIZE] = "RX/TX";
4643
4644 if (!isup) {
4645 netdev_info(vsi->netdev, "NIC Link is Down\n");
4646 return;
4647 }
4648
Greg Rose148c2d82014-12-11 07:06:27 +00004649 /* Warn user if link speed on NPAR enabled partition is not at
4650 * least 10GB
4651 */
4652 if (vsi->back->hw.func_caps.npar_enable &&
4653 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
4654 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
4655 netdev_warn(vsi->netdev,
4656 "The partition detected link speed that is less than 10Gbps\n");
4657
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004658 switch (vsi->back->hw.phy.link_info.link_speed) {
4659 case I40E_LINK_SPEED_40GB:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004660 strlcpy(speed, "40 Gbps", SPEED_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004661 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07004662 case I40E_LINK_SPEED_20GB:
4663 strncpy(speed, "20 Gbps", SPEED_SIZE);
4664 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004665 case I40E_LINK_SPEED_10GB:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004666 strlcpy(speed, "10 Gbps", SPEED_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004667 break;
4668 case I40E_LINK_SPEED_1GB:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004669 strlcpy(speed, "1000 Mbps", SPEED_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004670 break;
Mitch Williams5960d332014-09-13 07:40:47 +00004671 case I40E_LINK_SPEED_100MB:
4672 strncpy(speed, "100 Mbps", SPEED_SIZE);
4673 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004674 default:
4675 break;
4676 }
4677
4678 switch (vsi->back->hw.fc.current_mode) {
4679 case I40E_FC_FULL:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004680 strlcpy(fc, "RX/TX", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004681 break;
4682 case I40E_FC_TX_PAUSE:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004683 strlcpy(fc, "TX", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004684 break;
4685 case I40E_FC_RX_PAUSE:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004686 strlcpy(fc, "RX", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004687 break;
4688 default:
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00004689 strlcpy(fc, "None", FC_SIZE);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004690 break;
4691 }
4692
4693 netdev_info(vsi->netdev, "NIC Link is Up %s Full Duplex, Flow Control: %s\n",
4694 speed, fc);
4695}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004696
4697/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004698 * i40e_up_complete - Finish the last steps of bringing up a connection
4699 * @vsi: the VSI being configured
4700 **/
4701static int i40e_up_complete(struct i40e_vsi *vsi)
4702{
4703 struct i40e_pf *pf = vsi->back;
4704 int err;
4705
4706 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4707 i40e_vsi_configure_msix(vsi);
4708 else
4709 i40e_configure_msi_and_legacy(vsi);
4710
4711 /* start rings */
4712 err = i40e_vsi_control_rings(vsi, true);
4713 if (err)
4714 return err;
4715
4716 clear_bit(__I40E_DOWN, &vsi->state);
4717 i40e_napi_enable_all(vsi);
4718 i40e_vsi_enable_irq(vsi);
4719
4720 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4721 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004722 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004723 netif_tx_start_all_queues(vsi->netdev);
4724 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004725 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004726 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00004727 /* need to check for qualified module here*/
4728 if ((pf->hw.phy.link_info.link_info &
4729 I40E_AQ_MEDIA_AVAILABLE) &&
4730 (!(pf->hw.phy.link_info.an_info &
4731 I40E_AQ_QUALIFIED_MODULE)))
4732 netdev_err(vsi->netdev,
4733 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004734 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004735
4736 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00004737 if (vsi->type == I40E_VSI_FDIR) {
4738 /* reset fd counters */
4739 pf->fd_add_err = pf->fd_atr_cnt = 0;
4740 if (pf->fd_tcp_rule > 0) {
4741 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
4742 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
4743 pf->fd_tcp_rule = 0;
4744 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004745 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00004746 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004747 i40e_service_event_schedule(pf);
4748
4749 return 0;
4750}
4751
4752/**
4753 * i40e_vsi_reinit_locked - Reset the VSI
4754 * @vsi: the VSI being configured
4755 *
4756 * Rebuild the ring structs after some configuration
4757 * has changed, e.g. MTU size.
4758 **/
4759static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4760{
4761 struct i40e_pf *pf = vsi->back;
4762
4763 WARN_ON(in_interrupt());
4764 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4765 usleep_range(1000, 2000);
4766 i40e_down(vsi);
4767
4768 /* Give a VF some time to respond to the reset. The
4769 * two second wait is based upon the watchdog cycle in
4770 * the VF driver.
4771 */
4772 if (vsi->type == I40E_VSI_SRIOV)
4773 msleep(2000);
4774 i40e_up(vsi);
4775 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
4776}
4777
4778/**
4779 * i40e_up - Bring the connection back up after being down
4780 * @vsi: the VSI being configured
4781 **/
4782int i40e_up(struct i40e_vsi *vsi)
4783{
4784 int err;
4785
4786 err = i40e_vsi_configure(vsi);
4787 if (!err)
4788 err = i40e_up_complete(vsi);
4789
4790 return err;
4791}
4792
4793/**
4794 * i40e_down - Shutdown the connection processing
4795 * @vsi: the VSI being stopped
4796 **/
4797void i40e_down(struct i40e_vsi *vsi)
4798{
4799 int i;
4800
4801 /* It is assumed that the caller of this function
4802 * sets the vsi->state __I40E_DOWN bit.
4803 */
4804 if (vsi->netdev) {
4805 netif_carrier_off(vsi->netdev);
4806 netif_tx_disable(vsi->netdev);
4807 }
4808 i40e_vsi_disable_irq(vsi);
4809 i40e_vsi_control_rings(vsi, false);
4810 i40e_napi_disable_all(vsi);
4811
4812 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00004813 i40e_clean_tx_ring(vsi->tx_rings[i]);
4814 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004815 }
4816}
4817
4818/**
4819 * i40e_setup_tc - configure multiple traffic classes
4820 * @netdev: net device to configure
4821 * @tc: number of traffic classes to enable
4822 **/
Vasu Dev38e00432014-08-01 13:27:03 -07004823#ifdef I40E_FCOE
4824int i40e_setup_tc(struct net_device *netdev, u8 tc)
4825#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004826static int i40e_setup_tc(struct net_device *netdev, u8 tc)
Vasu Dev38e00432014-08-01 13:27:03 -07004827#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004828{
4829 struct i40e_netdev_priv *np = netdev_priv(netdev);
4830 struct i40e_vsi *vsi = np->vsi;
4831 struct i40e_pf *pf = vsi->back;
4832 u8 enabled_tc = 0;
4833 int ret = -EINVAL;
4834 int i;
4835
4836 /* Check if DCB enabled to continue */
4837 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
4838 netdev_info(netdev, "DCB is not enabled for adapter\n");
4839 goto exit;
4840 }
4841
4842 /* Check if MFP enabled */
4843 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4844 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
4845 goto exit;
4846 }
4847
4848 /* Check whether tc count is within enabled limit */
4849 if (tc > i40e_pf_get_num_tc(pf)) {
4850 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
4851 goto exit;
4852 }
4853
4854 /* Generate TC map for number of tc requested */
4855 for (i = 0; i < tc; i++)
4856 enabled_tc |= (1 << i);
4857
4858 /* Requesting same TC configuration as already enabled */
4859 if (enabled_tc == vsi->tc_config.enabled_tc)
4860 return 0;
4861
4862 /* Quiesce VSI queues */
4863 i40e_quiesce_vsi(vsi);
4864
4865 /* Configure VSI for enabled TCs */
4866 ret = i40e_vsi_config_tc(vsi, enabled_tc);
4867 if (ret) {
4868 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
4869 vsi->seid);
4870 goto exit;
4871 }
4872
4873 /* Unquiesce VSI */
4874 i40e_unquiesce_vsi(vsi);
4875
4876exit:
4877 return ret;
4878}
4879
4880/**
4881 * i40e_open - Called when a network interface is made active
4882 * @netdev: network interface device structure
4883 *
4884 * The open entry point is called when a network interface is made
4885 * active by the system (IFF_UP). At this point all resources needed
4886 * for transmit and receive operations are allocated, the interrupt
4887 * handler is registered with the OS, the netdev watchdog subtask is
4888 * enabled, and the stack is notified that the interface is ready.
4889 *
4890 * Returns 0 on success, negative value on failure
4891 **/
Vasu Dev38e00432014-08-01 13:27:03 -07004892int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004893{
4894 struct i40e_netdev_priv *np = netdev_priv(netdev);
4895 struct i40e_vsi *vsi = np->vsi;
4896 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004897 int err;
4898
Shannon Nelson4eb3f762014-03-06 08:59:58 +00004899 /* disallow open during test or if eeprom is broken */
4900 if (test_bit(__I40E_TESTING, &pf->state) ||
4901 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004902 return -EBUSY;
4903
4904 netif_carrier_off(netdev);
4905
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004906 err = i40e_vsi_open(vsi);
4907 if (err)
4908 return err;
4909
Jesse Brandeburg059dab62014-04-01 09:07:20 +00004910 /* configure global TSO hardware offload settings */
4911 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
4912 TCP_FLAG_FIN) >> 16);
4913 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
4914 TCP_FLAG_FIN |
4915 TCP_FLAG_CWR) >> 16);
4916 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
4917
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004918#ifdef CONFIG_I40E_VXLAN
4919 vxlan_get_rx_port(netdev);
4920#endif
4921
4922 return 0;
4923}
4924
4925/**
4926 * i40e_vsi_open -
4927 * @vsi: the VSI to open
4928 *
4929 * Finish initialization of the VSI.
4930 *
4931 * Returns 0 on success, negative value on failure
4932 **/
4933int i40e_vsi_open(struct i40e_vsi *vsi)
4934{
4935 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00004936 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004937 int err;
4938
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004939 /* allocate descriptors */
4940 err = i40e_vsi_setup_tx_resources(vsi);
4941 if (err)
4942 goto err_setup_tx;
4943 err = i40e_vsi_setup_rx_resources(vsi);
4944 if (err)
4945 goto err_setup_rx;
4946
4947 err = i40e_vsi_configure(vsi);
4948 if (err)
4949 goto err_setup_rx;
4950
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004951 if (vsi->netdev) {
4952 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4953 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
4954 err = i40e_vsi_request_irq(vsi, int_name);
4955 if (err)
4956 goto err_setup_rx;
4957
4958 /* Notify the stack of the actual queue counts. */
4959 err = netif_set_real_num_tx_queues(vsi->netdev,
4960 vsi->num_queue_pairs);
4961 if (err)
4962 goto err_set_queues;
4963
4964 err = netif_set_real_num_rx_queues(vsi->netdev,
4965 vsi->num_queue_pairs);
4966 if (err)
4967 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00004968
4969 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00004970 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00004971 dev_driver_string(&pf->pdev->dev),
4972 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00004973 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00004974
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004975 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00004976 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004977 goto err_setup_rx;
4978 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004979
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004980 err = i40e_up_complete(vsi);
4981 if (err)
4982 goto err_up_complete;
4983
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004984 return 0;
4985
4986err_up_complete:
4987 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004988err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004989 i40e_vsi_free_irq(vsi);
4990err_setup_rx:
4991 i40e_vsi_free_rx_resources(vsi);
4992err_setup_tx:
4993 i40e_vsi_free_tx_resources(vsi);
4994 if (vsi == pf->vsi[pf->lan_vsi])
4995 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4996
4997 return err;
4998}
4999
5000/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005001 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005002 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005003 *
5004 * This function destroys the hlist where all the Flow Director
5005 * filters were saved.
5006 **/
5007static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5008{
5009 struct i40e_fdir_filter *filter;
5010 struct hlist_node *node2;
5011
5012 hlist_for_each_entry_safe(filter, node2,
5013 &pf->fdir_filter_list, fdir_node) {
5014 hlist_del(&filter->fdir_node);
5015 kfree(filter);
5016 }
5017 pf->fdir_pf_active_filters = 0;
5018}
5019
5020/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005021 * i40e_close - Disables a network interface
5022 * @netdev: network interface device structure
5023 *
5024 * The close entry point is called when an interface is de-activated
5025 * by the OS. The hardware is still under the driver's control, but
5026 * this netdev interface is disabled.
5027 *
5028 * Returns 0, this is not allowed to fail
5029 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005030#ifdef I40E_FCOE
5031int i40e_close(struct net_device *netdev)
5032#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005033static int i40e_close(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07005034#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005035{
5036 struct i40e_netdev_priv *np = netdev_priv(netdev);
5037 struct i40e_vsi *vsi = np->vsi;
5038
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005039 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005040
5041 return 0;
5042}
5043
5044/**
5045 * i40e_do_reset - Start a PF or Core Reset sequence
5046 * @pf: board private structure
5047 * @reset_flags: which reset is requested
5048 *
5049 * The essential difference in resets is that the PF Reset
5050 * doesn't clear the packet buffers, doesn't reset the PE
5051 * firmware, and doesn't bother the other PFs on the chip.
5052 **/
5053void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5054{
5055 u32 val;
5056
5057 WARN_ON(in_interrupt());
5058
Mitch Williams263fc482014-04-23 04:50:11 +00005059 if (i40e_check_asq_alive(&pf->hw))
5060 i40e_vc_notify_reset(pf);
5061
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005062 /* do the biggest reset indicated */
5063 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
5064
5065 /* Request a Global Reset
5066 *
5067 * This will start the chip's countdown to the actual full
5068 * chip reset event, and a warning interrupt to be sent
5069 * to all PFs, including the requestor. Our handler
5070 * for the warning interrupt will deal with the shutdown
5071 * and recovery of the switch setup.
5072 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005073 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005074 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5075 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5076 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5077
5078 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
5079
5080 /* Request a Core Reset
5081 *
5082 * Same as Global Reset, except does *not* include the MAC/PHY
5083 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005084 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005085 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5086 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5087 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5088 i40e_flush(&pf->hw);
5089
5090 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
5091
5092 /* Request a PF Reset
5093 *
5094 * Resets only the PF-specific registers
5095 *
5096 * This goes directly to the tear-down and rebuild of
5097 * the switch, since we need to do all the recovery as
5098 * for the Core Reset.
5099 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005100 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005101 i40e_handle_reset_warning(pf);
5102
5103 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
5104 int v;
5105
5106 /* Find the VSI(s) that requested a re-init */
5107 dev_info(&pf->pdev->dev,
5108 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005109 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005110 struct i40e_vsi *vsi = pf->vsi[v];
5111 if (vsi != NULL &&
5112 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5113 i40e_vsi_reinit_locked(pf->vsi[v]);
5114 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5115 }
5116 }
5117
5118 /* no further action needed, so return now */
5119 return;
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005120 } else if (reset_flags & (1 << __I40E_DOWN_REQUESTED)) {
5121 int v;
5122
5123 /* Find the VSI(s) that needs to be brought down */
5124 dev_info(&pf->pdev->dev, "VSI down requested\n");
5125 for (v = 0; v < pf->num_alloc_vsi; v++) {
5126 struct i40e_vsi *vsi = pf->vsi[v];
5127 if (vsi != NULL &&
5128 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5129 set_bit(__I40E_DOWN, &vsi->state);
5130 i40e_down(vsi);
5131 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5132 }
5133 }
5134
5135 /* no further action needed, so return now */
5136 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005137 } else {
5138 dev_info(&pf->pdev->dev,
5139 "bad reset request 0x%08x\n", reset_flags);
5140 return;
5141 }
5142}
5143
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005144#ifdef CONFIG_I40E_DCB
5145/**
5146 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5147 * @pf: board private structure
5148 * @old_cfg: current DCB config
5149 * @new_cfg: new DCB config
5150 **/
5151bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5152 struct i40e_dcbx_config *old_cfg,
5153 struct i40e_dcbx_config *new_cfg)
5154{
5155 bool need_reconfig = false;
5156
5157 /* Check if ETS configuration has changed */
5158 if (memcmp(&new_cfg->etscfg,
5159 &old_cfg->etscfg,
5160 sizeof(new_cfg->etscfg))) {
5161 /* If Priority Table has changed reconfig is needed */
5162 if (memcmp(&new_cfg->etscfg.prioritytable,
5163 &old_cfg->etscfg.prioritytable,
5164 sizeof(new_cfg->etscfg.prioritytable))) {
5165 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005166 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005167 }
5168
5169 if (memcmp(&new_cfg->etscfg.tcbwtable,
5170 &old_cfg->etscfg.tcbwtable,
5171 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005172 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005173
5174 if (memcmp(&new_cfg->etscfg.tsatable,
5175 &old_cfg->etscfg.tsatable,
5176 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005177 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005178 }
5179
5180 /* Check if PFC configuration has changed */
5181 if (memcmp(&new_cfg->pfc,
5182 &old_cfg->pfc,
5183 sizeof(new_cfg->pfc))) {
5184 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005185 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005186 }
5187
5188 /* Check if APP Table has changed */
5189 if (memcmp(&new_cfg->app,
5190 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005191 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005192 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005193 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005194 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005195
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005196 dev_dbg(&pf->pdev->dev, "%s: need_reconfig=%d\n", __func__,
5197 need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005198 return need_reconfig;
5199}
5200
5201/**
5202 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5203 * @pf: board private structure
5204 * @e: event info posted on ARQ
5205 **/
5206static int i40e_handle_lldp_event(struct i40e_pf *pf,
5207 struct i40e_arq_event_info *e)
5208{
5209 struct i40e_aqc_lldp_get_mib *mib =
5210 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5211 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005212 struct i40e_dcbx_config tmp_dcbx_cfg;
5213 bool need_reconfig = false;
5214 int ret = 0;
5215 u8 type;
5216
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005217 /* Not DCB capable or capability disabled */
5218 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
5219 return ret;
5220
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005221 /* Ignore if event is not for Nearest Bridge */
5222 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5223 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005224 dev_dbg(&pf->pdev->dev,
5225 "%s: LLDP event mib bridge type 0x%x\n", __func__, type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005226 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5227 return ret;
5228
5229 /* Check MIB Type and return if event for Remote MIB update */
5230 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005231 dev_dbg(&pf->pdev->dev,
5232 "%s: LLDP event mib type %s\n", __func__,
5233 type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005234 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5235 /* Update the remote cached instance and return */
5236 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5237 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5238 &hw->remote_dcbx_config);
5239 goto exit;
5240 }
5241
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005242 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005243 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005244
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005245 /* Reset the old DCBx configuration data */
5246 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005247 /* Get updated DCBX data from firmware */
5248 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005249 if (ret) {
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005250 dev_info(&pf->pdev->dev, "Failed querying DCB configuration data from firmware.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005251 goto exit;
5252 }
5253
5254 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005255 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5256 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005257 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005258 goto exit;
5259 }
5260
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005261 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5262 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005263
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005264 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005265
5266 if (!need_reconfig)
5267 goto exit;
5268
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005269 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005270 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005271 pf->flags |= I40E_FLAG_DCB_ENABLED;
5272 else
5273 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5274
Neerav Parikh69129dc2014-11-12 00:18:46 +00005275 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005276 /* Reconfiguration needed quiesce all VSIs */
5277 i40e_pf_quiesce_all_vsi(pf);
5278
5279 /* Changes in configuration update VEB/VSI */
5280 i40e_dcb_reconfigure(pf);
5281
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005282 ret = i40e_resume_port_tx(pf);
5283
Neerav Parikh69129dc2014-11-12 00:18:46 +00005284 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005285 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005286 if (ret)
5287 goto exit;
5288
5289 /* Wait for the PF's Tx queues to be disabled */
5290 ret = i40e_pf_wait_txq_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005291 if (ret) {
5292 /* Schedule PF reset to recover */
5293 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5294 i40e_service_event_schedule(pf);
5295 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005296 i40e_pf_unquiesce_all_vsi(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005297 }
5298
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005299exit:
5300 return ret;
5301}
5302#endif /* CONFIG_I40E_DCB */
5303
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005304/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005305 * i40e_do_reset_safe - Protected reset path for userland calls.
5306 * @pf: board private structure
5307 * @reset_flags: which reset is requested
5308 *
5309 **/
5310void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5311{
5312 rtnl_lock();
5313 i40e_do_reset(pf, reset_flags);
5314 rtnl_unlock();
5315}
5316
5317/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005318 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5319 * @pf: board private structure
5320 * @e: event info posted on ARQ
5321 *
5322 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5323 * and VF queues
5324 **/
5325static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5326 struct i40e_arq_event_info *e)
5327{
5328 struct i40e_aqc_lan_overflow *data =
5329 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5330 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5331 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5332 struct i40e_hw *hw = &pf->hw;
5333 struct i40e_vf *vf;
5334 u16 vf_id;
5335
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005336 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5337 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005338
5339 /* Queue belongs to VF, find the VF and issue VF reset */
5340 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5341 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5342 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5343 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5344 vf_id -= hw->func_caps.vf_base_id;
5345 vf = &pf->vf[vf_id];
5346 i40e_vc_notify_vf_reset(vf);
5347 /* Allow VF to process pending reset notification */
5348 msleep(20);
5349 i40e_reset_vf(vf, false);
5350 }
5351}
5352
5353/**
5354 * i40e_service_event_complete - Finish up the service event
5355 * @pf: board private structure
5356 **/
5357static void i40e_service_event_complete(struct i40e_pf *pf)
5358{
5359 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
5360
5361 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005362 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005363 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5364}
5365
5366/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005367 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5368 * @pf: board private structure
5369 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005370u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005371{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005372 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005373
5374 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5375 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5376 return fcnt_prog;
5377}
5378
5379/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005380 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005381 * @pf: board private structure
5382 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005383u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005384{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005385 u32 val, fcnt_prog;
5386
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005387 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5388 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5389 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5390 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5391 return fcnt_prog;
5392}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005393
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005394/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005395 * i40e_get_global_fd_count - Get total FD filters programmed on device
5396 * @pf: board private structure
5397 **/
5398u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5399{
5400 u32 val, fcnt_prog;
5401
5402 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5403 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5404 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5405 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5406 return fcnt_prog;
5407}
5408
5409/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005410 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5411 * @pf: board private structure
5412 **/
5413void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5414{
5415 u32 fcnt_prog, fcnt_avail;
5416
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005417 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5418 return;
5419
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005420 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5421 * to re-enable
5422 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005423 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005424 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005425 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5426 (pf->fd_add_err == 0) ||
5427 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005428 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5429 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5430 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
5431 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
5432 }
5433 }
5434 /* Wait for some more space to be available to turn on ATR */
5435 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5436 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
5437 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
5438 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
5439 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
5440 }
5441 }
5442}
5443
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005444#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005445#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005446/**
5447 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
5448 * @pf: board private structure
5449 **/
5450static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
5451{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005452 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005453 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005454 bool disable_atr = false;
5455 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005456 int reg;
5457
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005458 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5459 return;
5460
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005461 if (time_after(jiffies, pf->fd_flush_timestamp +
5462 (I40E_MIN_FD_FLUSH_INTERVAL * HZ))) {
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005463 /* If the flush is happening too quick and we have mostly
5464 * SB rules we should not re-enable ATR for some time.
5465 */
5466 min_flush_time = pf->fd_flush_timestamp
5467 + (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
5468 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
5469
5470 if (!(time_after(jiffies, min_flush_time)) &&
5471 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
5472 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
5473 disable_atr = true;
5474 }
5475
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005476 pf->fd_flush_timestamp = jiffies;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005477 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
5478 /* flush all filters */
5479 wr32(&pf->hw, I40E_PFQF_CTL_1,
5480 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
5481 i40e_flush(&pf->hw);
Anjali Singhai Jain60793f4a2014-07-09 07:46:23 +00005482 pf->fd_flush_cnt++;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005483 pf->fd_add_err = 0;
5484 do {
5485 /* Check FD flush status every 5-6msec */
5486 usleep_range(5000, 6000);
5487 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
5488 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
5489 break;
5490 } while (flush_wait_retry--);
5491 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
5492 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
5493 } else {
5494 /* replay sideband filters */
5495 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005496 if (!disable_atr)
5497 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005498 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
5499 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
5500 }
5501 }
5502}
5503
5504/**
5505 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
5506 * @pf: board private structure
5507 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005508u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005509{
5510 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
5511}
5512
5513/* We can see up to 256 filter programming desc in transit if the filters are
5514 * being applied really fast; before we see the first
5515 * filter miss error on Rx queue 0. Accumulating enough error messages before
5516 * reacting will make sure we don't cause flush too often.
5517 */
5518#define I40E_MAX_FD_PROGRAM_ERROR 256
5519
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005520/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005521 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
5522 * @pf: board private structure
5523 **/
5524static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
5525{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005526
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005527 /* if interface is down do nothing */
5528 if (test_bit(__I40E_DOWN, &pf->state))
5529 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005530
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005531 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5532 return;
5533
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005534 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005535 i40e_fdir_flush_and_replay(pf);
5536
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005537 i40e_fdir_check_and_reenable(pf);
5538
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005539}
5540
5541/**
5542 * i40e_vsi_link_event - notify VSI of a link event
5543 * @vsi: vsi to be notified
5544 * @link_up: link up or down
5545 **/
5546static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
5547{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00005548 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005549 return;
5550
5551 switch (vsi->type) {
5552 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07005553#ifdef I40E_FCOE
5554 case I40E_VSI_FCOE:
5555#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005556 if (!vsi->netdev || !vsi->netdev_registered)
5557 break;
5558
5559 if (link_up) {
5560 netif_carrier_on(vsi->netdev);
5561 netif_tx_wake_all_queues(vsi->netdev);
5562 } else {
5563 netif_carrier_off(vsi->netdev);
5564 netif_tx_stop_all_queues(vsi->netdev);
5565 }
5566 break;
5567
5568 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005569 case I40E_VSI_VMDQ2:
5570 case I40E_VSI_CTRL:
5571 case I40E_VSI_MIRROR:
5572 default:
5573 /* there is no notification for other VSIs */
5574 break;
5575 }
5576}
5577
5578/**
5579 * i40e_veb_link_event - notify elements on the veb of a link event
5580 * @veb: veb to be notified
5581 * @link_up: link up or down
5582 **/
5583static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
5584{
5585 struct i40e_pf *pf;
5586 int i;
5587
5588 if (!veb || !veb->pf)
5589 return;
5590 pf = veb->pf;
5591
5592 /* depth first... */
5593 for (i = 0; i < I40E_MAX_VEB; i++)
5594 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
5595 i40e_veb_link_event(pf->veb[i], link_up);
5596
5597 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00005598 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005599 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
5600 i40e_vsi_link_event(pf->vsi[i], link_up);
5601}
5602
5603/**
5604 * i40e_link_event - Update netif_carrier status
5605 * @pf: board private structure
5606 **/
5607static void i40e_link_event(struct i40e_pf *pf)
5608{
5609 bool new_link, old_link;
Mitch Williams320684c2014-10-17 03:14:43 +00005610 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00005611 u8 new_link_speed, old_link_speed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005612
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005613 /* set this to force the get_link_status call to refresh state */
5614 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005615
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005616 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
5617 new_link = i40e_get_link_status(&pf->hw);
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00005618 old_link_speed = pf->hw.phy.link_info_old.link_speed;
5619 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005620
5621 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00005622 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00005623 (test_bit(__I40E_DOWN, &vsi->state) ||
5624 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005625 return;
Mitch Williams320684c2014-10-17 03:14:43 +00005626
5627 if (!test_bit(__I40E_DOWN, &vsi->state))
5628 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005629
5630 /* Notify the base of the switch tree connected to
5631 * the link. Floating VEBs are not notified.
5632 */
5633 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
5634 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
5635 else
Mitch Williams320684c2014-10-17 03:14:43 +00005636 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005637
5638 if (pf->vf)
5639 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005640
5641 if (pf->flags & I40E_FLAG_PTP)
5642 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005643}
5644
5645/**
5646 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
5647 * @pf: board private structure
5648 *
5649 * Set the per-queue flags to request a check for stuck queues in the irq
5650 * clean functions, then force interrupts to be sure the irq clean is called.
5651 **/
5652static void i40e_check_hang_subtask(struct i40e_pf *pf)
5653{
5654 int i, v;
5655
5656 /* If we're down or resetting, just bail */
Akeem G Abodunrinb67a0332015-02-21 06:42:15 +00005657 if (test_bit(__I40E_DOWN, &pf->state) ||
5658 test_bit(__I40E_CONFIG_BUSY, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005659 return;
5660
5661 /* for each VSI/netdev
5662 * for each Tx queue
5663 * set the check flag
5664 * for each q_vector
5665 * force an interrupt
5666 */
Mitch Williams505682c2014-05-20 08:01:37 +00005667 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005668 struct i40e_vsi *vsi = pf->vsi[v];
5669 int armed = 0;
5670
5671 if (!pf->vsi[v] ||
5672 test_bit(__I40E_DOWN, &vsi->state) ||
5673 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
5674 continue;
5675
5676 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005677 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005678 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e152013-09-28 06:00:58 +00005679 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005680 armed++;
5681 }
5682
5683 if (armed) {
5684 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
5685 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
5686 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
Shannon Nelson5d1ff1062014-11-11 20:04:35 +00005687 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK |
5688 I40E_PFINT_DYN_CTL0_ITR_INDX_MASK |
5689 I40E_PFINT_DYN_CTL0_SW_ITR_INDX_ENA_MASK |
5690 I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005691 } else {
5692 u16 vec = vsi->base_vector - 1;
5693 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
Shannon Nelson5d1ff1062014-11-11 20:04:35 +00005694 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK |
5695 I40E_PFINT_DYN_CTLN_ITR_INDX_MASK |
5696 I40E_PFINT_DYN_CTLN_SW_ITR_INDX_ENA_MASK |
5697 I40E_PFINT_DYN_CTLN_SW_ITR_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005698 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
5699 wr32(&vsi->back->hw,
5700 I40E_PFINT_DYN_CTLN(vec), val);
5701 }
5702 i40e_flush(&vsi->back->hw);
5703 }
5704 }
5705}
5706
5707/**
Shannon Nelson21536712014-10-25 10:35:25 +00005708 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005709 * @pf: board private structure
5710 **/
5711static void i40e_watchdog_subtask(struct i40e_pf *pf)
5712{
5713 int i;
5714
5715 /* if interface is down do nothing */
5716 if (test_bit(__I40E_DOWN, &pf->state) ||
5717 test_bit(__I40E_CONFIG_BUSY, &pf->state))
5718 return;
5719
Shannon Nelson21536712014-10-25 10:35:25 +00005720 /* make sure we don't do these things too often */
5721 if (time_before(jiffies, (pf->service_timer_previous +
5722 pf->service_timer_period)))
5723 return;
5724 pf->service_timer_previous = jiffies;
5725
5726 i40e_check_hang_subtask(pf);
5727 i40e_link_event(pf);
5728
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005729 /* Update the stats for active netdevs so the network stack
5730 * can look at updated numbers whenever it cares to
5731 */
Mitch Williams505682c2014-05-20 08:01:37 +00005732 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005733 if (pf->vsi[i] && pf->vsi[i]->netdev)
5734 i40e_update_stats(pf->vsi[i]);
5735
5736 /* Update the stats for the active switching components */
5737 for (i = 0; i < I40E_MAX_VEB; i++)
5738 if (pf->veb[i])
5739 i40e_update_veb_stats(pf->veb[i]);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005740
5741 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005742}
5743
5744/**
5745 * i40e_reset_subtask - Set up for resetting the device and driver
5746 * @pf: board private structure
5747 **/
5748static void i40e_reset_subtask(struct i40e_pf *pf)
5749{
5750 u32 reset_flags = 0;
5751
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005752 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005753 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
5754 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
5755 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
5756 }
5757 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
5758 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
5759 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5760 }
5761 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
5762 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
5763 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
5764 }
5765 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
5766 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
5767 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
5768 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005769 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
5770 reset_flags |= (1 << __I40E_DOWN_REQUESTED);
5771 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
5772 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005773
5774 /* If there's a recovery already waiting, it takes
5775 * precedence before starting a new reset sequence.
5776 */
5777 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
5778 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005779 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005780 }
5781
5782 /* If we're already down or resetting, just bail */
5783 if (reset_flags &&
5784 !test_bit(__I40E_DOWN, &pf->state) &&
5785 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
5786 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005787
5788unlock:
5789 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005790}
5791
5792/**
5793 * i40e_handle_link_event - Handle link event
5794 * @pf: board private structure
5795 * @e: event info posted on ARQ
5796 **/
5797static void i40e_handle_link_event(struct i40e_pf *pf,
5798 struct i40e_arq_event_info *e)
5799{
5800 struct i40e_hw *hw = &pf->hw;
5801 struct i40e_aqc_get_link_status *status =
5802 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005803
5804 /* save off old link status information */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005805 hw->phy.link_info_old = hw->phy.link_info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005806
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00005807 /* Do a new status request to re-enable LSE reporting
5808 * and load new status information into the hw struct
5809 * This completely ignores any state information
5810 * in the ARQ event info, instead choosing to always
5811 * issue the AQ update link status command.
5812 */
5813 i40e_link_event(pf);
5814
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005815 /* check for unqualified module, if link is down */
5816 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
5817 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
5818 (!(status->link_info & I40E_AQ_LINK_UP)))
5819 dev_err(&pf->pdev->dev,
5820 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005821}
5822
5823/**
5824 * i40e_clean_adminq_subtask - Clean the AdminQ rings
5825 * @pf: board private structure
5826 **/
5827static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
5828{
5829 struct i40e_arq_event_info event;
5830 struct i40e_hw *hw = &pf->hw;
5831 u16 pending, i = 0;
5832 i40e_status ret;
5833 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00005834 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005835 u32 val;
5836
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00005837 /* Do not run clean AQ when PF reset fails */
5838 if (test_bit(__I40E_RESET_FAILED, &pf->state))
5839 return;
5840
Shannon Nelson86df2422014-05-20 08:01:35 +00005841 /* check for error indications */
5842 val = rd32(&pf->hw, pf->hw.aq.arq.len);
5843 oldval = val;
5844 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
5845 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
5846 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
5847 }
5848 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
5849 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
5850 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
5851 }
5852 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
5853 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
5854 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
5855 }
5856 if (oldval != val)
5857 wr32(&pf->hw, pf->hw.aq.arq.len, val);
5858
5859 val = rd32(&pf->hw, pf->hw.aq.asq.len);
5860 oldval = val;
5861 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
5862 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
5863 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
5864 }
5865 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
5866 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
5867 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
5868 }
5869 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
5870 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
5871 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
5872 }
5873 if (oldval != val)
5874 wr32(&pf->hw, pf->hw.aq.asq.len, val);
5875
Mitch Williams1001dc32014-11-11 20:02:19 +00005876 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
5877 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005878 if (!event.msg_buf)
5879 return;
5880
5881 do {
5882 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00005883 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005884 break;
Mitch Williams56497972014-06-04 08:45:18 +00005885 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005886 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
5887 break;
5888 }
5889
5890 opcode = le16_to_cpu(event.desc.opcode);
5891 switch (opcode) {
5892
5893 case i40e_aqc_opc_get_link_status:
5894 i40e_handle_link_event(pf, &event);
5895 break;
5896 case i40e_aqc_opc_send_msg_to_pf:
5897 ret = i40e_vc_process_vf_msg(pf,
5898 le16_to_cpu(event.desc.retval),
5899 le32_to_cpu(event.desc.cookie_high),
5900 le32_to_cpu(event.desc.cookie_low),
5901 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00005902 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005903 break;
5904 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005905 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005906#ifdef CONFIG_I40E_DCB
5907 rtnl_lock();
5908 ret = i40e_handle_lldp_event(pf, &event);
5909 rtnl_unlock();
5910#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005911 break;
5912 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005913 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005914 i40e_handle_lan_overflow_event(pf, &event);
5915 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00005916 case i40e_aqc_opc_send_msg_to_peer:
5917 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
5918 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07005919 case i40e_aqc_opc_nvm_erase:
5920 case i40e_aqc_opc_nvm_update:
5921 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "ARQ NVM operation completed\n");
5922 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005923 default:
5924 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00005925 "ARQ Error: Unknown event 0x%04x received\n",
5926 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005927 break;
5928 }
5929 } while (pending && (i++ < pf->adminq_work_limit));
5930
5931 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
5932 /* re-enable Admin queue interrupt cause */
5933 val = rd32(hw, I40E_PFINT_ICR0_ENA);
5934 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
5935 wr32(hw, I40E_PFINT_ICR0_ENA, val);
5936 i40e_flush(hw);
5937
5938 kfree(event.msg_buf);
5939}
5940
5941/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005942 * i40e_verify_eeprom - make sure eeprom is good to use
5943 * @pf: board private structure
5944 **/
5945static void i40e_verify_eeprom(struct i40e_pf *pf)
5946{
5947 int err;
5948
5949 err = i40e_diag_eeprom_test(&pf->hw);
5950 if (err) {
5951 /* retry in case of garbage read */
5952 err = i40e_diag_eeprom_test(&pf->hw);
5953 if (err) {
5954 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
5955 err);
5956 set_bit(__I40E_BAD_EEPROM, &pf->state);
5957 }
5958 }
5959
5960 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
5961 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
5962 clear_bit(__I40E_BAD_EEPROM, &pf->state);
5963 }
5964}
5965
5966/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00005967 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005968 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00005969 *
5970 * enable switch loop back or die - no point in a return value
5971 **/
5972static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
5973{
5974 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5975 struct i40e_vsi_context ctxt;
5976 int aq_ret;
5977
5978 ctxt.seid = pf->main_vsi_seid;
5979 ctxt.pf_num = pf->hw.pf_id;
5980 ctxt.vf_num = 0;
5981 aq_ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
5982 if (aq_ret) {
5983 dev_info(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005984 "%s couldn't get PF vsi config, err %d, aq_err %d\n",
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00005985 __func__, aq_ret, pf->hw.aq.asq_last_status);
5986 return;
5987 }
5988 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
5989 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5990 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5991
5992 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5993 if (aq_ret) {
5994 dev_info(&pf->pdev->dev,
5995 "%s: update vsi switch failed, aq_err=%d\n",
5996 __func__, vsi->back->hw.aq.asq_last_status);
5997 }
5998}
5999
6000/**
6001 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006002 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006003 *
6004 * disable switch loop back or die - no point in a return value
6005 **/
6006static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6007{
6008 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6009 struct i40e_vsi_context ctxt;
6010 int aq_ret;
6011
6012 ctxt.seid = pf->main_vsi_seid;
6013 ctxt.pf_num = pf->hw.pf_id;
6014 ctxt.vf_num = 0;
6015 aq_ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6016 if (aq_ret) {
6017 dev_info(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006018 "%s couldn't get PF vsi config, err %d, aq_err %d\n",
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006019 __func__, aq_ret, pf->hw.aq.asq_last_status);
6020 return;
6021 }
6022 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6023 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6024 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6025
6026 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6027 if (aq_ret) {
6028 dev_info(&pf->pdev->dev,
6029 "%s: update vsi switch failed, aq_err=%d\n",
6030 __func__, vsi->back->hw.aq.asq_last_status);
6031 }
6032}
6033
6034/**
Neerav Parikh51616012015-02-06 08:52:14 +00006035 * i40e_config_bridge_mode - Configure the HW bridge mode
6036 * @veb: pointer to the bridge instance
6037 *
6038 * Configure the loop back mode for the LAN VSI that is downlink to the
6039 * specified HW bridge instance. It is expected this function is called
6040 * when a new HW bridge is instantiated.
6041 **/
6042static void i40e_config_bridge_mode(struct i40e_veb *veb)
6043{
6044 struct i40e_pf *pf = veb->pf;
6045
6046 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6047 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
6048 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6049 i40e_disable_pf_switch_lb(pf);
6050 else
6051 i40e_enable_pf_switch_lb(pf);
6052}
6053
6054/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006055 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6056 * @veb: pointer to the VEB instance
6057 *
6058 * This is a recursive function that first builds the attached VSIs then
6059 * recurses in to build the next layer of VEB. We track the connections
6060 * through our own index numbers because the seid's from the HW could
6061 * change across the reset.
6062 **/
6063static int i40e_reconstitute_veb(struct i40e_veb *veb)
6064{
6065 struct i40e_vsi *ctl_vsi = NULL;
6066 struct i40e_pf *pf = veb->pf;
6067 int v, veb_idx;
6068 int ret;
6069
6070 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006071 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006072 if (pf->vsi[v] &&
6073 pf->vsi[v]->veb_idx == veb->idx &&
6074 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6075 ctl_vsi = pf->vsi[v];
6076 break;
6077 }
6078 }
6079 if (!ctl_vsi) {
6080 dev_info(&pf->pdev->dev,
6081 "missing owner VSI for veb_idx %d\n", veb->idx);
6082 ret = -ENOENT;
6083 goto end_reconstitute;
6084 }
6085 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6086 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6087 ret = i40e_add_vsi(ctl_vsi);
6088 if (ret) {
6089 dev_info(&pf->pdev->dev,
6090 "rebuild of owner VSI failed: %d\n", ret);
6091 goto end_reconstitute;
6092 }
6093 i40e_vsi_reset_stats(ctl_vsi);
6094
6095 /* create the VEB in the switch and move the VSI onto the VEB */
6096 ret = i40e_add_veb(veb, ctl_vsi);
6097 if (ret)
6098 goto end_reconstitute;
6099
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006100 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6101 veb->bridge_mode = BRIDGE_MODE_VEB;
6102 else
6103 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006104 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006105
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006106 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006107 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006108 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6109 continue;
6110
6111 if (pf->vsi[v]->veb_idx == veb->idx) {
6112 struct i40e_vsi *vsi = pf->vsi[v];
6113 vsi->uplink_seid = veb->seid;
6114 ret = i40e_add_vsi(vsi);
6115 if (ret) {
6116 dev_info(&pf->pdev->dev,
6117 "rebuild of vsi_idx %d failed: %d\n",
6118 v, ret);
6119 goto end_reconstitute;
6120 }
6121 i40e_vsi_reset_stats(vsi);
6122 }
6123 }
6124
6125 /* create any VEBs attached to this VEB - RECURSION */
6126 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6127 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6128 pf->veb[veb_idx]->uplink_seid = veb->seid;
6129 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6130 if (ret)
6131 break;
6132 }
6133 }
6134
6135end_reconstitute:
6136 return ret;
6137}
6138
6139/**
6140 * i40e_get_capabilities - get info about the HW
6141 * @pf: the PF struct
6142 **/
6143static int i40e_get_capabilities(struct i40e_pf *pf)
6144{
6145 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6146 u16 data_size;
6147 int buf_len;
6148 int err;
6149
6150 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6151 do {
6152 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6153 if (!cap_buf)
6154 return -ENOMEM;
6155
6156 /* this loads the data into the hw struct for us */
6157 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6158 &data_size,
6159 i40e_aqc_opc_list_func_capabilities,
6160 NULL);
6161 /* data loaded, buffer no longer needed */
6162 kfree(cap_buf);
6163
6164 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6165 /* retry with a larger buffer */
6166 buf_len = data_size;
6167 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6168 dev_info(&pf->pdev->dev,
6169 "capability discovery failed: aq=%d\n",
6170 pf->hw.aq.asq_last_status);
6171 return -ENODEV;
6172 }
6173 } while (err);
6174
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00006175 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
6176 (pf->hw.aq.fw_maj_ver < 2)) {
6177 pf->hw.func_caps.num_msix_vectors++;
6178 pf->hw.func_caps.num_msix_vectors_vf++;
6179 }
6180
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006181 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6182 dev_info(&pf->pdev->dev,
6183 "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",
6184 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6185 pf->hw.func_caps.num_msix_vectors,
6186 pf->hw.func_caps.num_msix_vectors_vf,
6187 pf->hw.func_caps.fd_filters_guaranteed,
6188 pf->hw.func_caps.fd_filters_best_effort,
6189 pf->hw.func_caps.num_tx_qp,
6190 pf->hw.func_caps.num_vsis);
6191
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006192#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6193 + pf->hw.func_caps.num_vfs)
6194 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6195 dev_info(&pf->pdev->dev,
6196 "got num_vsis %d, setting num_vsis to %d\n",
6197 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6198 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6199 }
6200
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006201 return 0;
6202}
6203
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006204static int i40e_vsi_clear(struct i40e_vsi *vsi);
6205
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006206/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006207 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006208 * @pf: board private structure
6209 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006210static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006211{
6212 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006213 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006214
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006215 /* quick workaround for an NVM issue that leaves a critical register
6216 * uninitialized
6217 */
6218 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6219 static const u32 hkey[] = {
6220 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6221 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6222 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6223 0x95b3a76d};
6224
6225 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6226 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6227 }
6228
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006229 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006230 return;
6231
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006232 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006233 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00006234 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006235 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006236 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006237 break;
6238 }
6239 }
6240
6241 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006242 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006243 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6244 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006245 if (!vsi) {
6246 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006247 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6248 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006249 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006250 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006251
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006252 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006253}
6254
6255/**
6256 * i40e_fdir_teardown - release the Flow Director resources
6257 * @pf: board private structure
6258 **/
6259static void i40e_fdir_teardown(struct i40e_pf *pf)
6260{
6261 int i;
6262
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006263 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00006264 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006265 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
6266 i40e_vsi_release(pf->vsi[i]);
6267 break;
6268 }
6269 }
6270}
6271
6272/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006273 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006274 * @pf: board private structure
6275 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006276 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006277 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006278static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006279{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006280 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006281 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006282 u32 v;
6283
6284 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6285 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006286 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006287
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006288 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006289
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006290 /* quiesce the VSIs and their queues that are not already DOWN */
6291 i40e_pf_quiesce_all_vsi(pf);
6292
Mitch Williams505682c2014-05-20 08:01:37 +00006293 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006294 if (pf->vsi[v])
6295 pf->vsi[v]->seid = 0;
6296 }
6297
6298 i40e_shutdown_adminq(&pf->hw);
6299
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006300 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006301 if (hw->hmc.hmc_obj) {
6302 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006303 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006304 dev_warn(&pf->pdev->dev,
6305 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006306 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006307}
6308
6309/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006310 * i40e_send_version - update firmware with driver version
6311 * @pf: PF struct
6312 */
6313static void i40e_send_version(struct i40e_pf *pf)
6314{
6315 struct i40e_driver_version dv;
6316
6317 dv.major_version = DRV_VERSION_MAJOR;
6318 dv.minor_version = DRV_VERSION_MINOR;
6319 dv.build_version = DRV_VERSION_BUILD;
6320 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006321 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006322 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6323}
6324
6325/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006326 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006327 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006328 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006329 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006330static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006331{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006332 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006333 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006334 i40e_status ret;
6335 u32 v;
6336
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006337 /* Now we wait for GRST to settle out.
6338 * We don't have to delete the VEBs or VSIs from the hw switch
6339 * because the reset will make them disappear.
6340 */
6341 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006342 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006343 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006344 set_bit(__I40E_RESET_FAILED, &pf->state);
6345 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006346 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006347 pf->pfr_count++;
6348
6349 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006350 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006351 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006352
6353 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6354 ret = i40e_init_adminq(&pf->hw);
6355 if (ret) {
6356 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006357 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006358 }
6359
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006360 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006361 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006362 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006363
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006364 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006365 ret = i40e_get_capabilities(pf);
6366 if (ret) {
6367 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
6368 ret);
6369 goto end_core_reset;
6370 }
6371
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006372 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6373 hw->func_caps.num_rx_qp,
6374 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6375 if (ret) {
6376 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6377 goto end_core_reset;
6378 }
6379 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6380 if (ret) {
6381 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6382 goto end_core_reset;
6383 }
6384
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006385#ifdef CONFIG_I40E_DCB
6386 ret = i40e_init_pf_dcb(pf);
6387 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006388 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6389 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6390 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006391 }
6392#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006393#ifdef I40E_FCOE
6394 ret = i40e_init_pf_fcoe(pf);
6395 if (ret)
6396 dev_info(&pf->pdev->dev, "init_pf_fcoe failed: %d\n", ret);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006397
Vasu Dev38e00432014-08-01 13:27:03 -07006398#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006399 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006400 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006401 if (ret)
6402 goto end_core_reset;
6403
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006404 /* driver is only interested in link up/down and module qualification
6405 * reports from firmware
6406 */
6407 ret = i40e_aq_set_phy_int_mask(&pf->hw,
6408 I40E_AQ_EVENT_LINK_UPDOWN |
6409 I40E_AQ_EVENT_MODULE_QUAL_FAIL, NULL);
6410 if (ret)
6411 dev_info(&pf->pdev->dev, "set phy mask fail, aq_err %d\n", ret);
6412
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006413 /* make sure our flow control settings are restored */
6414 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6415 if (ret)
6416 dev_info(&pf->pdev->dev, "set fc fail, aq_err %d\n", ret);
6417
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006418 /* Rebuild the VSIs and VEBs that existed before reset.
6419 * They are still in our local switch element arrays, so only
6420 * need to rebuild the switch model in the HW.
6421 *
6422 * If there were VEBs but the reconstitution failed, we'll try
6423 * try to recover minimal use by getting the basic PF VSI working.
6424 */
6425 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006426 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006427 /* find the one VEB connected to the MAC, and find orphans */
6428 for (v = 0; v < I40E_MAX_VEB; v++) {
6429 if (!pf->veb[v])
6430 continue;
6431
6432 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6433 pf->veb[v]->uplink_seid == 0) {
6434 ret = i40e_reconstitute_veb(pf->veb[v]);
6435
6436 if (!ret)
6437 continue;
6438
6439 /* If Main VEB failed, we're in deep doodoo,
6440 * so give up rebuilding the switch and set up
6441 * for minimal rebuild of PF VSI.
6442 * If orphan failed, we'll report the error
6443 * but try to keep going.
6444 */
6445 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6446 dev_info(&pf->pdev->dev,
6447 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6448 ret);
6449 pf->vsi[pf->lan_vsi]->uplink_seid
6450 = pf->mac_seid;
6451 break;
6452 } else if (pf->veb[v]->uplink_seid == 0) {
6453 dev_info(&pf->pdev->dev,
6454 "rebuild of orphan VEB failed: %d\n",
6455 ret);
6456 }
6457 }
6458 }
6459 }
6460
6461 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006462 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006463 /* no VEB, so rebuild only the Main VSI */
6464 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6465 if (ret) {
6466 dev_info(&pf->pdev->dev,
6467 "rebuild of Main VSI failed: %d\n", ret);
6468 goto end_core_reset;
6469 }
6470 }
6471
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006472 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
6473 (pf->hw.aq.fw_maj_ver < 4)) {
6474 msleep(75);
6475 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6476 if (ret)
6477 dev_info(&pf->pdev->dev, "link restart failed, aq_err=%d\n",
6478 pf->hw.aq.asq_last_status);
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006479 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006480 /* reinit the misc interrupt */
6481 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6482 ret = i40e_setup_misc_vector(pf);
6483
6484 /* restart the VSIs that were rebuilt and running before the reset */
6485 i40e_pf_unquiesce_all_vsi(pf);
6486
Mitch Williams69f64b22014-02-13 03:48:46 -08006487 if (pf->num_alloc_vfs) {
6488 for (v = 0; v < pf->num_alloc_vfs; v++)
6489 i40e_reset_vf(&pf->vf[v], true);
6490 }
6491
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006492 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006493 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006494
6495end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006496 clear_bit(__I40E_RESET_FAILED, &pf->state);
6497clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006498 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6499}
6500
6501/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006502 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006503 * @pf: board private structure
6504 *
6505 * Close up the VFs and other things in prep for a Core Reset,
6506 * then get ready to rebuild the world.
6507 **/
6508static void i40e_handle_reset_warning(struct i40e_pf *pf)
6509{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006510 i40e_prep_for_reset(pf);
6511 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006512}
6513
6514/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006515 * i40e_handle_mdd_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006516 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006517 *
6518 * Called from the MDD irq handler to identify possibly malicious vfs
6519 **/
6520static void i40e_handle_mdd_event(struct i40e_pf *pf)
6521{
6522 struct i40e_hw *hw = &pf->hw;
6523 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00006524 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006525 struct i40e_vf *vf;
6526 u32 reg;
6527 int i;
6528
6529 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
6530 return;
6531
6532 /* find what triggered the MDD event */
6533 reg = rd32(hw, I40E_GL_MDET_TX);
6534 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006535 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
6536 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006537 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006538 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006539 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006540 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006541 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
6542 I40E_GL_MDET_TX_QUEUE_SHIFT) -
6543 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006544 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006545 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006546 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006547 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
6548 mdd_detected = true;
6549 }
6550 reg = rd32(hw, I40E_GL_MDET_RX);
6551 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006552 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
6553 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006554 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006555 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006556 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
6557 I40E_GL_MDET_RX_QUEUE_SHIFT) -
6558 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006559 if (netif_msg_rx_err(pf))
6560 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
6561 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006562 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
6563 mdd_detected = true;
6564 }
6565
Neerav Parikhdf430b12014-06-04 01:23:15 +00006566 if (mdd_detected) {
6567 reg = rd32(hw, I40E_PF_MDET_TX);
6568 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
6569 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006570 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00006571 pf_mdd_detected = true;
6572 }
6573 reg = rd32(hw, I40E_PF_MDET_RX);
6574 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
6575 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006576 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00006577 pf_mdd_detected = true;
6578 }
6579 /* Queue belongs to the PF, initiate a reset */
6580 if (pf_mdd_detected) {
6581 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6582 i40e_service_event_schedule(pf);
6583 }
6584 }
6585
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006586 /* see if one of the VFs needs its hand slapped */
6587 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
6588 vf = &(pf->vf[i]);
6589 reg = rd32(hw, I40E_VP_MDET_TX(i));
6590 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
6591 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
6592 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006593 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
6594 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006595 }
6596
6597 reg = rd32(hw, I40E_VP_MDET_RX(i));
6598 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
6599 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
6600 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006601 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
6602 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006603 }
6604
6605 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
6606 dev_info(&pf->pdev->dev,
6607 "Too many MDD events on VF %d, disabled\n", i);
6608 dev_info(&pf->pdev->dev,
6609 "Use PF Control I/F to re-enable the VF\n");
6610 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
6611 }
6612 }
6613
6614 /* re-enable mdd interrupt cause */
6615 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
6616 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
6617 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
6618 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
6619 i40e_flush(hw);
6620}
6621
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006622#ifdef CONFIG_I40E_VXLAN
6623/**
6624 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
6625 * @pf: board private structure
6626 **/
6627static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
6628{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006629 struct i40e_hw *hw = &pf->hw;
6630 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006631 __be16 port;
6632 int i;
6633
6634 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
6635 return;
6636
6637 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
6638
6639 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6640 if (pf->pending_vxlan_bitmap & (1 << i)) {
6641 pf->pending_vxlan_bitmap &= ~(1 << i);
6642 port = pf->vxlan_ports[i];
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07006643 if (port)
6644 ret = i40e_aq_add_udp_tunnel(hw, ntohs(port),
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006645 I40E_AQC_TUNNEL_TYPE_VXLAN,
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07006646 NULL, NULL);
6647 else
6648 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006649
6650 if (ret) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07006651 dev_info(&pf->pdev->dev,
6652 "%s vxlan port %d, index %d failed, err %d, aq_err %d\n",
6653 port ? "add" : "delete",
6654 ntohs(port), i, ret,
6655 pf->hw.aq.asq_last_status);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006656 pf->vxlan_ports[i] = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006657 }
6658 }
6659 }
6660}
6661
6662#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006663/**
6664 * i40e_service_task - Run the driver's async subtasks
6665 * @work: pointer to work_struct containing our data
6666 **/
6667static void i40e_service_task(struct work_struct *work)
6668{
6669 struct i40e_pf *pf = container_of(work,
6670 struct i40e_pf,
6671 service_task);
6672 unsigned long start_time = jiffies;
6673
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00006674 /* don't bother with service tasks if a reset is in progress */
6675 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6676 i40e_service_event_complete(pf);
6677 return;
6678 }
6679
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006680 i40e_reset_subtask(pf);
6681 i40e_handle_mdd_event(pf);
6682 i40e_vc_process_vflr_event(pf);
6683 i40e_watchdog_subtask(pf);
6684 i40e_fdir_reinit_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006685 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006686#ifdef CONFIG_I40E_VXLAN
6687 i40e_sync_vxlan_filters_subtask(pf);
6688#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006689 i40e_clean_adminq_subtask(pf);
6690
6691 i40e_service_event_complete(pf);
6692
6693 /* If the tasks have taken longer than one timer cycle or there
6694 * is more work to be done, reschedule the service task now
6695 * rather than wait for the timer to tick again.
6696 */
6697 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
6698 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
6699 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
6700 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
6701 i40e_service_event_schedule(pf);
6702}
6703
6704/**
6705 * i40e_service_timer - timer callback
6706 * @data: pointer to PF struct
6707 **/
6708static void i40e_service_timer(unsigned long data)
6709{
6710 struct i40e_pf *pf = (struct i40e_pf *)data;
6711
6712 mod_timer(&pf->service_timer,
6713 round_jiffies(jiffies + pf->service_timer_period));
6714 i40e_service_event_schedule(pf);
6715}
6716
6717/**
6718 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
6719 * @vsi: the VSI being configured
6720 **/
6721static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
6722{
6723 struct i40e_pf *pf = vsi->back;
6724
6725 switch (vsi->type) {
6726 case I40E_VSI_MAIN:
6727 vsi->alloc_queue_pairs = pf->num_lan_qps;
6728 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6729 I40E_REQ_DESCRIPTOR_MULTIPLE);
6730 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6731 vsi->num_q_vectors = pf->num_lan_msix;
6732 else
6733 vsi->num_q_vectors = 1;
6734
6735 break;
6736
6737 case I40E_VSI_FDIR:
6738 vsi->alloc_queue_pairs = 1;
6739 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
6740 I40E_REQ_DESCRIPTOR_MULTIPLE);
6741 vsi->num_q_vectors = 1;
6742 break;
6743
6744 case I40E_VSI_VMDQ2:
6745 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
6746 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6747 I40E_REQ_DESCRIPTOR_MULTIPLE);
6748 vsi->num_q_vectors = pf->num_vmdq_msix;
6749 break;
6750
6751 case I40E_VSI_SRIOV:
6752 vsi->alloc_queue_pairs = pf->num_vf_qps;
6753 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6754 I40E_REQ_DESCRIPTOR_MULTIPLE);
6755 break;
6756
Vasu Dev38e00432014-08-01 13:27:03 -07006757#ifdef I40E_FCOE
6758 case I40E_VSI_FCOE:
6759 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
6760 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
6761 I40E_REQ_DESCRIPTOR_MULTIPLE);
6762 vsi->num_q_vectors = pf->num_fcoe_msix;
6763 break;
6764
6765#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006766 default:
6767 WARN_ON(1);
6768 return -ENODATA;
6769 }
6770
6771 return 0;
6772}
6773
6774/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006775 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
6776 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006777 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006778 *
6779 * On error: returns error code (negative)
6780 * On success: returns 0
6781 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006782static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006783{
6784 int size;
6785 int ret = 0;
6786
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00006787 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006788 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
6789 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
6790 if (!vsi->tx_rings)
6791 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006792 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
6793
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006794 if (alloc_qvectors) {
6795 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00006796 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006797 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
6798 if (!vsi->q_vectors) {
6799 ret = -ENOMEM;
6800 goto err_vectors;
6801 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006802 }
6803 return ret;
6804
6805err_vectors:
6806 kfree(vsi->tx_rings);
6807 return ret;
6808}
6809
6810/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006811 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
6812 * @pf: board private structure
6813 * @type: type of VSI
6814 *
6815 * On error: returns error code (negative)
6816 * On success: returns vsi index in PF (positive)
6817 **/
6818static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
6819{
6820 int ret = -ENODEV;
6821 struct i40e_vsi *vsi;
6822 int vsi_idx;
6823 int i;
6824
6825 /* Need to protect the allocation of the VSIs at the PF level */
6826 mutex_lock(&pf->switch_mutex);
6827
6828 /* VSI list may be fragmented if VSI creation/destruction has
6829 * been happening. We can afford to do a quick scan to look
6830 * for any free VSIs in the list.
6831 *
6832 * find next empty vsi slot, looping back around if necessary
6833 */
6834 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00006835 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006836 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00006837 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006838 i = 0;
6839 while (i < pf->next_vsi && pf->vsi[i])
6840 i++;
6841 }
6842
Mitch Williams505682c2014-05-20 08:01:37 +00006843 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006844 vsi_idx = i; /* Found one! */
6845 } else {
6846 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00006847 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006848 }
6849 pf->next_vsi = ++i;
6850
6851 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
6852 if (!vsi) {
6853 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00006854 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006855 }
6856 vsi->type = type;
6857 vsi->back = pf;
6858 set_bit(__I40E_DOWN, &vsi->state);
6859 vsi->flags = 0;
6860 vsi->idx = vsi_idx;
6861 vsi->rx_itr_setting = pf->rx_itr_default;
6862 vsi->tx_itr_setting = pf->tx_itr_default;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00006863 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
6864 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006865 vsi->netdev_registered = false;
6866 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
6867 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00006868 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006869
Alexander Duyck9f65e152013-09-28 06:00:58 +00006870 ret = i40e_set_num_rings_in_vsi(vsi);
6871 if (ret)
6872 goto err_rings;
6873
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006874 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006875 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00006876 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00006877
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006878 /* Setup default MSIX irq handler for VSI */
6879 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
6880
6881 pf->vsi[vsi_idx] = vsi;
6882 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00006883 goto unlock_pf;
6884
Alexander Duyck9f65e152013-09-28 06:00:58 +00006885err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00006886 pf->next_vsi = i - 1;
6887 kfree(vsi);
6888unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006889 mutex_unlock(&pf->switch_mutex);
6890 return ret;
6891}
6892
6893/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006894 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
6895 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006896 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006897 *
6898 * On error: returns error code (negative)
6899 * On success: returns 0
6900 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006901static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006902{
6903 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006904 if (free_qvectors) {
6905 kfree(vsi->q_vectors);
6906 vsi->q_vectors = NULL;
6907 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006908 kfree(vsi->tx_rings);
6909 vsi->tx_rings = NULL;
6910 vsi->rx_rings = NULL;
6911}
6912
6913/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006914 * i40e_vsi_clear - Deallocate the VSI provided
6915 * @vsi: the VSI being un-configured
6916 **/
6917static int i40e_vsi_clear(struct i40e_vsi *vsi)
6918{
6919 struct i40e_pf *pf;
6920
6921 if (!vsi)
6922 return 0;
6923
6924 if (!vsi->back)
6925 goto free_vsi;
6926 pf = vsi->back;
6927
6928 mutex_lock(&pf->switch_mutex);
6929 if (!pf->vsi[vsi->idx]) {
6930 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
6931 vsi->idx, vsi->idx, vsi, vsi->type);
6932 goto unlock_vsi;
6933 }
6934
6935 if (pf->vsi[vsi->idx] != vsi) {
6936 dev_err(&pf->pdev->dev,
6937 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
6938 pf->vsi[vsi->idx]->idx,
6939 pf->vsi[vsi->idx],
6940 pf->vsi[vsi->idx]->type,
6941 vsi->idx, vsi, vsi->type);
6942 goto unlock_vsi;
6943 }
6944
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006945 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006946 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
6947 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
6948
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006949 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00006950
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006951 pf->vsi[vsi->idx] = NULL;
6952 if (vsi->idx < pf->next_vsi)
6953 pf->next_vsi = vsi->idx;
6954
6955unlock_vsi:
6956 mutex_unlock(&pf->switch_mutex);
6957free_vsi:
6958 kfree(vsi);
6959
6960 return 0;
6961}
6962
6963/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00006964 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
6965 * @vsi: the VSI being cleaned
6966 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006967static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00006968{
6969 int i;
6970
Greg Rose8e9dca52013-12-18 13:45:53 +00006971 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00006972 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00006973 kfree_rcu(vsi->tx_rings[i], rcu);
6974 vsi->tx_rings[i] = NULL;
6975 vsi->rx_rings[i] = NULL;
6976 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006977 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00006978}
6979
6980/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006981 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
6982 * @vsi: the VSI being configured
6983 **/
6984static int i40e_alloc_rings(struct i40e_vsi *vsi)
6985{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00006986 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006987 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006988 int i;
6989
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006990 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00006991 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00006992 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00006993 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
6994 if (!tx_ring)
6995 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006996
6997 tx_ring->queue_index = i;
6998 tx_ring->reg_idx = vsi->base_queue + i;
6999 tx_ring->ring_active = false;
7000 tx_ring->vsi = vsi;
7001 tx_ring->netdev = vsi->netdev;
7002 tx_ring->dev = &pf->pdev->dev;
7003 tx_ring->count = vsi->num_desc;
7004 tx_ring->size = 0;
7005 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007006 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007007
Alexander Duyck9f65e152013-09-28 06:00:58 +00007008 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007009 rx_ring->queue_index = i;
7010 rx_ring->reg_idx = vsi->base_queue + i;
7011 rx_ring->ring_active = false;
7012 rx_ring->vsi = vsi;
7013 rx_ring->netdev = vsi->netdev;
7014 rx_ring->dev = &pf->pdev->dev;
7015 rx_ring->count = vsi->num_desc;
7016 rx_ring->size = 0;
7017 rx_ring->dcb_tc = 0;
7018 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
7019 set_ring_16byte_desc_enabled(rx_ring);
7020 else
7021 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e152013-09-28 06:00:58 +00007022 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007023 }
7024
7025 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007026
7027err_out:
7028 i40e_vsi_clear_rings(vsi);
7029 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007030}
7031
7032/**
7033 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7034 * @pf: board private structure
7035 * @vectors: the number of MSI-X vectors to request
7036 *
7037 * Returns the number of vectors reserved, or error
7038 **/
7039static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7040{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007041 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7042 I40E_MIN_MSIX, vectors);
7043 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007044 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007045 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007046 vectors = 0;
7047 }
7048
7049 return vectors;
7050}
7051
7052/**
7053 * i40e_init_msix - Setup the MSIX capability
7054 * @pf: board private structure
7055 *
7056 * Work with the OS to set up the MSIX vectors needed.
7057 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007058 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007059 **/
7060static int i40e_init_msix(struct i40e_pf *pf)
7061{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007062 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007063 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007064 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007065 int v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007066
7067 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7068 return -ENODEV;
7069
7070 /* The number of vectors we'll request will be comprised of:
7071 * - Add 1 for "other" cause for Admin Queue events, etc.
7072 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007073 * - Queues being used for RSS.
7074 * We don't need as many as max_rss_size vectors.
7075 * use rss_size instead in the calculation since that
7076 * is governed by number of cpus in the system.
7077 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007078 * - The number of VMDq pairs
Vasu Dev38e00432014-08-01 13:27:03 -07007079#ifdef I40E_FCOE
7080 * - The number of FCOE qps.
7081#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007082 * Once we count this up, try the request.
7083 *
7084 * If we can't get what we want, we'll simplify to nearly nothing
7085 * and try again. If that still fails, we punt.
7086 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007087 vectors_left = hw->func_caps.num_msix_vectors;
7088 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007089
Shannon Nelson1e200e42015-02-27 09:15:24 +00007090 /* reserve one vector for miscellaneous handler */
7091 if (vectors_left) {
7092 v_budget++;
7093 vectors_left--;
7094 }
7095
7096 /* reserve vectors for the main PF traffic queues */
7097 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7098 vectors_left -= pf->num_lan_msix;
7099 v_budget += pf->num_lan_msix;
7100
7101 /* reserve one vector for sideband flow director */
7102 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7103 if (vectors_left) {
7104 v_budget++;
7105 vectors_left--;
7106 } else {
7107 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7108 }
7109 }
John W Linville83840e42015-01-14 03:06:28 +00007110
Vasu Dev38e00432014-08-01 13:27:03 -07007111#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007112 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007113 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007114 if (!vectors_left)
7115 pf->num_fcoe_msix = 0;
7116 else if (vectors_left >= pf->num_fcoe_qps)
7117 pf->num_fcoe_msix = pf->num_fcoe_qps;
7118 else
7119 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007120 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007121 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007122 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007123
Vasu Dev38e00432014-08-01 13:27:03 -07007124#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007125 /* any vectors left over go for VMDq support */
7126 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7127 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7128 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7129
7130 /* if we're short on vectors for what's desired, we limit
7131 * the queues per vmdq. If this is still more than are
7132 * available, the user will need to change the number of
7133 * queues/vectors used by the PF later with the ethtool
7134 * channels command
7135 */
7136 if (vmdq_vecs < vmdq_vecs_wanted)
7137 pf->num_vmdq_qps = 1;
7138 pf->num_vmdq_msix = pf->num_vmdq_qps;
7139
7140 v_budget += vmdq_vecs;
7141 vectors_left -= vmdq_vecs;
7142 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007143
7144 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7145 GFP_KERNEL);
7146 if (!pf->msix_entries)
7147 return -ENOMEM;
7148
7149 for (i = 0; i < v_budget; i++)
7150 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007151 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007152
Shannon Nelson3b444392015-02-26 16:15:57 +00007153 if (v_actual != v_budget) {
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007154 /* If we have limited resources, we will start with no vectors
7155 * for the special features and then allocate vectors to some
7156 * of these features based on the policy and at the end disable
7157 * the features that did not get any vectors.
7158 */
Vasu Dev38e00432014-08-01 13:27:03 -07007159#ifdef I40E_FCOE
7160 pf->num_fcoe_qps = 0;
7161 pf->num_fcoe_msix = 0;
7162#endif
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007163 pf->num_vmdq_msix = 0;
7164 }
7165
Shannon Nelson3b444392015-02-26 16:15:57 +00007166 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007167 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7168 kfree(pf->msix_entries);
7169 pf->msix_entries = NULL;
7170 return -ENODEV;
7171
Shannon Nelson3b444392015-02-26 16:15:57 +00007172 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007173 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007174 pf->num_vmdq_vsis = 0;
7175 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007176 pf->num_lan_qps = 1;
7177 pf->num_lan_msix = 1;
7178
Shannon Nelson3b444392015-02-26 16:15:57 +00007179 } else if (v_actual != v_budget) {
7180 int vec;
7181
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007182 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007183 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007184
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007185 /* Scale vector usage down */
7186 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007187 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007188 pf->num_vmdq_qps = 1;
7189 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007190
7191 /* partition out the remaining vectors */
7192 switch (vec) {
7193 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007194 pf->num_lan_msix = 1;
7195 break;
7196 case 3:
Vasu Dev38e00432014-08-01 13:27:03 -07007197#ifdef I40E_FCOE
7198 /* give one vector to FCoE */
7199 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7200 pf->num_lan_msix = 1;
7201 pf->num_fcoe_msix = 1;
7202 }
7203#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007204 pf->num_lan_msix = 2;
Vasu Dev38e00432014-08-01 13:27:03 -07007205#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007206 break;
7207 default:
Vasu Dev38e00432014-08-01 13:27:03 -07007208#ifdef I40E_FCOE
7209 /* give one vector to FCoE */
7210 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7211 pf->num_fcoe_msix = 1;
7212 vec--;
7213 }
7214#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007215 /* give the rest to the PF */
7216 pf->num_lan_msix = min_t(int, vec, pf->num_lan_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007217 break;
7218 }
7219 }
7220
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007221 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7222 (pf->num_vmdq_msix == 0)) {
7223 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7224 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7225 }
Vasu Dev38e00432014-08-01 13:27:03 -07007226#ifdef I40E_FCOE
7227
7228 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7229 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7230 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7231 }
7232#endif
Shannon Nelson3b444392015-02-26 16:15:57 +00007233 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007234}
7235
7236/**
Greg Rose90e04072014-03-06 08:59:57 +00007237 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007238 * @vsi: the VSI being configured
7239 * @v_idx: index of the vector in the vsi struct
7240 *
7241 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7242 **/
Greg Rose90e04072014-03-06 08:59:57 +00007243static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00007244{
7245 struct i40e_q_vector *q_vector;
7246
7247 /* allocate q_vector */
7248 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7249 if (!q_vector)
7250 return -ENOMEM;
7251
7252 q_vector->vsi = vsi;
7253 q_vector->v_idx = v_idx;
7254 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
7255 if (vsi->netdev)
7256 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007257 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007258
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007259 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7260 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7261
Alexander Duyck493fb302013-09-28 07:01:44 +00007262 /* tie q_vector and vsi together */
7263 vsi->q_vectors[v_idx] = q_vector;
7264
7265 return 0;
7266}
7267
7268/**
Greg Rose90e04072014-03-06 08:59:57 +00007269 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007270 * @vsi: the VSI being configured
7271 *
7272 * We allocate one q_vector per queue interrupt. If allocation fails we
7273 * return -ENOMEM.
7274 **/
Greg Rose90e04072014-03-06 08:59:57 +00007275static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007276{
7277 struct i40e_pf *pf = vsi->back;
7278 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00007279 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007280
7281 /* if not MSIX, give the one vector only to the LAN VSI */
7282 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7283 num_q_vectors = vsi->num_q_vectors;
7284 else if (vsi == pf->vsi[pf->lan_vsi])
7285 num_q_vectors = 1;
7286 else
7287 return -EINVAL;
7288
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007289 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00007290 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00007291 if (err)
7292 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007293 }
7294
7295 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007296
7297err_out:
7298 while (v_idx--)
7299 i40e_free_q_vector(vsi, v_idx);
7300
7301 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007302}
7303
7304/**
7305 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7306 * @pf: board private structure to initialize
7307 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007308static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007309{
Shannon Nelson3b444392015-02-26 16:15:57 +00007310 int vectors = 0;
7311 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007312
7313 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007314 vectors = i40e_init_msix(pf);
7315 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007316 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007317#ifdef I40E_FCOE
7318 I40E_FLAG_FCOE_ENABLED |
7319#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007320 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007321 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007322 I40E_FLAG_SRIOV_ENABLED |
7323 I40E_FLAG_FD_SB_ENABLED |
7324 I40E_FLAG_FD_ATR_ENABLED |
7325 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007326
7327 /* rework the queue expectations without MSIX */
7328 i40e_determine_queue_usage(pf);
7329 }
7330 }
7331
7332 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7333 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007334 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007335 vectors = pci_enable_msi(pf->pdev);
7336 if (vectors < 0) {
7337 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7338 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007339 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7340 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007341 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007342 }
7343
Shannon Nelson958a3e32013-09-28 07:13:28 +00007344 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007345 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007346
Shannon Nelson3b444392015-02-26 16:15:57 +00007347 /* set up vector assignment tracking */
7348 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7349 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007350 if (!pf->irq_pile) {
7351 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7352 return -ENOMEM;
7353 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007354 pf->irq_pile->num_entries = vectors;
7355 pf->irq_pile->search_hint = 0;
7356
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007357 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007358 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007359
7360 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007361}
7362
7363/**
7364 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7365 * @pf: board private structure
7366 *
7367 * This sets up the handler for MSIX 0, which is used to manage the
7368 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7369 * when in MSI or Legacy interrupt mode.
7370 **/
7371static int i40e_setup_misc_vector(struct i40e_pf *pf)
7372{
7373 struct i40e_hw *hw = &pf->hw;
7374 int err = 0;
7375
7376 /* Only request the irq if this is the first time through, and
7377 * not when we're rebuilding after a Reset
7378 */
7379 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7380 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007381 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007382 if (err) {
7383 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007384 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007385 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007386 return -EFAULT;
7387 }
7388 }
7389
Jacob Kellerab437b52014-12-14 01:55:08 +00007390 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007391
7392 /* associate no queues to the misc vector */
7393 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
7394 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
7395
7396 i40e_flush(hw);
7397
7398 i40e_irq_dynamic_enable_icr0(pf);
7399
7400 return err;
7401}
7402
7403/**
7404 * i40e_config_rss - Prepare for RSS if used
7405 * @pf: board private structure
7406 **/
7407static int i40e_config_rss(struct i40e_pf *pf)
7408{
Eric Dumazet22f258a2014-11-16 06:23:13 -08007409 u32 rss_key[I40E_PFQF_HKEY_MAX_INDEX + 1];
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00007410 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00007411 struct i40e_hw *hw = &pf->hw;
7412 u32 lut = 0;
7413 int i, j;
7414 u64 hena;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007415 u32 reg_val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007416
Eric Dumazet22f258a2014-11-16 06:23:13 -08007417 netdev_rss_key_fill(rss_key, sizeof(rss_key));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007418 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Eric Dumazet22f258a2014-11-16 06:23:13 -08007419 wr32(hw, I40E_PFQF_HKEY(i), rss_key[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007420
7421 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
7422 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
7423 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00007424 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007425 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
7426 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
7427
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00007428 vsi->rss_size = min_t(int, pf->rss_size, vsi->num_queue_pairs);
7429
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007430 /* Check capability and Set table size and register per hw expectation*/
7431 reg_val = rd32(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaind9e894e2015-02-27 09:15:30 +00007432 if (pf->rss_table_size == 512)
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007433 reg_val |= I40E_PFQF_CTL_0_HASHLUTSIZE_512;
Anjali Singhai Jaind9e894e2015-02-27 09:15:30 +00007434 else
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007435 reg_val &= ~I40E_PFQF_CTL_0_HASHLUTSIZE_512;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007436 wr32(hw, I40E_PFQF_CTL_0, reg_val);
7437
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007438 /* Populate the LUT with max no. of queues in round robin fashion */
Carolyn Wybornye157ea32014-06-03 23:50:22 +00007439 for (i = 0, j = 0; i < pf->rss_table_size; i++, j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007440
7441 /* The assumption is that lan qp count will be the highest
7442 * qp count for any PF VSI that needs RSS.
7443 * If multiple VSIs need RSS support, all the qp counts
7444 * for those VSIs should be a power of 2 for RSS to work.
7445 * If LAN VSI is the only consumer for RSS then this requirement
7446 * is not necessary.
7447 */
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00007448 if (j == vsi->rss_size)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007449 j = 0;
7450 /* lut = 4-byte sliding window of 4 lut entries */
7451 lut = (lut << 8) | (j &
7452 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
7453 /* On i = 3, we have 4 entries in lut; write to the register */
7454 if ((i & 3) == 3)
7455 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
7456 }
7457 i40e_flush(hw);
7458
7459 return 0;
7460}
7461
7462/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007463 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
7464 * @pf: board private structure
7465 * @queue_count: the requested queue count for rss.
7466 *
7467 * returns 0 if rss is not enabled, if enabled returns the final rss queue
7468 * count which may be different from the requested queue count.
7469 **/
7470int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
7471{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007472 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
7473 int new_rss_size;
7474
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007475 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
7476 return 0;
7477
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007478 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007479
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007480 if (queue_count != vsi->num_queue_pairs) {
7481 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007482 i40e_prep_for_reset(pf);
7483
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00007484 pf->rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007485
7486 i40e_reset_and_rebuild(pf, true);
7487 i40e_config_rss(pf);
7488 }
7489 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
7490 return pf->rss_size;
7491}
7492
7493/**
Greg Rosef4492db2015-02-06 08:52:12 +00007494 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
7495 * @pf: board private structure
7496 **/
7497i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
7498{
7499 i40e_status status;
7500 bool min_valid, max_valid;
7501 u32 max_bw, min_bw;
7502
7503 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
7504 &min_valid, &max_valid);
7505
7506 if (!status) {
7507 if (min_valid)
7508 pf->npar_min_bw = min_bw;
7509 if (max_valid)
7510 pf->npar_max_bw = max_bw;
7511 }
7512
7513 return status;
7514}
7515
7516/**
7517 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
7518 * @pf: board private structure
7519 **/
7520i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
7521{
7522 struct i40e_aqc_configure_partition_bw_data bw_data;
7523 i40e_status status;
7524
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007525 /* Set the valid bit for this PF */
Greg Rosef4492db2015-02-06 08:52:12 +00007526 bw_data.pf_valid_bits = cpu_to_le16(1 << pf->hw.pf_id);
7527 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
7528 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
7529
7530 /* Set the new bandwidths */
7531 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
7532
7533 return status;
7534}
7535
7536/**
7537 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
7538 * @pf: board private structure
7539 **/
7540i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
7541{
7542 /* Commit temporary BW setting to permanent NVM image */
7543 enum i40e_admin_queue_err last_aq_status;
7544 i40e_status ret;
7545 u16 nvm_word;
7546
7547 if (pf->hw.partition_id != 1) {
7548 dev_info(&pf->pdev->dev,
7549 "Commit BW only works on partition 1! This is partition %d",
7550 pf->hw.partition_id);
7551 ret = I40E_NOT_SUPPORTED;
7552 goto bw_commit_out;
7553 }
7554
7555 /* Acquire NVM for read access */
7556 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
7557 last_aq_status = pf->hw.aq.asq_last_status;
7558 if (ret) {
7559 dev_info(&pf->pdev->dev,
7560 "Cannot acquire NVM for read access, err %d: aq_err %d\n",
7561 ret, last_aq_status);
7562 goto bw_commit_out;
7563 }
7564
7565 /* Read word 0x10 of NVM - SW compatibility word 1 */
7566 ret = i40e_aq_read_nvm(&pf->hw,
7567 I40E_SR_NVM_CONTROL_WORD,
7568 0x10, sizeof(nvm_word), &nvm_word,
7569 false, NULL);
7570 /* Save off last admin queue command status before releasing
7571 * the NVM
7572 */
7573 last_aq_status = pf->hw.aq.asq_last_status;
7574 i40e_release_nvm(&pf->hw);
7575 if (ret) {
7576 dev_info(&pf->pdev->dev, "NVM read error, err %d aq_err %d\n",
7577 ret, last_aq_status);
7578 goto bw_commit_out;
7579 }
7580
7581 /* Wait a bit for NVM release to complete */
7582 msleep(50);
7583
7584 /* Acquire NVM for write access */
7585 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
7586 last_aq_status = pf->hw.aq.asq_last_status;
7587 if (ret) {
7588 dev_info(&pf->pdev->dev,
7589 "Cannot acquire NVM for write access, err %d: aq_err %d\n",
7590 ret, last_aq_status);
7591 goto bw_commit_out;
7592 }
7593 /* Write it back out unchanged to initiate update NVM,
7594 * which will force a write of the shadow (alt) RAM to
7595 * the NVM - thus storing the bandwidth values permanently.
7596 */
7597 ret = i40e_aq_update_nvm(&pf->hw,
7598 I40E_SR_NVM_CONTROL_WORD,
7599 0x10, sizeof(nvm_word),
7600 &nvm_word, true, NULL);
7601 /* Save off last admin queue command status before releasing
7602 * the NVM
7603 */
7604 last_aq_status = pf->hw.aq.asq_last_status;
7605 i40e_release_nvm(&pf->hw);
7606 if (ret)
7607 dev_info(&pf->pdev->dev,
7608 "BW settings NOT SAVED, err %d aq_err %d\n",
7609 ret, last_aq_status);
7610bw_commit_out:
7611
7612 return ret;
7613}
7614
7615/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007616 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
7617 * @pf: board private structure to initialize
7618 *
7619 * i40e_sw_init initializes the Adapter private data structure.
7620 * Fields are initialized based on PCI device information and
7621 * OS network device settings (MTU size).
7622 **/
7623static int i40e_sw_init(struct i40e_pf *pf)
7624{
7625 int err = 0;
7626 int size;
7627
7628 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
7629 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00007630 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007631 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
7632 if (I40E_DEBUG_USER & debug)
7633 pf->hw.debug_mask = debug;
7634 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
7635 I40E_DEFAULT_MSG_ENABLE);
7636 }
7637
7638 /* Set default capability flags */
7639 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
7640 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00007641 I40E_FLAG_MSIX_ENABLED;
7642
7643 if (iommu_present(&pci_bus_type))
7644 pf->flags |= I40E_FLAG_RX_PS_ENABLED;
7645 else
7646 pf->flags |= I40E_FLAG_RX_1BUF_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007647
Mitch Williamsca99eb92014-04-04 04:43:07 +00007648 /* Set default ITR */
7649 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
7650 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
7651
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00007652 /* Depending on PF configurations, it is possible that the RSS
7653 * maximum might end up larger than the available queues
7654 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007655 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Paul M Stillwell Jrec9a7db2014-07-09 07:46:10 +00007656 pf->rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007657 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00007658 pf->rss_size_max = min_t(int, pf->rss_size_max,
7659 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007660 if (pf->hw.func_caps.rss) {
7661 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00007662 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007663 }
7664
Catherine Sullivan2050bc62013-12-18 13:46:03 +00007665 /* MFP mode enabled */
7666 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
7667 pf->flags |= I40E_FLAG_MFP_ENABLED;
7668 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00007669 if (i40e_get_npar_bw_setting(pf))
7670 dev_warn(&pf->pdev->dev,
7671 "Could not get NPAR bw settings\n");
7672 else
7673 dev_info(&pf->pdev->dev,
7674 "Min BW = %8.8x, Max BW = %8.8x\n",
7675 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00007676 }
7677
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007678 /* FW/NVM is not yet fixed in this regard */
7679 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
7680 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
7681 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
7682 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007683 /* Setup a counter for fd_atr per PF */
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00007684 pf->fd_atr_cnt_idx = I40E_FD_ATR_STAT_IDX(pf->hw.pf_id);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007685 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007686 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007687 /* Setup a counter for fd_sb per PF */
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00007688 pf->fd_sb_cnt_idx = I40E_FD_SB_STAT_IDX(pf->hw.pf_id);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007689 } else {
7690 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00007691 "Flow Director Sideband mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007692 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007693 pf->fdir_pf_filter_count =
7694 pf->hw.func_caps.fd_filters_guaranteed;
7695 pf->hw.fdir_shared_filter_count =
7696 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007697 }
7698
7699 if (pf->hw.func_caps.vmdq) {
7700 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
7701 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
7702 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
7703 }
7704
Vasu Dev38e00432014-08-01 13:27:03 -07007705#ifdef I40E_FCOE
7706 err = i40e_init_pf_fcoe(pf);
7707 if (err)
7708 dev_info(&pf->pdev->dev, "init_pf_fcoe failed: %d\n", err);
7709
7710#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007711#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00007712 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007713 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
7714 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
7715 pf->num_req_vfs = min_t(int,
7716 pf->hw.func_caps.num_vfs,
7717 I40E_MAX_VF_COUNT);
7718 }
7719#endif /* CONFIG_PCI_IOV */
7720 pf->eeprom_version = 0xDEAD;
7721 pf->lan_veb = I40E_NO_VEB;
7722 pf->lan_vsi = I40E_NO_VSI;
7723
7724 /* set up queue assignment tracking */
7725 size = sizeof(struct i40e_lump_tracking)
7726 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
7727 pf->qp_pile = kzalloc(size, GFP_KERNEL);
7728 if (!pf->qp_pile) {
7729 err = -ENOMEM;
7730 goto sw_init_done;
7731 }
7732 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
7733 pf->qp_pile->search_hint = 0;
7734
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00007735 pf->tx_timeout_recovery_level = 1;
7736
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007737 mutex_init(&pf->switch_mutex);
7738
Greg Rosec668a122015-02-26 16:10:39 +00007739 /* If NPAR is enabled nudge the Tx scheduler */
7740 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
7741 i40e_set_npar_bw_setting(pf);
7742
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007743sw_init_done:
7744 return err;
7745}
7746
7747/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00007748 * i40e_set_ntuple - set the ntuple feature flag and take action
7749 * @pf: board private structure to initialize
7750 * @features: the feature set that the stack is suggesting
7751 *
7752 * returns a bool to indicate if reset needs to happen
7753 **/
7754bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
7755{
7756 bool need_reset = false;
7757
7758 /* Check if Flow Director n-tuple support was enabled or disabled. If
7759 * the state changed, we need to reset.
7760 */
7761 if (features & NETIF_F_NTUPLE) {
7762 /* Enable filters and mark for reset */
7763 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
7764 need_reset = true;
7765 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7766 } else {
7767 /* turn off filters, mark for reset and clear SW filter list */
7768 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7769 need_reset = true;
7770 i40e_fdir_filter_exit(pf);
7771 }
7772 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00007773 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00007774 /* reset fd counters */
7775 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
7776 pf->fdir_pf_active_filters = 0;
7777 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
7778 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00007779 /* if ATR was auto disabled it can be re-enabled. */
7780 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
7781 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
7782 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00007783 }
7784 return need_reset;
7785}
7786
7787/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007788 * i40e_set_features - set the netdev feature flags
7789 * @netdev: ptr to the netdev being adjusted
7790 * @features: the feature set that the stack is suggesting
7791 **/
7792static int i40e_set_features(struct net_device *netdev,
7793 netdev_features_t features)
7794{
7795 struct i40e_netdev_priv *np = netdev_priv(netdev);
7796 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00007797 struct i40e_pf *pf = vsi->back;
7798 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007799
7800 if (features & NETIF_F_HW_VLAN_CTAG_RX)
7801 i40e_vlan_stripping_enable(vsi);
7802 else
7803 i40e_vlan_stripping_disable(vsi);
7804
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00007805 need_reset = i40e_set_ntuple(pf, features);
7806
7807 if (need_reset)
7808 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
7809
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007810 return 0;
7811}
7812
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007813#ifdef CONFIG_I40E_VXLAN
7814/**
7815 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
7816 * @pf: board private structure
7817 * @port: The UDP port to look up
7818 *
7819 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
7820 **/
7821static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
7822{
7823 u8 i;
7824
7825 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
7826 if (pf->vxlan_ports[i] == port)
7827 return i;
7828 }
7829
7830 return i;
7831}
7832
7833/**
7834 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
7835 * @netdev: This physical port's netdev
7836 * @sa_family: Socket Family that VXLAN is notifying us about
7837 * @port: New UDP port number that VXLAN started listening to
7838 **/
7839static void i40e_add_vxlan_port(struct net_device *netdev,
7840 sa_family_t sa_family, __be16 port)
7841{
7842 struct i40e_netdev_priv *np = netdev_priv(netdev);
7843 struct i40e_vsi *vsi = np->vsi;
7844 struct i40e_pf *pf = vsi->back;
7845 u8 next_idx;
7846 u8 idx;
7847
7848 if (sa_family == AF_INET6)
7849 return;
7850
7851 idx = i40e_get_vxlan_port_idx(pf, port);
7852
7853 /* Check if port already exists */
7854 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007855 netdev_info(netdev, "vxlan port %d already offloaded\n",
7856 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007857 return;
7858 }
7859
7860 /* Now check if there is space to add the new port */
7861 next_idx = i40e_get_vxlan_port_idx(pf, 0);
7862
7863 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007864 netdev_info(netdev, "maximum number of vxlan UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007865 ntohs(port));
7866 return;
7867 }
7868
7869 /* New port: add it and mark its index in the bitmap */
7870 pf->vxlan_ports[next_idx] = port;
7871 pf->pending_vxlan_bitmap |= (1 << next_idx);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007872 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007873
7874 dev_info(&pf->pdev->dev, "adding vxlan port %d\n", ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007875}
7876
7877/**
7878 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
7879 * @netdev: This physical port's netdev
7880 * @sa_family: Socket Family that VXLAN is notifying us about
7881 * @port: UDP port number that VXLAN stopped listening to
7882 **/
7883static void i40e_del_vxlan_port(struct net_device *netdev,
7884 sa_family_t sa_family, __be16 port)
7885{
7886 struct i40e_netdev_priv *np = netdev_priv(netdev);
7887 struct i40e_vsi *vsi = np->vsi;
7888 struct i40e_pf *pf = vsi->back;
7889 u8 idx;
7890
7891 if (sa_family == AF_INET6)
7892 return;
7893
7894 idx = i40e_get_vxlan_port_idx(pf, port);
7895
7896 /* Check if port already exists */
7897 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
7898 /* if port exists, set it to 0 (mark for deletion)
7899 * and make it pending
7900 */
7901 pf->vxlan_ports[idx] = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007902 pf->pending_vxlan_bitmap |= (1 << idx);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007903 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007904
7905 dev_info(&pf->pdev->dev, "deleting vxlan port %d\n",
7906 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007907 } else {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007908 netdev_warn(netdev, "vxlan port %d was not found, not deleting\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007909 ntohs(port));
7910 }
7911}
7912
7913#endif
Neerav Parikh1f224ad2014-02-12 01:45:31 +00007914static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01007915 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00007916{
7917 struct i40e_netdev_priv *np = netdev_priv(netdev);
7918 struct i40e_pf *pf = np->vsi->back;
7919 struct i40e_hw *hw = &pf->hw;
7920
7921 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
7922 return -EOPNOTSUPP;
7923
7924 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
7925 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
7926
7927 return 0;
7928}
7929
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08007930/**
7931 * i40e_ndo_fdb_add - add an entry to the hardware database
7932 * @ndm: the input from the stack
7933 * @tb: pointer to array of nladdr (unused)
7934 * @dev: the net device pointer
7935 * @addr: the MAC address entry being added
7936 * @flags: instructions from stack about fdb operation
7937 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00007938static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
7939 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01007940 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00007941 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00007942{
7943 struct i40e_netdev_priv *np = netdev_priv(dev);
7944 struct i40e_pf *pf = np->vsi->back;
7945 int err = 0;
7946
7947 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
7948 return -EOPNOTSUPP;
7949
Or Gerlitz65891fe2014-12-14 18:19:05 +02007950 if (vid) {
7951 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
7952 return -EINVAL;
7953 }
7954
Greg Rose4ba0dea2014-03-06 08:59:55 +00007955 /* Hardware does not support aging addresses so if a
7956 * ndm_state is given only allow permanent addresses
7957 */
7958 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
7959 netdev_info(dev, "FDB only supports static addresses\n");
7960 return -EINVAL;
7961 }
7962
7963 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
7964 err = dev_uc_add_excl(dev, addr);
7965 else if (is_multicast_ether_addr(addr))
7966 err = dev_mc_add_excl(dev, addr);
7967 else
7968 err = -EINVAL;
7969
7970 /* Only return duplicate errors if NLM_F_EXCL is set */
7971 if (err == -EEXIST && !(flags & NLM_F_EXCL))
7972 err = 0;
7973
7974 return err;
7975}
7976
Neerav Parikh51616012015-02-06 08:52:14 +00007977#ifdef HAVE_BRIDGE_ATTRIBS
7978/**
7979 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
7980 * @dev: the netdev being configured
7981 * @nlh: RTNL message
7982 *
7983 * Inserts a new hardware bridge if not already created and
7984 * enables the bridging mode requested (VEB or VEPA). If the
7985 * hardware bridge has already been inserted and the request
7986 * is to change the mode then that requires a PF reset to
7987 * allow rebuild of the components with required hardware
7988 * bridge mode enabled.
7989 **/
7990static int i40e_ndo_bridge_setlink(struct net_device *dev,
7991 struct nlmsghdr *nlh)
7992{
7993 struct i40e_netdev_priv *np = netdev_priv(dev);
7994 struct i40e_vsi *vsi = np->vsi;
7995 struct i40e_pf *pf = vsi->back;
7996 struct i40e_veb *veb = NULL;
7997 struct nlattr *attr, *br_spec;
7998 int i, rem;
7999
8000 /* Only for PF VSI for now */
8001 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8002 return -EOPNOTSUPP;
8003
8004 /* Find the HW bridge for PF VSI */
8005 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8006 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8007 veb = pf->veb[i];
8008 }
8009
8010 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8011
8012 nla_for_each_nested(attr, br_spec, rem) {
8013 __u16 mode;
8014
8015 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8016 continue;
8017
8018 mode = nla_get_u16(attr);
8019 if ((mode != BRIDGE_MODE_VEPA) &&
8020 (mode != BRIDGE_MODE_VEB))
8021 return -EINVAL;
8022
8023 /* Insert a new HW bridge */
8024 if (!veb) {
8025 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8026 vsi->tc_config.enabled_tc);
8027 if (veb) {
8028 veb->bridge_mode = mode;
8029 i40e_config_bridge_mode(veb);
8030 } else {
8031 /* No Bridge HW offload available */
8032 return -ENOENT;
8033 }
8034 break;
8035 } else if (mode != veb->bridge_mode) {
8036 /* Existing HW bridge but different mode needs reset */
8037 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008038 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
8039 if (mode == BRIDGE_MODE_VEB)
8040 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
8041 else
8042 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
8043 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00008044 break;
8045 }
8046 }
8047
8048 return 0;
8049}
8050
8051/**
8052 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
8053 * @skb: skb buff
8054 * @pid: process id
8055 * @seq: RTNL message seq #
8056 * @dev: the netdev being configured
8057 * @filter_mask: unused
8058 *
8059 * Return the mode in which the hardware bridge is operating in
8060 * i.e VEB or VEPA.
8061 **/
8062#ifdef HAVE_BRIDGE_FILTER
8063static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8064 struct net_device *dev,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02008065 u32 __always_unused filter_mask, int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00008066#else
8067static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02008068 struct net_device *dev, int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00008069#endif /* HAVE_BRIDGE_FILTER */
8070{
8071 struct i40e_netdev_priv *np = netdev_priv(dev);
8072 struct i40e_vsi *vsi = np->vsi;
8073 struct i40e_pf *pf = vsi->back;
8074 struct i40e_veb *veb = NULL;
8075 int i;
8076
8077 /* Only for PF VSI for now */
8078 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8079 return -EOPNOTSUPP;
8080
8081 /* Find the HW bridge for the PF VSI */
8082 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8083 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8084 veb = pf->veb[i];
8085 }
8086
8087 if (!veb)
8088 return 0;
8089
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02008090 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
8091 nlflags);
Neerav Parikh51616012015-02-06 08:52:14 +00008092}
8093#endif /* HAVE_BRIDGE_ATTRIBS */
8094
Shannon Nelson37a29732015-02-27 09:15:19 +00008095static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008096 .ndo_open = i40e_open,
8097 .ndo_stop = i40e_close,
8098 .ndo_start_xmit = i40e_lan_xmit_frame,
8099 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
8100 .ndo_set_rx_mode = i40e_set_rx_mode,
8101 .ndo_validate_addr = eth_validate_addr,
8102 .ndo_set_mac_address = i40e_set_mac,
8103 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008104 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008105 .ndo_tx_timeout = i40e_tx_timeout,
8106 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
8107 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
8108#ifdef CONFIG_NET_POLL_CONTROLLER
8109 .ndo_poll_controller = i40e_netpoll,
8110#endif
8111 .ndo_setup_tc = i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07008112#ifdef I40E_FCOE
8113 .ndo_fcoe_enable = i40e_fcoe_enable,
8114 .ndo_fcoe_disable = i40e_fcoe_disable,
8115#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008116 .ndo_set_features = i40e_set_features,
8117 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
8118 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04008119 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008120 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00008121 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00008122 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008123#ifdef CONFIG_I40E_VXLAN
8124 .ndo_add_vxlan_port = i40e_add_vxlan_port,
8125 .ndo_del_vxlan_port = i40e_del_vxlan_port,
8126#endif
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008127 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008128 .ndo_fdb_add = i40e_ndo_fdb_add,
Neerav Parikh51616012015-02-06 08:52:14 +00008129#ifdef HAVE_BRIDGE_ATTRIBS
8130 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
8131 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
8132#endif /* HAVE_BRIDGE_ATTRIBS */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008133};
8134
8135/**
8136 * i40e_config_netdev - Setup the netdev flags
8137 * @vsi: the VSI being configured
8138 *
8139 * Returns 0 on success, negative value on failure
8140 **/
8141static int i40e_config_netdev(struct i40e_vsi *vsi)
8142{
Greg Rose1a103702013-11-28 06:42:39 +00008143 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008144 struct i40e_pf *pf = vsi->back;
8145 struct i40e_hw *hw = &pf->hw;
8146 struct i40e_netdev_priv *np;
8147 struct net_device *netdev;
8148 u8 mac_addr[ETH_ALEN];
8149 int etherdev_size;
8150
8151 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008152 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008153 if (!netdev)
8154 return -ENOMEM;
8155
8156 vsi->netdev = netdev;
8157 np = netdev_priv(netdev);
8158 np->vsi = vsi;
8159
Or Gerlitzd70e9412014-03-18 10:36:45 +02008160 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008161 NETIF_F_GSO_UDP_TUNNEL |
Or Gerlitzd70e9412014-03-18 10:36:45 +02008162 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008163
8164 netdev->features = NETIF_F_SG |
8165 NETIF_F_IP_CSUM |
8166 NETIF_F_SCTP_CSUM |
8167 NETIF_F_HIGHDMA |
8168 NETIF_F_GSO_UDP_TUNNEL |
8169 NETIF_F_HW_VLAN_CTAG_TX |
8170 NETIF_F_HW_VLAN_CTAG_RX |
8171 NETIF_F_HW_VLAN_CTAG_FILTER |
8172 NETIF_F_IPV6_CSUM |
8173 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00008174 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008175 NETIF_F_TSO6 |
8176 NETIF_F_RXCSUM |
8177 NETIF_F_RXHASH |
8178 0;
8179
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00008180 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
8181 netdev->features |= NETIF_F_NTUPLE;
8182
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008183 /* copy netdev features into list of user selectable features */
8184 netdev->hw_features |= netdev->features;
8185
8186 if (vsi->type == I40E_VSI_MAIN) {
8187 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00008188 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Shannon Nelson30650cc2014-07-29 04:01:50 +00008189 /* The following steps are necessary to prevent reception
8190 * of tagged packets - some older NVM configurations load a
8191 * default a MAC-VLAN filter that accepts any tagged packet
8192 * which must be replaced by a normal filter.
Greg Rose8c27d422014-05-22 06:31:56 +00008193 */
Shannon Nelson30650cc2014-07-29 04:01:50 +00008194 if (!i40e_rm_default_mac_filter(vsi, mac_addr))
8195 i40e_add_filter(vsi, mac_addr,
8196 I40E_VLAN_ANY, false, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008197 } else {
8198 /* relate the VSI_VMDQ name to the VSI_MAIN name */
8199 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
8200 pf->vsi[pf->lan_vsi]->netdev->name);
8201 random_ether_addr(mac_addr);
8202 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
8203 }
Greg Rose1a103702013-11-28 06:42:39 +00008204 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008205
Greg Rose9a173902014-05-22 06:32:02 +00008206 ether_addr_copy(netdev->dev_addr, mac_addr);
8207 ether_addr_copy(netdev->perm_addr, mac_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008208 /* vlan gets same features (except vlan offload)
8209 * after any tweaks for specific VSI types
8210 */
8211 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
8212 NETIF_F_HW_VLAN_CTAG_RX |
8213 NETIF_F_HW_VLAN_CTAG_FILTER);
8214 netdev->priv_flags |= IFF_UNICAST_FLT;
8215 netdev->priv_flags |= IFF_SUPP_NOFCS;
8216 /* Setup netdev TC information */
8217 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
8218
8219 netdev->netdev_ops = &i40e_netdev_ops;
8220 netdev->watchdog_timeo = 5 * HZ;
8221 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07008222#ifdef I40E_FCOE
8223 i40e_fcoe_config_netdev(netdev, vsi);
8224#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008225
8226 return 0;
8227}
8228
8229/**
8230 * i40e_vsi_delete - Delete a VSI from the switch
8231 * @vsi: the VSI being removed
8232 *
8233 * Returns 0 on success, negative value on failure
8234 **/
8235static void i40e_vsi_delete(struct i40e_vsi *vsi)
8236{
8237 /* remove default VSI is not allowed */
8238 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
8239 return;
8240
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008241 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008242}
8243
8244/**
Neerav Parikh51616012015-02-06 08:52:14 +00008245 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
8246 * @vsi: the VSI being queried
8247 *
8248 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
8249 **/
8250int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
8251{
8252 struct i40e_veb *veb;
8253 struct i40e_pf *pf = vsi->back;
8254
8255 /* Uplink is not a bridge so default to VEB */
8256 if (vsi->veb_idx == I40E_NO_VEB)
8257 return 1;
8258
8259 veb = pf->veb[vsi->veb_idx];
8260 /* Uplink is a bridge in VEPA mode */
8261 if (veb && (veb->bridge_mode & BRIDGE_MODE_VEPA))
8262 return 0;
8263
8264 /* Uplink is a bridge in VEB mode */
8265 return 1;
8266}
8267
8268/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008269 * i40e_add_vsi - Add a VSI to the switch
8270 * @vsi: the VSI being configured
8271 *
8272 * This initializes a VSI context depending on the VSI type to be added and
8273 * passes it down to the add_vsi aq command.
8274 **/
8275static int i40e_add_vsi(struct i40e_vsi *vsi)
8276{
8277 int ret = -ENODEV;
8278 struct i40e_mac_filter *f, *ftmp;
8279 struct i40e_pf *pf = vsi->back;
8280 struct i40e_hw *hw = &pf->hw;
8281 struct i40e_vsi_context ctxt;
8282 u8 enabled_tc = 0x1; /* TC0 enabled */
8283 int f_count = 0;
8284
8285 memset(&ctxt, 0, sizeof(ctxt));
8286 switch (vsi->type) {
8287 case I40E_VSI_MAIN:
8288 /* The PF's main VSI is already setup as part of the
8289 * device initialization, so we'll not bother with
8290 * the add_vsi call, but we will retrieve the current
8291 * VSI context.
8292 */
8293 ctxt.seid = pf->main_vsi_seid;
8294 ctxt.pf_num = pf->hw.pf_id;
8295 ctxt.vf_num = 0;
8296 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8297 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8298 if (ret) {
8299 dev_info(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00008300 "couldn't get PF vsi config, err %d, aq_err %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008301 ret, pf->hw.aq.asq_last_status);
8302 return -ENOENT;
8303 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07008304 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008305 vsi->info.valid_sections = 0;
8306
8307 vsi->seid = ctxt.seid;
8308 vsi->id = ctxt.vsi_number;
8309
8310 enabled_tc = i40e_pf_get_tc_map(pf);
8311
8312 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00008313 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
8314 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008315 memset(&ctxt, 0, sizeof(ctxt));
8316 ctxt.seid = pf->main_vsi_seid;
8317 ctxt.pf_num = pf->hw.pf_id;
8318 ctxt.vf_num = 0;
8319 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
8320 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
8321 if (ret) {
8322 dev_info(&pf->pdev->dev,
8323 "update vsi failed, aq_err=%d\n",
8324 pf->hw.aq.asq_last_status);
8325 ret = -ENOENT;
8326 goto err;
8327 }
8328 /* update the local VSI info queue map */
8329 i40e_vsi_update_queue_map(vsi, &ctxt);
8330 vsi->info.valid_sections = 0;
8331 } else {
8332 /* Default/Main VSI is only enabled for TC0
8333 * reconfigure it to enable all TCs that are
8334 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00008335 * For MFP case the iSCSI PF would use this
8336 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008337 */
8338 ret = i40e_vsi_config_tc(vsi, enabled_tc);
8339 if (ret) {
8340 dev_info(&pf->pdev->dev,
8341 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
8342 enabled_tc, ret,
8343 pf->hw.aq.asq_last_status);
8344 ret = -ENOENT;
8345 }
8346 }
8347 break;
8348
8349 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008350 ctxt.pf_num = hw->pf_id;
8351 ctxt.vf_num = 0;
8352 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00008353 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008354 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008355 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
8356 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00008357 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008358 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00008359 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008360 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00008361 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008362 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008363 break;
8364
8365 case I40E_VSI_VMDQ2:
8366 ctxt.pf_num = hw->pf_id;
8367 ctxt.vf_num = 0;
8368 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00008369 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008370 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
8371
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008372 /* This VSI is connected to VEB so the switch_id
8373 * should be set to zero by default.
8374 */
Neerav Parikh51616012015-02-06 08:52:14 +00008375 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
8376 ctxt.info.valid_sections |=
8377 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8378 ctxt.info.switch_id =
8379 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8380 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008381
8382 /* Setup the VSI tx/rx queue map for TC0 only for now */
8383 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
8384 break;
8385
8386 case I40E_VSI_SRIOV:
8387 ctxt.pf_num = hw->pf_id;
8388 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
8389 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00008390 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008391 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
8392
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008393 /* This VSI is connected to VEB so the switch_id
8394 * should be set to zero by default.
8395 */
Neerav Parikh51616012015-02-06 08:52:14 +00008396 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
8397 ctxt.info.valid_sections |=
8398 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8399 ctxt.info.switch_id =
8400 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8401 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008402
8403 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
8404 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00008405 if (pf->vf[vsi->vf_id].spoofchk) {
8406 ctxt.info.valid_sections |=
8407 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
8408 ctxt.info.sec_flags |=
8409 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
8410 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
8411 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008412 /* Setup the VSI tx/rx queue map for TC0 only for now */
8413 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
8414 break;
8415
Vasu Dev38e00432014-08-01 13:27:03 -07008416#ifdef I40E_FCOE
8417 case I40E_VSI_FCOE:
8418 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
8419 if (ret) {
8420 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
8421 return ret;
8422 }
8423 break;
8424
8425#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008426 default:
8427 return -ENODEV;
8428 }
8429
8430 if (vsi->type != I40E_VSI_MAIN) {
8431 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
8432 if (ret) {
8433 dev_info(&vsi->back->pdev->dev,
8434 "add vsi failed, aq_err=%d\n",
8435 vsi->back->hw.aq.asq_last_status);
8436 ret = -ENOENT;
8437 goto err;
8438 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07008439 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008440 vsi->info.valid_sections = 0;
8441 vsi->seid = ctxt.seid;
8442 vsi->id = ctxt.vsi_number;
8443 }
8444
8445 /* If macvlan filters already exist, force them to get loaded */
8446 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
8447 f->changed = true;
8448 f_count++;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00008449
8450 if (f->is_laa && vsi->type == I40E_VSI_MAIN) {
Shannon Nelson30650cc2014-07-29 04:01:50 +00008451 struct i40e_aqc_remove_macvlan_element_data element;
8452
8453 memset(&element, 0, sizeof(element));
8454 ether_addr_copy(element.mac_addr, f->macaddr);
8455 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
8456 ret = i40e_aq_remove_macvlan(hw, vsi->seid,
8457 &element, 1, NULL);
8458 if (ret) {
8459 /* some older FW has a different default */
8460 element.flags |=
8461 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
8462 i40e_aq_remove_macvlan(hw, vsi->seid,
8463 &element, 1, NULL);
8464 }
8465
8466 i40e_aq_mac_address_write(hw,
Shannon Nelson6252c7e2014-06-04 01:23:23 +00008467 I40E_AQC_WRITE_TYPE_LAA_WOL,
8468 f->macaddr, NULL);
8469 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008470 }
8471 if (f_count) {
8472 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
8473 pf->flags |= I40E_FLAG_FILTER_SYNC;
8474 }
8475
8476 /* Update VSI BW information */
8477 ret = i40e_vsi_get_bw_info(vsi);
8478 if (ret) {
8479 dev_info(&pf->pdev->dev,
8480 "couldn't get vsi bw info, err %d, aq_err %d\n",
8481 ret, pf->hw.aq.asq_last_status);
8482 /* VSI is already added so not tearing that up */
8483 ret = 0;
8484 }
8485
8486err:
8487 return ret;
8488}
8489
8490/**
8491 * i40e_vsi_release - Delete a VSI and free its resources
8492 * @vsi: the VSI being removed
8493 *
8494 * Returns 0 on success or < 0 on error
8495 **/
8496int i40e_vsi_release(struct i40e_vsi *vsi)
8497{
8498 struct i40e_mac_filter *f, *ftmp;
8499 struct i40e_veb *veb = NULL;
8500 struct i40e_pf *pf;
8501 u16 uplink_seid;
8502 int i, n;
8503
8504 pf = vsi->back;
8505
8506 /* release of a VEB-owner or last VSI is not allowed */
8507 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
8508 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
8509 vsi->seid, vsi->uplink_seid);
8510 return -ENODEV;
8511 }
8512 if (vsi == pf->vsi[pf->lan_vsi] &&
8513 !test_bit(__I40E_DOWN, &pf->state)) {
8514 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
8515 return -ENODEV;
8516 }
8517
8518 uplink_seid = vsi->uplink_seid;
8519 if (vsi->type != I40E_VSI_SRIOV) {
8520 if (vsi->netdev_registered) {
8521 vsi->netdev_registered = false;
8522 if (vsi->netdev) {
8523 /* results in a call to i40e_close() */
8524 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008525 }
8526 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00008527 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008528 }
8529 i40e_vsi_disable_irq(vsi);
8530 }
8531
8532 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
8533 i40e_del_filter(vsi, f->macaddr, f->vlan,
8534 f->is_vf, f->is_netdev);
8535 i40e_sync_vsi_filters(vsi);
8536
8537 i40e_vsi_delete(vsi);
8538 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00008539 if (vsi->netdev) {
8540 free_netdev(vsi->netdev);
8541 vsi->netdev = NULL;
8542 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008543 i40e_vsi_clear_rings(vsi);
8544 i40e_vsi_clear(vsi);
8545
8546 /* If this was the last thing on the VEB, except for the
8547 * controlling VSI, remove the VEB, which puts the controlling
8548 * VSI onto the next level down in the switch.
8549 *
8550 * Well, okay, there's one more exception here: don't remove
8551 * the orphan VEBs yet. We'll wait for an explicit remove request
8552 * from up the network stack.
8553 */
Mitch Williams505682c2014-05-20 08:01:37 +00008554 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008555 if (pf->vsi[i] &&
8556 pf->vsi[i]->uplink_seid == uplink_seid &&
8557 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
8558 n++; /* count the VSIs */
8559 }
8560 }
8561 for (i = 0; i < I40E_MAX_VEB; i++) {
8562 if (!pf->veb[i])
8563 continue;
8564 if (pf->veb[i]->uplink_seid == uplink_seid)
8565 n++; /* count the VEBs */
8566 if (pf->veb[i]->seid == uplink_seid)
8567 veb = pf->veb[i];
8568 }
8569 if (n == 0 && veb && veb->uplink_seid != 0)
8570 i40e_veb_release(veb);
8571
8572 return 0;
8573}
8574
8575/**
8576 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
8577 * @vsi: ptr to the VSI
8578 *
8579 * This should only be called after i40e_vsi_mem_alloc() which allocates the
8580 * corresponding SW VSI structure and initializes num_queue_pairs for the
8581 * newly allocated VSI.
8582 *
8583 * Returns 0 on success or negative on failure
8584 **/
8585static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
8586{
8587 int ret = -ENOENT;
8588 struct i40e_pf *pf = vsi->back;
8589
Alexander Duyck493fb302013-09-28 07:01:44 +00008590 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008591 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
8592 vsi->seid);
8593 return -EEXIST;
8594 }
8595
8596 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00008597 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008598 vsi->seid, vsi->base_vector);
8599 return -EEXIST;
8600 }
8601
Greg Rose90e04072014-03-06 08:59:57 +00008602 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008603 if (ret) {
8604 dev_info(&pf->pdev->dev,
8605 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
8606 vsi->num_q_vectors, vsi->seid, ret);
8607 vsi->num_q_vectors = 0;
8608 goto vector_setup_out;
8609 }
8610
Shannon Nelson958a3e32013-09-28 07:13:28 +00008611 if (vsi->num_q_vectors)
8612 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
8613 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008614 if (vsi->base_vector < 0) {
8615 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00008616 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
8617 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008618 i40e_vsi_free_q_vectors(vsi);
8619 ret = -ENOENT;
8620 goto vector_setup_out;
8621 }
8622
8623vector_setup_out:
8624 return ret;
8625}
8626
8627/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008628 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
8629 * @vsi: pointer to the vsi.
8630 *
8631 * This re-allocates a vsi's queue resources.
8632 *
8633 * Returns pointer to the successfully allocated and configured VSI sw struct
8634 * on success, otherwise returns NULL on failure.
8635 **/
8636static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
8637{
8638 struct i40e_pf *pf = vsi->back;
8639 u8 enabled_tc;
8640 int ret;
8641
8642 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
8643 i40e_vsi_clear_rings(vsi);
8644
8645 i40e_vsi_free_arrays(vsi, false);
8646 i40e_set_num_rings_in_vsi(vsi);
8647 ret = i40e_vsi_alloc_arrays(vsi, false);
8648 if (ret)
8649 goto err_vsi;
8650
8651 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
8652 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00008653 dev_info(&pf->pdev->dev,
8654 "failed to get tracking for %d queues for VSI %d err=%d\n",
8655 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008656 goto err_vsi;
8657 }
8658 vsi->base_queue = ret;
8659
8660 /* Update the FW view of the VSI. Force a reset of TC and queue
8661 * layout configurations.
8662 */
8663 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
8664 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
8665 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
8666 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
8667
8668 /* assign it some queues */
8669 ret = i40e_alloc_rings(vsi);
8670 if (ret)
8671 goto err_rings;
8672
8673 /* map all of the rings to the q_vectors */
8674 i40e_vsi_map_rings_to_vectors(vsi);
8675 return vsi;
8676
8677err_rings:
8678 i40e_vsi_free_q_vectors(vsi);
8679 if (vsi->netdev_registered) {
8680 vsi->netdev_registered = false;
8681 unregister_netdev(vsi->netdev);
8682 free_netdev(vsi->netdev);
8683 vsi->netdev = NULL;
8684 }
8685 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
8686err_vsi:
8687 i40e_vsi_clear(vsi);
8688 return NULL;
8689}
8690
8691/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008692 * i40e_vsi_setup - Set up a VSI by a given type
8693 * @pf: board private structure
8694 * @type: VSI type
8695 * @uplink_seid: the switch element to link to
8696 * @param1: usage depends upon VSI type. For VF types, indicates VF id
8697 *
8698 * This allocates the sw VSI structure and its queue resources, then add a VSI
8699 * to the identified VEB.
8700 *
8701 * Returns pointer to the successfully allocated and configure VSI sw struct on
8702 * success, otherwise returns NULL on failure.
8703 **/
8704struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
8705 u16 uplink_seid, u32 param1)
8706{
8707 struct i40e_vsi *vsi = NULL;
8708 struct i40e_veb *veb = NULL;
8709 int ret, i;
8710 int v_idx;
8711
8712 /* The requested uplink_seid must be either
8713 * - the PF's port seid
8714 * no VEB is needed because this is the PF
8715 * or this is a Flow Director special case VSI
8716 * - seid of an existing VEB
8717 * - seid of a VSI that owns an existing VEB
8718 * - seid of a VSI that doesn't own a VEB
8719 * a new VEB is created and the VSI becomes the owner
8720 * - seid of the PF VSI, which is what creates the first VEB
8721 * this is a special case of the previous
8722 *
8723 * Find which uplink_seid we were given and create a new VEB if needed
8724 */
8725 for (i = 0; i < I40E_MAX_VEB; i++) {
8726 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
8727 veb = pf->veb[i];
8728 break;
8729 }
8730 }
8731
8732 if (!veb && uplink_seid != pf->mac_seid) {
8733
Mitch Williams505682c2014-05-20 08:01:37 +00008734 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008735 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
8736 vsi = pf->vsi[i];
8737 break;
8738 }
8739 }
8740 if (!vsi) {
8741 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
8742 uplink_seid);
8743 return NULL;
8744 }
8745
8746 if (vsi->uplink_seid == pf->mac_seid)
8747 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
8748 vsi->tc_config.enabled_tc);
8749 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
8750 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8751 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00008752 if (veb) {
8753 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
8754 dev_info(&vsi->back->pdev->dev,
8755 "%s: New VSI creation error, uplink seid of LAN VSI expected.\n",
8756 __func__);
8757 return NULL;
8758 }
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008759 /* We come up by default in VEPA mode */
8760 veb->bridge_mode = BRIDGE_MODE_VEPA;
8761 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
Neerav Parikh51616012015-02-06 08:52:14 +00008762 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00008763 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008764 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8765 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8766 veb = pf->veb[i];
8767 }
8768 if (!veb) {
8769 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
8770 return NULL;
8771 }
8772
8773 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
8774 uplink_seid = veb->seid;
8775 }
8776
8777 /* get vsi sw struct */
8778 v_idx = i40e_vsi_mem_alloc(pf, type);
8779 if (v_idx < 0)
8780 goto err_alloc;
8781 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008782 if (!vsi)
8783 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008784 vsi->type = type;
8785 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
8786
8787 if (type == I40E_VSI_MAIN)
8788 pf->lan_vsi = v_idx;
8789 else if (type == I40E_VSI_SRIOV)
8790 vsi->vf_id = param1;
8791 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008792 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
8793 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008794 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00008795 dev_info(&pf->pdev->dev,
8796 "failed to get tracking for %d queues for VSI %d err=%d\n",
8797 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008798 goto err_vsi;
8799 }
8800 vsi->base_queue = ret;
8801
8802 /* get a VSI from the hardware */
8803 vsi->uplink_seid = uplink_seid;
8804 ret = i40e_add_vsi(vsi);
8805 if (ret)
8806 goto err_vsi;
8807
8808 switch (vsi->type) {
8809 /* setup the netdev if needed */
8810 case I40E_VSI_MAIN:
8811 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07008812 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008813 ret = i40e_config_netdev(vsi);
8814 if (ret)
8815 goto err_netdev;
8816 ret = register_netdev(vsi->netdev);
8817 if (ret)
8818 goto err_netdev;
8819 vsi->netdev_registered = true;
8820 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008821#ifdef CONFIG_I40E_DCB
8822 /* Setup DCB netlink interface */
8823 i40e_dcbnl_setup(vsi);
8824#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008825 /* fall through */
8826
8827 case I40E_VSI_FDIR:
8828 /* set up vectors and rings if needed */
8829 ret = i40e_vsi_setup_vectors(vsi);
8830 if (ret)
8831 goto err_msix;
8832
8833 ret = i40e_alloc_rings(vsi);
8834 if (ret)
8835 goto err_rings;
8836
8837 /* map all of the rings to the q_vectors */
8838 i40e_vsi_map_rings_to_vectors(vsi);
8839
8840 i40e_vsi_reset_stats(vsi);
8841 break;
8842
8843 default:
8844 /* no netdev or rings for the other VSI types */
8845 break;
8846 }
8847
8848 return vsi;
8849
8850err_rings:
8851 i40e_vsi_free_q_vectors(vsi);
8852err_msix:
8853 if (vsi->netdev_registered) {
8854 vsi->netdev_registered = false;
8855 unregister_netdev(vsi->netdev);
8856 free_netdev(vsi->netdev);
8857 vsi->netdev = NULL;
8858 }
8859err_netdev:
8860 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
8861err_vsi:
8862 i40e_vsi_clear(vsi);
8863err_alloc:
8864 return NULL;
8865}
8866
8867/**
8868 * i40e_veb_get_bw_info - Query VEB BW information
8869 * @veb: the veb to query
8870 *
8871 * Query the Tx scheduler BW configuration data for given VEB
8872 **/
8873static int i40e_veb_get_bw_info(struct i40e_veb *veb)
8874{
8875 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
8876 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
8877 struct i40e_pf *pf = veb->pf;
8878 struct i40e_hw *hw = &pf->hw;
8879 u32 tc_bw_max;
8880 int ret = 0;
8881 int i;
8882
8883 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
8884 &bw_data, NULL);
8885 if (ret) {
8886 dev_info(&pf->pdev->dev,
8887 "query veb bw config failed, aq_err=%d\n",
8888 hw->aq.asq_last_status);
8889 goto out;
8890 }
8891
8892 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
8893 &ets_data, NULL);
8894 if (ret) {
8895 dev_info(&pf->pdev->dev,
8896 "query veb bw ets config failed, aq_err=%d\n",
8897 hw->aq.asq_last_status);
8898 goto out;
8899 }
8900
8901 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
8902 veb->bw_max_quanta = ets_data.tc_bw_max;
8903 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +00008904 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008905 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
8906 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
8907 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
8908 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
8909 veb->bw_tc_limit_credits[i] =
8910 le16_to_cpu(bw_data.tc_bw_limits[i]);
8911 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
8912 }
8913
8914out:
8915 return ret;
8916}
8917
8918/**
8919 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
8920 * @pf: board private structure
8921 *
8922 * On error: returns error code (negative)
8923 * On success: returns vsi index in PF (positive)
8924 **/
8925static int i40e_veb_mem_alloc(struct i40e_pf *pf)
8926{
8927 int ret = -ENOENT;
8928 struct i40e_veb *veb;
8929 int i;
8930
8931 /* Need to protect the allocation of switch elements at the PF level */
8932 mutex_lock(&pf->switch_mutex);
8933
8934 /* VEB list may be fragmented if VEB creation/destruction has
8935 * been happening. We can afford to do a quick scan to look
8936 * for any free slots in the list.
8937 *
8938 * find next empty veb slot, looping back around if necessary
8939 */
8940 i = 0;
8941 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
8942 i++;
8943 if (i >= I40E_MAX_VEB) {
8944 ret = -ENOMEM;
8945 goto err_alloc_veb; /* out of VEB slots! */
8946 }
8947
8948 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
8949 if (!veb) {
8950 ret = -ENOMEM;
8951 goto err_alloc_veb;
8952 }
8953 veb->pf = pf;
8954 veb->idx = i;
8955 veb->enabled_tc = 1;
8956
8957 pf->veb[i] = veb;
8958 ret = i;
8959err_alloc_veb:
8960 mutex_unlock(&pf->switch_mutex);
8961 return ret;
8962}
8963
8964/**
8965 * i40e_switch_branch_release - Delete a branch of the switch tree
8966 * @branch: where to start deleting
8967 *
8968 * This uses recursion to find the tips of the branch to be
8969 * removed, deleting until we get back to and can delete this VEB.
8970 **/
8971static void i40e_switch_branch_release(struct i40e_veb *branch)
8972{
8973 struct i40e_pf *pf = branch->pf;
8974 u16 branch_seid = branch->seid;
8975 u16 veb_idx = branch->idx;
8976 int i;
8977
8978 /* release any VEBs on this VEB - RECURSION */
8979 for (i = 0; i < I40E_MAX_VEB; i++) {
8980 if (!pf->veb[i])
8981 continue;
8982 if (pf->veb[i]->uplink_seid == branch->seid)
8983 i40e_switch_branch_release(pf->veb[i]);
8984 }
8985
8986 /* Release the VSIs on this VEB, but not the owner VSI.
8987 *
8988 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
8989 * the VEB itself, so don't use (*branch) after this loop.
8990 */
Mitch Williams505682c2014-05-20 08:01:37 +00008991 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008992 if (!pf->vsi[i])
8993 continue;
8994 if (pf->vsi[i]->uplink_seid == branch_seid &&
8995 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
8996 i40e_vsi_release(pf->vsi[i]);
8997 }
8998 }
8999
9000 /* There's one corner case where the VEB might not have been
9001 * removed, so double check it here and remove it if needed.
9002 * This case happens if the veb was created from the debugfs
9003 * commands and no VSIs were added to it.
9004 */
9005 if (pf->veb[veb_idx])
9006 i40e_veb_release(pf->veb[veb_idx]);
9007}
9008
9009/**
9010 * i40e_veb_clear - remove veb struct
9011 * @veb: the veb to remove
9012 **/
9013static void i40e_veb_clear(struct i40e_veb *veb)
9014{
9015 if (!veb)
9016 return;
9017
9018 if (veb->pf) {
9019 struct i40e_pf *pf = veb->pf;
9020
9021 mutex_lock(&pf->switch_mutex);
9022 if (pf->veb[veb->idx] == veb)
9023 pf->veb[veb->idx] = NULL;
9024 mutex_unlock(&pf->switch_mutex);
9025 }
9026
9027 kfree(veb);
9028}
9029
9030/**
9031 * i40e_veb_release - Delete a VEB and free its resources
9032 * @veb: the VEB being removed
9033 **/
9034void i40e_veb_release(struct i40e_veb *veb)
9035{
9036 struct i40e_vsi *vsi = NULL;
9037 struct i40e_pf *pf;
9038 int i, n = 0;
9039
9040 pf = veb->pf;
9041
9042 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +00009043 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009044 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
9045 n++;
9046 vsi = pf->vsi[i];
9047 }
9048 }
9049 if (n != 1) {
9050 dev_info(&pf->pdev->dev,
9051 "can't remove VEB %d with %d VSIs left\n",
9052 veb->seid, n);
9053 return;
9054 }
9055
9056 /* move the remaining VSI to uplink veb */
9057 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
9058 if (veb->uplink_seid) {
9059 vsi->uplink_seid = veb->uplink_seid;
9060 if (veb->uplink_seid == pf->mac_seid)
9061 vsi->veb_idx = I40E_NO_VEB;
9062 else
9063 vsi->veb_idx = veb->veb_idx;
9064 } else {
9065 /* floating VEB */
9066 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
9067 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
9068 }
9069
9070 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
9071 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009072}
9073
9074/**
9075 * i40e_add_veb - create the VEB in the switch
9076 * @veb: the VEB to be instantiated
9077 * @vsi: the controlling VSI
9078 **/
9079static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
9080{
Greg Rose56747262013-11-28 06:39:37 +00009081 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00009082 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009083 int ret;
9084
9085 /* get a VEB from the hardware */
9086 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00009087 veb->enabled_tc, is_default,
9088 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009089 if (ret) {
9090 dev_info(&veb->pf->pdev->dev,
9091 "couldn't add VEB, err %d, aq_err %d\n",
9092 ret, veb->pf->hw.aq.asq_last_status);
9093 return -EPERM;
9094 }
9095
9096 /* get statistics counter */
9097 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
9098 &veb->stats_idx, NULL, NULL, NULL);
9099 if (ret) {
9100 dev_info(&veb->pf->pdev->dev,
9101 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
9102 ret, veb->pf->hw.aq.asq_last_status);
9103 return -EPERM;
9104 }
9105 ret = i40e_veb_get_bw_info(veb);
9106 if (ret) {
9107 dev_info(&veb->pf->pdev->dev,
9108 "couldn't get VEB bw info, err %d, aq_err %d\n",
9109 ret, veb->pf->hw.aq.asq_last_status);
9110 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
9111 return -ENOENT;
9112 }
9113
9114 vsi->uplink_seid = veb->seid;
9115 vsi->veb_idx = veb->idx;
9116 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9117
9118 return 0;
9119}
9120
9121/**
9122 * i40e_veb_setup - Set up a VEB
9123 * @pf: board private structure
9124 * @flags: VEB setup flags
9125 * @uplink_seid: the switch element to link to
9126 * @vsi_seid: the initial VSI seid
9127 * @enabled_tc: Enabled TC bit-map
9128 *
9129 * This allocates the sw VEB structure and links it into the switch
9130 * It is possible and legal for this to be a duplicate of an already
9131 * existing VEB. It is also possible for both uplink and vsi seids
9132 * to be zero, in order to create a floating VEB.
9133 *
9134 * Returns pointer to the successfully allocated VEB sw struct on
9135 * success, otherwise returns NULL on failure.
9136 **/
9137struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
9138 u16 uplink_seid, u16 vsi_seid,
9139 u8 enabled_tc)
9140{
9141 struct i40e_veb *veb, *uplink_veb = NULL;
9142 int vsi_idx, veb_idx;
9143 int ret;
9144
9145 /* if one seid is 0, the other must be 0 to create a floating relay */
9146 if ((uplink_seid == 0 || vsi_seid == 0) &&
9147 (uplink_seid + vsi_seid != 0)) {
9148 dev_info(&pf->pdev->dev,
9149 "one, not both seid's are 0: uplink=%d vsi=%d\n",
9150 uplink_seid, vsi_seid);
9151 return NULL;
9152 }
9153
9154 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +00009155 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009156 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
9157 break;
Mitch Williams505682c2014-05-20 08:01:37 +00009158 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009159 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
9160 vsi_seid);
9161 return NULL;
9162 }
9163
9164 if (uplink_seid && uplink_seid != pf->mac_seid) {
9165 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
9166 if (pf->veb[veb_idx] &&
9167 pf->veb[veb_idx]->seid == uplink_seid) {
9168 uplink_veb = pf->veb[veb_idx];
9169 break;
9170 }
9171 }
9172 if (!uplink_veb) {
9173 dev_info(&pf->pdev->dev,
9174 "uplink seid %d not found\n", uplink_seid);
9175 return NULL;
9176 }
9177 }
9178
9179 /* get veb sw struct */
9180 veb_idx = i40e_veb_mem_alloc(pf);
9181 if (veb_idx < 0)
9182 goto err_alloc;
9183 veb = pf->veb[veb_idx];
9184 veb->flags = flags;
9185 veb->uplink_seid = uplink_seid;
9186 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
9187 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
9188
9189 /* create the VEB in the switch */
9190 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
9191 if (ret)
9192 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +00009193 if (vsi_idx == pf->lan_vsi)
9194 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009195
9196 return veb;
9197
9198err_veb:
9199 i40e_veb_clear(veb);
9200err_alloc:
9201 return NULL;
9202}
9203
9204/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00009205 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009206 * @pf: board private structure
9207 * @ele: element we are building info from
9208 * @num_reported: total number of elements
9209 * @printconfig: should we print the contents
9210 *
9211 * helper function to assist in extracting a few useful SEID values.
9212 **/
9213static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
9214 struct i40e_aqc_switch_config_element_resp *ele,
9215 u16 num_reported, bool printconfig)
9216{
9217 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
9218 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
9219 u8 element_type = ele->element_type;
9220 u16 seid = le16_to_cpu(ele->seid);
9221
9222 if (printconfig)
9223 dev_info(&pf->pdev->dev,
9224 "type=%d seid=%d uplink=%d downlink=%d\n",
9225 element_type, seid, uplink_seid, downlink_seid);
9226
9227 switch (element_type) {
9228 case I40E_SWITCH_ELEMENT_TYPE_MAC:
9229 pf->mac_seid = seid;
9230 break;
9231 case I40E_SWITCH_ELEMENT_TYPE_VEB:
9232 /* Main VEB? */
9233 if (uplink_seid != pf->mac_seid)
9234 break;
9235 if (pf->lan_veb == I40E_NO_VEB) {
9236 int v;
9237
9238 /* find existing or else empty VEB */
9239 for (v = 0; v < I40E_MAX_VEB; v++) {
9240 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
9241 pf->lan_veb = v;
9242 break;
9243 }
9244 }
9245 if (pf->lan_veb == I40E_NO_VEB) {
9246 v = i40e_veb_mem_alloc(pf);
9247 if (v < 0)
9248 break;
9249 pf->lan_veb = v;
9250 }
9251 }
9252
9253 pf->veb[pf->lan_veb]->seid = seid;
9254 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
9255 pf->veb[pf->lan_veb]->pf = pf;
9256 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
9257 break;
9258 case I40E_SWITCH_ELEMENT_TYPE_VSI:
9259 if (num_reported != 1)
9260 break;
9261 /* This is immediately after a reset so we can assume this is
9262 * the PF's VSI
9263 */
9264 pf->mac_seid = uplink_seid;
9265 pf->pf_seid = downlink_seid;
9266 pf->main_vsi_seid = seid;
9267 if (printconfig)
9268 dev_info(&pf->pdev->dev,
9269 "pf_seid=%d main_vsi_seid=%d\n",
9270 pf->pf_seid, pf->main_vsi_seid);
9271 break;
9272 case I40E_SWITCH_ELEMENT_TYPE_PF:
9273 case I40E_SWITCH_ELEMENT_TYPE_VF:
9274 case I40E_SWITCH_ELEMENT_TYPE_EMP:
9275 case I40E_SWITCH_ELEMENT_TYPE_BMC:
9276 case I40E_SWITCH_ELEMENT_TYPE_PE:
9277 case I40E_SWITCH_ELEMENT_TYPE_PA:
9278 /* ignore these for now */
9279 break;
9280 default:
9281 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
9282 element_type, seid);
9283 break;
9284 }
9285}
9286
9287/**
9288 * i40e_fetch_switch_configuration - Get switch config from firmware
9289 * @pf: board private structure
9290 * @printconfig: should we print the contents
9291 *
9292 * Get the current switch configuration from the device and
9293 * extract a few useful SEID values.
9294 **/
9295int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
9296{
9297 struct i40e_aqc_get_switch_config_resp *sw_config;
9298 u16 next_seid = 0;
9299 int ret = 0;
9300 u8 *aq_buf;
9301 int i;
9302
9303 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
9304 if (!aq_buf)
9305 return -ENOMEM;
9306
9307 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
9308 do {
9309 u16 num_reported, num_total;
9310
9311 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
9312 I40E_AQ_LARGE_BUF,
9313 &next_seid, NULL);
9314 if (ret) {
9315 dev_info(&pf->pdev->dev,
9316 "get switch config failed %d aq_err=%x\n",
9317 ret, pf->hw.aq.asq_last_status);
9318 kfree(aq_buf);
9319 return -ENOENT;
9320 }
9321
9322 num_reported = le16_to_cpu(sw_config->header.num_reported);
9323 num_total = le16_to_cpu(sw_config->header.num_total);
9324
9325 if (printconfig)
9326 dev_info(&pf->pdev->dev,
9327 "header: %d reported %d total\n",
9328 num_reported, num_total);
9329
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009330 for (i = 0; i < num_reported; i++) {
9331 struct i40e_aqc_switch_config_element_resp *ele =
9332 &sw_config->element[i];
9333
9334 i40e_setup_pf_switch_element(pf, ele, num_reported,
9335 printconfig);
9336 }
9337 } while (next_seid != 0);
9338
9339 kfree(aq_buf);
9340 return ret;
9341}
9342
9343/**
9344 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
9345 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009346 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009347 *
9348 * Returns 0 on success, negative value on failure
9349 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009350static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009351{
9352 int ret;
9353
9354 /* find out what's out there already */
9355 ret = i40e_fetch_switch_configuration(pf, false);
9356 if (ret) {
9357 dev_info(&pf->pdev->dev,
9358 "couldn't fetch switch config, err %d, aq_err %d\n",
9359 ret, pf->hw.aq.asq_last_status);
9360 return ret;
9361 }
9362 i40e_pf_reset_stats(pf);
9363
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009364 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009365 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009366 struct i40e_vsi *vsi = NULL;
9367 u16 uplink_seid;
9368
9369 /* Set up the PF VSI associated with the PF's main VSI
9370 * that is already in the HW switch
9371 */
9372 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
9373 uplink_seid = pf->veb[pf->lan_veb]->seid;
9374 else
9375 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009376 if (pf->lan_vsi == I40E_NO_VSI)
9377 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
9378 else if (reinit)
9379 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009380 if (!vsi) {
9381 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
9382 i40e_fdir_teardown(pf);
9383 return -EAGAIN;
9384 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009385 } else {
9386 /* force a reset of TC and queue layout configurations */
9387 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9388 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9389 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9390 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9391 }
9392 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
9393
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009394 i40e_fdir_sb_setup(pf);
9395
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009396 /* Setup static PF queue filter control settings */
9397 ret = i40e_setup_pf_filter_control(pf);
9398 if (ret) {
9399 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
9400 ret);
9401 /* Failure here should not stop continuing other steps */
9402 }
9403
9404 /* enable RSS in the HW, even for only one queue, as the stack can use
9405 * the hash
9406 */
9407 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
9408 i40e_config_rss(pf);
9409
9410 /* fill in link information and enable LSE reporting */
Catherine Sullivan21af70f2015-01-24 09:58:41 +00009411 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009412 i40e_link_event(pf);
9413
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00009414 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009415 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
9416 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00009417
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009418 i40e_ptp_init(pf);
9419
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009420 return ret;
9421}
9422
9423/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009424 * i40e_determine_queue_usage - Work out queue distribution
9425 * @pf: board private structure
9426 **/
9427static void i40e_determine_queue_usage(struct i40e_pf *pf)
9428{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009429 int queues_left;
9430
9431 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07009432#ifdef I40E_FCOE
9433 pf->num_fcoe_qps = 0;
9434#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009435
9436 /* Find the max queues to be put into basic use. We'll always be
9437 * using TC0, whether or not DCB is running, and TC0 will get the
9438 * big RSS set.
9439 */
9440 queues_left = pf->hw.func_caps.num_tx_qp;
9441
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009442 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +00009443 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009444 /* one qp for PF, no queues for anything else */
9445 queues_left = 0;
9446 pf->rss_size = pf->num_lan_qps = 1;
9447
9448 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009449 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07009450#ifdef I40E_FCOE
9451 I40E_FLAG_FCOE_ENABLED |
9452#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009453 I40E_FLAG_FD_SB_ENABLED |
9454 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009455 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009456 I40E_FLAG_SRIOV_ENABLED |
9457 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +00009458 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
9459 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +00009460 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009461 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +00009462 /* one qp for PF */
9463 pf->rss_size = pf->num_lan_qps = 1;
9464 queues_left -= pf->num_lan_qps;
9465
9466 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07009467#ifdef I40E_FCOE
9468 I40E_FLAG_FCOE_ENABLED |
9469#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +00009470 I40E_FLAG_FD_SB_ENABLED |
9471 I40E_FLAG_FD_ATR_ENABLED |
9472 I40E_FLAG_DCB_ENABLED |
9473 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009474 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009475 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009476 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009477 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009478 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009479 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
9480 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00009481 pf->num_lan_qps = max_t(int, pf->rss_size_max,
9482 num_online_cpus());
9483 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
9484 pf->hw.func_caps.num_tx_qp);
9485
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009486 queues_left -= pf->num_lan_qps;
9487 }
9488
Vasu Dev38e00432014-08-01 13:27:03 -07009489#ifdef I40E_FCOE
9490 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
9491 if (I40E_DEFAULT_FCOE <= queues_left) {
9492 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
9493 } else if (I40E_MINIMUM_FCOE <= queues_left) {
9494 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
9495 } else {
9496 pf->num_fcoe_qps = 0;
9497 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
9498 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
9499 }
9500
9501 queues_left -= pf->num_fcoe_qps;
9502 }
9503
9504#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009505 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9506 if (queues_left > 1) {
9507 queues_left -= 1; /* save 1 queue for FD */
9508 } else {
9509 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9510 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
9511 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009512 }
9513
9514 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
9515 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009516 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
9517 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009518 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
9519 }
9520
9521 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
9522 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
9523 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
9524 (queues_left / pf->num_vmdq_qps));
9525 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
9526 }
9527
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009528 pf->queues_left = queues_left;
Vasu Dev38e00432014-08-01 13:27:03 -07009529#ifdef I40E_FCOE
9530 dev_info(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
9531#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009532}
9533
9534/**
9535 * i40e_setup_pf_filter_control - Setup PF static filter control
9536 * @pf: PF to be setup
9537 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00009538 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009539 * settings. If PE/FCoE are enabled then it will also set the per PF
9540 * based filter sizes required for them. It also enables Flow director,
9541 * ethertype and macvlan type filter settings for the pf.
9542 *
9543 * Returns 0 on success, negative on failure
9544 **/
9545static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
9546{
9547 struct i40e_filter_control_settings *settings = &pf->filter_settings;
9548
9549 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
9550
9551 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08009552 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009553 settings->enable_fdir = true;
9554
9555 /* Ethtype and MACVLAN filters enabled for PF */
9556 settings->enable_ethtype = true;
9557 settings->enable_macvlan = true;
9558
9559 if (i40e_set_filter_control(&pf->hw, settings))
9560 return -ENOENT;
9561
9562 return 0;
9563}
9564
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009565#define INFO_STRING_LEN 255
9566static void i40e_print_features(struct i40e_pf *pf)
9567{
9568 struct i40e_hw *hw = &pf->hw;
9569 char *buf, *string;
9570
9571 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
9572 if (!string) {
9573 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
9574 return;
9575 }
9576
9577 buf = string;
9578
9579 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
9580#ifdef CONFIG_PCI_IOV
9581 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
9582#endif
Mitch Williamsaba237d2015-02-06 08:52:17 +00009583 buf += sprintf(buf, "VSIs: %d QP: %d RX: %s ",
9584 pf->hw.func_caps.num_vsis,
9585 pf->vsi[pf->lan_vsi]->num_queue_pairs,
9586 pf->flags & I40E_FLAG_RX_PS_ENABLED ? "PS" : "1BUF");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009587
9588 if (pf->flags & I40E_FLAG_RSS_ENABLED)
9589 buf += sprintf(buf, "RSS ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009590 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00009591 buf += sprintf(buf, "FD_ATR ");
9592 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
9593 buf += sprintf(buf, "FD_SB ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009594 buf += sprintf(buf, "NTUPLE ");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00009595 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009596 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009597 buf += sprintf(buf, "DCB ");
9598 if (pf->flags & I40E_FLAG_PTP)
9599 buf += sprintf(buf, "PTP ");
Vasu Dev38e00432014-08-01 13:27:03 -07009600#ifdef I40E_FCOE
9601 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
9602 buf += sprintf(buf, "FCOE ");
9603#endif
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009604
9605 BUG_ON(buf > (string + INFO_STRING_LEN));
9606 dev_info(&pf->pdev->dev, "%s\n", string);
9607 kfree(string);
9608}
9609
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009610/**
9611 * i40e_probe - Device initialization routine
9612 * @pdev: PCI device information struct
9613 * @ent: entry in i40e_pci_tbl
9614 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00009615 * i40e_probe initializes a PF identified by a pci_dev structure.
9616 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009617 * and a hardware reset occur.
9618 *
9619 * Returns 0 on success, negative on failure
9620 **/
9621static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
9622{
Catherine Sullivane8278452015-02-06 08:52:08 +00009623 struct i40e_aq_get_phy_abilities_resp abilities;
Jeff Kirshere8156652015-03-05 19:02:35 -08009624 unsigned long ioremap_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009625 struct i40e_pf *pf;
9626 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00009627 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00009628 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009629 int err = 0;
9630 u32 len;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00009631 u32 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009632
9633 err = pci_enable_device_mem(pdev);
9634 if (err)
9635 return err;
9636
9637 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +00009638 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00009639 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00009640 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
9641 if (err) {
9642 dev_err(&pdev->dev,
9643 "DMA configuration failed: 0x%x\n", err);
9644 goto err_dma;
9645 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009646 }
9647
9648 /* set up pci connections */
9649 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
9650 IORESOURCE_MEM), i40e_driver_name);
9651 if (err) {
9652 dev_info(&pdev->dev,
9653 "pci_request_selected_regions failed %d\n", err);
9654 goto err_pci_reg;
9655 }
9656
9657 pci_enable_pcie_error_reporting(pdev);
9658 pci_set_master(pdev);
9659
9660 /* Now that we have a PCI connection, we need to do the
9661 * low level device setup. This is primarily setting up
9662 * the Admin Queue structures and then querying for the
9663 * device's current profile information.
9664 */
9665 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
9666 if (!pf) {
9667 err = -ENOMEM;
9668 goto err_pf_alloc;
9669 }
9670 pf->next_vsi = 0;
9671 pf->pdev = pdev;
9672 set_bit(__I40E_DOWN, &pf->state);
9673
9674 hw = &pf->hw;
9675 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +00009676
Jeff Kirshere8156652015-03-05 19:02:35 -08009677 ioremap_len = min_t(unsigned long, pci_resource_len(pdev, 0),
Anjali Singhai232f4702015-02-26 16:15:39 +00009678 I40E_MAX_CSR_SPACE);
9679
9680 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009681 if (!hw->hw_addr) {
9682 err = -EIO;
9683 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
9684 (unsigned int)pci_resource_start(pdev, 0),
9685 (unsigned int)pci_resource_len(pdev, 0), err);
9686 goto err_ioremap;
9687 }
9688 hw->vendor_id = pdev->vendor;
9689 hw->device_id = pdev->device;
9690 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
9691 hw->subsystem_vendor_id = pdev->subsystem_vendor;
9692 hw->subsystem_device_id = pdev->subsystem_device;
9693 hw->bus.device = PCI_SLOT(pdev->devfn);
9694 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00009695 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009696
Shannon Nelson5b5faa42014-10-17 03:14:51 +00009697 if (debug != -1) {
9698 pf->msg_enable = pf->hw.debug_mask;
9699 pf->msg_enable = debug;
9700 }
9701
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00009702 /* do a special CORER for clearing PXE mode once at init */
9703 if (hw->revision_id == 0 &&
9704 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
9705 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
9706 i40e_flush(hw);
9707 msleep(200);
9708 pf->corer_count++;
9709
9710 i40e_clear_pxe_mode(hw);
9711 }
9712
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009713 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +00009714 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009715 err = i40e_pf_reset(hw);
9716 if (err) {
9717 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
9718 goto err_pf_reset;
9719 }
9720 pf->pfr_count++;
9721
9722 hw->aq.num_arq_entries = I40E_AQ_LEN;
9723 hw->aq.num_asq_entries = I40E_AQ_LEN;
9724 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
9725 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
9726 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00009727
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00009728 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00009729 "%s-%s:misc",
9730 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009731
9732 err = i40e_init_shared_code(hw);
9733 if (err) {
9734 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
9735 goto err_pf_reset;
9736 }
9737
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00009738 /* set up a default setting for link flow control */
9739 pf->hw.fc.requested_mode = I40E_FC_NONE;
9740
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009741 err = i40e_init_adminq(hw);
9742 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
9743 if (err) {
9744 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +00009745 "The driver for the device stopped because the NVM image is newer than expected. You must install the most recent version of the network driver.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009746 goto err_pf_reset;
9747 }
9748
Catherine Sullivan7aa67612014-07-09 07:46:17 +00009749 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
9750 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +00009751 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +00009752 "The driver for the device detected a newer version of the NVM image than expected. Please install the most recent version of the network driver.\n");
9753 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
9754 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +00009755 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +00009756 "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
Shannon Nelson278b6f62014-06-04 01:41:03 +00009757
Shannon Nelson4eb3f762014-03-06 08:59:58 +00009758 i40e_verify_eeprom(pf);
9759
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +00009760 /* Rev 0 hardware was never productized */
9761 if (hw->revision_id < 1)
9762 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");
9763
Shannon Nelson6ff4ef82013-12-21 05:44:49 +00009764 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009765 err = i40e_get_capabilities(pf);
9766 if (err)
9767 goto err_adminq_setup;
9768
9769 err = i40e_sw_init(pf);
9770 if (err) {
9771 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
9772 goto err_sw_init;
9773 }
9774
9775 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
9776 hw->func_caps.num_rx_qp,
9777 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
9778 if (err) {
9779 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
9780 goto err_init_lan_hmc;
9781 }
9782
9783 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
9784 if (err) {
9785 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
9786 err = -ENOENT;
9787 goto err_configure_lan_hmc;
9788 }
9789
Neerav Parikhb686ece2014-12-14 01:55:11 +00009790 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
9791 * Ignore error return codes because if it was already disabled via
9792 * hardware settings this will fail
9793 */
9794 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
9795 (pf->hw.aq.fw_maj_ver < 4)) {
9796 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
9797 i40e_aq_stop_lldp(hw, true, NULL);
9798 }
9799
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009800 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00009801 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009802 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
9803 err = -EIO;
9804 goto err_mac_addr;
9805 }
9806 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +00009807 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009808 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
9809 if (is_valid_ether_addr(hw->mac.port_addr))
9810 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -07009811#ifdef I40E_FCOE
9812 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
9813 if (err)
9814 dev_info(&pdev->dev,
9815 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
9816 if (!is_valid_ether_addr(hw->mac.san_addr)) {
9817 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
9818 hw->mac.san_addr);
9819 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
9820 }
9821 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
9822#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009823
9824 pci_set_drvdata(pdev, pf);
9825 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009826#ifdef CONFIG_I40E_DCB
9827 err = i40e_init_pf_dcb(pf);
9828 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00009829 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
Neerav Parikh4d9b6042014-05-22 06:31:51 +00009830 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Neerav Parikh014269f2014-04-01 07:11:48 +00009831 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009832 }
9833#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009834
9835 /* set up periodic task facility */
9836 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
9837 pf->service_timer_period = HZ;
9838
9839 INIT_WORK(&pf->service_task, i40e_service_task);
9840 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
9841 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
9842 pf->link_check_timeout = jiffies;
9843
Shannon Nelson8e2773a2013-11-28 06:39:22 +00009844 /* WoL defaults to disabled */
9845 pf->wol_en = false;
9846 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
9847
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009848 /* set up the main switch operations */
9849 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04009850 err = i40e_init_interrupt_scheme(pf);
9851 if (err)
9852 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009853
Mitch Williams505682c2014-05-20 08:01:37 +00009854 /* The number of VSIs reported by the FW is the minimum guaranteed
9855 * to us; HW supports far more and we share the remaining pool with
9856 * the other PFs. We allocate space for more than the guarantee with
9857 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009858 */
Mitch Williams505682c2014-05-20 08:01:37 +00009859 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
9860 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
9861 else
9862 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
9863
9864 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
9865 len = sizeof(struct i40e_vsi *) * pf->num_alloc_vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009866 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00009867 if (!pf->vsi) {
9868 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009869 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00009870 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009871
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009872 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009873 if (err) {
9874 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
9875 goto err_vsis;
9876 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00009877 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +00009878 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00009879 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
9880 i40e_vsi_open(pf->vsi[i]);
9881 break;
9882 }
9883 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009884
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00009885 /* driver is only interested in link up/down and module qualification
9886 * reports from firmware
9887 */
9888 err = i40e_aq_set_phy_int_mask(&pf->hw,
9889 I40E_AQ_EVENT_LINK_UPDOWN |
9890 I40E_AQ_EVENT_MODULE_QUAL_FAIL, NULL);
9891 if (err)
9892 dev_info(&pf->pdev->dev, "set phy mask fail, aq_err %d\n", err);
9893
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00009894 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
9895 (pf->hw.aq.fw_maj_ver < 4)) {
9896 msleep(75);
9897 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
9898 if (err)
9899 dev_info(&pf->pdev->dev, "link restart failed, aq_err=%d\n",
9900 pf->hw.aq.asq_last_status);
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00009901 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009902 /* The main driver is (mostly) up and happy. We need to set this state
9903 * before setting up the misc vector or we get a race and the vector
9904 * ends up disabled forever.
9905 */
9906 clear_bit(__I40E_DOWN, &pf->state);
9907
9908 /* In case of MSIX we are going to setup the misc vector right here
9909 * to handle admin queue events etc. In case of legacy and MSI
9910 * the misc functionality and queue processing is combined in
9911 * the same vector and that gets setup at open.
9912 */
9913 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
9914 err = i40e_setup_misc_vector(pf);
9915 if (err) {
9916 dev_info(&pdev->dev,
9917 "setup of misc vector failed: %d\n", err);
9918 goto err_vsis;
9919 }
9920 }
9921
Greg Rosedf805f62014-04-04 04:43:16 +00009922#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009923 /* prep for VF support */
9924 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +00009925 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
9926 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009927 u32 val;
9928
9929 /* disable link interrupts for VFs */
9930 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
9931 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
9932 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
9933 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -08009934
9935 if (pci_num_vf(pdev)) {
9936 dev_info(&pdev->dev,
9937 "Active VFs found, allocating resources.\n");
9938 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
9939 if (err)
9940 dev_info(&pdev->dev,
9941 "Error %d allocating resources for existing VFs\n",
9942 err);
9943 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009944 }
Greg Rosedf805f62014-04-04 04:43:16 +00009945#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009946
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00009947 pfs_found++;
9948
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009949 i40e_dbg_pf_init(pf);
9950
9951 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00009952 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009953
9954 /* since everything's happy, start the service_task timer */
9955 mod_timer(&pf->service_timer,
9956 round_jiffies(jiffies + pf->service_timer_period));
9957
Vasu Dev38e00432014-08-01 13:27:03 -07009958#ifdef I40E_FCOE
9959 /* create FCoE interface */
9960 i40e_fcoe_vsi_setup(pf);
9961
9962#endif
Catherine Sullivand4dfb812013-11-28 06:39:21 +00009963 /* Get the negotiated link width and speed from PCI config space */
9964 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
9965
9966 i40e_set_pci_config_data(hw, link_status);
9967
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00009968 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +00009969 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
9970 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
9971 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
9972 "Unknown"),
9973 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
9974 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
9975 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
9976 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
9977 "Unknown"));
9978
9979 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
9980 hw->bus.speed < i40e_bus_speed_8000) {
9981 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
9982 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
9983 }
9984
Catherine Sullivane8278452015-02-06 08:52:08 +00009985 /* get the requested speeds from the fw */
9986 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
9987 if (err)
9988 dev_info(&pf->pdev->dev, "get phy abilities failed, aq_err %d, advertised speed settings may not be correct\n",
9989 err);
9990 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
9991
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00009992 /* print a string summarizing features */
9993 i40e_print_features(pf);
9994
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009995 return 0;
9996
9997 /* Unwind what we've done if something failed in the setup */
9998err_vsis:
9999 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010000 i40e_clear_interrupt_scheme(pf);
10001 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000010002err_switch_setup:
10003 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010004 del_timer_sync(&pf->service_timer);
10005err_mac_addr:
10006err_configure_lan_hmc:
10007 (void)i40e_shutdown_lan_hmc(hw);
10008err_init_lan_hmc:
10009 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010010err_sw_init:
10011err_adminq_setup:
10012 (void)i40e_shutdown_adminq(hw);
10013err_pf_reset:
10014 iounmap(hw->hw_addr);
10015err_ioremap:
10016 kfree(pf);
10017err_pf_alloc:
10018 pci_disable_pcie_error_reporting(pdev);
10019 pci_release_selected_regions(pdev,
10020 pci_select_bars(pdev, IORESOURCE_MEM));
10021err_pci_reg:
10022err_dma:
10023 pci_disable_device(pdev);
10024 return err;
10025}
10026
10027/**
10028 * i40e_remove - Device removal routine
10029 * @pdev: PCI device information struct
10030 *
10031 * i40e_remove is called by the PCI subsystem to alert the driver
10032 * that is should release a PCI device. This could be caused by a
10033 * Hot-Plug event, or because the driver is going to be removed from
10034 * memory.
10035 **/
10036static void i40e_remove(struct pci_dev *pdev)
10037{
10038 struct i40e_pf *pf = pci_get_drvdata(pdev);
10039 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010040 int i;
10041
10042 i40e_dbg_pf_exit(pf);
10043
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010044 i40e_ptp_stop(pf);
10045
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010046 /* no more scheduling of any task */
10047 set_bit(__I40E_DOWN, &pf->state);
10048 del_timer_sync(&pf->service_timer);
10049 cancel_work_sync(&pf->service_task);
Mitch A Williams33c62b32015-02-21 06:44:51 +000010050 i40e_fdir_teardown(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010051
Mitch Williamseb2d80b2014-02-13 03:48:48 -080010052 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
10053 i40e_free_vfs(pf);
10054 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
10055 }
10056
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010057 i40e_fdir_teardown(pf);
10058
10059 /* If there is a switch structure or any orphans, remove them.
10060 * This will leave only the PF's VSI remaining.
10061 */
10062 for (i = 0; i < I40E_MAX_VEB; i++) {
10063 if (!pf->veb[i])
10064 continue;
10065
10066 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
10067 pf->veb[i]->uplink_seid == 0)
10068 i40e_switch_branch_release(pf->veb[i]);
10069 }
10070
10071 /* Now we can shutdown the PF's VSI, just before we kill
10072 * adminq and hmc.
10073 */
10074 if (pf->vsi[pf->lan_vsi])
10075 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
10076
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010077 /* shutdown and destroy the HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +000010078 if (pf->hw.hmc.hmc_obj) {
10079 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
10080 if (ret_code)
10081 dev_warn(&pdev->dev,
10082 "Failed to destroy the HMC resources: %d\n",
10083 ret_code);
10084 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010085
10086 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010087 ret_code = i40e_shutdown_adminq(&pf->hw);
10088 if (ret_code)
10089 dev_warn(&pdev->dev,
10090 "Failed to destroy the Admin Queue resources: %d\n",
10091 ret_code);
10092
10093 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
10094 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000010095 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010096 if (pf->vsi[i]) {
10097 i40e_vsi_clear_rings(pf->vsi[i]);
10098 i40e_vsi_clear(pf->vsi[i]);
10099 pf->vsi[i] = NULL;
10100 }
10101 }
10102
10103 for (i = 0; i < I40E_MAX_VEB; i++) {
10104 kfree(pf->veb[i]);
10105 pf->veb[i] = NULL;
10106 }
10107
10108 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010109 kfree(pf->vsi);
10110
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010111 iounmap(pf->hw.hw_addr);
10112 kfree(pf);
10113 pci_release_selected_regions(pdev,
10114 pci_select_bars(pdev, IORESOURCE_MEM));
10115
10116 pci_disable_pcie_error_reporting(pdev);
10117 pci_disable_device(pdev);
10118}
10119
10120/**
10121 * i40e_pci_error_detected - warning that something funky happened in PCI land
10122 * @pdev: PCI device information struct
10123 *
10124 * Called to warn that something happened and the error handling steps
10125 * are in progress. Allows the driver to quiesce things, be ready for
10126 * remediation.
10127 **/
10128static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
10129 enum pci_channel_state error)
10130{
10131 struct i40e_pf *pf = pci_get_drvdata(pdev);
10132
10133 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
10134
10135 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010136 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
10137 rtnl_lock();
10138 i40e_prep_for_reset(pf);
10139 rtnl_unlock();
10140 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010141
10142 /* Request a slot reset */
10143 return PCI_ERS_RESULT_NEED_RESET;
10144}
10145
10146/**
10147 * i40e_pci_error_slot_reset - a PCI slot reset just happened
10148 * @pdev: PCI device information struct
10149 *
10150 * Called to find if the driver can work with the device now that
10151 * the pci slot has been reset. If a basic connection seems good
10152 * (registers are readable and have sane content) then return a
10153 * happy little PCI_ERS_RESULT_xxx.
10154 **/
10155static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
10156{
10157 struct i40e_pf *pf = pci_get_drvdata(pdev);
10158 pci_ers_result_t result;
10159 int err;
10160 u32 reg;
10161
10162 dev_info(&pdev->dev, "%s\n", __func__);
10163 if (pci_enable_device_mem(pdev)) {
10164 dev_info(&pdev->dev,
10165 "Cannot re-enable PCI device after reset.\n");
10166 result = PCI_ERS_RESULT_DISCONNECT;
10167 } else {
10168 pci_set_master(pdev);
10169 pci_restore_state(pdev);
10170 pci_save_state(pdev);
10171 pci_wake_from_d3(pdev, false);
10172
10173 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
10174 if (reg == 0)
10175 result = PCI_ERS_RESULT_RECOVERED;
10176 else
10177 result = PCI_ERS_RESULT_DISCONNECT;
10178 }
10179
10180 err = pci_cleanup_aer_uncorrect_error_status(pdev);
10181 if (err) {
10182 dev_info(&pdev->dev,
10183 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
10184 err);
10185 /* non-fatal, continue */
10186 }
10187
10188 return result;
10189}
10190
10191/**
10192 * i40e_pci_error_resume - restart operations after PCI error recovery
10193 * @pdev: PCI device information struct
10194 *
10195 * Called to allow the driver to bring things back up after PCI error
10196 * and/or reset recovery has finished.
10197 **/
10198static void i40e_pci_error_resume(struct pci_dev *pdev)
10199{
10200 struct i40e_pf *pf = pci_get_drvdata(pdev);
10201
10202 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010203 if (test_bit(__I40E_SUSPENDED, &pf->state))
10204 return;
10205
10206 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010207 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010208 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010209}
10210
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010211/**
10212 * i40e_shutdown - PCI callback for shutting down
10213 * @pdev: PCI device information struct
10214 **/
10215static void i40e_shutdown(struct pci_dev *pdev)
10216{
10217 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010218 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010219
10220 set_bit(__I40E_SUSPENDED, &pf->state);
10221 set_bit(__I40E_DOWN, &pf->state);
10222 rtnl_lock();
10223 i40e_prep_for_reset(pf);
10224 rtnl_unlock();
10225
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010226 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
10227 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
10228
Shannon Nelsone1477582015-02-21 06:44:33 +000010229 i40e_clear_interrupt_scheme(pf);
10230
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010231 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010232 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010233 pci_set_power_state(pdev, PCI_D3hot);
10234 }
10235}
10236
10237#ifdef CONFIG_PM
10238/**
10239 * i40e_suspend - PCI callback for moving to D3
10240 * @pdev: PCI device information struct
10241 **/
10242static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
10243{
10244 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010245 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010246
10247 set_bit(__I40E_SUSPENDED, &pf->state);
10248 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams88086e5d2015-01-09 11:18:19 +000010249 del_timer_sync(&pf->service_timer);
10250 cancel_work_sync(&pf->service_task);
Mitch Williams3932dbf2015-03-31 00:45:04 -070010251 i40e_fdir_teardown(pf);
10252
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010253 rtnl_lock();
10254 i40e_prep_for_reset(pf);
10255 rtnl_unlock();
10256
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010257 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
10258 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
10259
10260 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010261 pci_set_power_state(pdev, PCI_D3hot);
10262
10263 return 0;
10264}
10265
10266/**
10267 * i40e_resume - PCI callback for waking up from D3
10268 * @pdev: PCI device information struct
10269 **/
10270static int i40e_resume(struct pci_dev *pdev)
10271{
10272 struct i40e_pf *pf = pci_get_drvdata(pdev);
10273 u32 err;
10274
10275 pci_set_power_state(pdev, PCI_D0);
10276 pci_restore_state(pdev);
10277 /* pci_restore_state() clears dev->state_saves, so
10278 * call pci_save_state() again to restore it.
10279 */
10280 pci_save_state(pdev);
10281
10282 err = pci_enable_device_mem(pdev);
10283 if (err) {
10284 dev_err(&pdev->dev,
10285 "%s: Cannot enable PCI device from suspend\n",
10286 __func__);
10287 return err;
10288 }
10289 pci_set_master(pdev);
10290
10291 /* no wakeup events while running */
10292 pci_wake_from_d3(pdev, false);
10293
10294 /* handling the reset will rebuild the device state */
10295 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
10296 clear_bit(__I40E_DOWN, &pf->state);
10297 rtnl_lock();
10298 i40e_reset_and_rebuild(pf, false);
10299 rtnl_unlock();
10300 }
10301
10302 return 0;
10303}
10304
10305#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010306static const struct pci_error_handlers i40e_err_handler = {
10307 .error_detected = i40e_pci_error_detected,
10308 .slot_reset = i40e_pci_error_slot_reset,
10309 .resume = i40e_pci_error_resume,
10310};
10311
10312static struct pci_driver i40e_driver = {
10313 .name = i40e_driver_name,
10314 .id_table = i40e_pci_tbl,
10315 .probe = i40e_probe,
10316 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000010317#ifdef CONFIG_PM
10318 .suspend = i40e_suspend,
10319 .resume = i40e_resume,
10320#endif
10321 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010322 .err_handler = &i40e_err_handler,
10323 .sriov_configure = i40e_pci_sriov_configure,
10324};
10325
10326/**
10327 * i40e_init_module - Driver registration routine
10328 *
10329 * i40e_init_module is the first routine called when the driver is
10330 * loaded. All it does is register with the PCI subsystem.
10331 **/
10332static int __init i40e_init_module(void)
10333{
10334 pr_info("%s: %s - version %s\n", i40e_driver_name,
10335 i40e_driver_string, i40e_driver_version_str);
10336 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000010337
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010338 i40e_dbg_init();
10339 return pci_register_driver(&i40e_driver);
10340}
10341module_init(i40e_init_module);
10342
10343/**
10344 * i40e_exit_module - Driver exit cleanup routine
10345 *
10346 * i40e_exit_module is called just before the driver is removed
10347 * from memory.
10348 **/
10349static void __exit i40e_exit_module(void)
10350{
10351 pci_unregister_driver(&i40e_driver);
10352 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010353}
10354module_exit(i40e_exit_module);