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