• Home
  • Raw
  • Download

Lines Matching full:ring

22 #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")  argument
26 * use this ring for anything else.
41 static int ring_interrupt_index(struct tb_ring *ring) in ring_interrupt_index() argument
43 int bit = ring->hop; in ring_interrupt_index()
44 if (!ring->is_tx) in ring_interrupt_index()
45 bit += ring->nhi->hop_count; in ring_interrupt_index()
50 * ring_interrupt_active() - activate/deactivate interrupts for a single ring
52 * ring->nhi->lock must be held.
54 static void ring_interrupt_active(struct tb_ring *ring, bool active) in ring_interrupt_active() argument
57 ring_interrupt_index(ring) / 32 * 4; in ring_interrupt_active()
58 int bit = ring_interrupt_index(ring) & 31; in ring_interrupt_active()
62 if (ring->irq > 0) { in ring_interrupt_active()
67 if (ring->is_tx) in ring_interrupt_active()
68 index = ring->hop; in ring_interrupt_active()
70 index = ring->hop + ring->nhi->hop_count; in ring_interrupt_active()
76 misc = ioread32(ring->nhi->iobase + REG_DMA_MISC); in ring_interrupt_active()
79 iowrite32(misc, ring->nhi->iobase + REG_DMA_MISC); in ring_interrupt_active()
82 ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE; in ring_interrupt_active()
88 ivr |= ring->vector << shift; in ring_interrupt_active()
92 old = ioread32(ring->nhi->iobase + reg); in ring_interrupt_active()
98 dev_info(&ring->nhi->pdev->dev, in ring_interrupt_active()
103 dev_WARN(&ring->nhi->pdev->dev, in ring_interrupt_active()
105 RING_TYPE(ring), ring->hop, in ring_interrupt_active()
107 iowrite32(new, ring->nhi->iobase + reg); in ring_interrupt_active()
127 /* ring helper methods */
129 static void __iomem *ring_desc_base(struct tb_ring *ring) in ring_desc_base() argument
131 void __iomem *io = ring->nhi->iobase; in ring_desc_base()
132 io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE; in ring_desc_base()
133 io += ring->hop * 16; in ring_desc_base()
137 static void __iomem *ring_options_base(struct tb_ring *ring) in ring_options_base() argument
139 void __iomem *io = ring->nhi->iobase; in ring_options_base()
140 io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE; in ring_options_base()
141 io += ring->hop * 32; in ring_options_base()
145 static void ring_iowrite_cons(struct tb_ring *ring, u16 cons) in ring_iowrite_cons() argument
152 iowrite32(cons, ring_desc_base(ring) + 8); in ring_iowrite_cons()
155 static void ring_iowrite_prod(struct tb_ring *ring, u16 prod) in ring_iowrite_prod() argument
158 iowrite32(prod << 16, ring_desc_base(ring) + 8); in ring_iowrite_prod()
161 static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset) in ring_iowrite32desc() argument
163 iowrite32(value, ring_desc_base(ring) + offset); in ring_iowrite32desc()
166 static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset) in ring_iowrite64desc() argument
168 iowrite32(value, ring_desc_base(ring) + offset); in ring_iowrite64desc()
169 iowrite32(value >> 32, ring_desc_base(ring) + offset + 4); in ring_iowrite64desc()
172 static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset) in ring_iowrite32options() argument
174 iowrite32(value, ring_options_base(ring) + offset); in ring_iowrite32options()
177 static bool ring_full(struct tb_ring *ring) in ring_full() argument
179 return ((ring->head + 1) % ring->size) == ring->tail; in ring_full()
182 static bool ring_empty(struct tb_ring *ring) in ring_empty() argument
184 return ring->head == ring->tail; in ring_empty()
188 * ring_write_descriptors() - post frames from ring->queue to the controller
190 * ring->lock is held.
192 static void ring_write_descriptors(struct tb_ring *ring) in ring_write_descriptors() argument
196 list_for_each_entry_safe(frame, n, &ring->queue, list) { in ring_write_descriptors()
197 if (ring_full(ring)) in ring_write_descriptors()
199 list_move_tail(&frame->list, &ring->in_flight); in ring_write_descriptors()
200 descriptor = &ring->descriptors[ring->head]; in ring_write_descriptors()
204 if (ring->is_tx) { in ring_write_descriptors()
209 ring->head = (ring->head + 1) % ring->size; in ring_write_descriptors()
210 if (ring->is_tx) in ring_write_descriptors()
211 ring_iowrite_prod(ring, ring->head); in ring_write_descriptors()
213 ring_iowrite_cons(ring, ring->head); in ring_write_descriptors()
220 * If the ring is shutting down then all frames are marked as canceled and
223 * Otherwise we collect all completed frame from the ring buffer, write new
224 * frame to the ring buffer and invoke the callbacks for the completed frames.
228 struct tb_ring *ring = container_of(work, typeof(*ring), work); in ring_work() local
234 spin_lock_irqsave(&ring->lock, flags); in ring_work()
236 if (!ring->running) { in ring_work()
238 list_splice_tail_init(&ring->in_flight, &done); in ring_work()
239 list_splice_tail_init(&ring->queue, &done); in ring_work()
244 while (!ring_empty(ring)) { in ring_work()
245 if (!(ring->descriptors[ring->tail].flags in ring_work()
248 frame = list_first_entry(&ring->in_flight, typeof(*frame), in ring_work()
251 if (!ring->is_tx) { in ring_work()
252 frame->size = ring->descriptors[ring->tail].length; in ring_work()
253 frame->eof = ring->descriptors[ring->tail].eof; in ring_work()
254 frame->sof = ring->descriptors[ring->tail].sof; in ring_work()
255 frame->flags = ring->descriptors[ring->tail].flags; in ring_work()
257 ring->tail = (ring->tail + 1) % ring->size; in ring_work()
259 ring_write_descriptors(ring); in ring_work()
263 spin_unlock_irqrestore(&ring->lock, flags); in ring_work()
272 frame->callback(ring, frame, canceled); in ring_work()
276 int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame) in __tb_ring_enqueue() argument
281 spin_lock_irqsave(&ring->lock, flags); in __tb_ring_enqueue()
282 if (ring->running) { in __tb_ring_enqueue()
283 list_add_tail(&frame->list, &ring->queue); in __tb_ring_enqueue()
284 ring_write_descriptors(ring); in __tb_ring_enqueue()
288 spin_unlock_irqrestore(&ring->lock, flags); in __tb_ring_enqueue()
294 * tb_ring_poll() - Poll one completed frame from the ring
295 * @ring: Ring to poll
297 * This function can be called when @start_poll callback of the @ring
298 * has been called. It will read one completed frame from the ring and
302 struct ring_frame *tb_ring_poll(struct tb_ring *ring) in tb_ring_poll() argument
307 spin_lock_irqsave(&ring->lock, flags); in tb_ring_poll()
308 if (!ring->running) in tb_ring_poll()
310 if (ring_empty(ring)) in tb_ring_poll()
313 if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) { in tb_ring_poll()
314 frame = list_first_entry(&ring->in_flight, typeof(*frame), in tb_ring_poll()
318 if (!ring->is_tx) { in tb_ring_poll()
319 frame->size = ring->descriptors[ring->tail].length; in tb_ring_poll()
320 frame->eof = ring->descriptors[ring->tail].eof; in tb_ring_poll()
321 frame->sof = ring->descriptors[ring->tail].sof; in tb_ring_poll()
322 frame->flags = ring->descriptors[ring->tail].flags; in tb_ring_poll()
325 ring->tail = (ring->tail + 1) % ring->size; in tb_ring_poll()
329 spin_unlock_irqrestore(&ring->lock, flags); in tb_ring_poll()
334 static void __ring_interrupt_mask(struct tb_ring *ring, bool mask) in __ring_interrupt_mask() argument
336 int idx = ring_interrupt_index(ring); in __ring_interrupt_mask()
341 val = ioread32(ring->nhi->iobase + reg); in __ring_interrupt_mask()
346 iowrite32(val, ring->nhi->iobase + reg); in __ring_interrupt_mask()
349 /* Both @nhi->lock and @ring->lock should be held */
350 static void __ring_interrupt(struct tb_ring *ring) in __ring_interrupt() argument
352 if (!ring->running) in __ring_interrupt()
355 if (ring->start_poll) { in __ring_interrupt()
356 __ring_interrupt_mask(ring, true); in __ring_interrupt()
357 ring->start_poll(ring->poll_data); in __ring_interrupt()
359 schedule_work(&ring->work); in __ring_interrupt()
364 * tb_ring_poll_complete() - Re-start interrupt for the ring
365 * @ring: Ring to re-start the interrupt
367 * This will re-start (unmask) the ring interrupt once the user is done
370 void tb_ring_poll_complete(struct tb_ring *ring) in tb_ring_poll_complete() argument
374 spin_lock_irqsave(&ring->nhi->lock, flags); in tb_ring_poll_complete()
375 spin_lock(&ring->lock); in tb_ring_poll_complete()
376 if (ring->start_poll) in tb_ring_poll_complete()
377 __ring_interrupt_mask(ring, false); in tb_ring_poll_complete()
378 spin_unlock(&ring->lock); in tb_ring_poll_complete()
379 spin_unlock_irqrestore(&ring->nhi->lock, flags); in tb_ring_poll_complete()
385 struct tb_ring *ring = data; in ring_msix() local
387 spin_lock(&ring->nhi->lock); in ring_msix()
388 spin_lock(&ring->lock); in ring_msix()
389 __ring_interrupt(ring); in ring_msix()
390 spin_unlock(&ring->lock); in ring_msix()
391 spin_unlock(&ring->nhi->lock); in ring_msix()
396 static int ring_request_msix(struct tb_ring *ring, bool no_suspend) in ring_request_msix() argument
398 struct tb_nhi *nhi = ring->nhi; in ring_request_msix()
409 ring->vector = ret; in ring_request_msix()
411 ring->irq = pci_irq_vector(ring->nhi->pdev, ring->vector); in ring_request_msix()
412 if (ring->irq < 0) in ring_request_msix()
413 return ring->irq; in ring_request_msix()
416 return request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring); in ring_request_msix()
419 static void ring_release_msix(struct tb_ring *ring) in ring_release_msix() argument
421 if (ring->irq <= 0) in ring_release_msix()
424 free_irq(ring->irq, ring); in ring_release_msix()
425 ida_simple_remove(&ring->nhi->msix_ida, ring->vector); in ring_release_msix()
426 ring->vector = 0; in ring_release_msix()
427 ring->irq = 0; in ring_release_msix()
430 static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring) in nhi_alloc_hop() argument
436 if (ring->hop < 0) { in nhi_alloc_hop()
444 if (ring->is_tx) { in nhi_alloc_hop()
446 ring->hop = i; in nhi_alloc_hop()
451 ring->hop = i; in nhi_alloc_hop()
458 if (ring->hop < 0 || ring->hop >= nhi->hop_count) { in nhi_alloc_hop()
459 dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop); in nhi_alloc_hop()
463 if (ring->is_tx && nhi->tx_rings[ring->hop]) { in nhi_alloc_hop()
465 ring->hop); in nhi_alloc_hop()
468 } else if (!ring->is_tx && nhi->rx_rings[ring->hop]) { in nhi_alloc_hop()
470 ring->hop); in nhi_alloc_hop()
475 if (ring->is_tx) in nhi_alloc_hop()
476 nhi->tx_rings[ring->hop] = ring; in nhi_alloc_hop()
478 nhi->rx_rings[ring->hop] = ring; in nhi_alloc_hop()
492 struct tb_ring *ring = NULL; in tb_ring_alloc() local
493 dev_info(&nhi->pdev->dev, "allocating %s ring %d of size %d\n", in tb_ring_alloc()
496 /* Tx Ring 2 is reserved for E2E workaround */ in tb_ring_alloc()
500 ring = kzalloc(sizeof(*ring), GFP_KERNEL); in tb_ring_alloc()
501 if (!ring) in tb_ring_alloc()
504 spin_lock_init(&ring->lock); in tb_ring_alloc()
505 INIT_LIST_HEAD(&ring->queue); in tb_ring_alloc()
506 INIT_LIST_HEAD(&ring->in_flight); in tb_ring_alloc()
507 INIT_WORK(&ring->work, ring_work); in tb_ring_alloc()
509 ring->nhi = nhi; in tb_ring_alloc()
510 ring->hop = hop; in tb_ring_alloc()
511 ring->is_tx = transmit; in tb_ring_alloc()
512 ring->size = size; in tb_ring_alloc()
513 ring->flags = flags; in tb_ring_alloc()
514 ring->sof_mask = sof_mask; in tb_ring_alloc()
515 ring->eof_mask = eof_mask; in tb_ring_alloc()
516 ring->head = 0; in tb_ring_alloc()
517 ring->tail = 0; in tb_ring_alloc()
518 ring->running = false; in tb_ring_alloc()
519 ring->start_poll = start_poll; in tb_ring_alloc()
520 ring->poll_data = poll_data; in tb_ring_alloc()
522 ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev, in tb_ring_alloc()
523 size * sizeof(*ring->descriptors), in tb_ring_alloc()
524 &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO); in tb_ring_alloc()
525 if (!ring->descriptors) in tb_ring_alloc()
528 if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND)) in tb_ring_alloc()
531 if (nhi_alloc_hop(nhi, ring)) in tb_ring_alloc()
534 return ring; in tb_ring_alloc()
537 ring_release_msix(ring); in tb_ring_alloc()
539 dma_free_coherent(&ring->nhi->pdev->dev, in tb_ring_alloc()
540 ring->size * sizeof(*ring->descriptors), in tb_ring_alloc()
541 ring->descriptors, ring->descriptors_dma); in tb_ring_alloc()
543 kfree(ring); in tb_ring_alloc()
549 * tb_ring_alloc_tx() - Allocate DMA ring for transmit
550 * @nhi: Pointer to the NHI the ring is to be allocated
551 * @hop: HopID (ring) to allocate
552 * @size: Number of entries in the ring
553 * @flags: Flags for the ring
563 * tb_ring_alloc_rx() - Allocate DMA ring for receive
564 * @nhi: Pointer to the NHI the ring is to be allocated
565 * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
566 * @size: Number of entries in the ring
567 * @flags: Flags for the ring
570 * @start_poll: If not %NULL the ring will call this function when an
585 * tb_ring_start() - enable a ring
589 void tb_ring_start(struct tb_ring *ring) in tb_ring_start() argument
594 spin_lock_irq(&ring->nhi->lock); in tb_ring_start()
595 spin_lock(&ring->lock); in tb_ring_start()
596 if (ring->nhi->going_away) in tb_ring_start()
598 if (ring->running) { in tb_ring_start()
599 dev_WARN(&ring->nhi->pdev->dev, "ring already started\n"); in tb_ring_start()
602 dev_info(&ring->nhi->pdev->dev, "starting %s %d\n", in tb_ring_start()
603 RING_TYPE(ring), ring->hop); in tb_ring_start()
605 if (ring->flags & RING_FLAG_FRAME) { in tb_ring_start()
614 if (ring->flags & RING_FLAG_E2E && !ring->is_tx) { in tb_ring_start()
627 ring_iowrite64desc(ring, ring->descriptors_dma, 0); in tb_ring_start()
628 if (ring->is_tx) { in tb_ring_start()
629 ring_iowrite32desc(ring, ring->size, 12); in tb_ring_start()
630 ring_iowrite32options(ring, 0, 4); /* time releated ? */ in tb_ring_start()
631 ring_iowrite32options(ring, flags, 0); in tb_ring_start()
633 u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask; in tb_ring_start()
635 ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12); in tb_ring_start()
636 ring_iowrite32options(ring, sof_eof_mask, 4); in tb_ring_start()
637 ring_iowrite32options(ring, flags, 0); in tb_ring_start()
639 ring_interrupt_active(ring, true); in tb_ring_start()
640 ring->running = true; in tb_ring_start()
642 spin_unlock(&ring->lock); in tb_ring_start()
643 spin_unlock_irq(&ring->nhi->lock); in tb_ring_start()
648 * tb_ring_stop() - shutdown a ring
652 * This method will disable the ring. Further calls to
660 void tb_ring_stop(struct tb_ring *ring) in tb_ring_stop() argument
662 spin_lock_irq(&ring->nhi->lock); in tb_ring_stop()
663 spin_lock(&ring->lock); in tb_ring_stop()
664 dev_info(&ring->nhi->pdev->dev, "stopping %s %d\n", in tb_ring_stop()
665 RING_TYPE(ring), ring->hop); in tb_ring_stop()
666 if (ring->nhi->going_away) in tb_ring_stop()
668 if (!ring->running) { in tb_ring_stop()
669 dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n", in tb_ring_stop()
670 RING_TYPE(ring), ring->hop); in tb_ring_stop()
673 ring_interrupt_active(ring, false); in tb_ring_stop()
675 ring_iowrite32options(ring, 0, 0); in tb_ring_stop()
676 ring_iowrite64desc(ring, 0, 0); in tb_ring_stop()
677 ring_iowrite32desc(ring, 0, 8); in tb_ring_stop()
678 ring_iowrite32desc(ring, 0, 12); in tb_ring_stop()
679 ring->head = 0; in tb_ring_stop()
680 ring->tail = 0; in tb_ring_stop()
681 ring->running = false; in tb_ring_stop()
684 spin_unlock(&ring->lock); in tb_ring_stop()
685 spin_unlock_irq(&ring->nhi->lock); in tb_ring_stop()
688 * schedule ring->work to invoke callbacks on all remaining frames. in tb_ring_stop()
690 schedule_work(&ring->work); in tb_ring_stop()
691 flush_work(&ring->work); in tb_ring_stop()
696 * tb_ring_free() - free ring
698 * When this method returns all invocations of ring->callback will have
701 * Ring must be stopped.
705 void tb_ring_free(struct tb_ring *ring) in tb_ring_free() argument
707 spin_lock_irq(&ring->nhi->lock); in tb_ring_free()
709 * Dissociate the ring from the NHI. This also ensures that in tb_ring_free()
710 * nhi_interrupt_work cannot reschedule ring->work. in tb_ring_free()
712 if (ring->is_tx) in tb_ring_free()
713 ring->nhi->tx_rings[ring->hop] = NULL; in tb_ring_free()
715 ring->nhi->rx_rings[ring->hop] = NULL; in tb_ring_free()
717 if (ring->running) { in tb_ring_free()
718 dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n", in tb_ring_free()
719 RING_TYPE(ring), ring->hop); in tb_ring_free()
721 spin_unlock_irq(&ring->nhi->lock); in tb_ring_free()
723 ring_release_msix(ring); in tb_ring_free()
725 dma_free_coherent(&ring->nhi->pdev->dev, in tb_ring_free()
726 ring->size * sizeof(*ring->descriptors), in tb_ring_free()
727 ring->descriptors, ring->descriptors_dma); in tb_ring_free()
729 ring->descriptors = NULL; in tb_ring_free()
730 ring->descriptors_dma = 0; in tb_ring_free()
733 dev_info(&ring->nhi->pdev->dev, in tb_ring_free()
735 RING_TYPE(ring), in tb_ring_free()
736 ring->hop); in tb_ring_free()
739 * ring->work can no longer be scheduled (it is scheduled only in tb_ring_free()
741 * to finish before freeing the ring. in tb_ring_free()
743 flush_work(&ring->work); in tb_ring_free()
744 kfree(ring); in tb_ring_free()
810 struct tb_ring *ring; in nhi_interrupt_work() local
832 "RX overflow for ring %d\n", in nhi_interrupt_work()
837 ring = nhi->tx_rings[hop]; in nhi_interrupt_work()
839 ring = nhi->rx_rings[hop]; in nhi_interrupt_work()
840 if (ring == NULL) { in nhi_interrupt_work()
842 "got interrupt for inactive %s ring %d\n", in nhi_interrupt_work()
848 spin_lock(&ring->lock); in nhi_interrupt_work()
849 __ring_interrupt(ring); in nhi_interrupt_work()
850 spin_unlock(&ring->lock); in nhi_interrupt_work()
953 "TX ring %d is still active\n", i); in nhi_shutdown()
956 "RX ring %d is still active\n", i); in nhi_shutdown()
984 * get all MSI-X vectors and if we succeed, each ring will have in nhi_init_msi()