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