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>
1697486e98SChristophe JAILLET #include <linux/dma-mapping.h>
1716603153SAndreas Noever #include <linux/interrupt.h>
1886eaf4a5SRobin Murphy #include <linux/iommu.h>
1916603153SAndreas Noever #include <linux/module.h>
20cd446ee2SMika Westerberg #include <linux/delay.h>
213cdb9446SMika Westerberg #include <linux/property.h>
2286eaf4a5SRobin Murphy #include <linux/string_helpers.h>
2316603153SAndreas Noever
2416603153SAndreas Noever #include "nhi.h"
2516603153SAndreas Noever #include "nhi_regs.h"
26d6cc51cdSAndreas Noever #include "tb.h"
2716603153SAndreas Noever
2816603153SAndreas Noever #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")
2916603153SAndreas Noever
3053f13319SMika Westerberg #define RING_FIRST_USABLE_HOPID 1
3154669e2fSMika Westerberg /*
3254669e2fSMika Westerberg * Used with QUIRK_E2E to specify an unused HopID the Rx credits are
3354669e2fSMika Westerberg * transferred.
3454669e2fSMika Westerberg */
3554669e2fSMika Westerberg #define RING_E2E_RESERVED_HOPID RING_FIRST_USABLE_HOPID
369fb1e654SMika Westerberg /*
37046bee1fSMika Westerberg * Minimal number of vectors when we use MSI-X. Two for control channel
38046bee1fSMika Westerberg * Rx/Tx and the rest four are for cross domain DMA paths.
39046bee1fSMika Westerberg */
40046bee1fSMika Westerberg #define MSIX_MIN_VECS 6
41046bee1fSMika Westerberg #define MSIX_MAX_VECS 16
4216603153SAndreas Noever
43cd446ee2SMika Westerberg #define NHI_MAILBOX_TIMEOUT 500 /* ms */
44cd446ee2SMika Westerberg
4554669e2fSMika Westerberg /* Host interface quirks */
46e390909aSSanjay R Mehta #define QUIRK_AUTO_CLEAR_INT BIT(0)
4754669e2fSMika Westerberg #define QUIRK_E2E BIT(1)
48e390909aSSanjay R Mehta
490fc70886SMika Westerberg static bool host_reset = true;
500fc70886SMika Westerberg module_param(host_reset, bool, 0444);
510fc70886SMika Westerberg MODULE_PARM_DESC(host_reset, "reset USBv2 host router (default: true)");
520fc70886SMika Westerberg
ring_interrupt_index(const struct tb_ring * ring)531716efdbSMario Limonciello static int ring_interrupt_index(const struct tb_ring *ring)
5416603153SAndreas Noever {
5516603153SAndreas Noever int bit = ring->hop;
5616603153SAndreas Noever if (!ring->is_tx)
5716603153SAndreas Noever bit += ring->nhi->hop_count;
5816603153SAndreas Noever return bit;
5916603153SAndreas Noever }
6016603153SAndreas Noever
nhi_mask_interrupt(struct tb_nhi * nhi,int mask,int ring)61c4af8e3fSMario Limonciello static void nhi_mask_interrupt(struct tb_nhi *nhi, int mask, int ring)
62c4af8e3fSMario Limonciello {
639f9666e6SMika Westerberg if (nhi->quirks & QUIRK_AUTO_CLEAR_INT) {
649f9666e6SMika Westerberg u32 val;
659f9666e6SMika Westerberg
669f9666e6SMika Westerberg val = ioread32(nhi->iobase + REG_RING_INTERRUPT_BASE + ring);
679f9666e6SMika Westerberg iowrite32(val & ~mask, nhi->iobase + REG_RING_INTERRUPT_BASE + ring);
689f9666e6SMika Westerberg } else {
69c4af8e3fSMario Limonciello iowrite32(mask, nhi->iobase + REG_RING_INTERRUPT_MASK_CLEAR_BASE + ring);
70c4af8e3fSMario Limonciello }
719f9666e6SMika Westerberg }
72c4af8e3fSMario Limonciello
nhi_clear_interrupt(struct tb_nhi * nhi,int ring)73c4af8e3fSMario Limonciello static void nhi_clear_interrupt(struct tb_nhi *nhi, int ring)
74c4af8e3fSMario Limonciello {
75c4af8e3fSMario Limonciello if (nhi->quirks & QUIRK_AUTO_CLEAR_INT)
76c4af8e3fSMario Limonciello ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + ring);
77c4af8e3fSMario Limonciello else
78c4af8e3fSMario Limonciello iowrite32(~0, nhi->iobase + REG_RING_INT_CLEAR + ring);
79c4af8e3fSMario Limonciello }
80c4af8e3fSMario Limonciello
81a7bfb27bSLee Jones /*
8216603153SAndreas Noever * ring_interrupt_active() - activate/deactivate interrupts for a single ring
8316603153SAndreas Noever *
8416603153SAndreas Noever * ring->nhi->lock must be held.
8516603153SAndreas Noever */
ring_interrupt_active(struct tb_ring * ring,bool active)8616603153SAndreas Noever static void ring_interrupt_active(struct tb_ring *ring, bool active)
8716603153SAndreas Noever {
88c4af8e3fSMario Limonciello int index = ring_interrupt_index(ring) / 32 * 4;
89c4af8e3fSMario Limonciello int reg = REG_RING_INTERRUPT_BASE + index;
9058cdfe6fSTom Rix int interrupt_bit = ring_interrupt_index(ring) & 31;
9158cdfe6fSTom Rix int mask = 1 << interrupt_bit;
9216603153SAndreas Noever u32 old, new;
93046bee1fSMika Westerberg
94046bee1fSMika Westerberg if (ring->irq > 0) {
95046bee1fSMika Westerberg u32 step, shift, ivr, misc;
96046bee1fSMika Westerberg void __iomem *ivr_base;
9758cdfe6fSTom Rix int auto_clear_bit;
98046bee1fSMika Westerberg int index;
99046bee1fSMika Westerberg
100046bee1fSMika Westerberg if (ring->is_tx)
101046bee1fSMika Westerberg index = ring->hop;
102046bee1fSMika Westerberg else
103046bee1fSMika Westerberg index = ring->hop + ring->nhi->hop_count;
104046bee1fSMika Westerberg
105046bee1fSMika Westerberg /*
106468c49f4SMario Limonciello * Intel routers support a bit that isn't part of
107468c49f4SMario Limonciello * the USB4 spec to ask the hardware to clear
108468c49f4SMario Limonciello * interrupt status bits automatically since
109468c49f4SMario Limonciello * we already know which interrupt was triggered.
110468c49f4SMario Limonciello *
111468c49f4SMario Limonciello * Other routers explicitly disable auto-clear
112468c49f4SMario Limonciello * to prevent conditions that may occur where two
113468c49f4SMario Limonciello * MSIX interrupts are simultaneously active and
114468c49f4SMario Limonciello * reading the register clears both of them.
115046bee1fSMika Westerberg */
116046bee1fSMika Westerberg misc = ioread32(ring->nhi->iobase + REG_DMA_MISC);
117468c49f4SMario Limonciello if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
11858cdfe6fSTom Rix auto_clear_bit = REG_DMA_MISC_INT_AUTO_CLEAR;
119468c49f4SMario Limonciello else
12058cdfe6fSTom Rix auto_clear_bit = REG_DMA_MISC_DISABLE_AUTO_CLEAR;
12158cdfe6fSTom Rix if (!(misc & auto_clear_bit))
12258cdfe6fSTom Rix iowrite32(misc | auto_clear_bit,
12358cdfe6fSTom Rix ring->nhi->iobase + REG_DMA_MISC);
124046bee1fSMika Westerberg
125046bee1fSMika Westerberg ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE;
126046bee1fSMika Westerberg step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
127046bee1fSMika Westerberg shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
128046bee1fSMika Westerberg ivr = ioread32(ivr_base + step);
129046bee1fSMika Westerberg ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift);
130046bee1fSMika Westerberg if (active)
131046bee1fSMika Westerberg ivr |= ring->vector << shift;
132046bee1fSMika Westerberg iowrite32(ivr, ivr_base + step);
133046bee1fSMika Westerberg }
134046bee1fSMika Westerberg
13516603153SAndreas Noever old = ioread32(ring->nhi->iobase + reg);
13616603153SAndreas Noever if (active)
13716603153SAndreas Noever new = old | mask;
13816603153SAndreas Noever else
13916603153SAndreas Noever new = old & ~mask;
14016603153SAndreas Noever
141daa5140fSMika Westerberg dev_dbg(&ring->nhi->pdev->dev,
14216603153SAndreas Noever "%s interrupt at register %#x bit %d (%#x -> %#x)\n",
14358cdfe6fSTom Rix active ? "enabling" : "disabling", reg, interrupt_bit, old, new);
14416603153SAndreas Noever
14516603153SAndreas Noever if (new == old)
14616603153SAndreas Noever dev_WARN(&ring->nhi->pdev->dev,
14716603153SAndreas Noever "interrupt for %s %d is already %s\n",
14816603153SAndreas Noever RING_TYPE(ring), ring->hop,
14916603153SAndreas Noever active ? "enabled" : "disabled");
150c4af8e3fSMario Limonciello
151c4af8e3fSMario Limonciello if (active)
15216603153SAndreas Noever iowrite32(new, ring->nhi->iobase + reg);
153c4af8e3fSMario Limonciello else
154c4af8e3fSMario Limonciello nhi_mask_interrupt(ring->nhi, mask, index);
15516603153SAndreas Noever }
15616603153SAndreas Noever
157a7bfb27bSLee Jones /*
15816603153SAndreas Noever * nhi_disable_interrupts() - disable interrupts for all rings
15916603153SAndreas Noever *
16016603153SAndreas Noever * Use only during init and shutdown.
16116603153SAndreas Noever */
nhi_disable_interrupts(struct tb_nhi * nhi)16216603153SAndreas Noever static void nhi_disable_interrupts(struct tb_nhi *nhi)
16316603153SAndreas Noever {
16416603153SAndreas Noever int i = 0;
16516603153SAndreas Noever /* disable interrupts */
16616603153SAndreas Noever for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++)
167c4af8e3fSMario Limonciello nhi_mask_interrupt(nhi, ~0, 4 * i);
16816603153SAndreas Noever
16916603153SAndreas Noever /* clear interrupt status bits */
17016603153SAndreas Noever for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++)
171c4af8e3fSMario Limonciello nhi_clear_interrupt(nhi, 4 * i);
17216603153SAndreas Noever }
17316603153SAndreas Noever
17416603153SAndreas Noever /* ring helper methods */
17516603153SAndreas Noever
ring_desc_base(struct tb_ring * ring)17616603153SAndreas Noever static void __iomem *ring_desc_base(struct tb_ring *ring)
17716603153SAndreas Noever {
17816603153SAndreas Noever void __iomem *io = ring->nhi->iobase;
17916603153SAndreas Noever io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE;
18016603153SAndreas Noever io += ring->hop * 16;
18116603153SAndreas Noever return io;
18216603153SAndreas Noever }
18316603153SAndreas Noever
ring_options_base(struct tb_ring * ring)18416603153SAndreas Noever static void __iomem *ring_options_base(struct tb_ring *ring)
18516603153SAndreas Noever {
18616603153SAndreas Noever void __iomem *io = ring->nhi->iobase;
18716603153SAndreas Noever io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE;
18816603153SAndreas Noever io += ring->hop * 32;
18916603153SAndreas Noever return io;
19016603153SAndreas Noever }
19116603153SAndreas Noever
ring_iowrite_cons(struct tb_ring * ring,u16 cons)19294379521SMika Westerberg static void ring_iowrite_cons(struct tb_ring *ring, u16 cons)
19316603153SAndreas Noever {
19494379521SMika Westerberg /*
19594379521SMika Westerberg * The other 16-bits in the register is read-only and writes to it
19694379521SMika Westerberg * are ignored by the hardware so we can save one ioread32() by
19794379521SMika Westerberg * filling the read-only bits with zeroes.
19894379521SMika Westerberg */
19994379521SMika Westerberg iowrite32(cons, ring_desc_base(ring) + 8);
20094379521SMika Westerberg }
20194379521SMika Westerberg
ring_iowrite_prod(struct tb_ring * ring,u16 prod)20294379521SMika Westerberg static void ring_iowrite_prod(struct tb_ring *ring, u16 prod)
20394379521SMika Westerberg {
20494379521SMika Westerberg /* See ring_iowrite_cons() above for explanation */
20594379521SMika Westerberg iowrite32(prod << 16, ring_desc_base(ring) + 8);
20616603153SAndreas Noever }
20716603153SAndreas Noever
ring_iowrite32desc(struct tb_ring * ring,u32 value,u32 offset)20816603153SAndreas Noever static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
20916603153SAndreas Noever {
21016603153SAndreas Noever iowrite32(value, ring_desc_base(ring) + offset);
21116603153SAndreas Noever }
21216603153SAndreas Noever
ring_iowrite64desc(struct tb_ring * ring,u64 value,u32 offset)21316603153SAndreas Noever static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
21416603153SAndreas Noever {
21516603153SAndreas Noever iowrite32(value, ring_desc_base(ring) + offset);
21616603153SAndreas Noever iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
21716603153SAndreas Noever }
21816603153SAndreas Noever
ring_iowrite32options(struct tb_ring * ring,u32 value,u32 offset)21916603153SAndreas Noever static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
22016603153SAndreas Noever {
22116603153SAndreas Noever iowrite32(value, ring_options_base(ring) + offset);
22216603153SAndreas Noever }
22316603153SAndreas Noever
ring_full(struct tb_ring * ring)22416603153SAndreas Noever static bool ring_full(struct tb_ring *ring)
22516603153SAndreas Noever {
22616603153SAndreas Noever return ((ring->head + 1) % ring->size) == ring->tail;
22716603153SAndreas Noever }
22816603153SAndreas Noever
ring_empty(struct tb_ring * ring)22916603153SAndreas Noever static bool ring_empty(struct tb_ring *ring)
23016603153SAndreas Noever {
23116603153SAndreas Noever return ring->head == ring->tail;
23216603153SAndreas Noever }
23316603153SAndreas Noever
234a7bfb27bSLee Jones /*
23516603153SAndreas Noever * ring_write_descriptors() - post frames from ring->queue to the controller
23616603153SAndreas Noever *
23716603153SAndreas Noever * ring->lock is held.
23816603153SAndreas Noever */
ring_write_descriptors(struct tb_ring * ring)23916603153SAndreas Noever static void ring_write_descriptors(struct tb_ring *ring)
24016603153SAndreas Noever {
24116603153SAndreas Noever struct ring_frame *frame, *n;
24216603153SAndreas Noever struct ring_desc *descriptor;
24316603153SAndreas Noever list_for_each_entry_safe(frame, n, &ring->queue, list) {
24416603153SAndreas Noever if (ring_full(ring))
24516603153SAndreas Noever break;
24616603153SAndreas Noever list_move_tail(&frame->list, &ring->in_flight);
24716603153SAndreas Noever descriptor = &ring->descriptors[ring->head];
24816603153SAndreas Noever descriptor->phys = frame->buffer_phy;
24916603153SAndreas Noever descriptor->time = 0;
25016603153SAndreas Noever descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT;
25116603153SAndreas Noever if (ring->is_tx) {
25216603153SAndreas Noever descriptor->length = frame->size;
25316603153SAndreas Noever descriptor->eof = frame->eof;
25416603153SAndreas Noever descriptor->sof = frame->sof;
25516603153SAndreas Noever }
25616603153SAndreas Noever ring->head = (ring->head + 1) % ring->size;
25794379521SMika Westerberg if (ring->is_tx)
25894379521SMika Westerberg ring_iowrite_prod(ring, ring->head);
25994379521SMika Westerberg else
26094379521SMika Westerberg ring_iowrite_cons(ring, ring->head);
26116603153SAndreas Noever }
26216603153SAndreas Noever }
26316603153SAndreas Noever
264a7bfb27bSLee Jones /*
26516603153SAndreas Noever * ring_work() - progress completed frames
26616603153SAndreas Noever *
26716603153SAndreas Noever * If the ring is shutting down then all frames are marked as canceled and
26816603153SAndreas Noever * their callbacks are invoked.
26916603153SAndreas Noever *
27016603153SAndreas Noever * Otherwise we collect all completed frame from the ring buffer, write new
27116603153SAndreas Noever * frame to the ring buffer and invoke the callbacks for the completed frames.
27216603153SAndreas Noever */
ring_work(struct work_struct * work)27316603153SAndreas Noever static void ring_work(struct work_struct *work)
27416603153SAndreas Noever {
27516603153SAndreas Noever struct tb_ring *ring = container_of(work, typeof(*ring), work);
27616603153SAndreas Noever struct ring_frame *frame;
27716603153SAndreas Noever bool canceled = false;
27822b7de10SMika Westerberg unsigned long flags;
27916603153SAndreas Noever LIST_HEAD(done);
28022b7de10SMika Westerberg
28122b7de10SMika Westerberg spin_lock_irqsave(&ring->lock, flags);
28216603153SAndreas Noever
28316603153SAndreas Noever if (!ring->running) {
28416603153SAndreas Noever /* Move all frames to done and mark them as canceled. */
28516603153SAndreas Noever list_splice_tail_init(&ring->in_flight, &done);
28616603153SAndreas Noever list_splice_tail_init(&ring->queue, &done);
28716603153SAndreas Noever canceled = true;
28816603153SAndreas Noever goto invoke_callback;
28916603153SAndreas Noever }
29016603153SAndreas Noever
29116603153SAndreas Noever while (!ring_empty(ring)) {
29216603153SAndreas Noever if (!(ring->descriptors[ring->tail].flags
29316603153SAndreas Noever & RING_DESC_COMPLETED))
29416603153SAndreas Noever break;
29516603153SAndreas Noever frame = list_first_entry(&ring->in_flight, typeof(*frame),
29616603153SAndreas Noever list);
29716603153SAndreas Noever list_move_tail(&frame->list, &done);
29816603153SAndreas Noever if (!ring->is_tx) {
29916603153SAndreas Noever frame->size = ring->descriptors[ring->tail].length;
30016603153SAndreas Noever frame->eof = ring->descriptors[ring->tail].eof;
30116603153SAndreas Noever frame->sof = ring->descriptors[ring->tail].sof;
30216603153SAndreas Noever frame->flags = ring->descriptors[ring->tail].flags;
30316603153SAndreas Noever }
30416603153SAndreas Noever ring->tail = (ring->tail + 1) % ring->size;
30516603153SAndreas Noever }
30616603153SAndreas Noever ring_write_descriptors(ring);
30716603153SAndreas Noever
30816603153SAndreas Noever invoke_callback:
30922b7de10SMika Westerberg /* allow callbacks to schedule new work */
31022b7de10SMika Westerberg spin_unlock_irqrestore(&ring->lock, flags);
31116603153SAndreas Noever while (!list_empty(&done)) {
31216603153SAndreas Noever frame = list_first_entry(&done, typeof(*frame), list);
31316603153SAndreas Noever /*
31416603153SAndreas Noever * The callback may reenqueue or delete frame.
31516603153SAndreas Noever * Do not hold on to it.
31616603153SAndreas Noever */
31716603153SAndreas Noever list_del_init(&frame->list);
3184ffe722eSMika Westerberg if (frame->callback)
31916603153SAndreas Noever frame->callback(ring, frame, canceled);
32016603153SAndreas Noever }
32116603153SAndreas Noever }
32216603153SAndreas Noever
__tb_ring_enqueue(struct tb_ring * ring,struct ring_frame * frame)3233b3d9f4dSMika Westerberg int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
32416603153SAndreas Noever {
32522b7de10SMika Westerberg unsigned long flags;
32616603153SAndreas Noever int ret = 0;
32722b7de10SMika Westerberg
32822b7de10SMika Westerberg spin_lock_irqsave(&ring->lock, flags);
32916603153SAndreas Noever if (ring->running) {
33016603153SAndreas Noever list_add_tail(&frame->list, &ring->queue);
33116603153SAndreas Noever ring_write_descriptors(ring);
33216603153SAndreas Noever } else {
33316603153SAndreas Noever ret = -ESHUTDOWN;
33416603153SAndreas Noever }
33522b7de10SMika Westerberg spin_unlock_irqrestore(&ring->lock, flags);
33616603153SAndreas Noever return ret;
33716603153SAndreas Noever }
3383b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(__tb_ring_enqueue);
33916603153SAndreas Noever
3404ffe722eSMika Westerberg /**
3414ffe722eSMika Westerberg * tb_ring_poll() - Poll one completed frame from the ring
3424ffe722eSMika Westerberg * @ring: Ring to poll
3434ffe722eSMika Westerberg *
3444ffe722eSMika Westerberg * This function can be called when @start_poll callback of the @ring
3454ffe722eSMika Westerberg * has been called. It will read one completed frame from the ring and
3464ffe722eSMika Westerberg * return it to the caller. Returns %NULL if there is no more completed
3474ffe722eSMika Westerberg * frames.
3484ffe722eSMika Westerberg */
tb_ring_poll(struct tb_ring * ring)3494ffe722eSMika Westerberg struct ring_frame *tb_ring_poll(struct tb_ring *ring)
3504ffe722eSMika Westerberg {
3514ffe722eSMika Westerberg struct ring_frame *frame = NULL;
3524ffe722eSMika Westerberg unsigned long flags;
3534ffe722eSMika Westerberg
3544ffe722eSMika Westerberg spin_lock_irqsave(&ring->lock, flags);
3554ffe722eSMika Westerberg if (!ring->running)
3564ffe722eSMika Westerberg goto unlock;
3574ffe722eSMika Westerberg if (ring_empty(ring))
3584ffe722eSMika Westerberg goto unlock;
3594ffe722eSMika Westerberg
3604ffe722eSMika Westerberg if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) {
3614ffe722eSMika Westerberg frame = list_first_entry(&ring->in_flight, typeof(*frame),
3624ffe722eSMika Westerberg list);
3634ffe722eSMika Westerberg list_del_init(&frame->list);
3644ffe722eSMika Westerberg
3654ffe722eSMika Westerberg if (!ring->is_tx) {
3664ffe722eSMika Westerberg frame->size = ring->descriptors[ring->tail].length;
3674ffe722eSMika Westerberg frame->eof = ring->descriptors[ring->tail].eof;
3684ffe722eSMika Westerberg frame->sof = ring->descriptors[ring->tail].sof;
3694ffe722eSMika Westerberg frame->flags = ring->descriptors[ring->tail].flags;
3704ffe722eSMika Westerberg }
3714ffe722eSMika Westerberg
3724ffe722eSMika Westerberg ring->tail = (ring->tail + 1) % ring->size;
3734ffe722eSMika Westerberg }
3744ffe722eSMika Westerberg
3754ffe722eSMika Westerberg unlock:
3764ffe722eSMika Westerberg spin_unlock_irqrestore(&ring->lock, flags);
3774ffe722eSMika Westerberg return frame;
3784ffe722eSMika Westerberg }
3794ffe722eSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_poll);
3804ffe722eSMika Westerberg
__ring_interrupt_mask(struct tb_ring * ring,bool mask)3814ffe722eSMika Westerberg static void __ring_interrupt_mask(struct tb_ring *ring, bool mask)
3824ffe722eSMika Westerberg {
3834ffe722eSMika Westerberg int idx = ring_interrupt_index(ring);
3844ffe722eSMika Westerberg int reg = REG_RING_INTERRUPT_BASE + idx / 32 * 4;
3854ffe722eSMika Westerberg int bit = idx % 32;
3864ffe722eSMika Westerberg u32 val;
3874ffe722eSMika Westerberg
3884ffe722eSMika Westerberg val = ioread32(ring->nhi->iobase + reg);
3894ffe722eSMika Westerberg if (mask)
3904ffe722eSMika Westerberg val &= ~BIT(bit);
3914ffe722eSMika Westerberg else
3924ffe722eSMika Westerberg val |= BIT(bit);
3934ffe722eSMika Westerberg iowrite32(val, ring->nhi->iobase + reg);
3944ffe722eSMika Westerberg }
3954ffe722eSMika Westerberg
3964ffe722eSMika Westerberg /* Both @nhi->lock and @ring->lock should be held */
__ring_interrupt(struct tb_ring * ring)3974ffe722eSMika Westerberg static void __ring_interrupt(struct tb_ring *ring)
3984ffe722eSMika Westerberg {
3994ffe722eSMika Westerberg if (!ring->running)
4004ffe722eSMika Westerberg return;
4014ffe722eSMika Westerberg
4024ffe722eSMika Westerberg if (ring->start_poll) {
40374657181SMika Westerberg __ring_interrupt_mask(ring, true);
4044ffe722eSMika Westerberg ring->start_poll(ring->poll_data);
4054ffe722eSMika Westerberg } else {
4064ffe722eSMika Westerberg schedule_work(&ring->work);
4074ffe722eSMika Westerberg }
4084ffe722eSMika Westerberg }
4094ffe722eSMika Westerberg
4104ffe722eSMika Westerberg /**
4114ffe722eSMika Westerberg * tb_ring_poll_complete() - Re-start interrupt for the ring
4124ffe722eSMika Westerberg * @ring: Ring to re-start the interrupt
4134ffe722eSMika Westerberg *
4144ffe722eSMika Westerberg * This will re-start (unmask) the ring interrupt once the user is done
4154ffe722eSMika Westerberg * with polling.
4164ffe722eSMika Westerberg */
tb_ring_poll_complete(struct tb_ring * ring)4174ffe722eSMika Westerberg void tb_ring_poll_complete(struct tb_ring *ring)
4184ffe722eSMika Westerberg {
4194ffe722eSMika Westerberg unsigned long flags;
4204ffe722eSMika Westerberg
4214ffe722eSMika Westerberg spin_lock_irqsave(&ring->nhi->lock, flags);
4224ffe722eSMika Westerberg spin_lock(&ring->lock);
4234ffe722eSMika Westerberg if (ring->start_poll)
4244ffe722eSMika Westerberg __ring_interrupt_mask(ring, false);
4254ffe722eSMika Westerberg spin_unlock(&ring->lock);
4264ffe722eSMika Westerberg spin_unlock_irqrestore(&ring->nhi->lock, flags);
4274ffe722eSMika Westerberg }
4284ffe722eSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_poll_complete);
4294ffe722eSMika Westerberg
ring_clear_msix(const struct tb_ring * ring)4307a1808f8SSanjay R Mehta static void ring_clear_msix(const struct tb_ring *ring)
4317a1808f8SSanjay R Mehta {
432468c49f4SMario Limonciello int bit;
433468c49f4SMario Limonciello
4347a1808f8SSanjay R Mehta if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
4357a1808f8SSanjay R Mehta return;
4367a1808f8SSanjay R Mehta
437468c49f4SMario Limonciello bit = ring_interrupt_index(ring) & 31;
4387a1808f8SSanjay R Mehta if (ring->is_tx)
439468c49f4SMario Limonciello iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR);
4407a1808f8SSanjay R Mehta else
441468c49f4SMario Limonciello iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR +
4427a1808f8SSanjay R Mehta 4 * (ring->nhi->hop_count / 32));
4437a1808f8SSanjay R Mehta }
4447a1808f8SSanjay R Mehta
ring_msix(int irq,void * data)445046bee1fSMika Westerberg static irqreturn_t ring_msix(int irq, void *data)
446046bee1fSMika Westerberg {
447046bee1fSMika Westerberg struct tb_ring *ring = data;
448046bee1fSMika Westerberg
4494ffe722eSMika Westerberg spin_lock(&ring->nhi->lock);
4507a1808f8SSanjay R Mehta ring_clear_msix(ring);
4514ffe722eSMika Westerberg spin_lock(&ring->lock);
4524ffe722eSMika Westerberg __ring_interrupt(ring);
4534ffe722eSMika Westerberg spin_unlock(&ring->lock);
4544ffe722eSMika Westerberg spin_unlock(&ring->nhi->lock);
4554ffe722eSMika Westerberg
456046bee1fSMika Westerberg return IRQ_HANDLED;
457046bee1fSMika Westerberg }
458046bee1fSMika Westerberg
ring_request_msix(struct tb_ring * ring,bool no_suspend)459046bee1fSMika Westerberg static int ring_request_msix(struct tb_ring *ring, bool no_suspend)
460046bee1fSMika Westerberg {
461046bee1fSMika Westerberg struct tb_nhi *nhi = ring->nhi;
462046bee1fSMika Westerberg unsigned long irqflags;
463046bee1fSMika Westerberg int ret;
464046bee1fSMika Westerberg
465046bee1fSMika Westerberg if (!nhi->pdev->msix_enabled)
466046bee1fSMika Westerberg return 0;
467046bee1fSMika Westerberg
468046bee1fSMika Westerberg ret = ida_simple_get(&nhi->msix_ida, 0, MSIX_MAX_VECS, GFP_KERNEL);
469046bee1fSMika Westerberg if (ret < 0)
470046bee1fSMika Westerberg return ret;
471046bee1fSMika Westerberg
472046bee1fSMika Westerberg ring->vector = ret;
473046bee1fSMika Westerberg
4747342ca34SJing Xiangfeng ret = pci_irq_vector(ring->nhi->pdev, ring->vector);
4757342ca34SJing Xiangfeng if (ret < 0)
4767342ca34SJing Xiangfeng goto err_ida_remove;
4777342ca34SJing Xiangfeng
4787342ca34SJing Xiangfeng ring->irq = ret;
479046bee1fSMika Westerberg
480046bee1fSMika Westerberg irqflags = no_suspend ? IRQF_NO_SUSPEND : 0;
4817342ca34SJing Xiangfeng ret = request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring);
4827342ca34SJing Xiangfeng if (ret)
4837342ca34SJing Xiangfeng goto err_ida_remove;
4847342ca34SJing Xiangfeng
4857342ca34SJing Xiangfeng return 0;
4867342ca34SJing Xiangfeng
4877342ca34SJing Xiangfeng err_ida_remove:
4887342ca34SJing Xiangfeng ida_simple_remove(&nhi->msix_ida, ring->vector);
4897342ca34SJing Xiangfeng
4907342ca34SJing Xiangfeng return ret;
491046bee1fSMika Westerberg }
492046bee1fSMika Westerberg
ring_release_msix(struct tb_ring * ring)493046bee1fSMika Westerberg static void ring_release_msix(struct tb_ring *ring)
494046bee1fSMika Westerberg {
495046bee1fSMika Westerberg if (ring->irq <= 0)
496046bee1fSMika Westerberg return;
497046bee1fSMika Westerberg
498046bee1fSMika Westerberg free_irq(ring->irq, ring);
499046bee1fSMika Westerberg ida_simple_remove(&ring->nhi->msix_ida, ring->vector);
500046bee1fSMika Westerberg ring->vector = 0;
501046bee1fSMika Westerberg ring->irq = 0;
502046bee1fSMika Westerberg }
503046bee1fSMika Westerberg
nhi_alloc_hop(struct tb_nhi * nhi,struct tb_ring * ring)5049a01c7c2SMika Westerberg static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring)
5059a01c7c2SMika Westerberg {
50654669e2fSMika Westerberg unsigned int start_hop = RING_FIRST_USABLE_HOPID;
5079a01c7c2SMika Westerberg int ret = 0;
5089a01c7c2SMika Westerberg
50954669e2fSMika Westerberg if (nhi->quirks & QUIRK_E2E) {
51054669e2fSMika Westerberg start_hop = RING_FIRST_USABLE_HOPID + 1;
51154669e2fSMika Westerberg if (ring->flags & RING_FLAG_E2E && !ring->is_tx) {
51254669e2fSMika Westerberg dev_dbg(&nhi->pdev->dev, "quirking E2E TX HopID %u -> %u\n",
51354669e2fSMika Westerberg ring->e2e_tx_hop, RING_E2E_RESERVED_HOPID);
51454669e2fSMika Westerberg ring->e2e_tx_hop = RING_E2E_RESERVED_HOPID;
51554669e2fSMika Westerberg }
51654669e2fSMika Westerberg }
51754669e2fSMika Westerberg
5189a01c7c2SMika Westerberg spin_lock_irq(&nhi->lock);
5199a01c7c2SMika Westerberg
5209a01c7c2SMika Westerberg if (ring->hop < 0) {
5219a01c7c2SMika Westerberg unsigned int i;
5229a01c7c2SMika Westerberg
5239a01c7c2SMika Westerberg /*
5249a01c7c2SMika Westerberg * Automatically allocate HopID from the non-reserved
52553f13319SMika Westerberg * range 1 .. hop_count - 1.
5269a01c7c2SMika Westerberg */
52754669e2fSMika Westerberg for (i = start_hop; i < nhi->hop_count; i++) {
5289a01c7c2SMika Westerberg if (ring->is_tx) {
5299a01c7c2SMika Westerberg if (!nhi->tx_rings[i]) {
5309a01c7c2SMika Westerberg ring->hop = i;
5319a01c7c2SMika Westerberg break;
5329a01c7c2SMika Westerberg }
5339a01c7c2SMika Westerberg } else {
5349a01c7c2SMika Westerberg if (!nhi->rx_rings[i]) {
5359a01c7c2SMika Westerberg ring->hop = i;
5369a01c7c2SMika Westerberg break;
5379a01c7c2SMika Westerberg }
5389a01c7c2SMika Westerberg }
5399a01c7c2SMika Westerberg }
5409a01c7c2SMika Westerberg }
5419a01c7c2SMika Westerberg
54254669e2fSMika Westerberg if (ring->hop > 0 && ring->hop < start_hop) {
54354669e2fSMika Westerberg dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
54454669e2fSMika Westerberg ret = -EINVAL;
54554669e2fSMika Westerberg goto err_unlock;
54654669e2fSMika Westerberg }
5479a01c7c2SMika Westerberg if (ring->hop < 0 || ring->hop >= nhi->hop_count) {
5489a01c7c2SMika Westerberg dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
5499a01c7c2SMika Westerberg ret = -EINVAL;
5509a01c7c2SMika Westerberg goto err_unlock;
5519a01c7c2SMika Westerberg }
5529a01c7c2SMika Westerberg if (ring->is_tx && nhi->tx_rings[ring->hop]) {
5539a01c7c2SMika Westerberg dev_warn(&nhi->pdev->dev, "TX hop %d already allocated\n",
5549a01c7c2SMika Westerberg ring->hop);
5559a01c7c2SMika Westerberg ret = -EBUSY;
5569a01c7c2SMika Westerberg goto err_unlock;
5574e99c98eSAndy Shevchenko }
5584e99c98eSAndy Shevchenko if (!ring->is_tx && nhi->rx_rings[ring->hop]) {
5599a01c7c2SMika Westerberg dev_warn(&nhi->pdev->dev, "RX hop %d already allocated\n",
5609a01c7c2SMika Westerberg ring->hop);
5619a01c7c2SMika Westerberg ret = -EBUSY;
5629a01c7c2SMika Westerberg goto err_unlock;
5639a01c7c2SMika Westerberg }
5649a01c7c2SMika Westerberg
5659a01c7c2SMika Westerberg if (ring->is_tx)
5669a01c7c2SMika Westerberg nhi->tx_rings[ring->hop] = ring;
5679a01c7c2SMika Westerberg else
5689a01c7c2SMika Westerberg nhi->rx_rings[ring->hop] = ring;
5699a01c7c2SMika Westerberg
5709a01c7c2SMika Westerberg err_unlock:
5719a01c7c2SMika Westerberg spin_unlock_irq(&nhi->lock);
5729a01c7c2SMika Westerberg
5739a01c7c2SMika Westerberg return ret;
5749a01c7c2SMika Westerberg }
5759a01c7c2SMika Westerberg
tb_ring_alloc(struct tb_nhi * nhi,u32 hop,int size,bool transmit,unsigned int flags,int e2e_tx_hop,u16 sof_mask,u16 eof_mask,void (* start_poll)(void *),void * poll_data)5763b3d9f4dSMika Westerberg static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
5779fb1e654SMika Westerberg bool transmit, unsigned int flags,
578afe704a2SMika Westerberg int e2e_tx_hop, u16 sof_mask, u16 eof_mask,
5794ffe722eSMika Westerberg void (*start_poll)(void *),
5804ffe722eSMika Westerberg void *poll_data)
58116603153SAndreas Noever {
58216603153SAndreas Noever struct tb_ring *ring = NULL;
583daa5140fSMika Westerberg
584daa5140fSMika Westerberg dev_dbg(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
58516603153SAndreas Noever transmit ? "TX" : "RX", hop, size);
58616603153SAndreas Noever
58716603153SAndreas Noever ring = kzalloc(sizeof(*ring), GFP_KERNEL);
58816603153SAndreas Noever if (!ring)
58959120e06SMika Westerberg return NULL;
59016603153SAndreas Noever
59122b7de10SMika Westerberg spin_lock_init(&ring->lock);
59216603153SAndreas Noever INIT_LIST_HEAD(&ring->queue);
59316603153SAndreas Noever INIT_LIST_HEAD(&ring->in_flight);
59416603153SAndreas Noever INIT_WORK(&ring->work, ring_work);
59516603153SAndreas Noever
59616603153SAndreas Noever ring->nhi = nhi;
59716603153SAndreas Noever ring->hop = hop;
59816603153SAndreas Noever ring->is_tx = transmit;
59916603153SAndreas Noever ring->size = size;
600046bee1fSMika Westerberg ring->flags = flags;
601afe704a2SMika Westerberg ring->e2e_tx_hop = e2e_tx_hop;
6029fb1e654SMika Westerberg ring->sof_mask = sof_mask;
6039fb1e654SMika Westerberg ring->eof_mask = eof_mask;
60416603153SAndreas Noever ring->head = 0;
60516603153SAndreas Noever ring->tail = 0;
60616603153SAndreas Noever ring->running = false;
6074ffe722eSMika Westerberg ring->start_poll = start_poll;
6084ffe722eSMika Westerberg ring->poll_data = poll_data;
609046bee1fSMika Westerberg
61016603153SAndreas Noever ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
61116603153SAndreas Noever size * sizeof(*ring->descriptors),
61216603153SAndreas Noever &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
61316603153SAndreas Noever if (!ring->descriptors)
61459120e06SMika Westerberg goto err_free_ring;
61516603153SAndreas Noever
61659120e06SMika Westerberg if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND))
61759120e06SMika Westerberg goto err_free_descs;
61859120e06SMika Westerberg
6199a01c7c2SMika Westerberg if (nhi_alloc_hop(nhi, ring))
62059120e06SMika Westerberg goto err_release_msix;
62159120e06SMika Westerberg
62216603153SAndreas Noever return ring;
62316603153SAndreas Noever
62459120e06SMika Westerberg err_release_msix:
62559120e06SMika Westerberg ring_release_msix(ring);
62659120e06SMika Westerberg err_free_descs:
62759120e06SMika Westerberg dma_free_coherent(&ring->nhi->pdev->dev,
62859120e06SMika Westerberg ring->size * sizeof(*ring->descriptors),
62959120e06SMika Westerberg ring->descriptors, ring->descriptors_dma);
63059120e06SMika Westerberg err_free_ring:
63116603153SAndreas Noever kfree(ring);
63259120e06SMika Westerberg
63316603153SAndreas Noever return NULL;
63416603153SAndreas Noever }
63516603153SAndreas Noever
6363b3d9f4dSMika Westerberg /**
6373b3d9f4dSMika Westerberg * tb_ring_alloc_tx() - Allocate DMA ring for transmit
6383b3d9f4dSMika Westerberg * @nhi: Pointer to the NHI the ring is to be allocated
6393b3d9f4dSMika Westerberg * @hop: HopID (ring) to allocate
6403b3d9f4dSMika Westerberg * @size: Number of entries in the ring
6413b3d9f4dSMika Westerberg * @flags: Flags for the ring
6423b3d9f4dSMika Westerberg */
tb_ring_alloc_tx(struct tb_nhi * nhi,int hop,int size,unsigned int flags)6433b3d9f4dSMika Westerberg struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
644046bee1fSMika Westerberg unsigned int flags)
64516603153SAndreas Noever {
646afe704a2SMika Westerberg return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0, 0, NULL, NULL);
64716603153SAndreas Noever }
6483b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_alloc_tx);
64916603153SAndreas Noever
65016603153SAndreas Noever /**
6513b3d9f4dSMika Westerberg * tb_ring_alloc_rx() - Allocate DMA ring for receive
6523b3d9f4dSMika Westerberg * @nhi: Pointer to the NHI the ring is to be allocated
6539a01c7c2SMika Westerberg * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
6543b3d9f4dSMika Westerberg * @size: Number of entries in the ring
6553b3d9f4dSMika Westerberg * @flags: Flags for the ring
656afe704a2SMika Westerberg * @e2e_tx_hop: Transmit HopID when E2E is enabled in @flags
6573b3d9f4dSMika Westerberg * @sof_mask: Mask of PDF values that start a frame
6583b3d9f4dSMika Westerberg * @eof_mask: Mask of PDF values that end a frame
6594ffe722eSMika Westerberg * @start_poll: If not %NULL the ring will call this function when an
6604ffe722eSMika Westerberg * interrupt is triggered and masked, instead of callback
6614ffe722eSMika Westerberg * in each Rx frame.
6624ffe722eSMika Westerberg * @poll_data: Optional data passed to @start_poll
66316603153SAndreas Noever */
tb_ring_alloc_rx(struct tb_nhi * nhi,int hop,int size,unsigned int flags,int e2e_tx_hop,u16 sof_mask,u16 eof_mask,void (* start_poll)(void *),void * poll_data)6643b3d9f4dSMika Westerberg struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
665afe704a2SMika Westerberg unsigned int flags, int e2e_tx_hop,
666afe704a2SMika Westerberg u16 sof_mask, u16 eof_mask,
6674ffe722eSMika Westerberg void (*start_poll)(void *), void *poll_data)
6683b3d9f4dSMika Westerberg {
669afe704a2SMika Westerberg return tb_ring_alloc(nhi, hop, size, false, flags, e2e_tx_hop, sof_mask, eof_mask,
6704ffe722eSMika Westerberg start_poll, poll_data);
6713b3d9f4dSMika Westerberg }
6723b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_alloc_rx);
6733b3d9f4dSMika Westerberg
6743b3d9f4dSMika Westerberg /**
6753b3d9f4dSMika Westerberg * tb_ring_start() - enable a ring
6766894bd37SMika Westerberg * @ring: Ring to start
6773b3d9f4dSMika Westerberg *
6783b3d9f4dSMika Westerberg * Must not be invoked in parallel with tb_ring_stop().
6793b3d9f4dSMika Westerberg */
tb_ring_start(struct tb_ring * ring)6803b3d9f4dSMika Westerberg void tb_ring_start(struct tb_ring *ring)
68116603153SAndreas Noever {
6829fb1e654SMika Westerberg u16 frame_size;
6839fb1e654SMika Westerberg u32 flags;
6849fb1e654SMika Westerberg
68559120e06SMika Westerberg spin_lock_irq(&ring->nhi->lock);
68659120e06SMika Westerberg spin_lock(&ring->lock);
687bdccf295SMika Westerberg if (ring->nhi->going_away)
688bdccf295SMika Westerberg goto err;
68916603153SAndreas Noever if (ring->running) {
69016603153SAndreas Noever dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
69116603153SAndreas Noever goto err;
69216603153SAndreas Noever }
693daa5140fSMika Westerberg dev_dbg(&ring->nhi->pdev->dev, "starting %s %d\n",
69416603153SAndreas Noever RING_TYPE(ring), ring->hop);
69516603153SAndreas Noever
6969fb1e654SMika Westerberg if (ring->flags & RING_FLAG_FRAME) {
6979fb1e654SMika Westerberg /* Means 4096 */
6989fb1e654SMika Westerberg frame_size = 0;
6999fb1e654SMika Westerberg flags = RING_FLAG_ENABLE;
7009fb1e654SMika Westerberg } else {
7019fb1e654SMika Westerberg frame_size = TB_FRAME_SIZE;
7029fb1e654SMika Westerberg flags = RING_FLAG_ENABLE | RING_FLAG_RAW;
7039fb1e654SMika Westerberg }
7049fb1e654SMika Westerberg
70516603153SAndreas Noever ring_iowrite64desc(ring, ring->descriptors_dma, 0);
70616603153SAndreas Noever if (ring->is_tx) {
70716603153SAndreas Noever ring_iowrite32desc(ring, ring->size, 12);
70816603153SAndreas Noever ring_iowrite32options(ring, 0, 4); /* time releated ? */
7099fb1e654SMika Westerberg ring_iowrite32options(ring, flags, 0);
71016603153SAndreas Noever } else {
7119fb1e654SMika Westerberg u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask;
7129fb1e654SMika Westerberg
7139fb1e654SMika Westerberg ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12);
7149fb1e654SMika Westerberg ring_iowrite32options(ring, sof_eof_mask, 4);
7159fb1e654SMika Westerberg ring_iowrite32options(ring, flags, 0);
71616603153SAndreas Noever }
717afe704a2SMika Westerberg
718afe704a2SMika Westerberg /*
719afe704a2SMika Westerberg * Now that the ring valid bit is set we can configure E2E if
720afe704a2SMika Westerberg * enabled for the ring.
721afe704a2SMika Westerberg */
722afe704a2SMika Westerberg if (ring->flags & RING_FLAG_E2E) {
723afe704a2SMika Westerberg if (!ring->is_tx) {
724afe704a2SMika Westerberg u32 hop;
725afe704a2SMika Westerberg
726afe704a2SMika Westerberg hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT;
727afe704a2SMika Westerberg hop &= REG_RX_OPTIONS_E2E_HOP_MASK;
728afe704a2SMika Westerberg flags |= hop;
729afe704a2SMika Westerberg
730afe704a2SMika Westerberg dev_dbg(&ring->nhi->pdev->dev,
731afe704a2SMika Westerberg "enabling E2E for %s %d with TX HopID %d\n",
732afe704a2SMika Westerberg RING_TYPE(ring), ring->hop, ring->e2e_tx_hop);
733afe704a2SMika Westerberg } else {
734afe704a2SMika Westerberg dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d\n",
735afe704a2SMika Westerberg RING_TYPE(ring), ring->hop);
736afe704a2SMika Westerberg }
737afe704a2SMika Westerberg
738afe704a2SMika Westerberg flags |= RING_FLAG_E2E_FLOW_CONTROL;
739afe704a2SMika Westerberg ring_iowrite32options(ring, flags, 0);
740afe704a2SMika Westerberg }
741afe704a2SMika Westerberg
74216603153SAndreas Noever ring_interrupt_active(ring, true);
74316603153SAndreas Noever ring->running = true;
74416603153SAndreas Noever err:
74559120e06SMika Westerberg spin_unlock(&ring->lock);
74659120e06SMika Westerberg spin_unlock_irq(&ring->nhi->lock);
74716603153SAndreas Noever }
7483b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_start);
74916603153SAndreas Noever
75016603153SAndreas Noever /**
7513b3d9f4dSMika Westerberg * tb_ring_stop() - shutdown a ring
7526894bd37SMika Westerberg * @ring: Ring to stop
75316603153SAndreas Noever *
75416603153SAndreas Noever * Must not be invoked from a callback.
75516603153SAndreas Noever *
7563b3d9f4dSMika Westerberg * This method will disable the ring. Further calls to
7573b3d9f4dSMika Westerberg * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been
7583b3d9f4dSMika Westerberg * called.
75916603153SAndreas Noever *
76016603153SAndreas Noever * All enqueued frames will be canceled and their callbacks will be executed
76116603153SAndreas Noever * with frame->canceled set to true (on the callback thread). This method
76216603153SAndreas Noever * returns only after all callback invocations have finished.
76316603153SAndreas Noever */
tb_ring_stop(struct tb_ring * ring)7643b3d9f4dSMika Westerberg void tb_ring_stop(struct tb_ring *ring)
76516603153SAndreas Noever {
76659120e06SMika Westerberg spin_lock_irq(&ring->nhi->lock);
76759120e06SMika Westerberg spin_lock(&ring->lock);
768daa5140fSMika Westerberg dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n",
76916603153SAndreas Noever RING_TYPE(ring), ring->hop);
770bdccf295SMika Westerberg if (ring->nhi->going_away)
771bdccf295SMika Westerberg goto err;
77216603153SAndreas Noever if (!ring->running) {
77316603153SAndreas Noever dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
77416603153SAndreas Noever RING_TYPE(ring), ring->hop);
77516603153SAndreas Noever goto err;
77616603153SAndreas Noever }
77716603153SAndreas Noever ring_interrupt_active(ring, false);
77816603153SAndreas Noever
77916603153SAndreas Noever ring_iowrite32options(ring, 0, 0);
78016603153SAndreas Noever ring_iowrite64desc(ring, 0, 0);
78194379521SMika Westerberg ring_iowrite32desc(ring, 0, 8);
78216603153SAndreas Noever ring_iowrite32desc(ring, 0, 12);
78316603153SAndreas Noever ring->head = 0;
78416603153SAndreas Noever ring->tail = 0;
78516603153SAndreas Noever ring->running = false;
78616603153SAndreas Noever
78716603153SAndreas Noever err:
78859120e06SMika Westerberg spin_unlock(&ring->lock);
78959120e06SMika Westerberg spin_unlock_irq(&ring->nhi->lock);
79016603153SAndreas Noever
79116603153SAndreas Noever /*
79216603153SAndreas Noever * schedule ring->work to invoke callbacks on all remaining frames.
79316603153SAndreas Noever */
79416603153SAndreas Noever schedule_work(&ring->work);
79516603153SAndreas Noever flush_work(&ring->work);
79616603153SAndreas Noever }
7973b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_stop);
79816603153SAndreas Noever
79916603153SAndreas Noever /*
8003b3d9f4dSMika Westerberg * tb_ring_free() - free ring
80116603153SAndreas Noever *
80216603153SAndreas Noever * When this method returns all invocations of ring->callback will have
80316603153SAndreas Noever * finished.
80416603153SAndreas Noever *
80516603153SAndreas Noever * Ring must be stopped.
80616603153SAndreas Noever *
80716603153SAndreas Noever * Must NOT be called from ring_frame->callback!
80816603153SAndreas Noever */
tb_ring_free(struct tb_ring * ring)8093b3d9f4dSMika Westerberg void tb_ring_free(struct tb_ring *ring)
81016603153SAndreas Noever {
81159120e06SMika Westerberg spin_lock_irq(&ring->nhi->lock);
81216603153SAndreas Noever /*
81316603153SAndreas Noever * Dissociate the ring from the NHI. This also ensures that
81416603153SAndreas Noever * nhi_interrupt_work cannot reschedule ring->work.
81516603153SAndreas Noever */
81616603153SAndreas Noever if (ring->is_tx)
81716603153SAndreas Noever ring->nhi->tx_rings[ring->hop] = NULL;
81816603153SAndreas Noever else
81916603153SAndreas Noever ring->nhi->rx_rings[ring->hop] = NULL;
82016603153SAndreas Noever
82116603153SAndreas Noever if (ring->running) {
82216603153SAndreas Noever dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
82316603153SAndreas Noever RING_TYPE(ring), ring->hop);
82416603153SAndreas Noever }
8254ffe722eSMika Westerberg spin_unlock_irq(&ring->nhi->lock);
82616603153SAndreas Noever
827046bee1fSMika Westerberg ring_release_msix(ring);
828046bee1fSMika Westerberg
82916603153SAndreas Noever dma_free_coherent(&ring->nhi->pdev->dev,
83016603153SAndreas Noever ring->size * sizeof(*ring->descriptors),
83116603153SAndreas Noever ring->descriptors, ring->descriptors_dma);
83216603153SAndreas Noever
833f19b72c6SSachin Kamat ring->descriptors = NULL;
83416603153SAndreas Noever ring->descriptors_dma = 0;
83516603153SAndreas Noever
83616603153SAndreas Noever
837daa5140fSMika Westerberg dev_dbg(&ring->nhi->pdev->dev, "freeing %s %d\n", RING_TYPE(ring),
83816603153SAndreas Noever ring->hop);
83916603153SAndreas Noever
840a7bfb27bSLee Jones /*
841046bee1fSMika Westerberg * ring->work can no longer be scheduled (it is scheduled only
842046bee1fSMika Westerberg * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
843046bee1fSMika Westerberg * to finish before freeing the ring.
84416603153SAndreas Noever */
84516603153SAndreas Noever flush_work(&ring->work);
84616603153SAndreas Noever kfree(ring);
84716603153SAndreas Noever }
8483b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_free);
84916603153SAndreas Noever
850cd446ee2SMika Westerberg /**
851cd446ee2SMika Westerberg * nhi_mailbox_cmd() - Send a command through NHI mailbox
852cd446ee2SMika Westerberg * @nhi: Pointer to the NHI structure
853cd446ee2SMika Westerberg * @cmd: Command to send
854cd446ee2SMika Westerberg * @data: Data to be send with the command
855cd446ee2SMika Westerberg *
856cd446ee2SMika Westerberg * Sends mailbox command to the firmware running on NHI. Returns %0 in
857cd446ee2SMika Westerberg * case of success and negative errno in case of failure.
858cd446ee2SMika Westerberg */
nhi_mailbox_cmd(struct tb_nhi * nhi,enum nhi_mailbox_cmd cmd,u32 data)859cd446ee2SMika Westerberg int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
860cd446ee2SMika Westerberg {
861cd446ee2SMika Westerberg ktime_t timeout;
862cd446ee2SMika Westerberg u32 val;
863cd446ee2SMika Westerberg
864cd446ee2SMika Westerberg iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
865cd446ee2SMika Westerberg
866cd446ee2SMika Westerberg val = ioread32(nhi->iobase + REG_INMAIL_CMD);
867cd446ee2SMika Westerberg val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
868cd446ee2SMika Westerberg val |= REG_INMAIL_OP_REQUEST | cmd;
869cd446ee2SMika Westerberg iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
870cd446ee2SMika Westerberg
871cd446ee2SMika Westerberg timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT);
872cd446ee2SMika Westerberg do {
873cd446ee2SMika Westerberg val = ioread32(nhi->iobase + REG_INMAIL_CMD);
874cd446ee2SMika Westerberg if (!(val & REG_INMAIL_OP_REQUEST))
875cd446ee2SMika Westerberg break;
876cd446ee2SMika Westerberg usleep_range(10, 20);
877cd446ee2SMika Westerberg } while (ktime_before(ktime_get(), timeout));
878cd446ee2SMika Westerberg
879cd446ee2SMika Westerberg if (val & REG_INMAIL_OP_REQUEST)
880cd446ee2SMika Westerberg return -ETIMEDOUT;
881cd446ee2SMika Westerberg if (val & REG_INMAIL_ERROR)
882cd446ee2SMika Westerberg return -EIO;
883cd446ee2SMika Westerberg
884cd446ee2SMika Westerberg return 0;
885cd446ee2SMika Westerberg }
886cd446ee2SMika Westerberg
887cd446ee2SMika Westerberg /**
888cd446ee2SMika Westerberg * nhi_mailbox_mode() - Return current firmware operation mode
889cd446ee2SMika Westerberg * @nhi: Pointer to the NHI structure
890cd446ee2SMika Westerberg *
891cd446ee2SMika Westerberg * The function reads current firmware operation mode using NHI mailbox
892cd446ee2SMika Westerberg * registers and returns it to the caller.
893cd446ee2SMika Westerberg */
nhi_mailbox_mode(struct tb_nhi * nhi)894cd446ee2SMika Westerberg enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
895cd446ee2SMika Westerberg {
896cd446ee2SMika Westerberg u32 val;
897cd446ee2SMika Westerberg
898cd446ee2SMika Westerberg val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
899cd446ee2SMika Westerberg val &= REG_OUTMAIL_CMD_OPMODE_MASK;
900cd446ee2SMika Westerberg val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
901cd446ee2SMika Westerberg
902cd446ee2SMika Westerberg return (enum nhi_fw_mode)val;
903cd446ee2SMika Westerberg }
904cd446ee2SMika Westerberg
nhi_interrupt_work(struct work_struct * work)90516603153SAndreas Noever static void nhi_interrupt_work(struct work_struct *work)
90616603153SAndreas Noever {
90716603153SAndreas Noever struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
90816603153SAndreas Noever int value = 0; /* Suppress uninitialized usage warning. */
90916603153SAndreas Noever int bit;
91016603153SAndreas Noever int hop = -1;
91116603153SAndreas Noever int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
91216603153SAndreas Noever struct tb_ring *ring;
91316603153SAndreas Noever
91459120e06SMika Westerberg spin_lock_irq(&nhi->lock);
91516603153SAndreas Noever
91616603153SAndreas Noever /*
91716603153SAndreas Noever * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
91816603153SAndreas Noever * (TX, RX, RX overflow). We iterate over the bits and read a new
91916603153SAndreas Noever * dwords as required. The registers are cleared on read.
92016603153SAndreas Noever */
92116603153SAndreas Noever for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
92216603153SAndreas Noever if (bit % 32 == 0)
92316603153SAndreas Noever value = ioread32(nhi->iobase
92416603153SAndreas Noever + REG_RING_NOTIFY_BASE
92516603153SAndreas Noever + 4 * (bit / 32));
92616603153SAndreas Noever if (++hop == nhi->hop_count) {
92716603153SAndreas Noever hop = 0;
92816603153SAndreas Noever type++;
92916603153SAndreas Noever }
93016603153SAndreas Noever if ((value & (1 << (bit % 32))) == 0)
93116603153SAndreas Noever continue;
93216603153SAndreas Noever if (type == 2) {
93316603153SAndreas Noever dev_warn(&nhi->pdev->dev,
93416603153SAndreas Noever "RX overflow for ring %d\n",
93516603153SAndreas Noever hop);
93616603153SAndreas Noever continue;
93716603153SAndreas Noever }
93816603153SAndreas Noever if (type == 0)
93916603153SAndreas Noever ring = nhi->tx_rings[hop];
94016603153SAndreas Noever else
94116603153SAndreas Noever ring = nhi->rx_rings[hop];
94216603153SAndreas Noever if (ring == NULL) {
94316603153SAndreas Noever dev_warn(&nhi->pdev->dev,
94416603153SAndreas Noever "got interrupt for inactive %s ring %d\n",
94516603153SAndreas Noever type ? "RX" : "TX",
94616603153SAndreas Noever hop);
94716603153SAndreas Noever continue;
94816603153SAndreas Noever }
9494ffe722eSMika Westerberg
9504ffe722eSMika Westerberg spin_lock(&ring->lock);
9514ffe722eSMika Westerberg __ring_interrupt(ring);
9524ffe722eSMika Westerberg spin_unlock(&ring->lock);
95316603153SAndreas Noever }
95459120e06SMika Westerberg spin_unlock_irq(&nhi->lock);
95516603153SAndreas Noever }
95616603153SAndreas Noever
nhi_msi(int irq,void * data)95716603153SAndreas Noever static irqreturn_t nhi_msi(int irq, void *data)
95816603153SAndreas Noever {
95916603153SAndreas Noever struct tb_nhi *nhi = data;
96016603153SAndreas Noever schedule_work(&nhi->interrupt_work);
96116603153SAndreas Noever return IRQ_HANDLED;
96216603153SAndreas Noever }
96316603153SAndreas Noever
__nhi_suspend_noirq(struct device * dev,bool wakeup)9643cdb9446SMika Westerberg static int __nhi_suspend_noirq(struct device *dev, bool wakeup)
96523dd5bb4SAndreas Noever {
96623dd5bb4SAndreas Noever struct pci_dev *pdev = to_pci_dev(dev);
96723dd5bb4SAndreas Noever struct tb *tb = pci_get_drvdata(pdev);
9683cdb9446SMika Westerberg struct tb_nhi *nhi = tb->nhi;
9693cdb9446SMika Westerberg int ret;
9709d3cce0bSMika Westerberg
9713cdb9446SMika Westerberg ret = tb_domain_suspend_noirq(tb);
9723cdb9446SMika Westerberg if (ret)
9733cdb9446SMika Westerberg return ret;
9743cdb9446SMika Westerberg
9753cdb9446SMika Westerberg if (nhi->ops && nhi->ops->suspend_noirq) {
9763cdb9446SMika Westerberg ret = nhi->ops->suspend_noirq(tb->nhi, wakeup);
9773cdb9446SMika Westerberg if (ret)
9783cdb9446SMika Westerberg return ret;
9793cdb9446SMika Westerberg }
9803cdb9446SMika Westerberg
9813cdb9446SMika Westerberg return 0;
9823cdb9446SMika Westerberg }
9833cdb9446SMika Westerberg
nhi_suspend_noirq(struct device * dev)9843cdb9446SMika Westerberg static int nhi_suspend_noirq(struct device *dev)
9853cdb9446SMika Westerberg {
9863cdb9446SMika Westerberg return __nhi_suspend_noirq(dev, device_may_wakeup(dev));
9873cdb9446SMika Westerberg }
9883cdb9446SMika Westerberg
nhi_freeze_noirq(struct device * dev)989884e4d57SMika Westerberg static int nhi_freeze_noirq(struct device *dev)
990884e4d57SMika Westerberg {
991884e4d57SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev);
992884e4d57SMika Westerberg struct tb *tb = pci_get_drvdata(pdev);
993884e4d57SMika Westerberg
994884e4d57SMika Westerberg return tb_domain_freeze_noirq(tb);
995884e4d57SMika Westerberg }
996884e4d57SMika Westerberg
nhi_thaw_noirq(struct device * dev)997884e4d57SMika Westerberg static int nhi_thaw_noirq(struct device *dev)
998884e4d57SMika Westerberg {
999884e4d57SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev);
1000884e4d57SMika Westerberg struct tb *tb = pci_get_drvdata(pdev);
1001884e4d57SMika Westerberg
1002884e4d57SMika Westerberg return tb_domain_thaw_noirq(tb);
1003884e4d57SMika Westerberg }
1004884e4d57SMika Westerberg
nhi_wake_supported(struct pci_dev * pdev)10053cdb9446SMika Westerberg static bool nhi_wake_supported(struct pci_dev *pdev)
10063cdb9446SMika Westerberg {
10073cdb9446SMika Westerberg u8 val;
10083cdb9446SMika Westerberg
10093cdb9446SMika Westerberg /*
10103cdb9446SMika Westerberg * If power rails are sustainable for wakeup from S4 this
10113cdb9446SMika Westerberg * property is set by the BIOS.
10123cdb9446SMika Westerberg */
10133cdb9446SMika Westerberg if (device_property_read_u8(&pdev->dev, "WAKE_SUPPORTED", &val))
10143cdb9446SMika Westerberg return !!val;
10153cdb9446SMika Westerberg
10163cdb9446SMika Westerberg return true;
10173cdb9446SMika Westerberg }
10183cdb9446SMika Westerberg
nhi_poweroff_noirq(struct device * dev)10193cdb9446SMika Westerberg static int nhi_poweroff_noirq(struct device *dev)
10203cdb9446SMika Westerberg {
10213cdb9446SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev);
10223cdb9446SMika Westerberg bool wakeup;
10233cdb9446SMika Westerberg
10243cdb9446SMika Westerberg wakeup = device_may_wakeup(dev) && nhi_wake_supported(pdev);
10253cdb9446SMika Westerberg return __nhi_suspend_noirq(dev, wakeup);
102623dd5bb4SAndreas Noever }
102723dd5bb4SAndreas Noever
nhi_enable_int_throttling(struct tb_nhi * nhi)10288c6bba10SMika Westerberg static void nhi_enable_int_throttling(struct tb_nhi *nhi)
10298c6bba10SMika Westerberg {
10308c6bba10SMika Westerberg /* Throttling is specified in 256ns increments */
10318c6bba10SMika Westerberg u32 throttle = DIV_ROUND_UP(128 * NSEC_PER_USEC, 256);
10328c6bba10SMika Westerberg unsigned int i;
10338c6bba10SMika Westerberg
10348c6bba10SMika Westerberg /*
10358c6bba10SMika Westerberg * Configure interrupt throttling for all vectors even if we
10368c6bba10SMika Westerberg * only use few.
10378c6bba10SMika Westerberg */
10388c6bba10SMika Westerberg for (i = 0; i < MSIX_MAX_VECS; i++) {
10398c6bba10SMika Westerberg u32 reg = REG_INT_THROTTLING_RATE + i * 4;
10408c6bba10SMika Westerberg iowrite32(throttle, nhi->iobase + reg);
10418c6bba10SMika Westerberg }
10428c6bba10SMika Westerberg }
10438c6bba10SMika Westerberg
nhi_resume_noirq(struct device * dev)104423dd5bb4SAndreas Noever static int nhi_resume_noirq(struct device *dev)
104523dd5bb4SAndreas Noever {
104623dd5bb4SAndreas Noever struct pci_dev *pdev = to_pci_dev(dev);
104723dd5bb4SAndreas Noever struct tb *tb = pci_get_drvdata(pdev);
10483cdb9446SMika Westerberg struct tb_nhi *nhi = tb->nhi;
10493cdb9446SMika Westerberg int ret;
10509d3cce0bSMika Westerberg
1051bdccf295SMika Westerberg /*
1052bdccf295SMika Westerberg * Check that the device is still there. It may be that the user
1053bdccf295SMika Westerberg * unplugged last device which causes the host controller to go
1054bdccf295SMika Westerberg * away on PCs.
1055bdccf295SMika Westerberg */
10563cdb9446SMika Westerberg if (!pci_device_is_present(pdev)) {
10573cdb9446SMika Westerberg nhi->going_away = true;
10583cdb9446SMika Westerberg } else {
10593cdb9446SMika Westerberg if (nhi->ops && nhi->ops->resume_noirq) {
10603cdb9446SMika Westerberg ret = nhi->ops->resume_noirq(nhi);
10613cdb9446SMika Westerberg if (ret)
10623cdb9446SMika Westerberg return ret;
10633cdb9446SMika Westerberg }
10648c6bba10SMika Westerberg nhi_enable_int_throttling(tb->nhi);
10653cdb9446SMika Westerberg }
1066bdccf295SMika Westerberg
10679d3cce0bSMika Westerberg return tb_domain_resume_noirq(tb);
106823dd5bb4SAndreas Noever }
106923dd5bb4SAndreas Noever
nhi_suspend(struct device * dev)1070f67cf491SMika Westerberg static int nhi_suspend(struct device *dev)
1071f67cf491SMika Westerberg {
1072f67cf491SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev);
1073f67cf491SMika Westerberg struct tb *tb = pci_get_drvdata(pdev);
1074f67cf491SMika Westerberg
1075f67cf491SMika Westerberg return tb_domain_suspend(tb);
1076f67cf491SMika Westerberg }
1077f67cf491SMika Westerberg
nhi_complete(struct device * dev)1078f67cf491SMika Westerberg static void nhi_complete(struct device *dev)
1079f67cf491SMika Westerberg {
1080f67cf491SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev);
1081f67cf491SMika Westerberg struct tb *tb = pci_get_drvdata(pdev);
1082f67cf491SMika Westerberg
10832d8ff0b5SMika Westerberg /*
10842d8ff0b5SMika Westerberg * If we were runtime suspended when system suspend started,
10852d8ff0b5SMika Westerberg * schedule runtime resume now. It should bring the domain back
10862d8ff0b5SMika Westerberg * to functional state.
10872d8ff0b5SMika Westerberg */
10882d8ff0b5SMika Westerberg if (pm_runtime_suspended(&pdev->dev))
10892d8ff0b5SMika Westerberg pm_runtime_resume(&pdev->dev);
10902d8ff0b5SMika Westerberg else
1091f67cf491SMika Westerberg tb_domain_complete(tb);
1092f67cf491SMika Westerberg }
1093f67cf491SMika Westerberg
nhi_runtime_suspend(struct device * dev)10942d8ff0b5SMika Westerberg static int nhi_runtime_suspend(struct device *dev)
10952d8ff0b5SMika Westerberg {
10962d8ff0b5SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev);
10972d8ff0b5SMika Westerberg struct tb *tb = pci_get_drvdata(pdev);
10983cdb9446SMika Westerberg struct tb_nhi *nhi = tb->nhi;
10993cdb9446SMika Westerberg int ret;
11002d8ff0b5SMika Westerberg
11013cdb9446SMika Westerberg ret = tb_domain_runtime_suspend(tb);
11023cdb9446SMika Westerberg if (ret)
11033cdb9446SMika Westerberg return ret;
11043cdb9446SMika Westerberg
11053cdb9446SMika Westerberg if (nhi->ops && nhi->ops->runtime_suspend) {
11063cdb9446SMika Westerberg ret = nhi->ops->runtime_suspend(tb->nhi);
11073cdb9446SMika Westerberg if (ret)
11083cdb9446SMika Westerberg return ret;
11093cdb9446SMika Westerberg }
11103cdb9446SMika Westerberg return 0;
11112d8ff0b5SMika Westerberg }
11122d8ff0b5SMika Westerberg
nhi_runtime_resume(struct device * dev)11132d8ff0b5SMika Westerberg static int nhi_runtime_resume(struct device *dev)
11142d8ff0b5SMika Westerberg {
11152d8ff0b5SMika Westerberg struct pci_dev *pdev = to_pci_dev(dev);
11162d8ff0b5SMika Westerberg struct tb *tb = pci_get_drvdata(pdev);
11173cdb9446SMika Westerberg struct tb_nhi *nhi = tb->nhi;
11183cdb9446SMika Westerberg int ret;
11192d8ff0b5SMika Westerberg
11203cdb9446SMika Westerberg if (nhi->ops && nhi->ops->runtime_resume) {
11213cdb9446SMika Westerberg ret = nhi->ops->runtime_resume(nhi);
11223cdb9446SMika Westerberg if (ret)
11233cdb9446SMika Westerberg return ret;
11243cdb9446SMika Westerberg }
11253cdb9446SMika Westerberg
11263cdb9446SMika Westerberg nhi_enable_int_throttling(nhi);
11272d8ff0b5SMika Westerberg return tb_domain_runtime_resume(tb);
11282d8ff0b5SMika Westerberg }
11292d8ff0b5SMika Westerberg
nhi_shutdown(struct tb_nhi * nhi)113016603153SAndreas Noever static void nhi_shutdown(struct tb_nhi *nhi)
113116603153SAndreas Noever {
113216603153SAndreas Noever int i;
1133daa5140fSMika Westerberg
1134daa5140fSMika Westerberg dev_dbg(&nhi->pdev->dev, "shutdown\n");
113516603153SAndreas Noever
113616603153SAndreas Noever for (i = 0; i < nhi->hop_count; i++) {
113716603153SAndreas Noever if (nhi->tx_rings[i])
113816603153SAndreas Noever dev_WARN(&nhi->pdev->dev,
113916603153SAndreas Noever "TX ring %d is still active\n", i);
114016603153SAndreas Noever if (nhi->rx_rings[i])
114116603153SAndreas Noever dev_WARN(&nhi->pdev->dev,
114216603153SAndreas Noever "RX ring %d is still active\n", i);
114316603153SAndreas Noever }
114416603153SAndreas Noever nhi_disable_interrupts(nhi);
114516603153SAndreas Noever /*
114616603153SAndreas Noever * We have to release the irq before calling flush_work. Otherwise an
114716603153SAndreas Noever * already executing IRQ handler could call schedule_work again.
114816603153SAndreas Noever */
1149046bee1fSMika Westerberg if (!nhi->pdev->msix_enabled) {
115016603153SAndreas Noever devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
115116603153SAndreas Noever flush_work(&nhi->interrupt_work);
1152046bee1fSMika Westerberg }
1153046bee1fSMika Westerberg ida_destroy(&nhi->msix_ida);
11543cdb9446SMika Westerberg
11553cdb9446SMika Westerberg if (nhi->ops && nhi->ops->shutdown)
11563cdb9446SMika Westerberg nhi->ops->shutdown(nhi);
1157046bee1fSMika Westerberg }
1158046bee1fSMika Westerberg
nhi_check_quirks(struct tb_nhi * nhi)1159e390909aSSanjay R Mehta static void nhi_check_quirks(struct tb_nhi *nhi)
1160e390909aSSanjay R Mehta {
116154669e2fSMika Westerberg if (nhi->pdev->vendor == PCI_VENDOR_ID_INTEL) {
1162e390909aSSanjay R Mehta /*
116354669e2fSMika Westerberg * Intel hardware supports auto clear of the interrupt
116454669e2fSMika Westerberg * status register right after interrupt is being
116554669e2fSMika Westerberg * issued.
1166e390909aSSanjay R Mehta */
1167e390909aSSanjay R Mehta nhi->quirks |= QUIRK_AUTO_CLEAR_INT;
116854669e2fSMika Westerberg
116954669e2fSMika Westerberg switch (nhi->pdev->device) {
117054669e2fSMika Westerberg case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
117154669e2fSMika Westerberg case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
117254669e2fSMika Westerberg /*
117354669e2fSMika Westerberg * Falcon Ridge controller needs the end-to-end
117454669e2fSMika Westerberg * flow control workaround to avoid losing Rx
117554669e2fSMika Westerberg * packets when RING_FLAG_E2E is set.
117654669e2fSMika Westerberg */
117754669e2fSMika Westerberg nhi->quirks |= QUIRK_E2E;
117854669e2fSMika Westerberg break;
117954669e2fSMika Westerberg }
118054669e2fSMika Westerberg }
1181e390909aSSanjay R Mehta }
1182e390909aSSanjay R Mehta
nhi_check_iommu_pdev(struct pci_dev * pdev,void * data)118386eaf4a5SRobin Murphy static int nhi_check_iommu_pdev(struct pci_dev *pdev, void *data)
118486eaf4a5SRobin Murphy {
118586eaf4a5SRobin Murphy if (!pdev->external_facing ||
118686eaf4a5SRobin Murphy !device_iommu_capable(&pdev->dev, IOMMU_CAP_PRE_BOOT_PROTECTION))
118786eaf4a5SRobin Murphy return 0;
118886eaf4a5SRobin Murphy *(bool *)data = true;
118986eaf4a5SRobin Murphy return 1; /* Stop walking */
119086eaf4a5SRobin Murphy }
119186eaf4a5SRobin Murphy
nhi_check_iommu(struct tb_nhi * nhi)119286eaf4a5SRobin Murphy static void nhi_check_iommu(struct tb_nhi *nhi)
119386eaf4a5SRobin Murphy {
119486eaf4a5SRobin Murphy struct pci_bus *bus = nhi->pdev->bus;
119586eaf4a5SRobin Murphy bool port_ok = false;
119686eaf4a5SRobin Murphy
119786eaf4a5SRobin Murphy /*
119886eaf4a5SRobin Murphy * Ideally what we'd do here is grab every PCI device that
119986eaf4a5SRobin Murphy * represents a tunnelling adapter for this NHI and check their
120086eaf4a5SRobin Murphy * status directly, but unfortunately USB4 seems to make it
120186eaf4a5SRobin Murphy * obnoxiously difficult to reliably make any correlation.
120286eaf4a5SRobin Murphy *
120386eaf4a5SRobin Murphy * So for now we'll have to bodge it... Hoping that the system
120486eaf4a5SRobin Murphy * is at least sane enough that an adapter is in the same PCI
120586eaf4a5SRobin Murphy * segment as its NHI, if we can find *something* on that segment
120686eaf4a5SRobin Murphy * which meets the requirements for Kernel DMA Protection, we'll
120786eaf4a5SRobin Murphy * take that to imply that firmware is aware and has (hopefully)
120886eaf4a5SRobin Murphy * done the right thing in general. We need to know that the PCI
120986eaf4a5SRobin Murphy * layer has seen the ExternalFacingPort property which will then
121086eaf4a5SRobin Murphy * inform the IOMMU layer to enforce the complete "untrusted DMA"
121186eaf4a5SRobin Murphy * flow, but also that the IOMMU driver itself can be trusted not
121286eaf4a5SRobin Murphy * to have been subverted by a pre-boot DMA attack.
121386eaf4a5SRobin Murphy */
121486eaf4a5SRobin Murphy while (bus->parent)
121586eaf4a5SRobin Murphy bus = bus->parent;
121686eaf4a5SRobin Murphy
121786eaf4a5SRobin Murphy pci_walk_bus(bus, nhi_check_iommu_pdev, &port_ok);
121886eaf4a5SRobin Murphy
121986eaf4a5SRobin Murphy nhi->iommu_dma_protection = port_ok;
122086eaf4a5SRobin Murphy dev_dbg(&nhi->pdev->dev, "IOMMU DMA protection is %s\n",
122186eaf4a5SRobin Murphy str_enabled_disabled(port_ok));
122286eaf4a5SRobin Murphy }
122386eaf4a5SRobin Murphy
nhi_reset(struct tb_nhi * nhi)1224*3c1d704dSSanath S static bool nhi_reset(struct tb_nhi *nhi)
12250fc70886SMika Westerberg {
12260fc70886SMika Westerberg ktime_t timeout;
12270fc70886SMika Westerberg u32 val;
12280fc70886SMika Westerberg
12290fc70886SMika Westerberg val = ioread32(nhi->iobase + REG_CAPS);
12300fc70886SMika Westerberg /* Reset only v2 and later routers */
12310fc70886SMika Westerberg if (FIELD_GET(REG_CAPS_VERSION_MASK, val) < REG_CAPS_VERSION_2)
1232*3c1d704dSSanath S return false;
12330fc70886SMika Westerberg
12340fc70886SMika Westerberg if (!host_reset) {
12350fc70886SMika Westerberg dev_dbg(&nhi->pdev->dev, "skipping host router reset\n");
1236*3c1d704dSSanath S return false;
12370fc70886SMika Westerberg }
12380fc70886SMika Westerberg
12390fc70886SMika Westerberg iowrite32(REG_RESET_HRR, nhi->iobase + REG_RESET);
12400fc70886SMika Westerberg msleep(100);
12410fc70886SMika Westerberg
12420fc70886SMika Westerberg timeout = ktime_add_ms(ktime_get(), 500);
12430fc70886SMika Westerberg do {
12440fc70886SMika Westerberg val = ioread32(nhi->iobase + REG_RESET);
12450fc70886SMika Westerberg if (!(val & REG_RESET_HRR)) {
12460fc70886SMika Westerberg dev_warn(&nhi->pdev->dev, "host router reset successful\n");
1247*3c1d704dSSanath S return true;
12480fc70886SMika Westerberg }
12490fc70886SMika Westerberg usleep_range(10, 20);
12500fc70886SMika Westerberg } while (ktime_before(ktime_get(), timeout));
12510fc70886SMika Westerberg
12520fc70886SMika Westerberg dev_warn(&nhi->pdev->dev, "timeout resetting host router\n");
1253*3c1d704dSSanath S
1254*3c1d704dSSanath S return false;
12550fc70886SMika Westerberg }
12560fc70886SMika Westerberg
nhi_init_msi(struct tb_nhi * nhi)1257046bee1fSMika Westerberg static int nhi_init_msi(struct tb_nhi *nhi)
1258046bee1fSMika Westerberg {
1259046bee1fSMika Westerberg struct pci_dev *pdev = nhi->pdev;
12608d9dcfffSAndy Shevchenko struct device *dev = &pdev->dev;
1261046bee1fSMika Westerberg int res, irq, nvec;
1262046bee1fSMika Westerberg
1263046bee1fSMika Westerberg /* In case someone left them on. */
1264046bee1fSMika Westerberg nhi_disable_interrupts(nhi);
1265046bee1fSMika Westerberg
12668c6bba10SMika Westerberg nhi_enable_int_throttling(nhi);
12678c6bba10SMika Westerberg
1268046bee1fSMika Westerberg ida_init(&nhi->msix_ida);
1269046bee1fSMika Westerberg
1270046bee1fSMika Westerberg /*
1271046bee1fSMika Westerberg * The NHI has 16 MSI-X vectors or a single MSI. We first try to
1272046bee1fSMika Westerberg * get all MSI-X vectors and if we succeed, each ring will have
1273046bee1fSMika Westerberg * one MSI-X. If for some reason that does not work out, we
1274046bee1fSMika Westerberg * fallback to a single MSI.
1275046bee1fSMika Westerberg */
1276046bee1fSMika Westerberg nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS,
1277046bee1fSMika Westerberg PCI_IRQ_MSIX);
1278046bee1fSMika Westerberg if (nvec < 0) {
1279046bee1fSMika Westerberg nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
1280046bee1fSMika Westerberg if (nvec < 0)
1281046bee1fSMika Westerberg return nvec;
1282046bee1fSMika Westerberg
1283046bee1fSMika Westerberg INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
1284046bee1fSMika Westerberg
1285046bee1fSMika Westerberg irq = pci_irq_vector(nhi->pdev, 0);
1286046bee1fSMika Westerberg if (irq < 0)
1287046bee1fSMika Westerberg return irq;
1288046bee1fSMika Westerberg
1289046bee1fSMika Westerberg res = devm_request_irq(&pdev->dev, irq, nhi_msi,
1290046bee1fSMika Westerberg IRQF_NO_SUSPEND, "thunderbolt", nhi);
12918d9dcfffSAndy Shevchenko if (res)
12928d9dcfffSAndy Shevchenko return dev_err_probe(dev, res, "request_irq failed, aborting\n");
1293046bee1fSMika Westerberg }
1294046bee1fSMika Westerberg
1295046bee1fSMika Westerberg return 0;
129616603153SAndreas Noever }
129716603153SAndreas Noever
nhi_imr_valid(struct pci_dev * pdev)12983cdb9446SMika Westerberg static bool nhi_imr_valid(struct pci_dev *pdev)
12993cdb9446SMika Westerberg {
13003cdb9446SMika Westerberg u8 val;
13013cdb9446SMika Westerberg
13023cdb9446SMika Westerberg if (!device_property_read_u8(&pdev->dev, "IMR_VALID", &val))
13033cdb9446SMika Westerberg return !!val;
13043cdb9446SMika Westerberg
13053cdb9446SMika Westerberg return true;
13063cdb9446SMika Westerberg }
13073cdb9446SMika Westerberg
nhi_select_cm(struct tb_nhi * nhi)1308c6da62a2SMika Westerberg static struct tb *nhi_select_cm(struct tb_nhi *nhi)
1309c6da62a2SMika Westerberg {
1310c6da62a2SMika Westerberg struct tb *tb;
1311c6da62a2SMika Westerberg
1312c6da62a2SMika Westerberg /*
1313c6da62a2SMika Westerberg * USB4 case is simple. If we got control of any of the
1314c6da62a2SMika Westerberg * capabilities, we use software CM.
1315c6da62a2SMika Westerberg */
1316c6da62a2SMika Westerberg if (tb_acpi_is_native())
1317c6da62a2SMika Westerberg return tb_probe(nhi);
1318c6da62a2SMika Westerberg
1319c6da62a2SMika Westerberg /*
1320c6da62a2SMika Westerberg * Either firmware based CM is running (we did not get control
1321c6da62a2SMika Westerberg * from the firmware) or this is pre-USB4 PC so try first
1322c6da62a2SMika Westerberg * firmware CM and then fallback to software CM.
1323c6da62a2SMika Westerberg */
1324c6da62a2SMika Westerberg tb = icm_probe(nhi);
1325c6da62a2SMika Westerberg if (!tb)
1326c6da62a2SMika Westerberg tb = tb_probe(nhi);
1327c6da62a2SMika Westerberg
1328c6da62a2SMika Westerberg return tb;
1329c6da62a2SMika Westerberg }
1330c6da62a2SMika Westerberg
nhi_probe(struct pci_dev * pdev,const struct pci_device_id * id)133116603153SAndreas Noever static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
133216603153SAndreas Noever {
13338d9dcfffSAndy Shevchenko struct device *dev = &pdev->dev;
133416603153SAndreas Noever struct tb_nhi *nhi;
1335d6cc51cdSAndreas Noever struct tb *tb;
1336*3c1d704dSSanath S bool reset;
133716603153SAndreas Noever int res;
133816603153SAndreas Noever
13398d9dcfffSAndy Shevchenko if (!nhi_imr_valid(pdev))
13408d9dcfffSAndy Shevchenko return dev_err_probe(dev, -ENODEV, "firmware image not valid, aborting\n");
13413cdb9446SMika Westerberg
134216603153SAndreas Noever res = pcim_enable_device(pdev);
13438d9dcfffSAndy Shevchenko if (res)
13448d9dcfffSAndy Shevchenko return dev_err_probe(dev, res, "cannot enable PCI device, aborting\n");
134516603153SAndreas Noever
134616603153SAndreas Noever res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
13478d9dcfffSAndy Shevchenko if (res)
13488d9dcfffSAndy Shevchenko return dev_err_probe(dev, res, "cannot obtain PCI resources, aborting\n");
134916603153SAndreas Noever
135016603153SAndreas Noever nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
135116603153SAndreas Noever if (!nhi)
135216603153SAndreas Noever return -ENOMEM;
135316603153SAndreas Noever
135416603153SAndreas Noever nhi->pdev = pdev;
13553cdb9446SMika Westerberg nhi->ops = (const struct tb_nhi_ops *)id->driver_data;
1356ca319f55SMika Westerberg /* cannot fail - table is allocated in pcim_iomap_regions */
135716603153SAndreas Noever nhi->iobase = pcim_iomap_table(pdev)[0];
13580fc70886SMika Westerberg nhi->hop_count = ioread32(nhi->iobase + REG_CAPS) & 0x3ff;
13598d9dcfffSAndy Shevchenko dev_dbg(dev, "total paths: %d\n", nhi->hop_count);
136016603153SAndreas Noever
13612a211f32SHimangi Saraogi nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
13622a211f32SHimangi Saraogi sizeof(*nhi->tx_rings), GFP_KERNEL);
13632a211f32SHimangi Saraogi nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
13642a211f32SHimangi Saraogi sizeof(*nhi->rx_rings), GFP_KERNEL);
136516603153SAndreas Noever if (!nhi->tx_rings || !nhi->rx_rings)
136616603153SAndreas Noever return -ENOMEM;
136716603153SAndreas Noever
1368e390909aSSanjay R Mehta nhi_check_quirks(nhi);
136986eaf4a5SRobin Murphy nhi_check_iommu(nhi);
1370e390909aSSanjay R Mehta
1371*3c1d704dSSanath S /*
1372*3c1d704dSSanath S * Only USB4 v2 hosts support host reset so if we already did
1373*3c1d704dSSanath S * that then don't do it again when the domain is initialized.
1374*3c1d704dSSanath S */
1375*3c1d704dSSanath S reset = nhi_reset(nhi) ? false : host_reset;
13760fc70886SMika Westerberg
1377046bee1fSMika Westerberg res = nhi_init_msi(nhi);
13788d9dcfffSAndy Shevchenko if (res)
13798d9dcfffSAndy Shevchenko return dev_err_probe(dev, res, "cannot enable MSI, aborting\n");
138016603153SAndreas Noever
138159120e06SMika Westerberg spin_lock_init(&nhi->lock);
138216603153SAndreas Noever
1383dba3caf6SMika Westerberg res = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
13848d9dcfffSAndy Shevchenko if (res)
13858d9dcfffSAndy Shevchenko return dev_err_probe(dev, res, "failed to set DMA mask\n");
1386dba3caf6SMika Westerberg
138716603153SAndreas Noever pci_set_master(pdev);
138816603153SAndreas Noever
13893cdb9446SMika Westerberg if (nhi->ops && nhi->ops->init) {
13903cdb9446SMika Westerberg res = nhi->ops->init(nhi);
13913cdb9446SMika Westerberg if (res)
13923cdb9446SMika Westerberg return res;
13933cdb9446SMika Westerberg }
13943cdb9446SMika Westerberg
1395c6da62a2SMika Westerberg tb = nhi_select_cm(nhi);
13968d9dcfffSAndy Shevchenko if (!tb)
13978d9dcfffSAndy Shevchenko return dev_err_probe(dev, -ENODEV,
1398f67cf491SMika Westerberg "failed to determine connection manager, aborting\n");
1399f67cf491SMika Westerberg
14008d9dcfffSAndy Shevchenko dev_dbg(dev, "NHI initialized, starting thunderbolt\n");
14019d3cce0bSMika Westerberg
1402*3c1d704dSSanath S res = tb_domain_add(tb, reset);
14039d3cce0bSMika Westerberg if (res) {
1404d6cc51cdSAndreas Noever /*
1405d6cc51cdSAndreas Noever * At this point the RX/TX rings might already have been
1406d6cc51cdSAndreas Noever * activated. Do a proper shutdown.
1407d6cc51cdSAndreas Noever */
14089d3cce0bSMika Westerberg tb_domain_put(tb);
1409d6cc51cdSAndreas Noever nhi_shutdown(nhi);
141068a7a2acSMika Westerberg return res;
1411d6cc51cdSAndreas Noever }
1412d6cc51cdSAndreas Noever pci_set_drvdata(pdev, tb);
141316603153SAndreas Noever
1414b2911a59SMika Westerberg device_wakeup_enable(&pdev->dev);
1415b2911a59SMika Westerberg
14162d8ff0b5SMika Westerberg pm_runtime_allow(&pdev->dev);
14172d8ff0b5SMika Westerberg pm_runtime_set_autosuspend_delay(&pdev->dev, TB_AUTOSUSPEND_DELAY);
14182d8ff0b5SMika Westerberg pm_runtime_use_autosuspend(&pdev->dev);
14192d8ff0b5SMika Westerberg pm_runtime_put_autosuspend(&pdev->dev);
14202d8ff0b5SMika Westerberg
142116603153SAndreas Noever return 0;
142216603153SAndreas Noever }
142316603153SAndreas Noever
nhi_remove(struct pci_dev * pdev)142416603153SAndreas Noever static void nhi_remove(struct pci_dev *pdev)
142516603153SAndreas Noever {
1426d6cc51cdSAndreas Noever struct tb *tb = pci_get_drvdata(pdev);
1427d6cc51cdSAndreas Noever struct tb_nhi *nhi = tb->nhi;
14289d3cce0bSMika Westerberg
14292d8ff0b5SMika Westerberg pm_runtime_get_sync(&pdev->dev);
14302d8ff0b5SMika Westerberg pm_runtime_dont_use_autosuspend(&pdev->dev);
14312d8ff0b5SMika Westerberg pm_runtime_forbid(&pdev->dev);
14322d8ff0b5SMika Westerberg
14339d3cce0bSMika Westerberg tb_domain_remove(tb);
143416603153SAndreas Noever nhi_shutdown(nhi);
143516603153SAndreas Noever }
143616603153SAndreas Noever
143723dd5bb4SAndreas Noever /*
143823dd5bb4SAndreas Noever * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
143923dd5bb4SAndreas Noever * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
144023dd5bb4SAndreas Noever * resume_noirq until we are done.
144123dd5bb4SAndreas Noever */
144223dd5bb4SAndreas Noever static const struct dev_pm_ops nhi_pm_ops = {
144323dd5bb4SAndreas Noever .suspend_noirq = nhi_suspend_noirq,
144423dd5bb4SAndreas Noever .resume_noirq = nhi_resume_noirq,
1445884e4d57SMika Westerberg .freeze_noirq = nhi_freeze_noirq, /*
144623dd5bb4SAndreas Noever * we just disable hotplug, the
144723dd5bb4SAndreas Noever * pci-tunnels stay alive.
144823dd5bb4SAndreas Noever */
1449884e4d57SMika Westerberg .thaw_noirq = nhi_thaw_noirq,
145023dd5bb4SAndreas Noever .restore_noirq = nhi_resume_noirq,
1451f67cf491SMika Westerberg .suspend = nhi_suspend,
14523cdb9446SMika Westerberg .poweroff_noirq = nhi_poweroff_noirq,
1453f67cf491SMika Westerberg .poweroff = nhi_suspend,
1454f67cf491SMika Westerberg .complete = nhi_complete,
14552d8ff0b5SMika Westerberg .runtime_suspend = nhi_runtime_suspend,
14562d8ff0b5SMika Westerberg .runtime_resume = nhi_runtime_resume,
145723dd5bb4SAndreas Noever };
145823dd5bb4SAndreas Noever
1459620863f7SSachin Kamat static struct pci_device_id nhi_ids[] = {
146016603153SAndreas Noever /*
146116603153SAndreas Noever * We have to specify class, the TB bridges use the same device and
14621d111406SLukas Wunner * vendor (sub)id on gen 1 and gen 2 controllers.
146316603153SAndreas Noever */
146416603153SAndreas Noever {
146516603153SAndreas Noever .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
14661d111406SLukas Wunner .vendor = PCI_VENDOR_ID_INTEL,
146719bf4d4fSLukas Wunner .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
146819bf4d4fSLukas Wunner .subvendor = 0x2222, .subdevice = 0x1111,
146919bf4d4fSLukas Wunner },
147019bf4d4fSLukas Wunner {
147119bf4d4fSLukas Wunner .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
147219bf4d4fSLukas Wunner .vendor = PCI_VENDOR_ID_INTEL,
14731d111406SLukas Wunner .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
147416603153SAndreas Noever .subvendor = 0x2222, .subdevice = 0x1111,
147516603153SAndreas Noever },
147616603153SAndreas Noever {
147716603153SAndreas Noever .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
14781d111406SLukas Wunner .vendor = PCI_VENDOR_ID_INTEL,
147982a6a81cSXavier Gnata .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI,
148082a6a81cSXavier Gnata .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
148182a6a81cSXavier Gnata },
148282a6a81cSXavier Gnata {
148382a6a81cSXavier Gnata .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
148482a6a81cSXavier Gnata .vendor = PCI_VENDOR_ID_INTEL,
14851d111406SLukas Wunner .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI,
1486a42fb351SKnuth Posern .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
148716603153SAndreas Noever },
14885e2781bcSMika Westerberg
14895e2781bcSMika Westerberg /* Thunderbolt 3 */
14905e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) },
14915e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) },
14925e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) },
14935e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) },
14945e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) },
14955e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) },
14965e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) },
14975e2781bcSMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) },
14984bac471dSRadion Mirchevsky { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI) },
14994bac471dSRadion Mirchevsky { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI) },
15003cdb9446SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI0),
15013cdb9446SMika Westerberg .driver_data = (kernel_ulong_t)&icl_nhi_ops },
15023cdb9446SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI1),
15033cdb9446SMika Westerberg .driver_data = (kernel_ulong_t)&icl_nhi_ops },
15041c1aac98SMika Westerberg /* Thunderbolt 4 */
150557d8df68SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI0),
150657d8df68SMika Westerberg .driver_data = (kernel_ulong_t)&icl_nhi_ops },
150757d8df68SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI1),
150857d8df68SMika Westerberg .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1509f6439c53SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI0),
1510f6439c53SMika Westerberg .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1511f6439c53SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI1),
1512f6439c53SMika Westerberg .driver_data = (kernel_ulong_t)&icl_nhi_ops },
151313579486SAzhar Shaikh { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI0),
151413579486SAzhar Shaikh .driver_data = (kernel_ulong_t)&icl_nhi_ops },
151513579486SAzhar Shaikh { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI1),
151613579486SAzhar Shaikh .driver_data = (kernel_ulong_t)&icl_nhi_ops },
15177ec58378SGeorge D Sworo { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI0),
15187ec58378SGeorge D Sworo .driver_data = (kernel_ulong_t)&icl_nhi_ops },
15197ec58378SGeorge D Sworo { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI1),
15207ec58378SGeorge D Sworo .driver_data = (kernel_ulong_t)&icl_nhi_ops },
152132249fd8SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_M_NHI0),
152232249fd8SMika Westerberg .driver_data = (kernel_ulong_t)&icl_nhi_ops },
152332249fd8SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_P_NHI0),
152432249fd8SMika Westerberg .driver_data = (kernel_ulong_t)&icl_nhi_ops },
152532249fd8SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_P_NHI1),
152632249fd8SMika Westerberg .driver_data = (kernel_ulong_t)&icl_nhi_ops },
15276f14a210SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BARLOW_RIDGE_HOST_80G_NHI) },
15286f14a210SMika Westerberg { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BARLOW_RIDGE_HOST_40G_NHI) },
15295e2781bcSMika Westerberg
1530b0407983SMika Westerberg /* Any USB4 compliant host */
1531b0407983SMika Westerberg { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_USB4, ~0) },
1532b0407983SMika Westerberg
153316603153SAndreas Noever { 0,}
153416603153SAndreas Noever };
153516603153SAndreas Noever
153616603153SAndreas Noever MODULE_DEVICE_TABLE(pci, nhi_ids);
1537714e57aaSMika Westerberg MODULE_DESCRIPTION("Thunderbolt/USB4 core driver");
153816603153SAndreas Noever MODULE_LICENSE("GPL");
153916603153SAndreas Noever
154016603153SAndreas Noever static struct pci_driver nhi_driver = {
154116603153SAndreas Noever .name = "thunderbolt",
154216603153SAndreas Noever .id_table = nhi_ids,
154316603153SAndreas Noever .probe = nhi_probe,
154416603153SAndreas Noever .remove = nhi_remove,
15454caf2511SMaxim Levitsky .shutdown = nhi_remove,
154623dd5bb4SAndreas Noever .driver.pm = &nhi_pm_ops,
154716603153SAndreas Noever };
154816603153SAndreas Noever
nhi_init(void)154916603153SAndreas Noever static int __init nhi_init(void)
155016603153SAndreas Noever {
15519d3cce0bSMika Westerberg int ret;
15529d3cce0bSMika Westerberg
15539d3cce0bSMika Westerberg ret = tb_domain_init();
15549d3cce0bSMika Westerberg if (ret)
15559d3cce0bSMika Westerberg return ret;
15569d3cce0bSMika Westerberg ret = pci_register_driver(&nhi_driver);
15579d3cce0bSMika Westerberg if (ret)
15589d3cce0bSMika Westerberg tb_domain_exit();
15599d3cce0bSMika Westerberg return ret;
156016603153SAndreas Noever }
156116603153SAndreas Noever
nhi_unload(void)156216603153SAndreas Noever static void __exit nhi_unload(void)
156316603153SAndreas Noever {
156416603153SAndreas Noever pci_unregister_driver(&nhi_driver);
15659d3cce0bSMika Westerberg tb_domain_exit();
156616603153SAndreas Noever }
156716603153SAndreas Noever
1568eafa717bSMika Westerberg rootfs_initcall(nhi_init);
156916603153SAndreas Noever module_exit(nhi_unload);
1570