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