Lines Matching full:ring
28 #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring") argument
49 static int ring_interrupt_index(struct tb_ring *ring) in ring_interrupt_index() argument
51 int bit = ring->hop; in ring_interrupt_index()
52 if (!ring->is_tx) in ring_interrupt_index()
53 bit += ring->nhi->hop_count; in ring_interrupt_index()
58 * ring_interrupt_active() - activate/deactivate interrupts for a single ring
60 * ring->nhi->lock must be held.
62 static void ring_interrupt_active(struct tb_ring *ring, bool active) in ring_interrupt_active() argument
65 ring_interrupt_index(ring) / 32 * 4; in ring_interrupt_active()
66 int bit = ring_interrupt_index(ring) & 31; in ring_interrupt_active()
70 if (ring->irq > 0) { in ring_interrupt_active()
75 if (ring->is_tx) in ring_interrupt_active()
76 index = ring->hop; in ring_interrupt_active()
78 index = ring->hop + ring->nhi->hop_count; in ring_interrupt_active()
80 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT) { in ring_interrupt_active()
86 misc = ioread32(ring->nhi->iobase + REG_DMA_MISC); in ring_interrupt_active()
89 iowrite32(misc, ring->nhi->iobase + REG_DMA_MISC); in ring_interrupt_active()
93 ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE; in ring_interrupt_active()
99 ivr |= ring->vector << shift; in ring_interrupt_active()
103 old = ioread32(ring->nhi->iobase + reg); in ring_interrupt_active()
109 dev_dbg(&ring->nhi->pdev->dev, in ring_interrupt_active()
114 dev_WARN(&ring->nhi->pdev->dev, in ring_interrupt_active()
116 RING_TYPE(ring), ring->hop, in ring_interrupt_active()
118 iowrite32(new, ring->nhi->iobase + reg); in ring_interrupt_active()
138 /* ring helper methods */
140 static void __iomem *ring_desc_base(struct tb_ring *ring) in ring_desc_base() argument
142 void __iomem *io = ring->nhi->iobase; in ring_desc_base()
143 io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE; in ring_desc_base()
144 io += ring->hop * 16; in ring_desc_base()
148 static void __iomem *ring_options_base(struct tb_ring *ring) in ring_options_base() argument
150 void __iomem *io = ring->nhi->iobase; in ring_options_base()
151 io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE; in ring_options_base()
152 io += ring->hop * 32; in ring_options_base()
156 static void ring_iowrite_cons(struct tb_ring *ring, u16 cons) in ring_iowrite_cons() argument
163 iowrite32(cons, ring_desc_base(ring) + 8); in ring_iowrite_cons()
166 static void ring_iowrite_prod(struct tb_ring *ring, u16 prod) in ring_iowrite_prod() argument
169 iowrite32(prod << 16, ring_desc_base(ring) + 8); in ring_iowrite_prod()
172 static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset) in ring_iowrite32desc() argument
174 iowrite32(value, ring_desc_base(ring) + offset); in ring_iowrite32desc()
177 static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset) in ring_iowrite64desc() argument
179 iowrite32(value, ring_desc_base(ring) + offset); in ring_iowrite64desc()
180 iowrite32(value >> 32, ring_desc_base(ring) + offset + 4); in ring_iowrite64desc()
183 static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset) in ring_iowrite32options() argument
185 iowrite32(value, ring_options_base(ring) + offset); in ring_iowrite32options()
188 static bool ring_full(struct tb_ring *ring) in ring_full() argument
190 return ((ring->head + 1) % ring->size) == ring->tail; in ring_full()
193 static bool ring_empty(struct tb_ring *ring) in ring_empty() argument
195 return ring->head == ring->tail; in ring_empty()
199 * ring_write_descriptors() - post frames from ring->queue to the controller
201 * ring->lock is held.
203 static void ring_write_descriptors(struct tb_ring *ring) in ring_write_descriptors() argument
207 list_for_each_entry_safe(frame, n, &ring->queue, list) { in ring_write_descriptors()
208 if (ring_full(ring)) in ring_write_descriptors()
210 list_move_tail(&frame->list, &ring->in_flight); in ring_write_descriptors()
211 descriptor = &ring->descriptors[ring->head]; in ring_write_descriptors()
215 if (ring->is_tx) { in ring_write_descriptors()
220 ring->head = (ring->head + 1) % ring->size; in ring_write_descriptors()
221 if (ring->is_tx) in ring_write_descriptors()
222 ring_iowrite_prod(ring, ring->head); in ring_write_descriptors()
224 ring_iowrite_cons(ring, ring->head); in ring_write_descriptors()
231 * If the ring is shutting down then all frames are marked as canceled and
234 * Otherwise we collect all completed frame from the ring buffer, write new
235 * frame to the ring buffer and invoke the callbacks for the completed frames.
239 struct tb_ring *ring = container_of(work, typeof(*ring), work); in ring_work() local
245 spin_lock_irqsave(&ring->lock, flags); in ring_work()
247 if (!ring->running) { in ring_work()
249 list_splice_tail_init(&ring->in_flight, &done); in ring_work()
250 list_splice_tail_init(&ring->queue, &done); in ring_work()
255 while (!ring_empty(ring)) { in ring_work()
256 if (!(ring->descriptors[ring->tail].flags in ring_work()
259 frame = list_first_entry(&ring->in_flight, typeof(*frame), in ring_work()
262 if (!ring->is_tx) { in ring_work()
263 frame->size = ring->descriptors[ring->tail].length; in ring_work()
264 frame->eof = ring->descriptors[ring->tail].eof; in ring_work()
265 frame->sof = ring->descriptors[ring->tail].sof; in ring_work()
266 frame->flags = ring->descriptors[ring->tail].flags; in ring_work()
268 ring->tail = (ring->tail + 1) % ring->size; in ring_work()
270 ring_write_descriptors(ring); in ring_work()
274 spin_unlock_irqrestore(&ring->lock, flags); in ring_work()
283 frame->callback(ring, frame, canceled); in ring_work()
287 int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame) in __tb_ring_enqueue() argument
292 spin_lock_irqsave(&ring->lock, flags); in __tb_ring_enqueue()
293 if (ring->running) { in __tb_ring_enqueue()
294 list_add_tail(&frame->list, &ring->queue); in __tb_ring_enqueue()
295 ring_write_descriptors(ring); in __tb_ring_enqueue()
299 spin_unlock_irqrestore(&ring->lock, flags); in __tb_ring_enqueue()
305 * tb_ring_poll() - Poll one completed frame from the ring
306 * @ring: Ring to poll
308 * This function can be called when @start_poll callback of the @ring
309 * has been called. It will read one completed frame from the ring and
313 struct ring_frame *tb_ring_poll(struct tb_ring *ring) in tb_ring_poll() argument
318 spin_lock_irqsave(&ring->lock, flags); in tb_ring_poll()
319 if (!ring->running) in tb_ring_poll()
321 if (ring_empty(ring)) in tb_ring_poll()
324 if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) { in tb_ring_poll()
325 frame = list_first_entry(&ring->in_flight, typeof(*frame), in tb_ring_poll()
329 if (!ring->is_tx) { in tb_ring_poll()
330 frame->size = ring->descriptors[ring->tail].length; in tb_ring_poll()
331 frame->eof = ring->descriptors[ring->tail].eof; in tb_ring_poll()
332 frame->sof = ring->descriptors[ring->tail].sof; in tb_ring_poll()
333 frame->flags = ring->descriptors[ring->tail].flags; in tb_ring_poll()
336 ring->tail = (ring->tail + 1) % ring->size; in tb_ring_poll()
340 spin_unlock_irqrestore(&ring->lock, flags); in tb_ring_poll()
345 static void __ring_interrupt_mask(struct tb_ring *ring, bool mask) in __ring_interrupt_mask() argument
347 int idx = ring_interrupt_index(ring); in __ring_interrupt_mask()
352 val = ioread32(ring->nhi->iobase + reg); in __ring_interrupt_mask()
357 iowrite32(val, ring->nhi->iobase + reg); in __ring_interrupt_mask()
360 /* Both @nhi->lock and @ring->lock should be held */
361 static void __ring_interrupt(struct tb_ring *ring) in __ring_interrupt() argument
363 if (!ring->running) in __ring_interrupt()
366 if (ring->start_poll) { in __ring_interrupt()
367 __ring_interrupt_mask(ring, true); in __ring_interrupt()
368 ring->start_poll(ring->poll_data); in __ring_interrupt()
370 schedule_work(&ring->work); in __ring_interrupt()
375 * tb_ring_poll_complete() - Re-start interrupt for the ring
376 * @ring: Ring to re-start the interrupt
378 * This will re-start (unmask) the ring interrupt once the user is done
381 void tb_ring_poll_complete(struct tb_ring *ring) in tb_ring_poll_complete() argument
385 spin_lock_irqsave(&ring->nhi->lock, flags); in tb_ring_poll_complete()
386 spin_lock(&ring->lock); in tb_ring_poll_complete()
387 if (ring->start_poll) in tb_ring_poll_complete()
388 __ring_interrupt_mask(ring, false); in tb_ring_poll_complete()
389 spin_unlock(&ring->lock); in tb_ring_poll_complete()
390 spin_unlock_irqrestore(&ring->nhi->lock, flags); in tb_ring_poll_complete()
394 static void ring_clear_msix(const struct tb_ring *ring) in ring_clear_msix() argument
396 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT) in ring_clear_msix()
399 if (ring->is_tx) in ring_clear_msix()
400 ioread32(ring->nhi->iobase + REG_RING_NOTIFY_BASE); in ring_clear_msix()
402 ioread32(ring->nhi->iobase + REG_RING_NOTIFY_BASE + in ring_clear_msix()
403 4 * (ring->nhi->hop_count / 32)); in ring_clear_msix()
408 struct tb_ring *ring = data; in ring_msix() local
410 spin_lock(&ring->nhi->lock); in ring_msix()
411 ring_clear_msix(ring); in ring_msix()
412 spin_lock(&ring->lock); in ring_msix()
413 __ring_interrupt(ring); in ring_msix()
414 spin_unlock(&ring->lock); in ring_msix()
415 spin_unlock(&ring->nhi->lock); in ring_msix()
420 static int ring_request_msix(struct tb_ring *ring, bool no_suspend) in ring_request_msix() argument
422 struct tb_nhi *nhi = ring->nhi; in ring_request_msix()
433 ring->vector = ret; in ring_request_msix()
435 ret = pci_irq_vector(ring->nhi->pdev, ring->vector); in ring_request_msix()
439 ring->irq = ret; in ring_request_msix()
442 ret = request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring); in ring_request_msix()
449 ida_simple_remove(&nhi->msix_ida, ring->vector); in ring_request_msix()
454 static void ring_release_msix(struct tb_ring *ring) in ring_release_msix() argument
456 if (ring->irq <= 0) in ring_release_msix()
459 free_irq(ring->irq, ring); in ring_release_msix()
460 ida_simple_remove(&ring->nhi->msix_ida, ring->vector); in ring_release_msix()
461 ring->vector = 0; in ring_release_msix()
462 ring->irq = 0; in ring_release_msix()
465 static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring) in nhi_alloc_hop() argument
472 if (ring->flags & RING_FLAG_E2E && !ring->is_tx) { in nhi_alloc_hop()
474 ring->e2e_tx_hop, RING_E2E_RESERVED_HOPID); in nhi_alloc_hop()
475 ring->e2e_tx_hop = RING_E2E_RESERVED_HOPID; in nhi_alloc_hop()
481 if (ring->hop < 0) { in nhi_alloc_hop()
489 if (ring->is_tx) { in nhi_alloc_hop()
491 ring->hop = i; in nhi_alloc_hop()
496 ring->hop = i; in nhi_alloc_hop()
503 if (ring->hop > 0 && ring->hop < start_hop) { in nhi_alloc_hop()
504 dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop); in nhi_alloc_hop()
508 if (ring->hop < 0 || ring->hop >= nhi->hop_count) { in nhi_alloc_hop()
509 dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop); in nhi_alloc_hop()
513 if (ring->is_tx && nhi->tx_rings[ring->hop]) { in nhi_alloc_hop()
515 ring->hop); in nhi_alloc_hop()
518 } else if (!ring->is_tx && nhi->rx_rings[ring->hop]) { in nhi_alloc_hop()
520 ring->hop); in nhi_alloc_hop()
525 if (ring->is_tx) in nhi_alloc_hop()
526 nhi->tx_rings[ring->hop] = ring; in nhi_alloc_hop()
528 nhi->rx_rings[ring->hop] = ring; in nhi_alloc_hop()
542 struct tb_ring *ring = NULL; in tb_ring_alloc() local
544 dev_dbg(&nhi->pdev->dev, "allocating %s ring %d of size %d\n", in tb_ring_alloc()
547 ring = kzalloc(sizeof(*ring), GFP_KERNEL); in tb_ring_alloc()
548 if (!ring) in tb_ring_alloc()
551 spin_lock_init(&ring->lock); in tb_ring_alloc()
552 INIT_LIST_HEAD(&ring->queue); in tb_ring_alloc()
553 INIT_LIST_HEAD(&ring->in_flight); in tb_ring_alloc()
554 INIT_WORK(&ring->work, ring_work); in tb_ring_alloc()
556 ring->nhi = nhi; in tb_ring_alloc()
557 ring->hop = hop; in tb_ring_alloc()
558 ring->is_tx = transmit; in tb_ring_alloc()
559 ring->size = size; in tb_ring_alloc()
560 ring->flags = flags; in tb_ring_alloc()
561 ring->e2e_tx_hop = e2e_tx_hop; in tb_ring_alloc()
562 ring->sof_mask = sof_mask; in tb_ring_alloc()
563 ring->eof_mask = eof_mask; in tb_ring_alloc()
564 ring->head = 0; in tb_ring_alloc()
565 ring->tail = 0; in tb_ring_alloc()
566 ring->running = false; in tb_ring_alloc()
567 ring->start_poll = start_poll; in tb_ring_alloc()
568 ring->poll_data = poll_data; in tb_ring_alloc()
570 ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev, in tb_ring_alloc()
571 size * sizeof(*ring->descriptors), in tb_ring_alloc()
572 &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO); in tb_ring_alloc()
573 if (!ring->descriptors) in tb_ring_alloc()
576 if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND)) in tb_ring_alloc()
579 if (nhi_alloc_hop(nhi, ring)) in tb_ring_alloc()
582 return ring; in tb_ring_alloc()
585 ring_release_msix(ring); in tb_ring_alloc()
587 dma_free_coherent(&ring->nhi->pdev->dev, in tb_ring_alloc()
588 ring->size * sizeof(*ring->descriptors), in tb_ring_alloc()
589 ring->descriptors, ring->descriptors_dma); in tb_ring_alloc()
591 kfree(ring); in tb_ring_alloc()
597 * tb_ring_alloc_tx() - Allocate DMA ring for transmit
598 * @nhi: Pointer to the NHI the ring is to be allocated
599 * @hop: HopID (ring) to allocate
600 * @size: Number of entries in the ring
601 * @flags: Flags for the ring
611 * tb_ring_alloc_rx() - Allocate DMA ring for receive
612 * @nhi: Pointer to the NHI the ring is to be allocated
613 * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
614 * @size: Number of entries in the ring
615 * @flags: Flags for the ring
619 * @start_poll: If not %NULL the ring will call this function when an
635 * tb_ring_start() - enable a ring
636 * @ring: Ring to start
640 void tb_ring_start(struct tb_ring *ring) in tb_ring_start() argument
645 spin_lock_irq(&ring->nhi->lock); in tb_ring_start()
646 spin_lock(&ring->lock); in tb_ring_start()
647 if (ring->nhi->going_away) in tb_ring_start()
649 if (ring->running) { in tb_ring_start()
650 dev_WARN(&ring->nhi->pdev->dev, "ring already started\n"); in tb_ring_start()
653 dev_dbg(&ring->nhi->pdev->dev, "starting %s %d\n", in tb_ring_start()
654 RING_TYPE(ring), ring->hop); in tb_ring_start()
656 if (ring->flags & RING_FLAG_FRAME) { in tb_ring_start()
665 ring_iowrite64desc(ring, ring->descriptors_dma, 0); in tb_ring_start()
666 if (ring->is_tx) { in tb_ring_start()
667 ring_iowrite32desc(ring, ring->size, 12); in tb_ring_start()
668 ring_iowrite32options(ring, 0, 4); /* time releated ? */ in tb_ring_start()
669 ring_iowrite32options(ring, flags, 0); in tb_ring_start()
671 u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask; in tb_ring_start()
673 ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12); in tb_ring_start()
674 ring_iowrite32options(ring, sof_eof_mask, 4); in tb_ring_start()
675 ring_iowrite32options(ring, flags, 0); in tb_ring_start()
679 * Now that the ring valid bit is set we can configure E2E if in tb_ring_start()
680 * enabled for the ring. in tb_ring_start()
682 if (ring->flags & RING_FLAG_E2E) { in tb_ring_start()
683 if (!ring->is_tx) { in tb_ring_start()
686 hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT; in tb_ring_start()
690 dev_dbg(&ring->nhi->pdev->dev, in tb_ring_start()
692 RING_TYPE(ring), ring->hop, ring->e2e_tx_hop); in tb_ring_start()
694 dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d\n", in tb_ring_start()
695 RING_TYPE(ring), ring->hop); in tb_ring_start()
699 ring_iowrite32options(ring, flags, 0); in tb_ring_start()
702 ring_interrupt_active(ring, true); in tb_ring_start()
703 ring->running = true; in tb_ring_start()
705 spin_unlock(&ring->lock); in tb_ring_start()
706 spin_unlock_irq(&ring->nhi->lock); in tb_ring_start()
711 * tb_ring_stop() - shutdown a ring
712 * @ring: Ring to stop
716 * This method will disable the ring. Further calls to
724 void tb_ring_stop(struct tb_ring *ring) in tb_ring_stop() argument
726 spin_lock_irq(&ring->nhi->lock); in tb_ring_stop()
727 spin_lock(&ring->lock); in tb_ring_stop()
728 dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n", in tb_ring_stop()
729 RING_TYPE(ring), ring->hop); in tb_ring_stop()
730 if (ring->nhi->going_away) in tb_ring_stop()
732 if (!ring->running) { in tb_ring_stop()
733 dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n", in tb_ring_stop()
734 RING_TYPE(ring), ring->hop); in tb_ring_stop()
737 ring_interrupt_active(ring, false); in tb_ring_stop()
739 ring_iowrite32options(ring, 0, 0); in tb_ring_stop()
740 ring_iowrite64desc(ring, 0, 0); in tb_ring_stop()
741 ring_iowrite32desc(ring, 0, 8); in tb_ring_stop()
742 ring_iowrite32desc(ring, 0, 12); in tb_ring_stop()
743 ring->head = 0; in tb_ring_stop()
744 ring->tail = 0; in tb_ring_stop()
745 ring->running = false; in tb_ring_stop()
748 spin_unlock(&ring->lock); in tb_ring_stop()
749 spin_unlock_irq(&ring->nhi->lock); in tb_ring_stop()
752 * schedule ring->work to invoke callbacks on all remaining frames. in tb_ring_stop()
754 schedule_work(&ring->work); in tb_ring_stop()
755 flush_work(&ring->work); in tb_ring_stop()
760 * tb_ring_free() - free ring
762 * When this method returns all invocations of ring->callback will have
765 * Ring must be stopped.
769 void tb_ring_free(struct tb_ring *ring) in tb_ring_free() argument
771 spin_lock_irq(&ring->nhi->lock); in tb_ring_free()
773 * Dissociate the ring from the NHI. This also ensures that in tb_ring_free()
774 * nhi_interrupt_work cannot reschedule ring->work. in tb_ring_free()
776 if (ring->is_tx) in tb_ring_free()
777 ring->nhi->tx_rings[ring->hop] = NULL; in tb_ring_free()
779 ring->nhi->rx_rings[ring->hop] = NULL; in tb_ring_free()
781 if (ring->running) { in tb_ring_free()
782 dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n", in tb_ring_free()
783 RING_TYPE(ring), ring->hop); in tb_ring_free()
785 spin_unlock_irq(&ring->nhi->lock); in tb_ring_free()
787 ring_release_msix(ring); in tb_ring_free()
789 dma_free_coherent(&ring->nhi->pdev->dev, in tb_ring_free()
790 ring->size * sizeof(*ring->descriptors), in tb_ring_free()
791 ring->descriptors, ring->descriptors_dma); in tb_ring_free()
793 ring->descriptors = NULL; in tb_ring_free()
794 ring->descriptors_dma = 0; in tb_ring_free()
797 dev_dbg(&ring->nhi->pdev->dev, "freeing %s %d\n", RING_TYPE(ring), in tb_ring_free()
798 ring->hop); in tb_ring_free()
801 * ring->work can no longer be scheduled (it is scheduled only in tb_ring_free()
803 * to finish before freeing the ring. in tb_ring_free()
805 flush_work(&ring->work); in tb_ring_free()
806 kfree(ring); in tb_ring_free()
872 struct tb_ring *ring; in nhi_interrupt_work() local
894 "RX overflow for ring %d\n", in nhi_interrupt_work()
899 ring = nhi->tx_rings[hop]; in nhi_interrupt_work()
901 ring = nhi->rx_rings[hop]; in nhi_interrupt_work()
902 if (ring == NULL) { in nhi_interrupt_work()
904 "got interrupt for inactive %s ring %d\n", in nhi_interrupt_work()
910 spin_lock(&ring->lock); in nhi_interrupt_work()
911 __ring_interrupt(ring); in nhi_interrupt_work()
912 spin_unlock(&ring->lock); in nhi_interrupt_work()
1099 "TX ring %d is still active\n", i); in nhi_shutdown()
1102 "RX ring %d is still active\n", i); in nhi_shutdown()
1199 * get all MSI-X vectors and if we succeed, each ring will have in nhi_init_msi()