116603153SAndreas Noever /* 216603153SAndreas Noever * Thunderbolt Cactus Ridge driver - NHI driver 316603153SAndreas Noever * 416603153SAndreas Noever * The NHI (native host interface) is the pci device that allows us to send and 516603153SAndreas Noever * receive frames from the thunderbolt bus. 616603153SAndreas Noever * 716603153SAndreas Noever * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> 816603153SAndreas Noever */ 916603153SAndreas Noever 1023dd5bb4SAndreas Noever #include <linux/pm_runtime.h> 1116603153SAndreas Noever #include <linux/slab.h> 1216603153SAndreas Noever #include <linux/errno.h> 1316603153SAndreas Noever #include <linux/pci.h> 1416603153SAndreas Noever #include <linux/interrupt.h> 1516603153SAndreas Noever #include <linux/module.h> 16cd446ee2SMika Westerberg #include <linux/delay.h> 1716603153SAndreas Noever 1816603153SAndreas Noever #include "nhi.h" 1916603153SAndreas Noever #include "nhi_regs.h" 20d6cc51cdSAndreas Noever #include "tb.h" 2116603153SAndreas Noever 2216603153SAndreas Noever #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring") 2316603153SAndreas Noever 24046bee1fSMika Westerberg /* 25046bee1fSMika Westerberg * Minimal number of vectors when we use MSI-X. Two for control channel 26046bee1fSMika Westerberg * Rx/Tx and the rest four are for cross domain DMA paths. 27046bee1fSMika Westerberg */ 28046bee1fSMika Westerberg #define MSIX_MIN_VECS 6 29046bee1fSMika Westerberg #define MSIX_MAX_VECS 16 3016603153SAndreas Noever 31cd446ee2SMika Westerberg #define NHI_MAILBOX_TIMEOUT 500 /* ms */ 32cd446ee2SMika Westerberg 3316603153SAndreas Noever static int ring_interrupt_index(struct tb_ring *ring) 3416603153SAndreas Noever { 3516603153SAndreas Noever int bit = ring->hop; 3616603153SAndreas Noever if (!ring->is_tx) 3716603153SAndreas Noever bit += ring->nhi->hop_count; 3816603153SAndreas Noever return bit; 3916603153SAndreas Noever } 4016603153SAndreas Noever 4116603153SAndreas Noever /** 4216603153SAndreas Noever * ring_interrupt_active() - activate/deactivate interrupts for a single ring 4316603153SAndreas Noever * 4416603153SAndreas Noever * ring->nhi->lock must be held. 4516603153SAndreas Noever */ 4616603153SAndreas Noever static void ring_interrupt_active(struct tb_ring *ring, bool active) 4716603153SAndreas Noever { 4819bf4d4fSLukas Wunner int reg = REG_RING_INTERRUPT_BASE + 4919bf4d4fSLukas Wunner ring_interrupt_index(ring) / 32 * 4; 5016603153SAndreas Noever int bit = ring_interrupt_index(ring) & 31; 5116603153SAndreas Noever int mask = 1 << bit; 5216603153SAndreas Noever u32 old, new; 53046bee1fSMika Westerberg 54046bee1fSMika Westerberg if (ring->irq > 0) { 55046bee1fSMika Westerberg u32 step, shift, ivr, misc; 56046bee1fSMika Westerberg void __iomem *ivr_base; 57046bee1fSMika Westerberg int index; 58046bee1fSMika Westerberg 59046bee1fSMika Westerberg if (ring->is_tx) 60046bee1fSMika Westerberg index = ring->hop; 61046bee1fSMika Westerberg else 62046bee1fSMika Westerberg index = ring->hop + ring->nhi->hop_count; 63046bee1fSMika Westerberg 64046bee1fSMika Westerberg /* 65046bee1fSMika Westerberg * Ask the hardware to clear interrupt status bits automatically 66046bee1fSMika Westerberg * since we already know which interrupt was triggered. 67046bee1fSMika Westerberg */ 68046bee1fSMika Westerberg misc = ioread32(ring->nhi->iobase + REG_DMA_MISC); 69046bee1fSMika Westerberg if (!(misc & REG_DMA_MISC_INT_AUTO_CLEAR)) { 70046bee1fSMika Westerberg misc |= REG_DMA_MISC_INT_AUTO_CLEAR; 71046bee1fSMika Westerberg iowrite32(misc, ring->nhi->iobase + REG_DMA_MISC); 72046bee1fSMika Westerberg } 73046bee1fSMika Westerberg 74046bee1fSMika Westerberg ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE; 75046bee1fSMika Westerberg step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS; 76046bee1fSMika Westerberg shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS; 77046bee1fSMika Westerberg ivr = ioread32(ivr_base + step); 78046bee1fSMika Westerberg ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift); 79046bee1fSMika Westerberg if (active) 80046bee1fSMika Westerberg ivr |= ring->vector << shift; 81046bee1fSMika Westerberg iowrite32(ivr, ivr_base + step); 82046bee1fSMika Westerberg } 83046bee1fSMika Westerberg 8416603153SAndreas Noever old = ioread32(ring->nhi->iobase + reg); 8516603153SAndreas Noever if (active) 8616603153SAndreas Noever new = old | mask; 8716603153SAndreas Noever else 8816603153SAndreas Noever new = old & ~mask; 8916603153SAndreas Noever 9016603153SAndreas Noever dev_info(&ring->nhi->pdev->dev, 9116603153SAndreas Noever "%s interrupt at register %#x bit %d (%#x -> %#x)\n", 9216603153SAndreas Noever active ? "enabling" : "disabling", reg, bit, old, new); 9316603153SAndreas Noever 9416603153SAndreas Noever if (new == old) 9516603153SAndreas Noever dev_WARN(&ring->nhi->pdev->dev, 9616603153SAndreas Noever "interrupt for %s %d is already %s\n", 9716603153SAndreas Noever RING_TYPE(ring), ring->hop, 9816603153SAndreas Noever active ? "enabled" : "disabled"); 9916603153SAndreas Noever iowrite32(new, ring->nhi->iobase + reg); 10016603153SAndreas Noever } 10116603153SAndreas Noever 10216603153SAndreas Noever /** 10316603153SAndreas Noever * nhi_disable_interrupts() - disable interrupts for all rings 10416603153SAndreas Noever * 10516603153SAndreas Noever * Use only during init and shutdown. 10616603153SAndreas Noever */ 10716603153SAndreas Noever static void nhi_disable_interrupts(struct tb_nhi *nhi) 10816603153SAndreas Noever { 10916603153SAndreas Noever int i = 0; 11016603153SAndreas Noever /* disable interrupts */ 11116603153SAndreas Noever for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++) 11216603153SAndreas Noever iowrite32(0, nhi->iobase + REG_RING_INTERRUPT_BASE + 4 * i); 11316603153SAndreas Noever 11416603153SAndreas Noever /* clear interrupt status bits */ 11516603153SAndreas Noever for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++) 11616603153SAndreas Noever ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + 4 * i); 11716603153SAndreas Noever } 11816603153SAndreas Noever 11916603153SAndreas Noever /* ring helper methods */ 12016603153SAndreas Noever 12116603153SAndreas Noever static void __iomem *ring_desc_base(struct tb_ring *ring) 12216603153SAndreas Noever { 12316603153SAndreas Noever void __iomem *io = ring->nhi->iobase; 12416603153SAndreas Noever io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE; 12516603153SAndreas Noever io += ring->hop * 16; 12616603153SAndreas Noever return io; 12716603153SAndreas Noever } 12816603153SAndreas Noever 12916603153SAndreas Noever static void __iomem *ring_options_base(struct tb_ring *ring) 13016603153SAndreas Noever { 13116603153SAndreas Noever void __iomem *io = ring->nhi->iobase; 13216603153SAndreas Noever io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE; 13316603153SAndreas Noever io += ring->hop * 32; 13416603153SAndreas Noever return io; 13516603153SAndreas Noever } 13616603153SAndreas Noever 13716603153SAndreas Noever static void ring_iowrite16desc(struct tb_ring *ring, u32 value, u32 offset) 13816603153SAndreas Noever { 13916603153SAndreas Noever iowrite16(value, ring_desc_base(ring) + offset); 14016603153SAndreas Noever } 14116603153SAndreas Noever 14216603153SAndreas Noever static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset) 14316603153SAndreas Noever { 14416603153SAndreas Noever iowrite32(value, ring_desc_base(ring) + offset); 14516603153SAndreas Noever } 14616603153SAndreas Noever 14716603153SAndreas Noever static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset) 14816603153SAndreas Noever { 14916603153SAndreas Noever iowrite32(value, ring_desc_base(ring) + offset); 15016603153SAndreas Noever iowrite32(value >> 32, ring_desc_base(ring) + offset + 4); 15116603153SAndreas Noever } 15216603153SAndreas Noever 15316603153SAndreas Noever static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset) 15416603153SAndreas Noever { 15516603153SAndreas Noever iowrite32(value, ring_options_base(ring) + offset); 15616603153SAndreas Noever } 15716603153SAndreas Noever 15816603153SAndreas Noever static bool ring_full(struct tb_ring *ring) 15916603153SAndreas Noever { 16016603153SAndreas Noever return ((ring->head + 1) % ring->size) == ring->tail; 16116603153SAndreas Noever } 16216603153SAndreas Noever 16316603153SAndreas Noever static bool ring_empty(struct tb_ring *ring) 16416603153SAndreas Noever { 16516603153SAndreas Noever return ring->head == ring->tail; 16616603153SAndreas Noever } 16716603153SAndreas Noever 16816603153SAndreas Noever /** 16916603153SAndreas Noever * ring_write_descriptors() - post frames from ring->queue to the controller 17016603153SAndreas Noever * 17116603153SAndreas Noever * ring->lock is held. 17216603153SAndreas Noever */ 17316603153SAndreas Noever static void ring_write_descriptors(struct tb_ring *ring) 17416603153SAndreas Noever { 17516603153SAndreas Noever struct ring_frame *frame, *n; 17616603153SAndreas Noever struct ring_desc *descriptor; 17716603153SAndreas Noever list_for_each_entry_safe(frame, n, &ring->queue, list) { 17816603153SAndreas Noever if (ring_full(ring)) 17916603153SAndreas Noever break; 18016603153SAndreas Noever list_move_tail(&frame->list, &ring->in_flight); 18116603153SAndreas Noever descriptor = &ring->descriptors[ring->head]; 18216603153SAndreas Noever descriptor->phys = frame->buffer_phy; 18316603153SAndreas Noever descriptor->time = 0; 18416603153SAndreas Noever descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT; 18516603153SAndreas Noever if (ring->is_tx) { 18616603153SAndreas Noever descriptor->length = frame->size; 18716603153SAndreas Noever descriptor->eof = frame->eof; 18816603153SAndreas Noever descriptor->sof = frame->sof; 18916603153SAndreas Noever } 19016603153SAndreas Noever ring->head = (ring->head + 1) % ring->size; 19116603153SAndreas Noever ring_iowrite16desc(ring, ring->head, ring->is_tx ? 10 : 8); 19216603153SAndreas Noever } 19316603153SAndreas Noever } 19416603153SAndreas Noever 19516603153SAndreas Noever /** 19616603153SAndreas Noever * ring_work() - progress completed frames 19716603153SAndreas Noever * 19816603153SAndreas Noever * If the ring is shutting down then all frames are marked as canceled and 19916603153SAndreas Noever * their callbacks are invoked. 20016603153SAndreas Noever * 20116603153SAndreas Noever * Otherwise we collect all completed frame from the ring buffer, write new 20216603153SAndreas Noever * frame to the ring buffer and invoke the callbacks for the completed frames. 20316603153SAndreas Noever */ 20416603153SAndreas Noever static void ring_work(struct work_struct *work) 20516603153SAndreas Noever { 20616603153SAndreas Noever struct tb_ring *ring = container_of(work, typeof(*ring), work); 20716603153SAndreas Noever struct ring_frame *frame; 20816603153SAndreas Noever bool canceled = false; 20916603153SAndreas Noever LIST_HEAD(done); 21016603153SAndreas Noever mutex_lock(&ring->lock); 21116603153SAndreas Noever 21216603153SAndreas Noever if (!ring->running) { 21316603153SAndreas Noever /* Move all frames to done and mark them as canceled. */ 21416603153SAndreas Noever list_splice_tail_init(&ring->in_flight, &done); 21516603153SAndreas Noever list_splice_tail_init(&ring->queue, &done); 21616603153SAndreas Noever canceled = true; 21716603153SAndreas Noever goto invoke_callback; 21816603153SAndreas Noever } 21916603153SAndreas Noever 22016603153SAndreas Noever while (!ring_empty(ring)) { 22116603153SAndreas Noever if (!(ring->descriptors[ring->tail].flags 22216603153SAndreas Noever & RING_DESC_COMPLETED)) 22316603153SAndreas Noever break; 22416603153SAndreas Noever frame = list_first_entry(&ring->in_flight, typeof(*frame), 22516603153SAndreas Noever list); 22616603153SAndreas Noever list_move_tail(&frame->list, &done); 22716603153SAndreas Noever if (!ring->is_tx) { 22816603153SAndreas Noever frame->size = ring->descriptors[ring->tail].length; 22916603153SAndreas Noever frame->eof = ring->descriptors[ring->tail].eof; 23016603153SAndreas Noever frame->sof = ring->descriptors[ring->tail].sof; 23116603153SAndreas Noever frame->flags = ring->descriptors[ring->tail].flags; 23216603153SAndreas Noever if (frame->sof != 0) 23316603153SAndreas Noever dev_WARN(&ring->nhi->pdev->dev, 23416603153SAndreas Noever "%s %d got unexpected SOF: %#x\n", 23516603153SAndreas Noever RING_TYPE(ring), ring->hop, 23616603153SAndreas Noever frame->sof); 23716603153SAndreas Noever /* 23816603153SAndreas Noever * known flags: 23916603153SAndreas Noever * raw not enabled, interupt not set: 0x2=0010 24016603153SAndreas Noever * raw enabled: 0xa=1010 24116603153SAndreas Noever * raw not enabled: 0xb=1011 24216603153SAndreas Noever * partial frame (>MAX_FRAME_SIZE): 0xe=1110 24316603153SAndreas Noever */ 24416603153SAndreas Noever if (frame->flags != 0xa) 24516603153SAndreas Noever dev_WARN(&ring->nhi->pdev->dev, 24616603153SAndreas Noever "%s %d got unexpected flags: %#x\n", 24716603153SAndreas Noever RING_TYPE(ring), ring->hop, 24816603153SAndreas Noever frame->flags); 24916603153SAndreas Noever } 25016603153SAndreas Noever ring->tail = (ring->tail + 1) % ring->size; 25116603153SAndreas Noever } 25216603153SAndreas Noever ring_write_descriptors(ring); 25316603153SAndreas Noever 25416603153SAndreas Noever invoke_callback: 25516603153SAndreas Noever mutex_unlock(&ring->lock); /* allow callbacks to schedule new work */ 25616603153SAndreas Noever while (!list_empty(&done)) { 25716603153SAndreas Noever frame = list_first_entry(&done, typeof(*frame), list); 25816603153SAndreas Noever /* 25916603153SAndreas Noever * The callback may reenqueue or delete frame. 26016603153SAndreas Noever * Do not hold on to it. 26116603153SAndreas Noever */ 26216603153SAndreas Noever list_del_init(&frame->list); 26316603153SAndreas Noever frame->callback(ring, frame, canceled); 26416603153SAndreas Noever } 26516603153SAndreas Noever } 26616603153SAndreas Noever 26716603153SAndreas Noever int __ring_enqueue(struct tb_ring *ring, struct ring_frame *frame) 26816603153SAndreas Noever { 26916603153SAndreas Noever int ret = 0; 27016603153SAndreas Noever mutex_lock(&ring->lock); 27116603153SAndreas Noever if (ring->running) { 27216603153SAndreas Noever list_add_tail(&frame->list, &ring->queue); 27316603153SAndreas Noever ring_write_descriptors(ring); 27416603153SAndreas Noever } else { 27516603153SAndreas Noever ret = -ESHUTDOWN; 27616603153SAndreas Noever } 27716603153SAndreas Noever mutex_unlock(&ring->lock); 27816603153SAndreas Noever return ret; 27916603153SAndreas Noever } 28016603153SAndreas Noever 281046bee1fSMika Westerberg static irqreturn_t ring_msix(int irq, void *data) 282046bee1fSMika Westerberg { 283046bee1fSMika Westerberg struct tb_ring *ring = data; 284046bee1fSMika Westerberg 285046bee1fSMika Westerberg schedule_work(&ring->work); 286046bee1fSMika Westerberg return IRQ_HANDLED; 287046bee1fSMika Westerberg } 288046bee1fSMika Westerberg 289046bee1fSMika Westerberg static int ring_request_msix(struct tb_ring *ring, bool no_suspend) 290046bee1fSMika Westerberg { 291046bee1fSMika Westerberg struct tb_nhi *nhi = ring->nhi; 292046bee1fSMika Westerberg unsigned long irqflags; 293046bee1fSMika Westerberg int ret; 294046bee1fSMika Westerberg 295046bee1fSMika Westerberg if (!nhi->pdev->msix_enabled) 296046bee1fSMika Westerberg return 0; 297046bee1fSMika Westerberg 298046bee1fSMika Westerberg ret = ida_simple_get(&nhi->msix_ida, 0, MSIX_MAX_VECS, GFP_KERNEL); 299046bee1fSMika Westerberg if (ret < 0) 300046bee1fSMika Westerberg return ret; 301046bee1fSMika Westerberg 302046bee1fSMika Westerberg ring->vector = ret; 303046bee1fSMika Westerberg 304046bee1fSMika Westerberg ring->irq = pci_irq_vector(ring->nhi->pdev, ring->vector); 305046bee1fSMika Westerberg if (ring->irq < 0) 306046bee1fSMika Westerberg return ring->irq; 307046bee1fSMika Westerberg 308046bee1fSMika Westerberg irqflags = no_suspend ? IRQF_NO_SUSPEND : 0; 309046bee1fSMika Westerberg return request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring); 310046bee1fSMika Westerberg } 311046bee1fSMika Westerberg 312046bee1fSMika Westerberg static void ring_release_msix(struct tb_ring *ring) 313046bee1fSMika Westerberg { 314046bee1fSMika Westerberg if (ring->irq <= 0) 315046bee1fSMika Westerberg return; 316046bee1fSMika Westerberg 317046bee1fSMika Westerberg free_irq(ring->irq, ring); 318046bee1fSMika Westerberg ida_simple_remove(&ring->nhi->msix_ida, ring->vector); 319046bee1fSMika Westerberg ring->vector = 0; 320046bee1fSMika Westerberg ring->irq = 0; 321046bee1fSMika Westerberg } 322046bee1fSMika Westerberg 32316603153SAndreas Noever static struct tb_ring *ring_alloc(struct tb_nhi *nhi, u32 hop, int size, 324046bee1fSMika Westerberg bool transmit, unsigned int flags) 32516603153SAndreas Noever { 32616603153SAndreas Noever struct tb_ring *ring = NULL; 32716603153SAndreas Noever dev_info(&nhi->pdev->dev, "allocating %s ring %d of size %d\n", 32816603153SAndreas Noever transmit ? "TX" : "RX", hop, size); 32916603153SAndreas Noever 33016603153SAndreas Noever mutex_lock(&nhi->lock); 33116603153SAndreas Noever if (hop >= nhi->hop_count) { 33216603153SAndreas Noever dev_WARN(&nhi->pdev->dev, "invalid hop: %d\n", hop); 33316603153SAndreas Noever goto err; 33416603153SAndreas Noever } 33516603153SAndreas Noever if (transmit && nhi->tx_rings[hop]) { 33616603153SAndreas Noever dev_WARN(&nhi->pdev->dev, "TX hop %d already allocated\n", hop); 33716603153SAndreas Noever goto err; 33816603153SAndreas Noever } else if (!transmit && nhi->rx_rings[hop]) { 33916603153SAndreas Noever dev_WARN(&nhi->pdev->dev, "RX hop %d already allocated\n", hop); 34016603153SAndreas Noever goto err; 34116603153SAndreas Noever } 34216603153SAndreas Noever ring = kzalloc(sizeof(*ring), GFP_KERNEL); 34316603153SAndreas Noever if (!ring) 34416603153SAndreas Noever goto err; 34516603153SAndreas Noever 34616603153SAndreas Noever mutex_init(&ring->lock); 34716603153SAndreas Noever INIT_LIST_HEAD(&ring->queue); 34816603153SAndreas Noever INIT_LIST_HEAD(&ring->in_flight); 34916603153SAndreas Noever INIT_WORK(&ring->work, ring_work); 35016603153SAndreas Noever 35116603153SAndreas Noever ring->nhi = nhi; 35216603153SAndreas Noever ring->hop = hop; 35316603153SAndreas Noever ring->is_tx = transmit; 35416603153SAndreas Noever ring->size = size; 355046bee1fSMika Westerberg ring->flags = flags; 35616603153SAndreas Noever ring->head = 0; 35716603153SAndreas Noever ring->tail = 0; 35816603153SAndreas Noever ring->running = false; 359046bee1fSMika Westerberg 360046bee1fSMika Westerberg if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND)) 361046bee1fSMika Westerberg goto err; 362046bee1fSMika Westerberg 36316603153SAndreas Noever ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev, 36416603153SAndreas Noever size * sizeof(*ring->descriptors), 36516603153SAndreas Noever &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO); 36616603153SAndreas Noever if (!ring->descriptors) 36716603153SAndreas Noever goto err; 36816603153SAndreas Noever 36916603153SAndreas Noever if (transmit) 37016603153SAndreas Noever nhi->tx_rings[hop] = ring; 37116603153SAndreas Noever else 37216603153SAndreas Noever nhi->rx_rings[hop] = ring; 37316603153SAndreas Noever mutex_unlock(&nhi->lock); 37416603153SAndreas Noever return ring; 37516603153SAndreas Noever 37616603153SAndreas Noever err: 37716603153SAndreas Noever if (ring) 37816603153SAndreas Noever mutex_destroy(&ring->lock); 37916603153SAndreas Noever kfree(ring); 38016603153SAndreas Noever mutex_unlock(&nhi->lock); 38116603153SAndreas Noever return NULL; 38216603153SAndreas Noever } 38316603153SAndreas Noever 384046bee1fSMika Westerberg struct tb_ring *ring_alloc_tx(struct tb_nhi *nhi, int hop, int size, 385046bee1fSMika Westerberg unsigned int flags) 38616603153SAndreas Noever { 387046bee1fSMika Westerberg return ring_alloc(nhi, hop, size, true, flags); 38816603153SAndreas Noever } 38916603153SAndreas Noever 390046bee1fSMika Westerberg struct tb_ring *ring_alloc_rx(struct tb_nhi *nhi, int hop, int size, 391046bee1fSMika Westerberg unsigned int flags) 39216603153SAndreas Noever { 393046bee1fSMika Westerberg return ring_alloc(nhi, hop, size, false, flags); 39416603153SAndreas Noever } 39516603153SAndreas Noever 39616603153SAndreas Noever /** 39716603153SAndreas Noever * ring_start() - enable a ring 39816603153SAndreas Noever * 39916603153SAndreas Noever * Must not be invoked in parallel with ring_stop(). 40016603153SAndreas Noever */ 40116603153SAndreas Noever void ring_start(struct tb_ring *ring) 40216603153SAndreas Noever { 40316603153SAndreas Noever mutex_lock(&ring->nhi->lock); 40416603153SAndreas Noever mutex_lock(&ring->lock); 405bdccf295SMika Westerberg if (ring->nhi->going_away) 406bdccf295SMika Westerberg goto err; 40716603153SAndreas Noever if (ring->running) { 40816603153SAndreas Noever dev_WARN(&ring->nhi->pdev->dev, "ring already started\n"); 40916603153SAndreas Noever goto err; 41016603153SAndreas Noever } 41116603153SAndreas Noever dev_info(&ring->nhi->pdev->dev, "starting %s %d\n", 41216603153SAndreas Noever RING_TYPE(ring), ring->hop); 41316603153SAndreas Noever 41416603153SAndreas Noever ring_iowrite64desc(ring, ring->descriptors_dma, 0); 41516603153SAndreas Noever if (ring->is_tx) { 41616603153SAndreas Noever ring_iowrite32desc(ring, ring->size, 12); 41716603153SAndreas Noever ring_iowrite32options(ring, 0, 4); /* time releated ? */ 41816603153SAndreas Noever ring_iowrite32options(ring, 41916603153SAndreas Noever RING_FLAG_ENABLE | RING_FLAG_RAW, 0); 42016603153SAndreas Noever } else { 42116603153SAndreas Noever ring_iowrite32desc(ring, 42216603153SAndreas Noever (TB_FRAME_SIZE << 16) | ring->size, 12); 42316603153SAndreas Noever ring_iowrite32options(ring, 0xffffffff, 4); /* SOF EOF mask */ 42416603153SAndreas Noever ring_iowrite32options(ring, 42516603153SAndreas Noever RING_FLAG_ENABLE | RING_FLAG_RAW, 0); 42616603153SAndreas Noever } 42716603153SAndreas Noever ring_interrupt_active(ring, true); 42816603153SAndreas Noever ring->running = true; 42916603153SAndreas Noever err: 43016603153SAndreas Noever mutex_unlock(&ring->lock); 43116603153SAndreas Noever mutex_unlock(&ring->nhi->lock); 43216603153SAndreas Noever } 43316603153SAndreas Noever 43416603153SAndreas Noever 43516603153SAndreas Noever /** 43616603153SAndreas Noever * ring_stop() - shutdown a ring 43716603153SAndreas Noever * 43816603153SAndreas Noever * Must not be invoked from a callback. 43916603153SAndreas Noever * 44016603153SAndreas Noever * This method will disable the ring. Further calls to ring_tx/ring_rx will 44116603153SAndreas Noever * return -ESHUTDOWN until ring_stop has been called. 44216603153SAndreas Noever * 44316603153SAndreas Noever * All enqueued frames will be canceled and their callbacks will be executed 44416603153SAndreas Noever * with frame->canceled set to true (on the callback thread). This method 44516603153SAndreas Noever * returns only after all callback invocations have finished. 44616603153SAndreas Noever */ 44716603153SAndreas Noever void ring_stop(struct tb_ring *ring) 44816603153SAndreas Noever { 44916603153SAndreas Noever mutex_lock(&ring->nhi->lock); 45016603153SAndreas Noever mutex_lock(&ring->lock); 45116603153SAndreas Noever dev_info(&ring->nhi->pdev->dev, "stopping %s %d\n", 45216603153SAndreas Noever RING_TYPE(ring), ring->hop); 453bdccf295SMika Westerberg if (ring->nhi->going_away) 454bdccf295SMika Westerberg goto err; 45516603153SAndreas Noever if (!ring->running) { 45616603153SAndreas Noever dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n", 45716603153SAndreas Noever RING_TYPE(ring), ring->hop); 45816603153SAndreas Noever goto err; 45916603153SAndreas Noever } 46016603153SAndreas Noever ring_interrupt_active(ring, false); 46116603153SAndreas Noever 46216603153SAndreas Noever ring_iowrite32options(ring, 0, 0); 46316603153SAndreas Noever ring_iowrite64desc(ring, 0, 0); 46416603153SAndreas Noever ring_iowrite16desc(ring, 0, ring->is_tx ? 10 : 8); 46516603153SAndreas Noever ring_iowrite32desc(ring, 0, 12); 46616603153SAndreas Noever ring->head = 0; 46716603153SAndreas Noever ring->tail = 0; 46816603153SAndreas Noever ring->running = false; 46916603153SAndreas Noever 47016603153SAndreas Noever err: 47116603153SAndreas Noever mutex_unlock(&ring->lock); 47216603153SAndreas Noever mutex_unlock(&ring->nhi->lock); 47316603153SAndreas Noever 47416603153SAndreas Noever /* 47516603153SAndreas Noever * schedule ring->work to invoke callbacks on all remaining frames. 47616603153SAndreas Noever */ 47716603153SAndreas Noever schedule_work(&ring->work); 47816603153SAndreas Noever flush_work(&ring->work); 47916603153SAndreas Noever } 48016603153SAndreas Noever 48116603153SAndreas Noever /* 48216603153SAndreas Noever * ring_free() - free ring 48316603153SAndreas Noever * 48416603153SAndreas Noever * When this method returns all invocations of ring->callback will have 48516603153SAndreas Noever * finished. 48616603153SAndreas Noever * 48716603153SAndreas Noever * Ring must be stopped. 48816603153SAndreas Noever * 48916603153SAndreas Noever * Must NOT be called from ring_frame->callback! 49016603153SAndreas Noever */ 49116603153SAndreas Noever void ring_free(struct tb_ring *ring) 49216603153SAndreas Noever { 49316603153SAndreas Noever mutex_lock(&ring->nhi->lock); 49416603153SAndreas Noever /* 49516603153SAndreas Noever * Dissociate the ring from the NHI. This also ensures that 49616603153SAndreas Noever * nhi_interrupt_work cannot reschedule ring->work. 49716603153SAndreas Noever */ 49816603153SAndreas Noever if (ring->is_tx) 49916603153SAndreas Noever ring->nhi->tx_rings[ring->hop] = NULL; 50016603153SAndreas Noever else 50116603153SAndreas Noever ring->nhi->rx_rings[ring->hop] = NULL; 50216603153SAndreas Noever 50316603153SAndreas Noever if (ring->running) { 50416603153SAndreas Noever dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n", 50516603153SAndreas Noever RING_TYPE(ring), ring->hop); 50616603153SAndreas Noever } 50716603153SAndreas Noever 508046bee1fSMika Westerberg ring_release_msix(ring); 509046bee1fSMika Westerberg 51016603153SAndreas Noever dma_free_coherent(&ring->nhi->pdev->dev, 51116603153SAndreas Noever ring->size * sizeof(*ring->descriptors), 51216603153SAndreas Noever ring->descriptors, ring->descriptors_dma); 51316603153SAndreas Noever 514f19b72c6SSachin Kamat ring->descriptors = NULL; 51516603153SAndreas Noever ring->descriptors_dma = 0; 51616603153SAndreas Noever 51716603153SAndreas Noever 51816603153SAndreas Noever dev_info(&ring->nhi->pdev->dev, 51916603153SAndreas Noever "freeing %s %d\n", 52016603153SAndreas Noever RING_TYPE(ring), 52116603153SAndreas Noever ring->hop); 52216603153SAndreas Noever 52316603153SAndreas Noever mutex_unlock(&ring->nhi->lock); 52416603153SAndreas Noever /** 525046bee1fSMika Westerberg * ring->work can no longer be scheduled (it is scheduled only 526046bee1fSMika Westerberg * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it 527046bee1fSMika Westerberg * to finish before freeing the ring. 52816603153SAndreas Noever */ 52916603153SAndreas Noever flush_work(&ring->work); 53016603153SAndreas Noever mutex_destroy(&ring->lock); 53116603153SAndreas Noever kfree(ring); 53216603153SAndreas Noever } 53316603153SAndreas Noever 534cd446ee2SMika Westerberg /** 535cd446ee2SMika Westerberg * nhi_mailbox_cmd() - Send a command through NHI mailbox 536cd446ee2SMika Westerberg * @nhi: Pointer to the NHI structure 537cd446ee2SMika Westerberg * @cmd: Command to send 538cd446ee2SMika Westerberg * @data: Data to be send with the command 539cd446ee2SMika Westerberg * 540cd446ee2SMika Westerberg * Sends mailbox command to the firmware running on NHI. Returns %0 in 541cd446ee2SMika Westerberg * case of success and negative errno in case of failure. 542cd446ee2SMika Westerberg */ 543cd446ee2SMika Westerberg int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data) 544cd446ee2SMika Westerberg { 545cd446ee2SMika Westerberg ktime_t timeout; 546cd446ee2SMika Westerberg u32 val; 547cd446ee2SMika Westerberg 548cd446ee2SMika Westerberg iowrite32(data, nhi->iobase + REG_INMAIL_DATA); 549cd446ee2SMika Westerberg 550cd446ee2SMika Westerberg val = ioread32(nhi->iobase + REG_INMAIL_CMD); 551cd446ee2SMika Westerberg val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR); 552cd446ee2SMika Westerberg val |= REG_INMAIL_OP_REQUEST | cmd; 553cd446ee2SMika Westerberg iowrite32(val, nhi->iobase + REG_INMAIL_CMD); 554cd446ee2SMika Westerberg 555cd446ee2SMika Westerberg timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT); 556cd446ee2SMika Westerberg do { 557cd446ee2SMika Westerberg val = ioread32(nhi->iobase + REG_INMAIL_CMD); 558cd446ee2SMika Westerberg if (!(val & REG_INMAIL_OP_REQUEST)) 559cd446ee2SMika Westerberg break; 560cd446ee2SMika Westerberg usleep_range(10, 20); 561cd446ee2SMika Westerberg } while (ktime_before(ktime_get(), timeout)); 562cd446ee2SMika Westerberg 563cd446ee2SMika Westerberg if (val & REG_INMAIL_OP_REQUEST) 564cd446ee2SMika Westerberg return -ETIMEDOUT; 565cd446ee2SMika Westerberg if (val & REG_INMAIL_ERROR) 566cd446ee2SMika Westerberg return -EIO; 567cd446ee2SMika Westerberg 568cd446ee2SMika Westerberg return 0; 569cd446ee2SMika Westerberg } 570cd446ee2SMika Westerberg 571cd446ee2SMika Westerberg /** 572cd446ee2SMika Westerberg * nhi_mailbox_mode() - Return current firmware operation mode 573cd446ee2SMika Westerberg * @nhi: Pointer to the NHI structure 574cd446ee2SMika Westerberg * 575cd446ee2SMika Westerberg * The function reads current firmware operation mode using NHI mailbox 576cd446ee2SMika Westerberg * registers and returns it to the caller. 577cd446ee2SMika Westerberg */ 578cd446ee2SMika Westerberg enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi) 579cd446ee2SMika Westerberg { 580cd446ee2SMika Westerberg u32 val; 581cd446ee2SMika Westerberg 582cd446ee2SMika Westerberg val = ioread32(nhi->iobase + REG_OUTMAIL_CMD); 583cd446ee2SMika Westerberg val &= REG_OUTMAIL_CMD_OPMODE_MASK; 584cd446ee2SMika Westerberg val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT; 585cd446ee2SMika Westerberg 586cd446ee2SMika Westerberg return (enum nhi_fw_mode)val; 587cd446ee2SMika Westerberg } 588cd446ee2SMika Westerberg 58916603153SAndreas Noever static void nhi_interrupt_work(struct work_struct *work) 59016603153SAndreas Noever { 59116603153SAndreas Noever struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work); 59216603153SAndreas Noever int value = 0; /* Suppress uninitialized usage warning. */ 59316603153SAndreas Noever int bit; 59416603153SAndreas Noever int hop = -1; 59516603153SAndreas Noever int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */ 59616603153SAndreas Noever struct tb_ring *ring; 59716603153SAndreas Noever 59816603153SAndreas Noever mutex_lock(&nhi->lock); 59916603153SAndreas Noever 60016603153SAndreas Noever /* 60116603153SAndreas Noever * Starting at REG_RING_NOTIFY_BASE there are three status bitfields 60216603153SAndreas Noever * (TX, RX, RX overflow). We iterate over the bits and read a new 60316603153SAndreas Noever * dwords as required. The registers are cleared on read. 60416603153SAndreas Noever */ 60516603153SAndreas Noever for (bit = 0; bit < 3 * nhi->hop_count; bit++) { 60616603153SAndreas Noever if (bit % 32 == 0) 60716603153SAndreas Noever value = ioread32(nhi->iobase 60816603153SAndreas Noever + REG_RING_NOTIFY_BASE 60916603153SAndreas Noever + 4 * (bit / 32)); 61016603153SAndreas Noever if (++hop == nhi->hop_count) { 61116603153SAndreas Noever hop = 0; 61216603153SAndreas Noever type++; 61316603153SAndreas Noever } 61416603153SAndreas Noever if ((value & (1 << (bit % 32))) == 0) 61516603153SAndreas Noever continue; 61616603153SAndreas Noever if (type == 2) { 61716603153SAndreas Noever dev_warn(&nhi->pdev->dev, 61816603153SAndreas Noever "RX overflow for ring %d\n", 61916603153SAndreas Noever hop); 62016603153SAndreas Noever continue; 62116603153SAndreas Noever } 62216603153SAndreas Noever if (type == 0) 62316603153SAndreas Noever ring = nhi->tx_rings[hop]; 62416603153SAndreas Noever else 62516603153SAndreas Noever ring = nhi->rx_rings[hop]; 62616603153SAndreas Noever if (ring == NULL) { 62716603153SAndreas Noever dev_warn(&nhi->pdev->dev, 62816603153SAndreas Noever "got interrupt for inactive %s ring %d\n", 62916603153SAndreas Noever type ? "RX" : "TX", 63016603153SAndreas Noever hop); 63116603153SAndreas Noever continue; 63216603153SAndreas Noever } 63316603153SAndreas Noever /* we do not check ring->running, this is done in ring->work */ 63416603153SAndreas Noever schedule_work(&ring->work); 63516603153SAndreas Noever } 63616603153SAndreas Noever mutex_unlock(&nhi->lock); 63716603153SAndreas Noever } 63816603153SAndreas Noever 63916603153SAndreas Noever static irqreturn_t nhi_msi(int irq, void *data) 64016603153SAndreas Noever { 64116603153SAndreas Noever struct tb_nhi *nhi = data; 64216603153SAndreas Noever schedule_work(&nhi->interrupt_work); 64316603153SAndreas Noever return IRQ_HANDLED; 64416603153SAndreas Noever } 64516603153SAndreas Noever 64623dd5bb4SAndreas Noever static int nhi_suspend_noirq(struct device *dev) 64723dd5bb4SAndreas Noever { 64823dd5bb4SAndreas Noever struct pci_dev *pdev = to_pci_dev(dev); 64923dd5bb4SAndreas Noever struct tb *tb = pci_get_drvdata(pdev); 6509d3cce0bSMika Westerberg 6519d3cce0bSMika Westerberg return tb_domain_suspend_noirq(tb); 65223dd5bb4SAndreas Noever } 65323dd5bb4SAndreas Noever 65423dd5bb4SAndreas Noever static int nhi_resume_noirq(struct device *dev) 65523dd5bb4SAndreas Noever { 65623dd5bb4SAndreas Noever struct pci_dev *pdev = to_pci_dev(dev); 65723dd5bb4SAndreas Noever struct tb *tb = pci_get_drvdata(pdev); 6589d3cce0bSMika Westerberg 659bdccf295SMika Westerberg /* 660bdccf295SMika Westerberg * Check that the device is still there. It may be that the user 661bdccf295SMika Westerberg * unplugged last device which causes the host controller to go 662bdccf295SMika Westerberg * away on PCs. 663bdccf295SMika Westerberg */ 664bdccf295SMika Westerberg if (!pci_device_is_present(pdev)) 665bdccf295SMika Westerberg tb->nhi->going_away = true; 666bdccf295SMika Westerberg 6679d3cce0bSMika Westerberg return tb_domain_resume_noirq(tb); 66823dd5bb4SAndreas Noever } 66923dd5bb4SAndreas Noever 670*f67cf491SMika Westerberg static int nhi_suspend(struct device *dev) 671*f67cf491SMika Westerberg { 672*f67cf491SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev); 673*f67cf491SMika Westerberg struct tb *tb = pci_get_drvdata(pdev); 674*f67cf491SMika Westerberg 675*f67cf491SMika Westerberg return tb_domain_suspend(tb); 676*f67cf491SMika Westerberg } 677*f67cf491SMika Westerberg 678*f67cf491SMika Westerberg static void nhi_complete(struct device *dev) 679*f67cf491SMika Westerberg { 680*f67cf491SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev); 681*f67cf491SMika Westerberg struct tb *tb = pci_get_drvdata(pdev); 682*f67cf491SMika Westerberg 683*f67cf491SMika Westerberg tb_domain_complete(tb); 684*f67cf491SMika Westerberg } 685*f67cf491SMika Westerberg 68616603153SAndreas Noever static void nhi_shutdown(struct tb_nhi *nhi) 68716603153SAndreas Noever { 68816603153SAndreas Noever int i; 68916603153SAndreas Noever dev_info(&nhi->pdev->dev, "shutdown\n"); 69016603153SAndreas Noever 69116603153SAndreas Noever for (i = 0; i < nhi->hop_count; i++) { 69216603153SAndreas Noever if (nhi->tx_rings[i]) 69316603153SAndreas Noever dev_WARN(&nhi->pdev->dev, 69416603153SAndreas Noever "TX ring %d is still active\n", i); 69516603153SAndreas Noever if (nhi->rx_rings[i]) 69616603153SAndreas Noever dev_WARN(&nhi->pdev->dev, 69716603153SAndreas Noever "RX ring %d is still active\n", i); 69816603153SAndreas Noever } 69916603153SAndreas Noever nhi_disable_interrupts(nhi); 70016603153SAndreas Noever /* 70116603153SAndreas Noever * We have to release the irq before calling flush_work. Otherwise an 70216603153SAndreas Noever * already executing IRQ handler could call schedule_work again. 70316603153SAndreas Noever */ 704046bee1fSMika Westerberg if (!nhi->pdev->msix_enabled) { 70516603153SAndreas Noever devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi); 70616603153SAndreas Noever flush_work(&nhi->interrupt_work); 707046bee1fSMika Westerberg } 70816603153SAndreas Noever mutex_destroy(&nhi->lock); 709046bee1fSMika Westerberg ida_destroy(&nhi->msix_ida); 710046bee1fSMika Westerberg } 711046bee1fSMika Westerberg 712046bee1fSMika Westerberg static int nhi_init_msi(struct tb_nhi *nhi) 713046bee1fSMika Westerberg { 714046bee1fSMika Westerberg struct pci_dev *pdev = nhi->pdev; 715046bee1fSMika Westerberg int res, irq, nvec; 716046bee1fSMika Westerberg 717046bee1fSMika Westerberg /* In case someone left them on. */ 718046bee1fSMika Westerberg nhi_disable_interrupts(nhi); 719046bee1fSMika Westerberg 720046bee1fSMika Westerberg ida_init(&nhi->msix_ida); 721046bee1fSMika Westerberg 722046bee1fSMika Westerberg /* 723046bee1fSMika Westerberg * The NHI has 16 MSI-X vectors or a single MSI. We first try to 724046bee1fSMika Westerberg * get all MSI-X vectors and if we succeed, each ring will have 725046bee1fSMika Westerberg * one MSI-X. If for some reason that does not work out, we 726046bee1fSMika Westerberg * fallback to a single MSI. 727046bee1fSMika Westerberg */ 728046bee1fSMika Westerberg nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS, 729046bee1fSMika Westerberg PCI_IRQ_MSIX); 730046bee1fSMika Westerberg if (nvec < 0) { 731046bee1fSMika Westerberg nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI); 732046bee1fSMika Westerberg if (nvec < 0) 733046bee1fSMika Westerberg return nvec; 734046bee1fSMika Westerberg 735046bee1fSMika Westerberg INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work); 736046bee1fSMika Westerberg 737046bee1fSMika Westerberg irq = pci_irq_vector(nhi->pdev, 0); 738046bee1fSMika Westerberg if (irq < 0) 739046bee1fSMika Westerberg return irq; 740046bee1fSMika Westerberg 741046bee1fSMika Westerberg res = devm_request_irq(&pdev->dev, irq, nhi_msi, 742046bee1fSMika Westerberg IRQF_NO_SUSPEND, "thunderbolt", nhi); 743046bee1fSMika Westerberg if (res) { 744046bee1fSMika Westerberg dev_err(&pdev->dev, "request_irq failed, aborting\n"); 745046bee1fSMika Westerberg return res; 746046bee1fSMika Westerberg } 747046bee1fSMika Westerberg } 748046bee1fSMika Westerberg 749046bee1fSMika Westerberg return 0; 75016603153SAndreas Noever } 75116603153SAndreas Noever 75216603153SAndreas Noever static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id) 75316603153SAndreas Noever { 75416603153SAndreas Noever struct tb_nhi *nhi; 755d6cc51cdSAndreas Noever struct tb *tb; 75616603153SAndreas Noever int res; 75716603153SAndreas Noever 75816603153SAndreas Noever res = pcim_enable_device(pdev); 75916603153SAndreas Noever if (res) { 76016603153SAndreas Noever dev_err(&pdev->dev, "cannot enable PCI device, aborting\n"); 76116603153SAndreas Noever return res; 76216603153SAndreas Noever } 76316603153SAndreas Noever 76416603153SAndreas Noever res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt"); 76516603153SAndreas Noever if (res) { 76616603153SAndreas Noever dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n"); 76716603153SAndreas Noever return res; 76816603153SAndreas Noever } 76916603153SAndreas Noever 77016603153SAndreas Noever nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL); 77116603153SAndreas Noever if (!nhi) 77216603153SAndreas Noever return -ENOMEM; 77316603153SAndreas Noever 77416603153SAndreas Noever nhi->pdev = pdev; 77516603153SAndreas Noever /* cannot fail - table is allocated bin pcim_iomap_regions */ 77616603153SAndreas Noever nhi->iobase = pcim_iomap_table(pdev)[0]; 77716603153SAndreas Noever nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff; 77819bf4d4fSLukas Wunner if (nhi->hop_count != 12 && nhi->hop_count != 32) 77916603153SAndreas Noever dev_warn(&pdev->dev, "unexpected hop count: %d\n", 78016603153SAndreas Noever nhi->hop_count); 78116603153SAndreas Noever 7822a211f32SHimangi Saraogi nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count, 7832a211f32SHimangi Saraogi sizeof(*nhi->tx_rings), GFP_KERNEL); 7842a211f32SHimangi Saraogi nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count, 7852a211f32SHimangi Saraogi sizeof(*nhi->rx_rings), GFP_KERNEL); 78616603153SAndreas Noever if (!nhi->tx_rings || !nhi->rx_rings) 78716603153SAndreas Noever return -ENOMEM; 78816603153SAndreas Noever 789046bee1fSMika Westerberg res = nhi_init_msi(nhi); 79016603153SAndreas Noever if (res) { 791046bee1fSMika Westerberg dev_err(&pdev->dev, "cannot enable MSI, aborting\n"); 79216603153SAndreas Noever return res; 79316603153SAndreas Noever } 79416603153SAndreas Noever 79516603153SAndreas Noever mutex_init(&nhi->lock); 79616603153SAndreas Noever 79716603153SAndreas Noever pci_set_master(pdev); 79816603153SAndreas Noever 79916603153SAndreas Noever /* magic value - clock related? */ 80016603153SAndreas Noever iowrite32(3906250 / 10000, nhi->iobase + 0x38c00); 80116603153SAndreas Noever 802*f67cf491SMika Westerberg tb = icm_probe(nhi); 8039d3cce0bSMika Westerberg if (!tb) 804*f67cf491SMika Westerberg tb = tb_probe(nhi); 805*f67cf491SMika Westerberg if (!tb) { 806*f67cf491SMika Westerberg dev_err(&nhi->pdev->dev, 807*f67cf491SMika Westerberg "failed to determine connection manager, aborting\n"); 8089d3cce0bSMika Westerberg return -ENODEV; 809*f67cf491SMika Westerberg } 810*f67cf491SMika Westerberg 811*f67cf491SMika Westerberg dev_info(&nhi->pdev->dev, "NHI initialized, starting thunderbolt\n"); 8129d3cce0bSMika Westerberg 8139d3cce0bSMika Westerberg res = tb_domain_add(tb); 8149d3cce0bSMika Westerberg if (res) { 815d6cc51cdSAndreas Noever /* 816d6cc51cdSAndreas Noever * At this point the RX/TX rings might already have been 817d6cc51cdSAndreas Noever * activated. Do a proper shutdown. 818d6cc51cdSAndreas Noever */ 8199d3cce0bSMika Westerberg tb_domain_put(tb); 820d6cc51cdSAndreas Noever nhi_shutdown(nhi); 821d6cc51cdSAndreas Noever return -EIO; 822d6cc51cdSAndreas Noever } 823d6cc51cdSAndreas Noever pci_set_drvdata(pdev, tb); 82416603153SAndreas Noever 82516603153SAndreas Noever return 0; 82616603153SAndreas Noever } 82716603153SAndreas Noever 82816603153SAndreas Noever static void nhi_remove(struct pci_dev *pdev) 82916603153SAndreas Noever { 830d6cc51cdSAndreas Noever struct tb *tb = pci_get_drvdata(pdev); 831d6cc51cdSAndreas Noever struct tb_nhi *nhi = tb->nhi; 8329d3cce0bSMika Westerberg 8339d3cce0bSMika Westerberg tb_domain_remove(tb); 83416603153SAndreas Noever nhi_shutdown(nhi); 83516603153SAndreas Noever } 83616603153SAndreas Noever 83723dd5bb4SAndreas Noever /* 83823dd5bb4SAndreas Noever * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable 83923dd5bb4SAndreas Noever * the tunnels asap. A corresponding pci quirk blocks the downstream bridges 84023dd5bb4SAndreas Noever * resume_noirq until we are done. 84123dd5bb4SAndreas Noever */ 84223dd5bb4SAndreas Noever static const struct dev_pm_ops nhi_pm_ops = { 84323dd5bb4SAndreas Noever .suspend_noirq = nhi_suspend_noirq, 84423dd5bb4SAndreas Noever .resume_noirq = nhi_resume_noirq, 84523dd5bb4SAndreas Noever .freeze_noirq = nhi_suspend_noirq, /* 84623dd5bb4SAndreas Noever * we just disable hotplug, the 84723dd5bb4SAndreas Noever * pci-tunnels stay alive. 84823dd5bb4SAndreas Noever */ 84923dd5bb4SAndreas Noever .restore_noirq = nhi_resume_noirq, 850*f67cf491SMika Westerberg .suspend = nhi_suspend, 851*f67cf491SMika Westerberg .freeze = nhi_suspend, 852*f67cf491SMika Westerberg .poweroff = nhi_suspend, 853*f67cf491SMika Westerberg .complete = nhi_complete, 85423dd5bb4SAndreas Noever }; 85523dd5bb4SAndreas Noever 856620863f7SSachin Kamat static struct pci_device_id nhi_ids[] = { 85716603153SAndreas Noever /* 85816603153SAndreas Noever * We have to specify class, the TB bridges use the same device and 8591d111406SLukas Wunner * vendor (sub)id on gen 1 and gen 2 controllers. 86016603153SAndreas Noever */ 86116603153SAndreas Noever { 86216603153SAndreas Noever .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0, 8631d111406SLukas Wunner .vendor = PCI_VENDOR_ID_INTEL, 86419bf4d4fSLukas Wunner .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE, 86519bf4d4fSLukas Wunner .subvendor = 0x2222, .subdevice = 0x1111, 86619bf4d4fSLukas Wunner }, 86719bf4d4fSLukas Wunner { 86819bf4d4fSLukas Wunner .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0, 86919bf4d4fSLukas Wunner .vendor = PCI_VENDOR_ID_INTEL, 8701d111406SLukas Wunner .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C, 87116603153SAndreas Noever .subvendor = 0x2222, .subdevice = 0x1111, 87216603153SAndreas Noever }, 87316603153SAndreas Noever { 87416603153SAndreas Noever .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0, 8751d111406SLukas Wunner .vendor = PCI_VENDOR_ID_INTEL, 87682a6a81cSXavier Gnata .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI, 87782a6a81cSXavier Gnata .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID, 87882a6a81cSXavier Gnata }, 87982a6a81cSXavier Gnata { 88082a6a81cSXavier Gnata .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0, 88182a6a81cSXavier Gnata .vendor = PCI_VENDOR_ID_INTEL, 8821d111406SLukas Wunner .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI, 883a42fb351SKnuth Posern .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID, 88416603153SAndreas Noever }, 8855e2781bcSMika Westerberg 8865e2781bcSMika Westerberg /* Thunderbolt 3 */ 8875e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) }, 8885e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) }, 8895e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) }, 8905e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) }, 8915e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) }, 8925e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) }, 8935e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) }, 8945e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) }, 8955e2781bcSMika Westerberg 89616603153SAndreas Noever { 0,} 89716603153SAndreas Noever }; 89816603153SAndreas Noever 89916603153SAndreas Noever MODULE_DEVICE_TABLE(pci, nhi_ids); 90016603153SAndreas Noever MODULE_LICENSE("GPL"); 90116603153SAndreas Noever 90216603153SAndreas Noever static struct pci_driver nhi_driver = { 90316603153SAndreas Noever .name = "thunderbolt", 90416603153SAndreas Noever .id_table = nhi_ids, 90516603153SAndreas Noever .probe = nhi_probe, 90616603153SAndreas Noever .remove = nhi_remove, 90723dd5bb4SAndreas Noever .driver.pm = &nhi_pm_ops, 90816603153SAndreas Noever }; 90916603153SAndreas Noever 91016603153SAndreas Noever static int __init nhi_init(void) 91116603153SAndreas Noever { 9129d3cce0bSMika Westerberg int ret; 9139d3cce0bSMika Westerberg 9149d3cce0bSMika Westerberg ret = tb_domain_init(); 9159d3cce0bSMika Westerberg if (ret) 9169d3cce0bSMika Westerberg return ret; 9179d3cce0bSMika Westerberg ret = pci_register_driver(&nhi_driver); 9189d3cce0bSMika Westerberg if (ret) 9199d3cce0bSMika Westerberg tb_domain_exit(); 9209d3cce0bSMika Westerberg return ret; 92116603153SAndreas Noever } 92216603153SAndreas Noever 92316603153SAndreas Noever static void __exit nhi_unload(void) 92416603153SAndreas Noever { 92516603153SAndreas Noever pci_unregister_driver(&nhi_driver); 9269d3cce0bSMika Westerberg tb_domain_exit(); 92716603153SAndreas Noever } 92816603153SAndreas Noever 92916603153SAndreas Noever module_init(nhi_init); 93016603153SAndreas Noever module_exit(nhi_unload); 931