Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Thunderbolt driver - NHI driver
0004  *
0005  * The NHI (native host interface) is the pci device that allows us to send and
0006  * receive frames from the thunderbolt bus.
0007  *
0008  * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
0009  * Copyright (C) 2018, Intel Corporation
0010  */
0011 
0012 #include <linux/pm_runtime.h>
0013 #include <linux/slab.h>
0014 #include <linux/errno.h>
0015 #include <linux/pci.h>
0016 #include <linux/dma-mapping.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/iommu.h>
0019 #include <linux/module.h>
0020 #include <linux/delay.h>
0021 #include <linux/property.h>
0022 #include <linux/string_helpers.h>
0023 
0024 #include "nhi.h"
0025 #include "nhi_regs.h"
0026 #include "tb.h"
0027 
0028 #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")
0029 
0030 #define RING_FIRST_USABLE_HOPID 1
0031 
0032 /*
0033  * Minimal number of vectors when we use MSI-X. Two for control channel
0034  * Rx/Tx and the rest four are for cross domain DMA paths.
0035  */
0036 #define MSIX_MIN_VECS       6
0037 #define MSIX_MAX_VECS       16
0038 
0039 #define NHI_MAILBOX_TIMEOUT 500 /* ms */
0040 
0041 #define QUIRK_AUTO_CLEAR_INT    BIT(0)
0042 
0043 static int ring_interrupt_index(struct tb_ring *ring)
0044 {
0045     int bit = ring->hop;
0046     if (!ring->is_tx)
0047         bit += ring->nhi->hop_count;
0048     return bit;
0049 }
0050 
0051 /*
0052  * ring_interrupt_active() - activate/deactivate interrupts for a single ring
0053  *
0054  * ring->nhi->lock must be held.
0055  */
0056 static void ring_interrupt_active(struct tb_ring *ring, bool active)
0057 {
0058     int reg = REG_RING_INTERRUPT_BASE +
0059           ring_interrupt_index(ring) / 32 * 4;
0060     int bit = ring_interrupt_index(ring) & 31;
0061     int mask = 1 << bit;
0062     u32 old, new;
0063 
0064     if (ring->irq > 0) {
0065         u32 step, shift, ivr, misc;
0066         void __iomem *ivr_base;
0067         int index;
0068 
0069         if (ring->is_tx)
0070             index = ring->hop;
0071         else
0072             index = ring->hop + ring->nhi->hop_count;
0073 
0074         if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT) {
0075             /*
0076              * Ask the hardware to clear interrupt status
0077              * bits automatically since we already know
0078              * which interrupt was triggered.
0079              */
0080             misc = ioread32(ring->nhi->iobase + REG_DMA_MISC);
0081             if (!(misc & REG_DMA_MISC_INT_AUTO_CLEAR)) {
0082                 misc |= REG_DMA_MISC_INT_AUTO_CLEAR;
0083                 iowrite32(misc, ring->nhi->iobase + REG_DMA_MISC);
0084             }
0085         }
0086 
0087         ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE;
0088         step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
0089         shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
0090         ivr = ioread32(ivr_base + step);
0091         ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift);
0092         if (active)
0093             ivr |= ring->vector << shift;
0094         iowrite32(ivr, ivr_base + step);
0095     }
0096 
0097     old = ioread32(ring->nhi->iobase + reg);
0098     if (active)
0099         new = old | mask;
0100     else
0101         new = old & ~mask;
0102 
0103     dev_dbg(&ring->nhi->pdev->dev,
0104         "%s interrupt at register %#x bit %d (%#x -> %#x)\n",
0105         active ? "enabling" : "disabling", reg, bit, old, new);
0106 
0107     if (new == old)
0108         dev_WARN(&ring->nhi->pdev->dev,
0109                      "interrupt for %s %d is already %s\n",
0110                      RING_TYPE(ring), ring->hop,
0111                      active ? "enabled" : "disabled");
0112     iowrite32(new, ring->nhi->iobase + reg);
0113 }
0114 
0115 /*
0116  * nhi_disable_interrupts() - disable interrupts for all rings
0117  *
0118  * Use only during init and shutdown.
0119  */
0120 static void nhi_disable_interrupts(struct tb_nhi *nhi)
0121 {
0122     int i = 0;
0123     /* disable interrupts */
0124     for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++)
0125         iowrite32(0, nhi->iobase + REG_RING_INTERRUPT_BASE + 4 * i);
0126 
0127     /* clear interrupt status bits */
0128     for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++)
0129         ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + 4 * i);
0130 }
0131 
0132 /* ring helper methods */
0133 
0134 static void __iomem *ring_desc_base(struct tb_ring *ring)
0135 {
0136     void __iomem *io = ring->nhi->iobase;
0137     io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE;
0138     io += ring->hop * 16;
0139     return io;
0140 }
0141 
0142 static void __iomem *ring_options_base(struct tb_ring *ring)
0143 {
0144     void __iomem *io = ring->nhi->iobase;
0145     io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE;
0146     io += ring->hop * 32;
0147     return io;
0148 }
0149 
0150 static void ring_iowrite_cons(struct tb_ring *ring, u16 cons)
0151 {
0152     /*
0153      * The other 16-bits in the register is read-only and writes to it
0154      * are ignored by the hardware so we can save one ioread32() by
0155      * filling the read-only bits with zeroes.
0156      */
0157     iowrite32(cons, ring_desc_base(ring) + 8);
0158 }
0159 
0160 static void ring_iowrite_prod(struct tb_ring *ring, u16 prod)
0161 {
0162     /* See ring_iowrite_cons() above for explanation */
0163     iowrite32(prod << 16, ring_desc_base(ring) + 8);
0164 }
0165 
0166 static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
0167 {
0168     iowrite32(value, ring_desc_base(ring) + offset);
0169 }
0170 
0171 static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
0172 {
0173     iowrite32(value, ring_desc_base(ring) + offset);
0174     iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
0175 }
0176 
0177 static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
0178 {
0179     iowrite32(value, ring_options_base(ring) + offset);
0180 }
0181 
0182 static bool ring_full(struct tb_ring *ring)
0183 {
0184     return ((ring->head + 1) % ring->size) == ring->tail;
0185 }
0186 
0187 static bool ring_empty(struct tb_ring *ring)
0188 {
0189     return ring->head == ring->tail;
0190 }
0191 
0192 /*
0193  * ring_write_descriptors() - post frames from ring->queue to the controller
0194  *
0195  * ring->lock is held.
0196  */
0197 static void ring_write_descriptors(struct tb_ring *ring)
0198 {
0199     struct ring_frame *frame, *n;
0200     struct ring_desc *descriptor;
0201     list_for_each_entry_safe(frame, n, &ring->queue, list) {
0202         if (ring_full(ring))
0203             break;
0204         list_move_tail(&frame->list, &ring->in_flight);
0205         descriptor = &ring->descriptors[ring->head];
0206         descriptor->phys = frame->buffer_phy;
0207         descriptor->time = 0;
0208         descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT;
0209         if (ring->is_tx) {
0210             descriptor->length = frame->size;
0211             descriptor->eof = frame->eof;
0212             descriptor->sof = frame->sof;
0213         }
0214         ring->head = (ring->head + 1) % ring->size;
0215         if (ring->is_tx)
0216             ring_iowrite_prod(ring, ring->head);
0217         else
0218             ring_iowrite_cons(ring, ring->head);
0219     }
0220 }
0221 
0222 /*
0223  * ring_work() - progress completed frames
0224  *
0225  * If the ring is shutting down then all frames are marked as canceled and
0226  * their callbacks are invoked.
0227  *
0228  * Otherwise we collect all completed frame from the ring buffer, write new
0229  * frame to the ring buffer and invoke the callbacks for the completed frames.
0230  */
0231 static void ring_work(struct work_struct *work)
0232 {
0233     struct tb_ring *ring = container_of(work, typeof(*ring), work);
0234     struct ring_frame *frame;
0235     bool canceled = false;
0236     unsigned long flags;
0237     LIST_HEAD(done);
0238 
0239     spin_lock_irqsave(&ring->lock, flags);
0240 
0241     if (!ring->running) {
0242         /*  Move all frames to done and mark them as canceled. */
0243         list_splice_tail_init(&ring->in_flight, &done);
0244         list_splice_tail_init(&ring->queue, &done);
0245         canceled = true;
0246         goto invoke_callback;
0247     }
0248 
0249     while (!ring_empty(ring)) {
0250         if (!(ring->descriptors[ring->tail].flags
0251                 & RING_DESC_COMPLETED))
0252             break;
0253         frame = list_first_entry(&ring->in_flight, typeof(*frame),
0254                      list);
0255         list_move_tail(&frame->list, &done);
0256         if (!ring->is_tx) {
0257             frame->size = ring->descriptors[ring->tail].length;
0258             frame->eof = ring->descriptors[ring->tail].eof;
0259             frame->sof = ring->descriptors[ring->tail].sof;
0260             frame->flags = ring->descriptors[ring->tail].flags;
0261         }
0262         ring->tail = (ring->tail + 1) % ring->size;
0263     }
0264     ring_write_descriptors(ring);
0265 
0266 invoke_callback:
0267     /* allow callbacks to schedule new work */
0268     spin_unlock_irqrestore(&ring->lock, flags);
0269     while (!list_empty(&done)) {
0270         frame = list_first_entry(&done, typeof(*frame), list);
0271         /*
0272          * The callback may reenqueue or delete frame.
0273          * Do not hold on to it.
0274          */
0275         list_del_init(&frame->list);
0276         if (frame->callback)
0277             frame->callback(ring, frame, canceled);
0278     }
0279 }
0280 
0281 int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
0282 {
0283     unsigned long flags;
0284     int ret = 0;
0285 
0286     spin_lock_irqsave(&ring->lock, flags);
0287     if (ring->running) {
0288         list_add_tail(&frame->list, &ring->queue);
0289         ring_write_descriptors(ring);
0290     } else {
0291         ret = -ESHUTDOWN;
0292     }
0293     spin_unlock_irqrestore(&ring->lock, flags);
0294     return ret;
0295 }
0296 EXPORT_SYMBOL_GPL(__tb_ring_enqueue);
0297 
0298 /**
0299  * tb_ring_poll() - Poll one completed frame from the ring
0300  * @ring: Ring to poll
0301  *
0302  * This function can be called when @start_poll callback of the @ring
0303  * has been called. It will read one completed frame from the ring and
0304  * return it to the caller. Returns %NULL if there is no more completed
0305  * frames.
0306  */
0307 struct ring_frame *tb_ring_poll(struct tb_ring *ring)
0308 {
0309     struct ring_frame *frame = NULL;
0310     unsigned long flags;
0311 
0312     spin_lock_irqsave(&ring->lock, flags);
0313     if (!ring->running)
0314         goto unlock;
0315     if (ring_empty(ring))
0316         goto unlock;
0317 
0318     if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) {
0319         frame = list_first_entry(&ring->in_flight, typeof(*frame),
0320                      list);
0321         list_del_init(&frame->list);
0322 
0323         if (!ring->is_tx) {
0324             frame->size = ring->descriptors[ring->tail].length;
0325             frame->eof = ring->descriptors[ring->tail].eof;
0326             frame->sof = ring->descriptors[ring->tail].sof;
0327             frame->flags = ring->descriptors[ring->tail].flags;
0328         }
0329 
0330         ring->tail = (ring->tail + 1) % ring->size;
0331     }
0332 
0333 unlock:
0334     spin_unlock_irqrestore(&ring->lock, flags);
0335     return frame;
0336 }
0337 EXPORT_SYMBOL_GPL(tb_ring_poll);
0338 
0339 static void __ring_interrupt_mask(struct tb_ring *ring, bool mask)
0340 {
0341     int idx = ring_interrupt_index(ring);
0342     int reg = REG_RING_INTERRUPT_BASE + idx / 32 * 4;
0343     int bit = idx % 32;
0344     u32 val;
0345 
0346     val = ioread32(ring->nhi->iobase + reg);
0347     if (mask)
0348         val &= ~BIT(bit);
0349     else
0350         val |= BIT(bit);
0351     iowrite32(val, ring->nhi->iobase + reg);
0352 }
0353 
0354 /* Both @nhi->lock and @ring->lock should be held */
0355 static void __ring_interrupt(struct tb_ring *ring)
0356 {
0357     if (!ring->running)
0358         return;
0359 
0360     if (ring->start_poll) {
0361         __ring_interrupt_mask(ring, true);
0362         ring->start_poll(ring->poll_data);
0363     } else {
0364         schedule_work(&ring->work);
0365     }
0366 }
0367 
0368 /**
0369  * tb_ring_poll_complete() - Re-start interrupt for the ring
0370  * @ring: Ring to re-start the interrupt
0371  *
0372  * This will re-start (unmask) the ring interrupt once the user is done
0373  * with polling.
0374  */
0375 void tb_ring_poll_complete(struct tb_ring *ring)
0376 {
0377     unsigned long flags;
0378 
0379     spin_lock_irqsave(&ring->nhi->lock, flags);
0380     spin_lock(&ring->lock);
0381     if (ring->start_poll)
0382         __ring_interrupt_mask(ring, false);
0383     spin_unlock(&ring->lock);
0384     spin_unlock_irqrestore(&ring->nhi->lock, flags);
0385 }
0386 EXPORT_SYMBOL_GPL(tb_ring_poll_complete);
0387 
0388 static void ring_clear_msix(const struct tb_ring *ring)
0389 {
0390     if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
0391         return;
0392 
0393     if (ring->is_tx)
0394         ioread32(ring->nhi->iobase + REG_RING_NOTIFY_BASE);
0395     else
0396         ioread32(ring->nhi->iobase + REG_RING_NOTIFY_BASE +
0397              4 * (ring->nhi->hop_count / 32));
0398 }
0399 
0400 static irqreturn_t ring_msix(int irq, void *data)
0401 {
0402     struct tb_ring *ring = data;
0403 
0404     spin_lock(&ring->nhi->lock);
0405     ring_clear_msix(ring);
0406     spin_lock(&ring->lock);
0407     __ring_interrupt(ring);
0408     spin_unlock(&ring->lock);
0409     spin_unlock(&ring->nhi->lock);
0410 
0411     return IRQ_HANDLED;
0412 }
0413 
0414 static int ring_request_msix(struct tb_ring *ring, bool no_suspend)
0415 {
0416     struct tb_nhi *nhi = ring->nhi;
0417     unsigned long irqflags;
0418     int ret;
0419 
0420     if (!nhi->pdev->msix_enabled)
0421         return 0;
0422 
0423     ret = ida_simple_get(&nhi->msix_ida, 0, MSIX_MAX_VECS, GFP_KERNEL);
0424     if (ret < 0)
0425         return ret;
0426 
0427     ring->vector = ret;
0428 
0429     ret = pci_irq_vector(ring->nhi->pdev, ring->vector);
0430     if (ret < 0)
0431         goto err_ida_remove;
0432 
0433     ring->irq = ret;
0434 
0435     irqflags = no_suspend ? IRQF_NO_SUSPEND : 0;
0436     ret = request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring);
0437     if (ret)
0438         goto err_ida_remove;
0439 
0440     return 0;
0441 
0442 err_ida_remove:
0443     ida_simple_remove(&nhi->msix_ida, ring->vector);
0444 
0445     return ret;
0446 }
0447 
0448 static void ring_release_msix(struct tb_ring *ring)
0449 {
0450     if (ring->irq <= 0)
0451         return;
0452 
0453     free_irq(ring->irq, ring);
0454     ida_simple_remove(&ring->nhi->msix_ida, ring->vector);
0455     ring->vector = 0;
0456     ring->irq = 0;
0457 }
0458 
0459 static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring)
0460 {
0461     int ret = 0;
0462 
0463     spin_lock_irq(&nhi->lock);
0464 
0465     if (ring->hop < 0) {
0466         unsigned int i;
0467 
0468         /*
0469          * Automatically allocate HopID from the non-reserved
0470          * range 1 .. hop_count - 1.
0471          */
0472         for (i = RING_FIRST_USABLE_HOPID; i < nhi->hop_count; i++) {
0473             if (ring->is_tx) {
0474                 if (!nhi->tx_rings[i]) {
0475                     ring->hop = i;
0476                     break;
0477                 }
0478             } else {
0479                 if (!nhi->rx_rings[i]) {
0480                     ring->hop = i;
0481                     break;
0482                 }
0483             }
0484         }
0485     }
0486 
0487     if (ring->hop < 0 || ring->hop >= nhi->hop_count) {
0488         dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
0489         ret = -EINVAL;
0490         goto err_unlock;
0491     }
0492     if (ring->is_tx && nhi->tx_rings[ring->hop]) {
0493         dev_warn(&nhi->pdev->dev, "TX hop %d already allocated\n",
0494              ring->hop);
0495         ret = -EBUSY;
0496         goto err_unlock;
0497     } else if (!ring->is_tx && nhi->rx_rings[ring->hop]) {
0498         dev_warn(&nhi->pdev->dev, "RX hop %d already allocated\n",
0499              ring->hop);
0500         ret = -EBUSY;
0501         goto err_unlock;
0502     }
0503 
0504     if (ring->is_tx)
0505         nhi->tx_rings[ring->hop] = ring;
0506     else
0507         nhi->rx_rings[ring->hop] = ring;
0508 
0509 err_unlock:
0510     spin_unlock_irq(&nhi->lock);
0511 
0512     return ret;
0513 }
0514 
0515 static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
0516                      bool transmit, unsigned int flags,
0517                      int e2e_tx_hop, u16 sof_mask, u16 eof_mask,
0518                      void (*start_poll)(void *),
0519                      void *poll_data)
0520 {
0521     struct tb_ring *ring = NULL;
0522 
0523     dev_dbg(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
0524         transmit ? "TX" : "RX", hop, size);
0525 
0526     ring = kzalloc(sizeof(*ring), GFP_KERNEL);
0527     if (!ring)
0528         return NULL;
0529 
0530     spin_lock_init(&ring->lock);
0531     INIT_LIST_HEAD(&ring->queue);
0532     INIT_LIST_HEAD(&ring->in_flight);
0533     INIT_WORK(&ring->work, ring_work);
0534 
0535     ring->nhi = nhi;
0536     ring->hop = hop;
0537     ring->is_tx = transmit;
0538     ring->size = size;
0539     ring->flags = flags;
0540     ring->e2e_tx_hop = e2e_tx_hop;
0541     ring->sof_mask = sof_mask;
0542     ring->eof_mask = eof_mask;
0543     ring->head = 0;
0544     ring->tail = 0;
0545     ring->running = false;
0546     ring->start_poll = start_poll;
0547     ring->poll_data = poll_data;
0548 
0549     ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
0550             size * sizeof(*ring->descriptors),
0551             &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
0552     if (!ring->descriptors)
0553         goto err_free_ring;
0554 
0555     if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND))
0556         goto err_free_descs;
0557 
0558     if (nhi_alloc_hop(nhi, ring))
0559         goto err_release_msix;
0560 
0561     return ring;
0562 
0563 err_release_msix:
0564     ring_release_msix(ring);
0565 err_free_descs:
0566     dma_free_coherent(&ring->nhi->pdev->dev,
0567               ring->size * sizeof(*ring->descriptors),
0568               ring->descriptors, ring->descriptors_dma);
0569 err_free_ring:
0570     kfree(ring);
0571 
0572     return NULL;
0573 }
0574 
0575 /**
0576  * tb_ring_alloc_tx() - Allocate DMA ring for transmit
0577  * @nhi: Pointer to the NHI the ring is to be allocated
0578  * @hop: HopID (ring) to allocate
0579  * @size: Number of entries in the ring
0580  * @flags: Flags for the ring
0581  */
0582 struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
0583                  unsigned int flags)
0584 {
0585     return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0, 0, NULL, NULL);
0586 }
0587 EXPORT_SYMBOL_GPL(tb_ring_alloc_tx);
0588 
0589 /**
0590  * tb_ring_alloc_rx() - Allocate DMA ring for receive
0591  * @nhi: Pointer to the NHI the ring is to be allocated
0592  * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
0593  * @size: Number of entries in the ring
0594  * @flags: Flags for the ring
0595  * @e2e_tx_hop: Transmit HopID when E2E is enabled in @flags
0596  * @sof_mask: Mask of PDF values that start a frame
0597  * @eof_mask: Mask of PDF values that end a frame
0598  * @start_poll: If not %NULL the ring will call this function when an
0599  *      interrupt is triggered and masked, instead of callback
0600  *      in each Rx frame.
0601  * @poll_data: Optional data passed to @start_poll
0602  */
0603 struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
0604                  unsigned int flags, int e2e_tx_hop,
0605                  u16 sof_mask, u16 eof_mask,
0606                  void (*start_poll)(void *), void *poll_data)
0607 {
0608     return tb_ring_alloc(nhi, hop, size, false, flags, e2e_tx_hop, sof_mask, eof_mask,
0609                  start_poll, poll_data);
0610 }
0611 EXPORT_SYMBOL_GPL(tb_ring_alloc_rx);
0612 
0613 /**
0614  * tb_ring_start() - enable a ring
0615  * @ring: Ring to start
0616  *
0617  * Must not be invoked in parallel with tb_ring_stop().
0618  */
0619 void tb_ring_start(struct tb_ring *ring)
0620 {
0621     u16 frame_size;
0622     u32 flags;
0623 
0624     spin_lock_irq(&ring->nhi->lock);
0625     spin_lock(&ring->lock);
0626     if (ring->nhi->going_away)
0627         goto err;
0628     if (ring->running) {
0629         dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
0630         goto err;
0631     }
0632     dev_dbg(&ring->nhi->pdev->dev, "starting %s %d\n",
0633         RING_TYPE(ring), ring->hop);
0634 
0635     if (ring->flags & RING_FLAG_FRAME) {
0636         /* Means 4096 */
0637         frame_size = 0;
0638         flags = RING_FLAG_ENABLE;
0639     } else {
0640         frame_size = TB_FRAME_SIZE;
0641         flags = RING_FLAG_ENABLE | RING_FLAG_RAW;
0642     }
0643 
0644     ring_iowrite64desc(ring, ring->descriptors_dma, 0);
0645     if (ring->is_tx) {
0646         ring_iowrite32desc(ring, ring->size, 12);
0647         ring_iowrite32options(ring, 0, 4); /* time releated ? */
0648         ring_iowrite32options(ring, flags, 0);
0649     } else {
0650         u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask;
0651 
0652         ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12);
0653         ring_iowrite32options(ring, sof_eof_mask, 4);
0654         ring_iowrite32options(ring, flags, 0);
0655     }
0656 
0657     /*
0658      * Now that the ring valid bit is set we can configure E2E if
0659      * enabled for the ring.
0660      */
0661     if (ring->flags & RING_FLAG_E2E) {
0662         if (!ring->is_tx) {
0663             u32 hop;
0664 
0665             hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT;
0666             hop &= REG_RX_OPTIONS_E2E_HOP_MASK;
0667             flags |= hop;
0668 
0669             dev_dbg(&ring->nhi->pdev->dev,
0670                 "enabling E2E for %s %d with TX HopID %d\n",
0671                 RING_TYPE(ring), ring->hop, ring->e2e_tx_hop);
0672         } else {
0673             dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d\n",
0674                 RING_TYPE(ring), ring->hop);
0675         }
0676 
0677         flags |= RING_FLAG_E2E_FLOW_CONTROL;
0678         ring_iowrite32options(ring, flags, 0);
0679     }
0680 
0681     ring_interrupt_active(ring, true);
0682     ring->running = true;
0683 err:
0684     spin_unlock(&ring->lock);
0685     spin_unlock_irq(&ring->nhi->lock);
0686 }
0687 EXPORT_SYMBOL_GPL(tb_ring_start);
0688 
0689 /**
0690  * tb_ring_stop() - shutdown a ring
0691  * @ring: Ring to stop
0692  *
0693  * Must not be invoked from a callback.
0694  *
0695  * This method will disable the ring. Further calls to
0696  * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been
0697  * called.
0698  *
0699  * All enqueued frames will be canceled and their callbacks will be executed
0700  * with frame->canceled set to true (on the callback thread). This method
0701  * returns only after all callback invocations have finished.
0702  */
0703 void tb_ring_stop(struct tb_ring *ring)
0704 {
0705     spin_lock_irq(&ring->nhi->lock);
0706     spin_lock(&ring->lock);
0707     dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n",
0708         RING_TYPE(ring), ring->hop);
0709     if (ring->nhi->going_away)
0710         goto err;
0711     if (!ring->running) {
0712         dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
0713              RING_TYPE(ring), ring->hop);
0714         goto err;
0715     }
0716     ring_interrupt_active(ring, false);
0717 
0718     ring_iowrite32options(ring, 0, 0);
0719     ring_iowrite64desc(ring, 0, 0);
0720     ring_iowrite32desc(ring, 0, 8);
0721     ring_iowrite32desc(ring, 0, 12);
0722     ring->head = 0;
0723     ring->tail = 0;
0724     ring->running = false;
0725 
0726 err:
0727     spin_unlock(&ring->lock);
0728     spin_unlock_irq(&ring->nhi->lock);
0729 
0730     /*
0731      * schedule ring->work to invoke callbacks on all remaining frames.
0732      */
0733     schedule_work(&ring->work);
0734     flush_work(&ring->work);
0735 }
0736 EXPORT_SYMBOL_GPL(tb_ring_stop);
0737 
0738 /*
0739  * tb_ring_free() - free ring
0740  *
0741  * When this method returns all invocations of ring->callback will have
0742  * finished.
0743  *
0744  * Ring must be stopped.
0745  *
0746  * Must NOT be called from ring_frame->callback!
0747  */
0748 void tb_ring_free(struct tb_ring *ring)
0749 {
0750     spin_lock_irq(&ring->nhi->lock);
0751     /*
0752      * Dissociate the ring from the NHI. This also ensures that
0753      * nhi_interrupt_work cannot reschedule ring->work.
0754      */
0755     if (ring->is_tx)
0756         ring->nhi->tx_rings[ring->hop] = NULL;
0757     else
0758         ring->nhi->rx_rings[ring->hop] = NULL;
0759 
0760     if (ring->running) {
0761         dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
0762              RING_TYPE(ring), ring->hop);
0763     }
0764     spin_unlock_irq(&ring->nhi->lock);
0765 
0766     ring_release_msix(ring);
0767 
0768     dma_free_coherent(&ring->nhi->pdev->dev,
0769               ring->size * sizeof(*ring->descriptors),
0770               ring->descriptors, ring->descriptors_dma);
0771 
0772     ring->descriptors = NULL;
0773     ring->descriptors_dma = 0;
0774 
0775 
0776     dev_dbg(&ring->nhi->pdev->dev, "freeing %s %d\n", RING_TYPE(ring),
0777         ring->hop);
0778 
0779     /*
0780      * ring->work can no longer be scheduled (it is scheduled only
0781      * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
0782      * to finish before freeing the ring.
0783      */
0784     flush_work(&ring->work);
0785     kfree(ring);
0786 }
0787 EXPORT_SYMBOL_GPL(tb_ring_free);
0788 
0789 /**
0790  * nhi_mailbox_cmd() - Send a command through NHI mailbox
0791  * @nhi: Pointer to the NHI structure
0792  * @cmd: Command to send
0793  * @data: Data to be send with the command
0794  *
0795  * Sends mailbox command to the firmware running on NHI. Returns %0 in
0796  * case of success and negative errno in case of failure.
0797  */
0798 int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
0799 {
0800     ktime_t timeout;
0801     u32 val;
0802 
0803     iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
0804 
0805     val = ioread32(nhi->iobase + REG_INMAIL_CMD);
0806     val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
0807     val |= REG_INMAIL_OP_REQUEST | cmd;
0808     iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
0809 
0810     timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT);
0811     do {
0812         val = ioread32(nhi->iobase + REG_INMAIL_CMD);
0813         if (!(val & REG_INMAIL_OP_REQUEST))
0814             break;
0815         usleep_range(10, 20);
0816     } while (ktime_before(ktime_get(), timeout));
0817 
0818     if (val & REG_INMAIL_OP_REQUEST)
0819         return -ETIMEDOUT;
0820     if (val & REG_INMAIL_ERROR)
0821         return -EIO;
0822 
0823     return 0;
0824 }
0825 
0826 /**
0827  * nhi_mailbox_mode() - Return current firmware operation mode
0828  * @nhi: Pointer to the NHI structure
0829  *
0830  * The function reads current firmware operation mode using NHI mailbox
0831  * registers and returns it to the caller.
0832  */
0833 enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
0834 {
0835     u32 val;
0836 
0837     val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
0838     val &= REG_OUTMAIL_CMD_OPMODE_MASK;
0839     val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
0840 
0841     return (enum nhi_fw_mode)val;
0842 }
0843 
0844 static void nhi_interrupt_work(struct work_struct *work)
0845 {
0846     struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
0847     int value = 0; /* Suppress uninitialized usage warning. */
0848     int bit;
0849     int hop = -1;
0850     int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
0851     struct tb_ring *ring;
0852 
0853     spin_lock_irq(&nhi->lock);
0854 
0855     /*
0856      * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
0857      * (TX, RX, RX overflow). We iterate over the bits and read a new
0858      * dwords as required. The registers are cleared on read.
0859      */
0860     for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
0861         if (bit % 32 == 0)
0862             value = ioread32(nhi->iobase
0863                      + REG_RING_NOTIFY_BASE
0864                      + 4 * (bit / 32));
0865         if (++hop == nhi->hop_count) {
0866             hop = 0;
0867             type++;
0868         }
0869         if ((value & (1 << (bit % 32))) == 0)
0870             continue;
0871         if (type == 2) {
0872             dev_warn(&nhi->pdev->dev,
0873                  "RX overflow for ring %d\n",
0874                  hop);
0875             continue;
0876         }
0877         if (type == 0)
0878             ring = nhi->tx_rings[hop];
0879         else
0880             ring = nhi->rx_rings[hop];
0881         if (ring == NULL) {
0882             dev_warn(&nhi->pdev->dev,
0883                  "got interrupt for inactive %s ring %d\n",
0884                  type ? "RX" : "TX",
0885                  hop);
0886             continue;
0887         }
0888 
0889         spin_lock(&ring->lock);
0890         __ring_interrupt(ring);
0891         spin_unlock(&ring->lock);
0892     }
0893     spin_unlock_irq(&nhi->lock);
0894 }
0895 
0896 static irqreturn_t nhi_msi(int irq, void *data)
0897 {
0898     struct tb_nhi *nhi = data;
0899     schedule_work(&nhi->interrupt_work);
0900     return IRQ_HANDLED;
0901 }
0902 
0903 static int __nhi_suspend_noirq(struct device *dev, bool wakeup)
0904 {
0905     struct pci_dev *pdev = to_pci_dev(dev);
0906     struct tb *tb = pci_get_drvdata(pdev);
0907     struct tb_nhi *nhi = tb->nhi;
0908     int ret;
0909 
0910     ret = tb_domain_suspend_noirq(tb);
0911     if (ret)
0912         return ret;
0913 
0914     if (nhi->ops && nhi->ops->suspend_noirq) {
0915         ret = nhi->ops->suspend_noirq(tb->nhi, wakeup);
0916         if (ret)
0917             return ret;
0918     }
0919 
0920     return 0;
0921 }
0922 
0923 static int nhi_suspend_noirq(struct device *dev)
0924 {
0925     return __nhi_suspend_noirq(dev, device_may_wakeup(dev));
0926 }
0927 
0928 static int nhi_freeze_noirq(struct device *dev)
0929 {
0930     struct pci_dev *pdev = to_pci_dev(dev);
0931     struct tb *tb = pci_get_drvdata(pdev);
0932 
0933     return tb_domain_freeze_noirq(tb);
0934 }
0935 
0936 static int nhi_thaw_noirq(struct device *dev)
0937 {
0938     struct pci_dev *pdev = to_pci_dev(dev);
0939     struct tb *tb = pci_get_drvdata(pdev);
0940 
0941     return tb_domain_thaw_noirq(tb);
0942 }
0943 
0944 static bool nhi_wake_supported(struct pci_dev *pdev)
0945 {
0946     u8 val;
0947 
0948     /*
0949      * If power rails are sustainable for wakeup from S4 this
0950      * property is set by the BIOS.
0951      */
0952     if (device_property_read_u8(&pdev->dev, "WAKE_SUPPORTED", &val))
0953         return !!val;
0954 
0955     return true;
0956 }
0957 
0958 static int nhi_poweroff_noirq(struct device *dev)
0959 {
0960     struct pci_dev *pdev = to_pci_dev(dev);
0961     bool wakeup;
0962 
0963     wakeup = device_may_wakeup(dev) && nhi_wake_supported(pdev);
0964     return __nhi_suspend_noirq(dev, wakeup);
0965 }
0966 
0967 static void nhi_enable_int_throttling(struct tb_nhi *nhi)
0968 {
0969     /* Throttling is specified in 256ns increments */
0970     u32 throttle = DIV_ROUND_UP(128 * NSEC_PER_USEC, 256);
0971     unsigned int i;
0972 
0973     /*
0974      * Configure interrupt throttling for all vectors even if we
0975      * only use few.
0976      */
0977     for (i = 0; i < MSIX_MAX_VECS; i++) {
0978         u32 reg = REG_INT_THROTTLING_RATE + i * 4;
0979         iowrite32(throttle, nhi->iobase + reg);
0980     }
0981 }
0982 
0983 static int nhi_resume_noirq(struct device *dev)
0984 {
0985     struct pci_dev *pdev = to_pci_dev(dev);
0986     struct tb *tb = pci_get_drvdata(pdev);
0987     struct tb_nhi *nhi = tb->nhi;
0988     int ret;
0989 
0990     /*
0991      * Check that the device is still there. It may be that the user
0992      * unplugged last device which causes the host controller to go
0993      * away on PCs.
0994      */
0995     if (!pci_device_is_present(pdev)) {
0996         nhi->going_away = true;
0997     } else {
0998         if (nhi->ops && nhi->ops->resume_noirq) {
0999             ret = nhi->ops->resume_noirq(nhi);
1000             if (ret)
1001                 return ret;
1002         }
1003         nhi_enable_int_throttling(tb->nhi);
1004     }
1005 
1006     return tb_domain_resume_noirq(tb);
1007 }
1008 
1009 static int nhi_suspend(struct device *dev)
1010 {
1011     struct pci_dev *pdev = to_pci_dev(dev);
1012     struct tb *tb = pci_get_drvdata(pdev);
1013 
1014     return tb_domain_suspend(tb);
1015 }
1016 
1017 static void nhi_complete(struct device *dev)
1018 {
1019     struct pci_dev *pdev = to_pci_dev(dev);
1020     struct tb *tb = pci_get_drvdata(pdev);
1021 
1022     /*
1023      * If we were runtime suspended when system suspend started,
1024      * schedule runtime resume now. It should bring the domain back
1025      * to functional state.
1026      */
1027     if (pm_runtime_suspended(&pdev->dev))
1028         pm_runtime_resume(&pdev->dev);
1029     else
1030         tb_domain_complete(tb);
1031 }
1032 
1033 static int nhi_runtime_suspend(struct device *dev)
1034 {
1035     struct pci_dev *pdev = to_pci_dev(dev);
1036     struct tb *tb = pci_get_drvdata(pdev);
1037     struct tb_nhi *nhi = tb->nhi;
1038     int ret;
1039 
1040     ret = tb_domain_runtime_suspend(tb);
1041     if (ret)
1042         return ret;
1043 
1044     if (nhi->ops && nhi->ops->runtime_suspend) {
1045         ret = nhi->ops->runtime_suspend(tb->nhi);
1046         if (ret)
1047             return ret;
1048     }
1049     return 0;
1050 }
1051 
1052 static int nhi_runtime_resume(struct device *dev)
1053 {
1054     struct pci_dev *pdev = to_pci_dev(dev);
1055     struct tb *tb = pci_get_drvdata(pdev);
1056     struct tb_nhi *nhi = tb->nhi;
1057     int ret;
1058 
1059     if (nhi->ops && nhi->ops->runtime_resume) {
1060         ret = nhi->ops->runtime_resume(nhi);
1061         if (ret)
1062             return ret;
1063     }
1064 
1065     nhi_enable_int_throttling(nhi);
1066     return tb_domain_runtime_resume(tb);
1067 }
1068 
1069 static void nhi_shutdown(struct tb_nhi *nhi)
1070 {
1071     int i;
1072 
1073     dev_dbg(&nhi->pdev->dev, "shutdown\n");
1074 
1075     for (i = 0; i < nhi->hop_count; i++) {
1076         if (nhi->tx_rings[i])
1077             dev_WARN(&nhi->pdev->dev,
1078                  "TX ring %d is still active\n", i);
1079         if (nhi->rx_rings[i])
1080             dev_WARN(&nhi->pdev->dev,
1081                  "RX ring %d is still active\n", i);
1082     }
1083     nhi_disable_interrupts(nhi);
1084     /*
1085      * We have to release the irq before calling flush_work. Otherwise an
1086      * already executing IRQ handler could call schedule_work again.
1087      */
1088     if (!nhi->pdev->msix_enabled) {
1089         devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
1090         flush_work(&nhi->interrupt_work);
1091     }
1092     ida_destroy(&nhi->msix_ida);
1093 
1094     if (nhi->ops && nhi->ops->shutdown)
1095         nhi->ops->shutdown(nhi);
1096 }
1097 
1098 static void nhi_check_quirks(struct tb_nhi *nhi)
1099 {
1100     /*
1101      * Intel hardware supports auto clear of the interrupt status
1102      * reqister right after interrupt is being issued.
1103      */
1104     if (nhi->pdev->vendor == PCI_VENDOR_ID_INTEL)
1105         nhi->quirks |= QUIRK_AUTO_CLEAR_INT;
1106 }
1107 
1108 static int nhi_check_iommu_pdev(struct pci_dev *pdev, void *data)
1109 {
1110     if (!pdev->external_facing ||
1111         !device_iommu_capable(&pdev->dev, IOMMU_CAP_PRE_BOOT_PROTECTION))
1112         return 0;
1113     *(bool *)data = true;
1114     return 1; /* Stop walking */
1115 }
1116 
1117 static void nhi_check_iommu(struct tb_nhi *nhi)
1118 {
1119     struct pci_bus *bus = nhi->pdev->bus;
1120     bool port_ok = false;
1121 
1122     /*
1123      * Ideally what we'd do here is grab every PCI device that
1124      * represents a tunnelling adapter for this NHI and check their
1125      * status directly, but unfortunately USB4 seems to make it
1126      * obnoxiously difficult to reliably make any correlation.
1127      *
1128      * So for now we'll have to bodge it... Hoping that the system
1129      * is at least sane enough that an adapter is in the same PCI
1130      * segment as its NHI, if we can find *something* on that segment
1131      * which meets the requirements for Kernel DMA Protection, we'll
1132      * take that to imply that firmware is aware and has (hopefully)
1133      * done the right thing in general. We need to know that the PCI
1134      * layer has seen the ExternalFacingPort property which will then
1135      * inform the IOMMU layer to enforce the complete "untrusted DMA"
1136      * flow, but also that the IOMMU driver itself can be trusted not
1137      * to have been subverted by a pre-boot DMA attack.
1138      */
1139     while (bus->parent)
1140         bus = bus->parent;
1141 
1142     pci_walk_bus(bus, nhi_check_iommu_pdev, &port_ok);
1143 
1144     nhi->iommu_dma_protection = port_ok;
1145     dev_dbg(&nhi->pdev->dev, "IOMMU DMA protection is %s\n",
1146         str_enabled_disabled(port_ok));
1147 }
1148 
1149 static int nhi_init_msi(struct tb_nhi *nhi)
1150 {
1151     struct pci_dev *pdev = nhi->pdev;
1152     int res, irq, nvec;
1153 
1154     /* In case someone left them on. */
1155     nhi_disable_interrupts(nhi);
1156 
1157     nhi_enable_int_throttling(nhi);
1158 
1159     ida_init(&nhi->msix_ida);
1160 
1161     /*
1162      * The NHI has 16 MSI-X vectors or a single MSI. We first try to
1163      * get all MSI-X vectors and if we succeed, each ring will have
1164      * one MSI-X. If for some reason that does not work out, we
1165      * fallback to a single MSI.
1166      */
1167     nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS,
1168                      PCI_IRQ_MSIX);
1169     if (nvec < 0) {
1170         nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
1171         if (nvec < 0)
1172             return nvec;
1173 
1174         INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
1175 
1176         irq = pci_irq_vector(nhi->pdev, 0);
1177         if (irq < 0)
1178             return irq;
1179 
1180         res = devm_request_irq(&pdev->dev, irq, nhi_msi,
1181                        IRQF_NO_SUSPEND, "thunderbolt", nhi);
1182         if (res) {
1183             dev_err(&pdev->dev, "request_irq failed, aborting\n");
1184             return res;
1185         }
1186     }
1187 
1188     return 0;
1189 }
1190 
1191 static bool nhi_imr_valid(struct pci_dev *pdev)
1192 {
1193     u8 val;
1194 
1195     if (!device_property_read_u8(&pdev->dev, "IMR_VALID", &val))
1196         return !!val;
1197 
1198     return true;
1199 }
1200 
1201 static struct tb *nhi_select_cm(struct tb_nhi *nhi)
1202 {
1203     struct tb *tb;
1204 
1205     /*
1206      * USB4 case is simple. If we got control of any of the
1207      * capabilities, we use software CM.
1208      */
1209     if (tb_acpi_is_native())
1210         return tb_probe(nhi);
1211 
1212     /*
1213      * Either firmware based CM is running (we did not get control
1214      * from the firmware) or this is pre-USB4 PC so try first
1215      * firmware CM and then fallback to software CM.
1216      */
1217     tb = icm_probe(nhi);
1218     if (!tb)
1219         tb = tb_probe(nhi);
1220 
1221     return tb;
1222 }
1223 
1224 static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1225 {
1226     struct tb_nhi *nhi;
1227     struct tb *tb;
1228     int res;
1229 
1230     if (!nhi_imr_valid(pdev)) {
1231         dev_warn(&pdev->dev, "firmware image not valid, aborting\n");
1232         return -ENODEV;
1233     }
1234 
1235     res = pcim_enable_device(pdev);
1236     if (res) {
1237         dev_err(&pdev->dev, "cannot enable PCI device, aborting\n");
1238         return res;
1239     }
1240 
1241     res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
1242     if (res) {
1243         dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
1244         return res;
1245     }
1246 
1247     nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
1248     if (!nhi)
1249         return -ENOMEM;
1250 
1251     nhi->pdev = pdev;
1252     nhi->ops = (const struct tb_nhi_ops *)id->driver_data;
1253     /* cannot fail - table is allocated in pcim_iomap_regions */
1254     nhi->iobase = pcim_iomap_table(pdev)[0];
1255     nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff;
1256     dev_dbg(&pdev->dev, "total paths: %d\n", nhi->hop_count);
1257 
1258     nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
1259                      sizeof(*nhi->tx_rings), GFP_KERNEL);
1260     nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
1261                      sizeof(*nhi->rx_rings), GFP_KERNEL);
1262     if (!nhi->tx_rings || !nhi->rx_rings)
1263         return -ENOMEM;
1264 
1265     nhi_check_quirks(nhi);
1266     nhi_check_iommu(nhi);
1267 
1268     res = nhi_init_msi(nhi);
1269     if (res) {
1270         dev_err(&pdev->dev, "cannot enable MSI, aborting\n");
1271         return res;
1272     }
1273 
1274     spin_lock_init(&nhi->lock);
1275 
1276     res = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1277     if (res) {
1278         dev_err(&pdev->dev, "failed to set DMA mask\n");
1279         return res;
1280     }
1281 
1282     pci_set_master(pdev);
1283 
1284     if (nhi->ops && nhi->ops->init) {
1285         res = nhi->ops->init(nhi);
1286         if (res)
1287             return res;
1288     }
1289 
1290     tb = nhi_select_cm(nhi);
1291     if (!tb) {
1292         dev_err(&nhi->pdev->dev,
1293             "failed to determine connection manager, aborting\n");
1294         return -ENODEV;
1295     }
1296 
1297     dev_dbg(&nhi->pdev->dev, "NHI initialized, starting thunderbolt\n");
1298 
1299     res = tb_domain_add(tb);
1300     if (res) {
1301         /*
1302          * At this point the RX/TX rings might already have been
1303          * activated. Do a proper shutdown.
1304          */
1305         tb_domain_put(tb);
1306         nhi_shutdown(nhi);
1307         return res;
1308     }
1309     pci_set_drvdata(pdev, tb);
1310 
1311     device_wakeup_enable(&pdev->dev);
1312 
1313     pm_runtime_allow(&pdev->dev);
1314     pm_runtime_set_autosuspend_delay(&pdev->dev, TB_AUTOSUSPEND_DELAY);
1315     pm_runtime_use_autosuspend(&pdev->dev);
1316     pm_runtime_put_autosuspend(&pdev->dev);
1317 
1318     return 0;
1319 }
1320 
1321 static void nhi_remove(struct pci_dev *pdev)
1322 {
1323     struct tb *tb = pci_get_drvdata(pdev);
1324     struct tb_nhi *nhi = tb->nhi;
1325 
1326     pm_runtime_get_sync(&pdev->dev);
1327     pm_runtime_dont_use_autosuspend(&pdev->dev);
1328     pm_runtime_forbid(&pdev->dev);
1329 
1330     tb_domain_remove(tb);
1331     nhi_shutdown(nhi);
1332 }
1333 
1334 /*
1335  * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
1336  * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
1337  * resume_noirq until we are done.
1338  */
1339 static const struct dev_pm_ops nhi_pm_ops = {
1340     .suspend_noirq = nhi_suspend_noirq,
1341     .resume_noirq = nhi_resume_noirq,
1342     .freeze_noirq = nhi_freeze_noirq,  /*
1343                         * we just disable hotplug, the
1344                         * pci-tunnels stay alive.
1345                         */
1346     .thaw_noirq = nhi_thaw_noirq,
1347     .restore_noirq = nhi_resume_noirq,
1348     .suspend = nhi_suspend,
1349     .poweroff_noirq = nhi_poweroff_noirq,
1350     .poweroff = nhi_suspend,
1351     .complete = nhi_complete,
1352     .runtime_suspend = nhi_runtime_suspend,
1353     .runtime_resume = nhi_runtime_resume,
1354 };
1355 
1356 static struct pci_device_id nhi_ids[] = {
1357     /*
1358      * We have to specify class, the TB bridges use the same device and
1359      * vendor (sub)id on gen 1 and gen 2 controllers.
1360      */
1361     {
1362         .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1363         .vendor = PCI_VENDOR_ID_INTEL,
1364         .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
1365         .subvendor = 0x2222, .subdevice = 0x1111,
1366     },
1367     {
1368         .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1369         .vendor = PCI_VENDOR_ID_INTEL,
1370         .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
1371         .subvendor = 0x2222, .subdevice = 0x1111,
1372     },
1373     {
1374         .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1375         .vendor = PCI_VENDOR_ID_INTEL,
1376         .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI,
1377         .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
1378     },
1379     {
1380         .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1381         .vendor = PCI_VENDOR_ID_INTEL,
1382         .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI,
1383         .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
1384     },
1385 
1386     /* Thunderbolt 3 */
1387     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) },
1388     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) },
1389     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) },
1390     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) },
1391     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) },
1392     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) },
1393     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) },
1394     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) },
1395     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI) },
1396     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI) },
1397     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI0),
1398       .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1399     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI1),
1400       .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1401     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI0),
1402       .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1403     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI1),
1404       .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1405     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI0),
1406       .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1407     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI1),
1408       .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1409     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI0),
1410       .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1411     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI1),
1412       .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1413     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI0),
1414       .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1415     { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI1),
1416       .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1417 
1418     /* Any USB4 compliant host */
1419     { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_USB4, ~0) },
1420 
1421     { 0,}
1422 };
1423 
1424 MODULE_DEVICE_TABLE(pci, nhi_ids);
1425 MODULE_LICENSE("GPL");
1426 
1427 static struct pci_driver nhi_driver = {
1428     .name = "thunderbolt",
1429     .id_table = nhi_ids,
1430     .probe = nhi_probe,
1431     .remove = nhi_remove,
1432     .shutdown = nhi_remove,
1433     .driver.pm = &nhi_pm_ops,
1434 };
1435 
1436 static int __init nhi_init(void)
1437 {
1438     int ret;
1439 
1440     ret = tb_domain_init();
1441     if (ret)
1442         return ret;
1443     ret = pci_register_driver(&nhi_driver);
1444     if (ret)
1445         tb_domain_exit();
1446     return ret;
1447 }
1448 
1449 static void __exit nhi_unload(void)
1450 {
1451     pci_unregister_driver(&nhi_driver);
1452     tb_domain_exit();
1453 }
1454 
1455 rootfs_initcall(nhi_init);
1456 module_exit(nhi_unload);