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