xref: /openbmc/linux/drivers/thunderbolt/nhi.c (revision 58cdfe6f58b35f17f56386f5fcf937168a423ad1)
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 
491716efdbSMario Limonciello static int ring_interrupt_index(const struct tb_ring *ring)
5016603153SAndreas Noever {
5116603153SAndreas Noever 	int bit = ring->hop;
5216603153SAndreas Noever 	if (!ring->is_tx)
5316603153SAndreas Noever 		bit += ring->nhi->hop_count;
5416603153SAndreas Noever 	return bit;
5516603153SAndreas Noever }
5616603153SAndreas Noever 
57a7bfb27bSLee Jones /*
5816603153SAndreas Noever  * ring_interrupt_active() - activate/deactivate interrupts for a single ring
5916603153SAndreas Noever  *
6016603153SAndreas Noever  * ring->nhi->lock must be held.
6116603153SAndreas Noever  */
6216603153SAndreas Noever static void ring_interrupt_active(struct tb_ring *ring, bool active)
6316603153SAndreas Noever {
6419bf4d4fSLukas Wunner 	int reg = REG_RING_INTERRUPT_BASE +
6519bf4d4fSLukas Wunner 		  ring_interrupt_index(ring) / 32 * 4;
66*58cdfe6fSTom Rix 	int interrupt_bit = ring_interrupt_index(ring) & 31;
67*58cdfe6fSTom Rix 	int mask = 1 << interrupt_bit;
6816603153SAndreas Noever 	u32 old, new;
69046bee1fSMika Westerberg 
70046bee1fSMika Westerberg 	if (ring->irq > 0) {
71046bee1fSMika Westerberg 		u32 step, shift, ivr, misc;
72046bee1fSMika Westerberg 		void __iomem *ivr_base;
73*58cdfe6fSTom Rix 		int auto_clear_bit;
74046bee1fSMika Westerberg 		int index;
75046bee1fSMika Westerberg 
76046bee1fSMika Westerberg 		if (ring->is_tx)
77046bee1fSMika Westerberg 			index = ring->hop;
78046bee1fSMika Westerberg 		else
79046bee1fSMika Westerberg 			index = ring->hop + ring->nhi->hop_count;
80046bee1fSMika Westerberg 
81046bee1fSMika Westerberg 		/*
82468c49f4SMario Limonciello 		 * Intel routers support a bit that isn't part of
83468c49f4SMario Limonciello 		 * the USB4 spec to ask the hardware to clear
84468c49f4SMario Limonciello 		 * interrupt status bits automatically since
85468c49f4SMario Limonciello 		 * we already know which interrupt was triggered.
86468c49f4SMario Limonciello 		 *
87468c49f4SMario Limonciello 		 * Other routers explicitly disable auto-clear
88468c49f4SMario Limonciello 		 * to prevent conditions that may occur where two
89468c49f4SMario Limonciello 		 * MSIX interrupts are simultaneously active and
90468c49f4SMario Limonciello 		 * reading the register clears both of them.
91046bee1fSMika Westerberg 		 */
92046bee1fSMika Westerberg 		misc = ioread32(ring->nhi->iobase + REG_DMA_MISC);
93468c49f4SMario Limonciello 		if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
94*58cdfe6fSTom Rix 			auto_clear_bit = REG_DMA_MISC_INT_AUTO_CLEAR;
95468c49f4SMario Limonciello 		else
96*58cdfe6fSTom Rix 			auto_clear_bit = REG_DMA_MISC_DISABLE_AUTO_CLEAR;
97*58cdfe6fSTom Rix 		if (!(misc & auto_clear_bit))
98*58cdfe6fSTom Rix 			iowrite32(misc | auto_clear_bit,
99*58cdfe6fSTom Rix 				  ring->nhi->iobase + REG_DMA_MISC);
100046bee1fSMika Westerberg 
101046bee1fSMika Westerberg 		ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE;
102046bee1fSMika Westerberg 		step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
103046bee1fSMika Westerberg 		shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
104046bee1fSMika Westerberg 		ivr = ioread32(ivr_base + step);
105046bee1fSMika Westerberg 		ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift);
106046bee1fSMika Westerberg 		if (active)
107046bee1fSMika Westerberg 			ivr |= ring->vector << shift;
108046bee1fSMika Westerberg 		iowrite32(ivr, ivr_base + step);
109046bee1fSMika Westerberg 	}
110046bee1fSMika Westerberg 
11116603153SAndreas Noever 	old = ioread32(ring->nhi->iobase + reg);
11216603153SAndreas Noever 	if (active)
11316603153SAndreas Noever 		new = old | mask;
11416603153SAndreas Noever 	else
11516603153SAndreas Noever 		new = old & ~mask;
11616603153SAndreas Noever 
117daa5140fSMika Westerberg 	dev_dbg(&ring->nhi->pdev->dev,
11816603153SAndreas Noever 		"%s interrupt at register %#x bit %d (%#x -> %#x)\n",
119*58cdfe6fSTom Rix 		active ? "enabling" : "disabling", reg, interrupt_bit, old, new);
12016603153SAndreas Noever 
12116603153SAndreas Noever 	if (new == old)
12216603153SAndreas Noever 		dev_WARN(&ring->nhi->pdev->dev,
12316603153SAndreas Noever 					 "interrupt for %s %d is already %s\n",
12416603153SAndreas Noever 					 RING_TYPE(ring), ring->hop,
12516603153SAndreas Noever 					 active ? "enabled" : "disabled");
12616603153SAndreas Noever 	iowrite32(new, ring->nhi->iobase + reg);
12716603153SAndreas Noever }
12816603153SAndreas Noever 
129a7bfb27bSLee Jones /*
13016603153SAndreas Noever  * nhi_disable_interrupts() - disable interrupts for all rings
13116603153SAndreas Noever  *
13216603153SAndreas Noever  * Use only during init and shutdown.
13316603153SAndreas Noever  */
13416603153SAndreas Noever static void nhi_disable_interrupts(struct tb_nhi *nhi)
13516603153SAndreas Noever {
13616603153SAndreas Noever 	int i = 0;
13716603153SAndreas Noever 	/* disable interrupts */
13816603153SAndreas Noever 	for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++)
13916603153SAndreas Noever 		iowrite32(0, nhi->iobase + REG_RING_INTERRUPT_BASE + 4 * i);
14016603153SAndreas Noever 
14116603153SAndreas Noever 	/* clear interrupt status bits */
14216603153SAndreas Noever 	for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++)
14316603153SAndreas Noever 		ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + 4 * i);
14416603153SAndreas Noever }
14516603153SAndreas Noever 
14616603153SAndreas Noever /* ring helper methods */
14716603153SAndreas Noever 
14816603153SAndreas Noever static void __iomem *ring_desc_base(struct tb_ring *ring)
14916603153SAndreas Noever {
15016603153SAndreas Noever 	void __iomem *io = ring->nhi->iobase;
15116603153SAndreas Noever 	io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE;
15216603153SAndreas Noever 	io += ring->hop * 16;
15316603153SAndreas Noever 	return io;
15416603153SAndreas Noever }
15516603153SAndreas Noever 
15616603153SAndreas Noever static void __iomem *ring_options_base(struct tb_ring *ring)
15716603153SAndreas Noever {
15816603153SAndreas Noever 	void __iomem *io = ring->nhi->iobase;
15916603153SAndreas Noever 	io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE;
16016603153SAndreas Noever 	io += ring->hop * 32;
16116603153SAndreas Noever 	return io;
16216603153SAndreas Noever }
16316603153SAndreas Noever 
16494379521SMika Westerberg static void ring_iowrite_cons(struct tb_ring *ring, u16 cons)
16516603153SAndreas Noever {
16694379521SMika Westerberg 	/*
16794379521SMika Westerberg 	 * The other 16-bits in the register is read-only and writes to it
16894379521SMika Westerberg 	 * are ignored by the hardware so we can save one ioread32() by
16994379521SMika Westerberg 	 * filling the read-only bits with zeroes.
17094379521SMika Westerberg 	 */
17194379521SMika Westerberg 	iowrite32(cons, ring_desc_base(ring) + 8);
17294379521SMika Westerberg }
17394379521SMika Westerberg 
17494379521SMika Westerberg static void ring_iowrite_prod(struct tb_ring *ring, u16 prod)
17594379521SMika Westerberg {
17694379521SMika Westerberg 	/* See ring_iowrite_cons() above for explanation */
17794379521SMika Westerberg 	iowrite32(prod << 16, ring_desc_base(ring) + 8);
17816603153SAndreas Noever }
17916603153SAndreas Noever 
18016603153SAndreas Noever static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
18116603153SAndreas Noever {
18216603153SAndreas Noever 	iowrite32(value, ring_desc_base(ring) + offset);
18316603153SAndreas Noever }
18416603153SAndreas Noever 
18516603153SAndreas Noever static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
18616603153SAndreas Noever {
18716603153SAndreas Noever 	iowrite32(value, ring_desc_base(ring) + offset);
18816603153SAndreas Noever 	iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
18916603153SAndreas Noever }
19016603153SAndreas Noever 
19116603153SAndreas Noever static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
19216603153SAndreas Noever {
19316603153SAndreas Noever 	iowrite32(value, ring_options_base(ring) + offset);
19416603153SAndreas Noever }
19516603153SAndreas Noever 
19616603153SAndreas Noever static bool ring_full(struct tb_ring *ring)
19716603153SAndreas Noever {
19816603153SAndreas Noever 	return ((ring->head + 1) % ring->size) == ring->tail;
19916603153SAndreas Noever }
20016603153SAndreas Noever 
20116603153SAndreas Noever static bool ring_empty(struct tb_ring *ring)
20216603153SAndreas Noever {
20316603153SAndreas Noever 	return ring->head == ring->tail;
20416603153SAndreas Noever }
20516603153SAndreas Noever 
206a7bfb27bSLee Jones /*
20716603153SAndreas Noever  * ring_write_descriptors() - post frames from ring->queue to the controller
20816603153SAndreas Noever  *
20916603153SAndreas Noever  * ring->lock is held.
21016603153SAndreas Noever  */
21116603153SAndreas Noever static void ring_write_descriptors(struct tb_ring *ring)
21216603153SAndreas Noever {
21316603153SAndreas Noever 	struct ring_frame *frame, *n;
21416603153SAndreas Noever 	struct ring_desc *descriptor;
21516603153SAndreas Noever 	list_for_each_entry_safe(frame, n, &ring->queue, list) {
21616603153SAndreas Noever 		if (ring_full(ring))
21716603153SAndreas Noever 			break;
21816603153SAndreas Noever 		list_move_tail(&frame->list, &ring->in_flight);
21916603153SAndreas Noever 		descriptor = &ring->descriptors[ring->head];
22016603153SAndreas Noever 		descriptor->phys = frame->buffer_phy;
22116603153SAndreas Noever 		descriptor->time = 0;
22216603153SAndreas Noever 		descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT;
22316603153SAndreas Noever 		if (ring->is_tx) {
22416603153SAndreas Noever 			descriptor->length = frame->size;
22516603153SAndreas Noever 			descriptor->eof = frame->eof;
22616603153SAndreas Noever 			descriptor->sof = frame->sof;
22716603153SAndreas Noever 		}
22816603153SAndreas Noever 		ring->head = (ring->head + 1) % ring->size;
22994379521SMika Westerberg 		if (ring->is_tx)
23094379521SMika Westerberg 			ring_iowrite_prod(ring, ring->head);
23194379521SMika Westerberg 		else
23294379521SMika Westerberg 			ring_iowrite_cons(ring, ring->head);
23316603153SAndreas Noever 	}
23416603153SAndreas Noever }
23516603153SAndreas Noever 
236a7bfb27bSLee Jones /*
23716603153SAndreas Noever  * ring_work() - progress completed frames
23816603153SAndreas Noever  *
23916603153SAndreas Noever  * If the ring is shutting down then all frames are marked as canceled and
24016603153SAndreas Noever  * their callbacks are invoked.
24116603153SAndreas Noever  *
24216603153SAndreas Noever  * Otherwise we collect all completed frame from the ring buffer, write new
24316603153SAndreas Noever  * frame to the ring buffer and invoke the callbacks for the completed frames.
24416603153SAndreas Noever  */
24516603153SAndreas Noever static void ring_work(struct work_struct *work)
24616603153SAndreas Noever {
24716603153SAndreas Noever 	struct tb_ring *ring = container_of(work, typeof(*ring), work);
24816603153SAndreas Noever 	struct ring_frame *frame;
24916603153SAndreas Noever 	bool canceled = false;
25022b7de10SMika Westerberg 	unsigned long flags;
25116603153SAndreas Noever 	LIST_HEAD(done);
25222b7de10SMika Westerberg 
25322b7de10SMika Westerberg 	spin_lock_irqsave(&ring->lock, flags);
25416603153SAndreas Noever 
25516603153SAndreas Noever 	if (!ring->running) {
25616603153SAndreas Noever 		/*  Move all frames to done and mark them as canceled. */
25716603153SAndreas Noever 		list_splice_tail_init(&ring->in_flight, &done);
25816603153SAndreas Noever 		list_splice_tail_init(&ring->queue, &done);
25916603153SAndreas Noever 		canceled = true;
26016603153SAndreas Noever 		goto invoke_callback;
26116603153SAndreas Noever 	}
26216603153SAndreas Noever 
26316603153SAndreas Noever 	while (!ring_empty(ring)) {
26416603153SAndreas Noever 		if (!(ring->descriptors[ring->tail].flags
26516603153SAndreas Noever 				& RING_DESC_COMPLETED))
26616603153SAndreas Noever 			break;
26716603153SAndreas Noever 		frame = list_first_entry(&ring->in_flight, typeof(*frame),
26816603153SAndreas Noever 					 list);
26916603153SAndreas Noever 		list_move_tail(&frame->list, &done);
27016603153SAndreas Noever 		if (!ring->is_tx) {
27116603153SAndreas Noever 			frame->size = ring->descriptors[ring->tail].length;
27216603153SAndreas Noever 			frame->eof = ring->descriptors[ring->tail].eof;
27316603153SAndreas Noever 			frame->sof = ring->descriptors[ring->tail].sof;
27416603153SAndreas Noever 			frame->flags = ring->descriptors[ring->tail].flags;
27516603153SAndreas Noever 		}
27616603153SAndreas Noever 		ring->tail = (ring->tail + 1) % ring->size;
27716603153SAndreas Noever 	}
27816603153SAndreas Noever 	ring_write_descriptors(ring);
27916603153SAndreas Noever 
28016603153SAndreas Noever invoke_callback:
28122b7de10SMika Westerberg 	/* allow callbacks to schedule new work */
28222b7de10SMika Westerberg 	spin_unlock_irqrestore(&ring->lock, flags);
28316603153SAndreas Noever 	while (!list_empty(&done)) {
28416603153SAndreas Noever 		frame = list_first_entry(&done, typeof(*frame), list);
28516603153SAndreas Noever 		/*
28616603153SAndreas Noever 		 * The callback may reenqueue or delete frame.
28716603153SAndreas Noever 		 * Do not hold on to it.
28816603153SAndreas Noever 		 */
28916603153SAndreas Noever 		list_del_init(&frame->list);
2904ffe722eSMika Westerberg 		if (frame->callback)
29116603153SAndreas Noever 			frame->callback(ring, frame, canceled);
29216603153SAndreas Noever 	}
29316603153SAndreas Noever }
29416603153SAndreas Noever 
2953b3d9f4dSMika Westerberg int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
29616603153SAndreas Noever {
29722b7de10SMika Westerberg 	unsigned long flags;
29816603153SAndreas Noever 	int ret = 0;
29922b7de10SMika Westerberg 
30022b7de10SMika Westerberg 	spin_lock_irqsave(&ring->lock, flags);
30116603153SAndreas Noever 	if (ring->running) {
30216603153SAndreas Noever 		list_add_tail(&frame->list, &ring->queue);
30316603153SAndreas Noever 		ring_write_descriptors(ring);
30416603153SAndreas Noever 	} else {
30516603153SAndreas Noever 		ret = -ESHUTDOWN;
30616603153SAndreas Noever 	}
30722b7de10SMika Westerberg 	spin_unlock_irqrestore(&ring->lock, flags);
30816603153SAndreas Noever 	return ret;
30916603153SAndreas Noever }
3103b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(__tb_ring_enqueue);
31116603153SAndreas Noever 
3124ffe722eSMika Westerberg /**
3134ffe722eSMika Westerberg  * tb_ring_poll() - Poll one completed frame from the ring
3144ffe722eSMika Westerberg  * @ring: Ring to poll
3154ffe722eSMika Westerberg  *
3164ffe722eSMika Westerberg  * This function can be called when @start_poll callback of the @ring
3174ffe722eSMika Westerberg  * has been called. It will read one completed frame from the ring and
3184ffe722eSMika Westerberg  * return it to the caller. Returns %NULL if there is no more completed
3194ffe722eSMika Westerberg  * frames.
3204ffe722eSMika Westerberg  */
3214ffe722eSMika Westerberg struct ring_frame *tb_ring_poll(struct tb_ring *ring)
3224ffe722eSMika Westerberg {
3234ffe722eSMika Westerberg 	struct ring_frame *frame = NULL;
3244ffe722eSMika Westerberg 	unsigned long flags;
3254ffe722eSMika Westerberg 
3264ffe722eSMika Westerberg 	spin_lock_irqsave(&ring->lock, flags);
3274ffe722eSMika Westerberg 	if (!ring->running)
3284ffe722eSMika Westerberg 		goto unlock;
3294ffe722eSMika Westerberg 	if (ring_empty(ring))
3304ffe722eSMika Westerberg 		goto unlock;
3314ffe722eSMika Westerberg 
3324ffe722eSMika Westerberg 	if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) {
3334ffe722eSMika Westerberg 		frame = list_first_entry(&ring->in_flight, typeof(*frame),
3344ffe722eSMika Westerberg 					 list);
3354ffe722eSMika Westerberg 		list_del_init(&frame->list);
3364ffe722eSMika Westerberg 
3374ffe722eSMika Westerberg 		if (!ring->is_tx) {
3384ffe722eSMika Westerberg 			frame->size = ring->descriptors[ring->tail].length;
3394ffe722eSMika Westerberg 			frame->eof = ring->descriptors[ring->tail].eof;
3404ffe722eSMika Westerberg 			frame->sof = ring->descriptors[ring->tail].sof;
3414ffe722eSMika Westerberg 			frame->flags = ring->descriptors[ring->tail].flags;
3424ffe722eSMika Westerberg 		}
3434ffe722eSMika Westerberg 
3444ffe722eSMika Westerberg 		ring->tail = (ring->tail + 1) % ring->size;
3454ffe722eSMika Westerberg 	}
3464ffe722eSMika Westerberg 
3474ffe722eSMika Westerberg unlock:
3484ffe722eSMika Westerberg 	spin_unlock_irqrestore(&ring->lock, flags);
3494ffe722eSMika Westerberg 	return frame;
3504ffe722eSMika Westerberg }
3514ffe722eSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_poll);
3524ffe722eSMika Westerberg 
3534ffe722eSMika Westerberg static void __ring_interrupt_mask(struct tb_ring *ring, bool mask)
3544ffe722eSMika Westerberg {
3554ffe722eSMika Westerberg 	int idx = ring_interrupt_index(ring);
3564ffe722eSMika Westerberg 	int reg = REG_RING_INTERRUPT_BASE + idx / 32 * 4;
3574ffe722eSMika Westerberg 	int bit = idx % 32;
3584ffe722eSMika Westerberg 	u32 val;
3594ffe722eSMika Westerberg 
3604ffe722eSMika Westerberg 	val = ioread32(ring->nhi->iobase + reg);
3614ffe722eSMika Westerberg 	if (mask)
3624ffe722eSMika Westerberg 		val &= ~BIT(bit);
3634ffe722eSMika Westerberg 	else
3644ffe722eSMika Westerberg 		val |= BIT(bit);
3654ffe722eSMika Westerberg 	iowrite32(val, ring->nhi->iobase + reg);
3664ffe722eSMika Westerberg }
3674ffe722eSMika Westerberg 
3684ffe722eSMika Westerberg /* Both @nhi->lock and @ring->lock should be held */
3694ffe722eSMika Westerberg static void __ring_interrupt(struct tb_ring *ring)
3704ffe722eSMika Westerberg {
3714ffe722eSMika Westerberg 	if (!ring->running)
3724ffe722eSMika Westerberg 		return;
3734ffe722eSMika Westerberg 
3744ffe722eSMika Westerberg 	if (ring->start_poll) {
37574657181SMika Westerberg 		__ring_interrupt_mask(ring, true);
3764ffe722eSMika Westerberg 		ring->start_poll(ring->poll_data);
3774ffe722eSMika Westerberg 	} else {
3784ffe722eSMika Westerberg 		schedule_work(&ring->work);
3794ffe722eSMika Westerberg 	}
3804ffe722eSMika Westerberg }
3814ffe722eSMika Westerberg 
3824ffe722eSMika Westerberg /**
3834ffe722eSMika Westerberg  * tb_ring_poll_complete() - Re-start interrupt for the ring
3844ffe722eSMika Westerberg  * @ring: Ring to re-start the interrupt
3854ffe722eSMika Westerberg  *
3864ffe722eSMika Westerberg  * This will re-start (unmask) the ring interrupt once the user is done
3874ffe722eSMika Westerberg  * with polling.
3884ffe722eSMika Westerberg  */
3894ffe722eSMika Westerberg void tb_ring_poll_complete(struct tb_ring *ring)
3904ffe722eSMika Westerberg {
3914ffe722eSMika Westerberg 	unsigned long flags;
3924ffe722eSMika Westerberg 
3934ffe722eSMika Westerberg 	spin_lock_irqsave(&ring->nhi->lock, flags);
3944ffe722eSMika Westerberg 	spin_lock(&ring->lock);
3954ffe722eSMika Westerberg 	if (ring->start_poll)
3964ffe722eSMika Westerberg 		__ring_interrupt_mask(ring, false);
3974ffe722eSMika Westerberg 	spin_unlock(&ring->lock);
3984ffe722eSMika Westerberg 	spin_unlock_irqrestore(&ring->nhi->lock, flags);
3994ffe722eSMika Westerberg }
4004ffe722eSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_poll_complete);
4014ffe722eSMika Westerberg 
4027a1808f8SSanjay R Mehta static void ring_clear_msix(const struct tb_ring *ring)
4037a1808f8SSanjay R Mehta {
404468c49f4SMario Limonciello 	int bit;
405468c49f4SMario Limonciello 
4067a1808f8SSanjay R Mehta 	if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
4077a1808f8SSanjay R Mehta 		return;
4087a1808f8SSanjay R Mehta 
409468c49f4SMario Limonciello 	bit = ring_interrupt_index(ring) & 31;
4107a1808f8SSanjay R Mehta 	if (ring->is_tx)
411468c49f4SMario Limonciello 		iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR);
4127a1808f8SSanjay R Mehta 	else
413468c49f4SMario Limonciello 		iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR +
4147a1808f8SSanjay R Mehta 			  4 * (ring->nhi->hop_count / 32));
4157a1808f8SSanjay R Mehta }
4167a1808f8SSanjay R Mehta 
417046bee1fSMika Westerberg static irqreturn_t ring_msix(int irq, void *data)
418046bee1fSMika Westerberg {
419046bee1fSMika Westerberg 	struct tb_ring *ring = data;
420046bee1fSMika Westerberg 
4214ffe722eSMika Westerberg 	spin_lock(&ring->nhi->lock);
4227a1808f8SSanjay R Mehta 	ring_clear_msix(ring);
4234ffe722eSMika Westerberg 	spin_lock(&ring->lock);
4244ffe722eSMika Westerberg 	__ring_interrupt(ring);
4254ffe722eSMika Westerberg 	spin_unlock(&ring->lock);
4264ffe722eSMika Westerberg 	spin_unlock(&ring->nhi->lock);
4274ffe722eSMika Westerberg 
428046bee1fSMika Westerberg 	return IRQ_HANDLED;
429046bee1fSMika Westerberg }
430046bee1fSMika Westerberg 
431046bee1fSMika Westerberg static int ring_request_msix(struct tb_ring *ring, bool no_suspend)
432046bee1fSMika Westerberg {
433046bee1fSMika Westerberg 	struct tb_nhi *nhi = ring->nhi;
434046bee1fSMika Westerberg 	unsigned long irqflags;
435046bee1fSMika Westerberg 	int ret;
436046bee1fSMika Westerberg 
437046bee1fSMika Westerberg 	if (!nhi->pdev->msix_enabled)
438046bee1fSMika Westerberg 		return 0;
439046bee1fSMika Westerberg 
440046bee1fSMika Westerberg 	ret = ida_simple_get(&nhi->msix_ida, 0, MSIX_MAX_VECS, GFP_KERNEL);
441046bee1fSMika Westerberg 	if (ret < 0)
442046bee1fSMika Westerberg 		return ret;
443046bee1fSMika Westerberg 
444046bee1fSMika Westerberg 	ring->vector = ret;
445046bee1fSMika Westerberg 
4467342ca34SJing Xiangfeng 	ret = pci_irq_vector(ring->nhi->pdev, ring->vector);
4477342ca34SJing Xiangfeng 	if (ret < 0)
4487342ca34SJing Xiangfeng 		goto err_ida_remove;
4497342ca34SJing Xiangfeng 
4507342ca34SJing Xiangfeng 	ring->irq = ret;
451046bee1fSMika Westerberg 
452046bee1fSMika Westerberg 	irqflags = no_suspend ? IRQF_NO_SUSPEND : 0;
4537342ca34SJing Xiangfeng 	ret = request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring);
4547342ca34SJing Xiangfeng 	if (ret)
4557342ca34SJing Xiangfeng 		goto err_ida_remove;
4567342ca34SJing Xiangfeng 
4577342ca34SJing Xiangfeng 	return 0;
4587342ca34SJing Xiangfeng 
4597342ca34SJing Xiangfeng err_ida_remove:
4607342ca34SJing Xiangfeng 	ida_simple_remove(&nhi->msix_ida, ring->vector);
4617342ca34SJing Xiangfeng 
4627342ca34SJing Xiangfeng 	return ret;
463046bee1fSMika Westerberg }
464046bee1fSMika Westerberg 
465046bee1fSMika Westerberg static void ring_release_msix(struct tb_ring *ring)
466046bee1fSMika Westerberg {
467046bee1fSMika Westerberg 	if (ring->irq <= 0)
468046bee1fSMika Westerberg 		return;
469046bee1fSMika Westerberg 
470046bee1fSMika Westerberg 	free_irq(ring->irq, ring);
471046bee1fSMika Westerberg 	ida_simple_remove(&ring->nhi->msix_ida, ring->vector);
472046bee1fSMika Westerberg 	ring->vector = 0;
473046bee1fSMika Westerberg 	ring->irq = 0;
474046bee1fSMika Westerberg }
475046bee1fSMika Westerberg 
4769a01c7c2SMika Westerberg static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring)
4779a01c7c2SMika Westerberg {
47854669e2fSMika Westerberg 	unsigned int start_hop = RING_FIRST_USABLE_HOPID;
4799a01c7c2SMika Westerberg 	int ret = 0;
4809a01c7c2SMika Westerberg 
48154669e2fSMika Westerberg 	if (nhi->quirks & QUIRK_E2E) {
48254669e2fSMika Westerberg 		start_hop = RING_FIRST_USABLE_HOPID + 1;
48354669e2fSMika Westerberg 		if (ring->flags & RING_FLAG_E2E && !ring->is_tx) {
48454669e2fSMika Westerberg 			dev_dbg(&nhi->pdev->dev, "quirking E2E TX HopID %u -> %u\n",
48554669e2fSMika Westerberg 				ring->e2e_tx_hop, RING_E2E_RESERVED_HOPID);
48654669e2fSMika Westerberg 			ring->e2e_tx_hop = RING_E2E_RESERVED_HOPID;
48754669e2fSMika Westerberg 		}
48854669e2fSMika Westerberg 	}
48954669e2fSMika Westerberg 
4909a01c7c2SMika Westerberg 	spin_lock_irq(&nhi->lock);
4919a01c7c2SMika Westerberg 
4929a01c7c2SMika Westerberg 	if (ring->hop < 0) {
4939a01c7c2SMika Westerberg 		unsigned int i;
4949a01c7c2SMika Westerberg 
4959a01c7c2SMika Westerberg 		/*
4969a01c7c2SMika Westerberg 		 * Automatically allocate HopID from the non-reserved
49753f13319SMika Westerberg 		 * range 1 .. hop_count - 1.
4989a01c7c2SMika Westerberg 		 */
49954669e2fSMika Westerberg 		for (i = start_hop; i < nhi->hop_count; i++) {
5009a01c7c2SMika Westerberg 			if (ring->is_tx) {
5019a01c7c2SMika Westerberg 				if (!nhi->tx_rings[i]) {
5029a01c7c2SMika Westerberg 					ring->hop = i;
5039a01c7c2SMika Westerberg 					break;
5049a01c7c2SMika Westerberg 				}
5059a01c7c2SMika Westerberg 			} else {
5069a01c7c2SMika Westerberg 				if (!nhi->rx_rings[i]) {
5079a01c7c2SMika Westerberg 					ring->hop = i;
5089a01c7c2SMika Westerberg 					break;
5099a01c7c2SMika Westerberg 				}
5109a01c7c2SMika Westerberg 			}
5119a01c7c2SMika Westerberg 		}
5129a01c7c2SMika Westerberg 	}
5139a01c7c2SMika Westerberg 
51454669e2fSMika Westerberg 	if (ring->hop > 0 && ring->hop < start_hop) {
51554669e2fSMika Westerberg 		dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
51654669e2fSMika Westerberg 		ret = -EINVAL;
51754669e2fSMika Westerberg 		goto err_unlock;
51854669e2fSMika Westerberg 	}
5199a01c7c2SMika Westerberg 	if (ring->hop < 0 || ring->hop >= nhi->hop_count) {
5209a01c7c2SMika Westerberg 		dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
5219a01c7c2SMika Westerberg 		ret = -EINVAL;
5229a01c7c2SMika Westerberg 		goto err_unlock;
5239a01c7c2SMika Westerberg 	}
5249a01c7c2SMika Westerberg 	if (ring->is_tx && nhi->tx_rings[ring->hop]) {
5259a01c7c2SMika Westerberg 		dev_warn(&nhi->pdev->dev, "TX hop %d already allocated\n",
5269a01c7c2SMika Westerberg 			 ring->hop);
5279a01c7c2SMika Westerberg 		ret = -EBUSY;
5289a01c7c2SMika Westerberg 		goto err_unlock;
5299a01c7c2SMika Westerberg 	} else if (!ring->is_tx && nhi->rx_rings[ring->hop]) {
5309a01c7c2SMika Westerberg 		dev_warn(&nhi->pdev->dev, "RX hop %d already allocated\n",
5319a01c7c2SMika Westerberg 			 ring->hop);
5329a01c7c2SMika Westerberg 		ret = -EBUSY;
5339a01c7c2SMika Westerberg 		goto err_unlock;
5349a01c7c2SMika Westerberg 	}
5359a01c7c2SMika Westerberg 
5369a01c7c2SMika Westerberg 	if (ring->is_tx)
5379a01c7c2SMika Westerberg 		nhi->tx_rings[ring->hop] = ring;
5389a01c7c2SMika Westerberg 	else
5399a01c7c2SMika Westerberg 		nhi->rx_rings[ring->hop] = ring;
5409a01c7c2SMika Westerberg 
5419a01c7c2SMika Westerberg err_unlock:
5429a01c7c2SMika Westerberg 	spin_unlock_irq(&nhi->lock);
5439a01c7c2SMika Westerberg 
5449a01c7c2SMika Westerberg 	return ret;
5459a01c7c2SMika Westerberg }
5469a01c7c2SMika Westerberg 
5473b3d9f4dSMika Westerberg static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
5489fb1e654SMika Westerberg 				     bool transmit, unsigned int flags,
549afe704a2SMika Westerberg 				     int e2e_tx_hop, u16 sof_mask, u16 eof_mask,
5504ffe722eSMika Westerberg 				     void (*start_poll)(void *),
5514ffe722eSMika Westerberg 				     void *poll_data)
55216603153SAndreas Noever {
55316603153SAndreas Noever 	struct tb_ring *ring = NULL;
554daa5140fSMika Westerberg 
555daa5140fSMika Westerberg 	dev_dbg(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
55616603153SAndreas Noever 		transmit ? "TX" : "RX", hop, size);
55716603153SAndreas Noever 
55816603153SAndreas Noever 	ring = kzalloc(sizeof(*ring), GFP_KERNEL);
55916603153SAndreas Noever 	if (!ring)
56059120e06SMika Westerberg 		return NULL;
56116603153SAndreas Noever 
56222b7de10SMika Westerberg 	spin_lock_init(&ring->lock);
56316603153SAndreas Noever 	INIT_LIST_HEAD(&ring->queue);
56416603153SAndreas Noever 	INIT_LIST_HEAD(&ring->in_flight);
56516603153SAndreas Noever 	INIT_WORK(&ring->work, ring_work);
56616603153SAndreas Noever 
56716603153SAndreas Noever 	ring->nhi = nhi;
56816603153SAndreas Noever 	ring->hop = hop;
56916603153SAndreas Noever 	ring->is_tx = transmit;
57016603153SAndreas Noever 	ring->size = size;
571046bee1fSMika Westerberg 	ring->flags = flags;
572afe704a2SMika Westerberg 	ring->e2e_tx_hop = e2e_tx_hop;
5739fb1e654SMika Westerberg 	ring->sof_mask = sof_mask;
5749fb1e654SMika Westerberg 	ring->eof_mask = eof_mask;
57516603153SAndreas Noever 	ring->head = 0;
57616603153SAndreas Noever 	ring->tail = 0;
57716603153SAndreas Noever 	ring->running = false;
5784ffe722eSMika Westerberg 	ring->start_poll = start_poll;
5794ffe722eSMika Westerberg 	ring->poll_data = poll_data;
580046bee1fSMika Westerberg 
58116603153SAndreas Noever 	ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
58216603153SAndreas Noever 			size * sizeof(*ring->descriptors),
58316603153SAndreas Noever 			&ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
58416603153SAndreas Noever 	if (!ring->descriptors)
58559120e06SMika Westerberg 		goto err_free_ring;
58616603153SAndreas Noever 
58759120e06SMika Westerberg 	if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND))
58859120e06SMika Westerberg 		goto err_free_descs;
58959120e06SMika Westerberg 
5909a01c7c2SMika Westerberg 	if (nhi_alloc_hop(nhi, ring))
59159120e06SMika Westerberg 		goto err_release_msix;
59259120e06SMika Westerberg 
59316603153SAndreas Noever 	return ring;
59416603153SAndreas Noever 
59559120e06SMika Westerberg err_release_msix:
59659120e06SMika Westerberg 	ring_release_msix(ring);
59759120e06SMika Westerberg err_free_descs:
59859120e06SMika Westerberg 	dma_free_coherent(&ring->nhi->pdev->dev,
59959120e06SMika Westerberg 			  ring->size * sizeof(*ring->descriptors),
60059120e06SMika Westerberg 			  ring->descriptors, ring->descriptors_dma);
60159120e06SMika Westerberg err_free_ring:
60216603153SAndreas Noever 	kfree(ring);
60359120e06SMika Westerberg 
60416603153SAndreas Noever 	return NULL;
60516603153SAndreas Noever }
60616603153SAndreas Noever 
6073b3d9f4dSMika Westerberg /**
6083b3d9f4dSMika Westerberg  * tb_ring_alloc_tx() - Allocate DMA ring for transmit
6093b3d9f4dSMika Westerberg  * @nhi: Pointer to the NHI the ring is to be allocated
6103b3d9f4dSMika Westerberg  * @hop: HopID (ring) to allocate
6113b3d9f4dSMika Westerberg  * @size: Number of entries in the ring
6123b3d9f4dSMika Westerberg  * @flags: Flags for the ring
6133b3d9f4dSMika Westerberg  */
6143b3d9f4dSMika Westerberg struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
615046bee1fSMika Westerberg 				 unsigned int flags)
61616603153SAndreas Noever {
617afe704a2SMika Westerberg 	return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0, 0, NULL, NULL);
61816603153SAndreas Noever }
6193b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_alloc_tx);
62016603153SAndreas Noever 
62116603153SAndreas Noever /**
6223b3d9f4dSMika Westerberg  * tb_ring_alloc_rx() - Allocate DMA ring for receive
6233b3d9f4dSMika Westerberg  * @nhi: Pointer to the NHI the ring is to be allocated
6249a01c7c2SMika Westerberg  * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
6253b3d9f4dSMika Westerberg  * @size: Number of entries in the ring
6263b3d9f4dSMika Westerberg  * @flags: Flags for the ring
627afe704a2SMika Westerberg  * @e2e_tx_hop: Transmit HopID when E2E is enabled in @flags
6283b3d9f4dSMika Westerberg  * @sof_mask: Mask of PDF values that start a frame
6293b3d9f4dSMika Westerberg  * @eof_mask: Mask of PDF values that end a frame
6304ffe722eSMika Westerberg  * @start_poll: If not %NULL the ring will call this function when an
6314ffe722eSMika Westerberg  *		interrupt is triggered and masked, instead of callback
6324ffe722eSMika Westerberg  *		in each Rx frame.
6334ffe722eSMika Westerberg  * @poll_data: Optional data passed to @start_poll
63416603153SAndreas Noever  */
6353b3d9f4dSMika Westerberg struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
636afe704a2SMika Westerberg 				 unsigned int flags, int e2e_tx_hop,
637afe704a2SMika Westerberg 				 u16 sof_mask, u16 eof_mask,
6384ffe722eSMika Westerberg 				 void (*start_poll)(void *), void *poll_data)
6393b3d9f4dSMika Westerberg {
640afe704a2SMika Westerberg 	return tb_ring_alloc(nhi, hop, size, false, flags, e2e_tx_hop, sof_mask, eof_mask,
6414ffe722eSMika Westerberg 			     start_poll, poll_data);
6423b3d9f4dSMika Westerberg }
6433b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_alloc_rx);
6443b3d9f4dSMika Westerberg 
6453b3d9f4dSMika Westerberg /**
6463b3d9f4dSMika Westerberg  * tb_ring_start() - enable a ring
6476894bd37SMika Westerberg  * @ring: Ring to start
6483b3d9f4dSMika Westerberg  *
6493b3d9f4dSMika Westerberg  * Must not be invoked in parallel with tb_ring_stop().
6503b3d9f4dSMika Westerberg  */
6513b3d9f4dSMika Westerberg void tb_ring_start(struct tb_ring *ring)
65216603153SAndreas Noever {
6539fb1e654SMika Westerberg 	u16 frame_size;
6549fb1e654SMika Westerberg 	u32 flags;
6559fb1e654SMika Westerberg 
65659120e06SMika Westerberg 	spin_lock_irq(&ring->nhi->lock);
65759120e06SMika Westerberg 	spin_lock(&ring->lock);
658bdccf295SMika Westerberg 	if (ring->nhi->going_away)
659bdccf295SMika Westerberg 		goto err;
66016603153SAndreas Noever 	if (ring->running) {
66116603153SAndreas Noever 		dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
66216603153SAndreas Noever 		goto err;
66316603153SAndreas Noever 	}
664daa5140fSMika Westerberg 	dev_dbg(&ring->nhi->pdev->dev, "starting %s %d\n",
66516603153SAndreas Noever 		RING_TYPE(ring), ring->hop);
66616603153SAndreas Noever 
6679fb1e654SMika Westerberg 	if (ring->flags & RING_FLAG_FRAME) {
6689fb1e654SMika Westerberg 		/* Means 4096 */
6699fb1e654SMika Westerberg 		frame_size = 0;
6709fb1e654SMika Westerberg 		flags = RING_FLAG_ENABLE;
6719fb1e654SMika Westerberg 	} else {
6729fb1e654SMika Westerberg 		frame_size = TB_FRAME_SIZE;
6739fb1e654SMika Westerberg 		flags = RING_FLAG_ENABLE | RING_FLAG_RAW;
6749fb1e654SMika Westerberg 	}
6759fb1e654SMika Westerberg 
67616603153SAndreas Noever 	ring_iowrite64desc(ring, ring->descriptors_dma, 0);
67716603153SAndreas Noever 	if (ring->is_tx) {
67816603153SAndreas Noever 		ring_iowrite32desc(ring, ring->size, 12);
67916603153SAndreas Noever 		ring_iowrite32options(ring, 0, 4); /* time releated ? */
6809fb1e654SMika Westerberg 		ring_iowrite32options(ring, flags, 0);
68116603153SAndreas Noever 	} else {
6829fb1e654SMika Westerberg 		u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask;
6839fb1e654SMika Westerberg 
6849fb1e654SMika Westerberg 		ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12);
6859fb1e654SMika Westerberg 		ring_iowrite32options(ring, sof_eof_mask, 4);
6869fb1e654SMika Westerberg 		ring_iowrite32options(ring, flags, 0);
68716603153SAndreas Noever 	}
688afe704a2SMika Westerberg 
689afe704a2SMika Westerberg 	/*
690afe704a2SMika Westerberg 	 * Now that the ring valid bit is set we can configure E2E if
691afe704a2SMika Westerberg 	 * enabled for the ring.
692afe704a2SMika Westerberg 	 */
693afe704a2SMika Westerberg 	if (ring->flags & RING_FLAG_E2E) {
694afe704a2SMika Westerberg 		if (!ring->is_tx) {
695afe704a2SMika Westerberg 			u32 hop;
696afe704a2SMika Westerberg 
697afe704a2SMika Westerberg 			hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT;
698afe704a2SMika Westerberg 			hop &= REG_RX_OPTIONS_E2E_HOP_MASK;
699afe704a2SMika Westerberg 			flags |= hop;
700afe704a2SMika Westerberg 
701afe704a2SMika Westerberg 			dev_dbg(&ring->nhi->pdev->dev,
702afe704a2SMika Westerberg 				"enabling E2E for %s %d with TX HopID %d\n",
703afe704a2SMika Westerberg 				RING_TYPE(ring), ring->hop, ring->e2e_tx_hop);
704afe704a2SMika Westerberg 		} else {
705afe704a2SMika Westerberg 			dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d\n",
706afe704a2SMika Westerberg 				RING_TYPE(ring), ring->hop);
707afe704a2SMika Westerberg 		}
708afe704a2SMika Westerberg 
709afe704a2SMika Westerberg 		flags |= RING_FLAG_E2E_FLOW_CONTROL;
710afe704a2SMika Westerberg 		ring_iowrite32options(ring, flags, 0);
711afe704a2SMika Westerberg 	}
712afe704a2SMika Westerberg 
71316603153SAndreas Noever 	ring_interrupt_active(ring, true);
71416603153SAndreas Noever 	ring->running = true;
71516603153SAndreas Noever err:
71659120e06SMika Westerberg 	spin_unlock(&ring->lock);
71759120e06SMika Westerberg 	spin_unlock_irq(&ring->nhi->lock);
71816603153SAndreas Noever }
7193b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_start);
72016603153SAndreas Noever 
72116603153SAndreas Noever /**
7223b3d9f4dSMika Westerberg  * tb_ring_stop() - shutdown a ring
7236894bd37SMika Westerberg  * @ring: Ring to stop
72416603153SAndreas Noever  *
72516603153SAndreas Noever  * Must not be invoked from a callback.
72616603153SAndreas Noever  *
7273b3d9f4dSMika Westerberg  * This method will disable the ring. Further calls to
7283b3d9f4dSMika Westerberg  * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been
7293b3d9f4dSMika Westerberg  * called.
73016603153SAndreas Noever  *
73116603153SAndreas Noever  * All enqueued frames will be canceled and their callbacks will be executed
73216603153SAndreas Noever  * with frame->canceled set to true (on the callback thread). This method
73316603153SAndreas Noever  * returns only after all callback invocations have finished.
73416603153SAndreas Noever  */
7353b3d9f4dSMika Westerberg void tb_ring_stop(struct tb_ring *ring)
73616603153SAndreas Noever {
73759120e06SMika Westerberg 	spin_lock_irq(&ring->nhi->lock);
73859120e06SMika Westerberg 	spin_lock(&ring->lock);
739daa5140fSMika Westerberg 	dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n",
74016603153SAndreas Noever 		RING_TYPE(ring), ring->hop);
741bdccf295SMika Westerberg 	if (ring->nhi->going_away)
742bdccf295SMika Westerberg 		goto err;
74316603153SAndreas Noever 	if (!ring->running) {
74416603153SAndreas Noever 		dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
74516603153SAndreas Noever 			 RING_TYPE(ring), ring->hop);
74616603153SAndreas Noever 		goto err;
74716603153SAndreas Noever 	}
74816603153SAndreas Noever 	ring_interrupt_active(ring, false);
74916603153SAndreas Noever 
75016603153SAndreas Noever 	ring_iowrite32options(ring, 0, 0);
75116603153SAndreas Noever 	ring_iowrite64desc(ring, 0, 0);
75294379521SMika Westerberg 	ring_iowrite32desc(ring, 0, 8);
75316603153SAndreas Noever 	ring_iowrite32desc(ring, 0, 12);
75416603153SAndreas Noever 	ring->head = 0;
75516603153SAndreas Noever 	ring->tail = 0;
75616603153SAndreas Noever 	ring->running = false;
75716603153SAndreas Noever 
75816603153SAndreas Noever err:
75959120e06SMika Westerberg 	spin_unlock(&ring->lock);
76059120e06SMika Westerberg 	spin_unlock_irq(&ring->nhi->lock);
76116603153SAndreas Noever 
76216603153SAndreas Noever 	/*
76316603153SAndreas Noever 	 * schedule ring->work to invoke callbacks on all remaining frames.
76416603153SAndreas Noever 	 */
76516603153SAndreas Noever 	schedule_work(&ring->work);
76616603153SAndreas Noever 	flush_work(&ring->work);
76716603153SAndreas Noever }
7683b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_stop);
76916603153SAndreas Noever 
77016603153SAndreas Noever /*
7713b3d9f4dSMika Westerberg  * tb_ring_free() - free ring
77216603153SAndreas Noever  *
77316603153SAndreas Noever  * When this method returns all invocations of ring->callback will have
77416603153SAndreas Noever  * finished.
77516603153SAndreas Noever  *
77616603153SAndreas Noever  * Ring must be stopped.
77716603153SAndreas Noever  *
77816603153SAndreas Noever  * Must NOT be called from ring_frame->callback!
77916603153SAndreas Noever  */
7803b3d9f4dSMika Westerberg void tb_ring_free(struct tb_ring *ring)
78116603153SAndreas Noever {
78259120e06SMika Westerberg 	spin_lock_irq(&ring->nhi->lock);
78316603153SAndreas Noever 	/*
78416603153SAndreas Noever 	 * Dissociate the ring from the NHI. This also ensures that
78516603153SAndreas Noever 	 * nhi_interrupt_work cannot reschedule ring->work.
78616603153SAndreas Noever 	 */
78716603153SAndreas Noever 	if (ring->is_tx)
78816603153SAndreas Noever 		ring->nhi->tx_rings[ring->hop] = NULL;
78916603153SAndreas Noever 	else
79016603153SAndreas Noever 		ring->nhi->rx_rings[ring->hop] = NULL;
79116603153SAndreas Noever 
79216603153SAndreas Noever 	if (ring->running) {
79316603153SAndreas Noever 		dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
79416603153SAndreas Noever 			 RING_TYPE(ring), ring->hop);
79516603153SAndreas Noever 	}
7964ffe722eSMika Westerberg 	spin_unlock_irq(&ring->nhi->lock);
79716603153SAndreas Noever 
798046bee1fSMika Westerberg 	ring_release_msix(ring);
799046bee1fSMika Westerberg 
80016603153SAndreas Noever 	dma_free_coherent(&ring->nhi->pdev->dev,
80116603153SAndreas Noever 			  ring->size * sizeof(*ring->descriptors),
80216603153SAndreas Noever 			  ring->descriptors, ring->descriptors_dma);
80316603153SAndreas Noever 
804f19b72c6SSachin Kamat 	ring->descriptors = NULL;
80516603153SAndreas Noever 	ring->descriptors_dma = 0;
80616603153SAndreas Noever 
80716603153SAndreas Noever 
808daa5140fSMika Westerberg 	dev_dbg(&ring->nhi->pdev->dev, "freeing %s %d\n", RING_TYPE(ring),
80916603153SAndreas Noever 		ring->hop);
81016603153SAndreas Noever 
811a7bfb27bSLee Jones 	/*
812046bee1fSMika Westerberg 	 * ring->work can no longer be scheduled (it is scheduled only
813046bee1fSMika Westerberg 	 * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
814046bee1fSMika Westerberg 	 * to finish before freeing the ring.
81516603153SAndreas Noever 	 */
81616603153SAndreas Noever 	flush_work(&ring->work);
81716603153SAndreas Noever 	kfree(ring);
81816603153SAndreas Noever }
8193b3d9f4dSMika Westerberg EXPORT_SYMBOL_GPL(tb_ring_free);
82016603153SAndreas Noever 
821cd446ee2SMika Westerberg /**
822cd446ee2SMika Westerberg  * nhi_mailbox_cmd() - Send a command through NHI mailbox
823cd446ee2SMika Westerberg  * @nhi: Pointer to the NHI structure
824cd446ee2SMika Westerberg  * @cmd: Command to send
825cd446ee2SMika Westerberg  * @data: Data to be send with the command
826cd446ee2SMika Westerberg  *
827cd446ee2SMika Westerberg  * Sends mailbox command to the firmware running on NHI. Returns %0 in
828cd446ee2SMika Westerberg  * case of success and negative errno in case of failure.
829cd446ee2SMika Westerberg  */
830cd446ee2SMika Westerberg int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
831cd446ee2SMika Westerberg {
832cd446ee2SMika Westerberg 	ktime_t timeout;
833cd446ee2SMika Westerberg 	u32 val;
834cd446ee2SMika Westerberg 
835cd446ee2SMika Westerberg 	iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
836cd446ee2SMika Westerberg 
837cd446ee2SMika Westerberg 	val = ioread32(nhi->iobase + REG_INMAIL_CMD);
838cd446ee2SMika Westerberg 	val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
839cd446ee2SMika Westerberg 	val |= REG_INMAIL_OP_REQUEST | cmd;
840cd446ee2SMika Westerberg 	iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
841cd446ee2SMika Westerberg 
842cd446ee2SMika Westerberg 	timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT);
843cd446ee2SMika Westerberg 	do {
844cd446ee2SMika Westerberg 		val = ioread32(nhi->iobase + REG_INMAIL_CMD);
845cd446ee2SMika Westerberg 		if (!(val & REG_INMAIL_OP_REQUEST))
846cd446ee2SMika Westerberg 			break;
847cd446ee2SMika Westerberg 		usleep_range(10, 20);
848cd446ee2SMika Westerberg 	} while (ktime_before(ktime_get(), timeout));
849cd446ee2SMika Westerberg 
850cd446ee2SMika Westerberg 	if (val & REG_INMAIL_OP_REQUEST)
851cd446ee2SMika Westerberg 		return -ETIMEDOUT;
852cd446ee2SMika Westerberg 	if (val & REG_INMAIL_ERROR)
853cd446ee2SMika Westerberg 		return -EIO;
854cd446ee2SMika Westerberg 
855cd446ee2SMika Westerberg 	return 0;
856cd446ee2SMika Westerberg }
857cd446ee2SMika Westerberg 
858cd446ee2SMika Westerberg /**
859cd446ee2SMika Westerberg  * nhi_mailbox_mode() - Return current firmware operation mode
860cd446ee2SMika Westerberg  * @nhi: Pointer to the NHI structure
861cd446ee2SMika Westerberg  *
862cd446ee2SMika Westerberg  * The function reads current firmware operation mode using NHI mailbox
863cd446ee2SMika Westerberg  * registers and returns it to the caller.
864cd446ee2SMika Westerberg  */
865cd446ee2SMika Westerberg enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
866cd446ee2SMika Westerberg {
867cd446ee2SMika Westerberg 	u32 val;
868cd446ee2SMika Westerberg 
869cd446ee2SMika Westerberg 	val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
870cd446ee2SMika Westerberg 	val &= REG_OUTMAIL_CMD_OPMODE_MASK;
871cd446ee2SMika Westerberg 	val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
872cd446ee2SMika Westerberg 
873cd446ee2SMika Westerberg 	return (enum nhi_fw_mode)val;
874cd446ee2SMika Westerberg }
875cd446ee2SMika Westerberg 
87616603153SAndreas Noever static void nhi_interrupt_work(struct work_struct *work)
87716603153SAndreas Noever {
87816603153SAndreas Noever 	struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
87916603153SAndreas Noever 	int value = 0; /* Suppress uninitialized usage warning. */
88016603153SAndreas Noever 	int bit;
88116603153SAndreas Noever 	int hop = -1;
88216603153SAndreas Noever 	int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
88316603153SAndreas Noever 	struct tb_ring *ring;
88416603153SAndreas Noever 
88559120e06SMika Westerberg 	spin_lock_irq(&nhi->lock);
88616603153SAndreas Noever 
88716603153SAndreas Noever 	/*
88816603153SAndreas Noever 	 * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
88916603153SAndreas Noever 	 * (TX, RX, RX overflow). We iterate over the bits and read a new
89016603153SAndreas Noever 	 * dwords as required. The registers are cleared on read.
89116603153SAndreas Noever 	 */
89216603153SAndreas Noever 	for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
89316603153SAndreas Noever 		if (bit % 32 == 0)
89416603153SAndreas Noever 			value = ioread32(nhi->iobase
89516603153SAndreas Noever 					 + REG_RING_NOTIFY_BASE
89616603153SAndreas Noever 					 + 4 * (bit / 32));
89716603153SAndreas Noever 		if (++hop == nhi->hop_count) {
89816603153SAndreas Noever 			hop = 0;
89916603153SAndreas Noever 			type++;
90016603153SAndreas Noever 		}
90116603153SAndreas Noever 		if ((value & (1 << (bit % 32))) == 0)
90216603153SAndreas Noever 			continue;
90316603153SAndreas Noever 		if (type == 2) {
90416603153SAndreas Noever 			dev_warn(&nhi->pdev->dev,
90516603153SAndreas Noever 				 "RX overflow for ring %d\n",
90616603153SAndreas Noever 				 hop);
90716603153SAndreas Noever 			continue;
90816603153SAndreas Noever 		}
90916603153SAndreas Noever 		if (type == 0)
91016603153SAndreas Noever 			ring = nhi->tx_rings[hop];
91116603153SAndreas Noever 		else
91216603153SAndreas Noever 			ring = nhi->rx_rings[hop];
91316603153SAndreas Noever 		if (ring == NULL) {
91416603153SAndreas Noever 			dev_warn(&nhi->pdev->dev,
91516603153SAndreas Noever 				 "got interrupt for inactive %s ring %d\n",
91616603153SAndreas Noever 				 type ? "RX" : "TX",
91716603153SAndreas Noever 				 hop);
91816603153SAndreas Noever 			continue;
91916603153SAndreas Noever 		}
9204ffe722eSMika Westerberg 
9214ffe722eSMika Westerberg 		spin_lock(&ring->lock);
9224ffe722eSMika Westerberg 		__ring_interrupt(ring);
9234ffe722eSMika Westerberg 		spin_unlock(&ring->lock);
92416603153SAndreas Noever 	}
92559120e06SMika Westerberg 	spin_unlock_irq(&nhi->lock);
92616603153SAndreas Noever }
92716603153SAndreas Noever 
92816603153SAndreas Noever static irqreturn_t nhi_msi(int irq, void *data)
92916603153SAndreas Noever {
93016603153SAndreas Noever 	struct tb_nhi *nhi = data;
93116603153SAndreas Noever 	schedule_work(&nhi->interrupt_work);
93216603153SAndreas Noever 	return IRQ_HANDLED;
93316603153SAndreas Noever }
93416603153SAndreas Noever 
9353cdb9446SMika Westerberg static int __nhi_suspend_noirq(struct device *dev, bool wakeup)
93623dd5bb4SAndreas Noever {
93723dd5bb4SAndreas Noever 	struct pci_dev *pdev = to_pci_dev(dev);
93823dd5bb4SAndreas Noever 	struct tb *tb = pci_get_drvdata(pdev);
9393cdb9446SMika Westerberg 	struct tb_nhi *nhi = tb->nhi;
9403cdb9446SMika Westerberg 	int ret;
9419d3cce0bSMika Westerberg 
9423cdb9446SMika Westerberg 	ret = tb_domain_suspend_noirq(tb);
9433cdb9446SMika Westerberg 	if (ret)
9443cdb9446SMika Westerberg 		return ret;
9453cdb9446SMika Westerberg 
9463cdb9446SMika Westerberg 	if (nhi->ops && nhi->ops->suspend_noirq) {
9473cdb9446SMika Westerberg 		ret = nhi->ops->suspend_noirq(tb->nhi, wakeup);
9483cdb9446SMika Westerberg 		if (ret)
9493cdb9446SMika Westerberg 			return ret;
9503cdb9446SMika Westerberg 	}
9513cdb9446SMika Westerberg 
9523cdb9446SMika Westerberg 	return 0;
9533cdb9446SMika Westerberg }
9543cdb9446SMika Westerberg 
9553cdb9446SMika Westerberg static int nhi_suspend_noirq(struct device *dev)
9563cdb9446SMika Westerberg {
9573cdb9446SMika Westerberg 	return __nhi_suspend_noirq(dev, device_may_wakeup(dev));
9583cdb9446SMika Westerberg }
9593cdb9446SMika Westerberg 
960884e4d57SMika Westerberg static int nhi_freeze_noirq(struct device *dev)
961884e4d57SMika Westerberg {
962884e4d57SMika Westerberg 	struct pci_dev *pdev = to_pci_dev(dev);
963884e4d57SMika Westerberg 	struct tb *tb = pci_get_drvdata(pdev);
964884e4d57SMika Westerberg 
965884e4d57SMika Westerberg 	return tb_domain_freeze_noirq(tb);
966884e4d57SMika Westerberg }
967884e4d57SMika Westerberg 
968884e4d57SMika Westerberg static int nhi_thaw_noirq(struct device *dev)
969884e4d57SMika Westerberg {
970884e4d57SMika Westerberg 	struct pci_dev *pdev = to_pci_dev(dev);
971884e4d57SMika Westerberg 	struct tb *tb = pci_get_drvdata(pdev);
972884e4d57SMika Westerberg 
973884e4d57SMika Westerberg 	return tb_domain_thaw_noirq(tb);
974884e4d57SMika Westerberg }
975884e4d57SMika Westerberg 
9763cdb9446SMika Westerberg static bool nhi_wake_supported(struct pci_dev *pdev)
9773cdb9446SMika Westerberg {
9783cdb9446SMika Westerberg 	u8 val;
9793cdb9446SMika Westerberg 
9803cdb9446SMika Westerberg 	/*
9813cdb9446SMika Westerberg 	 * If power rails are sustainable for wakeup from S4 this
9823cdb9446SMika Westerberg 	 * property is set by the BIOS.
9833cdb9446SMika Westerberg 	 */
9843cdb9446SMika Westerberg 	if (device_property_read_u8(&pdev->dev, "WAKE_SUPPORTED", &val))
9853cdb9446SMika Westerberg 		return !!val;
9863cdb9446SMika Westerberg 
9873cdb9446SMika Westerberg 	return true;
9883cdb9446SMika Westerberg }
9893cdb9446SMika Westerberg 
9903cdb9446SMika Westerberg static int nhi_poweroff_noirq(struct device *dev)
9913cdb9446SMika Westerberg {
9923cdb9446SMika Westerberg 	struct pci_dev *pdev = to_pci_dev(dev);
9933cdb9446SMika Westerberg 	bool wakeup;
9943cdb9446SMika Westerberg 
9953cdb9446SMika Westerberg 	wakeup = device_may_wakeup(dev) && nhi_wake_supported(pdev);
9963cdb9446SMika Westerberg 	return __nhi_suspend_noirq(dev, wakeup);
99723dd5bb4SAndreas Noever }
99823dd5bb4SAndreas Noever 
9998c6bba10SMika Westerberg static void nhi_enable_int_throttling(struct tb_nhi *nhi)
10008c6bba10SMika Westerberg {
10018c6bba10SMika Westerberg 	/* Throttling is specified in 256ns increments */
10028c6bba10SMika Westerberg 	u32 throttle = DIV_ROUND_UP(128 * NSEC_PER_USEC, 256);
10038c6bba10SMika Westerberg 	unsigned int i;
10048c6bba10SMika Westerberg 
10058c6bba10SMika Westerberg 	/*
10068c6bba10SMika Westerberg 	 * Configure interrupt throttling for all vectors even if we
10078c6bba10SMika Westerberg 	 * only use few.
10088c6bba10SMika Westerberg 	 */
10098c6bba10SMika Westerberg 	for (i = 0; i < MSIX_MAX_VECS; i++) {
10108c6bba10SMika Westerberg 		u32 reg = REG_INT_THROTTLING_RATE + i * 4;
10118c6bba10SMika Westerberg 		iowrite32(throttle, nhi->iobase + reg);
10128c6bba10SMika Westerberg 	}
10138c6bba10SMika Westerberg }
10148c6bba10SMika Westerberg 
101523dd5bb4SAndreas Noever static int nhi_resume_noirq(struct device *dev)
101623dd5bb4SAndreas Noever {
101723dd5bb4SAndreas Noever 	struct pci_dev *pdev = to_pci_dev(dev);
101823dd5bb4SAndreas Noever 	struct tb *tb = pci_get_drvdata(pdev);
10193cdb9446SMika Westerberg 	struct tb_nhi *nhi = tb->nhi;
10203cdb9446SMika Westerberg 	int ret;
10219d3cce0bSMika Westerberg 
1022bdccf295SMika Westerberg 	/*
1023bdccf295SMika Westerberg 	 * Check that the device is still there. It may be that the user
1024bdccf295SMika Westerberg 	 * unplugged last device which causes the host controller to go
1025bdccf295SMika Westerberg 	 * away on PCs.
1026bdccf295SMika Westerberg 	 */
10273cdb9446SMika Westerberg 	if (!pci_device_is_present(pdev)) {
10283cdb9446SMika Westerberg 		nhi->going_away = true;
10293cdb9446SMika Westerberg 	} else {
10303cdb9446SMika Westerberg 		if (nhi->ops && nhi->ops->resume_noirq) {
10313cdb9446SMika Westerberg 			ret = nhi->ops->resume_noirq(nhi);
10323cdb9446SMika Westerberg 			if (ret)
10333cdb9446SMika Westerberg 				return ret;
10343cdb9446SMika Westerberg 		}
10358c6bba10SMika Westerberg 		nhi_enable_int_throttling(tb->nhi);
10363cdb9446SMika Westerberg 	}
1037bdccf295SMika Westerberg 
10389d3cce0bSMika Westerberg 	return tb_domain_resume_noirq(tb);
103923dd5bb4SAndreas Noever }
104023dd5bb4SAndreas Noever 
1041f67cf491SMika Westerberg static int nhi_suspend(struct device *dev)
1042f67cf491SMika Westerberg {
1043f67cf491SMika Westerberg 	struct pci_dev *pdev = to_pci_dev(dev);
1044f67cf491SMika Westerberg 	struct tb *tb = pci_get_drvdata(pdev);
1045f67cf491SMika Westerberg 
1046f67cf491SMika Westerberg 	return tb_domain_suspend(tb);
1047f67cf491SMika Westerberg }
1048f67cf491SMika Westerberg 
1049f67cf491SMika Westerberg static void nhi_complete(struct device *dev)
1050f67cf491SMika Westerberg {
1051f67cf491SMika Westerberg 	struct pci_dev *pdev = to_pci_dev(dev);
1052f67cf491SMika Westerberg 	struct tb *tb = pci_get_drvdata(pdev);
1053f67cf491SMika Westerberg 
10542d8ff0b5SMika Westerberg 	/*
10552d8ff0b5SMika Westerberg 	 * If we were runtime suspended when system suspend started,
10562d8ff0b5SMika Westerberg 	 * schedule runtime resume now. It should bring the domain back
10572d8ff0b5SMika Westerberg 	 * to functional state.
10582d8ff0b5SMika Westerberg 	 */
10592d8ff0b5SMika Westerberg 	if (pm_runtime_suspended(&pdev->dev))
10602d8ff0b5SMika Westerberg 		pm_runtime_resume(&pdev->dev);
10612d8ff0b5SMika Westerberg 	else
1062f67cf491SMika Westerberg 		tb_domain_complete(tb);
1063f67cf491SMika Westerberg }
1064f67cf491SMika Westerberg 
10652d8ff0b5SMika Westerberg static int nhi_runtime_suspend(struct device *dev)
10662d8ff0b5SMika Westerberg {
10672d8ff0b5SMika Westerberg 	struct pci_dev *pdev = to_pci_dev(dev);
10682d8ff0b5SMika Westerberg 	struct tb *tb = pci_get_drvdata(pdev);
10693cdb9446SMika Westerberg 	struct tb_nhi *nhi = tb->nhi;
10703cdb9446SMika Westerberg 	int ret;
10712d8ff0b5SMika Westerberg 
10723cdb9446SMika Westerberg 	ret = tb_domain_runtime_suspend(tb);
10733cdb9446SMika Westerberg 	if (ret)
10743cdb9446SMika Westerberg 		return ret;
10753cdb9446SMika Westerberg 
10763cdb9446SMika Westerberg 	if (nhi->ops && nhi->ops->runtime_suspend) {
10773cdb9446SMika Westerberg 		ret = nhi->ops->runtime_suspend(tb->nhi);
10783cdb9446SMika Westerberg 		if (ret)
10793cdb9446SMika Westerberg 			return ret;
10803cdb9446SMika Westerberg 	}
10813cdb9446SMika Westerberg 	return 0;
10822d8ff0b5SMika Westerberg }
10832d8ff0b5SMika Westerberg 
10842d8ff0b5SMika Westerberg static int nhi_runtime_resume(struct device *dev)
10852d8ff0b5SMika Westerberg {
10862d8ff0b5SMika Westerberg 	struct pci_dev *pdev = to_pci_dev(dev);
10872d8ff0b5SMika Westerberg 	struct tb *tb = pci_get_drvdata(pdev);
10883cdb9446SMika Westerberg 	struct tb_nhi *nhi = tb->nhi;
10893cdb9446SMika Westerberg 	int ret;
10902d8ff0b5SMika Westerberg 
10913cdb9446SMika Westerberg 	if (nhi->ops && nhi->ops->runtime_resume) {
10923cdb9446SMika Westerberg 		ret = nhi->ops->runtime_resume(nhi);
10933cdb9446SMika Westerberg 		if (ret)
10943cdb9446SMika Westerberg 			return ret;
10953cdb9446SMika Westerberg 	}
10963cdb9446SMika Westerberg 
10973cdb9446SMika Westerberg 	nhi_enable_int_throttling(nhi);
10982d8ff0b5SMika Westerberg 	return tb_domain_runtime_resume(tb);
10992d8ff0b5SMika Westerberg }
11002d8ff0b5SMika Westerberg 
110116603153SAndreas Noever static void nhi_shutdown(struct tb_nhi *nhi)
110216603153SAndreas Noever {
110316603153SAndreas Noever 	int i;
1104daa5140fSMika Westerberg 
1105daa5140fSMika Westerberg 	dev_dbg(&nhi->pdev->dev, "shutdown\n");
110616603153SAndreas Noever 
110716603153SAndreas Noever 	for (i = 0; i < nhi->hop_count; i++) {
110816603153SAndreas Noever 		if (nhi->tx_rings[i])
110916603153SAndreas Noever 			dev_WARN(&nhi->pdev->dev,
111016603153SAndreas Noever 				 "TX ring %d is still active\n", i);
111116603153SAndreas Noever 		if (nhi->rx_rings[i])
111216603153SAndreas Noever 			dev_WARN(&nhi->pdev->dev,
111316603153SAndreas Noever 				 "RX ring %d is still active\n", i);
111416603153SAndreas Noever 	}
111516603153SAndreas Noever 	nhi_disable_interrupts(nhi);
111616603153SAndreas Noever 	/*
111716603153SAndreas Noever 	 * We have to release the irq before calling flush_work. Otherwise an
111816603153SAndreas Noever 	 * already executing IRQ handler could call schedule_work again.
111916603153SAndreas Noever 	 */
1120046bee1fSMika Westerberg 	if (!nhi->pdev->msix_enabled) {
112116603153SAndreas Noever 		devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
112216603153SAndreas Noever 		flush_work(&nhi->interrupt_work);
1123046bee1fSMika Westerberg 	}
1124046bee1fSMika Westerberg 	ida_destroy(&nhi->msix_ida);
11253cdb9446SMika Westerberg 
11263cdb9446SMika Westerberg 	if (nhi->ops && nhi->ops->shutdown)
11273cdb9446SMika Westerberg 		nhi->ops->shutdown(nhi);
1128046bee1fSMika Westerberg }
1129046bee1fSMika Westerberg 
1130e390909aSSanjay R Mehta static void nhi_check_quirks(struct tb_nhi *nhi)
1131e390909aSSanjay R Mehta {
113254669e2fSMika Westerberg 	if (nhi->pdev->vendor == PCI_VENDOR_ID_INTEL) {
1133e390909aSSanjay R Mehta 		/*
113454669e2fSMika Westerberg 		 * Intel hardware supports auto clear of the interrupt
113554669e2fSMika Westerberg 		 * status register right after interrupt is being
113654669e2fSMika Westerberg 		 * issued.
1137e390909aSSanjay R Mehta 		 */
1138e390909aSSanjay R Mehta 		nhi->quirks |= QUIRK_AUTO_CLEAR_INT;
113954669e2fSMika Westerberg 
114054669e2fSMika Westerberg 		switch (nhi->pdev->device) {
114154669e2fSMika Westerberg 		case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
114254669e2fSMika Westerberg 		case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
114354669e2fSMika Westerberg 			/*
114454669e2fSMika Westerberg 			 * Falcon Ridge controller needs the end-to-end
114554669e2fSMika Westerberg 			 * flow control workaround to avoid losing Rx
114654669e2fSMika Westerberg 			 * packets when RING_FLAG_E2E is set.
114754669e2fSMika Westerberg 			 */
114854669e2fSMika Westerberg 			nhi->quirks |= QUIRK_E2E;
114954669e2fSMika Westerberg 			break;
115054669e2fSMika Westerberg 		}
115154669e2fSMika Westerberg 	}
1152e390909aSSanjay R Mehta }
1153e390909aSSanjay R Mehta 
115486eaf4a5SRobin Murphy static int nhi_check_iommu_pdev(struct pci_dev *pdev, void *data)
115586eaf4a5SRobin Murphy {
115686eaf4a5SRobin Murphy 	if (!pdev->external_facing ||
115786eaf4a5SRobin Murphy 	    !device_iommu_capable(&pdev->dev, IOMMU_CAP_PRE_BOOT_PROTECTION))
115886eaf4a5SRobin Murphy 		return 0;
115986eaf4a5SRobin Murphy 	*(bool *)data = true;
116086eaf4a5SRobin Murphy 	return 1; /* Stop walking */
116186eaf4a5SRobin Murphy }
116286eaf4a5SRobin Murphy 
116386eaf4a5SRobin Murphy static void nhi_check_iommu(struct tb_nhi *nhi)
116486eaf4a5SRobin Murphy {
116586eaf4a5SRobin Murphy 	struct pci_bus *bus = nhi->pdev->bus;
116686eaf4a5SRobin Murphy 	bool port_ok = false;
116786eaf4a5SRobin Murphy 
116886eaf4a5SRobin Murphy 	/*
116986eaf4a5SRobin Murphy 	 * Ideally what we'd do here is grab every PCI device that
117086eaf4a5SRobin Murphy 	 * represents a tunnelling adapter for this NHI and check their
117186eaf4a5SRobin Murphy 	 * status directly, but unfortunately USB4 seems to make it
117286eaf4a5SRobin Murphy 	 * obnoxiously difficult to reliably make any correlation.
117386eaf4a5SRobin Murphy 	 *
117486eaf4a5SRobin Murphy 	 * So for now we'll have to bodge it... Hoping that the system
117586eaf4a5SRobin Murphy 	 * is at least sane enough that an adapter is in the same PCI
117686eaf4a5SRobin Murphy 	 * segment as its NHI, if we can find *something* on that segment
117786eaf4a5SRobin Murphy 	 * which meets the requirements for Kernel DMA Protection, we'll
117886eaf4a5SRobin Murphy 	 * take that to imply that firmware is aware and has (hopefully)
117986eaf4a5SRobin Murphy 	 * done the right thing in general. We need to know that the PCI
118086eaf4a5SRobin Murphy 	 * layer has seen the ExternalFacingPort property which will then
118186eaf4a5SRobin Murphy 	 * inform the IOMMU layer to enforce the complete "untrusted DMA"
118286eaf4a5SRobin Murphy 	 * flow, but also that the IOMMU driver itself can be trusted not
118386eaf4a5SRobin Murphy 	 * to have been subverted by a pre-boot DMA attack.
118486eaf4a5SRobin Murphy 	 */
118586eaf4a5SRobin Murphy 	while (bus->parent)
118686eaf4a5SRobin Murphy 		bus = bus->parent;
118786eaf4a5SRobin Murphy 
118886eaf4a5SRobin Murphy 	pci_walk_bus(bus, nhi_check_iommu_pdev, &port_ok);
118986eaf4a5SRobin Murphy 
119086eaf4a5SRobin Murphy 	nhi->iommu_dma_protection = port_ok;
119186eaf4a5SRobin Murphy 	dev_dbg(&nhi->pdev->dev, "IOMMU DMA protection is %s\n",
119286eaf4a5SRobin Murphy 		str_enabled_disabled(port_ok));
119386eaf4a5SRobin Murphy }
119486eaf4a5SRobin Murphy 
1195046bee1fSMika Westerberg static int nhi_init_msi(struct tb_nhi *nhi)
1196046bee1fSMika Westerberg {
1197046bee1fSMika Westerberg 	struct pci_dev *pdev = nhi->pdev;
11988d9dcfffSAndy Shevchenko 	struct device *dev = &pdev->dev;
1199046bee1fSMika Westerberg 	int res, irq, nvec;
1200046bee1fSMika Westerberg 
1201046bee1fSMika Westerberg 	/* In case someone left them on. */
1202046bee1fSMika Westerberg 	nhi_disable_interrupts(nhi);
1203046bee1fSMika Westerberg 
12048c6bba10SMika Westerberg 	nhi_enable_int_throttling(nhi);
12058c6bba10SMika Westerberg 
1206046bee1fSMika Westerberg 	ida_init(&nhi->msix_ida);
1207046bee1fSMika Westerberg 
1208046bee1fSMika Westerberg 	/*
1209046bee1fSMika Westerberg 	 * The NHI has 16 MSI-X vectors or a single MSI. We first try to
1210046bee1fSMika Westerberg 	 * get all MSI-X vectors and if we succeed, each ring will have
1211046bee1fSMika Westerberg 	 * one MSI-X. If for some reason that does not work out, we
1212046bee1fSMika Westerberg 	 * fallback to a single MSI.
1213046bee1fSMika Westerberg 	 */
1214046bee1fSMika Westerberg 	nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS,
1215046bee1fSMika Westerberg 				     PCI_IRQ_MSIX);
1216046bee1fSMika Westerberg 	if (nvec < 0) {
1217046bee1fSMika Westerberg 		nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
1218046bee1fSMika Westerberg 		if (nvec < 0)
1219046bee1fSMika Westerberg 			return nvec;
1220046bee1fSMika Westerberg 
1221046bee1fSMika Westerberg 		INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
1222046bee1fSMika Westerberg 
1223046bee1fSMika Westerberg 		irq = pci_irq_vector(nhi->pdev, 0);
1224046bee1fSMika Westerberg 		if (irq < 0)
1225046bee1fSMika Westerberg 			return irq;
1226046bee1fSMika Westerberg 
1227046bee1fSMika Westerberg 		res = devm_request_irq(&pdev->dev, irq, nhi_msi,
1228046bee1fSMika Westerberg 				       IRQF_NO_SUSPEND, "thunderbolt", nhi);
12298d9dcfffSAndy Shevchenko 		if (res)
12308d9dcfffSAndy Shevchenko 			return dev_err_probe(dev, res, "request_irq failed, aborting\n");
1231046bee1fSMika Westerberg 	}
1232046bee1fSMika Westerberg 
1233046bee1fSMika Westerberg 	return 0;
123416603153SAndreas Noever }
123516603153SAndreas Noever 
12363cdb9446SMika Westerberg static bool nhi_imr_valid(struct pci_dev *pdev)
12373cdb9446SMika Westerberg {
12383cdb9446SMika Westerberg 	u8 val;
12393cdb9446SMika Westerberg 
12403cdb9446SMika Westerberg 	if (!device_property_read_u8(&pdev->dev, "IMR_VALID", &val))
12413cdb9446SMika Westerberg 		return !!val;
12423cdb9446SMika Westerberg 
12433cdb9446SMika Westerberg 	return true;
12443cdb9446SMika Westerberg }
12453cdb9446SMika Westerberg 
1246c6da62a2SMika Westerberg static struct tb *nhi_select_cm(struct tb_nhi *nhi)
1247c6da62a2SMika Westerberg {
1248c6da62a2SMika Westerberg 	struct tb *tb;
1249c6da62a2SMika Westerberg 
1250c6da62a2SMika Westerberg 	/*
1251c6da62a2SMika Westerberg 	 * USB4 case is simple. If we got control of any of the
1252c6da62a2SMika Westerberg 	 * capabilities, we use software CM.
1253c6da62a2SMika Westerberg 	 */
1254c6da62a2SMika Westerberg 	if (tb_acpi_is_native())
1255c6da62a2SMika Westerberg 		return tb_probe(nhi);
1256c6da62a2SMika Westerberg 
1257c6da62a2SMika Westerberg 	/*
1258c6da62a2SMika Westerberg 	 * Either firmware based CM is running (we did not get control
1259c6da62a2SMika Westerberg 	 * from the firmware) or this is pre-USB4 PC so try first
1260c6da62a2SMika Westerberg 	 * firmware CM and then fallback to software CM.
1261c6da62a2SMika Westerberg 	 */
1262c6da62a2SMika Westerberg 	tb = icm_probe(nhi);
1263c6da62a2SMika Westerberg 	if (!tb)
1264c6da62a2SMika Westerberg 		tb = tb_probe(nhi);
1265c6da62a2SMika Westerberg 
1266c6da62a2SMika Westerberg 	return tb;
1267c6da62a2SMika Westerberg }
1268c6da62a2SMika Westerberg 
126916603153SAndreas Noever static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
127016603153SAndreas Noever {
12718d9dcfffSAndy Shevchenko 	struct device *dev = &pdev->dev;
127216603153SAndreas Noever 	struct tb_nhi *nhi;
1273d6cc51cdSAndreas Noever 	struct tb *tb;
127416603153SAndreas Noever 	int res;
127516603153SAndreas Noever 
12768d9dcfffSAndy Shevchenko 	if (!nhi_imr_valid(pdev))
12778d9dcfffSAndy Shevchenko 		return dev_err_probe(dev, -ENODEV, "firmware image not valid, aborting\n");
12783cdb9446SMika Westerberg 
127916603153SAndreas Noever 	res = pcim_enable_device(pdev);
12808d9dcfffSAndy Shevchenko 	if (res)
12818d9dcfffSAndy Shevchenko 		return dev_err_probe(dev, res, "cannot enable PCI device, aborting\n");
128216603153SAndreas Noever 
128316603153SAndreas Noever 	res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
12848d9dcfffSAndy Shevchenko 	if (res)
12858d9dcfffSAndy Shevchenko 		return dev_err_probe(dev, res, "cannot obtain PCI resources, aborting\n");
128616603153SAndreas Noever 
128716603153SAndreas Noever 	nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
128816603153SAndreas Noever 	if (!nhi)
128916603153SAndreas Noever 		return -ENOMEM;
129016603153SAndreas Noever 
129116603153SAndreas Noever 	nhi->pdev = pdev;
12923cdb9446SMika Westerberg 	nhi->ops = (const struct tb_nhi_ops *)id->driver_data;
1293ca319f55SMika Westerberg 	/* cannot fail - table is allocated in pcim_iomap_regions */
129416603153SAndreas Noever 	nhi->iobase = pcim_iomap_table(pdev)[0];
129516603153SAndreas Noever 	nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff;
12968d9dcfffSAndy Shevchenko 	dev_dbg(dev, "total paths: %d\n", nhi->hop_count);
129716603153SAndreas Noever 
12982a211f32SHimangi Saraogi 	nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
12992a211f32SHimangi Saraogi 				     sizeof(*nhi->tx_rings), GFP_KERNEL);
13002a211f32SHimangi Saraogi 	nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
13012a211f32SHimangi Saraogi 				     sizeof(*nhi->rx_rings), GFP_KERNEL);
130216603153SAndreas Noever 	if (!nhi->tx_rings || !nhi->rx_rings)
130316603153SAndreas Noever 		return -ENOMEM;
130416603153SAndreas Noever 
1305e390909aSSanjay R Mehta 	nhi_check_quirks(nhi);
130686eaf4a5SRobin Murphy 	nhi_check_iommu(nhi);
1307e390909aSSanjay R Mehta 
1308046bee1fSMika Westerberg 	res = nhi_init_msi(nhi);
13098d9dcfffSAndy Shevchenko 	if (res)
13108d9dcfffSAndy Shevchenko 		return dev_err_probe(dev, res, "cannot enable MSI, aborting\n");
131116603153SAndreas Noever 
131259120e06SMika Westerberg 	spin_lock_init(&nhi->lock);
131316603153SAndreas Noever 
1314dba3caf6SMika Westerberg 	res = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
13158d9dcfffSAndy Shevchenko 	if (res)
13168d9dcfffSAndy Shevchenko 		return dev_err_probe(dev, res, "failed to set DMA mask\n");
1317dba3caf6SMika Westerberg 
131816603153SAndreas Noever 	pci_set_master(pdev);
131916603153SAndreas Noever 
13203cdb9446SMika Westerberg 	if (nhi->ops && nhi->ops->init) {
13213cdb9446SMika Westerberg 		res = nhi->ops->init(nhi);
13223cdb9446SMika Westerberg 		if (res)
13233cdb9446SMika Westerberg 			return res;
13243cdb9446SMika Westerberg 	}
13253cdb9446SMika Westerberg 
1326c6da62a2SMika Westerberg 	tb = nhi_select_cm(nhi);
13278d9dcfffSAndy Shevchenko 	if (!tb)
13288d9dcfffSAndy Shevchenko 		return dev_err_probe(dev, -ENODEV,
1329f67cf491SMika Westerberg 			"failed to determine connection manager, aborting\n");
1330f67cf491SMika Westerberg 
13318d9dcfffSAndy Shevchenko 	dev_dbg(dev, "NHI initialized, starting thunderbolt\n");
13329d3cce0bSMika Westerberg 
13339d3cce0bSMika Westerberg 	res = tb_domain_add(tb);
13349d3cce0bSMika Westerberg 	if (res) {
1335d6cc51cdSAndreas Noever 		/*
1336d6cc51cdSAndreas Noever 		 * At this point the RX/TX rings might already have been
1337d6cc51cdSAndreas Noever 		 * activated. Do a proper shutdown.
1338d6cc51cdSAndreas Noever 		 */
13399d3cce0bSMika Westerberg 		tb_domain_put(tb);
1340d6cc51cdSAndreas Noever 		nhi_shutdown(nhi);
134168a7a2acSMika Westerberg 		return res;
1342d6cc51cdSAndreas Noever 	}
1343d6cc51cdSAndreas Noever 	pci_set_drvdata(pdev, tb);
134416603153SAndreas Noever 
1345b2911a59SMika Westerberg 	device_wakeup_enable(&pdev->dev);
1346b2911a59SMika Westerberg 
13472d8ff0b5SMika Westerberg 	pm_runtime_allow(&pdev->dev);
13482d8ff0b5SMika Westerberg 	pm_runtime_set_autosuspend_delay(&pdev->dev, TB_AUTOSUSPEND_DELAY);
13492d8ff0b5SMika Westerberg 	pm_runtime_use_autosuspend(&pdev->dev);
13502d8ff0b5SMika Westerberg 	pm_runtime_put_autosuspend(&pdev->dev);
13512d8ff0b5SMika Westerberg 
135216603153SAndreas Noever 	return 0;
135316603153SAndreas Noever }
135416603153SAndreas Noever 
135516603153SAndreas Noever static void nhi_remove(struct pci_dev *pdev)
135616603153SAndreas Noever {
1357d6cc51cdSAndreas Noever 	struct tb *tb = pci_get_drvdata(pdev);
1358d6cc51cdSAndreas Noever 	struct tb_nhi *nhi = tb->nhi;
13599d3cce0bSMika Westerberg 
13602d8ff0b5SMika Westerberg 	pm_runtime_get_sync(&pdev->dev);
13612d8ff0b5SMika Westerberg 	pm_runtime_dont_use_autosuspend(&pdev->dev);
13622d8ff0b5SMika Westerberg 	pm_runtime_forbid(&pdev->dev);
13632d8ff0b5SMika Westerberg 
13649d3cce0bSMika Westerberg 	tb_domain_remove(tb);
136516603153SAndreas Noever 	nhi_shutdown(nhi);
136616603153SAndreas Noever }
136716603153SAndreas Noever 
136823dd5bb4SAndreas Noever /*
136923dd5bb4SAndreas Noever  * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
137023dd5bb4SAndreas Noever  * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
137123dd5bb4SAndreas Noever  * resume_noirq until we are done.
137223dd5bb4SAndreas Noever  */
137323dd5bb4SAndreas Noever static const struct dev_pm_ops nhi_pm_ops = {
137423dd5bb4SAndreas Noever 	.suspend_noirq = nhi_suspend_noirq,
137523dd5bb4SAndreas Noever 	.resume_noirq = nhi_resume_noirq,
1376884e4d57SMika Westerberg 	.freeze_noirq = nhi_freeze_noirq,  /*
137723dd5bb4SAndreas Noever 					    * we just disable hotplug, the
137823dd5bb4SAndreas Noever 					    * pci-tunnels stay alive.
137923dd5bb4SAndreas Noever 					    */
1380884e4d57SMika Westerberg 	.thaw_noirq = nhi_thaw_noirq,
138123dd5bb4SAndreas Noever 	.restore_noirq = nhi_resume_noirq,
1382f67cf491SMika Westerberg 	.suspend = nhi_suspend,
13833cdb9446SMika Westerberg 	.poweroff_noirq = nhi_poweroff_noirq,
1384f67cf491SMika Westerberg 	.poweroff = nhi_suspend,
1385f67cf491SMika Westerberg 	.complete = nhi_complete,
13862d8ff0b5SMika Westerberg 	.runtime_suspend = nhi_runtime_suspend,
13872d8ff0b5SMika Westerberg 	.runtime_resume = nhi_runtime_resume,
138823dd5bb4SAndreas Noever };
138923dd5bb4SAndreas Noever 
1390620863f7SSachin Kamat static struct pci_device_id nhi_ids[] = {
139116603153SAndreas Noever 	/*
139216603153SAndreas Noever 	 * We have to specify class, the TB bridges use the same device and
13931d111406SLukas Wunner 	 * vendor (sub)id on gen 1 and gen 2 controllers.
139416603153SAndreas Noever 	 */
139516603153SAndreas Noever 	{
139616603153SAndreas Noever 		.class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
13971d111406SLukas Wunner 		.vendor = PCI_VENDOR_ID_INTEL,
139819bf4d4fSLukas Wunner 		.device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
139919bf4d4fSLukas Wunner 		.subvendor = 0x2222, .subdevice = 0x1111,
140019bf4d4fSLukas Wunner 	},
140119bf4d4fSLukas Wunner 	{
140219bf4d4fSLukas Wunner 		.class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
140319bf4d4fSLukas Wunner 		.vendor = PCI_VENDOR_ID_INTEL,
14041d111406SLukas Wunner 		.device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
140516603153SAndreas Noever 		.subvendor = 0x2222, .subdevice = 0x1111,
140616603153SAndreas Noever 	},
140716603153SAndreas Noever 	{
140816603153SAndreas Noever 		.class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
14091d111406SLukas Wunner 		.vendor = PCI_VENDOR_ID_INTEL,
141082a6a81cSXavier Gnata 		.device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI,
141182a6a81cSXavier Gnata 		.subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
141282a6a81cSXavier Gnata 	},
141382a6a81cSXavier Gnata 	{
141482a6a81cSXavier Gnata 		.class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
141582a6a81cSXavier Gnata 		.vendor = PCI_VENDOR_ID_INTEL,
14161d111406SLukas Wunner 		.device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI,
1417a42fb351SKnuth Posern 		.subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
141816603153SAndreas Noever 	},
14195e2781bcSMika Westerberg 
14205e2781bcSMika Westerberg 	/* Thunderbolt 3 */
14215e2781bcSMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) },
14225e2781bcSMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) },
14235e2781bcSMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) },
14245e2781bcSMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) },
14255e2781bcSMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) },
14265e2781bcSMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) },
14275e2781bcSMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) },
14285e2781bcSMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) },
14294bac471dSRadion Mirchevsky 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI) },
14304bac471dSRadion Mirchevsky 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI) },
14313cdb9446SMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI0),
14323cdb9446SMika Westerberg 	  .driver_data = (kernel_ulong_t)&icl_nhi_ops },
14333cdb9446SMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI1),
14343cdb9446SMika Westerberg 	  .driver_data = (kernel_ulong_t)&icl_nhi_ops },
14351c1aac98SMika Westerberg 	/* Thunderbolt 4 */
143657d8df68SMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI0),
143757d8df68SMika Westerberg 	  .driver_data = (kernel_ulong_t)&icl_nhi_ops },
143857d8df68SMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI1),
143957d8df68SMika Westerberg 	  .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1440f6439c53SMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI0),
1441f6439c53SMika Westerberg 	  .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1442f6439c53SMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI1),
1443f6439c53SMika Westerberg 	  .driver_data = (kernel_ulong_t)&icl_nhi_ops },
144413579486SAzhar Shaikh 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI0),
144513579486SAzhar Shaikh 	  .driver_data = (kernel_ulong_t)&icl_nhi_ops },
144613579486SAzhar Shaikh 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI1),
144713579486SAzhar Shaikh 	  .driver_data = (kernel_ulong_t)&icl_nhi_ops },
14487ec58378SGeorge D Sworo 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI0),
14497ec58378SGeorge D Sworo 	  .driver_data = (kernel_ulong_t)&icl_nhi_ops },
14507ec58378SGeorge D Sworo 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI1),
14517ec58378SGeorge D Sworo 	  .driver_data = (kernel_ulong_t)&icl_nhi_ops },
145232249fd8SMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_M_NHI0),
145332249fd8SMika Westerberg 	  .driver_data = (kernel_ulong_t)&icl_nhi_ops },
145432249fd8SMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_P_NHI0),
145532249fd8SMika Westerberg 	  .driver_data = (kernel_ulong_t)&icl_nhi_ops },
145632249fd8SMika Westerberg 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_P_NHI1),
145732249fd8SMika Westerberg 	  .driver_data = (kernel_ulong_t)&icl_nhi_ops },
14585e2781bcSMika Westerberg 
1459b0407983SMika Westerberg 	/* Any USB4 compliant host */
1460b0407983SMika Westerberg 	{ PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_USB4, ~0) },
1461b0407983SMika Westerberg 
146216603153SAndreas Noever 	{ 0,}
146316603153SAndreas Noever };
146416603153SAndreas Noever 
146516603153SAndreas Noever MODULE_DEVICE_TABLE(pci, nhi_ids);
146616603153SAndreas Noever MODULE_LICENSE("GPL");
146716603153SAndreas Noever 
146816603153SAndreas Noever static struct pci_driver nhi_driver = {
146916603153SAndreas Noever 	.name = "thunderbolt",
147016603153SAndreas Noever 	.id_table = nhi_ids,
147116603153SAndreas Noever 	.probe = nhi_probe,
147216603153SAndreas Noever 	.remove = nhi_remove,
14734caf2511SMaxim Levitsky 	.shutdown = nhi_remove,
147423dd5bb4SAndreas Noever 	.driver.pm = &nhi_pm_ops,
147516603153SAndreas Noever };
147616603153SAndreas Noever 
147716603153SAndreas Noever static int __init nhi_init(void)
147816603153SAndreas Noever {
14799d3cce0bSMika Westerberg 	int ret;
14809d3cce0bSMika Westerberg 
14819d3cce0bSMika Westerberg 	ret = tb_domain_init();
14829d3cce0bSMika Westerberg 	if (ret)
14839d3cce0bSMika Westerberg 		return ret;
14849d3cce0bSMika Westerberg 	ret = pci_register_driver(&nhi_driver);
14859d3cce0bSMika Westerberg 	if (ret)
14869d3cce0bSMika Westerberg 		tb_domain_exit();
14879d3cce0bSMika Westerberg 	return ret;
148816603153SAndreas Noever }
148916603153SAndreas Noever 
149016603153SAndreas Noever static void __exit nhi_unload(void)
149116603153SAndreas Noever {
149216603153SAndreas Noever 	pci_unregister_driver(&nhi_driver);
14939d3cce0bSMika Westerberg 	tb_domain_exit();
149416603153SAndreas Noever }
149516603153SAndreas Noever 
1496eafa717bSMika Westerberg rootfs_initcall(nhi_init);
149716603153SAndreas Noever module_exit(nhi_unload);
1498