Lines Matching refs:ring

28 #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")
53 static int ring_interrupt_index(const struct tb_ring *ring)
55 int bit = ring->hop;
56 if (!ring->is_tx)
57 bit += ring->nhi->hop_count;
61 static void nhi_mask_interrupt(struct tb_nhi *nhi, int mask, int ring)
66 val = ioread32(nhi->iobase + REG_RING_INTERRUPT_BASE + ring);
67 iowrite32(val & ~mask, nhi->iobase + REG_RING_INTERRUPT_BASE + ring);
69 iowrite32(mask, nhi->iobase + REG_RING_INTERRUPT_MASK_CLEAR_BASE + ring);
73 static void nhi_clear_interrupt(struct tb_nhi *nhi, int ring)
76 ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + ring);
78 iowrite32(~0, nhi->iobase + REG_RING_INT_CLEAR + ring);
82 * ring_interrupt_active() - activate/deactivate interrupts for a single ring
84 * ring->nhi->lock must be held.
86 static void ring_interrupt_active(struct tb_ring *ring, bool active)
88 int index = ring_interrupt_index(ring) / 32 * 4;
90 int interrupt_bit = ring_interrupt_index(ring) & 31;
94 if (ring->irq > 0) {
100 if (ring->is_tx)
101 index = ring->hop;
103 index = ring->hop + ring->nhi->hop_count;
116 misc = ioread32(ring->nhi->iobase + REG_DMA_MISC);
117 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
123 ring->nhi->iobase + REG_DMA_MISC);
125 ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE;
131 ivr |= ring->vector << shift;
135 old = ioread32(ring->nhi->iobase + reg);
141 dev_dbg(&ring->nhi->pdev->dev,
146 dev_WARN(&ring->nhi->pdev->dev,
148 RING_TYPE(ring), ring->hop,
152 iowrite32(new, ring->nhi->iobase + reg);
154 nhi_mask_interrupt(ring->nhi, mask, index);
174 /* ring helper methods */
176 static void __iomem *ring_desc_base(struct tb_ring *ring)
178 void __iomem *io = ring->nhi->iobase;
179 io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE;
180 io += ring->hop * 16;
184 static void __iomem *ring_options_base(struct tb_ring *ring)
186 void __iomem *io = ring->nhi->iobase;
187 io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE;
188 io += ring->hop * 32;
192 static void ring_iowrite_cons(struct tb_ring *ring, u16 cons)
199 iowrite32(cons, ring_desc_base(ring) + 8);
202 static void ring_iowrite_prod(struct tb_ring *ring, u16 prod)
205 iowrite32(prod << 16, ring_desc_base(ring) + 8);
208 static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
210 iowrite32(value, ring_desc_base(ring) + offset);
213 static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
215 iowrite32(value, ring_desc_base(ring) + offset);
216 iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
219 static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
221 iowrite32(value, ring_options_base(ring) + offset);
224 static bool ring_full(struct tb_ring *ring)
226 return ((ring->head + 1) % ring->size) == ring->tail;
229 static bool ring_empty(struct tb_ring *ring)
231 return ring->head == ring->tail;
235 * ring_write_descriptors() - post frames from ring->queue to the controller
237 * ring->lock is held.
239 static void ring_write_descriptors(struct tb_ring *ring)
243 list_for_each_entry_safe(frame, n, &ring->queue, list) {
244 if (ring_full(ring))
246 list_move_tail(&frame->list, &ring->in_flight);
247 descriptor = &ring->descriptors[ring->head];
251 if (ring->is_tx) {
256 ring->head = (ring->head + 1) % ring->size;
257 if (ring->is_tx)
258 ring_iowrite_prod(ring, ring->head);
260 ring_iowrite_cons(ring, ring->head);
267 * If the ring is shutting down then all frames are marked as canceled and
270 * Otherwise we collect all completed frame from the ring buffer, write new
271 * frame to the ring buffer and invoke the callbacks for the completed frames.
275 struct tb_ring *ring = container_of(work, typeof(*ring), work);
281 spin_lock_irqsave(&ring->lock, flags);
283 if (!ring->running) {
285 list_splice_tail_init(&ring->in_flight, &done);
286 list_splice_tail_init(&ring->queue, &done);
291 while (!ring_empty(ring)) {
292 if (!(ring->descriptors[ring->tail].flags
295 frame = list_first_entry(&ring->in_flight, typeof(*frame),
298 if (!ring->is_tx) {
299 frame->size = ring->descriptors[ring->tail].length;
300 frame->eof = ring->descriptors[ring->tail].eof;
301 frame->sof = ring->descriptors[ring->tail].sof;
302 frame->flags = ring->descriptors[ring->tail].flags;
304 ring->tail = (ring->tail + 1) % ring->size;
306 ring_write_descriptors(ring);
310 spin_unlock_irqrestore(&ring->lock, flags);
319 frame->callback(ring, frame, canceled);
323 int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
328 spin_lock_irqsave(&ring->lock, flags);
329 if (ring->running) {
330 list_add_tail(&frame->list, &ring->queue);
331 ring_write_descriptors(ring);
335 spin_unlock_irqrestore(&ring->lock, flags);
341 * tb_ring_poll() - Poll one completed frame from the ring
342 * @ring: Ring to poll
344 * This function can be called when @start_poll callback of the @ring
345 * has been called. It will read one completed frame from the ring and
349 struct ring_frame *tb_ring_poll(struct tb_ring *ring)
354 spin_lock_irqsave(&ring->lock, flags);
355 if (!ring->running)
357 if (ring_empty(ring))
360 if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) {
361 frame = list_first_entry(&ring->in_flight, typeof(*frame),
365 if (!ring->is_tx) {
366 frame->size = ring->descriptors[ring->tail].length;
367 frame->eof = ring->descriptors[ring->tail].eof;
368 frame->sof = ring->descriptors[ring->tail].sof;
369 frame->flags = ring->descriptors[ring->tail].flags;
372 ring->tail = (ring->tail + 1) % ring->size;
376 spin_unlock_irqrestore(&ring->lock, flags);
381 static void __ring_interrupt_mask(struct tb_ring *ring, bool mask)
383 int idx = ring_interrupt_index(ring);
388 val = ioread32(ring->nhi->iobase + reg);
393 iowrite32(val, ring->nhi->iobase + reg);
396 /* Both @nhi->lock and @ring->lock should be held */
397 static void __ring_interrupt(struct tb_ring *ring)
399 if (!ring->running)
402 if (ring->start_poll) {
403 __ring_interrupt_mask(ring, true);
404 ring->start_poll(ring->poll_data);
406 schedule_work(&ring->work);
411 * tb_ring_poll_complete() - Re-start interrupt for the ring
412 * @ring: Ring to re-start the interrupt
414 * This will re-start (unmask) the ring interrupt once the user is done
417 void tb_ring_poll_complete(struct tb_ring *ring)
421 spin_lock_irqsave(&ring->nhi->lock, flags);
422 spin_lock(&ring->lock);
423 if (ring->start_poll)
424 __ring_interrupt_mask(ring, false);
425 spin_unlock(&ring->lock);
426 spin_unlock_irqrestore(&ring->nhi->lock, flags);
430 static void ring_clear_msix(const struct tb_ring *ring)
434 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
437 bit = ring_interrupt_index(ring) & 31;
438 if (ring->is_tx)
439 iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR);
441 iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR +
442 4 * (ring->nhi->hop_count / 32));
447 struct tb_ring *ring = data;
449 spin_lock(&ring->nhi->lock);
450 ring_clear_msix(ring);
451 spin_lock(&ring->lock);
452 __ring_interrupt(ring);
453 spin_unlock(&ring->lock);
454 spin_unlock(&ring->nhi->lock);
459 static int ring_request_msix(struct tb_ring *ring, bool no_suspend)
461 struct tb_nhi *nhi = ring->nhi;
472 ring->vector = ret;
474 ret = pci_irq_vector(ring->nhi->pdev, ring->vector);
478 ring->irq = ret;
481 ret = request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring);
488 ida_free(&nhi->msix_ida, ring->vector);
493 static void ring_release_msix(struct tb_ring *ring)
495 if (ring->irq <= 0)
498 free_irq(ring->irq, ring);
499 ida_free(&ring->nhi->msix_ida, ring->vector);
500 ring->vector = 0;
501 ring->irq = 0;
504 static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring)
511 if (ring->flags & RING_FLAG_E2E && !ring->is_tx) {
513 ring->e2e_tx_hop, RING_E2E_RESERVED_HOPID);
514 ring->e2e_tx_hop = RING_E2E_RESERVED_HOPID;
520 if (ring->hop < 0) {
528 if (ring->is_tx) {
530 ring->hop = i;
535 ring->hop = i;
542 if (ring->hop > 0 && ring->hop < start_hop) {
543 dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
547 if (ring->hop < 0 || ring->hop >= nhi->hop_count) {
548 dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
552 if (ring->is_tx && nhi->tx_rings[ring->hop]) {
554 ring->hop);
558 if (!ring->is_tx && nhi->rx_rings[ring->hop]) {
560 ring->hop);
565 if (ring->is_tx)
566 nhi->tx_rings[ring->hop] = ring;
568 nhi->rx_rings[ring->hop] = ring;
582 struct tb_ring *ring = NULL;
584 dev_dbg(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
587 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
588 if (!ring)
591 spin_lock_init(&ring->lock);
592 INIT_LIST_HEAD(&ring->queue);
593 INIT_LIST_HEAD(&ring->in_flight);
594 INIT_WORK(&ring->work, ring_work);
596 ring->nhi = nhi;
597 ring->hop = hop;
598 ring->is_tx = transmit;
599 ring->size = size;
600 ring->flags = flags;
601 ring->e2e_tx_hop = e2e_tx_hop;
602 ring->sof_mask = sof_mask;
603 ring->eof_mask = eof_mask;
604 ring->head = 0;
605 ring->tail = 0;
606 ring->running = false;
607 ring->start_poll = start_poll;
608 ring->poll_data = poll_data;
610 ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
611 size * sizeof(*ring->descriptors),
612 &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
613 if (!ring->descriptors)
616 if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND))
619 if (nhi_alloc_hop(nhi, ring))
622 return ring;
625 ring_release_msix(ring);
627 dma_free_coherent(&ring->nhi->pdev->dev,
628 ring->size * sizeof(*ring->descriptors),
629 ring->descriptors, ring->descriptors_dma);
631 kfree(ring);
637 * tb_ring_alloc_tx() - Allocate DMA ring for transmit
638 * @nhi: Pointer to the NHI the ring is to be allocated
639 * @hop: HopID (ring) to allocate
640 * @size: Number of entries in the ring
641 * @flags: Flags for the ring
651 * tb_ring_alloc_rx() - Allocate DMA ring for receive
652 * @nhi: Pointer to the NHI the ring is to be allocated
653 * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
654 * @size: Number of entries in the ring
655 * @flags: Flags for the ring
659 * @start_poll: If not %NULL the ring will call this function when an
675 * tb_ring_start() - enable a ring
676 * @ring: Ring to start
680 void tb_ring_start(struct tb_ring *ring)
685 spin_lock_irq(&ring->nhi->lock);
686 spin_lock(&ring->lock);
687 if (ring->nhi->going_away)
689 if (ring->running) {
690 dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
693 dev_dbg(&ring->nhi->pdev->dev, "starting %s %d\n",
694 RING_TYPE(ring), ring->hop);
696 if (ring->flags & RING_FLAG_FRAME) {
705 ring_iowrite64desc(ring, ring->descriptors_dma, 0);
706 if (ring->is_tx) {
707 ring_iowrite32desc(ring, ring->size, 12);
708 ring_iowrite32options(ring, 0, 4); /* time releated ? */
709 ring_iowrite32options(ring, flags, 0);
711 u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask;
713 ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12);
714 ring_iowrite32options(ring, sof_eof_mask, 4);
715 ring_iowrite32options(ring, flags, 0);
719 * Now that the ring valid bit is set we can configure E2E if
720 * enabled for the ring.
722 if (ring->flags & RING_FLAG_E2E) {
723 if (!ring->is_tx) {
726 hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT;
730 dev_dbg(&ring->nhi->pdev->dev,
732 RING_TYPE(ring), ring->hop, ring->e2e_tx_hop);
734 dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d\n",
735 RING_TYPE(ring), ring->hop);
739 ring_iowrite32options(ring, flags, 0);
742 ring_interrupt_active(ring, true);
743 ring->running = true;
745 spin_unlock(&ring->lock);
746 spin_unlock_irq(&ring->nhi->lock);
751 * tb_ring_stop() - shutdown a ring
752 * @ring: Ring to stop
756 * This method will disable the ring. Further calls to
764 void tb_ring_stop(struct tb_ring *ring)
766 spin_lock_irq(&ring->nhi->lock);
767 spin_lock(&ring->lock);
768 dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n",
769 RING_TYPE(ring), ring->hop);
770 if (ring->nhi->going_away)
772 if (!ring->running) {
773 dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
774 RING_TYPE(ring), ring->hop);
777 ring_interrupt_active(ring, false);
779 ring_iowrite32options(ring, 0, 0);
780 ring_iowrite64desc(ring, 0, 0);
781 ring_iowrite32desc(ring, 0, 8);
782 ring_iowrite32desc(ring, 0, 12);
783 ring->head = 0;
784 ring->tail = 0;
785 ring->running = false;
788 spin_unlock(&ring->lock);
789 spin_unlock_irq(&ring->nhi->lock);
792 * schedule ring->work to invoke callbacks on all remaining frames.
794 schedule_work(&ring->work);
795 flush_work(&ring->work);
800 * tb_ring_free() - free ring
802 * When this method returns all invocations of ring->callback will have
809 void tb_ring_free(struct tb_ring *ring)
811 spin_lock_irq(&ring->nhi->lock);
813 * Dissociate the ring from the NHI. This also ensures that
814 * nhi_interrupt_work cannot reschedule ring->work.
816 if (ring->is_tx)
817 ring->nhi->tx_rings[ring->hop] = NULL;
819 ring->nhi->rx_rings[ring->hop] = NULL;
821 if (ring->running) {
822 dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
823 RING_TYPE(ring), ring->hop);
825 spin_unlock_irq(&ring->nhi->lock);
827 ring_release_msix(ring);
829 dma_free_coherent(&ring->nhi->pdev->dev,
830 ring->size * sizeof(*ring->descriptors),
831 ring->descriptors, ring->descriptors_dma);
833 ring->descriptors = NULL;
834 ring->descriptors_dma = 0;
837 dev_dbg(&ring->nhi->pdev->dev, "freeing %s %d\n", RING_TYPE(ring),
838 ring->hop);
841 * ring->work can no longer be scheduled (it is scheduled only
843 * to finish before freeing the ring.
845 flush_work(&ring->work);
846 kfree(ring);
912 struct tb_ring *ring;
934 "RX overflow for ring %d\n",
939 ring = nhi->tx_rings[hop];
941 ring = nhi->rx_rings[hop];
942 if (ring == NULL) {
944 "got interrupt for inactive %s ring %d\n",
950 spin_lock(&ring->lock);
951 __ring_interrupt(ring);
952 spin_unlock(&ring->lock);
1139 "TX ring %d is still active\n", i);
1142 "RX ring %d is still active\n", i);
1270 * get all MSI-X vectors and if we succeed, each ring will have