xref: /linux/drivers/thunderbolt/nhi.c (revision 42bcac323cc3b16566fed8c5b250318ad02a0de0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Thunderbolt driver - NHI driver
4  *
5  * The NHI (native host interface) is the device that allows us to send and
6  * receive frames from the thunderbolt bus.
7  *
8  * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
9  * Copyright (C) 2018, Intel Corporation
10  */
11 
12 #include <linux/pm_runtime.h>
13 #include <linux/slab.h>
14 #include <linux/errno.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/interrupt.h>
17 #include <linux/iommu.h>
18 #include <linux/lockdep.h>
19 #include <linux/module.h>
20 #include <linux/delay.h>
21 #include <linux/property.h>
22 #include <linux/string_choices.h>
23 #include <linux/string_helpers.h>
24 
25 #include "nhi.h"
26 #include "nhi_regs.h"
27 #include "tb.h"
28 
29 #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")
30 
31 #define RING_FIRST_USABLE_HOPID	1
32 /*
33  * Used with QUIRK_E2E to specify an unused HopID the Rx credits are
34  * transferred.
35  */
36 #define RING_E2E_RESERVED_HOPID	RING_FIRST_USABLE_HOPID
37 
38 #define NHI_MAILBOX_TIMEOUT	500 /* ms */
39 
40 static bool host_reset = true;
41 module_param(host_reset, bool, 0444);
42 MODULE_PARM_DESC(host_reset, "reset USB4 host router (default: true)");
43 
44 /* Returns absolute bit number of the ring in the interrupt registers */
45 static int ring_interrupt_index(const struct tb_ring *ring)
46 {
47 	int bit = ring->hop;
48 	if (!ring->is_tx)
49 		bit += ring->nhi->hop_count;
50 	return bit;
51 }
52 
53 static void nhi_mask_interrupt(struct tb_nhi *nhi, u32 mask, int reg_index)
54 {
55 	int offset = reg_index * 4;
56 	u32 val;
57 
58 	/* Use shadow copy instead of reading the register */
59 	val = nhi->interrupt_mask[reg_index] & ~mask;
60 	nhi->interrupt_mask[reg_index] = val;
61 
62 	if (nhi->quirks & QUIRK_AUTO_CLEAR_INT)
63 		iowrite32(val, nhi->iobase + REG_RING_INTERRUPT_BASE + offset);
64 	else
65 		iowrite32(mask, nhi->iobase + REG_RING_INTERRUPT_MASK_CLEAR_BASE + offset);
66 }
67 
68 static void nhi_unmask_interrupt(struct tb_nhi *nhi, u32 mask, int reg_index)
69 {
70 	int offset = reg_index * 4;
71 	u32 val;
72 
73 	/* Use shadow copy instead of reading the register */
74 	val = nhi->interrupt_mask[reg_index] | mask;
75 	nhi->interrupt_mask[reg_index] = val;
76 
77 	iowrite32(val, nhi->iobase + REG_RING_INTERRUPT_BASE + offset);
78 }
79 
80 static void nhi_clear_interrupt(struct tb_nhi *nhi, int reg_index)
81 {
82 	int offset = reg_index * 4;
83 
84 	if (nhi->quirks & QUIRK_AUTO_CLEAR_INT)
85 		ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + offset);
86 	else
87 		iowrite32(~0, nhi->iobase + REG_RING_INT_CLEAR + offset);
88 }
89 
90 /*
91  * ring_interrupt_active() - activate/deactivate interrupts for a single ring
92  *
93  * ring->nhi->lock must be held.
94  */
95 static void ring_interrupt_active(struct tb_ring *ring, bool active)
96 {
97 	int interrupt_index = ring_interrupt_index(ring);
98 	int reg_index = interrupt_index / 32;
99 	int reg = REG_RING_INTERRUPT_BASE + reg_index * 4;
100 	int interrupt_bit = interrupt_index % 32;
101 	u32 mask = BIT(interrupt_bit);
102 	u32 old, new;
103 
104 	if (ring->irq > 0) {
105 		u32 step, shift, ivr, misc, itr;
106 		void __iomem *ivr_base;
107 		int auto_clear_bit;
108 
109 		/*
110 		 * Intel routers support a bit that isn't part of
111 		 * the USB4 spec to ask the hardware to clear
112 		 * interrupt status bits automatically since
113 		 * we already know which interrupt was triggered.
114 		 *
115 		 * Other routers explicitly disable auto-clear
116 		 * to prevent conditions that may occur where two
117 		 * MSIX interrupts are simultaneously active and
118 		 * reading the register clears both of them.
119 		 */
120 		misc = ioread32(ring->nhi->iobase + REG_DMA_MISC);
121 		if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
122 			auto_clear_bit = REG_DMA_MISC_INT_AUTO_CLEAR;
123 		else
124 			auto_clear_bit = REG_DMA_MISC_DISABLE_AUTO_CLEAR;
125 		if (!(misc & auto_clear_bit))
126 			iowrite32(misc | auto_clear_bit,
127 				  ring->nhi->iobase + REG_DMA_MISC);
128 
129 		ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE;
130 		step = interrupt_index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
131 		shift = interrupt_index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
132 		ivr = ioread32(ivr_base + step);
133 		ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift);
134 		if (active)
135 			ivr |= ring->vector << shift;
136 		iowrite32(ivr, ivr_base + step);
137 
138 		/* Throttling is specified in 256ns increments */
139 		itr = DIV_ROUND_UP(ring->interval_nsec, 256);
140 		itr &= REG_INT_THROTTLING_RATE_INTERVAL_MASK;
141 		iowrite32(itr, ring->nhi->iobase + REG_INT_THROTTLING_RATE +
142 			  ring->vector * 4);
143 	}
144 
145 	old = ring->nhi->interrupt_mask[reg_index];
146 	if (active)
147 		new = old | mask;
148 	else
149 		new = old & ~mask;
150 
151 	dev_dbg(ring->nhi->dev,
152 		"%s interrupt at register %#x bit %d (%#x -> %#x)\n",
153 		active ? "enabling" : "disabling", reg, interrupt_bit, old, new);
154 
155 	if (new == old) {
156 		/*
157 		 * Rings that are polled mask the interrupt while the
158 		 * completions are being advanced (see __ring_interrupt())
159 		 * so for those it can already be disabled by the time
160 		 * the ring is stopped.
161 		 */
162 		if (active || !ring->start_poll)
163 			dev_WARN(ring->nhi->dev,
164 				 "interrupt for %s %d is already %s\n",
165 				 RING_TYPE(ring), ring->hop,
166 				 str_enabled_disabled(active));
167 	}
168 
169 	if (active)
170 		nhi_unmask_interrupt(ring->nhi, mask, reg_index);
171 	else
172 		nhi_mask_interrupt(ring->nhi, mask, reg_index);
173 }
174 
175 /*
176  * nhi_disable_interrupts() - disable interrupts for all rings
177  *
178  * Use only during init and shutdown.
179  */
180 void nhi_disable_interrupts(struct tb_nhi *nhi)
181 {
182 	int i = 0;
183 	/* disable interrupts */
184 	for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++)
185 		nhi_mask_interrupt(nhi, ~0, i);
186 
187 	/* clear interrupt status bits */
188 	for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++)
189 		nhi_clear_interrupt(nhi, i);
190 }
191 
192 /* ring helper methods */
193 
194 static void __iomem *ring_desc_base(struct tb_ring *ring)
195 {
196 	void __iomem *io = ring->nhi->iobase;
197 	io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE;
198 	io += ring->hop * 16;
199 	return io;
200 }
201 
202 static void __iomem *ring_options_base(struct tb_ring *ring)
203 {
204 	void __iomem *io = ring->nhi->iobase;
205 	io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE;
206 	io += ring->hop * 32;
207 	return io;
208 }
209 
210 static void ring_iowrite_cons(struct tb_ring *ring, u16 cons)
211 {
212 	/*
213 	 * The other 16-bits in the register is read-only and writes to it
214 	 * are ignored by the hardware so we can save one ioread32() by
215 	 * filling the read-only bits with zeroes.
216 	 */
217 	iowrite32(cons, ring_desc_base(ring) + 8);
218 }
219 
220 static void ring_iowrite_prod(struct tb_ring *ring, u16 prod)
221 {
222 	/* See ring_iowrite_cons() above for explanation */
223 	iowrite32(prod << 16, ring_desc_base(ring) + 8);
224 }
225 
226 static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
227 {
228 	iowrite32(value, ring_desc_base(ring) + offset);
229 }
230 
231 static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
232 {
233 	iowrite32(value, ring_desc_base(ring) + offset);
234 	iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
235 }
236 
237 static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
238 {
239 	iowrite32(value, ring_options_base(ring) + offset);
240 }
241 
242 static bool ring_full(struct tb_ring *ring)
243 {
244 	return ((ring->head + 1) % ring->size) == ring->tail;
245 }
246 
247 static bool ring_empty(struct tb_ring *ring)
248 {
249 	return ring->head == ring->tail;
250 }
251 
252 static void __ring_notify(struct tb_ring *ring)
253 {
254 	lockdep_assert_held(&ring->lock);
255 
256 	if (ring->notify_pending) {
257 		/*
258 		 * The doorbell carries the absolute index of the head
259 		 * so a single write covers all the descriptors posted
260 		 * since the previous one.
261 		 */
262 		if (ring->is_tx)
263 			ring_iowrite_prod(ring, ring->head);
264 		else
265 			ring_iowrite_cons(ring, ring->head);
266 	}
267 	ring->notify_pending = false;
268 }
269 
270 /*
271  * ring_write_descriptors() - post frames from ring->queue to the controller
272  * @ring: Ring to post the frames to
273  * @notify: Notify the controller about the posted descriptors
274  *
275  * Unless @notify is %true the controller is not notified about the posted
276  * descriptors and the caller is expected to call tb_ring_notify() once it
277  * is done queuing frames. This allows batching of frames before
278  * updating the producer/consumer indices.
279  *
280  * ring->lock is held.
281  */
282 static void ring_write_descriptors(struct tb_ring *ring, bool notify)
283 {
284 	struct ring_frame *frame, *n;
285 	struct ring_desc *descriptor;
286 	u32 flags;
287 
288 	flags = RING_DESC_POSTED;
289 	if (!(ring->flags & RING_FLAG_NO_INTERRUPT))
290 		flags |= RING_DESC_INTERRUPT;
291 
292 	list_for_each_entry_safe(frame, n, &ring->queue, list) {
293 		if (ring_full(ring))
294 			break;
295 		list_move_tail(&frame->list, &ring->in_flight);
296 		descriptor = &ring->descriptors[ring->head];
297 		descriptor->phys = frame->buffer_phy;
298 		descriptor->time = 0;
299 		descriptor->flags = flags;
300 		if (ring->is_tx) {
301 			descriptor->length = frame->size;
302 			descriptor->eof = frame->eof;
303 			descriptor->sof = frame->sof;
304 		}
305 		ring->head = (ring->head + 1) % ring->size;
306 		ring->notify_pending = true;
307 	}
308 
309 	if (notify)
310 		__ring_notify(ring);
311 }
312 
313 /*
314  * ring_work() - progress completed frames
315  *
316  * If the ring is shutting down then all frames are marked as canceled and
317  * their callbacks are invoked.
318  *
319  * Otherwise we collect all completed frame from the ring buffer, write new
320  * frame to the ring buffer and invoke the callbacks for the completed frames.
321  */
322 static void ring_work(struct work_struct *work)
323 {
324 	struct tb_ring *ring = container_of(work, typeof(*ring), work);
325 	struct ring_frame *frame;
326 	bool canceled = false;
327 	unsigned long flags;
328 	LIST_HEAD(done);
329 
330 	spin_lock_irqsave(&ring->lock, flags);
331 
332 	if (!ring->running) {
333 		/*  Move all frames to done and mark them as canceled. */
334 		list_splice_tail_init(&ring->in_flight, &done);
335 		list_splice_tail_init(&ring->queue, &done);
336 		canceled = true;
337 		goto invoke_callback;
338 	}
339 
340 	while (!ring_empty(ring)) {
341 		if (!(ring->descriptors[ring->tail].flags
342 				& RING_DESC_COMPLETED))
343 			break;
344 		frame = list_first_entry(&ring->in_flight, typeof(*frame),
345 					 list);
346 		list_move_tail(&frame->list, &done);
347 		if (!ring->is_tx) {
348 			frame->size = ring->descriptors[ring->tail].length;
349 			frame->eof = ring->descriptors[ring->tail].eof;
350 			frame->sof = ring->descriptors[ring->tail].sof;
351 			frame->flags = ring->descriptors[ring->tail].flags;
352 		}
353 		ring->tail = (ring->tail + 1) % ring->size;
354 	}
355 	ring_write_descriptors(ring, true);
356 
357 invoke_callback:
358 	/* allow callbacks to schedule new work */
359 	spin_unlock_irqrestore(&ring->lock, flags);
360 	while (!list_empty(&done)) {
361 		frame = list_first_entry(&done, typeof(*frame), list);
362 		/*
363 		 * The callback may reenqueue or delete frame.
364 		 * Do not hold on to it.
365 		 */
366 		list_del_init(&frame->list);
367 		if (frame->callback)
368 			frame->callback(ring, frame, canceled);
369 	}
370 
371 	wake_up(&ring->wait);
372 }
373 
374 int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame, bool more)
375 {
376 	unsigned long flags;
377 	int ret = 0;
378 
379 	spin_lock_irqsave(&ring->lock, flags);
380 	if (ring->running) {
381 		list_add_tail(&frame->list, &ring->queue);
382 		ring_write_descriptors(ring, !more);
383 	} else {
384 		ret = -ESHUTDOWN;
385 	}
386 	spin_unlock_irqrestore(&ring->lock, flags);
387 	return ret;
388 }
389 EXPORT_SYMBOL_GPL(__tb_ring_enqueue);
390 
391 /**
392  * tb_ring_notify() - Notify the controller about the queued frames
393  * @ring: Ring to notify
394  *
395  * Notifies the controller about frames that were enqueued using
396  * tb_ring_tx_more() or tb_ring_rx_more(). Does nothing if there are no
397  * such frames pending.
398  */
399 void tb_ring_notify(struct tb_ring *ring)
400 {
401 	guard(spinlock_irqsave)(&ring->lock);
402 	if (ring->running)
403 		__ring_notify(ring);
404 }
405 EXPORT_SYMBOL_GPL(tb_ring_notify);
406 
407 /**
408  * tb_ring_poll() - Poll one completed frame from the ring
409  * @ring: Ring to poll
410  *
411  * This function can be called when @start_poll callback of the @ring
412  * has been called or the ring is created with %RING_FLAG_NO_INTERRUPT.
413  * It will read one completed frame from the ring and return it to the
414  * caller.
415  *
416  * Return: Pointer to &struct ring_frame, %NULL if there is no more
417  * completed frames.
418  */
419 struct ring_frame *tb_ring_poll(struct tb_ring *ring)
420 {
421 	struct ring_frame *frame = NULL;
422 	unsigned long flags;
423 
424 	spin_lock_irqsave(&ring->lock, flags);
425 	if (!ring->running)
426 		goto unlock;
427 	if (ring_empty(ring))
428 		goto unlock;
429 
430 	if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) {
431 		frame = list_first_entry(&ring->in_flight, typeof(*frame),
432 					 list);
433 		list_del_init(&frame->list);
434 
435 		if (!ring->is_tx) {
436 			frame->size = ring->descriptors[ring->tail].length;
437 			frame->eof = ring->descriptors[ring->tail].eof;
438 			frame->sof = ring->descriptors[ring->tail].sof;
439 			frame->flags = ring->descriptors[ring->tail].flags;
440 		}
441 
442 		ring->tail = (ring->tail + 1) % ring->size;
443 
444 		/*
445 		 * There is one more slot available so we can push next
446 		 * descriptor to the ring. This is needed when the ring
447 		 * is created with %RING_FLAG_NO_INTERRUPT.
448 		 */
449 		ring_write_descriptors(ring, true);
450 	}
451 
452 unlock:
453 	spin_unlock_irqrestore(&ring->lock, flags);
454 	return frame;
455 }
456 EXPORT_SYMBOL_GPL(tb_ring_poll);
457 
458 /**
459  * tb_ring_poll_pending() - Does the ring have completed frame
460  * @ring: Ring to check
461  *
462  * Can be used to check whether there is a completed frame in the ring
463  * that next call to tb_ring_poll() returns.
464  *
465  * Return: %true if a completed frame is waiting, %false otherwise.
466  */
467 bool tb_ring_poll_pending(struct tb_ring *ring)
468 {
469 	guard(spinlock_irqsave)(&ring->lock);
470 
471 	if (!ring->running || ring_empty(ring))
472 		return false;
473 	return !!(ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED);
474 }
475 EXPORT_SYMBOL_GPL(tb_ring_poll_pending);
476 
477 static void __ring_interrupt_mask(struct tb_ring *ring, bool mask)
478 {
479 	int interrupt_index = ring_interrupt_index(ring);
480 	int reg_index = interrupt_index / 32;
481 	int interrupt_bit = interrupt_index % 32;
482 
483 	if (mask)
484 		nhi_mask_interrupt(ring->nhi, BIT(interrupt_bit), reg_index);
485 	else
486 		nhi_unmask_interrupt(ring->nhi, BIT(interrupt_bit), reg_index);
487 }
488 
489 /* Both @nhi->lock and @ring->lock should be held */
490 static void __ring_interrupt(struct tb_ring *ring)
491 {
492 	if (!ring->running)
493 		return;
494 
495 	if (ring->start_poll) {
496 		__ring_interrupt_mask(ring, true);
497 		ring->start_poll(ring->poll_data);
498 	} else {
499 		schedule_work(&ring->work);
500 	}
501 }
502 
503 /**
504  * tb_ring_poll_complete() - Re-start interrupt for the ring
505  * @ring: Ring to re-start the interrupt
506  *
507  * This will re-start (unmask) the ring interrupt once the user is done
508  * with polling.
509  */
510 void tb_ring_poll_complete(struct tb_ring *ring)
511 {
512 	unsigned long flags;
513 
514 	spin_lock_irqsave(&ring->nhi->lock, flags);
515 	spin_lock(&ring->lock);
516 	if (ring->start_poll)
517 		__ring_interrupt_mask(ring, false);
518 	spin_unlock(&ring->lock);
519 	spin_unlock_irqrestore(&ring->nhi->lock, flags);
520 }
521 EXPORT_SYMBOL_GPL(tb_ring_poll_complete);
522 
523 static void ring_clear_msix(const struct tb_ring *ring)
524 {
525 	int bit;
526 
527 	if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
528 		return;
529 
530 	bit = ring_interrupt_index(ring) & 31;
531 	if (ring->is_tx)
532 		iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR);
533 	else
534 		iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR +
535 			  4 * (ring->nhi->hop_count / 32));
536 }
537 
538 irqreturn_t ring_msix(int irq, void *data)
539 {
540 	struct tb_ring *ring = data;
541 
542 	spin_lock(&ring->nhi->lock);
543 	ring_clear_msix(ring);
544 	spin_lock(&ring->lock);
545 	__ring_interrupt(ring);
546 	spin_unlock(&ring->lock);
547 	spin_unlock(&ring->nhi->lock);
548 
549 	return IRQ_HANDLED;
550 }
551 
552 static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring)
553 {
554 	unsigned int start_hop = RING_FIRST_USABLE_HOPID;
555 	int ret = 0;
556 
557 	if (nhi->quirks & QUIRK_E2E) {
558 		start_hop = RING_FIRST_USABLE_HOPID + 1;
559 		if (ring->flags & RING_FLAG_E2E && !ring->is_tx) {
560 			dev_dbg(nhi->dev, "quirking E2E TX HopID %u -> %u\n",
561 				ring->e2e_tx_hop, RING_E2E_RESERVED_HOPID);
562 			ring->e2e_tx_hop = RING_E2E_RESERVED_HOPID;
563 		}
564 	}
565 
566 	spin_lock_irq(&nhi->lock);
567 
568 	if (ring->hop < 0) {
569 		unsigned int i;
570 
571 		/*
572 		 * Automatically allocate HopID from the non-reserved
573 		 * range 1 .. hop_count - 1.
574 		 */
575 		for (i = start_hop; i < nhi->hop_count; i++) {
576 			if (ring->is_tx) {
577 				if (!nhi->tx_rings[i]) {
578 					ring->hop = i;
579 					break;
580 				}
581 			} else {
582 				if (!nhi->rx_rings[i]) {
583 					ring->hop = i;
584 					break;
585 				}
586 			}
587 		}
588 	}
589 
590 	if (ring->hop > 0 && ring->hop < start_hop) {
591 		dev_warn(nhi->dev, "invalid hop: %d\n", ring->hop);
592 		ret = -EINVAL;
593 		goto err_unlock;
594 	}
595 	if (ring->hop < 0 || ring->hop >= nhi->hop_count) {
596 		dev_warn(nhi->dev, "invalid hop: %d\n", ring->hop);
597 		ret = -EINVAL;
598 		goto err_unlock;
599 	}
600 	if (ring->is_tx && nhi->tx_rings[ring->hop]) {
601 		dev_warn(nhi->dev, "TX hop %d already allocated\n",
602 			 ring->hop);
603 		ret = -EBUSY;
604 		goto err_unlock;
605 	}
606 	if (!ring->is_tx && nhi->rx_rings[ring->hop]) {
607 		dev_warn(nhi->dev, "RX hop %d already allocated\n",
608 			 ring->hop);
609 		ret = -EBUSY;
610 		goto err_unlock;
611 	}
612 
613 	if (ring->is_tx)
614 		nhi->tx_rings[ring->hop] = ring;
615 	else
616 		nhi->rx_rings[ring->hop] = ring;
617 
618 err_unlock:
619 	spin_unlock_irq(&nhi->lock);
620 
621 	return ret;
622 }
623 
624 static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
625 				     bool transmit, unsigned int flags,
626 				     int e2e_tx_hop, u16 sof_mask, u16 eof_mask,
627 				     void (*start_poll)(void *),
628 				     void *poll_data)
629 {
630 	struct tb_ring *ring = NULL;
631 
632 	dev_dbg(nhi->dev, "allocating %s ring %d of size %d\n",
633 		transmit ? "TX" : "RX", hop, size);
634 
635 	if ((flags & RING_FLAG_NO_INTERRUPT) && start_poll) {
636 		dev_WARN(nhi->dev,
637 			 "start_poll() and NO_INTERRUPT cannot be used at the same time\n");
638 		return NULL;
639 	}
640 
641 	ring = kzalloc_obj(*ring);
642 	if (!ring)
643 		return NULL;
644 
645 	spin_lock_init(&ring->lock);
646 	INIT_LIST_HEAD(&ring->queue);
647 	INIT_LIST_HEAD(&ring->in_flight);
648 	INIT_WORK(&ring->work, ring_work);
649 	init_waitqueue_head(&ring->wait);
650 	lockdep_register_key(&ring->lock_key);
651 	lockdep_init_map(&ring->work.lockdep_map, "ring.work", &ring->lock_key, 0);
652 
653 	ring->nhi = nhi;
654 	ring->hop = hop;
655 	ring->is_tx = transmit;
656 	ring->size = size;
657 	ring->flags = flags;
658 	ring->e2e_tx_hop = e2e_tx_hop;
659 	ring->sof_mask = sof_mask;
660 	ring->eof_mask = eof_mask;
661 	ring->head = 0;
662 	ring->tail = 0;
663 	ring->running = false;
664 	ring->start_poll = start_poll;
665 	ring->poll_data = poll_data;
666 
667 	ring->descriptors = dma_alloc_coherent(ring->nhi->dev,
668 					       size * sizeof(*ring->descriptors),
669 					       &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
670 	if (!ring->descriptors)
671 		goto err_free_ring;
672 
673 	if (!(flags & RING_FLAG_NO_INTERRUPT) && nhi->ops->request_ring_irq) {
674 		if (nhi->ops->request_ring_irq(ring, flags & RING_FLAG_NO_SUSPEND))
675 			goto err_free_descs;
676 	}
677 
678 	if (nhi_alloc_hop(nhi, ring))
679 		goto err_release_msix;
680 
681 	return ring;
682 
683 err_release_msix:
684 	if (nhi->ops->release_ring_irq)
685 		nhi->ops->release_ring_irq(ring);
686 err_free_descs:
687 	dma_free_coherent(ring->nhi->dev,
688 			  ring->size * sizeof(*ring->descriptors),
689 			  ring->descriptors, ring->descriptors_dma);
690 err_free_ring:
691 	lockdep_unregister_key(&ring->lock_key);
692 	kfree(ring);
693 
694 	return NULL;
695 }
696 
697 /**
698  * tb_ring_alloc_tx() - Allocate DMA ring for transmit
699  * @nhi: Pointer to the NHI the ring is to be allocated
700  * @hop: HopID (ring) to allocate
701  * @size: Number of entries in the ring
702  * @flags: Flags for the ring
703  *
704  * Return: Pointer to &struct tb_ring, %NULL otherwise.
705  */
706 struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
707 				 unsigned int flags)
708 {
709 	return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0, 0, NULL, NULL);
710 }
711 EXPORT_SYMBOL_GPL(tb_ring_alloc_tx);
712 
713 /**
714  * tb_ring_alloc_rx() - Allocate DMA ring for receive
715  * @nhi: Pointer to the NHI the ring is to be allocated
716  * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
717  * @size: Number of entries in the ring
718  * @flags: Flags for the ring
719  * @e2e_tx_hop: Transmit HopID when E2E is enabled in @flags
720  * @sof_mask: Mask of PDF values that start a frame
721  * @eof_mask: Mask of PDF values that end a frame
722  * @start_poll: If not %NULL the ring will call this function when an
723  *		interrupt is triggered and masked, instead of callback
724  *		in each Rx frame.
725  * @poll_data: Optional data passed to @start_poll
726  *
727  * Return: Pointer to &struct tb_ring, %NULL otherwise.
728  */
729 struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
730 				 unsigned int flags, int e2e_tx_hop,
731 				 u16 sof_mask, u16 eof_mask,
732 				 void (*start_poll)(void *), void *poll_data)
733 {
734 	return tb_ring_alloc(nhi, hop, size, false, flags, e2e_tx_hop, sof_mask, eof_mask,
735 			     start_poll, poll_data);
736 }
737 EXPORT_SYMBOL_GPL(tb_ring_alloc_rx);
738 
739 /**
740  * tb_ring_start() - enable a ring
741  * @ring: Ring to start
742  *
743  * Must not be invoked in parallel with tb_ring_stop().
744  */
745 void tb_ring_start(struct tb_ring *ring)
746 {
747 	u16 frame_size;
748 	u32 flags;
749 
750 	spin_lock_irq(&ring->nhi->lock);
751 	spin_lock(&ring->lock);
752 	if (ring->nhi->going_away)
753 		goto err;
754 	if (ring->running) {
755 		dev_WARN(ring->nhi->dev, "ring already started\n");
756 		goto err;
757 	}
758 	dev_dbg(ring->nhi->dev, "starting %s %d\n",
759 		RING_TYPE(ring), ring->hop);
760 
761 	if (ring->flags & RING_FLAG_FRAME) {
762 		/* Means 4096 */
763 		frame_size = 0;
764 		flags = RING_FLAG_ENABLE;
765 	} else {
766 		frame_size = TB_FRAME_SIZE;
767 		flags = RING_FLAG_ENABLE | RING_FLAG_RAW;
768 	}
769 
770 	ring_iowrite64desc(ring, ring->descriptors_dma, 0);
771 	if (ring->is_tx) {
772 		ring_iowrite32desc(ring, ring->size, 12);
773 		ring_iowrite32options(ring, 0, 4);
774 		ring_iowrite32options(ring, flags, 0);
775 	} else {
776 		u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask;
777 
778 		ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12);
779 		ring_iowrite32options(ring, sof_eof_mask, 4);
780 		ring_iowrite32options(ring, flags, 0);
781 	}
782 
783 	/*
784 	 * Now that the ring valid bit is set we can configure E2E if
785 	 * enabled for the ring.
786 	 */
787 	if (ring->flags & RING_FLAG_E2E) {
788 		if (!ring->is_tx) {
789 			u32 hop;
790 
791 			hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT;
792 			hop &= REG_RX_OPTIONS_E2E_HOP_MASK;
793 			flags |= hop;
794 
795 			dev_dbg(ring->nhi->dev,
796 				"enabling E2E for %s %d with TX HopID %d\n",
797 				RING_TYPE(ring), ring->hop, ring->e2e_tx_hop);
798 		} else {
799 			dev_dbg(ring->nhi->dev, "enabling E2E for %s %d\n",
800 				RING_TYPE(ring), ring->hop);
801 		}
802 
803 		flags |= RING_FLAG_E2E_FLOW_CONTROL;
804 		ring_iowrite32options(ring, flags, 0);
805 	}
806 
807 	if (!(ring->flags & RING_FLAG_NO_INTERRUPT))
808 		ring_interrupt_active(ring, true);
809 	ring->running = true;
810 err:
811 	spin_unlock(&ring->lock);
812 	spin_unlock_irq(&ring->nhi->lock);
813 }
814 EXPORT_SYMBOL_GPL(tb_ring_start);
815 
816 static bool tb_ring_empty(struct tb_ring *ring)
817 {
818 	guard(spinlock_irqsave)(&ring->lock);
819 	return list_empty(&ring->in_flight);
820 }
821 
822 /**
823  * tb_ring_flush() - Waits for a ring to be empty
824  * @ring: Ring to wait
825  * @timeout_msec: Timeout in ms how long to wait.
826  *
827  * This can be called before stopping a ring to make sure all the frames
828  * submitted prior have been completed.
829  *
830  * Return: %true if the ring is empty now, %false otherwise.
831  */
832 bool tb_ring_flush(struct tb_ring *ring, unsigned int timeout_msec)
833 {
834 	if (!wait_event_timeout(ring->wait, tb_ring_empty(ring),
835 				msecs_to_jiffies(timeout_msec)))
836 		return false;
837 	return tb_ring_empty(ring);
838 }
839 EXPORT_SYMBOL_GPL(tb_ring_flush);
840 
841 /**
842  * tb_ring_stop() - shutdown a ring
843  * @ring: Ring to stop
844  *
845  * Must not be invoked from a callback.
846  *
847  * This method will disable the ring. Further calls to
848  * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been
849  * called.
850  *
851  * All enqueued frames will be canceled and their callbacks will be executed
852  * with frame->canceled set to true (on the callback thread). This method
853  * returns only after all callback invocations have finished.
854  */
855 void tb_ring_stop(struct tb_ring *ring)
856 {
857 	spin_lock_irq(&ring->nhi->lock);
858 	spin_lock(&ring->lock);
859 	dev_dbg(ring->nhi->dev, "stopping %s %d\n",
860 		RING_TYPE(ring), ring->hop);
861 	if (ring->nhi->going_away)
862 		goto err;
863 	if (!ring->running) {
864 		dev_WARN(ring->nhi->dev, "%s %d already stopped\n",
865 			 RING_TYPE(ring), ring->hop);
866 		goto err;
867 	}
868 	if (!(ring->flags & RING_FLAG_NO_INTERRUPT))
869 		ring_interrupt_active(ring, false);
870 
871 	ring_iowrite32options(ring, 0, 0);
872 	ring_iowrite64desc(ring, 0, 0);
873 	ring_iowrite32desc(ring, 0, 8);
874 	ring_iowrite32desc(ring, 0, 12);
875 	ring->head = 0;
876 	ring->tail = 0;
877 	ring->notify_pending = false;
878 	ring->running = false;
879 
880 err:
881 	spin_unlock(&ring->lock);
882 	spin_unlock_irq(&ring->nhi->lock);
883 
884 	/*
885 	 * schedule ring->work to invoke callbacks on all remaining frames.
886 	 */
887 	schedule_work(&ring->work);
888 	flush_work(&ring->work);
889 }
890 EXPORT_SYMBOL_GPL(tb_ring_stop);
891 
892 /*
893  * tb_ring_free() - free ring
894  *
895  * When this method returns all invocations of ring->callback will have
896  * finished.
897  *
898  * Ring must be stopped.
899  *
900  * Must NOT be called from ring_frame->callback!
901  */
902 void tb_ring_free(struct tb_ring *ring)
903 {
904 	struct tb_nhi *nhi = ring->nhi;
905 
906 	spin_lock_irq(&ring->nhi->lock);
907 	/*
908 	 * Dissociate the ring from the NHI. This also ensures that
909 	 * nhi_interrupt_work cannot reschedule ring->work.
910 	 */
911 	if (ring->is_tx)
912 		ring->nhi->tx_rings[ring->hop] = NULL;
913 	else
914 		ring->nhi->rx_rings[ring->hop] = NULL;
915 
916 	if (ring->running) {
917 		dev_WARN(ring->nhi->dev, "%s %d still running\n",
918 			 RING_TYPE(ring), ring->hop);
919 	}
920 	spin_unlock_irq(&ring->nhi->lock);
921 
922 	if (nhi->ops->release_ring_irq)
923 		nhi->ops->release_ring_irq(ring);
924 
925 	dma_free_coherent(ring->nhi->dev,
926 			  ring->size * sizeof(*ring->descriptors),
927 			  ring->descriptors, ring->descriptors_dma);
928 
929 	ring->descriptors = NULL;
930 	ring->descriptors_dma = 0;
931 
932 
933 	dev_dbg(ring->nhi->dev, "freeing %s %d\n", RING_TYPE(ring),
934 		ring->hop);
935 
936 	/*
937 	 * ring->work can no longer be scheduled (it is scheduled only
938 	 * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
939 	 * to finish before freeing the ring.
940 	 */
941 	flush_work(&ring->work);
942 	lockdep_unregister_key(&ring->lock_key);
943 	kfree(ring);
944 }
945 EXPORT_SYMBOL_GPL(tb_ring_free);
946 
947 /**
948  * tb_ring_throttling() - Configure throttling for ring interrupt
949  * @ring: Ring to configure
950  * @interval_nsec: Interval counter for moderation (in ns), %0 disables
951  *
952  * Enables or disables ring interrupt throttling. The ring must be
953  * stopped for this to be called. Granularity is 256 ns.
954  *
955  * Return: %0 on success, negative errno otherwise.
956  */
957 int tb_ring_throttling(struct tb_ring *ring, unsigned int interval_nsec)
958 {
959 	guard(spinlock_irqsave)(&ring->lock);
960 	if (WARN_ON_ONCE(ring->running))
961 		return -EBUSY;
962 	ring->interval_nsec = interval_nsec;
963 	return 0;
964 }
965 EXPORT_SYMBOL_GPL(tb_ring_throttling);
966 
967 /**
968  * nhi_mailbox_cmd() - Send a command through NHI mailbox
969  * @nhi: Pointer to the NHI structure
970  * @cmd: Command to send
971  * @data: Data to be send with the command
972  *
973  * Sends mailbox command to the firmware running on NHI.
974  *
975  * Return: %0 on success, negative errno otherwise.
976  */
977 int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
978 {
979 	ktime_t timeout;
980 	u32 val;
981 
982 	iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
983 
984 	val = ioread32(nhi->iobase + REG_INMAIL_CMD);
985 	val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
986 	val |= REG_INMAIL_OP_REQUEST | cmd;
987 	iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
988 
989 	timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT);
990 	do {
991 		val = ioread32(nhi->iobase + REG_INMAIL_CMD);
992 		if (!(val & REG_INMAIL_OP_REQUEST))
993 			break;
994 		usleep_range(10, 20);
995 	} while (ktime_before(ktime_get(), timeout));
996 
997 	if (val & REG_INMAIL_OP_REQUEST)
998 		return -ETIMEDOUT;
999 	if (val & REG_INMAIL_ERROR)
1000 		return -EIO;
1001 
1002 	return 0;
1003 }
1004 
1005 /**
1006  * nhi_mailbox_mode() - Return current firmware operation mode
1007  * @nhi: Pointer to the NHI structure
1008  *
1009  * The function reads current firmware operation mode using NHI mailbox
1010  * registers and returns it to the caller.
1011  *
1012  * Return: &enum nhi_fw_mode.
1013  */
1014 enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
1015 {
1016 	u32 val;
1017 
1018 	val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
1019 	val &= REG_OUTMAIL_CMD_OPMODE_MASK;
1020 	val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
1021 
1022 	return (enum nhi_fw_mode)val;
1023 }
1024 
1025 void nhi_interrupt_work(struct work_struct *work)
1026 {
1027 	struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
1028 	int value = 0; /* Suppress uninitialized usage warning. */
1029 	int bit;
1030 	int hop = -1;
1031 	int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
1032 	struct tb_ring *ring;
1033 
1034 	spin_lock_irq(&nhi->lock);
1035 
1036 	/*
1037 	 * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
1038 	 * (TX, RX, RX overflow). We iterate over the bits and read a new
1039 	 * dwords as required. The registers are cleared on read.
1040 	 */
1041 	for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
1042 		if (bit % 32 == 0)
1043 			value = ioread32(nhi->iobase
1044 					 + REG_RING_NOTIFY_BASE
1045 					 + 4 * (bit / 32));
1046 		if (++hop == nhi->hop_count) {
1047 			hop = 0;
1048 			type++;
1049 		}
1050 		if ((value & (1 << (bit % 32))) == 0)
1051 			continue;
1052 		if (type == 2) {
1053 			dev_warn(nhi->dev, "RX overflow for ring %d\n", hop);
1054 			continue;
1055 		}
1056 		if (type == 0)
1057 			ring = nhi->tx_rings[hop];
1058 		else
1059 			ring = nhi->rx_rings[hop];
1060 		if (ring == NULL) {
1061 			dev_warn(nhi->dev,
1062 				 "got interrupt for inactive %s ring %d\n",
1063 				 type ? "RX" : "TX",
1064 				 hop);
1065 			continue;
1066 		}
1067 
1068 		spin_lock(&ring->lock);
1069 		__ring_interrupt(ring);
1070 		spin_unlock(&ring->lock);
1071 	}
1072 	spin_unlock_irq(&nhi->lock);
1073 }
1074 
1075 irqreturn_t nhi_msi(int irq, void *data)
1076 {
1077 	struct tb_nhi *nhi = data;
1078 	schedule_work(&nhi->interrupt_work);
1079 	return IRQ_HANDLED;
1080 }
1081 
1082 static int __nhi_suspend_noirq(struct device *dev, bool wakeup)
1083 {
1084 	struct tb *tb = dev_get_drvdata(dev);
1085 	struct tb_nhi *nhi = tb->nhi;
1086 	int ret;
1087 
1088 	ret = tb_domain_suspend_noirq(tb);
1089 	if (ret)
1090 		return ret;
1091 
1092 	if (nhi->ops->suspend_noirq) {
1093 		ret = nhi->ops->suspend_noirq(tb->nhi, wakeup);
1094 		if (ret)
1095 			return ret;
1096 	}
1097 
1098 	return 0;
1099 }
1100 
1101 static int nhi_suspend_noirq(struct device *dev)
1102 {
1103 	return __nhi_suspend_noirq(dev, device_may_wakeup(dev));
1104 }
1105 
1106 static int nhi_freeze_noirq(struct device *dev)
1107 {
1108 	struct tb *tb = dev_get_drvdata(dev);
1109 
1110 	return tb_domain_freeze_noirq(tb);
1111 }
1112 
1113 static int nhi_thaw_noirq(struct device *dev)
1114 {
1115 	struct tb *tb = dev_get_drvdata(dev);
1116 
1117 	return tb_domain_thaw_noirq(tb);
1118 }
1119 
1120 static bool nhi_wake_supported(struct device *dev)
1121 {
1122 	u8 val;
1123 
1124 	/*
1125 	 * If power rails are sustainable for wakeup from S4 this
1126 	 * property is set by the BIOS.
1127 	 */
1128 	if (!device_property_read_u8(dev, "WAKE_SUPPORTED", &val))
1129 		return !!val;
1130 
1131 	return true;
1132 }
1133 
1134 static int nhi_poweroff_noirq(struct device *dev)
1135 {
1136 	bool wakeup;
1137 
1138 	wakeup = device_may_wakeup(dev) && nhi_wake_supported(dev);
1139 	return __nhi_suspend_noirq(dev, wakeup);
1140 }
1141 
1142 static int nhi_resume_noirq(struct device *dev)
1143 {
1144 	struct tb *tb = dev_get_drvdata(dev);
1145 	struct tb_nhi *nhi = tb->nhi;
1146 	int ret;
1147 
1148 	/*
1149 	 * Check that the device is still there. It may be that the user
1150 	 * unplugged last device which causes the host controller to go
1151 	 * away on PCs.
1152 	 */
1153 	if ((nhi->ops->is_present && !nhi->ops->is_present(nhi))) {
1154 		nhi->going_away = true;
1155 	} else if (nhi->ops->resume_noirq) {
1156 		ret = nhi->ops->resume_noirq(nhi);
1157 		if (ret)
1158 			return ret;
1159 	}
1160 
1161 	return tb_domain_resume_noirq(tb);
1162 }
1163 
1164 static int nhi_suspend(struct device *dev)
1165 {
1166 	struct tb *tb = dev_get_drvdata(dev);
1167 
1168 	return tb_domain_suspend(tb);
1169 }
1170 
1171 static void nhi_complete(struct device *dev)
1172 {
1173 	struct tb *tb = dev_get_drvdata(dev);
1174 
1175 	/*
1176 	 * If we were runtime suspended when system suspend started,
1177 	 * schedule runtime resume now. It should bring the domain back
1178 	 * to functional state.
1179 	 */
1180 	if (pm_runtime_suspended(dev))
1181 		pm_runtime_resume(dev);
1182 	else
1183 		tb_domain_complete(tb);
1184 }
1185 
1186 static int nhi_runtime_suspend(struct device *dev)
1187 {
1188 	struct tb *tb = dev_get_drvdata(dev);
1189 	struct tb_nhi *nhi = tb->nhi;
1190 	int ret;
1191 
1192 	ret = tb_domain_runtime_suspend(tb);
1193 	if (ret)
1194 		return ret;
1195 
1196 	if (nhi->ops->runtime_suspend) {
1197 		ret = nhi->ops->runtime_suspend(tb->nhi);
1198 		if (ret)
1199 			return ret;
1200 	}
1201 	return 0;
1202 }
1203 
1204 static int nhi_runtime_resume(struct device *dev)
1205 {
1206 	struct tb *tb = dev_get_drvdata(dev);
1207 	struct tb_nhi *nhi = tb->nhi;
1208 	int ret;
1209 
1210 	if (nhi->ops->runtime_resume) {
1211 		ret = nhi->ops->runtime_resume(nhi);
1212 		if (ret)
1213 			return ret;
1214 	}
1215 
1216 	return tb_domain_runtime_resume(tb);
1217 }
1218 
1219 void nhi_shutdown(struct tb_nhi *nhi)
1220 {
1221 	int i;
1222 
1223 	dev_dbg(nhi->dev, "shutdown\n");
1224 
1225 	for (i = 0; i < nhi->hop_count; i++) {
1226 		if (nhi->tx_rings[i])
1227 			dev_WARN(nhi->dev,
1228 				 "TX ring %d is still active\n", i);
1229 		if (nhi->rx_rings[i])
1230 			dev_WARN(nhi->dev,
1231 				 "RX ring %d is still active\n", i);
1232 	}
1233 	nhi_disable_interrupts(nhi);
1234 
1235 	if (nhi->ops->shutdown)
1236 		nhi->ops->shutdown(nhi);
1237 }
1238 
1239 static void nhi_reset(struct tb_nhi *nhi)
1240 {
1241 	ktime_t timeout;
1242 	u32 val;
1243 
1244 	val = ioread32(nhi->iobase + REG_CAPS);
1245 	/* Reset only v2 and later routers */
1246 	if (FIELD_GET(REG_CAPS_VERSION_MASK, val) < REG_CAPS_VERSION_2)
1247 		return;
1248 
1249 	if (!host_reset) {
1250 		dev_dbg(nhi->dev, "skipping host router reset\n");
1251 		return;
1252 	}
1253 
1254 	iowrite32(REG_RESET_HRR, nhi->iobase + REG_RESET);
1255 	msleep(100);
1256 
1257 	timeout = ktime_add_ms(ktime_get(), 500);
1258 	do {
1259 		val = ioread32(nhi->iobase + REG_RESET);
1260 		if (!(val & REG_RESET_HRR)) {
1261 			dev_warn(nhi->dev, "host router reset successful\n");
1262 			return;
1263 		}
1264 		usleep_range(10, 20);
1265 	} while (ktime_before(ktime_get(), timeout));
1266 
1267 	dev_warn(nhi->dev, "timeout resetting host router\n");
1268 }
1269 
1270 static struct tb *nhi_select_cm(struct tb_nhi *nhi)
1271 {
1272 	bool linked = false;
1273 	struct tb *tb;
1274 
1275 	/*
1276 	 * USB4 case is simple. If we got control of any of the
1277 	 * capabilities, we use software CM.
1278 	 */
1279 	if (!tb_acpi_is_native()) {
1280 		/*
1281 		 * Either firmware based CM is running (we did not get
1282 		 * control from the firmware) or this is pre-USB4 PC so
1283 		 * try first firmware CM and then fallback to software CM.
1284 		 */
1285 		tb = icm_probe(nhi);
1286 		if (tb)
1287 			return tb;
1288 	}
1289 
1290 	tb = tb_probe(nhi);
1291 	if (!tb)
1292 		return NULL;
1293 
1294 	if (nhi->ops->add_links)
1295 		linked = nhi->ops->add_links(nhi);
1296 	if (!linked)
1297 		linked = tb_acpi_add_links(nhi);
1298 	/*
1299 	 * Device links are needed to make sure we establish tunnels
1300 	 * before protocol stacks are resumed so complain here if we
1301 	 * found them missing.
1302 	 */
1303 	if (!linked)
1304 		dev_warn(nhi->dev,
1305 			 "device links to tunneled native ports are missing!\n");
1306 	return tb;
1307 }
1308 
1309 int nhi_probe(struct tb_nhi *nhi)
1310 {
1311 	struct device *dev = nhi->dev;
1312 	struct tb *tb;
1313 	int res;
1314 
1315 	if (!nhi->ops)
1316 		return dev_err_probe(dev, -EINVAL, "NHI ops not set\n");
1317 
1318 	if (!nhi->ops->init_interrupts)
1319 		return dev_err_probe(dev, -EINVAL, "missing required NHI ops\n");
1320 
1321 	nhi->hop_count = ioread32(nhi->iobase + REG_CAPS) & 0x3ff;
1322 	dev_dbg(dev, "total paths: %d\n", nhi->hop_count);
1323 
1324 	nhi->tx_rings = devm_kcalloc(dev, nhi->hop_count,
1325 				     sizeof(*nhi->tx_rings), GFP_KERNEL);
1326 	nhi->rx_rings = devm_kcalloc(dev, nhi->hop_count,
1327 				     sizeof(*nhi->rx_rings), GFP_KERNEL);
1328 	nhi->interrupt_mask = devm_kcalloc(dev, RING_INTERRUPT_REG_COUNT(nhi),
1329 					   sizeof(*nhi->interrupt_mask),
1330 					   GFP_KERNEL);
1331 	if (!nhi->tx_rings || !nhi->rx_rings || !nhi->interrupt_mask)
1332 		return -ENOMEM;
1333 
1334 	nhi_reset(nhi);
1335 
1336 	/* In case someone left them on. */
1337 	nhi_disable_interrupts(nhi);
1338 
1339 	res = nhi->ops->init_interrupts(nhi);
1340 	if (res)
1341 		return dev_err_probe(dev, res, "cannot enable interrupts, aborting\n");
1342 
1343 	spin_lock_init(&nhi->lock);
1344 
1345 	res = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
1346 	if (res)
1347 		return dev_err_probe(dev, res, "failed to set DMA mask\n");
1348 
1349 	if (nhi->ops->init) {
1350 		res = nhi->ops->init(nhi);
1351 		if (res)
1352 			return dev_err_probe(dev, res, "NHI specific init failed\n");
1353 	}
1354 
1355 	init_completion(&nhi->domain_released);
1356 
1357 	tb = nhi_select_cm(nhi);
1358 	if (!tb)
1359 		return dev_err_probe(dev, -ENODEV,
1360 			"failed to determine connection manager, aborting\n");
1361 
1362 	dev_dbg(dev, "NHI initialized, starting thunderbolt\n");
1363 
1364 	nhi->host_reset = host_reset;
1365 
1366 	res = tb_domain_add(tb, host_reset);
1367 	if (res) {
1368 		/*
1369 		 * At this point the RX/TX rings might already have been
1370 		 * activated. Do a proper shutdown.
1371 		 */
1372 		tb_domain_put(tb);
1373 		wait_for_completion(&nhi->domain_released);
1374 		nhi_shutdown(nhi);
1375 		return dev_err_probe(dev, res, "failed to add domain\n");
1376 	}
1377 	dev_set_drvdata(dev, tb);
1378 
1379 	device_wakeup_enable(dev);
1380 
1381 	pm_runtime_allow(dev);
1382 	pm_runtime_set_autosuspend_delay(dev, TB_AUTOSUSPEND_DELAY);
1383 	pm_runtime_use_autosuspend(dev);
1384 	pm_runtime_put_autosuspend(dev);
1385 
1386 	return 0;
1387 }
1388 
1389 /*
1390  * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
1391  * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
1392  * resume_noirq until we are done.
1393  */
1394 const struct dev_pm_ops nhi_pm_ops = {
1395 	.suspend_noirq = nhi_suspend_noirq,
1396 	.resume_noirq = nhi_resume_noirq,
1397 	.freeze_noirq = nhi_freeze_noirq,  /*
1398 					    * we just disable hotplug, the
1399 					    * pci-tunnels stay alive.
1400 					    */
1401 	.thaw_noirq = nhi_thaw_noirq,
1402 	.restore_noirq = nhi_resume_noirq,
1403 	.suspend = nhi_suspend,
1404 	.poweroff_noirq = nhi_poweroff_noirq,
1405 	.poweroff = nhi_suspend,
1406 	.complete = nhi_complete,
1407 	.runtime_suspend = nhi_runtime_suspend,
1408 	.runtime_resume = nhi_runtime_resume,
1409 };
1410