xref: /openbmc/linux/drivers/net/ethernet/intel/fm10k/fm10k_pci.c (revision 28efb0046512e8a13ed9f9bdf0d68d10bbfbe9cf)
1 /* Intel(R) Ethernet Switch Host Interface Driver
2  * Copyright(c) 2013 - 2017 Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * The full GNU General Public License is included in this distribution in
14  * the file called "COPYING".
15  *
16  * Contact Information:
17  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
18  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
19  */
20 
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/aer.h>
24 
25 #include "fm10k.h"
26 
27 static const struct fm10k_info *fm10k_info_tbl[] = {
28 	[fm10k_device_pf] = &fm10k_pf_info,
29 	[fm10k_device_vf] = &fm10k_vf_info,
30 };
31 
32 /**
33  * fm10k_pci_tbl - PCI Device ID Table
34  *
35  * Wildcard entries (PCI_ANY_ID) should come last
36  * Last entry must be all 0s
37  *
38  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
39  *   Class, Class Mask, private data (not used) }
40  */
41 static const struct pci_device_id fm10k_pci_tbl[] = {
42 	{ PCI_VDEVICE(INTEL, FM10K_DEV_ID_PF), fm10k_device_pf },
43 	{ PCI_VDEVICE(INTEL, FM10K_DEV_ID_VF), fm10k_device_vf },
44 	/* required last entry */
45 	{ 0, }
46 };
47 MODULE_DEVICE_TABLE(pci, fm10k_pci_tbl);
48 
49 u16 fm10k_read_pci_cfg_word(struct fm10k_hw *hw, u32 reg)
50 {
51 	struct fm10k_intfc *interface = hw->back;
52 	u16 value = 0;
53 
54 	if (FM10K_REMOVED(hw->hw_addr))
55 		return ~value;
56 
57 	pci_read_config_word(interface->pdev, reg, &value);
58 	if (value == 0xFFFF)
59 		fm10k_write_flush(hw);
60 
61 	return value;
62 }
63 
64 u32 fm10k_read_reg(struct fm10k_hw *hw, int reg)
65 {
66 	u32 __iomem *hw_addr = READ_ONCE(hw->hw_addr);
67 	u32 value = 0;
68 
69 	if (FM10K_REMOVED(hw_addr))
70 		return ~value;
71 
72 	value = readl(&hw_addr[reg]);
73 	if (!(~value) && (!reg || !(~readl(hw_addr)))) {
74 		struct fm10k_intfc *interface = hw->back;
75 		struct net_device *netdev = interface->netdev;
76 
77 		hw->hw_addr = NULL;
78 		netif_device_detach(netdev);
79 		netdev_err(netdev, "PCIe link lost, device now detached\n");
80 	}
81 
82 	return value;
83 }
84 
85 static int fm10k_hw_ready(struct fm10k_intfc *interface)
86 {
87 	struct fm10k_hw *hw = &interface->hw;
88 
89 	fm10k_write_flush(hw);
90 
91 	return FM10K_REMOVED(hw->hw_addr) ? -ENODEV : 0;
92 }
93 
94 /**
95  * fm10k_macvlan_schedule - Schedule MAC/VLAN queue task
96  * @interface: fm10k private interface structure
97  *
98  * Schedule the MAC/VLAN queue monitor task. If the MAC/VLAN task cannot be
99  * started immediately, request that it be restarted when possible.
100  */
101 void fm10k_macvlan_schedule(struct fm10k_intfc *interface)
102 {
103 	/* Avoid processing the MAC/VLAN queue when the service task is
104 	 * disabled, or when we're resetting the device.
105 	 */
106 	if (!test_bit(__FM10K_MACVLAN_DISABLE, interface->state) &&
107 	    !test_and_set_bit(__FM10K_MACVLAN_SCHED, interface->state)) {
108 		clear_bit(__FM10K_MACVLAN_REQUEST, interface->state);
109 		/* We delay the actual start of execution in order to allow
110 		 * multiple MAC/VLAN updates to accumulate before handling
111 		 * them, and to allow some time to let the mailbox drain
112 		 * between runs.
113 		 */
114 		queue_delayed_work(fm10k_workqueue,
115 				   &interface->macvlan_task, 10);
116 	} else {
117 		set_bit(__FM10K_MACVLAN_REQUEST, interface->state);
118 	}
119 }
120 
121 /**
122  * fm10k_stop_macvlan_task - Stop the MAC/VLAN queue monitor
123  * @interface: fm10k private interface structure
124  *
125  * Wait until the MAC/VLAN queue task has stopped, and cancel any future
126  * requests.
127  */
128 static void fm10k_stop_macvlan_task(struct fm10k_intfc *interface)
129 {
130 	/* Disable the MAC/VLAN work item */
131 	set_bit(__FM10K_MACVLAN_DISABLE, interface->state);
132 
133 	/* Make sure we waited until any current invocations have stopped */
134 	cancel_delayed_work_sync(&interface->macvlan_task);
135 
136 	/* We set the __FM10K_MACVLAN_SCHED bit when we schedule the task.
137 	 * However, it may not be unset of the MAC/VLAN task never actually
138 	 * got a chance to run. Since we've canceled the task here, and it
139 	 * cannot be rescheuled right now, we need to ensure the scheduled bit
140 	 * gets unset.
141 	 */
142 	clear_bit(__FM10K_MACVLAN_SCHED, interface->state);
143 }
144 
145 /**
146  * fm10k_resume_macvlan_task - Restart the MAC/VLAN queue monitor
147  * @interface: fm10k private interface structure
148  *
149  * Clear the __FM10K_MACVLAN_DISABLE bit and, if a request occurred, schedule
150  * the MAC/VLAN work monitor.
151  */
152 static void fm10k_resume_macvlan_task(struct fm10k_intfc *interface)
153 {
154 	/* Re-enable the MAC/VLAN work item */
155 	clear_bit(__FM10K_MACVLAN_DISABLE, interface->state);
156 
157 	/* We might have received a MAC/VLAN request while disabled. If so,
158 	 * kick off the queue now.
159 	 */
160 	if (test_bit(__FM10K_MACVLAN_REQUEST, interface->state))
161 		fm10k_macvlan_schedule(interface);
162 }
163 
164 void fm10k_service_event_schedule(struct fm10k_intfc *interface)
165 {
166 	if (!test_bit(__FM10K_SERVICE_DISABLE, interface->state) &&
167 	    !test_and_set_bit(__FM10K_SERVICE_SCHED, interface->state)) {
168 		clear_bit(__FM10K_SERVICE_REQUEST, interface->state);
169 		queue_work(fm10k_workqueue, &interface->service_task);
170 	} else {
171 		set_bit(__FM10K_SERVICE_REQUEST, interface->state);
172 	}
173 }
174 
175 static void fm10k_service_event_complete(struct fm10k_intfc *interface)
176 {
177 	WARN_ON(!test_bit(__FM10K_SERVICE_SCHED, interface->state));
178 
179 	/* flush memory to make sure state is correct before next watchog */
180 	smp_mb__before_atomic();
181 	clear_bit(__FM10K_SERVICE_SCHED, interface->state);
182 
183 	/* If a service event was requested since we started, immediately
184 	 * re-schedule now. This ensures we don't drop a request until the
185 	 * next timer event.
186 	 */
187 	if (test_bit(__FM10K_SERVICE_REQUEST, interface->state))
188 		fm10k_service_event_schedule(interface);
189 }
190 
191 static void fm10k_stop_service_event(struct fm10k_intfc *interface)
192 {
193 	set_bit(__FM10K_SERVICE_DISABLE, interface->state);
194 	cancel_work_sync(&interface->service_task);
195 
196 	/* It's possible that cancel_work_sync stopped the service task from
197 	 * running before it could actually start. In this case the
198 	 * __FM10K_SERVICE_SCHED bit will never be cleared. Since we know that
199 	 * the service task cannot be running at this point, we need to clear
200 	 * the scheduled bit, as otherwise the service task may never be
201 	 * restarted.
202 	 */
203 	clear_bit(__FM10K_SERVICE_SCHED, interface->state);
204 }
205 
206 static void fm10k_start_service_event(struct fm10k_intfc *interface)
207 {
208 	clear_bit(__FM10K_SERVICE_DISABLE, interface->state);
209 	fm10k_service_event_schedule(interface);
210 }
211 
212 /**
213  * fm10k_service_timer - Timer Call-back
214  * @data: pointer to interface cast into an unsigned long
215  **/
216 static void fm10k_service_timer(unsigned long data)
217 {
218 	struct fm10k_intfc *interface = (struct fm10k_intfc *)data;
219 
220 	/* Reset the timer */
221 	mod_timer(&interface->service_timer, (HZ * 2) + jiffies);
222 
223 	fm10k_service_event_schedule(interface);
224 }
225 
226 /**
227  * fm10k_prepare_for_reset - Prepare the driver and device for a pending reset
228  * @interface: fm10k private data structure
229  *
230  * This function prepares for a device reset by shutting as much down as we
231  * can. It does nothing and returns false if __FM10K_RESETTING was already set
232  * prior to calling this function. It returns true if it actually did work.
233  */
234 static bool fm10k_prepare_for_reset(struct fm10k_intfc *interface)
235 {
236 	struct net_device *netdev = interface->netdev;
237 
238 	WARN_ON(in_interrupt());
239 
240 	/* put off any impending NetWatchDogTimeout */
241 	netif_trans_update(netdev);
242 
243 	/* Nothing to do if a reset is already in progress */
244 	if (test_and_set_bit(__FM10K_RESETTING, interface->state))
245 		return false;
246 
247 	/* As the MAC/VLAN task will be accessing registers it must not be
248 	 * running while we reset. Although the task will not be scheduled
249 	 * once we start resetting it may already be running
250 	 */
251 	fm10k_stop_macvlan_task(interface);
252 
253 	rtnl_lock();
254 
255 	fm10k_iov_suspend(interface->pdev);
256 
257 	if (netif_running(netdev))
258 		fm10k_close(netdev);
259 
260 	fm10k_mbx_free_irq(interface);
261 
262 	/* free interrupts */
263 	fm10k_clear_queueing_scheme(interface);
264 
265 	/* delay any future reset requests */
266 	interface->last_reset = jiffies + (10 * HZ);
267 
268 	rtnl_unlock();
269 
270 	return true;
271 }
272 
273 static int fm10k_handle_reset(struct fm10k_intfc *interface)
274 {
275 	struct net_device *netdev = interface->netdev;
276 	struct fm10k_hw *hw = &interface->hw;
277 	int err;
278 
279 	WARN_ON(!test_bit(__FM10K_RESETTING, interface->state));
280 
281 	rtnl_lock();
282 
283 	pci_set_master(interface->pdev);
284 
285 	/* reset and initialize the hardware so it is in a known state */
286 	err = hw->mac.ops.reset_hw(hw);
287 	if (err) {
288 		dev_err(&interface->pdev->dev, "reset_hw failed: %d\n", err);
289 		goto reinit_err;
290 	}
291 
292 	err = hw->mac.ops.init_hw(hw);
293 	if (err) {
294 		dev_err(&interface->pdev->dev, "init_hw failed: %d\n", err);
295 		goto reinit_err;
296 	}
297 
298 	err = fm10k_init_queueing_scheme(interface);
299 	if (err) {
300 		dev_err(&interface->pdev->dev,
301 			"init_queueing_scheme failed: %d\n", err);
302 		goto reinit_err;
303 	}
304 
305 	/* re-associate interrupts */
306 	err = fm10k_mbx_request_irq(interface);
307 	if (err)
308 		goto err_mbx_irq;
309 
310 	err = fm10k_hw_ready(interface);
311 	if (err)
312 		goto err_open;
313 
314 	/* update hardware address for VFs if perm_addr has changed */
315 	if (hw->mac.type == fm10k_mac_vf) {
316 		if (is_valid_ether_addr(hw->mac.perm_addr)) {
317 			ether_addr_copy(hw->mac.addr, hw->mac.perm_addr);
318 			ether_addr_copy(netdev->perm_addr, hw->mac.perm_addr);
319 			ether_addr_copy(netdev->dev_addr, hw->mac.perm_addr);
320 			netdev->addr_assign_type &= ~NET_ADDR_RANDOM;
321 		}
322 
323 		if (hw->mac.vlan_override)
324 			netdev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
325 		else
326 			netdev->features |= NETIF_F_HW_VLAN_CTAG_RX;
327 	}
328 
329 	err = netif_running(netdev) ? fm10k_open(netdev) : 0;
330 	if (err)
331 		goto err_open;
332 
333 	fm10k_iov_resume(interface->pdev);
334 
335 	rtnl_unlock();
336 
337 	fm10k_resume_macvlan_task(interface);
338 
339 	clear_bit(__FM10K_RESETTING, interface->state);
340 
341 	return err;
342 err_open:
343 	fm10k_mbx_free_irq(interface);
344 err_mbx_irq:
345 	fm10k_clear_queueing_scheme(interface);
346 reinit_err:
347 	netif_device_detach(netdev);
348 
349 	rtnl_unlock();
350 
351 	clear_bit(__FM10K_RESETTING, interface->state);
352 
353 	return err;
354 }
355 
356 static void fm10k_detach_subtask(struct fm10k_intfc *interface)
357 {
358 	struct net_device *netdev = interface->netdev;
359 	u32 __iomem *hw_addr;
360 	u32 value;
361 	int err;
362 
363 	/* do nothing if netdev is still present or hw_addr is set */
364 	if (netif_device_present(netdev) || interface->hw.hw_addr)
365 		return;
366 
367 	/* We've lost the PCIe register space, and can no longer access the
368 	 * device. Shut everything except the detach subtask down and prepare
369 	 * to reset the device in case we recover. If we actually prepare for
370 	 * reset, indicate that we're detached.
371 	 */
372 	if (fm10k_prepare_for_reset(interface))
373 		set_bit(__FM10K_RESET_DETACHED, interface->state);
374 
375 	/* check the real address space to see if we've recovered */
376 	hw_addr = READ_ONCE(interface->uc_addr);
377 	value = readl(hw_addr);
378 	if (~value) {
379 		/* Make sure the reset was initiated because we detached,
380 		 * otherwise we might race with a different reset flow.
381 		 */
382 		if (!test_and_clear_bit(__FM10K_RESET_DETACHED,
383 					interface->state))
384 			return;
385 
386 		/* Restore the hardware address */
387 		interface->hw.hw_addr = interface->uc_addr;
388 
389 		/* PCIe link has been restored, and the device is active
390 		 * again. Restore everything and reset the device.
391 		 */
392 		err = fm10k_handle_reset(interface);
393 		if (err) {
394 			netdev_err(netdev, "Unable to reset device: %d\n", err);
395 			interface->hw.hw_addr = NULL;
396 			return;
397 		}
398 
399 		/* Re-attach the netdev */
400 		netif_device_attach(netdev);
401 		netdev_warn(netdev, "PCIe link restored, device now attached\n");
402 		return;
403 	}
404 }
405 
406 static void fm10k_reset_subtask(struct fm10k_intfc *interface)
407 {
408 	int err;
409 
410 	if (!test_and_clear_bit(FM10K_FLAG_RESET_REQUESTED,
411 				interface->flags))
412 		return;
413 
414 	/* If another thread has already prepared to reset the device, we
415 	 * should not attempt to handle a reset here, since we'd race with
416 	 * that thread. This may happen if we suspend the device or if the
417 	 * PCIe link is lost. In this case, we'll just ignore the RESET
418 	 * request, as it will (eventually) be taken care of when the thread
419 	 * which actually started the reset is finished.
420 	 */
421 	if (!fm10k_prepare_for_reset(interface))
422 		return;
423 
424 	netdev_err(interface->netdev, "Reset interface\n");
425 
426 	err = fm10k_handle_reset(interface);
427 	if (err)
428 		dev_err(&interface->pdev->dev,
429 			"fm10k_handle_reset failed: %d\n", err);
430 }
431 
432 /**
433  * fm10k_configure_swpri_map - Configure Receive SWPRI to PC mapping
434  * @interface: board private structure
435  *
436  * Configure the SWPRI to PC mapping for the port.
437  **/
438 static void fm10k_configure_swpri_map(struct fm10k_intfc *interface)
439 {
440 	struct net_device *netdev = interface->netdev;
441 	struct fm10k_hw *hw = &interface->hw;
442 	int i;
443 
444 	/* clear flag indicating update is needed */
445 	clear_bit(FM10K_FLAG_SWPRI_CONFIG, interface->flags);
446 
447 	/* these registers are only available on the PF */
448 	if (hw->mac.type != fm10k_mac_pf)
449 		return;
450 
451 	/* configure SWPRI to PC map */
452 	for (i = 0; i < FM10K_SWPRI_MAX; i++)
453 		fm10k_write_reg(hw, FM10K_SWPRI_MAP(i),
454 				netdev_get_prio_tc_map(netdev, i));
455 }
456 
457 /**
458  * fm10k_watchdog_update_host_state - Update the link status based on host.
459  * @interface: board private structure
460  **/
461 static void fm10k_watchdog_update_host_state(struct fm10k_intfc *interface)
462 {
463 	struct fm10k_hw *hw = &interface->hw;
464 	s32 err;
465 
466 	if (test_bit(__FM10K_LINK_DOWN, interface->state)) {
467 		interface->host_ready = false;
468 		if (time_is_after_jiffies(interface->link_down_event))
469 			return;
470 		clear_bit(__FM10K_LINK_DOWN, interface->state);
471 	}
472 
473 	if (test_bit(FM10K_FLAG_SWPRI_CONFIG, interface->flags)) {
474 		if (rtnl_trylock()) {
475 			fm10k_configure_swpri_map(interface);
476 			rtnl_unlock();
477 		}
478 	}
479 
480 	/* lock the mailbox for transmit and receive */
481 	fm10k_mbx_lock(interface);
482 
483 	err = hw->mac.ops.get_host_state(hw, &interface->host_ready);
484 	if (err && time_is_before_jiffies(interface->last_reset))
485 		set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
486 
487 	/* free the lock */
488 	fm10k_mbx_unlock(interface);
489 }
490 
491 /**
492  * fm10k_mbx_subtask - Process upstream and downstream mailboxes
493  * @interface: board private structure
494  *
495  * This function will process both the upstream and downstream mailboxes.
496  **/
497 static void fm10k_mbx_subtask(struct fm10k_intfc *interface)
498 {
499 	/* If we're resetting, bail out */
500 	if (test_bit(__FM10K_RESETTING, interface->state))
501 		return;
502 
503 	/* process upstream mailbox and update device state */
504 	fm10k_watchdog_update_host_state(interface);
505 
506 	/* process downstream mailboxes */
507 	fm10k_iov_mbx(interface);
508 }
509 
510 /**
511  * fm10k_watchdog_host_is_ready - Update netdev status based on host ready
512  * @interface: board private structure
513  **/
514 static void fm10k_watchdog_host_is_ready(struct fm10k_intfc *interface)
515 {
516 	struct net_device *netdev = interface->netdev;
517 
518 	/* only continue if link state is currently down */
519 	if (netif_carrier_ok(netdev))
520 		return;
521 
522 	netif_info(interface, drv, netdev, "NIC Link is up\n");
523 
524 	netif_carrier_on(netdev);
525 	netif_tx_wake_all_queues(netdev);
526 }
527 
528 /**
529  * fm10k_watchdog_host_not_ready - Update netdev status based on host not ready
530  * @interface: board private structure
531  **/
532 static void fm10k_watchdog_host_not_ready(struct fm10k_intfc *interface)
533 {
534 	struct net_device *netdev = interface->netdev;
535 
536 	/* only continue if link state is currently up */
537 	if (!netif_carrier_ok(netdev))
538 		return;
539 
540 	netif_info(interface, drv, netdev, "NIC Link is down\n");
541 
542 	netif_carrier_off(netdev);
543 	netif_tx_stop_all_queues(netdev);
544 }
545 
546 /**
547  * fm10k_update_stats - Update the board statistics counters.
548  * @interface: board private structure
549  **/
550 void fm10k_update_stats(struct fm10k_intfc *interface)
551 {
552 	struct net_device_stats *net_stats = &interface->netdev->stats;
553 	struct fm10k_hw *hw = &interface->hw;
554 	u64 hw_csum_tx_good = 0, hw_csum_rx_good = 0, rx_length_errors = 0;
555 	u64 rx_switch_errors = 0, rx_drops = 0, rx_pp_errors = 0;
556 	u64 rx_link_errors = 0;
557 	u64 rx_errors = 0, rx_csum_errors = 0, tx_csum_errors = 0;
558 	u64 restart_queue = 0, tx_busy = 0, alloc_failed = 0;
559 	u64 rx_bytes_nic = 0, rx_pkts_nic = 0, rx_drops_nic = 0;
560 	u64 tx_bytes_nic = 0, tx_pkts_nic = 0;
561 	u64 bytes, pkts;
562 	int i;
563 
564 	/* ensure only one thread updates stats at a time */
565 	if (test_and_set_bit(__FM10K_UPDATING_STATS, interface->state))
566 		return;
567 
568 	/* do not allow stats update via service task for next second */
569 	interface->next_stats_update = jiffies + HZ;
570 
571 	/* gather some stats to the interface struct that are per queue */
572 	for (bytes = 0, pkts = 0, i = 0; i < interface->num_tx_queues; i++) {
573 		struct fm10k_ring *tx_ring = READ_ONCE(interface->tx_ring[i]);
574 
575 		if (!tx_ring)
576 			continue;
577 
578 		restart_queue += tx_ring->tx_stats.restart_queue;
579 		tx_busy += tx_ring->tx_stats.tx_busy;
580 		tx_csum_errors += tx_ring->tx_stats.csum_err;
581 		bytes += tx_ring->stats.bytes;
582 		pkts += tx_ring->stats.packets;
583 		hw_csum_tx_good += tx_ring->tx_stats.csum_good;
584 	}
585 
586 	interface->restart_queue = restart_queue;
587 	interface->tx_busy = tx_busy;
588 	net_stats->tx_bytes = bytes;
589 	net_stats->tx_packets = pkts;
590 	interface->tx_csum_errors = tx_csum_errors;
591 	interface->hw_csum_tx_good = hw_csum_tx_good;
592 
593 	/* gather some stats to the interface struct that are per queue */
594 	for (bytes = 0, pkts = 0, i = 0; i < interface->num_rx_queues; i++) {
595 		struct fm10k_ring *rx_ring = READ_ONCE(interface->rx_ring[i]);
596 
597 		if (!rx_ring)
598 			continue;
599 
600 		bytes += rx_ring->stats.bytes;
601 		pkts += rx_ring->stats.packets;
602 		alloc_failed += rx_ring->rx_stats.alloc_failed;
603 		rx_csum_errors += rx_ring->rx_stats.csum_err;
604 		rx_errors += rx_ring->rx_stats.errors;
605 		hw_csum_rx_good += rx_ring->rx_stats.csum_good;
606 		rx_switch_errors += rx_ring->rx_stats.switch_errors;
607 		rx_drops += rx_ring->rx_stats.drops;
608 		rx_pp_errors += rx_ring->rx_stats.pp_errors;
609 		rx_link_errors += rx_ring->rx_stats.link_errors;
610 		rx_length_errors += rx_ring->rx_stats.length_errors;
611 	}
612 
613 	net_stats->rx_bytes = bytes;
614 	net_stats->rx_packets = pkts;
615 	interface->alloc_failed = alloc_failed;
616 	interface->rx_csum_errors = rx_csum_errors;
617 	interface->hw_csum_rx_good = hw_csum_rx_good;
618 	interface->rx_switch_errors = rx_switch_errors;
619 	interface->rx_drops = rx_drops;
620 	interface->rx_pp_errors = rx_pp_errors;
621 	interface->rx_link_errors = rx_link_errors;
622 	interface->rx_length_errors = rx_length_errors;
623 
624 	hw->mac.ops.update_hw_stats(hw, &interface->stats);
625 
626 	for (i = 0; i < hw->mac.max_queues; i++) {
627 		struct fm10k_hw_stats_q *q = &interface->stats.q[i];
628 
629 		tx_bytes_nic += q->tx_bytes.count;
630 		tx_pkts_nic += q->tx_packets.count;
631 		rx_bytes_nic += q->rx_bytes.count;
632 		rx_pkts_nic += q->rx_packets.count;
633 		rx_drops_nic += q->rx_drops.count;
634 	}
635 
636 	interface->tx_bytes_nic = tx_bytes_nic;
637 	interface->tx_packets_nic = tx_pkts_nic;
638 	interface->rx_bytes_nic = rx_bytes_nic;
639 	interface->rx_packets_nic = rx_pkts_nic;
640 	interface->rx_drops_nic = rx_drops_nic;
641 
642 	/* Fill out the OS statistics structure */
643 	net_stats->rx_errors = rx_errors;
644 	net_stats->rx_dropped = interface->stats.nodesc_drop.count;
645 
646 	clear_bit(__FM10K_UPDATING_STATS, interface->state);
647 }
648 
649 /**
650  * fm10k_watchdog_flush_tx - flush queues on host not ready
651  * @interface - pointer to the device interface structure
652  **/
653 static void fm10k_watchdog_flush_tx(struct fm10k_intfc *interface)
654 {
655 	int some_tx_pending = 0;
656 	int i;
657 
658 	/* nothing to do if carrier is up */
659 	if (netif_carrier_ok(interface->netdev))
660 		return;
661 
662 	for (i = 0; i < interface->num_tx_queues; i++) {
663 		struct fm10k_ring *tx_ring = interface->tx_ring[i];
664 
665 		if (tx_ring->next_to_use != tx_ring->next_to_clean) {
666 			some_tx_pending = 1;
667 			break;
668 		}
669 	}
670 
671 	/* We've lost link, so the controller stops DMA, but we've got
672 	 * queued Tx work that's never going to get done, so reset
673 	 * controller to flush Tx.
674 	 */
675 	if (some_tx_pending)
676 		set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
677 }
678 
679 /**
680  * fm10k_watchdog_subtask - check and bring link up
681  * @interface - pointer to the device interface structure
682  **/
683 static void fm10k_watchdog_subtask(struct fm10k_intfc *interface)
684 {
685 	/* if interface is down do nothing */
686 	if (test_bit(__FM10K_DOWN, interface->state) ||
687 	    test_bit(__FM10K_RESETTING, interface->state))
688 		return;
689 
690 	if (interface->host_ready)
691 		fm10k_watchdog_host_is_ready(interface);
692 	else
693 		fm10k_watchdog_host_not_ready(interface);
694 
695 	/* update stats only once every second */
696 	if (time_is_before_jiffies(interface->next_stats_update))
697 		fm10k_update_stats(interface);
698 
699 	/* flush any uncompleted work */
700 	fm10k_watchdog_flush_tx(interface);
701 }
702 
703 /**
704  * fm10k_check_hang_subtask - check for hung queues and dropped interrupts
705  * @interface - pointer to the device interface structure
706  *
707  * This function serves two purposes.  First it strobes the interrupt lines
708  * in order to make certain interrupts are occurring.  Secondly it sets the
709  * bits needed to check for TX hangs.  As a result we should immediately
710  * determine if a hang has occurred.
711  */
712 static void fm10k_check_hang_subtask(struct fm10k_intfc *interface)
713 {
714 	int i;
715 
716 	/* If we're down or resetting, just bail */
717 	if (test_bit(__FM10K_DOWN, interface->state) ||
718 	    test_bit(__FM10K_RESETTING, interface->state))
719 		return;
720 
721 	/* rate limit tx hang checks to only once every 2 seconds */
722 	if (time_is_after_eq_jiffies(interface->next_tx_hang_check))
723 		return;
724 	interface->next_tx_hang_check = jiffies + (2 * HZ);
725 
726 	if (netif_carrier_ok(interface->netdev)) {
727 		/* Force detection of hung controller */
728 		for (i = 0; i < interface->num_tx_queues; i++)
729 			set_check_for_tx_hang(interface->tx_ring[i]);
730 
731 		/* Rearm all in-use q_vectors for immediate firing */
732 		for (i = 0; i < interface->num_q_vectors; i++) {
733 			struct fm10k_q_vector *qv = interface->q_vector[i];
734 
735 			if (!qv->tx.count && !qv->rx.count)
736 				continue;
737 			writel(FM10K_ITR_ENABLE | FM10K_ITR_PENDING2, qv->itr);
738 		}
739 	}
740 }
741 
742 /**
743  * fm10k_service_task - manages and runs subtasks
744  * @work: pointer to work_struct containing our data
745  **/
746 static void fm10k_service_task(struct work_struct *work)
747 {
748 	struct fm10k_intfc *interface;
749 
750 	interface = container_of(work, struct fm10k_intfc, service_task);
751 
752 	/* Check whether we're detached first */
753 	fm10k_detach_subtask(interface);
754 
755 	/* tasks run even when interface is down */
756 	fm10k_mbx_subtask(interface);
757 	fm10k_reset_subtask(interface);
758 
759 	/* tasks only run when interface is up */
760 	fm10k_watchdog_subtask(interface);
761 	fm10k_check_hang_subtask(interface);
762 
763 	/* release lock on service events to allow scheduling next event */
764 	fm10k_service_event_complete(interface);
765 }
766 
767 /**
768  * fm10k_macvlan_task - send queued MAC/VLAN requests to switch manager
769  * @work: pointer to work_struct containing our data
770  *
771  * This work item handles sending MAC/VLAN updates to the switch manager. When
772  * the interface is up, it will attempt to queue mailbox messages to the
773  * switch manager requesting updates for MAC/VLAN pairs. If the Tx fifo of the
774  * mailbox is full, it will reschedule itself to try again in a short while.
775  * This ensures that the driver does not overload the switch mailbox with too
776  * many simultaneous requests, causing an unnecessary reset.
777  **/
778 static void fm10k_macvlan_task(struct work_struct *work)
779 {
780 	struct fm10k_macvlan_request *item;
781 	struct fm10k_intfc *interface;
782 	struct delayed_work *dwork;
783 	struct list_head *requests;
784 	struct fm10k_hw *hw;
785 	unsigned long flags;
786 
787 	dwork = to_delayed_work(work);
788 	interface = container_of(dwork, struct fm10k_intfc, macvlan_task);
789 	hw = &interface->hw;
790 	requests = &interface->macvlan_requests;
791 
792 	do {
793 		/* Pop the first item off the list */
794 		spin_lock_irqsave(&interface->macvlan_lock, flags);
795 		item = list_first_entry_or_null(requests,
796 						struct fm10k_macvlan_request,
797 						list);
798 		if (item)
799 			list_del_init(&item->list);
800 
801 		spin_unlock_irqrestore(&interface->macvlan_lock, flags);
802 
803 		/* We have no more items to process */
804 		if (!item)
805 			goto done;
806 
807 		fm10k_mbx_lock(interface);
808 
809 		/* Check that we have plenty of space to send the message. We
810 		 * want to ensure that the mailbox stays low enough to avoid a
811 		 * change in the host state, otherwise we may see spurious
812 		 * link up / link down notifications.
813 		 */
814 		if (!hw->mbx.ops.tx_ready(&hw->mbx, FM10K_VFMBX_MSG_MTU + 5)) {
815 			hw->mbx.ops.process(hw, &hw->mbx);
816 			set_bit(__FM10K_MACVLAN_REQUEST, interface->state);
817 			fm10k_mbx_unlock(interface);
818 
819 			/* Put the request back on the list */
820 			spin_lock_irqsave(&interface->macvlan_lock, flags);
821 			list_add(&item->list, requests);
822 			spin_unlock_irqrestore(&interface->macvlan_lock, flags);
823 			break;
824 		}
825 
826 		switch (item->type) {
827 		case FM10K_MC_MAC_REQUEST:
828 			hw->mac.ops.update_mc_addr(hw,
829 						   item->mac.glort,
830 						   item->mac.addr,
831 						   item->mac.vid,
832 						   item->set);
833 			break;
834 		case FM10K_UC_MAC_REQUEST:
835 			hw->mac.ops.update_uc_addr(hw,
836 						   item->mac.glort,
837 						   item->mac.addr,
838 						   item->mac.vid,
839 						   item->set,
840 						   0);
841 			break;
842 		case FM10K_VLAN_REQUEST:
843 			hw->mac.ops.update_vlan(hw,
844 						item->vlan.vid,
845 						item->vlan.vsi,
846 						item->set);
847 			break;
848 		default:
849 			break;
850 		}
851 
852 		fm10k_mbx_unlock(interface);
853 
854 		/* Free the item now that we've sent the update */
855 		kfree(item);
856 	} while (true);
857 
858 done:
859 	WARN_ON(!test_bit(__FM10K_MACVLAN_SCHED, interface->state));
860 
861 	/* flush memory to make sure state is correct */
862 	smp_mb__before_atomic();
863 	clear_bit(__FM10K_MACVLAN_SCHED, interface->state);
864 
865 	/* If a MAC/VLAN request was scheduled since we started, we should
866 	 * re-schedule. However, there is no reason to re-schedule if there is
867 	 * no work to do.
868 	 */
869 	if (test_bit(__FM10K_MACVLAN_REQUEST, interface->state))
870 		fm10k_macvlan_schedule(interface);
871 }
872 
873 /**
874  * fm10k_configure_tx_ring - Configure Tx ring after Reset
875  * @interface: board private structure
876  * @ring: structure containing ring specific data
877  *
878  * Configure the Tx descriptor ring after a reset.
879  **/
880 static void fm10k_configure_tx_ring(struct fm10k_intfc *interface,
881 				    struct fm10k_ring *ring)
882 {
883 	struct fm10k_hw *hw = &interface->hw;
884 	u64 tdba = ring->dma;
885 	u32 size = ring->count * sizeof(struct fm10k_tx_desc);
886 	u32 txint = FM10K_INT_MAP_DISABLE;
887 	u32 txdctl = BIT(FM10K_TXDCTL_MAX_TIME_SHIFT) | FM10K_TXDCTL_ENABLE;
888 	u8 reg_idx = ring->reg_idx;
889 
890 	/* disable queue to avoid issues while updating state */
891 	fm10k_write_reg(hw, FM10K_TXDCTL(reg_idx), 0);
892 	fm10k_write_flush(hw);
893 
894 	/* possible poll here to verify ring resources have been cleaned */
895 
896 	/* set location and size for descriptor ring */
897 	fm10k_write_reg(hw, FM10K_TDBAL(reg_idx), tdba & DMA_BIT_MASK(32));
898 	fm10k_write_reg(hw, FM10K_TDBAH(reg_idx), tdba >> 32);
899 	fm10k_write_reg(hw, FM10K_TDLEN(reg_idx), size);
900 
901 	/* reset head and tail pointers */
902 	fm10k_write_reg(hw, FM10K_TDH(reg_idx), 0);
903 	fm10k_write_reg(hw, FM10K_TDT(reg_idx), 0);
904 
905 	/* store tail pointer */
906 	ring->tail = &interface->uc_addr[FM10K_TDT(reg_idx)];
907 
908 	/* reset ntu and ntc to place SW in sync with hardware */
909 	ring->next_to_clean = 0;
910 	ring->next_to_use = 0;
911 
912 	/* Map interrupt */
913 	if (ring->q_vector) {
914 		txint = ring->q_vector->v_idx + NON_Q_VECTORS(hw);
915 		txint |= FM10K_INT_MAP_TIMER0;
916 	}
917 
918 	fm10k_write_reg(hw, FM10K_TXINT(reg_idx), txint);
919 
920 	/* enable use of FTAG bit in Tx descriptor, register is RO for VF */
921 	fm10k_write_reg(hw, FM10K_PFVTCTL(reg_idx),
922 			FM10K_PFVTCTL_FTAG_DESC_ENABLE);
923 
924 	/* Initialize XPS */
925 	if (!test_and_set_bit(__FM10K_TX_XPS_INIT_DONE, ring->state) &&
926 	    ring->q_vector)
927 		netif_set_xps_queue(ring->netdev,
928 				    &ring->q_vector->affinity_mask,
929 				    ring->queue_index);
930 
931 	/* enable queue */
932 	fm10k_write_reg(hw, FM10K_TXDCTL(reg_idx), txdctl);
933 }
934 
935 /**
936  * fm10k_enable_tx_ring - Verify Tx ring is enabled after configuration
937  * @interface: board private structure
938  * @ring: structure containing ring specific data
939  *
940  * Verify the Tx descriptor ring is ready for transmit.
941  **/
942 static void fm10k_enable_tx_ring(struct fm10k_intfc *interface,
943 				 struct fm10k_ring *ring)
944 {
945 	struct fm10k_hw *hw = &interface->hw;
946 	int wait_loop = 10;
947 	u32 txdctl;
948 	u8 reg_idx = ring->reg_idx;
949 
950 	/* if we are already enabled just exit */
951 	if (fm10k_read_reg(hw, FM10K_TXDCTL(reg_idx)) & FM10K_TXDCTL_ENABLE)
952 		return;
953 
954 	/* poll to verify queue is enabled */
955 	do {
956 		usleep_range(1000, 2000);
957 		txdctl = fm10k_read_reg(hw, FM10K_TXDCTL(reg_idx));
958 	} while (!(txdctl & FM10K_TXDCTL_ENABLE) && --wait_loop);
959 	if (!wait_loop)
960 		netif_err(interface, drv, interface->netdev,
961 			  "Could not enable Tx Queue %d\n", reg_idx);
962 }
963 
964 /**
965  * fm10k_configure_tx - Configure Transmit Unit after Reset
966  * @interface: board private structure
967  *
968  * Configure the Tx unit of the MAC after a reset.
969  **/
970 static void fm10k_configure_tx(struct fm10k_intfc *interface)
971 {
972 	int i;
973 
974 	/* Setup the HW Tx Head and Tail descriptor pointers */
975 	for (i = 0; i < interface->num_tx_queues; i++)
976 		fm10k_configure_tx_ring(interface, interface->tx_ring[i]);
977 
978 	/* poll here to verify that Tx rings are now enabled */
979 	for (i = 0; i < interface->num_tx_queues; i++)
980 		fm10k_enable_tx_ring(interface, interface->tx_ring[i]);
981 }
982 
983 /**
984  * fm10k_configure_rx_ring - Configure Rx ring after Reset
985  * @interface: board private structure
986  * @ring: structure containing ring specific data
987  *
988  * Configure the Rx descriptor ring after a reset.
989  **/
990 static void fm10k_configure_rx_ring(struct fm10k_intfc *interface,
991 				    struct fm10k_ring *ring)
992 {
993 	u64 rdba = ring->dma;
994 	struct fm10k_hw *hw = &interface->hw;
995 	u32 size = ring->count * sizeof(union fm10k_rx_desc);
996 	u32 rxqctl, rxdctl = FM10K_RXDCTL_WRITE_BACK_MIN_DELAY;
997 	u32 srrctl = FM10K_SRRCTL_BUFFER_CHAINING_EN;
998 	u32 rxint = FM10K_INT_MAP_DISABLE;
999 	u8 rx_pause = interface->rx_pause;
1000 	u8 reg_idx = ring->reg_idx;
1001 
1002 	/* disable queue to avoid issues while updating state */
1003 	rxqctl = fm10k_read_reg(hw, FM10K_RXQCTL(reg_idx));
1004 	rxqctl &= ~FM10K_RXQCTL_ENABLE;
1005 	fm10k_write_reg(hw, FM10K_RXQCTL(reg_idx), rxqctl);
1006 	fm10k_write_flush(hw);
1007 
1008 	/* possible poll here to verify ring resources have been cleaned */
1009 
1010 	/* set location and size for descriptor ring */
1011 	fm10k_write_reg(hw, FM10K_RDBAL(reg_idx), rdba & DMA_BIT_MASK(32));
1012 	fm10k_write_reg(hw, FM10K_RDBAH(reg_idx), rdba >> 32);
1013 	fm10k_write_reg(hw, FM10K_RDLEN(reg_idx), size);
1014 
1015 	/* reset head and tail pointers */
1016 	fm10k_write_reg(hw, FM10K_RDH(reg_idx), 0);
1017 	fm10k_write_reg(hw, FM10K_RDT(reg_idx), 0);
1018 
1019 	/* store tail pointer */
1020 	ring->tail = &interface->uc_addr[FM10K_RDT(reg_idx)];
1021 
1022 	/* reset ntu and ntc to place SW in sync with hardware */
1023 	ring->next_to_clean = 0;
1024 	ring->next_to_use = 0;
1025 	ring->next_to_alloc = 0;
1026 
1027 	/* Configure the Rx buffer size for one buff without split */
1028 	srrctl |= FM10K_RX_BUFSZ >> FM10K_SRRCTL_BSIZEPKT_SHIFT;
1029 
1030 	/* Configure the Rx ring to suppress loopback packets */
1031 	srrctl |= FM10K_SRRCTL_LOOPBACK_SUPPRESS;
1032 	fm10k_write_reg(hw, FM10K_SRRCTL(reg_idx), srrctl);
1033 
1034 	/* Enable drop on empty */
1035 #ifdef CONFIG_DCB
1036 	if (interface->pfc_en)
1037 		rx_pause = interface->pfc_en;
1038 #endif
1039 	if (!(rx_pause & BIT(ring->qos_pc)))
1040 		rxdctl |= FM10K_RXDCTL_DROP_ON_EMPTY;
1041 
1042 	fm10k_write_reg(hw, FM10K_RXDCTL(reg_idx), rxdctl);
1043 
1044 	/* assign default VLAN to queue */
1045 	ring->vid = hw->mac.default_vid;
1046 
1047 	/* if we have an active VLAN, disable default VLAN ID */
1048 	if (test_bit(hw->mac.default_vid, interface->active_vlans))
1049 		ring->vid |= FM10K_VLAN_CLEAR;
1050 
1051 	/* Map interrupt */
1052 	if (ring->q_vector) {
1053 		rxint = ring->q_vector->v_idx + NON_Q_VECTORS(hw);
1054 		rxint |= FM10K_INT_MAP_TIMER1;
1055 	}
1056 
1057 	fm10k_write_reg(hw, FM10K_RXINT(reg_idx), rxint);
1058 
1059 	/* enable queue */
1060 	rxqctl = fm10k_read_reg(hw, FM10K_RXQCTL(reg_idx));
1061 	rxqctl |= FM10K_RXQCTL_ENABLE;
1062 	fm10k_write_reg(hw, FM10K_RXQCTL(reg_idx), rxqctl);
1063 
1064 	/* place buffers on ring for receive data */
1065 	fm10k_alloc_rx_buffers(ring, fm10k_desc_unused(ring));
1066 }
1067 
1068 /**
1069  * fm10k_update_rx_drop_en - Configures the drop enable bits for Rx rings
1070  * @interface: board private structure
1071  *
1072  * Configure the drop enable bits for the Rx rings.
1073  **/
1074 void fm10k_update_rx_drop_en(struct fm10k_intfc *interface)
1075 {
1076 	struct fm10k_hw *hw = &interface->hw;
1077 	u8 rx_pause = interface->rx_pause;
1078 	int i;
1079 
1080 #ifdef CONFIG_DCB
1081 	if (interface->pfc_en)
1082 		rx_pause = interface->pfc_en;
1083 
1084 #endif
1085 	for (i = 0; i < interface->num_rx_queues; i++) {
1086 		struct fm10k_ring *ring = interface->rx_ring[i];
1087 		u32 rxdctl = FM10K_RXDCTL_WRITE_BACK_MIN_DELAY;
1088 		u8 reg_idx = ring->reg_idx;
1089 
1090 		if (!(rx_pause & BIT(ring->qos_pc)))
1091 			rxdctl |= FM10K_RXDCTL_DROP_ON_EMPTY;
1092 
1093 		fm10k_write_reg(hw, FM10K_RXDCTL(reg_idx), rxdctl);
1094 	}
1095 }
1096 
1097 /**
1098  * fm10k_configure_dglort - Configure Receive DGLORT after reset
1099  * @interface: board private structure
1100  *
1101  * Configure the DGLORT description and RSS tables.
1102  **/
1103 static void fm10k_configure_dglort(struct fm10k_intfc *interface)
1104 {
1105 	struct fm10k_dglort_cfg dglort = { 0 };
1106 	struct fm10k_hw *hw = &interface->hw;
1107 	int i;
1108 	u32 mrqc;
1109 
1110 	/* Fill out hash function seeds */
1111 	for (i = 0; i < FM10K_RSSRK_SIZE; i++)
1112 		fm10k_write_reg(hw, FM10K_RSSRK(0, i), interface->rssrk[i]);
1113 
1114 	/* Write RETA table to hardware */
1115 	for (i = 0; i < FM10K_RETA_SIZE; i++)
1116 		fm10k_write_reg(hw, FM10K_RETA(0, i), interface->reta[i]);
1117 
1118 	/* Generate RSS hash based on packet types, TCP/UDP
1119 	 * port numbers and/or IPv4/v6 src and dst addresses
1120 	 */
1121 	mrqc = FM10K_MRQC_IPV4 |
1122 	       FM10K_MRQC_TCP_IPV4 |
1123 	       FM10K_MRQC_IPV6 |
1124 	       FM10K_MRQC_TCP_IPV6;
1125 
1126 	if (test_bit(FM10K_FLAG_RSS_FIELD_IPV4_UDP, interface->flags))
1127 		mrqc |= FM10K_MRQC_UDP_IPV4;
1128 	if (test_bit(FM10K_FLAG_RSS_FIELD_IPV6_UDP, interface->flags))
1129 		mrqc |= FM10K_MRQC_UDP_IPV6;
1130 
1131 	fm10k_write_reg(hw, FM10K_MRQC(0), mrqc);
1132 
1133 	/* configure default DGLORT mapping for RSS/DCB */
1134 	dglort.inner_rss = 1;
1135 	dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
1136 	dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
1137 	hw->mac.ops.configure_dglort_map(hw, &dglort);
1138 
1139 	/* assign GLORT per queue for queue mapped testing */
1140 	if (interface->glort_count > 64) {
1141 		memset(&dglort, 0, sizeof(dglort));
1142 		dglort.inner_rss = 1;
1143 		dglort.glort = interface->glort + 64;
1144 		dglort.idx = fm10k_dglort_pf_queue;
1145 		dglort.queue_l = fls(interface->num_rx_queues - 1);
1146 		hw->mac.ops.configure_dglort_map(hw, &dglort);
1147 	}
1148 
1149 	/* assign glort value for RSS/DCB specific to this interface */
1150 	memset(&dglort, 0, sizeof(dglort));
1151 	dglort.inner_rss = 1;
1152 	dglort.glort = interface->glort;
1153 	dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
1154 	dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
1155 	/* configure DGLORT mapping for RSS/DCB */
1156 	dglort.idx = fm10k_dglort_pf_rss;
1157 	if (interface->l2_accel)
1158 		dglort.shared_l = fls(interface->l2_accel->size);
1159 	hw->mac.ops.configure_dglort_map(hw, &dglort);
1160 }
1161 
1162 /**
1163  * fm10k_configure_rx - Configure Receive Unit after Reset
1164  * @interface: board private structure
1165  *
1166  * Configure the Rx unit of the MAC after a reset.
1167  **/
1168 static void fm10k_configure_rx(struct fm10k_intfc *interface)
1169 {
1170 	int i;
1171 
1172 	/* Configure SWPRI to PC map */
1173 	fm10k_configure_swpri_map(interface);
1174 
1175 	/* Configure RSS and DGLORT map */
1176 	fm10k_configure_dglort(interface);
1177 
1178 	/* Setup the HW Rx Head and Tail descriptor pointers */
1179 	for (i = 0; i < interface->num_rx_queues; i++)
1180 		fm10k_configure_rx_ring(interface, interface->rx_ring[i]);
1181 
1182 	/* possible poll here to verify that Rx rings are now enabled */
1183 }
1184 
1185 static void fm10k_napi_enable_all(struct fm10k_intfc *interface)
1186 {
1187 	struct fm10k_q_vector *q_vector;
1188 	int q_idx;
1189 
1190 	for (q_idx = 0; q_idx < interface->num_q_vectors; q_idx++) {
1191 		q_vector = interface->q_vector[q_idx];
1192 		napi_enable(&q_vector->napi);
1193 	}
1194 }
1195 
1196 static irqreturn_t fm10k_msix_clean_rings(int __always_unused irq, void *data)
1197 {
1198 	struct fm10k_q_vector *q_vector = data;
1199 
1200 	if (q_vector->rx.count || q_vector->tx.count)
1201 		napi_schedule_irqoff(&q_vector->napi);
1202 
1203 	return IRQ_HANDLED;
1204 }
1205 
1206 static irqreturn_t fm10k_msix_mbx_vf(int __always_unused irq, void *data)
1207 {
1208 	struct fm10k_intfc *interface = data;
1209 	struct fm10k_hw *hw = &interface->hw;
1210 	struct fm10k_mbx_info *mbx = &hw->mbx;
1211 
1212 	/* re-enable mailbox interrupt and indicate 20us delay */
1213 	fm10k_write_reg(hw, FM10K_VFITR(FM10K_MBX_VECTOR),
1214 			(FM10K_MBX_INT_DELAY >> hw->mac.itr_scale) |
1215 			FM10K_ITR_ENABLE);
1216 
1217 	/* service upstream mailbox */
1218 	if (fm10k_mbx_trylock(interface)) {
1219 		mbx->ops.process(hw, mbx);
1220 		fm10k_mbx_unlock(interface);
1221 	}
1222 
1223 	hw->mac.get_host_state = true;
1224 	fm10k_service_event_schedule(interface);
1225 
1226 	return IRQ_HANDLED;
1227 }
1228 
1229 #ifdef CONFIG_NET_POLL_CONTROLLER
1230 /**
1231  *  fm10k_netpoll - A Polling 'interrupt' handler
1232  *  @netdev: network interface device structure
1233  *
1234  *  This is used by netconsole to send skbs without having to re-enable
1235  *  interrupts. It's not called while the normal interrupt routine is executing.
1236  **/
1237 void fm10k_netpoll(struct net_device *netdev)
1238 {
1239 	struct fm10k_intfc *interface = netdev_priv(netdev);
1240 	int i;
1241 
1242 	/* if interface is down do nothing */
1243 	if (test_bit(__FM10K_DOWN, interface->state))
1244 		return;
1245 
1246 	for (i = 0; i < interface->num_q_vectors; i++)
1247 		fm10k_msix_clean_rings(0, interface->q_vector[i]);
1248 }
1249 
1250 #endif
1251 #define FM10K_ERR_MSG(type) case (type): error = #type; break
1252 static void fm10k_handle_fault(struct fm10k_intfc *interface, int type,
1253 			       struct fm10k_fault *fault)
1254 {
1255 	struct pci_dev *pdev = interface->pdev;
1256 	struct fm10k_hw *hw = &interface->hw;
1257 	struct fm10k_iov_data *iov_data = interface->iov_data;
1258 	char *error;
1259 
1260 	switch (type) {
1261 	case FM10K_PCA_FAULT:
1262 		switch (fault->type) {
1263 		default:
1264 			error = "Unknown PCA error";
1265 			break;
1266 		FM10K_ERR_MSG(PCA_NO_FAULT);
1267 		FM10K_ERR_MSG(PCA_UNMAPPED_ADDR);
1268 		FM10K_ERR_MSG(PCA_BAD_QACCESS_PF);
1269 		FM10K_ERR_MSG(PCA_BAD_QACCESS_VF);
1270 		FM10K_ERR_MSG(PCA_MALICIOUS_REQ);
1271 		FM10K_ERR_MSG(PCA_POISONED_TLP);
1272 		FM10K_ERR_MSG(PCA_TLP_ABORT);
1273 		}
1274 		break;
1275 	case FM10K_THI_FAULT:
1276 		switch (fault->type) {
1277 		default:
1278 			error = "Unknown THI error";
1279 			break;
1280 		FM10K_ERR_MSG(THI_NO_FAULT);
1281 		FM10K_ERR_MSG(THI_MAL_DIS_Q_FAULT);
1282 		}
1283 		break;
1284 	case FM10K_FUM_FAULT:
1285 		switch (fault->type) {
1286 		default:
1287 			error = "Unknown FUM error";
1288 			break;
1289 		FM10K_ERR_MSG(FUM_NO_FAULT);
1290 		FM10K_ERR_MSG(FUM_UNMAPPED_ADDR);
1291 		FM10K_ERR_MSG(FUM_BAD_VF_QACCESS);
1292 		FM10K_ERR_MSG(FUM_ADD_DECODE_ERR);
1293 		FM10K_ERR_MSG(FUM_RO_ERROR);
1294 		FM10K_ERR_MSG(FUM_QPRC_CRC_ERROR);
1295 		FM10K_ERR_MSG(FUM_CSR_TIMEOUT);
1296 		FM10K_ERR_MSG(FUM_INVALID_TYPE);
1297 		FM10K_ERR_MSG(FUM_INVALID_LENGTH);
1298 		FM10K_ERR_MSG(FUM_INVALID_BE);
1299 		FM10K_ERR_MSG(FUM_INVALID_ALIGN);
1300 		}
1301 		break;
1302 	default:
1303 		error = "Undocumented fault";
1304 		break;
1305 	}
1306 
1307 	dev_warn(&pdev->dev,
1308 		 "%s Address: 0x%llx SpecInfo: 0x%x Func: %02x.%0x\n",
1309 		 error, fault->address, fault->specinfo,
1310 		 PCI_SLOT(fault->func), PCI_FUNC(fault->func));
1311 
1312 	/* For VF faults, clear out the respective LPORT, reset the queue
1313 	 * resources, and then reconnect to the mailbox. This allows the
1314 	 * VF in question to resume behavior. For transient faults that are
1315 	 * the result of non-malicious behavior this will log the fault and
1316 	 * allow the VF to resume functionality. Obviously for malicious VFs
1317 	 * they will be able to attempt malicious behavior again. In this
1318 	 * case, the system administrator will need to step in and manually
1319 	 * remove or disable the VF in question.
1320 	 */
1321 	if (fault->func && iov_data) {
1322 		int vf = fault->func - 1;
1323 		struct fm10k_vf_info *vf_info = &iov_data->vf_info[vf];
1324 
1325 		hw->iov.ops.reset_lport(hw, vf_info);
1326 		hw->iov.ops.reset_resources(hw, vf_info);
1327 
1328 		/* reset_lport disables the VF, so re-enable it */
1329 		hw->iov.ops.set_lport(hw, vf_info, vf,
1330 				      FM10K_VF_FLAG_MULTI_CAPABLE);
1331 
1332 		/* reset_resources will disconnect from the mbx  */
1333 		vf_info->mbx.ops.connect(hw, &vf_info->mbx);
1334 	}
1335 }
1336 
1337 static void fm10k_report_fault(struct fm10k_intfc *interface, u32 eicr)
1338 {
1339 	struct fm10k_hw *hw = &interface->hw;
1340 	struct fm10k_fault fault = { 0 };
1341 	int type, err;
1342 
1343 	for (eicr &= FM10K_EICR_FAULT_MASK, type = FM10K_PCA_FAULT;
1344 	     eicr;
1345 	     eicr >>= 1, type += FM10K_FAULT_SIZE) {
1346 		/* only check if there is an error reported */
1347 		if (!(eicr & 0x1))
1348 			continue;
1349 
1350 		/* retrieve fault info */
1351 		err = hw->mac.ops.get_fault(hw, type, &fault);
1352 		if (err) {
1353 			dev_err(&interface->pdev->dev,
1354 				"error reading fault\n");
1355 			continue;
1356 		}
1357 
1358 		fm10k_handle_fault(interface, type, &fault);
1359 	}
1360 }
1361 
1362 static void fm10k_reset_drop_on_empty(struct fm10k_intfc *interface, u32 eicr)
1363 {
1364 	struct fm10k_hw *hw = &interface->hw;
1365 	const u32 rxdctl = FM10K_RXDCTL_WRITE_BACK_MIN_DELAY;
1366 	u32 maxholdq;
1367 	int q;
1368 
1369 	if (!(eicr & FM10K_EICR_MAXHOLDTIME))
1370 		return;
1371 
1372 	maxholdq = fm10k_read_reg(hw, FM10K_MAXHOLDQ(7));
1373 	if (maxholdq)
1374 		fm10k_write_reg(hw, FM10K_MAXHOLDQ(7), maxholdq);
1375 	for (q = 255;;) {
1376 		if (maxholdq & BIT(31)) {
1377 			if (q < FM10K_MAX_QUEUES_PF) {
1378 				interface->rx_overrun_pf++;
1379 				fm10k_write_reg(hw, FM10K_RXDCTL(q), rxdctl);
1380 			} else {
1381 				interface->rx_overrun_vf++;
1382 			}
1383 		}
1384 
1385 		maxholdq *= 2;
1386 		if (!maxholdq)
1387 			q &= ~(32 - 1);
1388 
1389 		if (!q)
1390 			break;
1391 
1392 		if (q-- % 32)
1393 			continue;
1394 
1395 		maxholdq = fm10k_read_reg(hw, FM10K_MAXHOLDQ(q / 32));
1396 		if (maxholdq)
1397 			fm10k_write_reg(hw, FM10K_MAXHOLDQ(q / 32), maxholdq);
1398 	}
1399 }
1400 
1401 static irqreturn_t fm10k_msix_mbx_pf(int __always_unused irq, void *data)
1402 {
1403 	struct fm10k_intfc *interface = data;
1404 	struct fm10k_hw *hw = &interface->hw;
1405 	struct fm10k_mbx_info *mbx = &hw->mbx;
1406 	u32 eicr;
1407 	s32 err = 0;
1408 
1409 	/* unmask any set bits related to this interrupt */
1410 	eicr = fm10k_read_reg(hw, FM10K_EICR);
1411 	fm10k_write_reg(hw, FM10K_EICR, eicr & (FM10K_EICR_MAILBOX |
1412 						FM10K_EICR_SWITCHREADY |
1413 						FM10K_EICR_SWITCHNOTREADY));
1414 
1415 	/* report any faults found to the message log */
1416 	fm10k_report_fault(interface, eicr);
1417 
1418 	/* reset any queues disabled due to receiver overrun */
1419 	fm10k_reset_drop_on_empty(interface, eicr);
1420 
1421 	/* service mailboxes */
1422 	if (fm10k_mbx_trylock(interface)) {
1423 		err = mbx->ops.process(hw, mbx);
1424 		/* handle VFLRE events */
1425 		fm10k_iov_event(interface);
1426 		fm10k_mbx_unlock(interface);
1427 	}
1428 
1429 	if (err == FM10K_ERR_RESET_REQUESTED)
1430 		set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1431 
1432 	/* if switch toggled state we should reset GLORTs */
1433 	if (eicr & FM10K_EICR_SWITCHNOTREADY) {
1434 		/* force link down for at least 4 seconds */
1435 		interface->link_down_event = jiffies + (4 * HZ);
1436 		set_bit(__FM10K_LINK_DOWN, interface->state);
1437 
1438 		/* reset dglort_map back to no config */
1439 		hw->mac.dglort_map = FM10K_DGLORTMAP_NONE;
1440 	}
1441 
1442 	/* we should validate host state after interrupt event */
1443 	hw->mac.get_host_state = true;
1444 
1445 	/* validate host state, and handle VF mailboxes in the service task */
1446 	fm10k_service_event_schedule(interface);
1447 
1448 	/* re-enable mailbox interrupt and indicate 20us delay */
1449 	fm10k_write_reg(hw, FM10K_ITR(FM10K_MBX_VECTOR),
1450 			(FM10K_MBX_INT_DELAY >> hw->mac.itr_scale) |
1451 			FM10K_ITR_ENABLE);
1452 
1453 	return IRQ_HANDLED;
1454 }
1455 
1456 void fm10k_mbx_free_irq(struct fm10k_intfc *interface)
1457 {
1458 	struct fm10k_hw *hw = &interface->hw;
1459 	struct msix_entry *entry;
1460 	int itr_reg;
1461 
1462 	/* no mailbox IRQ to free if MSI-X is not enabled */
1463 	if (!interface->msix_entries)
1464 		return;
1465 
1466 	entry = &interface->msix_entries[FM10K_MBX_VECTOR];
1467 
1468 	/* disconnect the mailbox */
1469 	hw->mbx.ops.disconnect(hw, &hw->mbx);
1470 
1471 	/* disable Mailbox cause */
1472 	if (hw->mac.type == fm10k_mac_pf) {
1473 		fm10k_write_reg(hw, FM10K_EIMR,
1474 				FM10K_EIMR_DISABLE(PCA_FAULT) |
1475 				FM10K_EIMR_DISABLE(FUM_FAULT) |
1476 				FM10K_EIMR_DISABLE(MAILBOX) |
1477 				FM10K_EIMR_DISABLE(SWITCHREADY) |
1478 				FM10K_EIMR_DISABLE(SWITCHNOTREADY) |
1479 				FM10K_EIMR_DISABLE(SRAMERROR) |
1480 				FM10K_EIMR_DISABLE(VFLR) |
1481 				FM10K_EIMR_DISABLE(MAXHOLDTIME));
1482 		itr_reg = FM10K_ITR(FM10K_MBX_VECTOR);
1483 	} else {
1484 		itr_reg = FM10K_VFITR(FM10K_MBX_VECTOR);
1485 	}
1486 
1487 	fm10k_write_reg(hw, itr_reg, FM10K_ITR_MASK_SET);
1488 
1489 	free_irq(entry->vector, interface);
1490 }
1491 
1492 static s32 fm10k_mbx_mac_addr(struct fm10k_hw *hw, u32 **results,
1493 			      struct fm10k_mbx_info *mbx)
1494 {
1495 	bool vlan_override = hw->mac.vlan_override;
1496 	u16 default_vid = hw->mac.default_vid;
1497 	struct fm10k_intfc *interface;
1498 	s32 err;
1499 
1500 	err = fm10k_msg_mac_vlan_vf(hw, results, mbx);
1501 	if (err)
1502 		return err;
1503 
1504 	interface = container_of(hw, struct fm10k_intfc, hw);
1505 
1506 	/* MAC was changed so we need reset */
1507 	if (is_valid_ether_addr(hw->mac.perm_addr) &&
1508 	    !ether_addr_equal(hw->mac.perm_addr, hw->mac.addr))
1509 		set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1510 
1511 	/* VLAN override was changed, or default VLAN changed */
1512 	if ((vlan_override != hw->mac.vlan_override) ||
1513 	    (default_vid != hw->mac.default_vid))
1514 		set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1515 
1516 	return 0;
1517 }
1518 
1519 /* generic error handler for mailbox issues */
1520 static s32 fm10k_mbx_error(struct fm10k_hw *hw, u32 **results,
1521 			   struct fm10k_mbx_info __always_unused *mbx)
1522 {
1523 	struct fm10k_intfc *interface;
1524 	struct pci_dev *pdev;
1525 
1526 	interface = container_of(hw, struct fm10k_intfc, hw);
1527 	pdev = interface->pdev;
1528 
1529 	dev_err(&pdev->dev, "Unknown message ID %u\n",
1530 		**results & FM10K_TLV_ID_MASK);
1531 
1532 	return 0;
1533 }
1534 
1535 static const struct fm10k_msg_data vf_mbx_data[] = {
1536 	FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test),
1537 	FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_mbx_mac_addr),
1538 	FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_msg_lport_state_vf),
1539 	FM10K_TLV_MSG_ERROR_HANDLER(fm10k_mbx_error),
1540 };
1541 
1542 static int fm10k_mbx_request_irq_vf(struct fm10k_intfc *interface)
1543 {
1544 	struct msix_entry *entry = &interface->msix_entries[FM10K_MBX_VECTOR];
1545 	struct net_device *dev = interface->netdev;
1546 	struct fm10k_hw *hw = &interface->hw;
1547 	int err;
1548 
1549 	/* Use timer0 for interrupt moderation on the mailbox */
1550 	u32 itr = entry->entry | FM10K_INT_MAP_TIMER0;
1551 
1552 	/* register mailbox handlers */
1553 	err = hw->mbx.ops.register_handlers(&hw->mbx, vf_mbx_data);
1554 	if (err)
1555 		return err;
1556 
1557 	/* request the IRQ */
1558 	err = request_irq(entry->vector, fm10k_msix_mbx_vf, 0,
1559 			  dev->name, interface);
1560 	if (err) {
1561 		netif_err(interface, probe, dev,
1562 			  "request_irq for msix_mbx failed: %d\n", err);
1563 		return err;
1564 	}
1565 
1566 	/* map all of the interrupt sources */
1567 	fm10k_write_reg(hw, FM10K_VFINT_MAP, itr);
1568 
1569 	/* enable interrupt */
1570 	fm10k_write_reg(hw, FM10K_VFITR(entry->entry), FM10K_ITR_ENABLE);
1571 
1572 	return 0;
1573 }
1574 
1575 static s32 fm10k_lport_map(struct fm10k_hw *hw, u32 **results,
1576 			   struct fm10k_mbx_info *mbx)
1577 {
1578 	struct fm10k_intfc *interface;
1579 	u32 dglort_map = hw->mac.dglort_map;
1580 	s32 err;
1581 
1582 	interface = container_of(hw, struct fm10k_intfc, hw);
1583 
1584 	err = fm10k_msg_err_pf(hw, results, mbx);
1585 	if (!err && hw->swapi.status) {
1586 		/* force link down for a reasonable delay */
1587 		interface->link_down_event = jiffies + (2 * HZ);
1588 		set_bit(__FM10K_LINK_DOWN, interface->state);
1589 
1590 		/* reset dglort_map back to no config */
1591 		hw->mac.dglort_map = FM10K_DGLORTMAP_NONE;
1592 
1593 		fm10k_service_event_schedule(interface);
1594 
1595 		/* prevent overloading kernel message buffer */
1596 		if (interface->lport_map_failed)
1597 			return 0;
1598 
1599 		interface->lport_map_failed = true;
1600 
1601 		if (hw->swapi.status == FM10K_MSG_ERR_PEP_NOT_SCHEDULED)
1602 			dev_warn(&interface->pdev->dev,
1603 				 "cannot obtain link because the host interface is configured for a PCIe host interface bandwidth of zero\n");
1604 		dev_warn(&interface->pdev->dev,
1605 			 "request logical port map failed: %d\n",
1606 			 hw->swapi.status);
1607 
1608 		return 0;
1609 	}
1610 
1611 	err = fm10k_msg_lport_map_pf(hw, results, mbx);
1612 	if (err)
1613 		return err;
1614 
1615 	interface->lport_map_failed = false;
1616 
1617 	/* we need to reset if port count was just updated */
1618 	if (dglort_map != hw->mac.dglort_map)
1619 		set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1620 
1621 	return 0;
1622 }
1623 
1624 static s32 fm10k_update_pvid(struct fm10k_hw *hw, u32 **results,
1625 			     struct fm10k_mbx_info __always_unused *mbx)
1626 {
1627 	struct fm10k_intfc *interface;
1628 	u16 glort, pvid;
1629 	u32 pvid_update;
1630 	s32 err;
1631 
1632 	err = fm10k_tlv_attr_get_u32(results[FM10K_PF_ATTR_ID_UPDATE_PVID],
1633 				     &pvid_update);
1634 	if (err)
1635 		return err;
1636 
1637 	/* extract values from the pvid update */
1638 	glort = FM10K_MSG_HDR_FIELD_GET(pvid_update, UPDATE_PVID_GLORT);
1639 	pvid = FM10K_MSG_HDR_FIELD_GET(pvid_update, UPDATE_PVID_PVID);
1640 
1641 	/* if glort is not valid return error */
1642 	if (!fm10k_glort_valid_pf(hw, glort))
1643 		return FM10K_ERR_PARAM;
1644 
1645 	/* verify VLAN ID is valid */
1646 	if (pvid >= FM10K_VLAN_TABLE_VID_MAX)
1647 		return FM10K_ERR_PARAM;
1648 
1649 	interface = container_of(hw, struct fm10k_intfc, hw);
1650 
1651 	/* check to see if this belongs to one of the VFs */
1652 	err = fm10k_iov_update_pvid(interface, glort, pvid);
1653 	if (!err)
1654 		return 0;
1655 
1656 	/* we need to reset if default VLAN was just updated */
1657 	if (pvid != hw->mac.default_vid)
1658 		set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1659 
1660 	hw->mac.default_vid = pvid;
1661 
1662 	return 0;
1663 }
1664 
1665 static const struct fm10k_msg_data pf_mbx_data[] = {
1666 	FM10K_PF_MSG_ERR_HANDLER(XCAST_MODES, fm10k_msg_err_pf),
1667 	FM10K_PF_MSG_ERR_HANDLER(UPDATE_MAC_FWD_RULE, fm10k_msg_err_pf),
1668 	FM10K_PF_MSG_LPORT_MAP_HANDLER(fm10k_lport_map),
1669 	FM10K_PF_MSG_ERR_HANDLER(LPORT_CREATE, fm10k_msg_err_pf),
1670 	FM10K_PF_MSG_ERR_HANDLER(LPORT_DELETE, fm10k_msg_err_pf),
1671 	FM10K_PF_MSG_UPDATE_PVID_HANDLER(fm10k_update_pvid),
1672 	FM10K_TLV_MSG_ERROR_HANDLER(fm10k_mbx_error),
1673 };
1674 
1675 static int fm10k_mbx_request_irq_pf(struct fm10k_intfc *interface)
1676 {
1677 	struct msix_entry *entry = &interface->msix_entries[FM10K_MBX_VECTOR];
1678 	struct net_device *dev = interface->netdev;
1679 	struct fm10k_hw *hw = &interface->hw;
1680 	int err;
1681 
1682 	/* Use timer0 for interrupt moderation on the mailbox */
1683 	u32 mbx_itr = entry->entry | FM10K_INT_MAP_TIMER0;
1684 	u32 other_itr = entry->entry | FM10K_INT_MAP_IMMEDIATE;
1685 
1686 	/* register mailbox handlers */
1687 	err = hw->mbx.ops.register_handlers(&hw->mbx, pf_mbx_data);
1688 	if (err)
1689 		return err;
1690 
1691 	/* request the IRQ */
1692 	err = request_irq(entry->vector, fm10k_msix_mbx_pf, 0,
1693 			  dev->name, interface);
1694 	if (err) {
1695 		netif_err(interface, probe, dev,
1696 			  "request_irq for msix_mbx failed: %d\n", err);
1697 		return err;
1698 	}
1699 
1700 	/* Enable interrupts w/ no moderation for "other" interrupts */
1701 	fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_pcie_fault), other_itr);
1702 	fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_switch_up_down), other_itr);
1703 	fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_sram), other_itr);
1704 	fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_max_hold_time), other_itr);
1705 	fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_vflr), other_itr);
1706 
1707 	/* Enable interrupts w/ moderation for mailbox */
1708 	fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_mailbox), mbx_itr);
1709 
1710 	/* Enable individual interrupt causes */
1711 	fm10k_write_reg(hw, FM10K_EIMR, FM10K_EIMR_ENABLE(PCA_FAULT) |
1712 					FM10K_EIMR_ENABLE(FUM_FAULT) |
1713 					FM10K_EIMR_ENABLE(MAILBOX) |
1714 					FM10K_EIMR_ENABLE(SWITCHREADY) |
1715 					FM10K_EIMR_ENABLE(SWITCHNOTREADY) |
1716 					FM10K_EIMR_ENABLE(SRAMERROR) |
1717 					FM10K_EIMR_ENABLE(VFLR) |
1718 					FM10K_EIMR_ENABLE(MAXHOLDTIME));
1719 
1720 	/* enable interrupt */
1721 	fm10k_write_reg(hw, FM10K_ITR(entry->entry), FM10K_ITR_ENABLE);
1722 
1723 	return 0;
1724 }
1725 
1726 int fm10k_mbx_request_irq(struct fm10k_intfc *interface)
1727 {
1728 	struct fm10k_hw *hw = &interface->hw;
1729 	int err;
1730 
1731 	/* enable Mailbox cause */
1732 	if (hw->mac.type == fm10k_mac_pf)
1733 		err = fm10k_mbx_request_irq_pf(interface);
1734 	else
1735 		err = fm10k_mbx_request_irq_vf(interface);
1736 	if (err)
1737 		return err;
1738 
1739 	/* connect mailbox */
1740 	err = hw->mbx.ops.connect(hw, &hw->mbx);
1741 
1742 	/* if the mailbox failed to connect, then free IRQ */
1743 	if (err)
1744 		fm10k_mbx_free_irq(interface);
1745 
1746 	return err;
1747 }
1748 
1749 /**
1750  * fm10k_qv_free_irq - release interrupts associated with queue vectors
1751  * @interface: board private structure
1752  *
1753  * Release all interrupts associated with this interface
1754  **/
1755 void fm10k_qv_free_irq(struct fm10k_intfc *interface)
1756 {
1757 	int vector = interface->num_q_vectors;
1758 	struct fm10k_hw *hw = &interface->hw;
1759 	struct msix_entry *entry;
1760 
1761 	entry = &interface->msix_entries[NON_Q_VECTORS(hw) + vector];
1762 
1763 	while (vector) {
1764 		struct fm10k_q_vector *q_vector;
1765 
1766 		vector--;
1767 		entry--;
1768 		q_vector = interface->q_vector[vector];
1769 
1770 		if (!q_vector->tx.count && !q_vector->rx.count)
1771 			continue;
1772 
1773 		/* clear the affinity_mask in the IRQ descriptor */
1774 		irq_set_affinity_hint(entry->vector, NULL);
1775 
1776 		/* disable interrupts */
1777 		writel(FM10K_ITR_MASK_SET, q_vector->itr);
1778 
1779 		free_irq(entry->vector, q_vector);
1780 	}
1781 }
1782 
1783 /**
1784  * fm10k_qv_request_irq - initialize interrupts for queue vectors
1785  * @interface: board private structure
1786  *
1787  * Attempts to configure interrupts using the best available
1788  * capabilities of the hardware and kernel.
1789  **/
1790 int fm10k_qv_request_irq(struct fm10k_intfc *interface)
1791 {
1792 	struct net_device *dev = interface->netdev;
1793 	struct fm10k_hw *hw = &interface->hw;
1794 	struct msix_entry *entry;
1795 	unsigned int ri = 0, ti = 0;
1796 	int vector, err;
1797 
1798 	entry = &interface->msix_entries[NON_Q_VECTORS(hw)];
1799 
1800 	for (vector = 0; vector < interface->num_q_vectors; vector++) {
1801 		struct fm10k_q_vector *q_vector = interface->q_vector[vector];
1802 
1803 		/* name the vector */
1804 		if (q_vector->tx.count && q_vector->rx.count) {
1805 			snprintf(q_vector->name, sizeof(q_vector->name),
1806 				 "%s-TxRx-%u", dev->name, ri++);
1807 			ti++;
1808 		} else if (q_vector->rx.count) {
1809 			snprintf(q_vector->name, sizeof(q_vector->name),
1810 				 "%s-rx-%u", dev->name, ri++);
1811 		} else if (q_vector->tx.count) {
1812 			snprintf(q_vector->name, sizeof(q_vector->name),
1813 				 "%s-tx-%u", dev->name, ti++);
1814 		} else {
1815 			/* skip this unused q_vector */
1816 			continue;
1817 		}
1818 
1819 		/* Assign ITR register to q_vector */
1820 		q_vector->itr = (hw->mac.type == fm10k_mac_pf) ?
1821 				&interface->uc_addr[FM10K_ITR(entry->entry)] :
1822 				&interface->uc_addr[FM10K_VFITR(entry->entry)];
1823 
1824 		/* request the IRQ */
1825 		err = request_irq(entry->vector, &fm10k_msix_clean_rings, 0,
1826 				  q_vector->name, q_vector);
1827 		if (err) {
1828 			netif_err(interface, probe, dev,
1829 				  "request_irq failed for MSIX interrupt Error: %d\n",
1830 				  err);
1831 			goto err_out;
1832 		}
1833 
1834 		/* assign the mask for this irq */
1835 		irq_set_affinity_hint(entry->vector, &q_vector->affinity_mask);
1836 
1837 		/* Enable q_vector */
1838 		writel(FM10K_ITR_ENABLE, q_vector->itr);
1839 
1840 		entry++;
1841 	}
1842 
1843 	return 0;
1844 
1845 err_out:
1846 	/* wind through the ring freeing all entries and vectors */
1847 	while (vector) {
1848 		struct fm10k_q_vector *q_vector;
1849 
1850 		entry--;
1851 		vector--;
1852 		q_vector = interface->q_vector[vector];
1853 
1854 		if (!q_vector->tx.count && !q_vector->rx.count)
1855 			continue;
1856 
1857 		/* clear the affinity_mask in the IRQ descriptor */
1858 		irq_set_affinity_hint(entry->vector, NULL);
1859 
1860 		/* disable interrupts */
1861 		writel(FM10K_ITR_MASK_SET, q_vector->itr);
1862 
1863 		free_irq(entry->vector, q_vector);
1864 	}
1865 
1866 	return err;
1867 }
1868 
1869 void fm10k_up(struct fm10k_intfc *interface)
1870 {
1871 	struct fm10k_hw *hw = &interface->hw;
1872 
1873 	/* Enable Tx/Rx DMA */
1874 	hw->mac.ops.start_hw(hw);
1875 
1876 	/* configure Tx descriptor rings */
1877 	fm10k_configure_tx(interface);
1878 
1879 	/* configure Rx descriptor rings */
1880 	fm10k_configure_rx(interface);
1881 
1882 	/* configure interrupts */
1883 	hw->mac.ops.update_int_moderator(hw);
1884 
1885 	/* enable statistics capture again */
1886 	clear_bit(__FM10K_UPDATING_STATS, interface->state);
1887 
1888 	/* clear down bit to indicate we are ready to go */
1889 	clear_bit(__FM10K_DOWN, interface->state);
1890 
1891 	/* enable polling cleanups */
1892 	fm10k_napi_enable_all(interface);
1893 
1894 	/* re-establish Rx filters */
1895 	fm10k_restore_rx_state(interface);
1896 
1897 	/* enable transmits */
1898 	netif_tx_start_all_queues(interface->netdev);
1899 
1900 	/* kick off the service timer now */
1901 	hw->mac.get_host_state = true;
1902 	mod_timer(&interface->service_timer, jiffies);
1903 }
1904 
1905 static void fm10k_napi_disable_all(struct fm10k_intfc *interface)
1906 {
1907 	struct fm10k_q_vector *q_vector;
1908 	int q_idx;
1909 
1910 	for (q_idx = 0; q_idx < interface->num_q_vectors; q_idx++) {
1911 		q_vector = interface->q_vector[q_idx];
1912 		napi_disable(&q_vector->napi);
1913 	}
1914 }
1915 
1916 void fm10k_down(struct fm10k_intfc *interface)
1917 {
1918 	struct net_device *netdev = interface->netdev;
1919 	struct fm10k_hw *hw = &interface->hw;
1920 	int err, i = 0, count = 0;
1921 
1922 	/* signal that we are down to the interrupt handler and service task */
1923 	if (test_and_set_bit(__FM10K_DOWN, interface->state))
1924 		return;
1925 
1926 	/* call carrier off first to avoid false dev_watchdog timeouts */
1927 	netif_carrier_off(netdev);
1928 
1929 	/* disable transmits */
1930 	netif_tx_stop_all_queues(netdev);
1931 	netif_tx_disable(netdev);
1932 
1933 	/* reset Rx filters */
1934 	fm10k_reset_rx_state(interface);
1935 
1936 	/* disable polling routines */
1937 	fm10k_napi_disable_all(interface);
1938 
1939 	/* capture stats one last time before stopping interface */
1940 	fm10k_update_stats(interface);
1941 
1942 	/* prevent updating statistics while we're down */
1943 	while (test_and_set_bit(__FM10K_UPDATING_STATS, interface->state))
1944 		usleep_range(1000, 2000);
1945 
1946 	/* skip waiting for TX DMA if we lost PCIe link */
1947 	if (FM10K_REMOVED(hw->hw_addr))
1948 		goto skip_tx_dma_drain;
1949 
1950 	/* In some rare circumstances it can take a while for Tx queues to
1951 	 * quiesce and be fully disabled. Attempt to .stop_hw() first, and
1952 	 * then if we get ERR_REQUESTS_PENDING, go ahead and wait in a loop
1953 	 * until the Tx queues have emptied, or until a number of retries. If
1954 	 * we fail to clear within the retry loop, we will issue a warning
1955 	 * indicating that Tx DMA is probably hung. Note this means we call
1956 	 * .stop_hw() twice but this shouldn't cause any problems.
1957 	 */
1958 	err = hw->mac.ops.stop_hw(hw);
1959 	if (err != FM10K_ERR_REQUESTS_PENDING)
1960 		goto skip_tx_dma_drain;
1961 
1962 #define TX_DMA_DRAIN_RETRIES 25
1963 	for (count = 0; count < TX_DMA_DRAIN_RETRIES; count++) {
1964 		usleep_range(10000, 20000);
1965 
1966 		/* start checking at the last ring to have pending Tx */
1967 		for (; i < interface->num_tx_queues; i++)
1968 			if (fm10k_get_tx_pending(interface->tx_ring[i], false))
1969 				break;
1970 
1971 		/* if all the queues are drained, we can break now */
1972 		if (i == interface->num_tx_queues)
1973 			break;
1974 	}
1975 
1976 	if (count >= TX_DMA_DRAIN_RETRIES)
1977 		dev_err(&interface->pdev->dev,
1978 			"Tx queues failed to drain after %d tries. Tx DMA is probably hung.\n",
1979 			count);
1980 skip_tx_dma_drain:
1981 	/* Disable DMA engine for Tx/Rx */
1982 	err = hw->mac.ops.stop_hw(hw);
1983 	if (err == FM10K_ERR_REQUESTS_PENDING)
1984 		dev_err(&interface->pdev->dev,
1985 			"due to pending requests hw was not shut down gracefully\n");
1986 	else if (err)
1987 		dev_err(&interface->pdev->dev, "stop_hw failed: %d\n", err);
1988 
1989 	/* free any buffers still on the rings */
1990 	fm10k_clean_all_tx_rings(interface);
1991 	fm10k_clean_all_rx_rings(interface);
1992 }
1993 
1994 /**
1995  * fm10k_sw_init - Initialize general software structures
1996  * @interface: host interface private structure to initialize
1997  *
1998  * fm10k_sw_init initializes the interface private data structure.
1999  * Fields are initialized based on PCI device information and
2000  * OS network device settings (MTU size).
2001  **/
2002 static int fm10k_sw_init(struct fm10k_intfc *interface,
2003 			 const struct pci_device_id *ent)
2004 {
2005 	const struct fm10k_info *fi = fm10k_info_tbl[ent->driver_data];
2006 	struct fm10k_hw *hw = &interface->hw;
2007 	struct pci_dev *pdev = interface->pdev;
2008 	struct net_device *netdev = interface->netdev;
2009 	u32 rss_key[FM10K_RSSRK_SIZE];
2010 	unsigned int rss;
2011 	int err;
2012 
2013 	/* initialize back pointer */
2014 	hw->back = interface;
2015 	hw->hw_addr = interface->uc_addr;
2016 
2017 	/* PCI config space info */
2018 	hw->vendor_id = pdev->vendor;
2019 	hw->device_id = pdev->device;
2020 	hw->revision_id = pdev->revision;
2021 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
2022 	hw->subsystem_device_id = pdev->subsystem_device;
2023 
2024 	/* Setup hw api */
2025 	memcpy(&hw->mac.ops, fi->mac_ops, sizeof(hw->mac.ops));
2026 	hw->mac.type = fi->mac;
2027 
2028 	/* Setup IOV handlers */
2029 	if (fi->iov_ops)
2030 		memcpy(&hw->iov.ops, fi->iov_ops, sizeof(hw->iov.ops));
2031 
2032 	/* Set common capability flags and settings */
2033 	rss = min_t(int, FM10K_MAX_RSS_INDICES, num_online_cpus());
2034 	interface->ring_feature[RING_F_RSS].limit = rss;
2035 	fi->get_invariants(hw);
2036 
2037 	/* pick up the PCIe bus settings for reporting later */
2038 	if (hw->mac.ops.get_bus_info)
2039 		hw->mac.ops.get_bus_info(hw);
2040 
2041 	/* limit the usable DMA range */
2042 	if (hw->mac.ops.set_dma_mask)
2043 		hw->mac.ops.set_dma_mask(hw, dma_get_mask(&pdev->dev));
2044 
2045 	/* update netdev with DMA restrictions */
2046 	if (dma_get_mask(&pdev->dev) > DMA_BIT_MASK(32)) {
2047 		netdev->features |= NETIF_F_HIGHDMA;
2048 		netdev->vlan_features |= NETIF_F_HIGHDMA;
2049 	}
2050 
2051 	/* reset and initialize the hardware so it is in a known state */
2052 	err = hw->mac.ops.reset_hw(hw);
2053 	if (err) {
2054 		dev_err(&pdev->dev, "reset_hw failed: %d\n", err);
2055 		return err;
2056 	}
2057 
2058 	err = hw->mac.ops.init_hw(hw);
2059 	if (err) {
2060 		dev_err(&pdev->dev, "init_hw failed: %d\n", err);
2061 		return err;
2062 	}
2063 
2064 	/* initialize hardware statistics */
2065 	hw->mac.ops.update_hw_stats(hw, &interface->stats);
2066 
2067 	/* Set upper limit on IOV VFs that can be allocated */
2068 	pci_sriov_set_totalvfs(pdev, hw->iov.total_vfs);
2069 
2070 	/* Start with random Ethernet address */
2071 	eth_random_addr(hw->mac.addr);
2072 
2073 	/* Initialize MAC address from hardware */
2074 	err = hw->mac.ops.read_mac_addr(hw);
2075 	if (err) {
2076 		dev_warn(&pdev->dev,
2077 			 "Failed to obtain MAC address defaulting to random\n");
2078 		/* tag address assignment as random */
2079 		netdev->addr_assign_type |= NET_ADDR_RANDOM;
2080 	}
2081 
2082 	ether_addr_copy(netdev->dev_addr, hw->mac.addr);
2083 	ether_addr_copy(netdev->perm_addr, hw->mac.addr);
2084 
2085 	if (!is_valid_ether_addr(netdev->perm_addr)) {
2086 		dev_err(&pdev->dev, "Invalid MAC Address\n");
2087 		return -EIO;
2088 	}
2089 
2090 	/* initialize DCBNL interface */
2091 	fm10k_dcbnl_set_ops(netdev);
2092 
2093 	/* set default ring sizes */
2094 	interface->tx_ring_count = FM10K_DEFAULT_TXD;
2095 	interface->rx_ring_count = FM10K_DEFAULT_RXD;
2096 
2097 	/* set default interrupt moderation */
2098 	interface->tx_itr = FM10K_TX_ITR_DEFAULT;
2099 	interface->rx_itr = FM10K_ITR_ADAPTIVE | FM10K_RX_ITR_DEFAULT;
2100 
2101 	/* initialize udp port lists */
2102 	INIT_LIST_HEAD(&interface->vxlan_port);
2103 	INIT_LIST_HEAD(&interface->geneve_port);
2104 
2105 	/* Initialize the MAC/VLAN queue */
2106 	INIT_LIST_HEAD(&interface->macvlan_requests);
2107 
2108 	netdev_rss_key_fill(rss_key, sizeof(rss_key));
2109 	memcpy(interface->rssrk, rss_key, sizeof(rss_key));
2110 
2111 	/* Initialize the mailbox lock */
2112 	spin_lock_init(&interface->mbx_lock);
2113 	spin_lock_init(&interface->macvlan_lock);
2114 
2115 	/* Start off interface as being down */
2116 	set_bit(__FM10K_DOWN, interface->state);
2117 	set_bit(__FM10K_UPDATING_STATS, interface->state);
2118 
2119 	return 0;
2120 }
2121 
2122 static void fm10k_slot_warn(struct fm10k_intfc *interface)
2123 {
2124 	enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
2125 	enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
2126 	struct fm10k_hw *hw = &interface->hw;
2127 	int max_gts = 0, expected_gts = 0;
2128 
2129 	if (pcie_get_minimum_link(interface->pdev, &speed, &width) ||
2130 	    speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
2131 		dev_warn(&interface->pdev->dev,
2132 			 "Unable to determine PCI Express bandwidth.\n");
2133 		return;
2134 	}
2135 
2136 	switch (speed) {
2137 	case PCIE_SPEED_2_5GT:
2138 		/* 8b/10b encoding reduces max throughput by 20% */
2139 		max_gts = 2 * width;
2140 		break;
2141 	case PCIE_SPEED_5_0GT:
2142 		/* 8b/10b encoding reduces max throughput by 20% */
2143 		max_gts = 4 * width;
2144 		break;
2145 	case PCIE_SPEED_8_0GT:
2146 		/* 128b/130b encoding has less than 2% impact on throughput */
2147 		max_gts = 8 * width;
2148 		break;
2149 	default:
2150 		dev_warn(&interface->pdev->dev,
2151 			 "Unable to determine PCI Express bandwidth.\n");
2152 		return;
2153 	}
2154 
2155 	dev_info(&interface->pdev->dev,
2156 		 "PCI Express bandwidth of %dGT/s available\n",
2157 		 max_gts);
2158 	dev_info(&interface->pdev->dev,
2159 		 "(Speed:%s, Width: x%d, Encoding Loss:%s, Payload:%s)\n",
2160 		 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
2161 		  speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
2162 		  speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
2163 		  "Unknown"),
2164 		 hw->bus.width,
2165 		 (speed == PCIE_SPEED_2_5GT ? "20%" :
2166 		  speed == PCIE_SPEED_5_0GT ? "20%" :
2167 		  speed == PCIE_SPEED_8_0GT ? "<2%" :
2168 		  "Unknown"),
2169 		 (hw->bus.payload == fm10k_bus_payload_128 ? "128B" :
2170 		  hw->bus.payload == fm10k_bus_payload_256 ? "256B" :
2171 		  hw->bus.payload == fm10k_bus_payload_512 ? "512B" :
2172 		  "Unknown"));
2173 
2174 	switch (hw->bus_caps.speed) {
2175 	case fm10k_bus_speed_2500:
2176 		/* 8b/10b encoding reduces max throughput by 20% */
2177 		expected_gts = 2 * hw->bus_caps.width;
2178 		break;
2179 	case fm10k_bus_speed_5000:
2180 		/* 8b/10b encoding reduces max throughput by 20% */
2181 		expected_gts = 4 * hw->bus_caps.width;
2182 		break;
2183 	case fm10k_bus_speed_8000:
2184 		/* 128b/130b encoding has less than 2% impact on throughput */
2185 		expected_gts = 8 * hw->bus_caps.width;
2186 		break;
2187 	default:
2188 		dev_warn(&interface->pdev->dev,
2189 			 "Unable to determine expected PCI Express bandwidth.\n");
2190 		return;
2191 	}
2192 
2193 	if (max_gts >= expected_gts)
2194 		return;
2195 
2196 	dev_warn(&interface->pdev->dev,
2197 		 "This device requires %dGT/s of bandwidth for optimal performance.\n",
2198 		 expected_gts);
2199 	dev_warn(&interface->pdev->dev,
2200 		 "A %sslot with x%d lanes is suggested.\n",
2201 		 (hw->bus_caps.speed == fm10k_bus_speed_2500 ? "2.5GT/s " :
2202 		  hw->bus_caps.speed == fm10k_bus_speed_5000 ? "5.0GT/s " :
2203 		  hw->bus_caps.speed == fm10k_bus_speed_8000 ? "8.0GT/s " : ""),
2204 		 hw->bus_caps.width);
2205 }
2206 
2207 /**
2208  * fm10k_probe - Device Initialization Routine
2209  * @pdev: PCI device information struct
2210  * @ent: entry in fm10k_pci_tbl
2211  *
2212  * Returns 0 on success, negative on failure
2213  *
2214  * fm10k_probe initializes an interface identified by a pci_dev structure.
2215  * The OS initialization, configuring of the interface private structure,
2216  * and a hardware reset occur.
2217  **/
2218 static int fm10k_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2219 {
2220 	struct net_device *netdev;
2221 	struct fm10k_intfc *interface;
2222 	int err;
2223 
2224 	if (pdev->error_state != pci_channel_io_normal) {
2225 		dev_err(&pdev->dev,
2226 			"PCI device still in an error state. Unable to load...\n");
2227 		return -EIO;
2228 	}
2229 
2230 	err = pci_enable_device_mem(pdev);
2231 	if (err) {
2232 		dev_err(&pdev->dev,
2233 			"PCI enable device failed: %d\n", err);
2234 		return err;
2235 	}
2236 
2237 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48));
2238 	if (err)
2239 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2240 	if (err) {
2241 		dev_err(&pdev->dev,
2242 			"DMA configuration failed: %d\n", err);
2243 		goto err_dma;
2244 	}
2245 
2246 	err = pci_request_mem_regions(pdev, fm10k_driver_name);
2247 	if (err) {
2248 		dev_err(&pdev->dev,
2249 			"pci_request_selected_regions failed: %d\n", err);
2250 		goto err_pci_reg;
2251 	}
2252 
2253 	pci_enable_pcie_error_reporting(pdev);
2254 
2255 	pci_set_master(pdev);
2256 	pci_save_state(pdev);
2257 
2258 	netdev = fm10k_alloc_netdev(fm10k_info_tbl[ent->driver_data]);
2259 	if (!netdev) {
2260 		err = -ENOMEM;
2261 		goto err_alloc_netdev;
2262 	}
2263 
2264 	SET_NETDEV_DEV(netdev, &pdev->dev);
2265 
2266 	interface = netdev_priv(netdev);
2267 	pci_set_drvdata(pdev, interface);
2268 
2269 	interface->netdev = netdev;
2270 	interface->pdev = pdev;
2271 
2272 	interface->uc_addr = ioremap(pci_resource_start(pdev, 0),
2273 				     FM10K_UC_ADDR_SIZE);
2274 	if (!interface->uc_addr) {
2275 		err = -EIO;
2276 		goto err_ioremap;
2277 	}
2278 
2279 	err = fm10k_sw_init(interface, ent);
2280 	if (err)
2281 		goto err_sw_init;
2282 
2283 	/* enable debugfs support */
2284 	fm10k_dbg_intfc_init(interface);
2285 
2286 	err = fm10k_init_queueing_scheme(interface);
2287 	if (err)
2288 		goto err_sw_init;
2289 
2290 	/* the mbx interrupt might attempt to schedule the service task, so we
2291 	 * must ensure it is disabled since we haven't yet requested the timer
2292 	 * or work item.
2293 	 */
2294 	set_bit(__FM10K_SERVICE_DISABLE, interface->state);
2295 
2296 	err = fm10k_mbx_request_irq(interface);
2297 	if (err)
2298 		goto err_mbx_interrupt;
2299 
2300 	/* final check of hardware state before registering the interface */
2301 	err = fm10k_hw_ready(interface);
2302 	if (err)
2303 		goto err_register;
2304 
2305 	err = register_netdev(netdev);
2306 	if (err)
2307 		goto err_register;
2308 
2309 	/* carrier off reporting is important to ethtool even BEFORE open */
2310 	netif_carrier_off(netdev);
2311 
2312 	/* stop all the transmit queues from transmitting until link is up */
2313 	netif_tx_stop_all_queues(netdev);
2314 
2315 	/* Initialize service timer and service task late in order to avoid
2316 	 * cleanup issues.
2317 	 */
2318 	setup_timer(&interface->service_timer, &fm10k_service_timer,
2319 		    (unsigned long)interface);
2320 	INIT_WORK(&interface->service_task, fm10k_service_task);
2321 
2322 	/* Setup the MAC/VLAN queue */
2323 	INIT_DELAYED_WORK(&interface->macvlan_task, fm10k_macvlan_task);
2324 
2325 	/* kick off service timer now, even when interface is down */
2326 	mod_timer(&interface->service_timer, (HZ * 2) + jiffies);
2327 
2328 	/* print warning for non-optimal configurations */
2329 	fm10k_slot_warn(interface);
2330 
2331 	/* report MAC address for logging */
2332 	dev_info(&pdev->dev, "%pM\n", netdev->dev_addr);
2333 
2334 	/* enable SR-IOV after registering netdev to enforce PF/VF ordering */
2335 	fm10k_iov_configure(pdev, 0);
2336 
2337 	/* clear the service task disable bit and kick off service task */
2338 	clear_bit(__FM10K_SERVICE_DISABLE, interface->state);
2339 	fm10k_service_event_schedule(interface);
2340 
2341 	return 0;
2342 
2343 err_register:
2344 	fm10k_mbx_free_irq(interface);
2345 err_mbx_interrupt:
2346 	fm10k_clear_queueing_scheme(interface);
2347 err_sw_init:
2348 	if (interface->sw_addr)
2349 		iounmap(interface->sw_addr);
2350 	iounmap(interface->uc_addr);
2351 err_ioremap:
2352 	free_netdev(netdev);
2353 err_alloc_netdev:
2354 	pci_release_mem_regions(pdev);
2355 err_pci_reg:
2356 err_dma:
2357 	pci_disable_device(pdev);
2358 	return err;
2359 }
2360 
2361 /**
2362  * fm10k_remove - Device Removal Routine
2363  * @pdev: PCI device information struct
2364  *
2365  * fm10k_remove is called by the PCI subsystem to alert the driver
2366  * that it should release a PCI device.  The could be caused by a
2367  * Hot-Plug event, or because the driver is going to be removed from
2368  * memory.
2369  **/
2370 static void fm10k_remove(struct pci_dev *pdev)
2371 {
2372 	struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2373 	struct net_device *netdev = interface->netdev;
2374 
2375 	del_timer_sync(&interface->service_timer);
2376 
2377 	fm10k_stop_service_event(interface);
2378 	fm10k_stop_macvlan_task(interface);
2379 
2380 	/* Remove all pending MAC/VLAN requests */
2381 	fm10k_clear_macvlan_queue(interface, interface->glort, true);
2382 
2383 	/* free netdev, this may bounce the interrupts due to setup_tc */
2384 	if (netdev->reg_state == NETREG_REGISTERED)
2385 		unregister_netdev(netdev);
2386 
2387 	/* release VFs */
2388 	fm10k_iov_disable(pdev);
2389 
2390 	/* disable mailbox interrupt */
2391 	fm10k_mbx_free_irq(interface);
2392 
2393 	/* free interrupts */
2394 	fm10k_clear_queueing_scheme(interface);
2395 
2396 	/* remove any debugfs interfaces */
2397 	fm10k_dbg_intfc_exit(interface);
2398 
2399 	if (interface->sw_addr)
2400 		iounmap(interface->sw_addr);
2401 	iounmap(interface->uc_addr);
2402 
2403 	free_netdev(netdev);
2404 
2405 	pci_release_mem_regions(pdev);
2406 
2407 	pci_disable_pcie_error_reporting(pdev);
2408 
2409 	pci_disable_device(pdev);
2410 }
2411 
2412 static void fm10k_prepare_suspend(struct fm10k_intfc *interface)
2413 {
2414 	/* the watchdog task reads from registers, which might appear like
2415 	 * a surprise remove if the PCIe device is disabled while we're
2416 	 * stopped. We stop the watchdog task until after we resume software
2417 	 * activity.
2418 	 *
2419 	 * Note that the MAC/VLAN task will be stopped as part of preparing
2420 	 * for reset so we don't need to handle it here.
2421 	 */
2422 	fm10k_stop_service_event(interface);
2423 
2424 	if (fm10k_prepare_for_reset(interface))
2425 		set_bit(__FM10K_RESET_SUSPENDED, interface->state);
2426 }
2427 
2428 static int fm10k_handle_resume(struct fm10k_intfc *interface)
2429 {
2430 	struct fm10k_hw *hw = &interface->hw;
2431 	int err;
2432 
2433 	/* Even if we didn't properly prepare for reset in
2434 	 * fm10k_prepare_suspend, we'll attempt to resume anyways.
2435 	 */
2436 	if (!test_and_clear_bit(__FM10K_RESET_SUSPENDED, interface->state))
2437 		dev_warn(&interface->pdev->dev,
2438 			 "Device was shut down as part of suspend... Attempting to recover\n");
2439 
2440 	/* reset statistics starting values */
2441 	hw->mac.ops.rebind_hw_stats(hw, &interface->stats);
2442 
2443 	err = fm10k_handle_reset(interface);
2444 	if (err)
2445 		return err;
2446 
2447 	/* assume host is not ready, to prevent race with watchdog in case we
2448 	 * actually don't have connection to the switch
2449 	 */
2450 	interface->host_ready = false;
2451 	fm10k_watchdog_host_not_ready(interface);
2452 
2453 	/* force link to stay down for a second to prevent link flutter */
2454 	interface->link_down_event = jiffies + (HZ);
2455 	set_bit(__FM10K_LINK_DOWN, interface->state);
2456 
2457 	/* restart the service task */
2458 	fm10k_start_service_event(interface);
2459 
2460 	/* Restart the MAC/VLAN request queue in-case of outstanding events */
2461 	fm10k_macvlan_schedule(interface);
2462 
2463 	return err;
2464 }
2465 
2466 #ifdef CONFIG_PM
2467 /**
2468  * fm10k_resume - Generic PM resume hook
2469  * @dev: generic device structure
2470  *
2471  * Generic PM hook used when waking the device from a low power state after
2472  * suspend or hibernation. This function does not need to handle lower PCIe
2473  * device state as the stack takes care of that for us.
2474  **/
2475 static int fm10k_resume(struct device *dev)
2476 {
2477 	struct fm10k_intfc *interface = pci_get_drvdata(to_pci_dev(dev));
2478 	struct net_device *netdev = interface->netdev;
2479 	struct fm10k_hw *hw = &interface->hw;
2480 	int err;
2481 
2482 	/* refresh hw_addr in case it was dropped */
2483 	hw->hw_addr = interface->uc_addr;
2484 
2485 	err = fm10k_handle_resume(interface);
2486 	if (err)
2487 		return err;
2488 
2489 	netif_device_attach(netdev);
2490 
2491 	return 0;
2492 }
2493 
2494 /**
2495  * fm10k_suspend - Generic PM suspend hook
2496  * @dev: generic device structure
2497  *
2498  * Generic PM hook used when setting the device into a low power state for
2499  * system suspend or hibernation. This function does not need to handle lower
2500  * PCIe device state as the stack takes care of that for us.
2501  **/
2502 static int fm10k_suspend(struct device *dev)
2503 {
2504 	struct fm10k_intfc *interface = pci_get_drvdata(to_pci_dev(dev));
2505 	struct net_device *netdev = interface->netdev;
2506 
2507 	netif_device_detach(netdev);
2508 
2509 	fm10k_prepare_suspend(interface);
2510 
2511 	return 0;
2512 }
2513 
2514 #endif /* CONFIG_PM */
2515 
2516 /**
2517  * fm10k_io_error_detected - called when PCI error is detected
2518  * @pdev: Pointer to PCI device
2519  * @state: The current pci connection state
2520  *
2521  * This function is called after a PCI bus error affecting
2522  * this device has been detected.
2523  */
2524 static pci_ers_result_t fm10k_io_error_detected(struct pci_dev *pdev,
2525 						pci_channel_state_t state)
2526 {
2527 	struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2528 	struct net_device *netdev = interface->netdev;
2529 
2530 	netif_device_detach(netdev);
2531 
2532 	if (state == pci_channel_io_perm_failure)
2533 		return PCI_ERS_RESULT_DISCONNECT;
2534 
2535 	fm10k_prepare_suspend(interface);
2536 
2537 	/* Request a slot reset. */
2538 	return PCI_ERS_RESULT_NEED_RESET;
2539 }
2540 
2541 /**
2542  * fm10k_io_slot_reset - called after the pci bus has been reset.
2543  * @pdev: Pointer to PCI device
2544  *
2545  * Restart the card from scratch, as if from a cold-boot.
2546  */
2547 static pci_ers_result_t fm10k_io_slot_reset(struct pci_dev *pdev)
2548 {
2549 	pci_ers_result_t result;
2550 
2551 	if (pci_reenable_device(pdev)) {
2552 		dev_err(&pdev->dev,
2553 			"Cannot re-enable PCI device after reset.\n");
2554 		result = PCI_ERS_RESULT_DISCONNECT;
2555 	} else {
2556 		pci_set_master(pdev);
2557 		pci_restore_state(pdev);
2558 
2559 		/* After second error pci->state_saved is false, this
2560 		 * resets it so EEH doesn't break.
2561 		 */
2562 		pci_save_state(pdev);
2563 
2564 		pci_wake_from_d3(pdev, false);
2565 
2566 		result = PCI_ERS_RESULT_RECOVERED;
2567 	}
2568 
2569 	pci_cleanup_aer_uncorrect_error_status(pdev);
2570 
2571 	return result;
2572 }
2573 
2574 /**
2575  * fm10k_io_resume - called when traffic can start flowing again.
2576  * @pdev: Pointer to PCI device
2577  *
2578  * This callback is called when the error recovery driver tells us that
2579  * its OK to resume normal operation.
2580  */
2581 static void fm10k_io_resume(struct pci_dev *pdev)
2582 {
2583 	struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2584 	struct net_device *netdev = interface->netdev;
2585 	int err;
2586 
2587 	err = fm10k_handle_resume(interface);
2588 
2589 	if (err)
2590 		dev_warn(&pdev->dev,
2591 			 "%s failed: %d\n", __func__, err);
2592 	else
2593 		netif_device_attach(netdev);
2594 }
2595 
2596 /**
2597  * fm10k_io_reset_prepare - called when PCI function is about to be reset
2598  * @pdev: Pointer to PCI device
2599  *
2600  * This callback is called when the PCI function is about to be reset,
2601  * allowing the device driver to prepare for it.
2602  */
2603 static void fm10k_io_reset_prepare(struct pci_dev *pdev)
2604 {
2605 	/* warn incase we have any active VF devices */
2606 	if (pci_num_vf(pdev))
2607 		dev_warn(&pdev->dev,
2608 			 "PCIe FLR may cause issues for any active VF devices\n");
2609 	fm10k_prepare_suspend(pci_get_drvdata(pdev));
2610 }
2611 
2612 /**
2613  * fm10k_io_reset_done - called when PCI function has finished resetting
2614  * @pdev: Pointer to PCI device
2615  *
2616  * This callback is called just after the PCI function is reset, such as via
2617  * /sys/class/net/<enpX>/device/reset or similar.
2618  */
2619 static void fm10k_io_reset_done(struct pci_dev *pdev)
2620 {
2621 	struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2622 	int err = fm10k_handle_resume(interface);
2623 
2624 	if (err) {
2625 		dev_warn(&pdev->dev,
2626 			 "%s failed: %d\n", __func__, err);
2627 		netif_device_detach(interface->netdev);
2628 	}
2629 }
2630 
2631 static const struct pci_error_handlers fm10k_err_handler = {
2632 	.error_detected = fm10k_io_error_detected,
2633 	.slot_reset = fm10k_io_slot_reset,
2634 	.resume = fm10k_io_resume,
2635 	.reset_prepare = fm10k_io_reset_prepare,
2636 	.reset_done = fm10k_io_reset_done,
2637 };
2638 
2639 static SIMPLE_DEV_PM_OPS(fm10k_pm_ops, fm10k_suspend, fm10k_resume);
2640 
2641 static struct pci_driver fm10k_driver = {
2642 	.name			= fm10k_driver_name,
2643 	.id_table		= fm10k_pci_tbl,
2644 	.probe			= fm10k_probe,
2645 	.remove			= fm10k_remove,
2646 #ifdef CONFIG_PM
2647 	.driver = {
2648 		.pm		= &fm10k_pm_ops,
2649 	},
2650 #endif /* CONFIG_PM */
2651 	.sriov_configure	= fm10k_iov_configure,
2652 	.err_handler		= &fm10k_err_handler
2653 };
2654 
2655 /**
2656  * fm10k_register_pci_driver - register driver interface
2657  *
2658  * This function is called on module load in order to register the driver.
2659  **/
2660 int fm10k_register_pci_driver(void)
2661 {
2662 	return pci_register_driver(&fm10k_driver);
2663 }
2664 
2665 /**
2666  * fm10k_unregister_pci_driver - unregister driver interface
2667  *
2668  * This function is called on module unload in order to remove the driver.
2669  **/
2670 void fm10k_unregister_pci_driver(void)
2671 {
2672 	pci_unregister_driver(&fm10k_driver);
2673 }
2674