Lines Matching refs:ring

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