2121
2222#define RING_TYPE (ring ) ((ring)->is_tx ? "TX ring" : "RX ring")
2323
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
2430
2531static int ring_interrupt_index (struct tb_ring * ring )
2632{
@@ -42,6 +48,37 @@ static void ring_interrupt_active(struct tb_ring *ring, bool active)
4248 int bit = ring_interrupt_index (ring ) & 31 ;
4349 int mask = 1 << bit ;
4450 u32 old , new ;
51+
52+ if (ring -> irq > 0 ) {
53+ u32 step , shift , ivr , misc ;
54+ void __iomem * ivr_base ;
55+ int index ;
56+
57+ if (ring -> is_tx )
58+ index = ring -> hop ;
59+ else
60+ index = ring -> hop + ring -> nhi -> hop_count ;
61+
62+ /*
63+ * Ask the hardware to clear interrupt status bits automatically
64+ * since we already know which interrupt was triggered.
65+ */
66+ misc = ioread32 (ring -> nhi -> iobase + REG_DMA_MISC );
67+ if (!(misc & REG_DMA_MISC_INT_AUTO_CLEAR )) {
68+ misc |= REG_DMA_MISC_INT_AUTO_CLEAR ;
69+ iowrite32 (misc , ring -> nhi -> iobase + REG_DMA_MISC );
70+ }
71+
72+ ivr_base = ring -> nhi -> iobase + REG_INT_VEC_ALLOC_BASE ;
73+ step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS ;
74+ shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS ;
75+ ivr = ioread32 (ivr_base + step );
76+ ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift );
77+ if (active )
78+ ivr |= ring -> vector << shift ;
79+ iowrite32 (ivr , ivr_base + step );
80+ }
81+
4582 old = ioread32 (ring -> nhi -> iobase + reg );
4683 if (active )
4784 new = old | mask ;
@@ -239,8 +276,50 @@ int __ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
239276 return ret ;
240277}
241278
279+ static irqreturn_t ring_msix (int irq , void * data )
280+ {
281+ struct tb_ring * ring = data ;
282+
283+ schedule_work (& ring -> work );
284+ return IRQ_HANDLED ;
285+ }
286+
287+ static int ring_request_msix (struct tb_ring * ring , bool no_suspend )
288+ {
289+ struct tb_nhi * nhi = ring -> nhi ;
290+ unsigned long irqflags ;
291+ int ret ;
292+
293+ if (!nhi -> pdev -> msix_enabled )
294+ return 0 ;
295+
296+ ret = ida_simple_get (& nhi -> msix_ida , 0 , MSIX_MAX_VECS , GFP_KERNEL );
297+ if (ret < 0 )
298+ return ret ;
299+
300+ ring -> vector = ret ;
301+
302+ ring -> irq = pci_irq_vector (ring -> nhi -> pdev , ring -> vector );
303+ if (ring -> irq < 0 )
304+ return ring -> irq ;
305+
306+ irqflags = no_suspend ? IRQF_NO_SUSPEND : 0 ;
307+ return request_irq (ring -> irq , ring_msix , irqflags , "thunderbolt" , ring );
308+ }
309+
310+ static void ring_release_msix (struct tb_ring * ring )
311+ {
312+ if (ring -> irq <= 0 )
313+ return ;
314+
315+ free_irq (ring -> irq , ring );
316+ ida_simple_remove (& ring -> nhi -> msix_ida , ring -> vector );
317+ ring -> vector = 0 ;
318+ ring -> irq = 0 ;
319+ }
320+
242321static struct tb_ring * ring_alloc (struct tb_nhi * nhi , u32 hop , int size ,
243- bool transmit )
322+ bool transmit , unsigned int flags )
244323{
245324 struct tb_ring * ring = NULL ;
246325 dev_info (& nhi -> pdev -> dev , "allocating %s ring %d of size %d\n" ,
@@ -271,9 +350,14 @@ static struct tb_ring *ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
271350 ring -> hop = hop ;
272351 ring -> is_tx = transmit ;
273352 ring -> size = size ;
353+ ring -> flags = flags ;
274354 ring -> head = 0 ;
275355 ring -> tail = 0 ;
276356 ring -> running = false;
357+
358+ if (ring_request_msix (ring , flags & RING_FLAG_NO_SUSPEND ))
359+ goto err ;
360+
277361 ring -> descriptors = dma_alloc_coherent (& ring -> nhi -> pdev -> dev ,
278362 size * sizeof (* ring -> descriptors ),
279363 & ring -> descriptors_dma , GFP_KERNEL | __GFP_ZERO );
@@ -295,14 +379,16 @@ static struct tb_ring *ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
295379 return NULL ;
296380}
297381
298- struct tb_ring * ring_alloc_tx (struct tb_nhi * nhi , int hop , int size )
382+ struct tb_ring * ring_alloc_tx (struct tb_nhi * nhi , int hop , int size ,
383+ unsigned int flags )
299384{
300- return ring_alloc (nhi , hop , size , true);
385+ return ring_alloc (nhi , hop , size , true, flags );
301386}
302387
303- struct tb_ring * ring_alloc_rx (struct tb_nhi * nhi , int hop , int size )
388+ struct tb_ring * ring_alloc_rx (struct tb_nhi * nhi , int hop , int size ,
389+ unsigned int flags )
304390{
305- return ring_alloc (nhi , hop , size , false);
391+ return ring_alloc (nhi , hop , size , false, flags );
306392}
307393
308394/**
@@ -413,6 +499,8 @@ void ring_free(struct tb_ring *ring)
413499 RING_TYPE (ring ), ring -> hop );
414500 }
415501
502+ ring_release_msix (ring );
503+
416504 dma_free_coherent (& ring -> nhi -> pdev -> dev ,
417505 ring -> size * sizeof (* ring -> descriptors ),
418506 ring -> descriptors , ring -> descriptors_dma );
@@ -428,9 +516,9 @@ void ring_free(struct tb_ring *ring)
428516
429517 mutex_unlock (& ring -> nhi -> lock );
430518 /**
431- * ring->work can no longer be scheduled (it is scheduled only by
432- * nhi_interrupt_work and ring_stop ). Wait for it to finish before
433- * freeing the ring.
519+ * ring->work can no longer be scheduled (it is scheduled only
520+ * by nhi_interrupt_work, ring_stop and ring_msix ). Wait for it
521+ * to finish before freeing the ring.
434522 */
435523 flush_work (& ring -> work );
436524 mutex_destroy (& ring -> lock );
@@ -528,9 +616,52 @@ static void nhi_shutdown(struct tb_nhi *nhi)
528616 * We have to release the irq before calling flush_work. Otherwise an
529617 * already executing IRQ handler could call schedule_work again.
530618 */
531- devm_free_irq (& nhi -> pdev -> dev , nhi -> pdev -> irq , nhi );
532- flush_work (& nhi -> interrupt_work );
619+ if (!nhi -> pdev -> msix_enabled ) {
620+ devm_free_irq (& nhi -> pdev -> dev , nhi -> pdev -> irq , nhi );
621+ flush_work (& nhi -> interrupt_work );
622+ }
533623 mutex_destroy (& nhi -> lock );
624+ ida_destroy (& nhi -> msix_ida );
625+ }
626+
627+ static int nhi_init_msi (struct tb_nhi * nhi )
628+ {
629+ struct pci_dev * pdev = nhi -> pdev ;
630+ int res , irq , nvec ;
631+
632+ /* In case someone left them on. */
633+ nhi_disable_interrupts (nhi );
634+
635+ ida_init (& nhi -> msix_ida );
636+
637+ /*
638+ * The NHI has 16 MSI-X vectors or a single MSI. We first try to
639+ * get all MSI-X vectors and if we succeed, each ring will have
640+ * one MSI-X. If for some reason that does not work out, we
641+ * fallback to a single MSI.
642+ */
643+ nvec = pci_alloc_irq_vectors (pdev , MSIX_MIN_VECS , MSIX_MAX_VECS ,
644+ PCI_IRQ_MSIX );
645+ if (nvec < 0 ) {
646+ nvec = pci_alloc_irq_vectors (pdev , 1 , 1 , PCI_IRQ_MSI );
647+ if (nvec < 0 )
648+ return nvec ;
649+
650+ INIT_WORK (& nhi -> interrupt_work , nhi_interrupt_work );
651+
652+ irq = pci_irq_vector (nhi -> pdev , 0 );
653+ if (irq < 0 )
654+ return irq ;
655+
656+ res = devm_request_irq (& pdev -> dev , irq , nhi_msi ,
657+ IRQF_NO_SUSPEND , "thunderbolt" , nhi );
658+ if (res ) {
659+ dev_err (& pdev -> dev , "request_irq failed, aborting\n" );
660+ return res ;
661+ }
662+ }
663+
664+ return 0 ;
534665}
535666
536667static int nhi_probe (struct pci_dev * pdev , const struct pci_device_id * id )
@@ -545,12 +676,6 @@ static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
545676 return res ;
546677 }
547678
548- res = pci_enable_msi (pdev );
549- if (res ) {
550- dev_err (& pdev -> dev , "cannot enable MSI, aborting\n" );
551- return res ;
552- }
553-
554679 res = pcim_iomap_regions (pdev , 1 << 0 , "thunderbolt" );
555680 if (res ) {
556681 dev_err (& pdev -> dev , "cannot obtain PCI resources, aborting\n" );
@@ -568,7 +693,6 @@ static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
568693 if (nhi -> hop_count != 12 && nhi -> hop_count != 32 )
569694 dev_warn (& pdev -> dev , "unexpected hop count: %d\n" ,
570695 nhi -> hop_count );
571- INIT_WORK (& nhi -> interrupt_work , nhi_interrupt_work );
572696
573697 nhi -> tx_rings = devm_kcalloc (& pdev -> dev , nhi -> hop_count ,
574698 sizeof (* nhi -> tx_rings ), GFP_KERNEL );
@@ -577,12 +701,9 @@ static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
577701 if (!nhi -> tx_rings || !nhi -> rx_rings )
578702 return - ENOMEM ;
579703
580- nhi_disable_interrupts (nhi ); /* In case someone left them on. */
581- res = devm_request_irq (& pdev -> dev , pdev -> irq , nhi_msi ,
582- IRQF_NO_SUSPEND , /* must work during _noirq */
583- "thunderbolt" , nhi );
704+ res = nhi_init_msi (nhi );
584705 if (res ) {
585- dev_err (& pdev -> dev , "request_irq failed , aborting\n" );
706+ dev_err (& pdev -> dev , "cannot enable MSI , aborting\n" );
586707 return res ;
587708 }
588709
0 commit comments