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