1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Thunderbolt Cactus Ridge driver - NHI driver
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
11 #include <linux/idr.h>
12 #include <linux/mutex.h>
13 #include <linux/workqueue.h>
16 * struct tb_nhi - thunderbolt native host interface
17 * @lock: Must be held during ring creation/destruction. Is acquired by
18 * interrupt_work when dispatching interrupts to individual rings.
19 * @pdev: Pointer to the PCI device
20 * @iobase: MMIO space of the NHI
21 * @tx_rings: All Tx rings available on this host controller
22 * @rx_rings: All Rx rings available on this host controller
23 * @msix_ida: Used to allocate MSI-X vectors for rings
24 * @going_away: The host controller device is about to disappear so when
25 * this flag is set, avoid touching the hardware anymore.
26 * @interrupt_work: Work scheduled to handle ring interrupt when no
28 * @hop_count: Number of rings (end point hops) supported by NHI.
34 struct tb_ring
**tx_rings
;
35 struct tb_ring
**rx_rings
;
38 struct work_struct interrupt_work
;
43 * struct tb_ring - thunderbolt TX or RX ring associated with a NHI
44 * @lock: Lock serializing actions to this ring. Must be acquired after
46 * @nhi: Pointer to the native host controller interface
47 * @size: Size of the ring
48 * @hop: Hop (DMA channel) associated with this ring
49 * @head: Head of the ring (write next descriptor here)
50 * @tail: Tail of the ring (complete next descriptor here)
51 * @descriptors: Allocated descriptors for this ring
52 * @queue: Queue holding frames to be transferred over this ring
53 * @in_flight: Queue holding frames that are currently in flight
54 * @work: Interrupt work structure
55 * @is_tx: Is the ring Tx or Rx
56 * @running: Is the ring running
57 * @irq: MSI-X irq number if the ring uses MSI-X. %0 otherwise.
58 * @vector: MSI-X vector number the ring uses (only set if @irq is > 0)
59 * @flags: Ring specific flags
68 struct ring_desc
*descriptors
;
69 dma_addr_t descriptors_dma
;
70 struct list_head queue
;
71 struct list_head in_flight
;
72 struct work_struct work
;
80 /* Leave ring interrupt enabled on suspend */
81 #define RING_FLAG_NO_SUSPEND BIT(0)
84 typedef void (*ring_cb
)(struct tb_ring
*, struct ring_frame
*, bool canceled
);
87 * struct ring_frame - for use with ring_rx/ring_tx
90 dma_addr_t buffer_phy
;
92 struct list_head list
;
93 u32 size
:12; /* TX: in, RX: out*/
94 u32 flags
:12; /* RX: out */
95 u32 eof
:4; /* TX:in, RX: out */
96 u32 sof
:4; /* TX:in, RX: out */
99 #define TB_FRAME_SIZE 0x100 /* minimum size for ring_rx */
101 struct tb_ring
*ring_alloc_tx(struct tb_nhi
*nhi
, int hop
, int size
,
103 struct tb_ring
*ring_alloc_rx(struct tb_nhi
*nhi
, int hop
, int size
,
105 void ring_start(struct tb_ring
*ring
);
106 void ring_stop(struct tb_ring
*ring
);
107 void ring_free(struct tb_ring
*ring
);
109 int __ring_enqueue(struct tb_ring
*ring
, struct ring_frame
*frame
);
112 * ring_rx() - enqueue a frame on an RX ring
114 * frame->buffer, frame->buffer_phy and frame->callback have to be set. The
115 * buffer must contain at least TB_FRAME_SIZE bytes.
117 * frame->callback will be invoked with frame->size, frame->flags, frame->eof,
118 * frame->sof set once the frame has been received.
120 * If ring_stop is called after the packet has been enqueued frame->callback
121 * will be called with canceled set to true.
123 * Return: Returns ESHUTDOWN if ring_stop has been called. Zero otherwise.
125 static inline int ring_rx(struct tb_ring
*ring
, struct ring_frame
*frame
)
127 WARN_ON(ring
->is_tx
);
128 return __ring_enqueue(ring
, frame
);
132 * ring_tx() - enqueue a frame on an TX ring
134 * frame->buffer, frame->buffer_phy, frame->callback, frame->size, frame->eof
135 * and frame->sof have to be set.
137 * frame->callback will be invoked with once the frame has been transmitted.
139 * If ring_stop is called after the packet has been enqueued frame->callback
140 * will be called with canceled set to true.
142 * Return: Returns ESHUTDOWN if ring_stop has been called. Zero otherwise.
144 static inline int ring_tx(struct tb_ring
*ring
, struct ring_frame
*frame
)
146 WARN_ON(!ring
->is_tx
);
147 return __ring_enqueue(ring
, frame
);
157 enum nhi_mailbox_cmd
{
158 NHI_MAILBOX_SAVE_DEVS
= 0x05,
159 NHI_MAILBOX_DISCONNECT_PCIE_PATHS
= 0x06,
160 NHI_MAILBOX_DRV_UNLOADS
= 0x07,
161 NHI_MAILBOX_ALLOW_ALL_DEVS
= 0x23,
164 int nhi_mailbox_cmd(struct tb_nhi
*nhi
, enum nhi_mailbox_cmd cmd
, u32 data
);
165 enum nhi_fw_mode
nhi_mailbox_mode(struct tb_nhi
*nhi
);
168 * PCI IDs used in this driver from Win Ridge forward. There is no
169 * need for the PCI quirk anymore as we will use ICM also on Apple
172 #define PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_NHI 0x157d
173 #define PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE 0x157e
174 #define PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI 0x15bf
175 #define PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE 0x15c0
176 #define PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI 0x15d2
177 #define PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE 0x15d3
178 #define PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI 0x15d9
179 #define PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE 0x15da
180 #define PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI 0x15dc
181 #define PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI 0x15dd
182 #define PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI 0x15de