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