2 * Thunderbolt Cactus Ridge driver - NHI driver
4 * The NHI (native host interface) is the pci device that allows us to send and
5 * receive frames from the thunderbolt bus.
7 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
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>
22 #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")
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.
28 #define MSIX_MIN_VECS 6
29 #define MSIX_MAX_VECS 16
31 #define NHI_MAILBOX_TIMEOUT 500 /* ms */
33 static int ring_interrupt_index(struct tb_ring
*ring
)
37 bit
+= ring
->nhi
->hop_count
;
42 * ring_interrupt_active() - activate/deactivate interrupts for a single ring
44 * ring->nhi->lock must be held.
46 static void ring_interrupt_active(struct tb_ring
*ring
, bool active
)
48 int reg
= REG_RING_INTERRUPT_BASE
+
49 ring_interrupt_index(ring
) / 32 * 4;
50 int bit
= ring_interrupt_index(ring
) & 31;
55 u32 step
, shift
, ivr
, misc
;
56 void __iomem
*ivr_base
;
62 index
= ring
->hop
+ ring
->nhi
->hop_count
;
65 * Ask the hardware to clear interrupt status bits automatically
66 * since we already know which interrupt was triggered.
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
);
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
);
80 ivr
|= ring
->vector
<< shift
;
81 iowrite32(ivr
, ivr_base
+ step
);
84 old
= ioread32(ring
->nhi
->iobase
+ reg
);
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);
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
);
103 * nhi_disable_interrupts() - disable interrupts for all rings
105 * Use only during init and shutdown.
107 static void nhi_disable_interrupts(struct tb_nhi
*nhi
)
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
);
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
);
119 /* ring helper methods */
121 static void __iomem
*ring_desc_base(struct tb_ring
*ring
)
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;
129 static void __iomem
*ring_options_base(struct tb_ring
*ring
)
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;
137 static void ring_iowrite16desc(struct tb_ring
*ring
, u32 value
, u32 offset
)
139 iowrite16(value
, ring_desc_base(ring
) + offset
);
142 static void ring_iowrite32desc(struct tb_ring
*ring
, u32 value
, u32 offset
)
144 iowrite32(value
, ring_desc_base(ring
) + offset
);
147 static void ring_iowrite64desc(struct tb_ring
*ring
, u64 value
, u32 offset
)
149 iowrite32(value
, ring_desc_base(ring
) + offset
);
150 iowrite32(value
>> 32, ring_desc_base(ring
) + offset
+ 4);
153 static void ring_iowrite32options(struct tb_ring
*ring
, u32 value
, u32 offset
)
155 iowrite32(value
, ring_options_base(ring
) + offset
);
158 static bool ring_full(struct tb_ring
*ring
)
160 return ((ring
->head
+ 1) % ring
->size
) == ring
->tail
;
163 static bool ring_empty(struct tb_ring
*ring
)
165 return ring
->head
== ring
->tail
;
169 * ring_write_descriptors() - post frames from ring->queue to the controller
171 * ring->lock is held.
173 static void ring_write_descriptors(struct tb_ring
*ring
)
175 struct ring_frame
*frame
, *n
;
176 struct ring_desc
*descriptor
;
177 list_for_each_entry_safe(frame
, n
, &ring
->queue
, list
) {
180 list_move_tail(&frame
->list
, &ring
->in_flight
);
181 descriptor
= &ring
->descriptors
[ring
->head
];
182 descriptor
->phys
= frame
->buffer_phy
;
183 descriptor
->time
= 0;
184 descriptor
->flags
= RING_DESC_POSTED
| RING_DESC_INTERRUPT
;
186 descriptor
->length
= frame
->size
;
187 descriptor
->eof
= frame
->eof
;
188 descriptor
->sof
= frame
->sof
;
190 ring
->head
= (ring
->head
+ 1) % ring
->size
;
191 ring_iowrite16desc(ring
, ring
->head
, ring
->is_tx
? 10 : 8);
196 * ring_work() - progress completed frames
198 * If the ring is shutting down then all frames are marked as canceled and
199 * their callbacks are invoked.
201 * Otherwise we collect all completed frame from the ring buffer, write new
202 * frame to the ring buffer and invoke the callbacks for the completed frames.
204 static void ring_work(struct work_struct
*work
)
206 struct tb_ring
*ring
= container_of(work
, typeof(*ring
), work
);
207 struct ring_frame
*frame
;
208 bool canceled
= false;
210 mutex_lock(&ring
->lock
);
212 if (!ring
->running
) {
213 /* Move all frames to done and mark them as canceled. */
214 list_splice_tail_init(&ring
->in_flight
, &done
);
215 list_splice_tail_init(&ring
->queue
, &done
);
217 goto invoke_callback
;
220 while (!ring_empty(ring
)) {
221 if (!(ring
->descriptors
[ring
->tail
].flags
222 & RING_DESC_COMPLETED
))
224 frame
= list_first_entry(&ring
->in_flight
, typeof(*frame
),
226 list_move_tail(&frame
->list
, &done
);
228 frame
->size
= ring
->descriptors
[ring
->tail
].length
;
229 frame
->eof
= ring
->descriptors
[ring
->tail
].eof
;
230 frame
->sof
= ring
->descriptors
[ring
->tail
].sof
;
231 frame
->flags
= ring
->descriptors
[ring
->tail
].flags
;
233 dev_WARN(&ring
->nhi
->pdev
->dev
,
234 "%s %d got unexpected SOF: %#x\n",
235 RING_TYPE(ring
), ring
->hop
,
239 * raw not enabled, interupt not set: 0x2=0010
240 * raw enabled: 0xa=1010
241 * raw not enabled: 0xb=1011
242 * partial frame (>MAX_FRAME_SIZE): 0xe=1110
244 if (frame
->flags
!= 0xa)
245 dev_WARN(&ring
->nhi
->pdev
->dev
,
246 "%s %d got unexpected flags: %#x\n",
247 RING_TYPE(ring
), ring
->hop
,
250 ring
->tail
= (ring
->tail
+ 1) % ring
->size
;
252 ring_write_descriptors(ring
);
255 mutex_unlock(&ring
->lock
); /* allow callbacks to schedule new work */
256 while (!list_empty(&done
)) {
257 frame
= list_first_entry(&done
, typeof(*frame
), list
);
259 * The callback may reenqueue or delete frame.
260 * Do not hold on to it.
262 list_del_init(&frame
->list
);
263 frame
->callback(ring
, frame
, canceled
);
267 int __ring_enqueue(struct tb_ring
*ring
, struct ring_frame
*frame
)
270 mutex_lock(&ring
->lock
);
272 list_add_tail(&frame
->list
, &ring
->queue
);
273 ring_write_descriptors(ring
);
277 mutex_unlock(&ring
->lock
);
281 static irqreturn_t
ring_msix(int irq
, void *data
)
283 struct tb_ring
*ring
= data
;
285 schedule_work(&ring
->work
);
289 static int ring_request_msix(struct tb_ring
*ring
, bool no_suspend
)
291 struct tb_nhi
*nhi
= ring
->nhi
;
292 unsigned long irqflags
;
295 if (!nhi
->pdev
->msix_enabled
)
298 ret
= ida_simple_get(&nhi
->msix_ida
, 0, MSIX_MAX_VECS
, GFP_KERNEL
);
304 ring
->irq
= pci_irq_vector(ring
->nhi
->pdev
, ring
->vector
);
308 irqflags
= no_suspend
? IRQF_NO_SUSPEND
: 0;
309 return request_irq(ring
->irq
, ring_msix
, irqflags
, "thunderbolt", ring
);
312 static void ring_release_msix(struct tb_ring
*ring
)
317 free_irq(ring
->irq
, ring
);
318 ida_simple_remove(&ring
->nhi
->msix_ida
, ring
->vector
);
323 static struct tb_ring
*ring_alloc(struct tb_nhi
*nhi
, u32 hop
, int size
,
324 bool transmit
, unsigned int flags
)
326 struct tb_ring
*ring
= NULL
;
327 dev_info(&nhi
->pdev
->dev
, "allocating %s ring %d of size %d\n",
328 transmit
? "TX" : "RX", hop
, size
);
330 mutex_lock(&nhi
->lock
);
331 if (hop
>= nhi
->hop_count
) {
332 dev_WARN(&nhi
->pdev
->dev
, "invalid hop: %d\n", hop
);
335 if (transmit
&& nhi
->tx_rings
[hop
]) {
336 dev_WARN(&nhi
->pdev
->dev
, "TX hop %d already allocated\n", hop
);
338 } else if (!transmit
&& nhi
->rx_rings
[hop
]) {
339 dev_WARN(&nhi
->pdev
->dev
, "RX hop %d already allocated\n", hop
);
342 ring
= kzalloc(sizeof(*ring
), GFP_KERNEL
);
346 mutex_init(&ring
->lock
);
347 INIT_LIST_HEAD(&ring
->queue
);
348 INIT_LIST_HEAD(&ring
->in_flight
);
349 INIT_WORK(&ring
->work
, ring_work
);
353 ring
->is_tx
= transmit
;
358 ring
->running
= false;
360 if (ring_request_msix(ring
, flags
& RING_FLAG_NO_SUSPEND
))
363 ring
->descriptors
= dma_alloc_coherent(&ring
->nhi
->pdev
->dev
,
364 size
* sizeof(*ring
->descriptors
),
365 &ring
->descriptors_dma
, GFP_KERNEL
| __GFP_ZERO
);
366 if (!ring
->descriptors
)
370 nhi
->tx_rings
[hop
] = ring
;
372 nhi
->rx_rings
[hop
] = ring
;
373 mutex_unlock(&nhi
->lock
);
378 mutex_destroy(&ring
->lock
);
380 mutex_unlock(&nhi
->lock
);
384 struct tb_ring
*ring_alloc_tx(struct tb_nhi
*nhi
, int hop
, int size
,
387 return ring_alloc(nhi
, hop
, size
, true, flags
);
390 struct tb_ring
*ring_alloc_rx(struct tb_nhi
*nhi
, int hop
, int size
,
393 return ring_alloc(nhi
, hop
, size
, false, flags
);
397 * ring_start() - enable a ring
399 * Must not be invoked in parallel with ring_stop().
401 void ring_start(struct tb_ring
*ring
)
403 mutex_lock(&ring
->nhi
->lock
);
404 mutex_lock(&ring
->lock
);
405 if (ring
->nhi
->going_away
)
408 dev_WARN(&ring
->nhi
->pdev
->dev
, "ring already started\n");
411 dev_info(&ring
->nhi
->pdev
->dev
, "starting %s %d\n",
412 RING_TYPE(ring
), ring
->hop
);
414 ring_iowrite64desc(ring
, ring
->descriptors_dma
, 0);
416 ring_iowrite32desc(ring
, ring
->size
, 12);
417 ring_iowrite32options(ring
, 0, 4); /* time releated ? */
418 ring_iowrite32options(ring
,
419 RING_FLAG_ENABLE
| RING_FLAG_RAW
, 0);
421 ring_iowrite32desc(ring
,
422 (TB_FRAME_SIZE
<< 16) | ring
->size
, 12);
423 ring_iowrite32options(ring
, 0xffffffff, 4); /* SOF EOF mask */
424 ring_iowrite32options(ring
,
425 RING_FLAG_ENABLE
| RING_FLAG_RAW
, 0);
427 ring_interrupt_active(ring
, true);
428 ring
->running
= true;
430 mutex_unlock(&ring
->lock
);
431 mutex_unlock(&ring
->nhi
->lock
);
436 * ring_stop() - shutdown a ring
438 * Must not be invoked from a callback.
440 * This method will disable the ring. Further calls to ring_tx/ring_rx will
441 * return -ESHUTDOWN until ring_stop has been called.
443 * All enqueued frames will be canceled and their callbacks will be executed
444 * with frame->canceled set to true (on the callback thread). This method
445 * returns only after all callback invocations have finished.
447 void ring_stop(struct tb_ring
*ring
)
449 mutex_lock(&ring
->nhi
->lock
);
450 mutex_lock(&ring
->lock
);
451 dev_info(&ring
->nhi
->pdev
->dev
, "stopping %s %d\n",
452 RING_TYPE(ring
), ring
->hop
);
453 if (ring
->nhi
->going_away
)
455 if (!ring
->running
) {
456 dev_WARN(&ring
->nhi
->pdev
->dev
, "%s %d already stopped\n",
457 RING_TYPE(ring
), ring
->hop
);
460 ring_interrupt_active(ring
, false);
462 ring_iowrite32options(ring
, 0, 0);
463 ring_iowrite64desc(ring
, 0, 0);
464 ring_iowrite16desc(ring
, 0, ring
->is_tx
? 10 : 8);
465 ring_iowrite32desc(ring
, 0, 12);
468 ring
->running
= false;
471 mutex_unlock(&ring
->lock
);
472 mutex_unlock(&ring
->nhi
->lock
);
475 * schedule ring->work to invoke callbacks on all remaining frames.
477 schedule_work(&ring
->work
);
478 flush_work(&ring
->work
);
482 * ring_free() - free ring
484 * When this method returns all invocations of ring->callback will have
487 * Ring must be stopped.
489 * Must NOT be called from ring_frame->callback!
491 void ring_free(struct tb_ring
*ring
)
493 mutex_lock(&ring
->nhi
->lock
);
495 * Dissociate the ring from the NHI. This also ensures that
496 * nhi_interrupt_work cannot reschedule ring->work.
499 ring
->nhi
->tx_rings
[ring
->hop
] = NULL
;
501 ring
->nhi
->rx_rings
[ring
->hop
] = NULL
;
504 dev_WARN(&ring
->nhi
->pdev
->dev
, "%s %d still running\n",
505 RING_TYPE(ring
), ring
->hop
);
508 ring_release_msix(ring
);
510 dma_free_coherent(&ring
->nhi
->pdev
->dev
,
511 ring
->size
* sizeof(*ring
->descriptors
),
512 ring
->descriptors
, ring
->descriptors_dma
);
514 ring
->descriptors
= NULL
;
515 ring
->descriptors_dma
= 0;
518 dev_info(&ring
->nhi
->pdev
->dev
,
523 mutex_unlock(&ring
->nhi
->lock
);
525 * ring->work can no longer be scheduled (it is scheduled only
526 * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
527 * to finish before freeing the ring.
529 flush_work(&ring
->work
);
530 mutex_destroy(&ring
->lock
);
535 * nhi_mailbox_cmd() - Send a command through NHI mailbox
536 * @nhi: Pointer to the NHI structure
537 * @cmd: Command to send
538 * @data: Data to be send with the command
540 * Sends mailbox command to the firmware running on NHI. Returns %0 in
541 * case of success and negative errno in case of failure.
543 int nhi_mailbox_cmd(struct tb_nhi
*nhi
, enum nhi_mailbox_cmd cmd
, u32 data
)
548 iowrite32(data
, nhi
->iobase
+ REG_INMAIL_DATA
);
550 val
= ioread32(nhi
->iobase
+ REG_INMAIL_CMD
);
551 val
&= ~(REG_INMAIL_CMD_MASK
| REG_INMAIL_ERROR
);
552 val
|= REG_INMAIL_OP_REQUEST
| cmd
;
553 iowrite32(val
, nhi
->iobase
+ REG_INMAIL_CMD
);
555 timeout
= ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT
);
557 val
= ioread32(nhi
->iobase
+ REG_INMAIL_CMD
);
558 if (!(val
& REG_INMAIL_OP_REQUEST
))
560 usleep_range(10, 20);
561 } while (ktime_before(ktime_get(), timeout
));
563 if (val
& REG_INMAIL_OP_REQUEST
)
565 if (val
& REG_INMAIL_ERROR
)
572 * nhi_mailbox_mode() - Return current firmware operation mode
573 * @nhi: Pointer to the NHI structure
575 * The function reads current firmware operation mode using NHI mailbox
576 * registers and returns it to the caller.
578 enum nhi_fw_mode
nhi_mailbox_mode(struct tb_nhi
*nhi
)
582 val
= ioread32(nhi
->iobase
+ REG_OUTMAIL_CMD
);
583 val
&= REG_OUTMAIL_CMD_OPMODE_MASK
;
584 val
>>= REG_OUTMAIL_CMD_OPMODE_SHIFT
;
586 return (enum nhi_fw_mode
)val
;
589 static void nhi_interrupt_work(struct work_struct
*work
)
591 struct tb_nhi
*nhi
= container_of(work
, typeof(*nhi
), interrupt_work
);
592 int value
= 0; /* Suppress uninitialized usage warning. */
595 int type
= 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
596 struct tb_ring
*ring
;
598 mutex_lock(&nhi
->lock
);
601 * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
602 * (TX, RX, RX overflow). We iterate over the bits and read a new
603 * dwords as required. The registers are cleared on read.
605 for (bit
= 0; bit
< 3 * nhi
->hop_count
; bit
++) {
607 value
= ioread32(nhi
->iobase
608 + REG_RING_NOTIFY_BASE
610 if (++hop
== nhi
->hop_count
) {
614 if ((value
& (1 << (bit
% 32))) == 0)
617 dev_warn(&nhi
->pdev
->dev
,
618 "RX overflow for ring %d\n",
623 ring
= nhi
->tx_rings
[hop
];
625 ring
= nhi
->rx_rings
[hop
];
627 dev_warn(&nhi
->pdev
->dev
,
628 "got interrupt for inactive %s ring %d\n",
633 /* we do not check ring->running, this is done in ring->work */
634 schedule_work(&ring
->work
);
636 mutex_unlock(&nhi
->lock
);
639 static irqreturn_t
nhi_msi(int irq
, void *data
)
641 struct tb_nhi
*nhi
= data
;
642 schedule_work(&nhi
->interrupt_work
);
646 static int nhi_suspend_noirq(struct device
*dev
)
648 struct pci_dev
*pdev
= to_pci_dev(dev
);
649 struct tb
*tb
= pci_get_drvdata(pdev
);
651 return tb_domain_suspend_noirq(tb
);
654 static int nhi_resume_noirq(struct device
*dev
)
656 struct pci_dev
*pdev
= to_pci_dev(dev
);
657 struct tb
*tb
= pci_get_drvdata(pdev
);
660 * Check that the device is still there. It may be that the user
661 * unplugged last device which causes the host controller to go
664 if (!pci_device_is_present(pdev
))
665 tb
->nhi
->going_away
= true;
667 return tb_domain_resume_noirq(tb
);
670 static int nhi_suspend(struct device
*dev
)
672 struct pci_dev
*pdev
= to_pci_dev(dev
);
673 struct tb
*tb
= pci_get_drvdata(pdev
);
675 return tb_domain_suspend(tb
);
678 static void nhi_complete(struct device
*dev
)
680 struct pci_dev
*pdev
= to_pci_dev(dev
);
681 struct tb
*tb
= pci_get_drvdata(pdev
);
683 tb_domain_complete(tb
);
686 static void nhi_shutdown(struct tb_nhi
*nhi
)
689 dev_info(&nhi
->pdev
->dev
, "shutdown\n");
691 for (i
= 0; i
< nhi
->hop_count
; i
++) {
692 if (nhi
->tx_rings
[i
])
693 dev_WARN(&nhi
->pdev
->dev
,
694 "TX ring %d is still active\n", i
);
695 if (nhi
->rx_rings
[i
])
696 dev_WARN(&nhi
->pdev
->dev
,
697 "RX ring %d is still active\n", i
);
699 nhi_disable_interrupts(nhi
);
701 * We have to release the irq before calling flush_work. Otherwise an
702 * already executing IRQ handler could call schedule_work again.
704 if (!nhi
->pdev
->msix_enabled
) {
705 devm_free_irq(&nhi
->pdev
->dev
, nhi
->pdev
->irq
, nhi
);
706 flush_work(&nhi
->interrupt_work
);
708 mutex_destroy(&nhi
->lock
);
709 ida_destroy(&nhi
->msix_ida
);
712 static int nhi_init_msi(struct tb_nhi
*nhi
)
714 struct pci_dev
*pdev
= nhi
->pdev
;
717 /* In case someone left them on. */
718 nhi_disable_interrupts(nhi
);
720 ida_init(&nhi
->msix_ida
);
723 * The NHI has 16 MSI-X vectors or a single MSI. We first try to
724 * get all MSI-X vectors and if we succeed, each ring will have
725 * one MSI-X. If for some reason that does not work out, we
726 * fallback to a single MSI.
728 nvec
= pci_alloc_irq_vectors(pdev
, MSIX_MIN_VECS
, MSIX_MAX_VECS
,
731 nvec
= pci_alloc_irq_vectors(pdev
, 1, 1, PCI_IRQ_MSI
);
735 INIT_WORK(&nhi
->interrupt_work
, nhi_interrupt_work
);
737 irq
= pci_irq_vector(nhi
->pdev
, 0);
741 res
= devm_request_irq(&pdev
->dev
, irq
, nhi_msi
,
742 IRQF_NO_SUSPEND
, "thunderbolt", nhi
);
744 dev_err(&pdev
->dev
, "request_irq failed, aborting\n");
752 static int nhi_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
758 res
= pcim_enable_device(pdev
);
760 dev_err(&pdev
->dev
, "cannot enable PCI device, aborting\n");
764 res
= pcim_iomap_regions(pdev
, 1 << 0, "thunderbolt");
766 dev_err(&pdev
->dev
, "cannot obtain PCI resources, aborting\n");
770 nhi
= devm_kzalloc(&pdev
->dev
, sizeof(*nhi
), GFP_KERNEL
);
775 /* cannot fail - table is allocated bin pcim_iomap_regions */
776 nhi
->iobase
= pcim_iomap_table(pdev
)[0];
777 nhi
->hop_count
= ioread32(nhi
->iobase
+ REG_HOP_COUNT
) & 0x3ff;
778 if (nhi
->hop_count
!= 12 && nhi
->hop_count
!= 32)
779 dev_warn(&pdev
->dev
, "unexpected hop count: %d\n",
782 nhi
->tx_rings
= devm_kcalloc(&pdev
->dev
, nhi
->hop_count
,
783 sizeof(*nhi
->tx_rings
), GFP_KERNEL
);
784 nhi
->rx_rings
= devm_kcalloc(&pdev
->dev
, nhi
->hop_count
,
785 sizeof(*nhi
->rx_rings
), GFP_KERNEL
);
786 if (!nhi
->tx_rings
|| !nhi
->rx_rings
)
789 res
= nhi_init_msi(nhi
);
791 dev_err(&pdev
->dev
, "cannot enable MSI, aborting\n");
795 mutex_init(&nhi
->lock
);
797 pci_set_master(pdev
);
799 /* magic value - clock related? */
800 iowrite32(3906250 / 10000, nhi
->iobase
+ 0x38c00);
806 dev_err(&nhi
->pdev
->dev
,
807 "failed to determine connection manager, aborting\n");
811 dev_info(&nhi
->pdev
->dev
, "NHI initialized, starting thunderbolt\n");
813 res
= tb_domain_add(tb
);
816 * At this point the RX/TX rings might already have been
817 * activated. Do a proper shutdown.
823 pci_set_drvdata(pdev
, tb
);
828 static void nhi_remove(struct pci_dev
*pdev
)
830 struct tb
*tb
= pci_get_drvdata(pdev
);
831 struct tb_nhi
*nhi
= tb
->nhi
;
833 tb_domain_remove(tb
);
838 * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
839 * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
840 * resume_noirq until we are done.
842 static const struct dev_pm_ops nhi_pm_ops
= {
843 .suspend_noirq
= nhi_suspend_noirq
,
844 .resume_noirq
= nhi_resume_noirq
,
845 .freeze_noirq
= nhi_suspend_noirq
, /*
846 * we just disable hotplug, the
847 * pci-tunnels stay alive.
849 .thaw_noirq
= nhi_resume_noirq
,
850 .restore_noirq
= nhi_resume_noirq
,
851 .suspend
= nhi_suspend
,
852 .freeze
= nhi_suspend
,
853 .poweroff
= nhi_suspend
,
854 .complete
= nhi_complete
,
857 static struct pci_device_id nhi_ids
[] = {
859 * We have to specify class, the TB bridges use the same device and
860 * vendor (sub)id on gen 1 and gen 2 controllers.
863 .class = PCI_CLASS_SYSTEM_OTHER
<< 8, .class_mask
= ~0,
864 .vendor
= PCI_VENDOR_ID_INTEL
,
865 .device
= PCI_DEVICE_ID_INTEL_LIGHT_RIDGE
,
866 .subvendor
= 0x2222, .subdevice
= 0x1111,
869 .class = PCI_CLASS_SYSTEM_OTHER
<< 8, .class_mask
= ~0,
870 .vendor
= PCI_VENDOR_ID_INTEL
,
871 .device
= PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C
,
872 .subvendor
= 0x2222, .subdevice
= 0x1111,
875 .class = PCI_CLASS_SYSTEM_OTHER
<< 8, .class_mask
= ~0,
876 .vendor
= PCI_VENDOR_ID_INTEL
,
877 .device
= PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI
,
878 .subvendor
= PCI_ANY_ID
, .subdevice
= PCI_ANY_ID
,
881 .class = PCI_CLASS_SYSTEM_OTHER
<< 8, .class_mask
= ~0,
882 .vendor
= PCI_VENDOR_ID_INTEL
,
883 .device
= PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI
,
884 .subvendor
= PCI_ANY_ID
, .subdevice
= PCI_ANY_ID
,
888 { PCI_VDEVICE(INTEL
, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI
) },
889 { PCI_VDEVICE(INTEL
, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI
) },
890 { PCI_VDEVICE(INTEL
, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI
) },
891 { PCI_VDEVICE(INTEL
, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI
) },
892 { PCI_VDEVICE(INTEL
, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI
) },
893 { PCI_VDEVICE(INTEL
, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI
) },
894 { PCI_VDEVICE(INTEL
, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI
) },
895 { PCI_VDEVICE(INTEL
, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI
) },
900 MODULE_DEVICE_TABLE(pci
, nhi_ids
);
901 MODULE_LICENSE("GPL");
903 static struct pci_driver nhi_driver
= {
904 .name
= "thunderbolt",
907 .remove
= nhi_remove
,
908 .driver
.pm
= &nhi_pm_ops
,
911 static int __init
nhi_init(void)
915 ret
= tb_domain_init();
918 ret
= pci_register_driver(&nhi_driver
);
924 static void __exit
nhi_unload(void)
926 pci_unregister_driver(&nhi_driver
);
930 module_init(nhi_init
);
931 module_exit(nhi_unload
);