Lines Matching full:ring

26 #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")  argument
39 static int ring_interrupt_index(struct tb_ring *ring) in ring_interrupt_index() argument
41 int bit = ring->hop; in ring_interrupt_index()
42 if (!ring->is_tx) in ring_interrupt_index()
43 bit += ring->nhi->hop_count; in ring_interrupt_index()
48 * ring_interrupt_active() - activate/deactivate interrupts for a single ring
50 * ring->nhi->lock must be held.
52 static void ring_interrupt_active(struct tb_ring *ring, bool active) in ring_interrupt_active() argument
55 ring_interrupt_index(ring) / 32 * 4; in ring_interrupt_active()
56 int bit = ring_interrupt_index(ring) & 31; in ring_interrupt_active()
60 if (ring->irq > 0) { in ring_interrupt_active()
65 if (ring->is_tx) in ring_interrupt_active()
66 index = ring->hop; in ring_interrupt_active()
68 index = ring->hop + ring->nhi->hop_count; in ring_interrupt_active()
74 misc = ioread32(ring->nhi->iobase + REG_DMA_MISC); in ring_interrupt_active()
77 iowrite32(misc, ring->nhi->iobase + REG_DMA_MISC); in ring_interrupt_active()
80 ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE; in ring_interrupt_active()
86 ivr |= ring->vector << shift; in ring_interrupt_active()
90 old = ioread32(ring->nhi->iobase + reg); in ring_interrupt_active()
96 dev_dbg(&ring->nhi->pdev->dev, in ring_interrupt_active()
101 dev_WARN(&ring->nhi->pdev->dev, in ring_interrupt_active()
103 RING_TYPE(ring), ring->hop, in ring_interrupt_active()
105 iowrite32(new, ring->nhi->iobase + reg); in ring_interrupt_active()
125 /* ring helper methods */
127 static void __iomem *ring_desc_base(struct tb_ring *ring) in ring_desc_base() argument
129 void __iomem *io = ring->nhi->iobase; in ring_desc_base()
130 io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE; in ring_desc_base()
131 io += ring->hop * 16; in ring_desc_base()
135 static void __iomem *ring_options_base(struct tb_ring *ring) in ring_options_base() argument
137 void __iomem *io = ring->nhi->iobase; in ring_options_base()
138 io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE; in ring_options_base()
139 io += ring->hop * 32; in ring_options_base()
143 static void ring_iowrite_cons(struct tb_ring *ring, u16 cons) in ring_iowrite_cons() argument
150 iowrite32(cons, ring_desc_base(ring) + 8); in ring_iowrite_cons()
153 static void ring_iowrite_prod(struct tb_ring *ring, u16 prod) in ring_iowrite_prod() argument
156 iowrite32(prod << 16, ring_desc_base(ring) + 8); in ring_iowrite_prod()
159 static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset) in ring_iowrite32desc() argument
161 iowrite32(value, ring_desc_base(ring) + offset); in ring_iowrite32desc()
164 static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset) in ring_iowrite64desc() argument
166 iowrite32(value, ring_desc_base(ring) + offset); in ring_iowrite64desc()
167 iowrite32(value >> 32, ring_desc_base(ring) + offset + 4); in ring_iowrite64desc()
170 static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset) in ring_iowrite32options() argument
172 iowrite32(value, ring_options_base(ring) + offset); in ring_iowrite32options()
175 static bool ring_full(struct tb_ring *ring) in ring_full() argument
177 return ((ring->head + 1) % ring->size) == ring->tail; in ring_full()
180 static bool ring_empty(struct tb_ring *ring) in ring_empty() argument
182 return ring->head == ring->tail; in ring_empty()
186 * ring_write_descriptors() - post frames from ring->queue to the controller
188 * ring->lock is held.
190 static void ring_write_descriptors(struct tb_ring *ring) in ring_write_descriptors() argument
194 list_for_each_entry_safe(frame, n, &ring->queue, list) { in ring_write_descriptors()
195 if (ring_full(ring)) in ring_write_descriptors()
197 list_move_tail(&frame->list, &ring->in_flight); in ring_write_descriptors()
198 descriptor = &ring->descriptors[ring->head]; in ring_write_descriptors()
202 if (ring->is_tx) { in ring_write_descriptors()
207 ring->head = (ring->head + 1) % ring->size; in ring_write_descriptors()
208 if (ring->is_tx) in ring_write_descriptors()
209 ring_iowrite_prod(ring, ring->head); in ring_write_descriptors()
211 ring_iowrite_cons(ring, ring->head); in ring_write_descriptors()
218 * If the ring is shutting down then all frames are marked as canceled and
221 * Otherwise we collect all completed frame from the ring buffer, write new
222 * frame to the ring buffer and invoke the callbacks for the completed frames.
226 struct tb_ring *ring = container_of(work, typeof(*ring), work); in ring_work() local
232 spin_lock_irqsave(&ring->lock, flags); in ring_work()
234 if (!ring->running) { in ring_work()
236 list_splice_tail_init(&ring->in_flight, &done); in ring_work()
237 list_splice_tail_init(&ring->queue, &done); in ring_work()
242 while (!ring_empty(ring)) { in ring_work()
243 if (!(ring->descriptors[ring->tail].flags in ring_work()
246 frame = list_first_entry(&ring->in_flight, typeof(*frame), in ring_work()
249 if (!ring->is_tx) { in ring_work()
250 frame->size = ring->descriptors[ring->tail].length; in ring_work()
251 frame->eof = ring->descriptors[ring->tail].eof; in ring_work()
252 frame->sof = ring->descriptors[ring->tail].sof; in ring_work()
253 frame->flags = ring->descriptors[ring->tail].flags; in ring_work()
255 ring->tail = (ring->tail + 1) % ring->size; in ring_work()
257 ring_write_descriptors(ring); in ring_work()
261 spin_unlock_irqrestore(&ring->lock, flags); in ring_work()
270 frame->callback(ring, frame, canceled); in ring_work()
274 int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame) in __tb_ring_enqueue() argument
279 spin_lock_irqsave(&ring->lock, flags); in __tb_ring_enqueue()
280 if (ring->running) { in __tb_ring_enqueue()
281 list_add_tail(&frame->list, &ring->queue); in __tb_ring_enqueue()
282 ring_write_descriptors(ring); in __tb_ring_enqueue()
286 spin_unlock_irqrestore(&ring->lock, flags); in __tb_ring_enqueue()
292 * tb_ring_poll() - Poll one completed frame from the ring
293 * @ring: Ring to poll
295 * This function can be called when @start_poll callback of the @ring
296 * has been called. It will read one completed frame from the ring and
300 struct ring_frame *tb_ring_poll(struct tb_ring *ring) in tb_ring_poll() argument
305 spin_lock_irqsave(&ring->lock, flags); in tb_ring_poll()
306 if (!ring->running) in tb_ring_poll()
308 if (ring_empty(ring)) in tb_ring_poll()
311 if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) { in tb_ring_poll()
312 frame = list_first_entry(&ring->in_flight, typeof(*frame), in tb_ring_poll()
316 if (!ring->is_tx) { in tb_ring_poll()
317 frame->size = ring->descriptors[ring->tail].length; in tb_ring_poll()
318 frame->eof = ring->descriptors[ring->tail].eof; in tb_ring_poll()
319 frame->sof = ring->descriptors[ring->tail].sof; in tb_ring_poll()
320 frame->flags = ring->descriptors[ring->tail].flags; in tb_ring_poll()
323 ring->tail = (ring->tail + 1) % ring->size; in tb_ring_poll()
327 spin_unlock_irqrestore(&ring->lock, flags); in tb_ring_poll()
332 static void __ring_interrupt_mask(struct tb_ring *ring, bool mask) in __ring_interrupt_mask() argument
334 int idx = ring_interrupt_index(ring); in __ring_interrupt_mask()
339 val = ioread32(ring->nhi->iobase + reg); in __ring_interrupt_mask()
344 iowrite32(val, ring->nhi->iobase + reg); in __ring_interrupt_mask()
347 /* Both @nhi->lock and @ring->lock should be held */
348 static void __ring_interrupt(struct tb_ring *ring) in __ring_interrupt() argument
350 if (!ring->running) in __ring_interrupt()
353 if (ring->start_poll) { in __ring_interrupt()
354 __ring_interrupt_mask(ring, true); in __ring_interrupt()
355 ring->start_poll(ring->poll_data); in __ring_interrupt()
357 schedule_work(&ring->work); in __ring_interrupt()
362 * tb_ring_poll_complete() - Re-start interrupt for the ring
363 * @ring: Ring to re-start the interrupt
365 * This will re-start (unmask) the ring interrupt once the user is done
368 void tb_ring_poll_complete(struct tb_ring *ring) in tb_ring_poll_complete() argument
372 spin_lock_irqsave(&ring->nhi->lock, flags); in tb_ring_poll_complete()
373 spin_lock(&ring->lock); in tb_ring_poll_complete()
374 if (ring->start_poll) in tb_ring_poll_complete()
375 __ring_interrupt_mask(ring, false); in tb_ring_poll_complete()
376 spin_unlock(&ring->lock); in tb_ring_poll_complete()
377 spin_unlock_irqrestore(&ring->nhi->lock, flags); in tb_ring_poll_complete()
383 struct tb_ring *ring = data; in ring_msix() local
385 spin_lock(&ring->nhi->lock); in ring_msix()
386 spin_lock(&ring->lock); in ring_msix()
387 __ring_interrupt(ring); in ring_msix()
388 spin_unlock(&ring->lock); in ring_msix()
389 spin_unlock(&ring->nhi->lock); in ring_msix()
394 static int ring_request_msix(struct tb_ring *ring, bool no_suspend) in ring_request_msix() argument
396 struct tb_nhi *nhi = ring->nhi; in ring_request_msix()
407 ring->vector = ret; in ring_request_msix()
409 ret = pci_irq_vector(ring->nhi->pdev, ring->vector); in ring_request_msix()
413 ring->irq = ret; in ring_request_msix()
416 ret = request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring); in ring_request_msix()
423 ida_simple_remove(&nhi->msix_ida, ring->vector); in ring_request_msix()
428 static void ring_release_msix(struct tb_ring *ring) in ring_release_msix() argument
430 if (ring->irq <= 0) in ring_release_msix()
433 free_irq(ring->irq, ring); in ring_release_msix()
434 ida_simple_remove(&ring->nhi->msix_ida, ring->vector); in ring_release_msix()
435 ring->vector = 0; in ring_release_msix()
436 ring->irq = 0; in ring_release_msix()
439 static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring) in nhi_alloc_hop() argument
445 if (ring->hop < 0) { in nhi_alloc_hop()
453 if (ring->is_tx) { in nhi_alloc_hop()
455 ring->hop = i; in nhi_alloc_hop()
460 ring->hop = i; in nhi_alloc_hop()
467 if (ring->hop < 0 || ring->hop >= nhi->hop_count) { in nhi_alloc_hop()
468 dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop); in nhi_alloc_hop()
472 if (ring->is_tx && nhi->tx_rings[ring->hop]) { in nhi_alloc_hop()
474 ring->hop); in nhi_alloc_hop()
477 } else if (!ring->is_tx && nhi->rx_rings[ring->hop]) { in nhi_alloc_hop()
479 ring->hop); in nhi_alloc_hop()
484 if (ring->is_tx) in nhi_alloc_hop()
485 nhi->tx_rings[ring->hop] = ring; in nhi_alloc_hop()
487 nhi->rx_rings[ring->hop] = ring; in nhi_alloc_hop()
501 struct tb_ring *ring = NULL; in tb_ring_alloc() local
503 dev_dbg(&nhi->pdev->dev, "allocating %s ring %d of size %d\n", in tb_ring_alloc()
506 ring = kzalloc(sizeof(*ring), GFP_KERNEL); in tb_ring_alloc()
507 if (!ring) in tb_ring_alloc()
510 spin_lock_init(&ring->lock); in tb_ring_alloc()
511 INIT_LIST_HEAD(&ring->queue); in tb_ring_alloc()
512 INIT_LIST_HEAD(&ring->in_flight); in tb_ring_alloc()
513 INIT_WORK(&ring->work, ring_work); in tb_ring_alloc()
515 ring->nhi = nhi; in tb_ring_alloc()
516 ring->hop = hop; in tb_ring_alloc()
517 ring->is_tx = transmit; in tb_ring_alloc()
518 ring->size = size; in tb_ring_alloc()
519 ring->flags = flags; in tb_ring_alloc()
520 ring->sof_mask = sof_mask; in tb_ring_alloc()
521 ring->eof_mask = eof_mask; in tb_ring_alloc()
522 ring->head = 0; in tb_ring_alloc()
523 ring->tail = 0; in tb_ring_alloc()
524 ring->running = false; in tb_ring_alloc()
525 ring->start_poll = start_poll; in tb_ring_alloc()
526 ring->poll_data = poll_data; in tb_ring_alloc()
528 ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev, in tb_ring_alloc()
529 size * sizeof(*ring->descriptors), in tb_ring_alloc()
530 &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO); in tb_ring_alloc()
531 if (!ring->descriptors) in tb_ring_alloc()
534 if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND)) in tb_ring_alloc()
537 if (nhi_alloc_hop(nhi, ring)) in tb_ring_alloc()
540 return ring; in tb_ring_alloc()
543 ring_release_msix(ring); in tb_ring_alloc()
545 dma_free_coherent(&ring->nhi->pdev->dev, in tb_ring_alloc()
546 ring->size * sizeof(*ring->descriptors), in tb_ring_alloc()
547 ring->descriptors, ring->descriptors_dma); in tb_ring_alloc()
549 kfree(ring); in tb_ring_alloc()
555 * tb_ring_alloc_tx() - Allocate DMA ring for transmit
556 * @nhi: Pointer to the NHI the ring is to be allocated
557 * @hop: HopID (ring) to allocate
558 * @size: Number of entries in the ring
559 * @flags: Flags for the ring
569 * tb_ring_alloc_rx() - Allocate DMA ring for receive
570 * @nhi: Pointer to the NHI the ring is to be allocated
571 * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
572 * @size: Number of entries in the ring
573 * @flags: Flags for the ring
576 * @start_poll: If not %NULL the ring will call this function when an
591 * tb_ring_start() - enable a ring
595 void tb_ring_start(struct tb_ring *ring) in tb_ring_start() argument
600 spin_lock_irq(&ring->nhi->lock); in tb_ring_start()
601 spin_lock(&ring->lock); in tb_ring_start()
602 if (ring->nhi->going_away) in tb_ring_start()
604 if (ring->running) { in tb_ring_start()
605 dev_WARN(&ring->nhi->pdev->dev, "ring already started\n"); in tb_ring_start()
608 dev_dbg(&ring->nhi->pdev->dev, "starting %s %d\n", in tb_ring_start()
609 RING_TYPE(ring), ring->hop); in tb_ring_start()
611 if (ring->flags & RING_FLAG_FRAME) { in tb_ring_start()
620 ring_iowrite64desc(ring, ring->descriptors_dma, 0); in tb_ring_start()
621 if (ring->is_tx) { in tb_ring_start()
622 ring_iowrite32desc(ring, ring->size, 12); in tb_ring_start()
623 ring_iowrite32options(ring, 0, 4); /* time releated ? */ in tb_ring_start()
624 ring_iowrite32options(ring, flags, 0); in tb_ring_start()
626 u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask; in tb_ring_start()
628 ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12); in tb_ring_start()
629 ring_iowrite32options(ring, sof_eof_mask, 4); in tb_ring_start()
630 ring_iowrite32options(ring, flags, 0); in tb_ring_start()
632 ring_interrupt_active(ring, true); in tb_ring_start()
633 ring->running = true; in tb_ring_start()
635 spin_unlock(&ring->lock); in tb_ring_start()
636 spin_unlock_irq(&ring->nhi->lock); in tb_ring_start()
641 * tb_ring_stop() - shutdown a ring
645 * This method will disable the ring. Further calls to
653 void tb_ring_stop(struct tb_ring *ring) in tb_ring_stop() argument
655 spin_lock_irq(&ring->nhi->lock); in tb_ring_stop()
656 spin_lock(&ring->lock); in tb_ring_stop()
657 dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n", in tb_ring_stop()
658 RING_TYPE(ring), ring->hop); in tb_ring_stop()
659 if (ring->nhi->going_away) in tb_ring_stop()
661 if (!ring->running) { in tb_ring_stop()
662 dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n", in tb_ring_stop()
663 RING_TYPE(ring), ring->hop); in tb_ring_stop()
666 ring_interrupt_active(ring, false); in tb_ring_stop()
668 ring_iowrite32options(ring, 0, 0); in tb_ring_stop()
669 ring_iowrite64desc(ring, 0, 0); in tb_ring_stop()
670 ring_iowrite32desc(ring, 0, 8); in tb_ring_stop()
671 ring_iowrite32desc(ring, 0, 12); in tb_ring_stop()
672 ring->head = 0; in tb_ring_stop()
673 ring->tail = 0; in tb_ring_stop()
674 ring->running = false; in tb_ring_stop()
677 spin_unlock(&ring->lock); in tb_ring_stop()
678 spin_unlock_irq(&ring->nhi->lock); in tb_ring_stop()
681 * schedule ring->work to invoke callbacks on all remaining frames. in tb_ring_stop()
683 schedule_work(&ring->work); in tb_ring_stop()
684 flush_work(&ring->work); in tb_ring_stop()
689 * tb_ring_free() - free ring
691 * When this method returns all invocations of ring->callback will have
694 * Ring must be stopped.
698 void tb_ring_free(struct tb_ring *ring) in tb_ring_free() argument
700 spin_lock_irq(&ring->nhi->lock); in tb_ring_free()
702 * Dissociate the ring from the NHI. This also ensures that in tb_ring_free()
703 * nhi_interrupt_work cannot reschedule ring->work. in tb_ring_free()
705 if (ring->is_tx) in tb_ring_free()
706 ring->nhi->tx_rings[ring->hop] = NULL; in tb_ring_free()
708 ring->nhi->rx_rings[ring->hop] = NULL; in tb_ring_free()
710 if (ring->running) { in tb_ring_free()
711 dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n", in tb_ring_free()
712 RING_TYPE(ring), ring->hop); in tb_ring_free()
714 spin_unlock_irq(&ring->nhi->lock); in tb_ring_free()
716 ring_release_msix(ring); in tb_ring_free()
718 dma_free_coherent(&ring->nhi->pdev->dev, in tb_ring_free()
719 ring->size * sizeof(*ring->descriptors), in tb_ring_free()
720 ring->descriptors, ring->descriptors_dma); in tb_ring_free()
722 ring->descriptors = NULL; in tb_ring_free()
723 ring->descriptors_dma = 0; in tb_ring_free()
726 dev_dbg(&ring->nhi->pdev->dev, "freeing %s %d\n", RING_TYPE(ring), in tb_ring_free()
727 ring->hop); in tb_ring_free()
730 * ring->work can no longer be scheduled (it is scheduled only in tb_ring_free()
732 * to finish before freeing the ring. in tb_ring_free()
734 flush_work(&ring->work); in tb_ring_free()
735 kfree(ring); in tb_ring_free()
801 struct tb_ring *ring; in nhi_interrupt_work() local
823 "RX overflow for ring %d\n", in nhi_interrupt_work()
828 ring = nhi->tx_rings[hop]; in nhi_interrupt_work()
830 ring = nhi->rx_rings[hop]; in nhi_interrupt_work()
831 if (ring == NULL) { in nhi_interrupt_work()
833 "got interrupt for inactive %s ring %d\n", in nhi_interrupt_work()
839 spin_lock(&ring->lock); in nhi_interrupt_work()
840 __ring_interrupt(ring); in nhi_interrupt_work()
841 spin_unlock(&ring->lock); in nhi_interrupt_work()
1028 "TX ring %d is still active\n", i); in nhi_shutdown()
1031 "RX ring %d is still active\n", i); in nhi_shutdown()
1062 * get all MSI-X vectors and if we succeed, each ring will have in nhi_init_msi()