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