Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Thunderbolt Cactus Ridge driver - NHI driver |
| 3 | * |
| 4 | * The NHI (native host interface) is the pci device that allows us to send and |
| 5 | * receive frames from the thunderbolt bus. |
| 6 | * |
| 7 | * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> |
| 8 | */ |
| 9 | |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 10 | #include <linux/pm_runtime.h> |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 11 | #include <linux/slab.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/pci.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/module.h> |
Mika Westerberg | cd446ee2 | 2017-06-06 15:25:12 +0300 | [diff] [blame] | 16 | #include <linux/delay.h> |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 17 | |
| 18 | #include "nhi.h" |
| 19 | #include "nhi_regs.h" |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 20 | #include "tb.h" |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 21 | |
| 22 | #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring") |
| 23 | |
Mika Westerberg | 046bee1 | 2017-06-06 15:24:57 +0300 | [diff] [blame] | 24 | /* |
Mika Westerberg | 9fb1e65 | 2017-10-02 13:38:36 +0300 | [diff] [blame] | 25 | * Used to enable end-to-end workaround for missing RX packets. Do not |
| 26 | * use this ring for anything else. |
| 27 | */ |
| 28 | #define RING_E2E_UNUSED_HOPID 2 |
| 29 | |
| 30 | /* |
Mika Westerberg | 046bee1 | 2017-06-06 15:24:57 +0300 | [diff] [blame] | 31 | * Minimal number of vectors when we use MSI-X. Two for control channel |
| 32 | * Rx/Tx and the rest four are for cross domain DMA paths. |
| 33 | */ |
| 34 | #define MSIX_MIN_VECS 6 |
| 35 | #define MSIX_MAX_VECS 16 |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 36 | |
Mika Westerberg | cd446ee2 | 2017-06-06 15:25:12 +0300 | [diff] [blame] | 37 | #define NHI_MAILBOX_TIMEOUT 500 /* ms */ |
| 38 | |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 39 | static int ring_interrupt_index(struct tb_ring *ring) |
| 40 | { |
| 41 | int bit = ring->hop; |
| 42 | if (!ring->is_tx) |
| 43 | bit += ring->nhi->hop_count; |
| 44 | return bit; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * ring_interrupt_active() - activate/deactivate interrupts for a single ring |
| 49 | * |
| 50 | * ring->nhi->lock must be held. |
| 51 | */ |
| 52 | static void ring_interrupt_active(struct tb_ring *ring, bool active) |
| 53 | { |
Lukas Wunner | 19bf4d4 | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 54 | int reg = REG_RING_INTERRUPT_BASE + |
| 55 | ring_interrupt_index(ring) / 32 * 4; |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 56 | int bit = ring_interrupt_index(ring) & 31; |
| 57 | int mask = 1 << bit; |
| 58 | u32 old, new; |
Mika Westerberg | 046bee1 | 2017-06-06 15:24:57 +0300 | [diff] [blame] | 59 | |
| 60 | if (ring->irq > 0) { |
| 61 | u32 step, shift, ivr, misc; |
| 62 | void __iomem *ivr_base; |
| 63 | int index; |
| 64 | |
| 65 | if (ring->is_tx) |
| 66 | index = ring->hop; |
| 67 | else |
| 68 | index = ring->hop + ring->nhi->hop_count; |
| 69 | |
| 70 | /* |
| 71 | * Ask the hardware to clear interrupt status bits automatically |
| 72 | * since we already know which interrupt was triggered. |
| 73 | */ |
| 74 | misc = ioread32(ring->nhi->iobase + REG_DMA_MISC); |
| 75 | if (!(misc & REG_DMA_MISC_INT_AUTO_CLEAR)) { |
| 76 | misc |= REG_DMA_MISC_INT_AUTO_CLEAR; |
| 77 | iowrite32(misc, ring->nhi->iobase + REG_DMA_MISC); |
| 78 | } |
| 79 | |
| 80 | ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE; |
| 81 | step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS; |
| 82 | shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS; |
| 83 | ivr = ioread32(ivr_base + step); |
| 84 | ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift); |
| 85 | if (active) |
| 86 | ivr |= ring->vector << shift; |
| 87 | iowrite32(ivr, ivr_base + step); |
| 88 | } |
| 89 | |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 90 | old = ioread32(ring->nhi->iobase + reg); |
| 91 | if (active) |
| 92 | new = old | mask; |
| 93 | else |
| 94 | new = old & ~mask; |
| 95 | |
| 96 | dev_info(&ring->nhi->pdev->dev, |
| 97 | "%s interrupt at register %#x bit %d (%#x -> %#x)\n", |
| 98 | active ? "enabling" : "disabling", reg, bit, old, new); |
| 99 | |
| 100 | if (new == old) |
| 101 | dev_WARN(&ring->nhi->pdev->dev, |
| 102 | "interrupt for %s %d is already %s\n", |
| 103 | RING_TYPE(ring), ring->hop, |
| 104 | active ? "enabled" : "disabled"); |
| 105 | iowrite32(new, ring->nhi->iobase + reg); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * nhi_disable_interrupts() - disable interrupts for all rings |
| 110 | * |
| 111 | * Use only during init and shutdown. |
| 112 | */ |
| 113 | static void nhi_disable_interrupts(struct tb_nhi *nhi) |
| 114 | { |
| 115 | int i = 0; |
| 116 | /* disable interrupts */ |
| 117 | for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++) |
| 118 | iowrite32(0, nhi->iobase + REG_RING_INTERRUPT_BASE + 4 * i); |
| 119 | |
| 120 | /* clear interrupt status bits */ |
| 121 | for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++) |
| 122 | ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + 4 * i); |
| 123 | } |
| 124 | |
| 125 | /* ring helper methods */ |
| 126 | |
| 127 | static void __iomem *ring_desc_base(struct tb_ring *ring) |
| 128 | { |
| 129 | void __iomem *io = ring->nhi->iobase; |
| 130 | io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE; |
| 131 | io += ring->hop * 16; |
| 132 | return io; |
| 133 | } |
| 134 | |
| 135 | static void __iomem *ring_options_base(struct tb_ring *ring) |
| 136 | { |
| 137 | void __iomem *io = ring->nhi->iobase; |
| 138 | io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE; |
| 139 | io += ring->hop * 32; |
| 140 | return io; |
| 141 | } |
| 142 | |
| 143 | static void ring_iowrite16desc(struct tb_ring *ring, u32 value, u32 offset) |
| 144 | { |
| 145 | iowrite16(value, ring_desc_base(ring) + offset); |
| 146 | } |
| 147 | |
| 148 | static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset) |
| 149 | { |
| 150 | iowrite32(value, ring_desc_base(ring) + offset); |
| 151 | } |
| 152 | |
| 153 | static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset) |
| 154 | { |
| 155 | iowrite32(value, ring_desc_base(ring) + offset); |
| 156 | iowrite32(value >> 32, ring_desc_base(ring) + offset + 4); |
| 157 | } |
| 158 | |
| 159 | static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset) |
| 160 | { |
| 161 | iowrite32(value, ring_options_base(ring) + offset); |
| 162 | } |
| 163 | |
| 164 | static bool ring_full(struct tb_ring *ring) |
| 165 | { |
| 166 | return ((ring->head + 1) % ring->size) == ring->tail; |
| 167 | } |
| 168 | |
| 169 | static bool ring_empty(struct tb_ring *ring) |
| 170 | { |
| 171 | return ring->head == ring->tail; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * ring_write_descriptors() - post frames from ring->queue to the controller |
| 176 | * |
| 177 | * ring->lock is held. |
| 178 | */ |
| 179 | static void ring_write_descriptors(struct tb_ring *ring) |
| 180 | { |
| 181 | struct ring_frame *frame, *n; |
| 182 | struct ring_desc *descriptor; |
| 183 | list_for_each_entry_safe(frame, n, &ring->queue, list) { |
| 184 | if (ring_full(ring)) |
| 185 | break; |
| 186 | list_move_tail(&frame->list, &ring->in_flight); |
| 187 | descriptor = &ring->descriptors[ring->head]; |
| 188 | descriptor->phys = frame->buffer_phy; |
| 189 | descriptor->time = 0; |
| 190 | descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT; |
| 191 | if (ring->is_tx) { |
| 192 | descriptor->length = frame->size; |
| 193 | descriptor->eof = frame->eof; |
| 194 | descriptor->sof = frame->sof; |
| 195 | } |
| 196 | ring->head = (ring->head + 1) % ring->size; |
| 197 | ring_iowrite16desc(ring, ring->head, ring->is_tx ? 10 : 8); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * ring_work() - progress completed frames |
| 203 | * |
| 204 | * If the ring is shutting down then all frames are marked as canceled and |
| 205 | * their callbacks are invoked. |
| 206 | * |
| 207 | * Otherwise we collect all completed frame from the ring buffer, write new |
| 208 | * frame to the ring buffer and invoke the callbacks for the completed frames. |
| 209 | */ |
| 210 | static void ring_work(struct work_struct *work) |
| 211 | { |
| 212 | struct tb_ring *ring = container_of(work, typeof(*ring), work); |
| 213 | struct ring_frame *frame; |
| 214 | bool canceled = false; |
| 215 | LIST_HEAD(done); |
| 216 | mutex_lock(&ring->lock); |
| 217 | |
| 218 | if (!ring->running) { |
| 219 | /* Move all frames to done and mark them as canceled. */ |
| 220 | list_splice_tail_init(&ring->in_flight, &done); |
| 221 | list_splice_tail_init(&ring->queue, &done); |
| 222 | canceled = true; |
| 223 | goto invoke_callback; |
| 224 | } |
| 225 | |
| 226 | while (!ring_empty(ring)) { |
| 227 | if (!(ring->descriptors[ring->tail].flags |
| 228 | & RING_DESC_COMPLETED)) |
| 229 | break; |
| 230 | frame = list_first_entry(&ring->in_flight, typeof(*frame), |
| 231 | list); |
| 232 | list_move_tail(&frame->list, &done); |
| 233 | if (!ring->is_tx) { |
| 234 | frame->size = ring->descriptors[ring->tail].length; |
| 235 | frame->eof = ring->descriptors[ring->tail].eof; |
| 236 | frame->sof = ring->descriptors[ring->tail].sof; |
| 237 | frame->flags = ring->descriptors[ring->tail].flags; |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 238 | } |
| 239 | ring->tail = (ring->tail + 1) % ring->size; |
| 240 | } |
| 241 | ring_write_descriptors(ring); |
| 242 | |
| 243 | invoke_callback: |
| 244 | mutex_unlock(&ring->lock); /* allow callbacks to schedule new work */ |
| 245 | while (!list_empty(&done)) { |
| 246 | frame = list_first_entry(&done, typeof(*frame), list); |
| 247 | /* |
| 248 | * The callback may reenqueue or delete frame. |
| 249 | * Do not hold on to it. |
| 250 | */ |
| 251 | list_del_init(&frame->list); |
| 252 | frame->callback(ring, frame, canceled); |
| 253 | } |
| 254 | } |
| 255 | |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 256 | int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame) |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 257 | { |
| 258 | int ret = 0; |
| 259 | mutex_lock(&ring->lock); |
| 260 | if (ring->running) { |
| 261 | list_add_tail(&frame->list, &ring->queue); |
| 262 | ring_write_descriptors(ring); |
| 263 | } else { |
| 264 | ret = -ESHUTDOWN; |
| 265 | } |
| 266 | mutex_unlock(&ring->lock); |
| 267 | return ret; |
| 268 | } |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 269 | EXPORT_SYMBOL_GPL(__tb_ring_enqueue); |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 270 | |
Mika Westerberg | 046bee1 | 2017-06-06 15:24:57 +0300 | [diff] [blame] | 271 | static irqreturn_t ring_msix(int irq, void *data) |
| 272 | { |
| 273 | struct tb_ring *ring = data; |
| 274 | |
| 275 | schedule_work(&ring->work); |
| 276 | return IRQ_HANDLED; |
| 277 | } |
| 278 | |
| 279 | static int ring_request_msix(struct tb_ring *ring, bool no_suspend) |
| 280 | { |
| 281 | struct tb_nhi *nhi = ring->nhi; |
| 282 | unsigned long irqflags; |
| 283 | int ret; |
| 284 | |
| 285 | if (!nhi->pdev->msix_enabled) |
| 286 | return 0; |
| 287 | |
| 288 | ret = ida_simple_get(&nhi->msix_ida, 0, MSIX_MAX_VECS, GFP_KERNEL); |
| 289 | if (ret < 0) |
| 290 | return ret; |
| 291 | |
| 292 | ring->vector = ret; |
| 293 | |
| 294 | ring->irq = pci_irq_vector(ring->nhi->pdev, ring->vector); |
| 295 | if (ring->irq < 0) |
| 296 | return ring->irq; |
| 297 | |
| 298 | irqflags = no_suspend ? IRQF_NO_SUSPEND : 0; |
| 299 | return request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring); |
| 300 | } |
| 301 | |
| 302 | static void ring_release_msix(struct tb_ring *ring) |
| 303 | { |
| 304 | if (ring->irq <= 0) |
| 305 | return; |
| 306 | |
| 307 | free_irq(ring->irq, ring); |
| 308 | ida_simple_remove(&ring->nhi->msix_ida, ring->vector); |
| 309 | ring->vector = 0; |
| 310 | ring->irq = 0; |
| 311 | } |
| 312 | |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 313 | static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size, |
| 314 | bool transmit, unsigned int flags, |
| 315 | u16 sof_mask, u16 eof_mask) |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 316 | { |
| 317 | struct tb_ring *ring = NULL; |
| 318 | dev_info(&nhi->pdev->dev, "allocating %s ring %d of size %d\n", |
| 319 | transmit ? "TX" : "RX", hop, size); |
| 320 | |
Mika Westerberg | 9fb1e65 | 2017-10-02 13:38:36 +0300 | [diff] [blame] | 321 | /* Tx Ring 2 is reserved for E2E workaround */ |
| 322 | if (transmit && hop == RING_E2E_UNUSED_HOPID) |
| 323 | return NULL; |
| 324 | |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 325 | mutex_lock(&nhi->lock); |
| 326 | if (hop >= nhi->hop_count) { |
| 327 | dev_WARN(&nhi->pdev->dev, "invalid hop: %d\n", hop); |
| 328 | goto err; |
| 329 | } |
| 330 | if (transmit && nhi->tx_rings[hop]) { |
| 331 | dev_WARN(&nhi->pdev->dev, "TX hop %d already allocated\n", hop); |
| 332 | goto err; |
| 333 | } else if (!transmit && nhi->rx_rings[hop]) { |
| 334 | dev_WARN(&nhi->pdev->dev, "RX hop %d already allocated\n", hop); |
| 335 | goto err; |
| 336 | } |
| 337 | ring = kzalloc(sizeof(*ring), GFP_KERNEL); |
| 338 | if (!ring) |
| 339 | goto err; |
| 340 | |
| 341 | mutex_init(&ring->lock); |
| 342 | INIT_LIST_HEAD(&ring->queue); |
| 343 | INIT_LIST_HEAD(&ring->in_flight); |
| 344 | INIT_WORK(&ring->work, ring_work); |
| 345 | |
| 346 | ring->nhi = nhi; |
| 347 | ring->hop = hop; |
| 348 | ring->is_tx = transmit; |
| 349 | ring->size = size; |
Mika Westerberg | 046bee1 | 2017-06-06 15:24:57 +0300 | [diff] [blame] | 350 | ring->flags = flags; |
Mika Westerberg | 9fb1e65 | 2017-10-02 13:38:36 +0300 | [diff] [blame] | 351 | ring->sof_mask = sof_mask; |
| 352 | ring->eof_mask = eof_mask; |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 353 | ring->head = 0; |
| 354 | ring->tail = 0; |
| 355 | ring->running = false; |
Mika Westerberg | 046bee1 | 2017-06-06 15:24:57 +0300 | [diff] [blame] | 356 | |
| 357 | if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND)) |
| 358 | goto err; |
| 359 | |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 360 | ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev, |
| 361 | size * sizeof(*ring->descriptors), |
| 362 | &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO); |
| 363 | if (!ring->descriptors) |
| 364 | goto err; |
| 365 | |
| 366 | if (transmit) |
| 367 | nhi->tx_rings[hop] = ring; |
| 368 | else |
| 369 | nhi->rx_rings[hop] = ring; |
| 370 | mutex_unlock(&nhi->lock); |
| 371 | return ring; |
| 372 | |
| 373 | err: |
| 374 | if (ring) |
| 375 | mutex_destroy(&ring->lock); |
| 376 | kfree(ring); |
| 377 | mutex_unlock(&nhi->lock); |
| 378 | return NULL; |
| 379 | } |
| 380 | |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 381 | /** |
| 382 | * tb_ring_alloc_tx() - Allocate DMA ring for transmit |
| 383 | * @nhi: Pointer to the NHI the ring is to be allocated |
| 384 | * @hop: HopID (ring) to allocate |
| 385 | * @size: Number of entries in the ring |
| 386 | * @flags: Flags for the ring |
| 387 | */ |
| 388 | struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size, |
| 389 | unsigned int flags) |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 390 | { |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 391 | return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0); |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 392 | } |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 393 | EXPORT_SYMBOL_GPL(tb_ring_alloc_tx); |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 394 | |
| 395 | /** |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 396 | * tb_ring_alloc_rx() - Allocate DMA ring for receive |
| 397 | * @nhi: Pointer to the NHI the ring is to be allocated |
| 398 | * @hop: HopID (ring) to allocate |
| 399 | * @size: Number of entries in the ring |
| 400 | * @flags: Flags for the ring |
| 401 | * @sof_mask: Mask of PDF values that start a frame |
| 402 | * @eof_mask: Mask of PDF values that end a frame |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 403 | */ |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 404 | struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size, |
| 405 | unsigned int flags, u16 sof_mask, u16 eof_mask) |
| 406 | { |
| 407 | return tb_ring_alloc(nhi, hop, size, false, flags, sof_mask, eof_mask); |
| 408 | } |
| 409 | EXPORT_SYMBOL_GPL(tb_ring_alloc_rx); |
| 410 | |
| 411 | /** |
| 412 | * tb_ring_start() - enable a ring |
| 413 | * |
| 414 | * Must not be invoked in parallel with tb_ring_stop(). |
| 415 | */ |
| 416 | void tb_ring_start(struct tb_ring *ring) |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 417 | { |
Mika Westerberg | 9fb1e65 | 2017-10-02 13:38:36 +0300 | [diff] [blame] | 418 | u16 frame_size; |
| 419 | u32 flags; |
| 420 | |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 421 | mutex_lock(&ring->nhi->lock); |
| 422 | mutex_lock(&ring->lock); |
Mika Westerberg | bdccf29 | 2017-06-06 15:25:15 +0300 | [diff] [blame] | 423 | if (ring->nhi->going_away) |
| 424 | goto err; |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 425 | if (ring->running) { |
| 426 | dev_WARN(&ring->nhi->pdev->dev, "ring already started\n"); |
| 427 | goto err; |
| 428 | } |
| 429 | dev_info(&ring->nhi->pdev->dev, "starting %s %d\n", |
| 430 | RING_TYPE(ring), ring->hop); |
| 431 | |
Mika Westerberg | 9fb1e65 | 2017-10-02 13:38:36 +0300 | [diff] [blame] | 432 | if (ring->flags & RING_FLAG_FRAME) { |
| 433 | /* Means 4096 */ |
| 434 | frame_size = 0; |
| 435 | flags = RING_FLAG_ENABLE; |
| 436 | } else { |
| 437 | frame_size = TB_FRAME_SIZE; |
| 438 | flags = RING_FLAG_ENABLE | RING_FLAG_RAW; |
| 439 | } |
| 440 | |
| 441 | if (ring->flags & RING_FLAG_E2E && !ring->is_tx) { |
| 442 | u32 hop; |
| 443 | |
| 444 | /* |
| 445 | * In order not to lose Rx packets we enable end-to-end |
| 446 | * workaround which transfers Rx credits to an unused Tx |
| 447 | * HopID. |
| 448 | */ |
| 449 | hop = RING_E2E_UNUSED_HOPID << REG_RX_OPTIONS_E2E_HOP_SHIFT; |
| 450 | hop &= REG_RX_OPTIONS_E2E_HOP_MASK; |
| 451 | flags |= hop | RING_FLAG_E2E_FLOW_CONTROL; |
| 452 | } |
| 453 | |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 454 | ring_iowrite64desc(ring, ring->descriptors_dma, 0); |
| 455 | if (ring->is_tx) { |
| 456 | ring_iowrite32desc(ring, ring->size, 12); |
| 457 | ring_iowrite32options(ring, 0, 4); /* time releated ? */ |
Mika Westerberg | 9fb1e65 | 2017-10-02 13:38:36 +0300 | [diff] [blame] | 458 | ring_iowrite32options(ring, flags, 0); |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 459 | } else { |
Mika Westerberg | 9fb1e65 | 2017-10-02 13:38:36 +0300 | [diff] [blame] | 460 | u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask; |
| 461 | |
| 462 | ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12); |
| 463 | ring_iowrite32options(ring, sof_eof_mask, 4); |
| 464 | ring_iowrite32options(ring, flags, 0); |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 465 | } |
| 466 | ring_interrupt_active(ring, true); |
| 467 | ring->running = true; |
| 468 | err: |
| 469 | mutex_unlock(&ring->lock); |
| 470 | mutex_unlock(&ring->nhi->lock); |
| 471 | } |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 472 | EXPORT_SYMBOL_GPL(tb_ring_start); |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 473 | |
| 474 | /** |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 475 | * tb_ring_stop() - shutdown a ring |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 476 | * |
| 477 | * Must not be invoked from a callback. |
| 478 | * |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 479 | * This method will disable the ring. Further calls to |
| 480 | * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been |
| 481 | * called. |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 482 | * |
| 483 | * All enqueued frames will be canceled and their callbacks will be executed |
| 484 | * with frame->canceled set to true (on the callback thread). This method |
| 485 | * returns only after all callback invocations have finished. |
| 486 | */ |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 487 | void tb_ring_stop(struct tb_ring *ring) |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 488 | { |
| 489 | mutex_lock(&ring->nhi->lock); |
| 490 | mutex_lock(&ring->lock); |
| 491 | dev_info(&ring->nhi->pdev->dev, "stopping %s %d\n", |
| 492 | RING_TYPE(ring), ring->hop); |
Mika Westerberg | bdccf29 | 2017-06-06 15:25:15 +0300 | [diff] [blame] | 493 | if (ring->nhi->going_away) |
| 494 | goto err; |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 495 | if (!ring->running) { |
| 496 | dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n", |
| 497 | RING_TYPE(ring), ring->hop); |
| 498 | goto err; |
| 499 | } |
| 500 | ring_interrupt_active(ring, false); |
| 501 | |
| 502 | ring_iowrite32options(ring, 0, 0); |
| 503 | ring_iowrite64desc(ring, 0, 0); |
| 504 | ring_iowrite16desc(ring, 0, ring->is_tx ? 10 : 8); |
| 505 | ring_iowrite32desc(ring, 0, 12); |
| 506 | ring->head = 0; |
| 507 | ring->tail = 0; |
| 508 | ring->running = false; |
| 509 | |
| 510 | err: |
| 511 | mutex_unlock(&ring->lock); |
| 512 | mutex_unlock(&ring->nhi->lock); |
| 513 | |
| 514 | /* |
| 515 | * schedule ring->work to invoke callbacks on all remaining frames. |
| 516 | */ |
| 517 | schedule_work(&ring->work); |
| 518 | flush_work(&ring->work); |
| 519 | } |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 520 | EXPORT_SYMBOL_GPL(tb_ring_stop); |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 521 | |
| 522 | /* |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 523 | * tb_ring_free() - free ring |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 524 | * |
| 525 | * When this method returns all invocations of ring->callback will have |
| 526 | * finished. |
| 527 | * |
| 528 | * Ring must be stopped. |
| 529 | * |
| 530 | * Must NOT be called from ring_frame->callback! |
| 531 | */ |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 532 | void tb_ring_free(struct tb_ring *ring) |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 533 | { |
| 534 | mutex_lock(&ring->nhi->lock); |
| 535 | /* |
| 536 | * Dissociate the ring from the NHI. This also ensures that |
| 537 | * nhi_interrupt_work cannot reschedule ring->work. |
| 538 | */ |
| 539 | if (ring->is_tx) |
| 540 | ring->nhi->tx_rings[ring->hop] = NULL; |
| 541 | else |
| 542 | ring->nhi->rx_rings[ring->hop] = NULL; |
| 543 | |
| 544 | if (ring->running) { |
| 545 | dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n", |
| 546 | RING_TYPE(ring), ring->hop); |
| 547 | } |
| 548 | |
Mika Westerberg | 046bee1 | 2017-06-06 15:24:57 +0300 | [diff] [blame] | 549 | ring_release_msix(ring); |
| 550 | |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 551 | dma_free_coherent(&ring->nhi->pdev->dev, |
| 552 | ring->size * sizeof(*ring->descriptors), |
| 553 | ring->descriptors, ring->descriptors_dma); |
| 554 | |
Sachin Kamat | f19b72c | 2014-06-20 14:32:33 +0530 | [diff] [blame] | 555 | ring->descriptors = NULL; |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 556 | ring->descriptors_dma = 0; |
| 557 | |
| 558 | |
| 559 | dev_info(&ring->nhi->pdev->dev, |
| 560 | "freeing %s %d\n", |
| 561 | RING_TYPE(ring), |
| 562 | ring->hop); |
| 563 | |
| 564 | mutex_unlock(&ring->nhi->lock); |
| 565 | /** |
Mika Westerberg | 046bee1 | 2017-06-06 15:24:57 +0300 | [diff] [blame] | 566 | * ring->work can no longer be scheduled (it is scheduled only |
| 567 | * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it |
| 568 | * to finish before freeing the ring. |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 569 | */ |
| 570 | flush_work(&ring->work); |
| 571 | mutex_destroy(&ring->lock); |
| 572 | kfree(ring); |
| 573 | } |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame^] | 574 | EXPORT_SYMBOL_GPL(tb_ring_free); |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 575 | |
Mika Westerberg | cd446ee2 | 2017-06-06 15:25:12 +0300 | [diff] [blame] | 576 | /** |
| 577 | * nhi_mailbox_cmd() - Send a command through NHI mailbox |
| 578 | * @nhi: Pointer to the NHI structure |
| 579 | * @cmd: Command to send |
| 580 | * @data: Data to be send with the command |
| 581 | * |
| 582 | * Sends mailbox command to the firmware running on NHI. Returns %0 in |
| 583 | * case of success and negative errno in case of failure. |
| 584 | */ |
| 585 | int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data) |
| 586 | { |
| 587 | ktime_t timeout; |
| 588 | u32 val; |
| 589 | |
| 590 | iowrite32(data, nhi->iobase + REG_INMAIL_DATA); |
| 591 | |
| 592 | val = ioread32(nhi->iobase + REG_INMAIL_CMD); |
| 593 | val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR); |
| 594 | val |= REG_INMAIL_OP_REQUEST | cmd; |
| 595 | iowrite32(val, nhi->iobase + REG_INMAIL_CMD); |
| 596 | |
| 597 | timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT); |
| 598 | do { |
| 599 | val = ioread32(nhi->iobase + REG_INMAIL_CMD); |
| 600 | if (!(val & REG_INMAIL_OP_REQUEST)) |
| 601 | break; |
| 602 | usleep_range(10, 20); |
| 603 | } while (ktime_before(ktime_get(), timeout)); |
| 604 | |
| 605 | if (val & REG_INMAIL_OP_REQUEST) |
| 606 | return -ETIMEDOUT; |
| 607 | if (val & REG_INMAIL_ERROR) |
| 608 | return -EIO; |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * nhi_mailbox_mode() - Return current firmware operation mode |
| 615 | * @nhi: Pointer to the NHI structure |
| 616 | * |
| 617 | * The function reads current firmware operation mode using NHI mailbox |
| 618 | * registers and returns it to the caller. |
| 619 | */ |
| 620 | enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi) |
| 621 | { |
| 622 | u32 val; |
| 623 | |
| 624 | val = ioread32(nhi->iobase + REG_OUTMAIL_CMD); |
| 625 | val &= REG_OUTMAIL_CMD_OPMODE_MASK; |
| 626 | val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT; |
| 627 | |
| 628 | return (enum nhi_fw_mode)val; |
| 629 | } |
| 630 | |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 631 | static void nhi_interrupt_work(struct work_struct *work) |
| 632 | { |
| 633 | struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work); |
| 634 | int value = 0; /* Suppress uninitialized usage warning. */ |
| 635 | int bit; |
| 636 | int hop = -1; |
| 637 | int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */ |
| 638 | struct tb_ring *ring; |
| 639 | |
| 640 | mutex_lock(&nhi->lock); |
| 641 | |
| 642 | /* |
| 643 | * Starting at REG_RING_NOTIFY_BASE there are three status bitfields |
| 644 | * (TX, RX, RX overflow). We iterate over the bits and read a new |
| 645 | * dwords as required. The registers are cleared on read. |
| 646 | */ |
| 647 | for (bit = 0; bit < 3 * nhi->hop_count; bit++) { |
| 648 | if (bit % 32 == 0) |
| 649 | value = ioread32(nhi->iobase |
| 650 | + REG_RING_NOTIFY_BASE |
| 651 | + 4 * (bit / 32)); |
| 652 | if (++hop == nhi->hop_count) { |
| 653 | hop = 0; |
| 654 | type++; |
| 655 | } |
| 656 | if ((value & (1 << (bit % 32))) == 0) |
| 657 | continue; |
| 658 | if (type == 2) { |
| 659 | dev_warn(&nhi->pdev->dev, |
| 660 | "RX overflow for ring %d\n", |
| 661 | hop); |
| 662 | continue; |
| 663 | } |
| 664 | if (type == 0) |
| 665 | ring = nhi->tx_rings[hop]; |
| 666 | else |
| 667 | ring = nhi->rx_rings[hop]; |
| 668 | if (ring == NULL) { |
| 669 | dev_warn(&nhi->pdev->dev, |
| 670 | "got interrupt for inactive %s ring %d\n", |
| 671 | type ? "RX" : "TX", |
| 672 | hop); |
| 673 | continue; |
| 674 | } |
| 675 | /* we do not check ring->running, this is done in ring->work */ |
| 676 | schedule_work(&ring->work); |
| 677 | } |
| 678 | mutex_unlock(&nhi->lock); |
| 679 | } |
| 680 | |
| 681 | static irqreturn_t nhi_msi(int irq, void *data) |
| 682 | { |
| 683 | struct tb_nhi *nhi = data; |
| 684 | schedule_work(&nhi->interrupt_work); |
| 685 | return IRQ_HANDLED; |
| 686 | } |
| 687 | |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 688 | static int nhi_suspend_noirq(struct device *dev) |
| 689 | { |
| 690 | struct pci_dev *pdev = to_pci_dev(dev); |
| 691 | struct tb *tb = pci_get_drvdata(pdev); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 692 | |
| 693 | return tb_domain_suspend_noirq(tb); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 694 | } |
| 695 | |
Mika Westerberg | 8c6bba1 | 2017-10-02 13:38:35 +0300 | [diff] [blame] | 696 | static void nhi_enable_int_throttling(struct tb_nhi *nhi) |
| 697 | { |
| 698 | /* Throttling is specified in 256ns increments */ |
| 699 | u32 throttle = DIV_ROUND_UP(128 * NSEC_PER_USEC, 256); |
| 700 | unsigned int i; |
| 701 | |
| 702 | /* |
| 703 | * Configure interrupt throttling for all vectors even if we |
| 704 | * only use few. |
| 705 | */ |
| 706 | for (i = 0; i < MSIX_MAX_VECS; i++) { |
| 707 | u32 reg = REG_INT_THROTTLING_RATE + i * 4; |
| 708 | iowrite32(throttle, nhi->iobase + reg); |
| 709 | } |
| 710 | } |
| 711 | |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 712 | static int nhi_resume_noirq(struct device *dev) |
| 713 | { |
| 714 | struct pci_dev *pdev = to_pci_dev(dev); |
| 715 | struct tb *tb = pci_get_drvdata(pdev); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 716 | |
Mika Westerberg | bdccf29 | 2017-06-06 15:25:15 +0300 | [diff] [blame] | 717 | /* |
| 718 | * Check that the device is still there. It may be that the user |
| 719 | * unplugged last device which causes the host controller to go |
| 720 | * away on PCs. |
| 721 | */ |
| 722 | if (!pci_device_is_present(pdev)) |
| 723 | tb->nhi->going_away = true; |
Mika Westerberg | 8c6bba1 | 2017-10-02 13:38:35 +0300 | [diff] [blame] | 724 | else |
| 725 | nhi_enable_int_throttling(tb->nhi); |
Mika Westerberg | bdccf29 | 2017-06-06 15:25:15 +0300 | [diff] [blame] | 726 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 727 | return tb_domain_resume_noirq(tb); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 728 | } |
| 729 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 730 | static int nhi_suspend(struct device *dev) |
| 731 | { |
| 732 | struct pci_dev *pdev = to_pci_dev(dev); |
| 733 | struct tb *tb = pci_get_drvdata(pdev); |
| 734 | |
| 735 | return tb_domain_suspend(tb); |
| 736 | } |
| 737 | |
| 738 | static void nhi_complete(struct device *dev) |
| 739 | { |
| 740 | struct pci_dev *pdev = to_pci_dev(dev); |
| 741 | struct tb *tb = pci_get_drvdata(pdev); |
| 742 | |
| 743 | tb_domain_complete(tb); |
| 744 | } |
| 745 | |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 746 | static void nhi_shutdown(struct tb_nhi *nhi) |
| 747 | { |
| 748 | int i; |
| 749 | dev_info(&nhi->pdev->dev, "shutdown\n"); |
| 750 | |
| 751 | for (i = 0; i < nhi->hop_count; i++) { |
| 752 | if (nhi->tx_rings[i]) |
| 753 | dev_WARN(&nhi->pdev->dev, |
| 754 | "TX ring %d is still active\n", i); |
| 755 | if (nhi->rx_rings[i]) |
| 756 | dev_WARN(&nhi->pdev->dev, |
| 757 | "RX ring %d is still active\n", i); |
| 758 | } |
| 759 | nhi_disable_interrupts(nhi); |
| 760 | /* |
| 761 | * We have to release the irq before calling flush_work. Otherwise an |
| 762 | * already executing IRQ handler could call schedule_work again. |
| 763 | */ |
Mika Westerberg | 046bee1 | 2017-06-06 15:24:57 +0300 | [diff] [blame] | 764 | if (!nhi->pdev->msix_enabled) { |
| 765 | devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi); |
| 766 | flush_work(&nhi->interrupt_work); |
| 767 | } |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 768 | mutex_destroy(&nhi->lock); |
Mika Westerberg | 046bee1 | 2017-06-06 15:24:57 +0300 | [diff] [blame] | 769 | ida_destroy(&nhi->msix_ida); |
| 770 | } |
| 771 | |
| 772 | static int nhi_init_msi(struct tb_nhi *nhi) |
| 773 | { |
| 774 | struct pci_dev *pdev = nhi->pdev; |
| 775 | int res, irq, nvec; |
| 776 | |
| 777 | /* In case someone left them on. */ |
| 778 | nhi_disable_interrupts(nhi); |
| 779 | |
Mika Westerberg | 8c6bba1 | 2017-10-02 13:38:35 +0300 | [diff] [blame] | 780 | nhi_enable_int_throttling(nhi); |
| 781 | |
Mika Westerberg | 046bee1 | 2017-06-06 15:24:57 +0300 | [diff] [blame] | 782 | ida_init(&nhi->msix_ida); |
| 783 | |
| 784 | /* |
| 785 | * The NHI has 16 MSI-X vectors or a single MSI. We first try to |
| 786 | * get all MSI-X vectors and if we succeed, each ring will have |
| 787 | * one MSI-X. If for some reason that does not work out, we |
| 788 | * fallback to a single MSI. |
| 789 | */ |
| 790 | nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS, |
| 791 | PCI_IRQ_MSIX); |
| 792 | if (nvec < 0) { |
| 793 | nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI); |
| 794 | if (nvec < 0) |
| 795 | return nvec; |
| 796 | |
| 797 | INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work); |
| 798 | |
| 799 | irq = pci_irq_vector(nhi->pdev, 0); |
| 800 | if (irq < 0) |
| 801 | return irq; |
| 802 | |
| 803 | res = devm_request_irq(&pdev->dev, irq, nhi_msi, |
| 804 | IRQF_NO_SUSPEND, "thunderbolt", nhi); |
| 805 | if (res) { |
| 806 | dev_err(&pdev->dev, "request_irq failed, aborting\n"); |
| 807 | return res; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | return 0; |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 815 | { |
| 816 | struct tb_nhi *nhi; |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 817 | struct tb *tb; |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 818 | int res; |
| 819 | |
| 820 | res = pcim_enable_device(pdev); |
| 821 | if (res) { |
| 822 | dev_err(&pdev->dev, "cannot enable PCI device, aborting\n"); |
| 823 | return res; |
| 824 | } |
| 825 | |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 826 | res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt"); |
| 827 | if (res) { |
| 828 | dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n"); |
| 829 | return res; |
| 830 | } |
| 831 | |
| 832 | nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL); |
| 833 | if (!nhi) |
| 834 | return -ENOMEM; |
| 835 | |
| 836 | nhi->pdev = pdev; |
| 837 | /* cannot fail - table is allocated bin pcim_iomap_regions */ |
| 838 | nhi->iobase = pcim_iomap_table(pdev)[0]; |
| 839 | nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff; |
Lukas Wunner | 19bf4d4 | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 840 | if (nhi->hop_count != 12 && nhi->hop_count != 32) |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 841 | dev_warn(&pdev->dev, "unexpected hop count: %d\n", |
| 842 | nhi->hop_count); |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 843 | |
Himangi Saraogi | 2a211f3 | 2014-07-12 01:12:43 +0530 | [diff] [blame] | 844 | nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count, |
| 845 | sizeof(*nhi->tx_rings), GFP_KERNEL); |
| 846 | nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count, |
| 847 | sizeof(*nhi->rx_rings), GFP_KERNEL); |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 848 | if (!nhi->tx_rings || !nhi->rx_rings) |
| 849 | return -ENOMEM; |
| 850 | |
Mika Westerberg | 046bee1 | 2017-06-06 15:24:57 +0300 | [diff] [blame] | 851 | res = nhi_init_msi(nhi); |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 852 | if (res) { |
Mika Westerberg | 046bee1 | 2017-06-06 15:24:57 +0300 | [diff] [blame] | 853 | dev_err(&pdev->dev, "cannot enable MSI, aborting\n"); |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 854 | return res; |
| 855 | } |
| 856 | |
| 857 | mutex_init(&nhi->lock); |
| 858 | |
| 859 | pci_set_master(pdev); |
| 860 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 861 | tb = icm_probe(nhi); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 862 | if (!tb) |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 863 | tb = tb_probe(nhi); |
| 864 | if (!tb) { |
| 865 | dev_err(&nhi->pdev->dev, |
| 866 | "failed to determine connection manager, aborting\n"); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 867 | return -ENODEV; |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | dev_info(&nhi->pdev->dev, "NHI initialized, starting thunderbolt\n"); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 871 | |
| 872 | res = tb_domain_add(tb); |
| 873 | if (res) { |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 874 | /* |
| 875 | * At this point the RX/TX rings might already have been |
| 876 | * activated. Do a proper shutdown. |
| 877 | */ |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 878 | tb_domain_put(tb); |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 879 | nhi_shutdown(nhi); |
| 880 | return -EIO; |
| 881 | } |
| 882 | pci_set_drvdata(pdev, tb); |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | static void nhi_remove(struct pci_dev *pdev) |
| 888 | { |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 889 | struct tb *tb = pci_get_drvdata(pdev); |
| 890 | struct tb_nhi *nhi = tb->nhi; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 891 | |
| 892 | tb_domain_remove(tb); |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 893 | nhi_shutdown(nhi); |
| 894 | } |
| 895 | |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 896 | /* |
| 897 | * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable |
| 898 | * the tunnels asap. A corresponding pci quirk blocks the downstream bridges |
| 899 | * resume_noirq until we are done. |
| 900 | */ |
| 901 | static const struct dev_pm_ops nhi_pm_ops = { |
| 902 | .suspend_noirq = nhi_suspend_noirq, |
| 903 | .resume_noirq = nhi_resume_noirq, |
| 904 | .freeze_noirq = nhi_suspend_noirq, /* |
| 905 | * we just disable hotplug, the |
| 906 | * pci-tunnels stay alive. |
| 907 | */ |
| 908 | .restore_noirq = nhi_resume_noirq, |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 909 | .suspend = nhi_suspend, |
| 910 | .freeze = nhi_suspend, |
| 911 | .poweroff = nhi_suspend, |
| 912 | .complete = nhi_complete, |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 913 | }; |
| 914 | |
Sachin Kamat | 620863f | 2014-06-20 14:32:34 +0530 | [diff] [blame] | 915 | static struct pci_device_id nhi_ids[] = { |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 916 | /* |
| 917 | * We have to specify class, the TB bridges use the same device and |
Lukas Wunner | 1d11140 | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 918 | * vendor (sub)id on gen 1 and gen 2 controllers. |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 919 | */ |
| 920 | { |
| 921 | .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0, |
Lukas Wunner | 1d11140 | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 922 | .vendor = PCI_VENDOR_ID_INTEL, |
Lukas Wunner | 19bf4d4 | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 923 | .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE, |
| 924 | .subvendor = 0x2222, .subdevice = 0x1111, |
| 925 | }, |
| 926 | { |
| 927 | .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0, |
| 928 | .vendor = PCI_VENDOR_ID_INTEL, |
Lukas Wunner | 1d11140 | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 929 | .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C, |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 930 | .subvendor = 0x2222, .subdevice = 0x1111, |
| 931 | }, |
| 932 | { |
| 933 | .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0, |
Lukas Wunner | 1d11140 | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 934 | .vendor = PCI_VENDOR_ID_INTEL, |
Xavier Gnata | 82a6a81 | 2016-07-26 18:40:38 +0200 | [diff] [blame] | 935 | .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI, |
| 936 | .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID, |
| 937 | }, |
| 938 | { |
| 939 | .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0, |
| 940 | .vendor = PCI_VENDOR_ID_INTEL, |
Lukas Wunner | 1d11140 | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 941 | .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI, |
Knuth Posern | a42fb35 | 2015-09-20 21:25:22 +0200 | [diff] [blame] | 942 | .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID, |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 943 | }, |
Mika Westerberg | 5e2781b | 2017-06-06 15:25:11 +0300 | [diff] [blame] | 944 | |
| 945 | /* Thunderbolt 3 */ |
| 946 | { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) }, |
| 947 | { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) }, |
| 948 | { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) }, |
| 949 | { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) }, |
| 950 | { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) }, |
| 951 | { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) }, |
| 952 | { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) }, |
| 953 | { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) }, |
| 954 | |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 955 | { 0,} |
| 956 | }; |
| 957 | |
| 958 | MODULE_DEVICE_TABLE(pci, nhi_ids); |
| 959 | MODULE_LICENSE("GPL"); |
| 960 | |
| 961 | static struct pci_driver nhi_driver = { |
| 962 | .name = "thunderbolt", |
| 963 | .id_table = nhi_ids, |
| 964 | .probe = nhi_probe, |
| 965 | .remove = nhi_remove, |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 966 | .driver.pm = &nhi_pm_ops, |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 967 | }; |
| 968 | |
| 969 | static int __init nhi_init(void) |
| 970 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 971 | int ret; |
| 972 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 973 | ret = tb_domain_init(); |
| 974 | if (ret) |
| 975 | return ret; |
| 976 | ret = pci_register_driver(&nhi_driver); |
| 977 | if (ret) |
| 978 | tb_domain_exit(); |
| 979 | return ret; |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | static void __exit nhi_unload(void) |
| 983 | { |
| 984 | pci_unregister_driver(&nhi_driver); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 985 | tb_domain_exit(); |
Andreas Noever | 1660315 | 2014-06-03 22:03:58 +0200 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | module_init(nhi_init); |
| 989 | module_exit(nhi_unload); |