109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 216603153SAndreas Noever /* 315c6784cSMika Westerberg * Thunderbolt driver - NHI driver 416603153SAndreas Noever * 516603153SAndreas Noever * The NHI (native host interface) is the pci device that allows us to send and 616603153SAndreas Noever * receive frames from the thunderbolt bus. 716603153SAndreas Noever * 816603153SAndreas Noever * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> 915c6784cSMika Westerberg * Copyright (C) 2018, Intel Corporation 1016603153SAndreas Noever */ 1116603153SAndreas Noever 1223dd5bb4SAndreas Noever #include <linux/pm_runtime.h> 1316603153SAndreas Noever #include <linux/slab.h> 1416603153SAndreas Noever #include <linux/errno.h> 1516603153SAndreas Noever #include <linux/pci.h> 1616603153SAndreas Noever #include <linux/interrupt.h> 1716603153SAndreas Noever #include <linux/module.h> 18cd446ee2SMika Westerberg #include <linux/delay.h> 193cdb9446SMika Westerberg #include <linux/property.h> 2016603153SAndreas Noever 2116603153SAndreas Noever #include "nhi.h" 2216603153SAndreas Noever #include "nhi_regs.h" 23d6cc51cdSAndreas Noever #include "tb.h" 2416603153SAndreas Noever 2516603153SAndreas Noever #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring") 2616603153SAndreas Noever 2753f13319SMika Westerberg #define RING_FIRST_USABLE_HOPID 1 289fb1e654SMika Westerberg 299fb1e654SMika Westerberg /* 30046bee1fSMika Westerberg * Minimal number of vectors when we use MSI-X. Two for control channel 31046bee1fSMika Westerberg * Rx/Tx and the rest four are for cross domain DMA paths. 32046bee1fSMika Westerberg */ 33046bee1fSMika Westerberg #define MSIX_MIN_VECS 6 34046bee1fSMika Westerberg #define MSIX_MAX_VECS 16 3516603153SAndreas Noever 36cd446ee2SMika Westerberg #define NHI_MAILBOX_TIMEOUT 500 /* ms */ 37cd446ee2SMika Westerberg 3816603153SAndreas Noever static int ring_interrupt_index(struct tb_ring *ring) 3916603153SAndreas Noever { 4016603153SAndreas Noever int bit = ring->hop; 4116603153SAndreas Noever if (!ring->is_tx) 4216603153SAndreas Noever bit += ring->nhi->hop_count; 4316603153SAndreas Noever return bit; 4416603153SAndreas Noever } 4516603153SAndreas Noever 46a7bfb27bSLee Jones /* 4716603153SAndreas Noever * ring_interrupt_active() - activate/deactivate interrupts for a single ring 4816603153SAndreas Noever * 4916603153SAndreas Noever * ring->nhi->lock must be held. 5016603153SAndreas Noever */ 5116603153SAndreas Noever static void ring_interrupt_active(struct tb_ring *ring, bool active) 5216603153SAndreas Noever { 5319bf4d4fSLukas Wunner int reg = REG_RING_INTERRUPT_BASE + 5419bf4d4fSLukas Wunner ring_interrupt_index(ring) / 32 * 4; 5516603153SAndreas Noever int bit = ring_interrupt_index(ring) & 31; 5616603153SAndreas Noever int mask = 1 << bit; 5716603153SAndreas Noever u32 old, new; 58046bee1fSMika Westerberg 59046bee1fSMika Westerberg if (ring->irq > 0) { 60046bee1fSMika Westerberg u32 step, shift, ivr, misc; 61046bee1fSMika Westerberg void __iomem *ivr_base; 62046bee1fSMika Westerberg int index; 63046bee1fSMika Westerberg 64046bee1fSMika Westerberg if (ring->is_tx) 65046bee1fSMika Westerberg index = ring->hop; 66046bee1fSMika Westerberg else 67046bee1fSMika Westerberg index = ring->hop + ring->nhi->hop_count; 68046bee1fSMika Westerberg 69046bee1fSMika Westerberg /* 70046bee1fSMika Westerberg * Ask the hardware to clear interrupt status bits automatically 71046bee1fSMika Westerberg * since we already know which interrupt was triggered. 72046bee1fSMika Westerberg */ 73046bee1fSMika Westerberg misc = ioread32(ring->nhi->iobase + REG_DMA_MISC); 74046bee1fSMika Westerberg if (!(misc & REG_DMA_MISC_INT_AUTO_CLEAR)) { 75046bee1fSMika Westerberg misc |= REG_DMA_MISC_INT_AUTO_CLEAR; 76046bee1fSMika Westerberg iowrite32(misc, ring->nhi->iobase + REG_DMA_MISC); 77046bee1fSMika Westerberg } 78046bee1fSMika Westerberg 79046bee1fSMika Westerberg ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE; 80046bee1fSMika Westerberg step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS; 81046bee1fSMika Westerberg shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS; 82046bee1fSMika Westerberg ivr = ioread32(ivr_base + step); 83046bee1fSMika Westerberg ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift); 84046bee1fSMika Westerberg if (active) 85046bee1fSMika Westerberg ivr |= ring->vector << shift; 86046bee1fSMika Westerberg iowrite32(ivr, ivr_base + step); 87046bee1fSMika Westerberg } 88046bee1fSMika Westerberg 8916603153SAndreas Noever old = ioread32(ring->nhi->iobase + reg); 9016603153SAndreas Noever if (active) 9116603153SAndreas Noever new = old | mask; 9216603153SAndreas Noever else 9316603153SAndreas Noever new = old & ~mask; 9416603153SAndreas Noever 95daa5140fSMika Westerberg dev_dbg(&ring->nhi->pdev->dev, 9616603153SAndreas Noever "%s interrupt at register %#x bit %d (%#x -> %#x)\n", 9716603153SAndreas Noever active ? "enabling" : "disabling", reg, bit, old, new); 9816603153SAndreas Noever 9916603153SAndreas Noever if (new == old) 10016603153SAndreas Noever dev_WARN(&ring->nhi->pdev->dev, 10116603153SAndreas Noever "interrupt for %s %d is already %s\n", 10216603153SAndreas Noever RING_TYPE(ring), ring->hop, 10316603153SAndreas Noever active ? "enabled" : "disabled"); 10416603153SAndreas Noever iowrite32(new, ring->nhi->iobase + reg); 10516603153SAndreas Noever } 10616603153SAndreas Noever 107a7bfb27bSLee Jones /* 10816603153SAndreas Noever * nhi_disable_interrupts() - disable interrupts for all rings 10916603153SAndreas Noever * 11016603153SAndreas Noever * Use only during init and shutdown. 11116603153SAndreas Noever */ 11216603153SAndreas Noever static void nhi_disable_interrupts(struct tb_nhi *nhi) 11316603153SAndreas Noever { 11416603153SAndreas Noever int i = 0; 11516603153SAndreas Noever /* disable interrupts */ 11616603153SAndreas Noever for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++) 11716603153SAndreas Noever iowrite32(0, nhi->iobase + REG_RING_INTERRUPT_BASE + 4 * i); 11816603153SAndreas Noever 11916603153SAndreas Noever /* clear interrupt status bits */ 12016603153SAndreas Noever for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++) 12116603153SAndreas Noever ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + 4 * i); 12216603153SAndreas Noever } 12316603153SAndreas Noever 12416603153SAndreas Noever /* ring helper methods */ 12516603153SAndreas Noever 12616603153SAndreas Noever static void __iomem *ring_desc_base(struct tb_ring *ring) 12716603153SAndreas Noever { 12816603153SAndreas Noever void __iomem *io = ring->nhi->iobase; 12916603153SAndreas Noever io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE; 13016603153SAndreas Noever io += ring->hop * 16; 13116603153SAndreas Noever return io; 13216603153SAndreas Noever } 13316603153SAndreas Noever 13416603153SAndreas Noever static void __iomem *ring_options_base(struct tb_ring *ring) 13516603153SAndreas Noever { 13616603153SAndreas Noever void __iomem *io = ring->nhi->iobase; 13716603153SAndreas Noever io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE; 13816603153SAndreas Noever io += ring->hop * 32; 13916603153SAndreas Noever return io; 14016603153SAndreas Noever } 14116603153SAndreas Noever 14294379521SMika Westerberg static void ring_iowrite_cons(struct tb_ring *ring, u16 cons) 14316603153SAndreas Noever { 14494379521SMika Westerberg /* 14594379521SMika Westerberg * The other 16-bits in the register is read-only and writes to it 14694379521SMika Westerberg * are ignored by the hardware so we can save one ioread32() by 14794379521SMika Westerberg * filling the read-only bits with zeroes. 14894379521SMika Westerberg */ 14994379521SMika Westerberg iowrite32(cons, ring_desc_base(ring) + 8); 15094379521SMika Westerberg } 15194379521SMika Westerberg 15294379521SMika Westerberg static void ring_iowrite_prod(struct tb_ring *ring, u16 prod) 15394379521SMika Westerberg { 15494379521SMika Westerberg /* See ring_iowrite_cons() above for explanation */ 15594379521SMika Westerberg iowrite32(prod << 16, ring_desc_base(ring) + 8); 15616603153SAndreas Noever } 15716603153SAndreas Noever 15816603153SAndreas Noever static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset) 15916603153SAndreas Noever { 16016603153SAndreas Noever iowrite32(value, ring_desc_base(ring) + offset); 16116603153SAndreas Noever } 16216603153SAndreas Noever 16316603153SAndreas Noever static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset) 16416603153SAndreas Noever { 16516603153SAndreas Noever iowrite32(value, ring_desc_base(ring) + offset); 16616603153SAndreas Noever iowrite32(value >> 32, ring_desc_base(ring) + offset + 4); 16716603153SAndreas Noever } 16816603153SAndreas Noever 16916603153SAndreas Noever static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset) 17016603153SAndreas Noever { 17116603153SAndreas Noever iowrite32(value, ring_options_base(ring) + offset); 17216603153SAndreas Noever } 17316603153SAndreas Noever 17416603153SAndreas Noever static bool ring_full(struct tb_ring *ring) 17516603153SAndreas Noever { 17616603153SAndreas Noever return ((ring->head + 1) % ring->size) == ring->tail; 17716603153SAndreas Noever } 17816603153SAndreas Noever 17916603153SAndreas Noever static bool ring_empty(struct tb_ring *ring) 18016603153SAndreas Noever { 18116603153SAndreas Noever return ring->head == ring->tail; 18216603153SAndreas Noever } 18316603153SAndreas Noever 184a7bfb27bSLee Jones /* 18516603153SAndreas Noever * ring_write_descriptors() - post frames from ring->queue to the controller 18616603153SAndreas Noever * 18716603153SAndreas Noever * ring->lock is held. 18816603153SAndreas Noever */ 18916603153SAndreas Noever static void ring_write_descriptors(struct tb_ring *ring) 19016603153SAndreas Noever { 19116603153SAndreas Noever struct ring_frame *frame, *n; 19216603153SAndreas Noever struct ring_desc *descriptor; 19316603153SAndreas Noever list_for_each_entry_safe(frame, n, &ring->queue, list) { 19416603153SAndreas Noever if (ring_full(ring)) 19516603153SAndreas Noever break; 19616603153SAndreas Noever list_move_tail(&frame->list, &ring->in_flight); 19716603153SAndreas Noever descriptor = &ring->descriptors[ring->head]; 19816603153SAndreas Noever descriptor->phys = frame->buffer_phy; 19916603153SAndreas Noever descriptor->time = 0; 20016603153SAndreas Noever descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT; 20116603153SAndreas Noever if (ring->is_tx) { 20216603153SAndreas Noever descriptor->length = frame->size; 20316603153SAndreas Noever descriptor->eof = frame->eof; 20416603153SAndreas Noever descriptor->sof = frame->sof; 20516603153SAndreas Noever } 20616603153SAndreas Noever ring->head = (ring->head + 1) % ring->size; 20794379521SMika Westerberg if (ring->is_tx) 20894379521SMika Westerberg ring_iowrite_prod(ring, ring->head); 20994379521SMika Westerberg else 21094379521SMika Westerberg ring_iowrite_cons(ring, ring->head); 21116603153SAndreas Noever } 21216603153SAndreas Noever } 21316603153SAndreas Noever 214a7bfb27bSLee Jones /* 21516603153SAndreas Noever * ring_work() - progress completed frames 21616603153SAndreas Noever * 21716603153SAndreas Noever * If the ring is shutting down then all frames are marked as canceled and 21816603153SAndreas Noever * their callbacks are invoked. 21916603153SAndreas Noever * 22016603153SAndreas Noever * Otherwise we collect all completed frame from the ring buffer, write new 22116603153SAndreas Noever * frame to the ring buffer and invoke the callbacks for the completed frames. 22216603153SAndreas Noever */ 22316603153SAndreas Noever static void ring_work(struct work_struct *work) 22416603153SAndreas Noever { 22516603153SAndreas Noever struct tb_ring *ring = container_of(work, typeof(*ring), work); 22616603153SAndreas Noever struct ring_frame *frame; 22716603153SAndreas Noever bool canceled = false; 22822b7de10SMika Westerberg unsigned long flags; 22916603153SAndreas Noever LIST_HEAD(done); 23022b7de10SMika Westerberg 23122b7de10SMika Westerberg spin_lock_irqsave(&ring->lock, flags); 23216603153SAndreas Noever 23316603153SAndreas Noever if (!ring->running) { 23416603153SAndreas Noever /* Move all frames to done and mark them as canceled. */ 23516603153SAndreas Noever list_splice_tail_init(&ring->in_flight, &done); 23616603153SAndreas Noever list_splice_tail_init(&ring->queue, &done); 23716603153SAndreas Noever canceled = true; 23816603153SAndreas Noever goto invoke_callback; 23916603153SAndreas Noever } 24016603153SAndreas Noever 24116603153SAndreas Noever while (!ring_empty(ring)) { 24216603153SAndreas Noever if (!(ring->descriptors[ring->tail].flags 24316603153SAndreas Noever & RING_DESC_COMPLETED)) 24416603153SAndreas Noever break; 24516603153SAndreas Noever frame = list_first_entry(&ring->in_flight, typeof(*frame), 24616603153SAndreas Noever list); 24716603153SAndreas Noever list_move_tail(&frame->list, &done); 24816603153SAndreas Noever if (!ring->is_tx) { 24916603153SAndreas Noever frame->size = ring->descriptors[ring->tail].length; 25016603153SAndreas Noever frame->eof = ring->descriptors[ring->tail].eof; 25116603153SAndreas Noever frame->sof = ring->descriptors[ring->tail].sof; 25216603153SAndreas Noever frame->flags = ring->descriptors[ring->tail].flags; 25316603153SAndreas Noever } 25416603153SAndreas Noever ring->tail = (ring->tail + 1) % ring->size; 25516603153SAndreas Noever } 25616603153SAndreas Noever ring_write_descriptors(ring); 25716603153SAndreas Noever 25816603153SAndreas Noever invoke_callback: 25922b7de10SMika Westerberg /* allow callbacks to schedule new work */ 26022b7de10SMika Westerberg spin_unlock_irqrestore(&ring->lock, flags); 26116603153SAndreas Noever while (!list_empty(&done)) { 26216603153SAndreas Noever frame = list_first_entry(&done, typeof(*frame), list); 26316603153SAndreas Noever /* 26416603153SAndreas Noever * The callback may reenqueue or delete frame. 26516603153SAndreas Noever * Do not hold on to it. 26616603153SAndreas Noever */ 26716603153SAndreas Noever list_del_init(&frame->list); 2684ffe722eSMika Westerberg if (frame->callback) 26916603153SAndreas Noever frame->callback(ring, frame, canceled); 27016603153SAndreas Noever } 27116603153SAndreas Noever } 27216603153SAndreas Noever 2733b3d9f4dSMika Westerberg int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame) 27416603153SAndreas Noever { 27522b7de10SMika Westerberg unsigned long flags; 27616603153SAndreas Noever int ret = 0; 27722b7de10SMika Westerberg 27822b7de10SMika Westerberg spin_lock_irqsave(&ring->lock, flags); 27916603153SAndreas Noever if (ring->running) { 28016603153SAndreas Noever list_add_tail(&frame->list, &ring->queue); 28116603153SAndreas Noever ring_write_descriptors(ring); 28216603153SAndreas Noever } else { 28316603153SAndreas Noever ret = -ESHUTDOWN; 28416603153SAndreas Noever } 28522b7de10SMika Westerberg spin_unlock_irqrestore(&ring->lock, flags); 28616603153SAndreas Noever return ret; 28716603153SAndreas Noever } 2883b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(__tb_ring_enqueue); 28916603153SAndreas Noever 2904ffe722eSMika Westerberg /** 2914ffe722eSMika Westerberg * tb_ring_poll() - Poll one completed frame from the ring 2924ffe722eSMika Westerberg * @ring: Ring to poll 2934ffe722eSMika Westerberg * 2944ffe722eSMika Westerberg * This function can be called when @start_poll callback of the @ring 2954ffe722eSMika Westerberg * has been called. It will read one completed frame from the ring and 2964ffe722eSMika Westerberg * return it to the caller. Returns %NULL if there is no more completed 2974ffe722eSMika Westerberg * frames. 2984ffe722eSMika Westerberg */ 2994ffe722eSMika Westerberg struct ring_frame *tb_ring_poll(struct tb_ring *ring) 3004ffe722eSMika Westerberg { 3014ffe722eSMika Westerberg struct ring_frame *frame = NULL; 3024ffe722eSMika Westerberg unsigned long flags; 3034ffe722eSMika Westerberg 3044ffe722eSMika Westerberg spin_lock_irqsave(&ring->lock, flags); 3054ffe722eSMika Westerberg if (!ring->running) 3064ffe722eSMika Westerberg goto unlock; 3074ffe722eSMika Westerberg if (ring_empty(ring)) 3084ffe722eSMika Westerberg goto unlock; 3094ffe722eSMika Westerberg 3104ffe722eSMika Westerberg if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) { 3114ffe722eSMika Westerberg frame = list_first_entry(&ring->in_flight, typeof(*frame), 3124ffe722eSMika Westerberg list); 3134ffe722eSMika Westerberg list_del_init(&frame->list); 3144ffe722eSMika Westerberg 3154ffe722eSMika Westerberg if (!ring->is_tx) { 3164ffe722eSMika Westerberg frame->size = ring->descriptors[ring->tail].length; 3174ffe722eSMika Westerberg frame->eof = ring->descriptors[ring->tail].eof; 3184ffe722eSMika Westerberg frame->sof = ring->descriptors[ring->tail].sof; 3194ffe722eSMika Westerberg frame->flags = ring->descriptors[ring->tail].flags; 3204ffe722eSMika Westerberg } 3214ffe722eSMika Westerberg 3224ffe722eSMika Westerberg ring->tail = (ring->tail + 1) % ring->size; 3234ffe722eSMika Westerberg } 3244ffe722eSMika Westerberg 3254ffe722eSMika Westerberg unlock: 3264ffe722eSMika Westerberg spin_unlock_irqrestore(&ring->lock, flags); 3274ffe722eSMika Westerberg return frame; 3284ffe722eSMika Westerberg } 3294ffe722eSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_poll); 3304ffe722eSMika Westerberg 3314ffe722eSMika Westerberg static void __ring_interrupt_mask(struct tb_ring *ring, bool mask) 3324ffe722eSMika Westerberg { 3334ffe722eSMika Westerberg int idx = ring_interrupt_index(ring); 3344ffe722eSMika Westerberg int reg = REG_RING_INTERRUPT_BASE + idx / 32 * 4; 3354ffe722eSMika Westerberg int bit = idx % 32; 3364ffe722eSMika Westerberg u32 val; 3374ffe722eSMika Westerberg 3384ffe722eSMika Westerberg val = ioread32(ring->nhi->iobase + reg); 3394ffe722eSMika Westerberg if (mask) 3404ffe722eSMika Westerberg val &= ~BIT(bit); 3414ffe722eSMika Westerberg else 3424ffe722eSMika Westerberg val |= BIT(bit); 3434ffe722eSMika Westerberg iowrite32(val, ring->nhi->iobase + reg); 3444ffe722eSMika Westerberg } 3454ffe722eSMika Westerberg 3464ffe722eSMika Westerberg /* Both @nhi->lock and @ring->lock should be held */ 3474ffe722eSMika Westerberg static void __ring_interrupt(struct tb_ring *ring) 3484ffe722eSMika Westerberg { 3494ffe722eSMika Westerberg if (!ring->running) 3504ffe722eSMika Westerberg return; 3514ffe722eSMika Westerberg 3524ffe722eSMika Westerberg if (ring->start_poll) { 35374657181SMika Westerberg __ring_interrupt_mask(ring, true); 3544ffe722eSMika Westerberg ring->start_poll(ring->poll_data); 3554ffe722eSMika Westerberg } else { 3564ffe722eSMika Westerberg schedule_work(&ring->work); 3574ffe722eSMika Westerberg } 3584ffe722eSMika Westerberg } 3594ffe722eSMika Westerberg 3604ffe722eSMika Westerberg /** 3614ffe722eSMika Westerberg * tb_ring_poll_complete() - Re-start interrupt for the ring 3624ffe722eSMika Westerberg * @ring: Ring to re-start the interrupt 3634ffe722eSMika Westerberg * 3644ffe722eSMika Westerberg * This will re-start (unmask) the ring interrupt once the user is done 3654ffe722eSMika Westerberg * with polling. 3664ffe722eSMika Westerberg */ 3674ffe722eSMika Westerberg void tb_ring_poll_complete(struct tb_ring *ring) 3684ffe722eSMika Westerberg { 3694ffe722eSMika Westerberg unsigned long flags; 3704ffe722eSMika Westerberg 3714ffe722eSMika Westerberg spin_lock_irqsave(&ring->nhi->lock, flags); 3724ffe722eSMika Westerberg spin_lock(&ring->lock); 3734ffe722eSMika Westerberg if (ring->start_poll) 3744ffe722eSMika Westerberg __ring_interrupt_mask(ring, false); 3754ffe722eSMika Westerberg spin_unlock(&ring->lock); 3764ffe722eSMika Westerberg spin_unlock_irqrestore(&ring->nhi->lock, flags); 3774ffe722eSMika Westerberg } 3784ffe722eSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_poll_complete); 3794ffe722eSMika Westerberg 380046bee1fSMika Westerberg static irqreturn_t ring_msix(int irq, void *data) 381046bee1fSMika Westerberg { 382046bee1fSMika Westerberg struct tb_ring *ring = data; 383046bee1fSMika Westerberg 3844ffe722eSMika Westerberg spin_lock(&ring->nhi->lock); 3854ffe722eSMika Westerberg spin_lock(&ring->lock); 3864ffe722eSMika Westerberg __ring_interrupt(ring); 3874ffe722eSMika Westerberg spin_unlock(&ring->lock); 3884ffe722eSMika Westerberg spin_unlock(&ring->nhi->lock); 3894ffe722eSMika Westerberg 390046bee1fSMika Westerberg return IRQ_HANDLED; 391046bee1fSMika Westerberg } 392046bee1fSMika Westerberg 393046bee1fSMika Westerberg static int ring_request_msix(struct tb_ring *ring, bool no_suspend) 394046bee1fSMika Westerberg { 395046bee1fSMika Westerberg struct tb_nhi *nhi = ring->nhi; 396046bee1fSMika Westerberg unsigned long irqflags; 397046bee1fSMika Westerberg int ret; 398046bee1fSMika Westerberg 399046bee1fSMika Westerberg if (!nhi->pdev->msix_enabled) 400046bee1fSMika Westerberg return 0; 401046bee1fSMika Westerberg 402046bee1fSMika Westerberg ret = ida_simple_get(&nhi->msix_ida, 0, MSIX_MAX_VECS, GFP_KERNEL); 403046bee1fSMika Westerberg if (ret < 0) 404046bee1fSMika Westerberg return ret; 405046bee1fSMika Westerberg 406046bee1fSMika Westerberg ring->vector = ret; 407046bee1fSMika Westerberg 4087342ca34SJing Xiangfeng ret = pci_irq_vector(ring->nhi->pdev, ring->vector); 4097342ca34SJing Xiangfeng if (ret < 0) 4107342ca34SJing Xiangfeng goto err_ida_remove; 4117342ca34SJing Xiangfeng 4127342ca34SJing Xiangfeng ring->irq = ret; 413046bee1fSMika Westerberg 414046bee1fSMika Westerberg irqflags = no_suspend ? IRQF_NO_SUSPEND : 0; 4157342ca34SJing Xiangfeng ret = request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring); 4167342ca34SJing Xiangfeng if (ret) 4177342ca34SJing Xiangfeng goto err_ida_remove; 4187342ca34SJing Xiangfeng 4197342ca34SJing Xiangfeng return 0; 4207342ca34SJing Xiangfeng 4217342ca34SJing Xiangfeng err_ida_remove: 4227342ca34SJing Xiangfeng ida_simple_remove(&nhi->msix_ida, ring->vector); 4237342ca34SJing Xiangfeng 4247342ca34SJing Xiangfeng return ret; 425046bee1fSMika Westerberg } 426046bee1fSMika Westerberg 427046bee1fSMika Westerberg static void ring_release_msix(struct tb_ring *ring) 428046bee1fSMika Westerberg { 429046bee1fSMika Westerberg if (ring->irq <= 0) 430046bee1fSMika Westerberg return; 431046bee1fSMika Westerberg 432046bee1fSMika Westerberg free_irq(ring->irq, ring); 433046bee1fSMika Westerberg ida_simple_remove(&ring->nhi->msix_ida, ring->vector); 434046bee1fSMika Westerberg ring->vector = 0; 435046bee1fSMika Westerberg ring->irq = 0; 436046bee1fSMika Westerberg } 437046bee1fSMika Westerberg 4389a01c7c2SMika Westerberg static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring) 4399a01c7c2SMika Westerberg { 4409a01c7c2SMika Westerberg int ret = 0; 4419a01c7c2SMika Westerberg 4429a01c7c2SMika Westerberg spin_lock_irq(&nhi->lock); 4439a01c7c2SMika Westerberg 4449a01c7c2SMika Westerberg if (ring->hop < 0) { 4459a01c7c2SMika Westerberg unsigned int i; 4469a01c7c2SMika Westerberg 4479a01c7c2SMika Westerberg /* 4489a01c7c2SMika Westerberg * Automatically allocate HopID from the non-reserved 44953f13319SMika Westerberg * range 1 .. hop_count - 1. 4509a01c7c2SMika Westerberg */ 4519a01c7c2SMika Westerberg for (i = RING_FIRST_USABLE_HOPID; i < nhi->hop_count; i++) { 4529a01c7c2SMika Westerberg if (ring->is_tx) { 4539a01c7c2SMika Westerberg if (!nhi->tx_rings[i]) { 4549a01c7c2SMika Westerberg ring->hop = i; 4559a01c7c2SMika Westerberg break; 4569a01c7c2SMika Westerberg } 4579a01c7c2SMika Westerberg } else { 4589a01c7c2SMika Westerberg if (!nhi->rx_rings[i]) { 4599a01c7c2SMika Westerberg ring->hop = i; 4609a01c7c2SMika Westerberg break; 4619a01c7c2SMika Westerberg } 4629a01c7c2SMika Westerberg } 4639a01c7c2SMika Westerberg } 4649a01c7c2SMika Westerberg } 4659a01c7c2SMika Westerberg 4669a01c7c2SMika Westerberg if (ring->hop < 0 || ring->hop >= nhi->hop_count) { 4679a01c7c2SMika Westerberg dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop); 4689a01c7c2SMika Westerberg ret = -EINVAL; 4699a01c7c2SMika Westerberg goto err_unlock; 4709a01c7c2SMika Westerberg } 4719a01c7c2SMika Westerberg if (ring->is_tx && nhi->tx_rings[ring->hop]) { 4729a01c7c2SMika Westerberg dev_warn(&nhi->pdev->dev, "TX hop %d already allocated\n", 4739a01c7c2SMika Westerberg ring->hop); 4749a01c7c2SMika Westerberg ret = -EBUSY; 4759a01c7c2SMika Westerberg goto err_unlock; 4769a01c7c2SMika Westerberg } else if (!ring->is_tx && nhi->rx_rings[ring->hop]) { 4779a01c7c2SMika Westerberg dev_warn(&nhi->pdev->dev, "RX hop %d already allocated\n", 4789a01c7c2SMika Westerberg ring->hop); 4799a01c7c2SMika Westerberg ret = -EBUSY; 4809a01c7c2SMika Westerberg goto err_unlock; 4819a01c7c2SMika Westerberg } 4829a01c7c2SMika Westerberg 4839a01c7c2SMika Westerberg if (ring->is_tx) 4849a01c7c2SMika Westerberg nhi->tx_rings[ring->hop] = ring; 4859a01c7c2SMika Westerberg else 4869a01c7c2SMika Westerberg nhi->rx_rings[ring->hop] = ring; 4879a01c7c2SMika Westerberg 4889a01c7c2SMika Westerberg err_unlock: 4899a01c7c2SMika Westerberg spin_unlock_irq(&nhi->lock); 4909a01c7c2SMika Westerberg 4919a01c7c2SMika Westerberg return ret; 4929a01c7c2SMika Westerberg } 4939a01c7c2SMika Westerberg 4943b3d9f4dSMika Westerberg static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size, 4959fb1e654SMika Westerberg bool transmit, unsigned int flags, 496afe704a2SMika Westerberg int e2e_tx_hop, u16 sof_mask, u16 eof_mask, 4974ffe722eSMika Westerberg void (*start_poll)(void *), 4984ffe722eSMika Westerberg void *poll_data) 49916603153SAndreas Noever { 50016603153SAndreas Noever struct tb_ring *ring = NULL; 501daa5140fSMika Westerberg 502daa5140fSMika Westerberg dev_dbg(&nhi->pdev->dev, "allocating %s ring %d of size %d\n", 50316603153SAndreas Noever transmit ? "TX" : "RX", hop, size); 50416603153SAndreas Noever 50516603153SAndreas Noever ring = kzalloc(sizeof(*ring), GFP_KERNEL); 50616603153SAndreas Noever if (!ring) 50759120e06SMika Westerberg return NULL; 50816603153SAndreas Noever 50922b7de10SMika Westerberg spin_lock_init(&ring->lock); 51016603153SAndreas Noever INIT_LIST_HEAD(&ring->queue); 51116603153SAndreas Noever INIT_LIST_HEAD(&ring->in_flight); 51216603153SAndreas Noever INIT_WORK(&ring->work, ring_work); 51316603153SAndreas Noever 51416603153SAndreas Noever ring->nhi = nhi; 51516603153SAndreas Noever ring->hop = hop; 51616603153SAndreas Noever ring->is_tx = transmit; 51716603153SAndreas Noever ring->size = size; 518046bee1fSMika Westerberg ring->flags = flags; 519afe704a2SMika Westerberg ring->e2e_tx_hop = e2e_tx_hop; 5209fb1e654SMika Westerberg ring->sof_mask = sof_mask; 5219fb1e654SMika Westerberg ring->eof_mask = eof_mask; 52216603153SAndreas Noever ring->head = 0; 52316603153SAndreas Noever ring->tail = 0; 52416603153SAndreas Noever ring->running = false; 5254ffe722eSMika Westerberg ring->start_poll = start_poll; 5264ffe722eSMika Westerberg ring->poll_data = poll_data; 527046bee1fSMika Westerberg 52816603153SAndreas Noever ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev, 52916603153SAndreas Noever size * sizeof(*ring->descriptors), 53016603153SAndreas Noever &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO); 53116603153SAndreas Noever if (!ring->descriptors) 53259120e06SMika Westerberg goto err_free_ring; 53316603153SAndreas Noever 53459120e06SMika Westerberg if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND)) 53559120e06SMika Westerberg goto err_free_descs; 53659120e06SMika Westerberg 5379a01c7c2SMika Westerberg if (nhi_alloc_hop(nhi, ring)) 53859120e06SMika Westerberg goto err_release_msix; 53959120e06SMika Westerberg 54016603153SAndreas Noever return ring; 54116603153SAndreas Noever 54259120e06SMika Westerberg err_release_msix: 54359120e06SMika Westerberg ring_release_msix(ring); 54459120e06SMika Westerberg err_free_descs: 54559120e06SMika Westerberg dma_free_coherent(&ring->nhi->pdev->dev, 54659120e06SMika Westerberg ring->size * sizeof(*ring->descriptors), 54759120e06SMika Westerberg ring->descriptors, ring->descriptors_dma); 54859120e06SMika Westerberg err_free_ring: 54916603153SAndreas Noever kfree(ring); 55059120e06SMika Westerberg 55116603153SAndreas Noever return NULL; 55216603153SAndreas Noever } 55316603153SAndreas Noever 5543b3d9f4dSMika Westerberg /** 5553b3d9f4dSMika Westerberg * tb_ring_alloc_tx() - Allocate DMA ring for transmit 5563b3d9f4dSMika Westerberg * @nhi: Pointer to the NHI the ring is to be allocated 5573b3d9f4dSMika Westerberg * @hop: HopID (ring) to allocate 5583b3d9f4dSMika Westerberg * @size: Number of entries in the ring 5593b3d9f4dSMika Westerberg * @flags: Flags for the ring 5603b3d9f4dSMika Westerberg */ 5613b3d9f4dSMika Westerberg struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size, 562046bee1fSMika Westerberg unsigned int flags) 56316603153SAndreas Noever { 564afe704a2SMika Westerberg return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0, 0, NULL, NULL); 56516603153SAndreas Noever } 5663b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_alloc_tx); 56716603153SAndreas Noever 56816603153SAndreas Noever /** 5693b3d9f4dSMika Westerberg * tb_ring_alloc_rx() - Allocate DMA ring for receive 5703b3d9f4dSMika Westerberg * @nhi: Pointer to the NHI the ring is to be allocated 5719a01c7c2SMika Westerberg * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation. 5723b3d9f4dSMika Westerberg * @size: Number of entries in the ring 5733b3d9f4dSMika Westerberg * @flags: Flags for the ring 574afe704a2SMika Westerberg * @e2e_tx_hop: Transmit HopID when E2E is enabled in @flags 5753b3d9f4dSMika Westerberg * @sof_mask: Mask of PDF values that start a frame 5763b3d9f4dSMika Westerberg * @eof_mask: Mask of PDF values that end a frame 5774ffe722eSMika Westerberg * @start_poll: If not %NULL the ring will call this function when an 5784ffe722eSMika Westerberg * interrupt is triggered and masked, instead of callback 5794ffe722eSMika Westerberg * in each Rx frame. 5804ffe722eSMika Westerberg * @poll_data: Optional data passed to @start_poll 58116603153SAndreas Noever */ 5823b3d9f4dSMika Westerberg struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size, 583afe704a2SMika Westerberg unsigned int flags, int e2e_tx_hop, 584afe704a2SMika Westerberg u16 sof_mask, u16 eof_mask, 5854ffe722eSMika Westerberg void (*start_poll)(void *), void *poll_data) 5863b3d9f4dSMika Westerberg { 587afe704a2SMika Westerberg return tb_ring_alloc(nhi, hop, size, false, flags, e2e_tx_hop, sof_mask, eof_mask, 5884ffe722eSMika Westerberg start_poll, poll_data); 5893b3d9f4dSMika Westerberg } 5903b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_alloc_rx); 5913b3d9f4dSMika Westerberg 5923b3d9f4dSMika Westerberg /** 5933b3d9f4dSMika Westerberg * tb_ring_start() - enable a ring 5946894bd37SMika Westerberg * @ring: Ring to start 5953b3d9f4dSMika Westerberg * 5963b3d9f4dSMika Westerberg * Must not be invoked in parallel with tb_ring_stop(). 5973b3d9f4dSMika Westerberg */ 5983b3d9f4dSMika Westerberg void tb_ring_start(struct tb_ring *ring) 59916603153SAndreas Noever { 6009fb1e654SMika Westerberg u16 frame_size; 6019fb1e654SMika Westerberg u32 flags; 6029fb1e654SMika Westerberg 60359120e06SMika Westerberg spin_lock_irq(&ring->nhi->lock); 60459120e06SMika Westerberg spin_lock(&ring->lock); 605bdccf295SMika Westerberg if (ring->nhi->going_away) 606bdccf295SMika Westerberg goto err; 60716603153SAndreas Noever if (ring->running) { 60816603153SAndreas Noever dev_WARN(&ring->nhi->pdev->dev, "ring already started\n"); 60916603153SAndreas Noever goto err; 61016603153SAndreas Noever } 611daa5140fSMika Westerberg dev_dbg(&ring->nhi->pdev->dev, "starting %s %d\n", 61216603153SAndreas Noever RING_TYPE(ring), ring->hop); 61316603153SAndreas Noever 6149fb1e654SMika Westerberg if (ring->flags & RING_FLAG_FRAME) { 6159fb1e654SMika Westerberg /* Means 4096 */ 6169fb1e654SMika Westerberg frame_size = 0; 6179fb1e654SMika Westerberg flags = RING_FLAG_ENABLE; 6189fb1e654SMika Westerberg } else { 6199fb1e654SMika Westerberg frame_size = TB_FRAME_SIZE; 6209fb1e654SMika Westerberg flags = RING_FLAG_ENABLE | RING_FLAG_RAW; 6219fb1e654SMika Westerberg } 6229fb1e654SMika Westerberg 62316603153SAndreas Noever ring_iowrite64desc(ring, ring->descriptors_dma, 0); 62416603153SAndreas Noever if (ring->is_tx) { 62516603153SAndreas Noever ring_iowrite32desc(ring, ring->size, 12); 62616603153SAndreas Noever ring_iowrite32options(ring, 0, 4); /* time releated ? */ 6279fb1e654SMika Westerberg ring_iowrite32options(ring, flags, 0); 62816603153SAndreas Noever } else { 6299fb1e654SMika Westerberg u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask; 6309fb1e654SMika Westerberg 6319fb1e654SMika Westerberg ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12); 6329fb1e654SMika Westerberg ring_iowrite32options(ring, sof_eof_mask, 4); 6339fb1e654SMika Westerberg ring_iowrite32options(ring, flags, 0); 63416603153SAndreas Noever } 635afe704a2SMika Westerberg 636afe704a2SMika Westerberg /* 637afe704a2SMika Westerberg * Now that the ring valid bit is set we can configure E2E if 638afe704a2SMika Westerberg * enabled for the ring. 639afe704a2SMika Westerberg */ 640afe704a2SMika Westerberg if (ring->flags & RING_FLAG_E2E) { 641afe704a2SMika Westerberg if (!ring->is_tx) { 642afe704a2SMika Westerberg u32 hop; 643afe704a2SMika Westerberg 644afe704a2SMika Westerberg hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT; 645afe704a2SMika Westerberg hop &= REG_RX_OPTIONS_E2E_HOP_MASK; 646afe704a2SMika Westerberg flags |= hop; 647afe704a2SMika Westerberg 648afe704a2SMika Westerberg dev_dbg(&ring->nhi->pdev->dev, 649afe704a2SMika Westerberg "enabling E2E for %s %d with TX HopID %d\n", 650afe704a2SMika Westerberg RING_TYPE(ring), ring->hop, ring->e2e_tx_hop); 651afe704a2SMika Westerberg } else { 652afe704a2SMika Westerberg dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d\n", 653afe704a2SMika Westerberg RING_TYPE(ring), ring->hop); 654afe704a2SMika Westerberg } 655afe704a2SMika Westerberg 656afe704a2SMika Westerberg flags |= RING_FLAG_E2E_FLOW_CONTROL; 657afe704a2SMika Westerberg ring_iowrite32options(ring, flags, 0); 658afe704a2SMika Westerberg } 659afe704a2SMika Westerberg 66016603153SAndreas Noever ring_interrupt_active(ring, true); 66116603153SAndreas Noever ring->running = true; 66216603153SAndreas Noever err: 66359120e06SMika Westerberg spin_unlock(&ring->lock); 66459120e06SMika Westerberg spin_unlock_irq(&ring->nhi->lock); 66516603153SAndreas Noever } 6663b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_start); 66716603153SAndreas Noever 66816603153SAndreas Noever /** 6693b3d9f4dSMika Westerberg * tb_ring_stop() - shutdown a ring 6706894bd37SMika Westerberg * @ring: Ring to stop 67116603153SAndreas Noever * 67216603153SAndreas Noever * Must not be invoked from a callback. 67316603153SAndreas Noever * 6743b3d9f4dSMika Westerberg * This method will disable the ring. Further calls to 6753b3d9f4dSMika Westerberg * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been 6763b3d9f4dSMika Westerberg * called. 67716603153SAndreas Noever * 67816603153SAndreas Noever * All enqueued frames will be canceled and their callbacks will be executed 67916603153SAndreas Noever * with frame->canceled set to true (on the callback thread). This method 68016603153SAndreas Noever * returns only after all callback invocations have finished. 68116603153SAndreas Noever */ 6823b3d9f4dSMika Westerberg void tb_ring_stop(struct tb_ring *ring) 68316603153SAndreas Noever { 68459120e06SMika Westerberg spin_lock_irq(&ring->nhi->lock); 68559120e06SMika Westerberg spin_lock(&ring->lock); 686daa5140fSMika Westerberg dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n", 68716603153SAndreas Noever RING_TYPE(ring), ring->hop); 688bdccf295SMika Westerberg if (ring->nhi->going_away) 689bdccf295SMika Westerberg goto err; 69016603153SAndreas Noever if (!ring->running) { 69116603153SAndreas Noever dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n", 69216603153SAndreas Noever RING_TYPE(ring), ring->hop); 69316603153SAndreas Noever goto err; 69416603153SAndreas Noever } 69516603153SAndreas Noever ring_interrupt_active(ring, false); 69616603153SAndreas Noever 69716603153SAndreas Noever ring_iowrite32options(ring, 0, 0); 69816603153SAndreas Noever ring_iowrite64desc(ring, 0, 0); 69994379521SMika Westerberg ring_iowrite32desc(ring, 0, 8); 70016603153SAndreas Noever ring_iowrite32desc(ring, 0, 12); 70116603153SAndreas Noever ring->head = 0; 70216603153SAndreas Noever ring->tail = 0; 70316603153SAndreas Noever ring->running = false; 70416603153SAndreas Noever 70516603153SAndreas Noever err: 70659120e06SMika Westerberg spin_unlock(&ring->lock); 70759120e06SMika Westerberg spin_unlock_irq(&ring->nhi->lock); 70816603153SAndreas Noever 70916603153SAndreas Noever /* 71016603153SAndreas Noever * schedule ring->work to invoke callbacks on all remaining frames. 71116603153SAndreas Noever */ 71216603153SAndreas Noever schedule_work(&ring->work); 71316603153SAndreas Noever flush_work(&ring->work); 71416603153SAndreas Noever } 7153b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_stop); 71616603153SAndreas Noever 71716603153SAndreas Noever /* 7183b3d9f4dSMika Westerberg * tb_ring_free() - free ring 71916603153SAndreas Noever * 72016603153SAndreas Noever * When this method returns all invocations of ring->callback will have 72116603153SAndreas Noever * finished. 72216603153SAndreas Noever * 72316603153SAndreas Noever * Ring must be stopped. 72416603153SAndreas Noever * 72516603153SAndreas Noever * Must NOT be called from ring_frame->callback! 72616603153SAndreas Noever */ 7273b3d9f4dSMika Westerberg void tb_ring_free(struct tb_ring *ring) 72816603153SAndreas Noever { 72959120e06SMika Westerberg spin_lock_irq(&ring->nhi->lock); 73016603153SAndreas Noever /* 73116603153SAndreas Noever * Dissociate the ring from the NHI. This also ensures that 73216603153SAndreas Noever * nhi_interrupt_work cannot reschedule ring->work. 73316603153SAndreas Noever */ 73416603153SAndreas Noever if (ring->is_tx) 73516603153SAndreas Noever ring->nhi->tx_rings[ring->hop] = NULL; 73616603153SAndreas Noever else 73716603153SAndreas Noever ring->nhi->rx_rings[ring->hop] = NULL; 73816603153SAndreas Noever 73916603153SAndreas Noever if (ring->running) { 74016603153SAndreas Noever dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n", 74116603153SAndreas Noever RING_TYPE(ring), ring->hop); 74216603153SAndreas Noever } 7434ffe722eSMika Westerberg spin_unlock_irq(&ring->nhi->lock); 74416603153SAndreas Noever 745046bee1fSMika Westerberg ring_release_msix(ring); 746046bee1fSMika Westerberg 74716603153SAndreas Noever dma_free_coherent(&ring->nhi->pdev->dev, 74816603153SAndreas Noever ring->size * sizeof(*ring->descriptors), 74916603153SAndreas Noever ring->descriptors, ring->descriptors_dma); 75016603153SAndreas Noever 751f19b72c6SSachin Kamat ring->descriptors = NULL; 75216603153SAndreas Noever ring->descriptors_dma = 0; 75316603153SAndreas Noever 75416603153SAndreas Noever 755daa5140fSMika Westerberg dev_dbg(&ring->nhi->pdev->dev, "freeing %s %d\n", RING_TYPE(ring), 75616603153SAndreas Noever ring->hop); 75716603153SAndreas Noever 758a7bfb27bSLee Jones /* 759046bee1fSMika Westerberg * ring->work can no longer be scheduled (it is scheduled only 760046bee1fSMika Westerberg * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it 761046bee1fSMika Westerberg * to finish before freeing the ring. 76216603153SAndreas Noever */ 76316603153SAndreas Noever flush_work(&ring->work); 76416603153SAndreas Noever kfree(ring); 76516603153SAndreas Noever } 7663b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_free); 76716603153SAndreas Noever 768cd446ee2SMika Westerberg /** 769cd446ee2SMika Westerberg * nhi_mailbox_cmd() - Send a command through NHI mailbox 770cd446ee2SMika Westerberg * @nhi: Pointer to the NHI structure 771cd446ee2SMika Westerberg * @cmd: Command to send 772cd446ee2SMika Westerberg * @data: Data to be send with the command 773cd446ee2SMika Westerberg * 774cd446ee2SMika Westerberg * Sends mailbox command to the firmware running on NHI. Returns %0 in 775cd446ee2SMika Westerberg * case of success and negative errno in case of failure. 776cd446ee2SMika Westerberg */ 777cd446ee2SMika Westerberg int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data) 778cd446ee2SMika Westerberg { 779cd446ee2SMika Westerberg ktime_t timeout; 780cd446ee2SMika Westerberg u32 val; 781cd446ee2SMika Westerberg 782cd446ee2SMika Westerberg iowrite32(data, nhi->iobase + REG_INMAIL_DATA); 783cd446ee2SMika Westerberg 784cd446ee2SMika Westerberg val = ioread32(nhi->iobase + REG_INMAIL_CMD); 785cd446ee2SMika Westerberg val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR); 786cd446ee2SMika Westerberg val |= REG_INMAIL_OP_REQUEST | cmd; 787cd446ee2SMika Westerberg iowrite32(val, nhi->iobase + REG_INMAIL_CMD); 788cd446ee2SMika Westerberg 789cd446ee2SMika Westerberg timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT); 790cd446ee2SMika Westerberg do { 791cd446ee2SMika Westerberg val = ioread32(nhi->iobase + REG_INMAIL_CMD); 792cd446ee2SMika Westerberg if (!(val & REG_INMAIL_OP_REQUEST)) 793cd446ee2SMika Westerberg break; 794cd446ee2SMika Westerberg usleep_range(10, 20); 795cd446ee2SMika Westerberg } while (ktime_before(ktime_get(), timeout)); 796cd446ee2SMika Westerberg 797cd446ee2SMika Westerberg if (val & REG_INMAIL_OP_REQUEST) 798cd446ee2SMika Westerberg return -ETIMEDOUT; 799cd446ee2SMika Westerberg if (val & REG_INMAIL_ERROR) 800cd446ee2SMika Westerberg return -EIO; 801cd446ee2SMika Westerberg 802cd446ee2SMika Westerberg return 0; 803cd446ee2SMika Westerberg } 804cd446ee2SMika Westerberg 805cd446ee2SMika Westerberg /** 806cd446ee2SMika Westerberg * nhi_mailbox_mode() - Return current firmware operation mode 807cd446ee2SMika Westerberg * @nhi: Pointer to the NHI structure 808cd446ee2SMika Westerberg * 809cd446ee2SMika Westerberg * The function reads current firmware operation mode using NHI mailbox 810cd446ee2SMika Westerberg * registers and returns it to the caller. 811cd446ee2SMika Westerberg */ 812cd446ee2SMika Westerberg enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi) 813cd446ee2SMika Westerberg { 814cd446ee2SMika Westerberg u32 val; 815cd446ee2SMika Westerberg 816cd446ee2SMika Westerberg val = ioread32(nhi->iobase + REG_OUTMAIL_CMD); 817cd446ee2SMika Westerberg val &= REG_OUTMAIL_CMD_OPMODE_MASK; 818cd446ee2SMika Westerberg val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT; 819cd446ee2SMika Westerberg 820cd446ee2SMika Westerberg return (enum nhi_fw_mode)val; 821cd446ee2SMika Westerberg } 822cd446ee2SMika Westerberg 82316603153SAndreas Noever static void nhi_interrupt_work(struct work_struct *work) 82416603153SAndreas Noever { 82516603153SAndreas Noever struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work); 82616603153SAndreas Noever int value = 0; /* Suppress uninitialized usage warning. */ 82716603153SAndreas Noever int bit; 82816603153SAndreas Noever int hop = -1; 82916603153SAndreas Noever int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */ 83016603153SAndreas Noever struct tb_ring *ring; 83116603153SAndreas Noever 83259120e06SMika Westerberg spin_lock_irq(&nhi->lock); 83316603153SAndreas Noever 83416603153SAndreas Noever /* 83516603153SAndreas Noever * Starting at REG_RING_NOTIFY_BASE there are three status bitfields 83616603153SAndreas Noever * (TX, RX, RX overflow). We iterate over the bits and read a new 83716603153SAndreas Noever * dwords as required. The registers are cleared on read. 83816603153SAndreas Noever */ 83916603153SAndreas Noever for (bit = 0; bit < 3 * nhi->hop_count; bit++) { 84016603153SAndreas Noever if (bit % 32 == 0) 84116603153SAndreas Noever value = ioread32(nhi->iobase 84216603153SAndreas Noever + REG_RING_NOTIFY_BASE 84316603153SAndreas Noever + 4 * (bit / 32)); 84416603153SAndreas Noever if (++hop == nhi->hop_count) { 84516603153SAndreas Noever hop = 0; 84616603153SAndreas Noever type++; 84716603153SAndreas Noever } 84816603153SAndreas Noever if ((value & (1 << (bit % 32))) == 0) 84916603153SAndreas Noever continue; 85016603153SAndreas Noever if (type == 2) { 85116603153SAndreas Noever dev_warn(&nhi->pdev->dev, 85216603153SAndreas Noever "RX overflow for ring %d\n", 85316603153SAndreas Noever hop); 85416603153SAndreas Noever continue; 85516603153SAndreas Noever } 85616603153SAndreas Noever if (type == 0) 85716603153SAndreas Noever ring = nhi->tx_rings[hop]; 85816603153SAndreas Noever else 85916603153SAndreas Noever ring = nhi->rx_rings[hop]; 86016603153SAndreas Noever if (ring == NULL) { 86116603153SAndreas Noever dev_warn(&nhi->pdev->dev, 86216603153SAndreas Noever "got interrupt for inactive %s ring %d\n", 86316603153SAndreas Noever type ? "RX" : "TX", 86416603153SAndreas Noever hop); 86516603153SAndreas Noever continue; 86616603153SAndreas Noever } 8674ffe722eSMika Westerberg 8684ffe722eSMika Westerberg spin_lock(&ring->lock); 8694ffe722eSMika Westerberg __ring_interrupt(ring); 8704ffe722eSMika Westerberg spin_unlock(&ring->lock); 87116603153SAndreas Noever } 87259120e06SMika Westerberg spin_unlock_irq(&nhi->lock); 87316603153SAndreas Noever } 87416603153SAndreas Noever 87516603153SAndreas Noever static irqreturn_t nhi_msi(int irq, void *data) 87616603153SAndreas Noever { 87716603153SAndreas Noever struct tb_nhi *nhi = data; 87816603153SAndreas Noever schedule_work(&nhi->interrupt_work); 87916603153SAndreas Noever return IRQ_HANDLED; 88016603153SAndreas Noever } 88116603153SAndreas Noever 8823cdb9446SMika Westerberg static int __nhi_suspend_noirq(struct device *dev, bool wakeup) 88323dd5bb4SAndreas Noever { 88423dd5bb4SAndreas Noever struct pci_dev *pdev = to_pci_dev(dev); 88523dd5bb4SAndreas Noever struct tb *tb = pci_get_drvdata(pdev); 8863cdb9446SMika Westerberg struct tb_nhi *nhi = tb->nhi; 8873cdb9446SMika Westerberg int ret; 8889d3cce0bSMika Westerberg 8893cdb9446SMika Westerberg ret = tb_domain_suspend_noirq(tb); 8903cdb9446SMika Westerberg if (ret) 8913cdb9446SMika Westerberg return ret; 8923cdb9446SMika Westerberg 8933cdb9446SMika Westerberg if (nhi->ops && nhi->ops->suspend_noirq) { 8943cdb9446SMika Westerberg ret = nhi->ops->suspend_noirq(tb->nhi, wakeup); 8953cdb9446SMika Westerberg if (ret) 8963cdb9446SMika Westerberg return ret; 8973cdb9446SMika Westerberg } 8983cdb9446SMika Westerberg 8993cdb9446SMika Westerberg return 0; 9003cdb9446SMika Westerberg } 9013cdb9446SMika Westerberg 9023cdb9446SMika Westerberg static int nhi_suspend_noirq(struct device *dev) 9033cdb9446SMika Westerberg { 9043cdb9446SMika Westerberg return __nhi_suspend_noirq(dev, device_may_wakeup(dev)); 9053cdb9446SMika Westerberg } 9063cdb9446SMika Westerberg 907884e4d57SMika Westerberg static int nhi_freeze_noirq(struct device *dev) 908884e4d57SMika Westerberg { 909884e4d57SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev); 910884e4d57SMika Westerberg struct tb *tb = pci_get_drvdata(pdev); 911884e4d57SMika Westerberg 912884e4d57SMika Westerberg return tb_domain_freeze_noirq(tb); 913884e4d57SMika Westerberg } 914884e4d57SMika Westerberg 915884e4d57SMika Westerberg static int nhi_thaw_noirq(struct device *dev) 916884e4d57SMika Westerberg { 917884e4d57SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev); 918884e4d57SMika Westerberg struct tb *tb = pci_get_drvdata(pdev); 919884e4d57SMika Westerberg 920884e4d57SMika Westerberg return tb_domain_thaw_noirq(tb); 921884e4d57SMika Westerberg } 922884e4d57SMika Westerberg 9233cdb9446SMika Westerberg static bool nhi_wake_supported(struct pci_dev *pdev) 9243cdb9446SMika Westerberg { 9253cdb9446SMika Westerberg u8 val; 9263cdb9446SMika Westerberg 9273cdb9446SMika Westerberg /* 9283cdb9446SMika Westerberg * If power rails are sustainable for wakeup from S4 this 9293cdb9446SMika Westerberg * property is set by the BIOS. 9303cdb9446SMika Westerberg */ 9313cdb9446SMika Westerberg if (device_property_read_u8(&pdev->dev, "WAKE_SUPPORTED", &val)) 9323cdb9446SMika Westerberg return !!val; 9333cdb9446SMika Westerberg 9343cdb9446SMika Westerberg return true; 9353cdb9446SMika Westerberg } 9363cdb9446SMika Westerberg 9373cdb9446SMika Westerberg static int nhi_poweroff_noirq(struct device *dev) 9383cdb9446SMika Westerberg { 9393cdb9446SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev); 9403cdb9446SMika Westerberg bool wakeup; 9413cdb9446SMika Westerberg 9423cdb9446SMika Westerberg wakeup = device_may_wakeup(dev) && nhi_wake_supported(pdev); 9433cdb9446SMika Westerberg return __nhi_suspend_noirq(dev, wakeup); 94423dd5bb4SAndreas Noever } 94523dd5bb4SAndreas Noever 9468c6bba10SMika Westerberg static void nhi_enable_int_throttling(struct tb_nhi *nhi) 9478c6bba10SMika Westerberg { 9488c6bba10SMika Westerberg /* Throttling is specified in 256ns increments */ 9498c6bba10SMika Westerberg u32 throttle = DIV_ROUND_UP(128 * NSEC_PER_USEC, 256); 9508c6bba10SMika Westerberg unsigned int i; 9518c6bba10SMika Westerberg 9528c6bba10SMika Westerberg /* 9538c6bba10SMika Westerberg * Configure interrupt throttling for all vectors even if we 9548c6bba10SMika Westerberg * only use few. 9558c6bba10SMika Westerberg */ 9568c6bba10SMika Westerberg for (i = 0; i < MSIX_MAX_VECS; i++) { 9578c6bba10SMika Westerberg u32 reg = REG_INT_THROTTLING_RATE + i * 4; 9588c6bba10SMika Westerberg iowrite32(throttle, nhi->iobase + reg); 9598c6bba10SMika Westerberg } 9608c6bba10SMika Westerberg } 9618c6bba10SMika Westerberg 96223dd5bb4SAndreas Noever static int nhi_resume_noirq(struct device *dev) 96323dd5bb4SAndreas Noever { 96423dd5bb4SAndreas Noever struct pci_dev *pdev = to_pci_dev(dev); 96523dd5bb4SAndreas Noever struct tb *tb = pci_get_drvdata(pdev); 9663cdb9446SMika Westerberg struct tb_nhi *nhi = tb->nhi; 9673cdb9446SMika Westerberg int ret; 9689d3cce0bSMika Westerberg 969bdccf295SMika Westerberg /* 970bdccf295SMika Westerberg * Check that the device is still there. It may be that the user 971bdccf295SMika Westerberg * unplugged last device which causes the host controller to go 972bdccf295SMika Westerberg * away on PCs. 973bdccf295SMika Westerberg */ 9743cdb9446SMika Westerberg if (!pci_device_is_present(pdev)) { 9753cdb9446SMika Westerberg nhi->going_away = true; 9763cdb9446SMika Westerberg } else { 9773cdb9446SMika Westerberg if (nhi->ops && nhi->ops->resume_noirq) { 9783cdb9446SMika Westerberg ret = nhi->ops->resume_noirq(nhi); 9793cdb9446SMika Westerberg if (ret) 9803cdb9446SMika Westerberg return ret; 9813cdb9446SMika Westerberg } 9828c6bba10SMika Westerberg nhi_enable_int_throttling(tb->nhi); 9833cdb9446SMika Westerberg } 984bdccf295SMika Westerberg 9859d3cce0bSMika Westerberg return tb_domain_resume_noirq(tb); 98623dd5bb4SAndreas Noever } 98723dd5bb4SAndreas Noever 988f67cf491SMika Westerberg static int nhi_suspend(struct device *dev) 989f67cf491SMika Westerberg { 990f67cf491SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev); 991f67cf491SMika Westerberg struct tb *tb = pci_get_drvdata(pdev); 992f67cf491SMika Westerberg 993f67cf491SMika Westerberg return tb_domain_suspend(tb); 994f67cf491SMika Westerberg } 995f67cf491SMika Westerberg 996f67cf491SMika Westerberg static void nhi_complete(struct device *dev) 997f67cf491SMika Westerberg { 998f67cf491SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev); 999f67cf491SMika Westerberg struct tb *tb = pci_get_drvdata(pdev); 1000f67cf491SMika Westerberg 10012d8ff0b5SMika Westerberg /* 10022d8ff0b5SMika Westerberg * If we were runtime suspended when system suspend started, 10032d8ff0b5SMika Westerberg * schedule runtime resume now. It should bring the domain back 10042d8ff0b5SMika Westerberg * to functional state. 10052d8ff0b5SMika Westerberg */ 10062d8ff0b5SMika Westerberg if (pm_runtime_suspended(&pdev->dev)) 10072d8ff0b5SMika Westerberg pm_runtime_resume(&pdev->dev); 10082d8ff0b5SMika Westerberg else 1009f67cf491SMika Westerberg tb_domain_complete(tb); 1010f67cf491SMika Westerberg } 1011f67cf491SMika Westerberg 10122d8ff0b5SMika Westerberg static int nhi_runtime_suspend(struct device *dev) 10132d8ff0b5SMika Westerberg { 10142d8ff0b5SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev); 10152d8ff0b5SMika Westerberg struct tb *tb = pci_get_drvdata(pdev); 10163cdb9446SMika Westerberg struct tb_nhi *nhi = tb->nhi; 10173cdb9446SMika Westerberg int ret; 10182d8ff0b5SMika Westerberg 10193cdb9446SMika Westerberg ret = tb_domain_runtime_suspend(tb); 10203cdb9446SMika Westerberg if (ret) 10213cdb9446SMika Westerberg return ret; 10223cdb9446SMika Westerberg 10233cdb9446SMika Westerberg if (nhi->ops && nhi->ops->runtime_suspend) { 10243cdb9446SMika Westerberg ret = nhi->ops->runtime_suspend(tb->nhi); 10253cdb9446SMika Westerberg if (ret) 10263cdb9446SMika Westerberg return ret; 10273cdb9446SMika Westerberg } 10283cdb9446SMika Westerberg return 0; 10292d8ff0b5SMika Westerberg } 10302d8ff0b5SMika Westerberg 10312d8ff0b5SMika Westerberg static int nhi_runtime_resume(struct device *dev) 10322d8ff0b5SMika Westerberg { 10332d8ff0b5SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev); 10342d8ff0b5SMika Westerberg struct tb *tb = pci_get_drvdata(pdev); 10353cdb9446SMika Westerberg struct tb_nhi *nhi = tb->nhi; 10363cdb9446SMika Westerberg int ret; 10372d8ff0b5SMika Westerberg 10383cdb9446SMika Westerberg if (nhi->ops && nhi->ops->runtime_resume) { 10393cdb9446SMika Westerberg ret = nhi->ops->runtime_resume(nhi); 10403cdb9446SMika Westerberg if (ret) 10413cdb9446SMika Westerberg return ret; 10423cdb9446SMika Westerberg } 10433cdb9446SMika Westerberg 10443cdb9446SMika Westerberg nhi_enable_int_throttling(nhi); 10452d8ff0b5SMika Westerberg return tb_domain_runtime_resume(tb); 10462d8ff0b5SMika Westerberg } 10472d8ff0b5SMika Westerberg 104816603153SAndreas Noever static void nhi_shutdown(struct tb_nhi *nhi) 104916603153SAndreas Noever { 105016603153SAndreas Noever int i; 1051daa5140fSMika Westerberg 1052daa5140fSMika Westerberg dev_dbg(&nhi->pdev->dev, "shutdown\n"); 105316603153SAndreas Noever 105416603153SAndreas Noever for (i = 0; i < nhi->hop_count; i++) { 105516603153SAndreas Noever if (nhi->tx_rings[i]) 105616603153SAndreas Noever dev_WARN(&nhi->pdev->dev, 105716603153SAndreas Noever "TX ring %d is still active\n", i); 105816603153SAndreas Noever if (nhi->rx_rings[i]) 105916603153SAndreas Noever dev_WARN(&nhi->pdev->dev, 106016603153SAndreas Noever "RX ring %d is still active\n", i); 106116603153SAndreas Noever } 106216603153SAndreas Noever nhi_disable_interrupts(nhi); 106316603153SAndreas Noever /* 106416603153SAndreas Noever * We have to release the irq before calling flush_work. Otherwise an 106516603153SAndreas Noever * already executing IRQ handler could call schedule_work again. 106616603153SAndreas Noever */ 1067046bee1fSMika Westerberg if (!nhi->pdev->msix_enabled) { 106816603153SAndreas Noever devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi); 106916603153SAndreas Noever flush_work(&nhi->interrupt_work); 1070046bee1fSMika Westerberg } 1071046bee1fSMika Westerberg ida_destroy(&nhi->msix_ida); 10723cdb9446SMika Westerberg 10733cdb9446SMika Westerberg if (nhi->ops && nhi->ops->shutdown) 10743cdb9446SMika Westerberg nhi->ops->shutdown(nhi); 1075046bee1fSMika Westerberg } 1076046bee1fSMika Westerberg 1077046bee1fSMika Westerberg static int nhi_init_msi(struct tb_nhi *nhi) 1078046bee1fSMika Westerberg { 1079046bee1fSMika Westerberg struct pci_dev *pdev = nhi->pdev; 1080046bee1fSMika Westerberg int res, irq, nvec; 1081046bee1fSMika Westerberg 1082046bee1fSMika Westerberg /* In case someone left them on. */ 1083046bee1fSMika Westerberg nhi_disable_interrupts(nhi); 1084046bee1fSMika Westerberg 10858c6bba10SMika Westerberg nhi_enable_int_throttling(nhi); 10868c6bba10SMika Westerberg 1087046bee1fSMika Westerberg ida_init(&nhi->msix_ida); 1088046bee1fSMika Westerberg 1089046bee1fSMika Westerberg /* 1090046bee1fSMika Westerberg * The NHI has 16 MSI-X vectors or a single MSI. We first try to 1091046bee1fSMika Westerberg * get all MSI-X vectors and if we succeed, each ring will have 1092046bee1fSMika Westerberg * one MSI-X. If for some reason that does not work out, we 1093046bee1fSMika Westerberg * fallback to a single MSI. 1094046bee1fSMika Westerberg */ 1095046bee1fSMika Westerberg nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS, 1096046bee1fSMika Westerberg PCI_IRQ_MSIX); 1097046bee1fSMika Westerberg if (nvec < 0) { 1098046bee1fSMika Westerberg nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI); 1099046bee1fSMika Westerberg if (nvec < 0) 1100046bee1fSMika Westerberg return nvec; 1101046bee1fSMika Westerberg 1102046bee1fSMika Westerberg INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work); 1103046bee1fSMika Westerberg 1104046bee1fSMika Westerberg irq = pci_irq_vector(nhi->pdev, 0); 1105046bee1fSMika Westerberg if (irq < 0) 1106046bee1fSMika Westerberg return irq; 1107046bee1fSMika Westerberg 1108046bee1fSMika Westerberg res = devm_request_irq(&pdev->dev, irq, nhi_msi, 1109046bee1fSMika Westerberg IRQF_NO_SUSPEND, "thunderbolt", nhi); 1110046bee1fSMika Westerberg if (res) { 1111046bee1fSMika Westerberg dev_err(&pdev->dev, "request_irq failed, aborting\n"); 1112046bee1fSMika Westerberg return res; 1113046bee1fSMika Westerberg } 1114046bee1fSMika Westerberg } 1115046bee1fSMika Westerberg 1116046bee1fSMika Westerberg return 0; 111716603153SAndreas Noever } 111816603153SAndreas Noever 11193cdb9446SMika Westerberg static bool nhi_imr_valid(struct pci_dev *pdev) 11203cdb9446SMika Westerberg { 11213cdb9446SMika Westerberg u8 val; 11223cdb9446SMika Westerberg 11233cdb9446SMika Westerberg if (!device_property_read_u8(&pdev->dev, "IMR_VALID", &val)) 11243cdb9446SMika Westerberg return !!val; 11253cdb9446SMika Westerberg 11263cdb9446SMika Westerberg return true; 11273cdb9446SMika Westerberg } 11283cdb9446SMika Westerberg 1129c6da62a2SMika Westerberg static struct tb *nhi_select_cm(struct tb_nhi *nhi) 1130c6da62a2SMika Westerberg { 1131c6da62a2SMika Westerberg struct tb *tb; 1132c6da62a2SMika Westerberg 1133c6da62a2SMika Westerberg /* 1134c6da62a2SMika Westerberg * USB4 case is simple. If we got control of any of the 1135c6da62a2SMika Westerberg * capabilities, we use software CM. 1136c6da62a2SMika Westerberg */ 1137c6da62a2SMika Westerberg if (tb_acpi_is_native()) 1138c6da62a2SMika Westerberg return tb_probe(nhi); 1139c6da62a2SMika Westerberg 1140c6da62a2SMika Westerberg /* 1141c6da62a2SMika Westerberg * Either firmware based CM is running (we did not get control 1142c6da62a2SMika Westerberg * from the firmware) or this is pre-USB4 PC so try first 1143c6da62a2SMika Westerberg * firmware CM and then fallback to software CM. 1144c6da62a2SMika Westerberg */ 1145c6da62a2SMika Westerberg tb = icm_probe(nhi); 1146c6da62a2SMika Westerberg if (!tb) 1147c6da62a2SMika Westerberg tb = tb_probe(nhi); 1148c6da62a2SMika Westerberg 1149c6da62a2SMika Westerberg return tb; 1150c6da62a2SMika Westerberg } 1151c6da62a2SMika Westerberg 115216603153SAndreas Noever static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id) 115316603153SAndreas Noever { 115416603153SAndreas Noever struct tb_nhi *nhi; 1155d6cc51cdSAndreas Noever struct tb *tb; 115616603153SAndreas Noever int res; 115716603153SAndreas Noever 11583cdb9446SMika Westerberg if (!nhi_imr_valid(pdev)) { 11593cdb9446SMika Westerberg dev_warn(&pdev->dev, "firmware image not valid, aborting\n"); 11603cdb9446SMika Westerberg return -ENODEV; 11613cdb9446SMika Westerberg } 11623cdb9446SMika Westerberg 116316603153SAndreas Noever res = pcim_enable_device(pdev); 116416603153SAndreas Noever if (res) { 116516603153SAndreas Noever dev_err(&pdev->dev, "cannot enable PCI device, aborting\n"); 116616603153SAndreas Noever return res; 116716603153SAndreas Noever } 116816603153SAndreas Noever 116916603153SAndreas Noever res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt"); 117016603153SAndreas Noever if (res) { 117116603153SAndreas Noever dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n"); 117216603153SAndreas Noever return res; 117316603153SAndreas Noever } 117416603153SAndreas Noever 117516603153SAndreas Noever nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL); 117616603153SAndreas Noever if (!nhi) 117716603153SAndreas Noever return -ENOMEM; 117816603153SAndreas Noever 117916603153SAndreas Noever nhi->pdev = pdev; 11803cdb9446SMika Westerberg nhi->ops = (const struct tb_nhi_ops *)id->driver_data; 118116603153SAndreas Noever /* cannot fail - table is allocated bin pcim_iomap_regions */ 118216603153SAndreas Noever nhi->iobase = pcim_iomap_table(pdev)[0]; 118316603153SAndreas Noever nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff; 1184177aa362SMika Westerberg dev_dbg(&pdev->dev, "total paths: %d\n", nhi->hop_count); 118516603153SAndreas Noever 11862a211f32SHimangi Saraogi nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count, 11872a211f32SHimangi Saraogi sizeof(*nhi->tx_rings), GFP_KERNEL); 11882a211f32SHimangi Saraogi nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count, 11892a211f32SHimangi Saraogi sizeof(*nhi->rx_rings), GFP_KERNEL); 119016603153SAndreas Noever if (!nhi->tx_rings || !nhi->rx_rings) 119116603153SAndreas Noever return -ENOMEM; 119216603153SAndreas Noever 1193046bee1fSMika Westerberg res = nhi_init_msi(nhi); 119416603153SAndreas Noever if (res) { 1195046bee1fSMika Westerberg dev_err(&pdev->dev, "cannot enable MSI, aborting\n"); 119616603153SAndreas Noever return res; 119716603153SAndreas Noever } 119816603153SAndreas Noever 119959120e06SMika Westerberg spin_lock_init(&nhi->lock); 120016603153SAndreas Noever 1201dba3caf6SMika Westerberg res = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 1202dba3caf6SMika Westerberg if (res) 1203dba3caf6SMika Westerberg res = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 1204dba3caf6SMika Westerberg if (res) { 1205dba3caf6SMika Westerberg dev_err(&pdev->dev, "failed to set DMA mask\n"); 1206dba3caf6SMika Westerberg return res; 1207dba3caf6SMika Westerberg } 1208dba3caf6SMika Westerberg 120916603153SAndreas Noever pci_set_master(pdev); 121016603153SAndreas Noever 12113cdb9446SMika Westerberg if (nhi->ops && nhi->ops->init) { 12123cdb9446SMika Westerberg res = nhi->ops->init(nhi); 12133cdb9446SMika Westerberg if (res) 12143cdb9446SMika Westerberg return res; 12153cdb9446SMika Westerberg } 12163cdb9446SMika Westerberg 1217c6da62a2SMika Westerberg tb = nhi_select_cm(nhi); 1218f67cf491SMika Westerberg if (!tb) { 1219f67cf491SMika Westerberg dev_err(&nhi->pdev->dev, 1220f67cf491SMika Westerberg "failed to determine connection manager, aborting\n"); 12219d3cce0bSMika Westerberg return -ENODEV; 1222f67cf491SMika Westerberg } 1223f67cf491SMika Westerberg 1224daa5140fSMika Westerberg dev_dbg(&nhi->pdev->dev, "NHI initialized, starting thunderbolt\n"); 12259d3cce0bSMika Westerberg 12269d3cce0bSMika Westerberg res = tb_domain_add(tb); 12279d3cce0bSMika Westerberg if (res) { 1228d6cc51cdSAndreas Noever /* 1229d6cc51cdSAndreas Noever * At this point the RX/TX rings might already have been 1230d6cc51cdSAndreas Noever * activated. Do a proper shutdown. 1231d6cc51cdSAndreas Noever */ 12329d3cce0bSMika Westerberg tb_domain_put(tb); 1233d6cc51cdSAndreas Noever nhi_shutdown(nhi); 123468a7a2acSMika Westerberg return res; 1235d6cc51cdSAndreas Noever } 1236d6cc51cdSAndreas Noever pci_set_drvdata(pdev, tb); 123716603153SAndreas Noever 1238b2911a59SMika Westerberg device_wakeup_enable(&pdev->dev); 1239b2911a59SMika Westerberg 12402d8ff0b5SMika Westerberg pm_runtime_allow(&pdev->dev); 12412d8ff0b5SMika Westerberg pm_runtime_set_autosuspend_delay(&pdev->dev, TB_AUTOSUSPEND_DELAY); 12422d8ff0b5SMika Westerberg pm_runtime_use_autosuspend(&pdev->dev); 12432d8ff0b5SMika Westerberg pm_runtime_put_autosuspend(&pdev->dev); 12442d8ff0b5SMika Westerberg 124516603153SAndreas Noever return 0; 124616603153SAndreas Noever } 124716603153SAndreas Noever 124816603153SAndreas Noever static void nhi_remove(struct pci_dev *pdev) 124916603153SAndreas Noever { 1250d6cc51cdSAndreas Noever struct tb *tb = pci_get_drvdata(pdev); 1251d6cc51cdSAndreas Noever struct tb_nhi *nhi = tb->nhi; 12529d3cce0bSMika Westerberg 12532d8ff0b5SMika Westerberg pm_runtime_get_sync(&pdev->dev); 12542d8ff0b5SMika Westerberg pm_runtime_dont_use_autosuspend(&pdev->dev); 12552d8ff0b5SMika Westerberg pm_runtime_forbid(&pdev->dev); 12562d8ff0b5SMika Westerberg 12579d3cce0bSMika Westerberg tb_domain_remove(tb); 125816603153SAndreas Noever nhi_shutdown(nhi); 125916603153SAndreas Noever } 126016603153SAndreas Noever 126123dd5bb4SAndreas Noever /* 126223dd5bb4SAndreas Noever * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable 126323dd5bb4SAndreas Noever * the tunnels asap. A corresponding pci quirk blocks the downstream bridges 126423dd5bb4SAndreas Noever * resume_noirq until we are done. 126523dd5bb4SAndreas Noever */ 126623dd5bb4SAndreas Noever static const struct dev_pm_ops nhi_pm_ops = { 126723dd5bb4SAndreas Noever .suspend_noirq = nhi_suspend_noirq, 126823dd5bb4SAndreas Noever .resume_noirq = nhi_resume_noirq, 1269884e4d57SMika Westerberg .freeze_noirq = nhi_freeze_noirq, /* 127023dd5bb4SAndreas Noever * we just disable hotplug, the 127123dd5bb4SAndreas Noever * pci-tunnels stay alive. 127223dd5bb4SAndreas Noever */ 1273884e4d57SMika Westerberg .thaw_noirq = nhi_thaw_noirq, 127423dd5bb4SAndreas Noever .restore_noirq = nhi_resume_noirq, 1275f67cf491SMika Westerberg .suspend = nhi_suspend, 12763cdb9446SMika Westerberg .poweroff_noirq = nhi_poweroff_noirq, 1277f67cf491SMika Westerberg .poweroff = nhi_suspend, 1278f67cf491SMika Westerberg .complete = nhi_complete, 12792d8ff0b5SMika Westerberg .runtime_suspend = nhi_runtime_suspend, 12802d8ff0b5SMika Westerberg .runtime_resume = nhi_runtime_resume, 128123dd5bb4SAndreas Noever }; 128223dd5bb4SAndreas Noever 1283620863f7SSachin Kamat static struct pci_device_id nhi_ids[] = { 128416603153SAndreas Noever /* 128516603153SAndreas Noever * We have to specify class, the TB bridges use the same device and 12861d111406SLukas Wunner * vendor (sub)id on gen 1 and gen 2 controllers. 128716603153SAndreas Noever */ 128816603153SAndreas Noever { 128916603153SAndreas Noever .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0, 12901d111406SLukas Wunner .vendor = PCI_VENDOR_ID_INTEL, 129119bf4d4fSLukas Wunner .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE, 129219bf4d4fSLukas Wunner .subvendor = 0x2222, .subdevice = 0x1111, 129319bf4d4fSLukas Wunner }, 129419bf4d4fSLukas Wunner { 129519bf4d4fSLukas Wunner .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0, 129619bf4d4fSLukas Wunner .vendor = PCI_VENDOR_ID_INTEL, 12971d111406SLukas Wunner .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C, 129816603153SAndreas Noever .subvendor = 0x2222, .subdevice = 0x1111, 129916603153SAndreas Noever }, 130016603153SAndreas Noever { 130116603153SAndreas Noever .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0, 13021d111406SLukas Wunner .vendor = PCI_VENDOR_ID_INTEL, 130382a6a81cSXavier Gnata .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI, 130482a6a81cSXavier Gnata .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID, 130582a6a81cSXavier Gnata }, 130682a6a81cSXavier Gnata { 130782a6a81cSXavier Gnata .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0, 130882a6a81cSXavier Gnata .vendor = PCI_VENDOR_ID_INTEL, 13091d111406SLukas Wunner .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI, 1310a42fb351SKnuth Posern .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID, 131116603153SAndreas Noever }, 13125e2781bcSMika Westerberg 13135e2781bcSMika Westerberg /* Thunderbolt 3 */ 13145e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) }, 13155e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) }, 13165e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) }, 13175e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) }, 13185e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) }, 13195e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) }, 13205e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) }, 13215e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) }, 13224bac471dSRadion Mirchevsky { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI) }, 13234bac471dSRadion Mirchevsky { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI) }, 13243cdb9446SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI0), 13253cdb9446SMika Westerberg .driver_data = (kernel_ulong_t)&icl_nhi_ops }, 13263cdb9446SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI1), 13273cdb9446SMika Westerberg .driver_data = (kernel_ulong_t)&icl_nhi_ops }, 132857d8df68SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI0), 132957d8df68SMika Westerberg .driver_data = (kernel_ulong_t)&icl_nhi_ops }, 133057d8df68SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI1), 133157d8df68SMika Westerberg .driver_data = (kernel_ulong_t)&icl_nhi_ops }, 1332f6439c53SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI0), 1333f6439c53SMika Westerberg .driver_data = (kernel_ulong_t)&icl_nhi_ops }, 1334f6439c53SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI1), 1335f6439c53SMika Westerberg .driver_data = (kernel_ulong_t)&icl_nhi_ops }, 1336*13579486SAzhar Shaikh { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI0), 1337*13579486SAzhar Shaikh .driver_data = (kernel_ulong_t)&icl_nhi_ops }, 1338*13579486SAzhar Shaikh { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI1), 1339*13579486SAzhar Shaikh .driver_data = (kernel_ulong_t)&icl_nhi_ops }, 13405e2781bcSMika Westerberg 1341b0407983SMika Westerberg /* Any USB4 compliant host */ 1342b0407983SMika Westerberg { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_USB4, ~0) }, 1343b0407983SMika Westerberg 134416603153SAndreas Noever { 0,} 134516603153SAndreas Noever }; 134616603153SAndreas Noever 134716603153SAndreas Noever MODULE_DEVICE_TABLE(pci, nhi_ids); 134816603153SAndreas Noever MODULE_LICENSE("GPL"); 134916603153SAndreas Noever 135016603153SAndreas Noever static struct pci_driver nhi_driver = { 135116603153SAndreas Noever .name = "thunderbolt", 135216603153SAndreas Noever .id_table = nhi_ids, 135316603153SAndreas Noever .probe = nhi_probe, 135416603153SAndreas Noever .remove = nhi_remove, 13554caf2511SMaxim Levitsky .shutdown = nhi_remove, 135623dd5bb4SAndreas Noever .driver.pm = &nhi_pm_ops, 135716603153SAndreas Noever }; 135816603153SAndreas Noever 135916603153SAndreas Noever static int __init nhi_init(void) 136016603153SAndreas Noever { 13619d3cce0bSMika Westerberg int ret; 13629d3cce0bSMika Westerberg 13639d3cce0bSMika Westerberg ret = tb_domain_init(); 13649d3cce0bSMika Westerberg if (ret) 13659d3cce0bSMika Westerberg return ret; 13669d3cce0bSMika Westerberg ret = pci_register_driver(&nhi_driver); 13679d3cce0bSMika Westerberg if (ret) 13689d3cce0bSMika Westerberg tb_domain_exit(); 13699d3cce0bSMika Westerberg return ret; 137016603153SAndreas Noever } 137116603153SAndreas Noever 137216603153SAndreas Noever static void __exit nhi_unload(void) 137316603153SAndreas Noever { 137416603153SAndreas Noever pci_unregister_driver(&nhi_driver); 13759d3cce0bSMika Westerberg tb_domain_exit(); 137616603153SAndreas Noever } 137716603153SAndreas Noever 1378eafa717bSMika Westerberg rootfs_initcall(nhi_init); 137916603153SAndreas Noever module_exit(nhi_unload); 1380