Lines Matching full:ring
29 #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring") argument
54 static int ring_interrupt_index(const struct tb_ring *ring) in ring_interrupt_index() argument
56 int bit = ring->hop; in ring_interrupt_index()
57 if (!ring->is_tx) in ring_interrupt_index()
58 bit += ring->nhi->hop_count; in ring_interrupt_index()
62 static void nhi_mask_interrupt(struct tb_nhi *nhi, int mask, int ring) in nhi_mask_interrupt() argument
67 val = ioread32(nhi->iobase + REG_RING_INTERRUPT_BASE + ring); in nhi_mask_interrupt()
68 iowrite32(val & ~mask, nhi->iobase + REG_RING_INTERRUPT_BASE + ring); in nhi_mask_interrupt()
70 iowrite32(mask, nhi->iobase + REG_RING_INTERRUPT_MASK_CLEAR_BASE + ring); in nhi_mask_interrupt()
74 static void nhi_clear_interrupt(struct tb_nhi *nhi, int ring) in nhi_clear_interrupt() argument
77 ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + ring); in nhi_clear_interrupt()
79 iowrite32(~0, nhi->iobase + REG_RING_INT_CLEAR + ring); in nhi_clear_interrupt()
83 * ring_interrupt_active() - activate/deactivate interrupts for a single ring
85 * ring->nhi->lock must be held.
87 static void ring_interrupt_active(struct tb_ring *ring, bool active) in ring_interrupt_active() argument
89 int index = ring_interrupt_index(ring) / 32 * 4; in ring_interrupt_active()
91 int interrupt_bit = ring_interrupt_index(ring) & 31; in ring_interrupt_active()
95 if (ring->irq > 0) { in ring_interrupt_active()
101 if (ring->is_tx) in ring_interrupt_active()
102 index = ring->hop; in ring_interrupt_active()
104 index = ring->hop + ring->nhi->hop_count; in ring_interrupt_active()
117 misc = ioread32(ring->nhi->iobase + REG_DMA_MISC); in ring_interrupt_active()
118 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT) in ring_interrupt_active()
124 ring->nhi->iobase + REG_DMA_MISC); in ring_interrupt_active()
126 ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE; in ring_interrupt_active()
132 ivr |= ring->vector << shift; in ring_interrupt_active()
136 old = ioread32(ring->nhi->iobase + reg); in ring_interrupt_active()
142 dev_dbg(&ring->nhi->pdev->dev, in ring_interrupt_active()
147 dev_WARN(&ring->nhi->pdev->dev, in ring_interrupt_active()
149 RING_TYPE(ring), ring->hop, in ring_interrupt_active()
153 iowrite32(new, ring->nhi->iobase + reg); in ring_interrupt_active()
155 nhi_mask_interrupt(ring->nhi, mask, index); in ring_interrupt_active()
175 /* ring helper methods */
177 static void __iomem *ring_desc_base(struct tb_ring *ring) in ring_desc_base() argument
179 void __iomem *io = ring->nhi->iobase; in ring_desc_base()
180 io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE; in ring_desc_base()
181 io += ring->hop * 16; in ring_desc_base()
185 static void __iomem *ring_options_base(struct tb_ring *ring) in ring_options_base() argument
187 void __iomem *io = ring->nhi->iobase; in ring_options_base()
188 io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE; in ring_options_base()
189 io += ring->hop * 32; in ring_options_base()
193 static void ring_iowrite_cons(struct tb_ring *ring, u16 cons) in ring_iowrite_cons() argument
200 iowrite32(cons, ring_desc_base(ring) + 8); in ring_iowrite_cons()
203 static void ring_iowrite_prod(struct tb_ring *ring, u16 prod) in ring_iowrite_prod() argument
206 iowrite32(prod << 16, ring_desc_base(ring) + 8); in ring_iowrite_prod()
209 static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset) in ring_iowrite32desc() argument
211 iowrite32(value, ring_desc_base(ring) + offset); in ring_iowrite32desc()
214 static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset) in ring_iowrite64desc() argument
216 iowrite32(value, ring_desc_base(ring) + offset); in ring_iowrite64desc()
217 iowrite32(value >> 32, ring_desc_base(ring) + offset + 4); in ring_iowrite64desc()
220 static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset) in ring_iowrite32options() argument
222 iowrite32(value, ring_options_base(ring) + offset); in ring_iowrite32options()
225 static bool ring_full(struct tb_ring *ring) in ring_full() argument
227 return ((ring->head + 1) % ring->size) == ring->tail; in ring_full()
230 static bool ring_empty(struct tb_ring *ring) in ring_empty() argument
232 return ring->head == ring->tail; in ring_empty()
236 * ring_write_descriptors() - post frames from ring->queue to the controller
238 * ring->lock is held.
240 static void ring_write_descriptors(struct tb_ring *ring) in ring_write_descriptors() argument
244 list_for_each_entry_safe(frame, n, &ring->queue, list) { in ring_write_descriptors()
245 if (ring_full(ring)) in ring_write_descriptors()
247 list_move_tail(&frame->list, &ring->in_flight); in ring_write_descriptors()
248 descriptor = &ring->descriptors[ring->head]; in ring_write_descriptors()
252 if (ring->is_tx) { in ring_write_descriptors()
257 ring->head = (ring->head + 1) % ring->size; in ring_write_descriptors()
258 if (ring->is_tx) in ring_write_descriptors()
259 ring_iowrite_prod(ring, ring->head); in ring_write_descriptors()
261 ring_iowrite_cons(ring, ring->head); in ring_write_descriptors()
268 * If the ring is shutting down then all frames are marked as canceled and
271 * Otherwise we collect all completed frame from the ring buffer, write new
272 * frame to the ring buffer and invoke the callbacks for the completed frames.
276 struct tb_ring *ring = container_of(work, typeof(*ring), work); in ring_work() local
282 spin_lock_irqsave(&ring->lock, flags); in ring_work()
284 if (!ring->running) { in ring_work()
286 list_splice_tail_init(&ring->in_flight, &done); in ring_work()
287 list_splice_tail_init(&ring->queue, &done); in ring_work()
292 while (!ring_empty(ring)) { in ring_work()
293 if (!(ring->descriptors[ring->tail].flags in ring_work()
296 frame = list_first_entry(&ring->in_flight, typeof(*frame), in ring_work()
299 if (!ring->is_tx) { in ring_work()
300 frame->size = ring->descriptors[ring->tail].length; in ring_work()
301 frame->eof = ring->descriptors[ring->tail].eof; in ring_work()
302 frame->sof = ring->descriptors[ring->tail].sof; in ring_work()
303 frame->flags = ring->descriptors[ring->tail].flags; in ring_work()
305 ring->tail = (ring->tail + 1) % ring->size; in ring_work()
307 ring_write_descriptors(ring); in ring_work()
311 spin_unlock_irqrestore(&ring->lock, flags); in ring_work()
320 frame->callback(ring, frame, canceled); in ring_work()
324 int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame) in __tb_ring_enqueue() argument
329 spin_lock_irqsave(&ring->lock, flags); in __tb_ring_enqueue()
330 if (ring->running) { in __tb_ring_enqueue()
331 list_add_tail(&frame->list, &ring->queue); in __tb_ring_enqueue()
332 ring_write_descriptors(ring); in __tb_ring_enqueue()
336 spin_unlock_irqrestore(&ring->lock, flags); in __tb_ring_enqueue()
342 * tb_ring_poll() - Poll one completed frame from the ring
343 * @ring: Ring to poll
345 * This function can be called when @start_poll callback of the @ring
346 * has been called. It will read one completed frame from the ring and
352 struct ring_frame *tb_ring_poll(struct tb_ring *ring) in tb_ring_poll() argument
357 spin_lock_irqsave(&ring->lock, flags); in tb_ring_poll()
358 if (!ring->running) in tb_ring_poll()
360 if (ring_empty(ring)) in tb_ring_poll()
363 if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) { in tb_ring_poll()
364 frame = list_first_entry(&ring->in_flight, typeof(*frame), in tb_ring_poll()
368 if (!ring->is_tx) { in tb_ring_poll()
369 frame->size = ring->descriptors[ring->tail].length; in tb_ring_poll()
370 frame->eof = ring->descriptors[ring->tail].eof; in tb_ring_poll()
371 frame->sof = ring->descriptors[ring->tail].sof; in tb_ring_poll()
372 frame->flags = ring->descriptors[ring->tail].flags; in tb_ring_poll()
375 ring->tail = (ring->tail + 1) % ring->size; in tb_ring_poll()
379 spin_unlock_irqrestore(&ring->lock, flags); in tb_ring_poll()
384 static void __ring_interrupt_mask(struct tb_ring *ring, bool mask) in __ring_interrupt_mask() argument
386 int idx = ring_interrupt_index(ring); in __ring_interrupt_mask()
391 val = ioread32(ring->nhi->iobase + reg); in __ring_interrupt_mask()
396 iowrite32(val, ring->nhi->iobase + reg); in __ring_interrupt_mask()
399 /* Both @nhi->lock and @ring->lock should be held */
400 static void __ring_interrupt(struct tb_ring *ring) in __ring_interrupt() argument
402 if (!ring->running) in __ring_interrupt()
405 if (ring->start_poll) { in __ring_interrupt()
406 __ring_interrupt_mask(ring, true); in __ring_interrupt()
407 ring->start_poll(ring->poll_data); in __ring_interrupt()
409 schedule_work(&ring->work); in __ring_interrupt()
414 * tb_ring_poll_complete() - Re-start interrupt for the ring
415 * @ring: Ring to re-start the interrupt
417 * This will re-start (unmask) the ring interrupt once the user is done
420 void tb_ring_poll_complete(struct tb_ring *ring) in tb_ring_poll_complete() argument
424 spin_lock_irqsave(&ring->nhi->lock, flags); in tb_ring_poll_complete()
425 spin_lock(&ring->lock); in tb_ring_poll_complete()
426 if (ring->start_poll) in tb_ring_poll_complete()
427 __ring_interrupt_mask(ring, false); in tb_ring_poll_complete()
428 spin_unlock(&ring->lock); in tb_ring_poll_complete()
429 spin_unlock_irqrestore(&ring->nhi->lock, flags); in tb_ring_poll_complete()
433 static void ring_clear_msix(const struct tb_ring *ring) in ring_clear_msix() argument
437 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT) in ring_clear_msix()
440 bit = ring_interrupt_index(ring) & 31; in ring_clear_msix()
441 if (ring->is_tx) in ring_clear_msix()
442 iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR); in ring_clear_msix()
444 iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR + in ring_clear_msix()
445 4 * (ring->nhi->hop_count / 32)); in ring_clear_msix()
450 struct tb_ring *ring = data; in ring_msix() local
452 spin_lock(&ring->nhi->lock); in ring_msix()
453 ring_clear_msix(ring); in ring_msix()
454 spin_lock(&ring->lock); in ring_msix()
455 __ring_interrupt(ring); in ring_msix()
456 spin_unlock(&ring->lock); in ring_msix()
457 spin_unlock(&ring->nhi->lock); in ring_msix()
462 static int ring_request_msix(struct tb_ring *ring, bool no_suspend) in ring_request_msix() argument
464 struct tb_nhi *nhi = ring->nhi; in ring_request_msix()
475 ring->vector = ret; in ring_request_msix()
477 ret = pci_irq_vector(ring->nhi->pdev, ring->vector); in ring_request_msix()
481 ring->irq = ret; in ring_request_msix()
484 ret = request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring); in ring_request_msix()
491 ida_free(&nhi->msix_ida, ring->vector); in ring_request_msix()
496 static void ring_release_msix(struct tb_ring *ring) in ring_release_msix() argument
498 if (ring->irq <= 0) in ring_release_msix()
501 free_irq(ring->irq, ring); in ring_release_msix()
502 ida_free(&ring->nhi->msix_ida, ring->vector); in ring_release_msix()
503 ring->vector = 0; in ring_release_msix()
504 ring->irq = 0; in ring_release_msix()
507 static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring) in nhi_alloc_hop() argument
514 if (ring->flags & RING_FLAG_E2E && !ring->is_tx) { in nhi_alloc_hop()
516 ring->e2e_tx_hop, RING_E2E_RESERVED_HOPID); in nhi_alloc_hop()
517 ring->e2e_tx_hop = RING_E2E_RESERVED_HOPID; in nhi_alloc_hop()
523 if (ring->hop < 0) { in nhi_alloc_hop()
531 if (ring->is_tx) { in nhi_alloc_hop()
533 ring->hop = i; in nhi_alloc_hop()
538 ring->hop = i; in nhi_alloc_hop()
545 if (ring->hop > 0 && ring->hop < start_hop) { in nhi_alloc_hop()
546 dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop); in nhi_alloc_hop()
550 if (ring->hop < 0 || ring->hop >= nhi->hop_count) { in nhi_alloc_hop()
551 dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop); in nhi_alloc_hop()
555 if (ring->is_tx && nhi->tx_rings[ring->hop]) { in nhi_alloc_hop()
557 ring->hop); in nhi_alloc_hop()
561 if (!ring->is_tx && nhi->rx_rings[ring->hop]) { in nhi_alloc_hop()
563 ring->hop); in nhi_alloc_hop()
568 if (ring->is_tx) in nhi_alloc_hop()
569 nhi->tx_rings[ring->hop] = ring; in nhi_alloc_hop()
571 nhi->rx_rings[ring->hop] = ring; in nhi_alloc_hop()
585 struct tb_ring *ring = NULL; in tb_ring_alloc() local
587 dev_dbg(&nhi->pdev->dev, "allocating %s ring %d of size %d\n", in tb_ring_alloc()
590 ring = kzalloc(sizeof(*ring), GFP_KERNEL); in tb_ring_alloc()
591 if (!ring) in tb_ring_alloc()
594 spin_lock_init(&ring->lock); in tb_ring_alloc()
595 INIT_LIST_HEAD(&ring->queue); in tb_ring_alloc()
596 INIT_LIST_HEAD(&ring->in_flight); in tb_ring_alloc()
597 INIT_WORK(&ring->work, ring_work); in tb_ring_alloc()
599 ring->nhi = nhi; in tb_ring_alloc()
600 ring->hop = hop; in tb_ring_alloc()
601 ring->is_tx = transmit; in tb_ring_alloc()
602 ring->size = size; in tb_ring_alloc()
603 ring->flags = flags; in tb_ring_alloc()
604 ring->e2e_tx_hop = e2e_tx_hop; in tb_ring_alloc()
605 ring->sof_mask = sof_mask; in tb_ring_alloc()
606 ring->eof_mask = eof_mask; in tb_ring_alloc()
607 ring->head = 0; in tb_ring_alloc()
608 ring->tail = 0; in tb_ring_alloc()
609 ring->running = false; in tb_ring_alloc()
610 ring->start_poll = start_poll; in tb_ring_alloc()
611 ring->poll_data = poll_data; in tb_ring_alloc()
613 ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev, in tb_ring_alloc()
614 size * sizeof(*ring->descriptors), in tb_ring_alloc()
615 &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO); in tb_ring_alloc()
616 if (!ring->descriptors) in tb_ring_alloc()
619 if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND)) in tb_ring_alloc()
622 if (nhi_alloc_hop(nhi, ring)) in tb_ring_alloc()
625 return ring; in tb_ring_alloc()
628 ring_release_msix(ring); in tb_ring_alloc()
630 dma_free_coherent(&ring->nhi->pdev->dev, in tb_ring_alloc()
631 ring->size * sizeof(*ring->descriptors), in tb_ring_alloc()
632 ring->descriptors, ring->descriptors_dma); in tb_ring_alloc()
634 kfree(ring); in tb_ring_alloc()
640 * tb_ring_alloc_tx() - Allocate DMA ring for transmit
641 * @nhi: Pointer to the NHI the ring is to be allocated
642 * @hop: HopID (ring) to allocate
643 * @size: Number of entries in the ring
644 * @flags: Flags for the ring
656 * tb_ring_alloc_rx() - Allocate DMA ring for receive
657 * @nhi: Pointer to the NHI the ring is to be allocated
658 * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
659 * @size: Number of entries in the ring
660 * @flags: Flags for the ring
664 * @start_poll: If not %NULL the ring will call this function when an
682 * tb_ring_start() - enable a ring
683 * @ring: Ring to start
687 void tb_ring_start(struct tb_ring *ring) in tb_ring_start() argument
692 spin_lock_irq(&ring->nhi->lock); in tb_ring_start()
693 spin_lock(&ring->lock); in tb_ring_start()
694 if (ring->nhi->going_away) in tb_ring_start()
696 if (ring->running) { in tb_ring_start()
697 dev_WARN(&ring->nhi->pdev->dev, "ring already started\n"); in tb_ring_start()
700 dev_dbg(&ring->nhi->pdev->dev, "starting %s %d\n", in tb_ring_start()
701 RING_TYPE(ring), ring->hop); in tb_ring_start()
703 if (ring->flags & RING_FLAG_FRAME) { in tb_ring_start()
712 ring_iowrite64desc(ring, ring->descriptors_dma, 0); in tb_ring_start()
713 if (ring->is_tx) { in tb_ring_start()
714 ring_iowrite32desc(ring, ring->size, 12); in tb_ring_start()
715 ring_iowrite32options(ring, 0, 4); in tb_ring_start()
716 ring_iowrite32options(ring, flags, 0); in tb_ring_start()
718 u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask; in tb_ring_start()
720 ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12); in tb_ring_start()
721 ring_iowrite32options(ring, sof_eof_mask, 4); in tb_ring_start()
722 ring_iowrite32options(ring, flags, 0); in tb_ring_start()
726 * Now that the ring valid bit is set we can configure E2E if in tb_ring_start()
727 * enabled for the ring. in tb_ring_start()
729 if (ring->flags & RING_FLAG_E2E) { in tb_ring_start()
730 if (!ring->is_tx) { in tb_ring_start()
733 hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT; in tb_ring_start()
737 dev_dbg(&ring->nhi->pdev->dev, in tb_ring_start()
739 RING_TYPE(ring), ring->hop, ring->e2e_tx_hop); in tb_ring_start()
741 dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d\n", in tb_ring_start()
742 RING_TYPE(ring), ring->hop); in tb_ring_start()
746 ring_iowrite32options(ring, flags, 0); in tb_ring_start()
749 ring_interrupt_active(ring, true); in tb_ring_start()
750 ring->running = true; in tb_ring_start()
752 spin_unlock(&ring->lock); in tb_ring_start()
753 spin_unlock_irq(&ring->nhi->lock); in tb_ring_start()
758 * tb_ring_stop() - shutdown a ring
759 * @ring: Ring to stop
763 * This method will disable the ring. Further calls to
771 void tb_ring_stop(struct tb_ring *ring) in tb_ring_stop() argument
773 spin_lock_irq(&ring->nhi->lock); in tb_ring_stop()
774 spin_lock(&ring->lock); in tb_ring_stop()
775 dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n", in tb_ring_stop()
776 RING_TYPE(ring), ring->hop); in tb_ring_stop()
777 if (ring->nhi->going_away) in tb_ring_stop()
779 if (!ring->running) { in tb_ring_stop()
780 dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n", in tb_ring_stop()
781 RING_TYPE(ring), ring->hop); in tb_ring_stop()
784 ring_interrupt_active(ring, false); in tb_ring_stop()
786 ring_iowrite32options(ring, 0, 0); in tb_ring_stop()
787 ring_iowrite64desc(ring, 0, 0); in tb_ring_stop()
788 ring_iowrite32desc(ring, 0, 8); in tb_ring_stop()
789 ring_iowrite32desc(ring, 0, 12); in tb_ring_stop()
790 ring->head = 0; in tb_ring_stop()
791 ring->tail = 0; in tb_ring_stop()
792 ring->running = false; in tb_ring_stop()
795 spin_unlock(&ring->lock); in tb_ring_stop()
796 spin_unlock_irq(&ring->nhi->lock); in tb_ring_stop()
799 * schedule ring->work to invoke callbacks on all remaining frames. in tb_ring_stop()
801 schedule_work(&ring->work); in tb_ring_stop()
802 flush_work(&ring->work); in tb_ring_stop()
807 * tb_ring_free() - free ring
809 * When this method returns all invocations of ring->callback will have
812 * Ring must be stopped.
816 void tb_ring_free(struct tb_ring *ring) in tb_ring_free() argument
818 spin_lock_irq(&ring->nhi->lock); in tb_ring_free()
820 * Dissociate the ring from the NHI. This also ensures that in tb_ring_free()
821 * nhi_interrupt_work cannot reschedule ring->work. in tb_ring_free()
823 if (ring->is_tx) in tb_ring_free()
824 ring->nhi->tx_rings[ring->hop] = NULL; in tb_ring_free()
826 ring->nhi->rx_rings[ring->hop] = NULL; in tb_ring_free()
828 if (ring->running) { in tb_ring_free()
829 dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n", in tb_ring_free()
830 RING_TYPE(ring), ring->hop); in tb_ring_free()
832 spin_unlock_irq(&ring->nhi->lock); in tb_ring_free()
834 ring_release_msix(ring); in tb_ring_free()
836 dma_free_coherent(&ring->nhi->pdev->dev, in tb_ring_free()
837 ring->size * sizeof(*ring->descriptors), in tb_ring_free()
838 ring->descriptors, ring->descriptors_dma); in tb_ring_free()
840 ring->descriptors = NULL; in tb_ring_free()
841 ring->descriptors_dma = 0; in tb_ring_free()
844 dev_dbg(&ring->nhi->pdev->dev, "freeing %s %d\n", RING_TYPE(ring), in tb_ring_free()
845 ring->hop); in tb_ring_free()
848 * ring->work can no longer be scheduled (it is scheduled only in tb_ring_free()
850 * to finish before freeing the ring. in tb_ring_free()
852 flush_work(&ring->work); in tb_ring_free()
853 kfree(ring); in tb_ring_free()
922 struct tb_ring *ring; in nhi_interrupt_work() local
944 "RX overflow for ring %d\n", in nhi_interrupt_work()
949 ring = nhi->tx_rings[hop]; in nhi_interrupt_work()
951 ring = nhi->rx_rings[hop]; in nhi_interrupt_work()
952 if (ring == NULL) { in nhi_interrupt_work()
954 "got interrupt for inactive %s ring %d\n", in nhi_interrupt_work()
960 spin_lock(&ring->lock); in nhi_interrupt_work()
961 __ring_interrupt(ring); in nhi_interrupt_work()
962 spin_unlock(&ring->lock); in nhi_interrupt_work()
1149 "TX ring %d is still active\n", i); in nhi_shutdown()
1152 "RX ring %d is still active\n", i); in nhi_shutdown()
1280 * get all MSI-X vectors and if we succeed, each ring will have in nhi_init_msi()