aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/host/xhci-mem.c
diff options
context:
space:
mode:
authorGerd Hoffmann <kraxel@redhat.com>2013-10-03 18:29:44 -0400
committerSarah Sharp <sarah.a.sharp@linux.intel.com>2014-03-04 18:38:00 -0500
commit153413032c6ea624fccc6732aba27a57688a7f91 (patch)
tree2d6273e9835906c6a6385a2ed59adc2b42666d2e /drivers/usb/host/xhci-mem.c
parente587b8b270d3706147c806d42cc4ac78232caac7 (diff)
xhci: fix usb3 streams
xhci maintains a radix tree for each stream endpoint because it must be able to map a trb address to the stream ring. Each ring segment must be added to the ring for this to work. Currently xhci sticks only the first segment of each stream ring into the radix tree. Result is that things work initially, but as soon as the first segment is full xhci can't map the trb address from the completion event to the stream ring any more -> BOOM. You'll find this message in the logs: ERROR Transfer event for disabled endpoint or incorrect stream ring This patch adds a helper function to update the radix tree, and a function to remove ring segments from the tree. Both functions loop over the segment list and handles all segments instead of just the first. [Note: Sarah changed this patch to add radix_tree_maybe_preload() and radix_tree_preload_end() calls around the radix tree insert, since we can now insert entries in interrupt context. There are now two helper functions to make the code cleaner, and those functions are moved to make them static.] Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> Signed-off-by: Hans de Goede <hdegoede@redhat.com> Signed-off-by: Sarah Sharp <sarah.a.sharp@linux.intel.com>
Diffstat (limited to 'drivers/usb/host/xhci-mem.c')
-rw-r--r--drivers/usb/host/xhci-mem.c132
1 files changed, 89 insertions, 43 deletions
diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index bce4391a0e7d..39f9a99a503b 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -149,14 +149,95 @@ static void xhci_link_rings(struct xhci_hcd *xhci, struct xhci_ring *ring,
149 } 149 }
150} 150}
151 151
152/*
153 * We need a radix tree for mapping physical addresses of TRBs to which stream
154 * ID they belong to. We need to do this because the host controller won't tell
155 * us which stream ring the TRB came from. We could store the stream ID in an
156 * event data TRB, but that doesn't help us for the cancellation case, since the
157 * endpoint may stop before it reaches that event data TRB.
158 *
159 * The radix tree maps the upper portion of the TRB DMA address to a ring
160 * segment that has the same upper portion of DMA addresses. For example, say I
161 * have segments of size 1KB, that are always 64-byte aligned. A segment may
162 * start at 0x10c91000 and end at 0x10c913f0. If I use the upper 10 bits, the
163 * key to the stream ID is 0x43244. I can use the DMA address of the TRB to
164 * pass the radix tree a key to get the right stream ID:
165 *
166 * 0x10c90fff >> 10 = 0x43243
167 * 0x10c912c0 >> 10 = 0x43244
168 * 0x10c91400 >> 10 = 0x43245
169 *
170 * Obviously, only those TRBs with DMA addresses that are within the segment
171 * will make the radix tree return the stream ID for that ring.
172 *
173 * Caveats for the radix tree:
174 *
175 * The radix tree uses an unsigned long as a key pair. On 32-bit systems, an
176 * unsigned long will be 32-bits; on a 64-bit system an unsigned long will be
177 * 64-bits. Since we only request 32-bit DMA addresses, we can use that as the
178 * key on 32-bit or 64-bit systems (it would also be fine if we asked for 64-bit
179 * PCI DMA addresses on a 64-bit system). There might be a problem on 32-bit
180 * extended systems (where the DMA address can be bigger than 32-bits),
181 * if we allow the PCI dma mask to be bigger than 32-bits. So don't do that.
182 */
183static int xhci_update_stream_mapping(struct xhci_ring *ring, gfp_t mem_flags)
184{
185 struct xhci_segment *seg;
186 unsigned long key;
187 int ret;
188
189 if (WARN_ON_ONCE(ring->trb_address_map == NULL))
190 return 0;
191
192 seg = ring->first_seg;
193 do {
194 key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
195 /* Skip any segments that were already added. */
196 if (radix_tree_lookup(ring->trb_address_map, key))
197 continue;
198
199 ret = radix_tree_maybe_preload(mem_flags);
200 if (ret)
201 return ret;
202 ret = radix_tree_insert(ring->trb_address_map,
203 key, ring);
204 radix_tree_preload_end();
205 if (ret)
206 return ret;
207 seg = seg->next;
208 } while (seg != ring->first_seg);
209
210 return 0;
211}
212
213static void xhci_remove_stream_mapping(struct xhci_ring *ring)
214{
215 struct xhci_segment *seg;
216 unsigned long key;
217
218 if (WARN_ON_ONCE(ring->trb_address_map == NULL))
219 return;
220
221 seg = ring->first_seg;
222 do {
223 key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
224 if (radix_tree_lookup(ring->trb_address_map, key))
225 radix_tree_delete(ring->trb_address_map, key);
226 seg = seg->next;
227 } while (seg != ring->first_seg);
228}
229
152/* XXX: Do we need the hcd structure in all these functions? */ 230/* XXX: Do we need the hcd structure in all these functions? */
153void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring) 231void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring)
154{ 232{
155 if (!ring) 233 if (!ring)
156 return; 234 return;
157 235
158 if (ring->first_seg) 236 if (ring->first_seg) {
237 if (ring->type == TYPE_STREAM)
238 xhci_remove_stream_mapping(ring);
159 xhci_free_segments_for_ring(xhci, ring->first_seg); 239 xhci_free_segments_for_ring(xhci, ring->first_seg);
240 }
160 241
161 kfree(ring); 242 kfree(ring);
162} 243}
@@ -354,6 +435,11 @@ int xhci_ring_expansion(struct xhci_hcd *xhci, struct xhci_ring *ring,
354 "ring expansion succeed, now has %d segments", 435 "ring expansion succeed, now has %d segments",
355 ring->num_segs); 436 ring->num_segs);
356 437
438 if (ring->type == TYPE_STREAM) {
439 ret = xhci_update_stream_mapping(ring, flags);
440 WARN_ON(ret); /* FIXME */
441 }
442
357 return 0; 443 return 0;
358} 444}
359 445
@@ -510,36 +596,6 @@ struct xhci_ring *xhci_stream_id_to_ring(
510 * The number of stream contexts in the stream context array may be bigger than 596 * The number of stream contexts in the stream context array may be bigger than
511 * the number of streams the driver wants to use. This is because the number of 597 * the number of streams the driver wants to use. This is because the number of
512 * stream context array entries must be a power of two. 598 * stream context array entries must be a power of two.
513 *
514 * We need a radix tree for mapping physical addresses of TRBs to which stream
515 * ID they belong to. We need to do this because the host controller won't tell
516 * us which stream ring the TRB came from. We could store the stream ID in an
517 * event data TRB, but that doesn't help us for the cancellation case, since the
518 * endpoint may stop before it reaches that event data TRB.
519 *
520 * The radix tree maps the upper portion of the TRB DMA address to a ring
521 * segment that has the same upper portion of DMA addresses. For example, say I
522 * have segments of size 1KB, that are always 64-byte aligned. A segment may
523 * start at 0x10c91000 and end at 0x10c913f0. If I use the upper 10 bits, the
524 * key to the stream ID is 0x43244. I can use the DMA address of the TRB to
525 * pass the radix tree a key to get the right stream ID:
526 *
527 * 0x10c90fff >> 10 = 0x43243
528 * 0x10c912c0 >> 10 = 0x43244
529 * 0x10c91400 >> 10 = 0x43245
530 *
531 * Obviously, only those TRBs with DMA addresses that are within the segment
532 * will make the radix tree return the stream ID for that ring.
533 *
534 * Caveats for the radix tree:
535 *
536 * The radix tree uses an unsigned long as a key pair. On 32-bit systems, an
537 * unsigned long will be 32-bits; on a 64-bit system an unsigned long will be
538 * 64-bits. Since we only request 32-bit DMA addresses, we can use that as the
539 * key on 32-bit or 64-bit systems (it would also be fine if we asked for 64-bit
540 * PCI DMA addresses on a 64-bit system). There might be a problem on 32-bit
541 * extended systems (where the DMA address can be bigger than 32-bits),
542 * if we allow the PCI dma mask to be bigger than 32-bits. So don't do that.
543 */ 599 */
544struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci, 600struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
545 unsigned int num_stream_ctxs, 601 unsigned int num_stream_ctxs,
@@ -548,7 +604,6 @@ struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
548 struct xhci_stream_info *stream_info; 604 struct xhci_stream_info *stream_info;
549 u32 cur_stream; 605 u32 cur_stream;
550 struct xhci_ring *cur_ring; 606 struct xhci_ring *cur_ring;
551 unsigned long key;
552 u64 addr; 607 u64 addr;
553 int ret; 608 int ret;
554 609
@@ -603,6 +658,7 @@ struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
603 if (!cur_ring) 658 if (!cur_ring)
604 goto cleanup_rings; 659 goto cleanup_rings;
605 cur_ring->stream_id = cur_stream; 660 cur_ring->stream_id = cur_stream;
661 cur_ring->trb_address_map = &stream_info->trb_address_map;
606 /* Set deq ptr, cycle bit, and stream context type */ 662 /* Set deq ptr, cycle bit, and stream context type */
607 addr = cur_ring->first_seg->dma | 663 addr = cur_ring->first_seg->dma |
608 SCT_FOR_CTX(SCT_PRI_TR) | 664 SCT_FOR_CTX(SCT_PRI_TR) |
@@ -612,10 +668,7 @@ struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
612 xhci_dbg(xhci, "Setting stream %d ring ptr to 0x%08llx\n", 668 xhci_dbg(xhci, "Setting stream %d ring ptr to 0x%08llx\n",
613 cur_stream, (unsigned long long) addr); 669 cur_stream, (unsigned long long) addr);
614 670
615 key = (unsigned long) 671 ret = xhci_update_stream_mapping(cur_ring, mem_flags);
616 (cur_ring->first_seg->dma >> TRB_SEGMENT_SHIFT);
617 ret = radix_tree_insert(&stream_info->trb_address_map,
618 key, cur_ring);
619 if (ret) { 672 if (ret) {
620 xhci_ring_free(xhci, cur_ring); 673 xhci_ring_free(xhci, cur_ring);
621 stream_info->stream_rings[cur_stream] = NULL; 674 stream_info->stream_rings[cur_stream] = NULL;
@@ -635,9 +688,6 @@ cleanup_rings:
635 for (cur_stream = 1; cur_stream < num_streams; cur_stream++) { 688 for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
636 cur_ring = stream_info->stream_rings[cur_stream]; 689 cur_ring = stream_info->stream_rings[cur_stream];
637 if (cur_ring) { 690 if (cur_ring) {
638 addr = cur_ring->first_seg->dma;
639 radix_tree_delete(&stream_info->trb_address_map,
640 addr >> TRB_SEGMENT_SHIFT);
641 xhci_ring_free(xhci, cur_ring); 691 xhci_ring_free(xhci, cur_ring);
642 stream_info->stream_rings[cur_stream] = NULL; 692 stream_info->stream_rings[cur_stream] = NULL;
643 } 693 }
@@ -698,7 +748,6 @@ void xhci_free_stream_info(struct xhci_hcd *xhci,
698{ 748{
699 int cur_stream; 749 int cur_stream;
700 struct xhci_ring *cur_ring; 750 struct xhci_ring *cur_ring;
701 dma_addr_t addr;
702 751
703 if (!stream_info) 752 if (!stream_info)
704 return; 753 return;
@@ -707,9 +756,6 @@ void xhci_free_stream_info(struct xhci_hcd *xhci,
707 cur_stream++) { 756 cur_stream++) {
708 cur_ring = stream_info->stream_rings[cur_stream]; 757 cur_ring = stream_info->stream_rings[cur_stream];
709 if (cur_ring) { 758 if (cur_ring) {
710 addr = cur_ring->first_seg->dma;
711 radix_tree_delete(&stream_info->trb_address_map,
712 addr >> TRB_SEGMENT_SHIFT);
713 xhci_ring_free(xhci, cur_ring); 759 xhci_ring_free(xhci, cur_ring);
714 stream_info->stream_rings[cur_stream] = NULL; 760 stream_info->stream_rings[cur_stream] = NULL;
715 } 761 }