Lines Matching +full:generic +full:- +full:xhci

1 // SPDX-License-Identifier: GPL-2.0
3 * xHCI host controller driver
15 #include <linux/dma-mapping.h>
17 #include "xhci.h"
18 #include "xhci-trace.h"
19 #include "xhci-debugfs.h"
22 * Allocates a generic ring segment from the ring pool, sets the dma address,
28 static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci, in xhci_segment_alloc() argument
36 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_segment_alloc()
42 seg->trbs = dma_pool_zalloc(xhci->segment_pool, flags, &dma); in xhci_segment_alloc()
43 if (!seg->trbs) { in xhci_segment_alloc()
49 seg->bounce_buf = kzalloc_node(max_packet, flags, in xhci_segment_alloc()
51 if (!seg->bounce_buf) { in xhci_segment_alloc()
52 dma_pool_free(xhci->segment_pool, seg->trbs, dma); in xhci_segment_alloc()
60 seg->trbs[i].link.control |= cpu_to_le32(TRB_CYCLE); in xhci_segment_alloc()
62 seg->dma = dma; in xhci_segment_alloc()
63 seg->next = NULL; in xhci_segment_alloc()
68 static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg) in xhci_segment_free() argument
70 if (seg->trbs) { in xhci_segment_free()
71 dma_pool_free(xhci->segment_pool, seg->trbs, seg->dma); in xhci_segment_free()
72 seg->trbs = NULL; in xhci_segment_free()
74 kfree(seg->bounce_buf); in xhci_segment_free()
78 static void xhci_free_segments_for_ring(struct xhci_hcd *xhci, in xhci_free_segments_for_ring() argument
83 seg = first->next; in xhci_free_segments_for_ring()
85 struct xhci_segment *next = seg->next; in xhci_free_segments_for_ring()
86 xhci_segment_free(xhci, seg); in xhci_free_segments_for_ring()
89 xhci_segment_free(xhci, first); in xhci_free_segments_for_ring()
107 prev->next = next; in xhci_link_segments()
109 prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = in xhci_link_segments()
110 cpu_to_le64(next->dma); in xhci_link_segments()
113 val = le32_to_cpu(prev->trbs[TRBS_PER_SEGMENT-1].link.control); in xhci_link_segments()
118 prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val); in xhci_link_segments()
126 static void xhci_link_rings(struct xhci_hcd *xhci, struct xhci_ring *ring, in xhci_link_rings() argument
137 chain_links = !!(xhci_link_trb_quirk(xhci) || in xhci_link_rings()
138 (ring->type == TYPE_ISOC && in xhci_link_rings()
139 (xhci->quirks & XHCI_AMD_0x96_HOST))); in xhci_link_rings()
141 next = ring->enq_seg->next; in xhci_link_rings()
142 xhci_link_segments(ring->enq_seg, first, ring->type, chain_links); in xhci_link_rings()
143 xhci_link_segments(last, next, ring->type, chain_links); in xhci_link_rings()
144 ring->num_segs += num_segs; in xhci_link_rings()
145 ring->num_trbs_free += (TRBS_PER_SEGMENT - 1) * num_segs; in xhci_link_rings()
147 if (ring->type != TYPE_EVENT && ring->enq_seg == ring->last_seg) { in xhci_link_rings()
148 ring->last_seg->trbs[TRBS_PER_SEGMENT-1].link.control in xhci_link_rings()
150 last->trbs[TRBS_PER_SEGMENT-1].link.control in xhci_link_rings()
152 ring->last_seg = last; in xhci_link_rings()
179 * The radix tree uses an unsigned long as a key pair. On 32-bit systems, an
180 * unsigned long will be 32-bits; on a 64-bit system an unsigned long will be
181 * 64-bits. Since we only request 32-bit DMA addresses, we can use that as the
182 * key on 32-bit or 64-bit systems (it would also be fine if we asked for 64-bit
183 * PCI DMA addresses on a 64-bit system). There might be a problem on 32-bit
184 * extended systems (where the DMA address can be bigger than 32-bits),
185 * if we allow the PCI dma mask to be bigger than 32-bits. So don't do that.
195 key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT); in xhci_insert_segment_mapping()
214 key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT); in xhci_remove_segment_mapping()
241 seg = seg->next; in xhci_update_stream_segment_mapping()
253 seg = seg->next; in xhci_update_stream_segment_mapping()
263 if (WARN_ON_ONCE(ring->trb_address_map == NULL)) in xhci_remove_stream_mapping()
266 seg = ring->first_seg; in xhci_remove_stream_mapping()
268 xhci_remove_segment_mapping(ring->trb_address_map, seg); in xhci_remove_stream_mapping()
269 seg = seg->next; in xhci_remove_stream_mapping()
270 } while (seg != ring->first_seg); in xhci_remove_stream_mapping()
275 return xhci_update_stream_segment_mapping(ring->trb_address_map, ring, in xhci_update_stream_mapping()
276 ring->first_seg, ring->last_seg, mem_flags); in xhci_update_stream_mapping()
280 void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring) in xhci_ring_free() argument
287 if (ring->first_seg) { in xhci_ring_free()
288 if (ring->type == TYPE_STREAM) in xhci_ring_free()
290 xhci_free_segments_for_ring(xhci, ring->first_seg); in xhci_ring_free()
300 ring->enqueue = ring->first_seg->trbs; in xhci_initialize_ring_info()
301 ring->enq_seg = ring->first_seg; in xhci_initialize_ring_info()
302 ring->dequeue = ring->enqueue; in xhci_initialize_ring_info()
303 ring->deq_seg = ring->first_seg; in xhci_initialize_ring_info()
311 ring->cycle_state = cycle_state; in xhci_initialize_ring_info()
317 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1; in xhci_initialize_ring_info()
321 static int xhci_alloc_segments_for_ring(struct xhci_hcd *xhci, in xhci_alloc_segments_for_ring() argument
330 chain_links = !!(xhci_link_trb_quirk(xhci) || in xhci_alloc_segments_for_ring()
332 (xhci->quirks & XHCI_AMD_0x96_HOST))); in xhci_alloc_segments_for_ring()
334 prev = xhci_segment_alloc(xhci, cycle_state, max_packet, flags); in xhci_alloc_segments_for_ring()
336 return -ENOMEM; in xhci_alloc_segments_for_ring()
337 num_segs--; in xhci_alloc_segments_for_ring()
343 next = xhci_segment_alloc(xhci, cycle_state, max_packet, flags); in xhci_alloc_segments_for_ring()
347 next = prev->next; in xhci_alloc_segments_for_ring()
348 xhci_segment_free(xhci, prev); in xhci_alloc_segments_for_ring()
351 return -ENOMEM; in xhci_alloc_segments_for_ring()
356 num_segs--; in xhci_alloc_segments_for_ring()
371 struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci, in xhci_ring_alloc() argument
377 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_ring_alloc()
383 ring->num_segs = num_segs; in xhci_ring_alloc()
384 ring->bounce_buf_len = max_packet; in xhci_ring_alloc()
385 INIT_LIST_HEAD(&ring->td_list); in xhci_ring_alloc()
386 ring->type = type; in xhci_ring_alloc()
390 ret = xhci_alloc_segments_for_ring(xhci, &ring->first_seg, in xhci_ring_alloc()
391 &ring->last_seg, num_segs, cycle_state, type, in xhci_ring_alloc()
399 ring->last_seg->trbs[TRBS_PER_SEGMENT - 1].link.control |= in xhci_ring_alloc()
411 void xhci_free_endpoint_ring(struct xhci_hcd *xhci, in xhci_free_endpoint_ring() argument
415 xhci_ring_free(xhci, virt_dev->eps[ep_index].ring); in xhci_free_endpoint_ring()
416 virt_dev->eps[ep_index].ring = NULL; in xhci_free_endpoint_ring()
423 int xhci_ring_expansion(struct xhci_hcd *xhci, struct xhci_ring *ring, in xhci_ring_expansion() argument
432 num_segs_needed = (num_trbs + (TRBS_PER_SEGMENT - 1) - 1) / in xhci_ring_expansion()
433 (TRBS_PER_SEGMENT - 1); in xhci_ring_expansion()
436 num_segs = ring->num_segs > num_segs_needed ? in xhci_ring_expansion()
437 ring->num_segs : num_segs_needed; in xhci_ring_expansion()
439 ret = xhci_alloc_segments_for_ring(xhci, &first, &last, in xhci_ring_expansion()
440 num_segs, ring->cycle_state, ring->type, in xhci_ring_expansion()
441 ring->bounce_buf_len, flags); in xhci_ring_expansion()
443 return -ENOMEM; in xhci_ring_expansion()
445 if (ring->type == TYPE_STREAM) in xhci_ring_expansion()
446 ret = xhci_update_stream_segment_mapping(ring->trb_address_map, in xhci_ring_expansion()
451 next = first->next; in xhci_ring_expansion()
452 xhci_segment_free(xhci, first); in xhci_ring_expansion()
460 xhci_link_rings(xhci, ring, first, last, num_segs); in xhci_ring_expansion()
462 xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion, in xhci_ring_expansion()
464 ring->num_segs); in xhci_ring_expansion()
469 struct xhci_container_ctx *xhci_alloc_container_ctx(struct xhci_hcd *xhci, in xhci_alloc_container_ctx() argument
473 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_alloc_container_ctx()
482 ctx->type = type; in xhci_alloc_container_ctx()
483 ctx->size = HCC_64BYTE_CONTEXT(xhci->hcc_params) ? 2048 : 1024; in xhci_alloc_container_ctx()
485 ctx->size += CTX_SIZE(xhci->hcc_params); in xhci_alloc_container_ctx()
487 ctx->bytes = dma_pool_zalloc(xhci->device_pool, flags, &ctx->dma); in xhci_alloc_container_ctx()
488 if (!ctx->bytes) { in xhci_alloc_container_ctx()
495 void xhci_free_container_ctx(struct xhci_hcd *xhci, in xhci_free_container_ctx() argument
500 dma_pool_free(xhci->device_pool, ctx->bytes, ctx->dma); in xhci_free_container_ctx()
507 if (ctx->type != XHCI_CTX_TYPE_INPUT) in xhci_get_input_control_ctx()
510 return (struct xhci_input_control_ctx *)ctx->bytes; in xhci_get_input_control_ctx()
513 struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_hcd *xhci, in xhci_get_slot_ctx() argument
516 if (ctx->type == XHCI_CTX_TYPE_DEVICE) in xhci_get_slot_ctx()
517 return (struct xhci_slot_ctx *)ctx->bytes; in xhci_get_slot_ctx()
520 (ctx->bytes + CTX_SIZE(xhci->hcc_params)); in xhci_get_slot_ctx()
523 struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_hcd *xhci, in xhci_get_ep_ctx() argument
529 if (ctx->type == XHCI_CTX_TYPE_INPUT) in xhci_get_ep_ctx()
533 (ctx->bytes + (ep_index * CTX_SIZE(xhci->hcc_params))); in xhci_get_ep_ctx()
539 static void xhci_free_stream_ctx(struct xhci_hcd *xhci, in xhci_free_stream_ctx() argument
543 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_free_stream_ctx()
550 return dma_pool_free(xhci->small_streams_pool, in xhci_free_stream_ctx()
553 return dma_pool_free(xhci->medium_streams_pool, in xhci_free_stream_ctx()
560 * - how many streams the endpoint supports,
561 * - the maximum primary stream array size the host controller supports,
562 * - and how many streams the device driver asks for.
567 static struct xhci_stream_ctx *xhci_alloc_stream_ctx(struct xhci_hcd *xhci, in xhci_alloc_stream_ctx() argument
571 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_alloc_stream_ctx()
578 return dma_pool_alloc(xhci->small_streams_pool, in xhci_alloc_stream_ctx()
581 return dma_pool_alloc(xhci->medium_streams_pool, in xhci_alloc_stream_ctx()
589 if (ep->ep_state & EP_HAS_STREAMS) in xhci_dma_to_transfer_ring()
590 return radix_tree_lookup(&ep->stream_info->trb_address_map, in xhci_dma_to_transfer_ring()
592 return ep->ring; in xhci_dma_to_transfer_ring()
604 struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci, in xhci_alloc_stream_info() argument
614 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_alloc_stream_info()
616 xhci_dbg(xhci, "Allocating %u streams and %u " in xhci_alloc_stream_info()
619 if (xhci->cmd_ring_reserved_trbs == MAX_RSVD_CMD_TRBS) { in xhci_alloc_stream_info()
620 xhci_dbg(xhci, "Command ring has no reserved TRBs available\n"); in xhci_alloc_stream_info()
623 xhci->cmd_ring_reserved_trbs++; in xhci_alloc_stream_info()
630 stream_info->num_streams = num_streams; in xhci_alloc_stream_info()
631 stream_info->num_stream_ctxs = num_stream_ctxs; in xhci_alloc_stream_info()
634 stream_info->stream_rings = kcalloc_node( in xhci_alloc_stream_info()
637 if (!stream_info->stream_rings) in xhci_alloc_stream_info()
641 stream_info->stream_ctx_array = xhci_alloc_stream_ctx(xhci, in xhci_alloc_stream_info()
642 num_stream_ctxs, &stream_info->ctx_array_dma, in xhci_alloc_stream_info()
644 if (!stream_info->stream_ctx_array) in xhci_alloc_stream_info()
646 memset(stream_info->stream_ctx_array, 0, in xhci_alloc_stream_info()
650 stream_info->free_streams_command = in xhci_alloc_stream_info()
651 xhci_alloc_command_with_ctx(xhci, true, mem_flags); in xhci_alloc_stream_info()
652 if (!stream_info->free_streams_command) in xhci_alloc_stream_info()
655 INIT_RADIX_TREE(&stream_info->trb_address_map, GFP_ATOMIC); in xhci_alloc_stream_info()
663 stream_info->stream_rings[cur_stream] = in xhci_alloc_stream_info()
664 xhci_ring_alloc(xhci, 2, 1, TYPE_STREAM, max_packet, in xhci_alloc_stream_info()
666 cur_ring = stream_info->stream_rings[cur_stream]; in xhci_alloc_stream_info()
669 cur_ring->stream_id = cur_stream; in xhci_alloc_stream_info()
670 cur_ring->trb_address_map = &stream_info->trb_address_map; in xhci_alloc_stream_info()
672 addr = cur_ring->first_seg->dma | in xhci_alloc_stream_info()
674 cur_ring->cycle_state; in xhci_alloc_stream_info()
675 stream_info->stream_ctx_array[cur_stream].stream_ring = in xhci_alloc_stream_info()
677 xhci_dbg(xhci, "Setting stream %d ring ptr to 0x%08llx\n", in xhci_alloc_stream_info()
682 xhci_ring_free(xhci, cur_ring); in xhci_alloc_stream_info()
683 stream_info->stream_rings[cur_stream] = NULL; in xhci_alloc_stream_info()
698 cur_ring = stream_info->stream_rings[cur_stream]; in xhci_alloc_stream_info()
700 xhci_ring_free(xhci, cur_ring); in xhci_alloc_stream_info()
701 stream_info->stream_rings[cur_stream] = NULL; in xhci_alloc_stream_info()
704 xhci_free_command(xhci, stream_info->free_streams_command); in xhci_alloc_stream_info()
706 kfree(stream_info->stream_rings); in xhci_alloc_stream_info()
710 xhci->cmd_ring_reserved_trbs--; in xhci_alloc_stream_info()
717 void xhci_setup_streams_ep_input_ctx(struct xhci_hcd *xhci, in xhci_setup_streams_ep_input_ctx() argument
726 max_primary_streams = fls(stream_info->num_stream_ctxs) - 2; in xhci_setup_streams_ep_input_ctx()
727 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_setup_streams_ep_input_ctx()
730 ep_ctx->ep_info &= cpu_to_le32(~EP_MAXPSTREAMS_MASK); in xhci_setup_streams_ep_input_ctx()
731 ep_ctx->ep_info |= cpu_to_le32(EP_MAXPSTREAMS(max_primary_streams) in xhci_setup_streams_ep_input_ctx()
733 ep_ctx->deq = cpu_to_le64(stream_info->ctx_array_dma); in xhci_setup_streams_ep_input_ctx()
745 ep_ctx->ep_info &= cpu_to_le32(~(EP_MAXPSTREAMS_MASK | EP_HAS_LSA)); in xhci_setup_no_streams_ep_input_ctx()
746 addr = xhci_trb_virt_to_dma(ep->ring->deq_seg, ep->ring->dequeue); in xhci_setup_no_streams_ep_input_ctx()
747 ep_ctx->deq = cpu_to_le64(addr | ep->ring->cycle_state); in xhci_setup_no_streams_ep_input_ctx()
754 void xhci_free_stream_info(struct xhci_hcd *xhci, in xhci_free_stream_info() argument
763 for (cur_stream = 1; cur_stream < stream_info->num_streams; in xhci_free_stream_info()
765 cur_ring = stream_info->stream_rings[cur_stream]; in xhci_free_stream_info()
767 xhci_ring_free(xhci, cur_ring); in xhci_free_stream_info()
768 stream_info->stream_rings[cur_stream] = NULL; in xhci_free_stream_info()
771 xhci_free_command(xhci, stream_info->free_streams_command); in xhci_free_stream_info()
772 xhci->cmd_ring_reserved_trbs--; in xhci_free_stream_info()
773 if (stream_info->stream_ctx_array) in xhci_free_stream_info()
774 xhci_free_stream_ctx(xhci, in xhci_free_stream_info()
775 stream_info->num_stream_ctxs, in xhci_free_stream_info()
776 stream_info->stream_ctx_array, in xhci_free_stream_info()
777 stream_info->ctx_array_dma); in xhci_free_stream_info()
779 kfree(stream_info->stream_rings); in xhci_free_stream_info()
786 static void xhci_init_endpoint_timer(struct xhci_hcd *xhci, in xhci_init_endpoint_timer() argument
789 timer_setup(&ep->stop_cmd_timer, xhci_stop_endpoint_command_watchdog, in xhci_init_endpoint_timer()
791 ep->xhci = xhci; in xhci_init_endpoint_timer()
794 static void xhci_free_tt_info(struct xhci_hcd *xhci, in xhci_free_tt_info() argument
805 if (virt_dev->real_port == 0 || in xhci_free_tt_info()
806 virt_dev->real_port > HCS_MAX_PORTS(xhci->hcs_params1)) { in xhci_free_tt_info()
807 xhci_dbg(xhci, "Bad real port.\n"); in xhci_free_tt_info()
811 tt_list_head = &(xhci->rh_bw[virt_dev->real_port - 1].tts); in xhci_free_tt_info()
813 /* Multi-TT hubs will have more than one entry */ in xhci_free_tt_info()
814 if (tt_info->slot_id == slot_id) { in xhci_free_tt_info()
816 list_del(&tt_info->tt_list); in xhci_free_tt_info()
824 int xhci_alloc_tt_info(struct xhci_hcd *xhci, in xhci_alloc_tt_info() argument
832 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_alloc_tt_info()
834 if (!tt->multi) in xhci_alloc_tt_info()
837 num_ports = hdev->maxchild; in xhci_alloc_tt_info()
846 INIT_LIST_HEAD(&tt_info->tt_list); in xhci_alloc_tt_info()
847 list_add(&tt_info->tt_list, in xhci_alloc_tt_info()
848 &xhci->rh_bw[virt_dev->real_port - 1].tts); in xhci_alloc_tt_info()
849 tt_info->slot_id = virt_dev->udev->slot_id; in xhci_alloc_tt_info()
850 if (tt->multi) in xhci_alloc_tt_info()
851 tt_info->ttport = i+1; in xhci_alloc_tt_info()
852 bw_table = &tt_info->bw_table; in xhci_alloc_tt_info()
854 INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints); in xhci_alloc_tt_info()
859 xhci_free_tt_info(xhci, virt_dev, virt_dev->udev->slot_id); in xhci_alloc_tt_info()
860 return -ENOMEM; in xhci_alloc_tt_info()
865 * Should be called with xhci->lock held if there is any chance the TT lists
869 void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id) in xhci_free_virt_device() argument
876 if (slot_id == 0 || !xhci->devs[slot_id]) in xhci_free_virt_device()
879 dev = xhci->devs[slot_id]; in xhci_free_virt_device()
881 xhci->dcbaa->dev_context_ptrs[slot_id] = 0; in xhci_free_virt_device()
887 if (dev->tt_info) in xhci_free_virt_device()
888 old_active_eps = dev->tt_info->active_eps; in xhci_free_virt_device()
891 if (dev->eps[i].ring) in xhci_free_virt_device()
892 xhci_ring_free(xhci, dev->eps[i].ring); in xhci_free_virt_device()
893 if (dev->eps[i].stream_info) in xhci_free_virt_device()
894 xhci_free_stream_info(xhci, in xhci_free_virt_device()
895 dev->eps[i].stream_info); in xhci_free_virt_device()
901 if (!list_empty(&dev->eps[i].bw_endpoint_list)) in xhci_free_virt_device()
902 xhci_warn(xhci, "Slot %u endpoint %u " in xhci_free_virt_device()
907 xhci_free_tt_info(xhci, dev, slot_id); in xhci_free_virt_device()
909 xhci_update_tt_active_eps(xhci, dev, old_active_eps); in xhci_free_virt_device()
911 if (dev->in_ctx) in xhci_free_virt_device()
912 xhci_free_container_ctx(xhci, dev->in_ctx); in xhci_free_virt_device()
913 if (dev->out_ctx) in xhci_free_virt_device()
914 xhci_free_container_ctx(xhci, dev->out_ctx); in xhci_free_virt_device()
916 if (dev->udev && dev->udev->slot_id) in xhci_free_virt_device()
917 dev->udev->slot_id = 0; in xhci_free_virt_device()
918 kfree(xhci->devs[slot_id]); in xhci_free_virt_device()
919 xhci->devs[slot_id] = NULL; in xhci_free_virt_device()
926 * We can't rely on udev at this point to find child-parent relationships.
928 static void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id) in xhci_free_virt_devices_depth_first() argument
935 vdev = xhci->devs[slot_id]; in xhci_free_virt_devices_depth_first()
939 if (vdev->real_port == 0 || in xhci_free_virt_devices_depth_first()
940 vdev->real_port > HCS_MAX_PORTS(xhci->hcs_params1)) { in xhci_free_virt_devices_depth_first()
941 xhci_dbg(xhci, "Bad vdev->real_port.\n"); in xhci_free_virt_devices_depth_first()
945 tt_list_head = &(xhci->rh_bw[vdev->real_port - 1].tts); in xhci_free_virt_devices_depth_first()
948 if (tt_info->slot_id == slot_id) { in xhci_free_virt_devices_depth_first()
950 for (i = 1; i < HCS_MAX_SLOTS(xhci->hcs_params1); i++) { in xhci_free_virt_devices_depth_first()
951 vdev = xhci->devs[i]; in xhci_free_virt_devices_depth_first()
952 if (vdev && (vdev->tt_info == tt_info)) in xhci_free_virt_devices_depth_first()
954 xhci, i); in xhci_free_virt_devices_depth_first()
960 xhci_debugfs_remove_slot(xhci, slot_id); in xhci_free_virt_devices_depth_first()
961 xhci_free_virt_device(xhci, slot_id); in xhci_free_virt_devices_depth_first()
964 int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id, in xhci_alloc_virt_device() argument
971 if (slot_id == 0 || xhci->devs[slot_id]) { in xhci_alloc_virt_device()
972 xhci_warn(xhci, "Bad Slot ID %d\n", slot_id); in xhci_alloc_virt_device()
980 dev->slot_id = slot_id; in xhci_alloc_virt_device()
983 dev->out_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_DEVICE, flags); in xhci_alloc_virt_device()
984 if (!dev->out_ctx) in xhci_alloc_virt_device()
987 xhci_dbg(xhci, "Slot %d output ctx = 0x%llx (dma)\n", slot_id, in xhci_alloc_virt_device()
988 (unsigned long long)dev->out_ctx->dma); in xhci_alloc_virt_device()
991 dev->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, flags); in xhci_alloc_virt_device()
992 if (!dev->in_ctx) in xhci_alloc_virt_device()
995 xhci_dbg(xhci, "Slot %d input ctx = 0x%llx (dma)\n", slot_id, in xhci_alloc_virt_device()
996 (unsigned long long)dev->in_ctx->dma); in xhci_alloc_virt_device()
1000 dev->eps[i].ep_index = i; in xhci_alloc_virt_device()
1001 dev->eps[i].vdev = dev; in xhci_alloc_virt_device()
1002 xhci_init_endpoint_timer(xhci, &dev->eps[i]); in xhci_alloc_virt_device()
1003 INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list); in xhci_alloc_virt_device()
1004 INIT_LIST_HEAD(&dev->eps[i].bw_endpoint_list); in xhci_alloc_virt_device()
1008 dev->eps[0].ring = xhci_ring_alloc(xhci, 2, 1, TYPE_CTRL, 0, flags); in xhci_alloc_virt_device()
1009 if (!dev->eps[0].ring) in xhci_alloc_virt_device()
1012 dev->udev = udev; in xhci_alloc_virt_device()
1015 xhci->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(dev->out_ctx->dma); in xhci_alloc_virt_device()
1016 xhci_dbg(xhci, "Set slot id %d dcbaa entry %p to 0x%llx\n", in xhci_alloc_virt_device()
1018 &xhci->dcbaa->dev_context_ptrs[slot_id], in xhci_alloc_virt_device()
1019 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[slot_id])); in xhci_alloc_virt_device()
1023 xhci->devs[slot_id] = dev; in xhci_alloc_virt_device()
1028 if (dev->in_ctx) in xhci_alloc_virt_device()
1029 xhci_free_container_ctx(xhci, dev->in_ctx); in xhci_alloc_virt_device()
1030 if (dev->out_ctx) in xhci_alloc_virt_device()
1031 xhci_free_container_ctx(xhci, dev->out_ctx); in xhci_alloc_virt_device()
1037 void xhci_copy_ep0_dequeue_into_input_ctx(struct xhci_hcd *xhci, in xhci_copy_ep0_dequeue_into_input_ctx() argument
1044 virt_dev = xhci->devs[udev->slot_id]; in xhci_copy_ep0_dequeue_into_input_ctx()
1045 ep0_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, 0); in xhci_copy_ep0_dequeue_into_input_ctx()
1046 ep_ring = virt_dev->eps[0].ring; in xhci_copy_ep0_dequeue_into_input_ctx()
1054 ep0_ctx->deq = cpu_to_le64(xhci_trb_virt_to_dma(ep_ring->enq_seg, in xhci_copy_ep0_dequeue_into_input_ctx()
1055 ep_ring->enqueue) in xhci_copy_ep0_dequeue_into_input_ctx()
1056 | ep_ring->cycle_state); in xhci_copy_ep0_dequeue_into_input_ctx()
1060 * The xHCI roothub may have ports of differing speeds in any order in the port
1063 * The xHCI hardware wants to know the roothub port number that the USB device
1069 static u32 xhci_find_real_port_number(struct xhci_hcd *xhci, in xhci_find_real_port_number() argument
1075 if (udev->speed >= USB_SPEED_SUPER) in xhci_find_real_port_number()
1076 hcd = xhci->shared_hcd; in xhci_find_real_port_number()
1078 hcd = xhci->main_hcd; in xhci_find_real_port_number()
1080 for (top_dev = udev; top_dev->parent && top_dev->parent->parent; in xhci_find_real_port_number()
1081 top_dev = top_dev->parent) in xhci_find_real_port_number()
1084 return xhci_find_raw_port_number(hcd, top_dev->portnum); in xhci_find_real_port_number()
1087 /* Setup an xHCI virtual device for a Set Address command */
1088 int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev) in xhci_setup_addressable_virt_dev() argument
1097 dev = xhci->devs[udev->slot_id]; in xhci_setup_addressable_virt_dev()
1099 if (udev->slot_id == 0 || !dev) { in xhci_setup_addressable_virt_dev()
1100 xhci_warn(xhci, "Slot ID %d is not assigned to this device\n", in xhci_setup_addressable_virt_dev()
1101 udev->slot_id); in xhci_setup_addressable_virt_dev()
1102 return -EINVAL; in xhci_setup_addressable_virt_dev()
1104 ep0_ctx = xhci_get_ep_ctx(xhci, dev->in_ctx, 0); in xhci_setup_addressable_virt_dev()
1105 slot_ctx = xhci_get_slot_ctx(xhci, dev->in_ctx); in xhci_setup_addressable_virt_dev()
1107 /* 3) Only the control endpoint is valid - one endpoint context */ in xhci_setup_addressable_virt_dev()
1108 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1) | udev->route); in xhci_setup_addressable_virt_dev()
1109 switch (udev->speed) { in xhci_setup_addressable_virt_dev()
1111 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SSP); in xhci_setup_addressable_virt_dev()
1115 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS); in xhci_setup_addressable_virt_dev()
1119 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS); in xhci_setup_addressable_virt_dev()
1122 /* USB core guesses at a 64-byte max packet first for FS devices */ in xhci_setup_addressable_virt_dev()
1124 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS); in xhci_setup_addressable_virt_dev()
1128 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS); in xhci_setup_addressable_virt_dev()
1132 xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n"); in xhci_setup_addressable_virt_dev()
1133 return -EINVAL; in xhci_setup_addressable_virt_dev()
1136 return -EINVAL; in xhci_setup_addressable_virt_dev()
1139 port_num = xhci_find_real_port_number(xhci, udev); in xhci_setup_addressable_virt_dev()
1141 return -EINVAL; in xhci_setup_addressable_virt_dev()
1142 slot_ctx->dev_info2 |= cpu_to_le32(ROOT_HUB_PORT(port_num)); in xhci_setup_addressable_virt_dev()
1144 for (top_dev = udev; top_dev->parent && top_dev->parent->parent; in xhci_setup_addressable_virt_dev()
1145 top_dev = top_dev->parent) in xhci_setup_addressable_virt_dev()
1147 dev->fake_port = top_dev->portnum; in xhci_setup_addressable_virt_dev()
1148 dev->real_port = port_num; in xhci_setup_addressable_virt_dev()
1149 xhci_dbg(xhci, "Set root hub portnum to %d\n", port_num); in xhci_setup_addressable_virt_dev()
1150 xhci_dbg(xhci, "Set fake root hub portnum to %d\n", dev->fake_port); in xhci_setup_addressable_virt_dev()
1158 if (!udev->tt || !udev->tt->hub->parent) { in xhci_setup_addressable_virt_dev()
1159 dev->bw_table = &xhci->rh_bw[port_num - 1].bw_table; in xhci_setup_addressable_virt_dev()
1164 rh_bw = &xhci->rh_bw[port_num - 1]; in xhci_setup_addressable_virt_dev()
1166 list_for_each_entry(tt_bw, &rh_bw->tts, tt_list) { in xhci_setup_addressable_virt_dev()
1167 if (tt_bw->slot_id != udev->tt->hub->slot_id) in xhci_setup_addressable_virt_dev()
1170 if (!dev->udev->tt->multi || in xhci_setup_addressable_virt_dev()
1171 (udev->tt->multi && in xhci_setup_addressable_virt_dev()
1172 tt_bw->ttport == dev->udev->ttport)) { in xhci_setup_addressable_virt_dev()
1173 dev->bw_table = &tt_bw->bw_table; in xhci_setup_addressable_virt_dev()
1174 dev->tt_info = tt_bw; in xhci_setup_addressable_virt_dev()
1178 if (!dev->tt_info) in xhci_setup_addressable_virt_dev()
1179 xhci_warn(xhci, "WARN: Didn't find a matching TT\n"); in xhci_setup_addressable_virt_dev()
1183 if (udev->tt && udev->tt->hub->parent) { in xhci_setup_addressable_virt_dev()
1184 slot_ctx->tt_info = cpu_to_le32(udev->tt->hub->slot_id | in xhci_setup_addressable_virt_dev()
1185 (udev->ttport << 8)); in xhci_setup_addressable_virt_dev()
1186 if (udev->tt->multi) in xhci_setup_addressable_virt_dev()
1187 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT); in xhci_setup_addressable_virt_dev()
1189 xhci_dbg(xhci, "udev->tt = %p\n", udev->tt); in xhci_setup_addressable_virt_dev()
1190 xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport); in xhci_setup_addressable_virt_dev()
1192 /* Step 4 - ring already allocated */ in xhci_setup_addressable_virt_dev()
1194 ep0_ctx->ep_info2 = cpu_to_le32(EP_TYPE(CTRL_EP)); in xhci_setup_addressable_virt_dev()
1197 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(0) | ERROR_COUNT(3) | in xhci_setup_addressable_virt_dev()
1200 ep0_ctx->deq = cpu_to_le64(dev->eps[0].ring->first_seg->dma | in xhci_setup_addressable_virt_dev()
1201 dev->eps[0].ring->cycle_state); in xhci_setup_addressable_virt_dev()
1211 * Convert interval expressed as 2^(bInterval - 1) == interval into
1220 interval = clamp_val(ep->desc.bInterval, 1, 16) - 1; in xhci_parse_exponent_interval()
1221 if (interval != ep->desc.bInterval - 1) in xhci_parse_exponent_interval()
1222 dev_warn(&udev->dev, in xhci_parse_exponent_interval()
1223 "ep %#x - rounding interval to %d %sframes\n", in xhci_parse_exponent_interval()
1224 ep->desc.bEndpointAddress, in xhci_parse_exponent_interval()
1226 udev->speed == USB_SPEED_FULL ? "" : "micro"); in xhci_parse_exponent_interval()
1228 if (udev->speed == USB_SPEED_FULL) { in xhci_parse_exponent_interval()
1241 * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
1250 interval = fls(desc_interval) - 1; in xhci_microframes_to_exponent()
1253 dev_dbg(&udev->dev, in xhci_microframes_to_exponent()
1254 "ep %#x - rounding interval to %d microframes, ep desc says %d microframes\n", in xhci_microframes_to_exponent()
1255 ep->desc.bEndpointAddress, in xhci_microframes_to_exponent()
1265 if (ep->desc.bInterval == 0) in xhci_parse_microframe_interval()
1268 ep->desc.bInterval, 0, 15); in xhci_parse_microframe_interval()
1276 ep->desc.bInterval * 8, 3, 10); in xhci_parse_frame_interval()
1281 * The polling interval is expressed in "microframes". If xHCI's Interval field
1292 switch (udev->speed) { in xhci_get_endpoint_interval()
1295 if (usb_endpoint_xfer_control(&ep->desc) || in xhci_get_endpoint_interval()
1296 usb_endpoint_xfer_bulk(&ep->desc)) { in xhci_get_endpoint_interval()
1304 if (usb_endpoint_xfer_int(&ep->desc) || in xhci_get_endpoint_interval()
1305 usb_endpoint_xfer_isoc(&ep->desc)) { in xhci_get_endpoint_interval()
1311 if (usb_endpoint_xfer_isoc(&ep->desc)) { in xhci_get_endpoint_interval()
1323 if (usb_endpoint_xfer_int(&ep->desc) || in xhci_get_endpoint_interval()
1324 usb_endpoint_xfer_isoc(&ep->desc)) { in xhci_get_endpoint_interval()
1344 if (udev->speed < USB_SPEED_SUPER || in xhci_get_endpoint_mult()
1345 !usb_endpoint_xfer_isoc(&ep->desc)) in xhci_get_endpoint_mult()
1347 return ep->ss_ep_comp.bmAttributes; in xhci_get_endpoint_mult()
1354 if (udev->speed >= USB_SPEED_SUPER) in xhci_get_endpoint_max_burst()
1355 return ep->ss_ep_comp.bMaxBurst; in xhci_get_endpoint_max_burst()
1357 if (udev->speed == USB_SPEED_HIGH && in xhci_get_endpoint_max_burst()
1358 (usb_endpoint_xfer_isoc(&ep->desc) || in xhci_get_endpoint_max_burst()
1359 usb_endpoint_xfer_int(&ep->desc))) in xhci_get_endpoint_max_burst()
1360 return usb_endpoint_maxp_mult(&ep->desc) - 1; in xhci_get_endpoint_max_burst()
1369 in = usb_endpoint_dir_in(&ep->desc); in xhci_get_endpoint_type()
1371 switch (usb_endpoint_type(&ep->desc)) { in xhci_get_endpoint_type()
1395 if (usb_endpoint_xfer_control(&ep->desc) || in xhci_get_max_esit_payload()
1396 usb_endpoint_xfer_bulk(&ep->desc)) in xhci_get_max_esit_payload()
1400 if ((udev->speed >= USB_SPEED_SUPER_PLUS) && in xhci_get_max_esit_payload()
1401 USB_SS_SSP_ISOC_COMP(ep->ss_ep_comp.bmAttributes)) in xhci_get_max_esit_payload()
1402 return le32_to_cpu(ep->ssp_isoc_ep_comp.dwBytesPerInterval); in xhci_get_max_esit_payload()
1404 else if (udev->speed >= USB_SPEED_SUPER) in xhci_get_max_esit_payload()
1405 return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval); in xhci_get_max_esit_payload()
1407 max_packet = usb_endpoint_maxp(&ep->desc); in xhci_get_max_esit_payload()
1408 max_burst = usb_endpoint_maxp_mult(&ep->desc); in xhci_get_max_esit_payload()
1416 int xhci_endpoint_init(struct xhci_hcd *xhci, in xhci_endpoint_init() argument
1435 ep_index = xhci_get_endpoint_index(&ep->desc); in xhci_endpoint_init()
1436 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index); in xhci_endpoint_init()
1440 return -EINVAL; in xhci_endpoint_init()
1442 ring_type = usb_endpoint_type(&ep->desc); in xhci_endpoint_init()
1448 * set it to max available. See xHCI 1.1 spec 4.14.1.1 for details. in xhci_endpoint_init()
1454 if (usb_endpoint_xfer_int(&ep->desc) || in xhci_endpoint_init()
1455 usb_endpoint_xfer_isoc(&ep->desc)) { in xhci_endpoint_init()
1456 if ((xhci->quirks & XHCI_LIMIT_ENDPOINT_INTERVAL_7) && in xhci_endpoint_init()
1457 udev->speed >= USB_SPEED_HIGH && in xhci_endpoint_init()
1464 max_packet = usb_endpoint_maxp(&ep->desc); in xhci_endpoint_init()
1471 if (!usb_endpoint_xfer_isoc(&ep->desc)) in xhci_endpoint_init()
1474 if (usb_endpoint_xfer_bulk(&ep->desc)) { in xhci_endpoint_init()
1475 if (udev->speed == USB_SPEED_HIGH) in xhci_endpoint_init()
1477 if (udev->speed == USB_SPEED_FULL) { in xhci_endpoint_init()
1482 /* xHCI 1.0 and 1.1 indicates that ctrl ep avg TRB Length should be 8 */ in xhci_endpoint_init()
1483 if (usb_endpoint_xfer_control(&ep->desc) && xhci->hci_version >= 0x100) in xhci_endpoint_init()
1485 /* xhci 1.1 with LEC support doesn't use mult field, use RsvdZ */ in xhci_endpoint_init()
1486 if ((xhci->hci_version > 0x100) && HCC2_LEC(xhci->hcc_params2)) in xhci_endpoint_init()
1490 virt_dev->eps[ep_index].new_ring = in xhci_endpoint_init()
1491 xhci_ring_alloc(xhci, 2, 1, ring_type, max_packet, mem_flags); in xhci_endpoint_init()
1492 if (!virt_dev->eps[ep_index].new_ring) in xhci_endpoint_init()
1493 return -ENOMEM; in xhci_endpoint_init()
1495 virt_dev->eps[ep_index].skip = false; in xhci_endpoint_init()
1496 ep_ring = virt_dev->eps[ep_index].new_ring; in xhci_endpoint_init()
1499 ep_ctx->ep_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) | in xhci_endpoint_init()
1502 ep_ctx->ep_info2 = cpu_to_le32(EP_TYPE(endpoint_type) | in xhci_endpoint_init()
1506 ep_ctx->deq = cpu_to_le64(ep_ring->first_seg->dma | in xhci_endpoint_init()
1507 ep_ring->cycle_state); in xhci_endpoint_init()
1509 ep_ctx->tx_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) | in xhci_endpoint_init()
1515 void xhci_endpoint_zero(struct xhci_hcd *xhci, in xhci_endpoint_zero() argument
1522 ep_index = xhci_get_endpoint_index(&ep->desc); in xhci_endpoint_zero()
1523 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index); in xhci_endpoint_zero()
1525 ep_ctx->ep_info = 0; in xhci_endpoint_zero()
1526 ep_ctx->ep_info2 = 0; in xhci_endpoint_zero()
1527 ep_ctx->deq = 0; in xhci_endpoint_zero()
1528 ep_ctx->tx_info = 0; in xhci_endpoint_zero()
1536 bw_info->ep_interval = 0; in xhci_clear_endpoint_bw_info()
1537 bw_info->mult = 0; in xhci_clear_endpoint_bw_info()
1538 bw_info->num_packets = 0; in xhci_clear_endpoint_bw_info()
1539 bw_info->max_packet_size = 0; in xhci_clear_endpoint_bw_info()
1540 bw_info->type = 0; in xhci_clear_endpoint_bw_info()
1541 bw_info->max_esit_payload = 0; in xhci_clear_endpoint_bw_info()
1544 void xhci_update_bw_info(struct xhci_hcd *xhci, in xhci_update_bw_info() argument
1555 bw_info = &virt_dev->eps[i].bw_info; in xhci_update_bw_info()
1558 * unconditionally clearing the bandwidth info for non-periodic in xhci_update_bw_info()
1569 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, i); in xhci_update_bw_info()
1570 ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2)); in xhci_update_bw_info()
1572 /* Ignore non-periodic endpoints */ in xhci_update_bw_info()
1579 bw_info->ep_interval = CTX_TO_EP_INTERVAL( in xhci_update_bw_info()
1580 le32_to_cpu(ep_ctx->ep_info)); in xhci_update_bw_info()
1581 /* Number of packets and mult are zero-based in the in xhci_update_bw_info()
1582 * input context, but we want one-based for the in xhci_update_bw_info()
1585 bw_info->mult = CTX_TO_EP_MULT( in xhci_update_bw_info()
1586 le32_to_cpu(ep_ctx->ep_info)) + 1; in xhci_update_bw_info()
1587 bw_info->num_packets = CTX_TO_MAX_BURST( in xhci_update_bw_info()
1588 le32_to_cpu(ep_ctx->ep_info2)) + 1; in xhci_update_bw_info()
1589 bw_info->max_packet_size = MAX_PACKET_DECODED( in xhci_update_bw_info()
1590 le32_to_cpu(ep_ctx->ep_info2)); in xhci_update_bw_info()
1591 bw_info->type = ep_type; in xhci_update_bw_info()
1592 bw_info->max_esit_payload = CTX_TO_MAX_ESIT_PAYLOAD( in xhci_update_bw_info()
1593 le32_to_cpu(ep_ctx->tx_info)); in xhci_update_bw_info()
1602 void xhci_endpoint_copy(struct xhci_hcd *xhci, in xhci_endpoint_copy() argument
1610 out_ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); in xhci_endpoint_copy()
1611 in_ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index); in xhci_endpoint_copy()
1613 in_ep_ctx->ep_info = out_ep_ctx->ep_info; in xhci_endpoint_copy()
1614 in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2; in xhci_endpoint_copy()
1615 in_ep_ctx->deq = out_ep_ctx->deq; in xhci_endpoint_copy()
1616 in_ep_ctx->tx_info = out_ep_ctx->tx_info; in xhci_endpoint_copy()
1617 if (xhci->quirks & XHCI_MTK_HOST) { in xhci_endpoint_copy()
1618 in_ep_ctx->reserved[0] = out_ep_ctx->reserved[0]; in xhci_endpoint_copy()
1619 in_ep_ctx->reserved[1] = out_ep_ctx->reserved[1]; in xhci_endpoint_copy()
1628 void xhci_slot_copy(struct xhci_hcd *xhci, in xhci_slot_copy() argument
1635 in_slot_ctx = xhci_get_slot_ctx(xhci, in_ctx); in xhci_slot_copy()
1636 out_slot_ctx = xhci_get_slot_ctx(xhci, out_ctx); in xhci_slot_copy()
1638 in_slot_ctx->dev_info = out_slot_ctx->dev_info; in xhci_slot_copy()
1639 in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2; in xhci_slot_copy()
1640 in_slot_ctx->tt_info = out_slot_ctx->tt_info; in xhci_slot_copy()
1641 in_slot_ctx->dev_state = out_slot_ctx->dev_state; in xhci_slot_copy()
1645 static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags) in scratchpad_alloc() argument
1648 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in scratchpad_alloc()
1649 int num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2); in scratchpad_alloc()
1651 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in scratchpad_alloc()
1657 xhci->scratchpad = kzalloc_node(sizeof(*xhci->scratchpad), flags, in scratchpad_alloc()
1659 if (!xhci->scratchpad) in scratchpad_alloc()
1662 xhci->scratchpad->sp_array = dma_alloc_coherent(dev, in scratchpad_alloc()
1664 &xhci->scratchpad->sp_dma, flags); in scratchpad_alloc()
1665 if (!xhci->scratchpad->sp_array) in scratchpad_alloc()
1668 xhci->scratchpad->sp_buffers = kcalloc_node(num_sp, sizeof(void *), in scratchpad_alloc()
1670 if (!xhci->scratchpad->sp_buffers) in scratchpad_alloc()
1673 xhci->dcbaa->dev_context_ptrs[0] = cpu_to_le64(xhci->scratchpad->sp_dma); in scratchpad_alloc()
1676 void *buf = dma_alloc_coherent(dev, xhci->page_size, &dma, in scratchpad_alloc()
1681 xhci->scratchpad->sp_array[i] = dma; in scratchpad_alloc()
1682 xhci->scratchpad->sp_buffers[i] = buf; in scratchpad_alloc()
1688 for (i = i - 1; i >= 0; i--) { in scratchpad_alloc()
1689 dma_free_coherent(dev, xhci->page_size, in scratchpad_alloc()
1690 xhci->scratchpad->sp_buffers[i], in scratchpad_alloc()
1691 xhci->scratchpad->sp_array[i]); in scratchpad_alloc()
1694 kfree(xhci->scratchpad->sp_buffers); in scratchpad_alloc()
1698 xhci->scratchpad->sp_array, in scratchpad_alloc()
1699 xhci->scratchpad->sp_dma); in scratchpad_alloc()
1702 kfree(xhci->scratchpad); in scratchpad_alloc()
1703 xhci->scratchpad = NULL; in scratchpad_alloc()
1706 return -ENOMEM; in scratchpad_alloc()
1709 static void scratchpad_free(struct xhci_hcd *xhci) in scratchpad_free() argument
1713 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in scratchpad_free()
1715 if (!xhci->scratchpad) in scratchpad_free()
1718 num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2); in scratchpad_free()
1721 dma_free_coherent(dev, xhci->page_size, in scratchpad_free()
1722 xhci->scratchpad->sp_buffers[i], in scratchpad_free()
1723 xhci->scratchpad->sp_array[i]); in scratchpad_free()
1725 kfree(xhci->scratchpad->sp_buffers); in scratchpad_free()
1727 xhci->scratchpad->sp_array, in scratchpad_free()
1728 xhci->scratchpad->sp_dma); in scratchpad_free()
1729 kfree(xhci->scratchpad); in scratchpad_free()
1730 xhci->scratchpad = NULL; in scratchpad_free()
1733 struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci, in xhci_alloc_command() argument
1737 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_alloc_command()
1744 command->completion = in xhci_alloc_command()
1747 if (!command->completion) { in xhci_alloc_command()
1751 init_completion(command->completion); in xhci_alloc_command()
1754 command->status = 0; in xhci_alloc_command()
1755 INIT_LIST_HEAD(&command->cmd_list); in xhci_alloc_command()
1759 struct xhci_command *xhci_alloc_command_with_ctx(struct xhci_hcd *xhci, in xhci_alloc_command_with_ctx() argument
1764 command = xhci_alloc_command(xhci, allocate_completion, mem_flags); in xhci_alloc_command_with_ctx()
1768 command->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, in xhci_alloc_command_with_ctx()
1770 if (!command->in_ctx) { in xhci_alloc_command_with_ctx()
1771 kfree(command->completion); in xhci_alloc_command_with_ctx()
1783 void xhci_free_command(struct xhci_hcd *xhci, in xhci_free_command() argument
1786 xhci_free_container_ctx(xhci, in xhci_free_command()
1787 command->in_ctx); in xhci_free_command()
1788 kfree(command->completion); in xhci_free_command()
1792 int xhci_alloc_erst(struct xhci_hcd *xhci, in xhci_alloc_erst() argument
1802 size = sizeof(struct xhci_erst_entry) * evt_ring->num_segs; in xhci_alloc_erst()
1803 erst->entries = dma_alloc_coherent(xhci_to_hcd(xhci)->self.sysdev, in xhci_alloc_erst()
1804 size, &erst->erst_dma_addr, flags); in xhci_alloc_erst()
1805 if (!erst->entries) in xhci_alloc_erst()
1806 return -ENOMEM; in xhci_alloc_erst()
1808 erst->num_entries = evt_ring->num_segs; in xhci_alloc_erst()
1810 seg = evt_ring->first_seg; in xhci_alloc_erst()
1811 for (val = 0; val < evt_ring->num_segs; val++) { in xhci_alloc_erst()
1812 entry = &erst->entries[val]; in xhci_alloc_erst()
1813 entry->seg_addr = cpu_to_le64(seg->dma); in xhci_alloc_erst()
1814 entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT); in xhci_alloc_erst()
1815 entry->rsvd = 0; in xhci_alloc_erst()
1816 seg = seg->next; in xhci_alloc_erst()
1822 void xhci_free_erst(struct xhci_hcd *xhci, struct xhci_erst *erst) in xhci_free_erst() argument
1825 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_free_erst()
1827 size = sizeof(struct xhci_erst_entry) * (erst->num_entries); in xhci_free_erst()
1828 if (erst->entries) in xhci_free_erst()
1830 erst->entries, in xhci_free_erst()
1831 erst->erst_dma_addr); in xhci_free_erst()
1832 erst->entries = NULL; in xhci_free_erst()
1835 void xhci_mem_cleanup(struct xhci_hcd *xhci) in xhci_mem_cleanup() argument
1837 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_mem_cleanup()
1840 cancel_delayed_work_sync(&xhci->cmd_timer); in xhci_mem_cleanup()
1842 xhci_free_erst(xhci, &xhci->erst); in xhci_mem_cleanup()
1844 if (xhci->event_ring) in xhci_mem_cleanup()
1845 xhci_ring_free(xhci, xhci->event_ring); in xhci_mem_cleanup()
1846 xhci->event_ring = NULL; in xhci_mem_cleanup()
1847 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed event ring"); in xhci_mem_cleanup()
1849 if (xhci->lpm_command) in xhci_mem_cleanup()
1850 xhci_free_command(xhci, xhci->lpm_command); in xhci_mem_cleanup()
1851 xhci->lpm_command = NULL; in xhci_mem_cleanup()
1852 if (xhci->cmd_ring) in xhci_mem_cleanup()
1853 xhci_ring_free(xhci, xhci->cmd_ring); in xhci_mem_cleanup()
1854 xhci->cmd_ring = NULL; in xhci_mem_cleanup()
1855 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed command ring"); in xhci_mem_cleanup()
1856 xhci_cleanup_command_queue(xhci); in xhci_mem_cleanup()
1858 num_ports = HCS_MAX_PORTS(xhci->hcs_params1); in xhci_mem_cleanup()
1859 for (i = 0; i < num_ports && xhci->rh_bw; i++) { in xhci_mem_cleanup()
1860 struct xhci_interval_bw_table *bwt = &xhci->rh_bw[i].bw_table; in xhci_mem_cleanup()
1862 struct list_head *ep = &bwt->interval_bw[j].endpoints; in xhci_mem_cleanup()
1864 list_del_init(ep->next); in xhci_mem_cleanup()
1868 for (i = HCS_MAX_SLOTS(xhci->hcs_params1); i > 0; i--) in xhci_mem_cleanup()
1869 xhci_free_virt_devices_depth_first(xhci, i); in xhci_mem_cleanup()
1871 dma_pool_destroy(xhci->segment_pool); in xhci_mem_cleanup()
1872 xhci->segment_pool = NULL; in xhci_mem_cleanup()
1873 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed segment pool"); in xhci_mem_cleanup()
1875 dma_pool_destroy(xhci->device_pool); in xhci_mem_cleanup()
1876 xhci->device_pool = NULL; in xhci_mem_cleanup()
1877 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed device context pool"); in xhci_mem_cleanup()
1879 dma_pool_destroy(xhci->small_streams_pool); in xhci_mem_cleanup()
1880 xhci->small_streams_pool = NULL; in xhci_mem_cleanup()
1881 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_cleanup()
1884 dma_pool_destroy(xhci->medium_streams_pool); in xhci_mem_cleanup()
1885 xhci->medium_streams_pool = NULL; in xhci_mem_cleanup()
1886 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_cleanup()
1889 if (xhci->dcbaa) in xhci_mem_cleanup()
1890 dma_free_coherent(dev, sizeof(*xhci->dcbaa), in xhci_mem_cleanup()
1891 xhci->dcbaa, xhci->dcbaa->dma); in xhci_mem_cleanup()
1892 xhci->dcbaa = NULL; in xhci_mem_cleanup()
1894 scratchpad_free(xhci); in xhci_mem_cleanup()
1896 if (!xhci->rh_bw) in xhci_mem_cleanup()
1901 list_for_each_entry_safe(tt, n, &xhci->rh_bw[i].tts, tt_list) { in xhci_mem_cleanup()
1902 list_del(&tt->tt_list); in xhci_mem_cleanup()
1908 xhci->cmd_ring_reserved_trbs = 0; in xhci_mem_cleanup()
1909 xhci->usb2_rhub.num_ports = 0; in xhci_mem_cleanup()
1910 xhci->usb3_rhub.num_ports = 0; in xhci_mem_cleanup()
1911 xhci->num_active_eps = 0; in xhci_mem_cleanup()
1912 kfree(xhci->usb2_rhub.ports); in xhci_mem_cleanup()
1913 kfree(xhci->usb3_rhub.ports); in xhci_mem_cleanup()
1914 kfree(xhci->hw_ports); in xhci_mem_cleanup()
1915 kfree(xhci->rh_bw); in xhci_mem_cleanup()
1916 kfree(xhci->ext_caps); in xhci_mem_cleanup()
1917 for (i = 0; i < xhci->num_port_caps; i++) in xhci_mem_cleanup()
1918 kfree(xhci->port_caps[i].psi); in xhci_mem_cleanup()
1919 kfree(xhci->port_caps); in xhci_mem_cleanup()
1920 xhci->num_port_caps = 0; in xhci_mem_cleanup()
1922 xhci->usb2_rhub.ports = NULL; in xhci_mem_cleanup()
1923 xhci->usb3_rhub.ports = NULL; in xhci_mem_cleanup()
1924 xhci->hw_ports = NULL; in xhci_mem_cleanup()
1925 xhci->rh_bw = NULL; in xhci_mem_cleanup()
1926 xhci->ext_caps = NULL; in xhci_mem_cleanup()
1927 xhci->port_caps = NULL; in xhci_mem_cleanup()
1929 xhci->page_size = 0; in xhci_mem_cleanup()
1930 xhci->page_shift = 0; in xhci_mem_cleanup()
1931 xhci->usb2_rhub.bus_state.bus_suspended = 0; in xhci_mem_cleanup()
1932 xhci->usb3_rhub.bus_state.bus_suspended = 0; in xhci_mem_cleanup()
1935 static int xhci_test_trb_in_td(struct xhci_hcd *xhci, in xhci_test_trb_in_td() argument
1950 seg = trb_in_td(xhci, input_seg, start_trb, end_trb, input_dma, false); in xhci_test_trb_in_td()
1952 xhci_warn(xhci, "WARN: %s TRB math test %d failed!\n", in xhci_test_trb_in_td()
1954 xhci_warn(xhci, "Tested TRB math w/ seg %p and " in xhci_test_trb_in_td()
1958 xhci_warn(xhci, "starting TRB %p (0x%llx DMA), " in xhci_test_trb_in_td()
1962 xhci_warn(xhci, "Expected seg %p, got seg %p\n", in xhci_test_trb_in_td()
1964 trb_in_td(xhci, input_seg, start_trb, end_trb, input_dma, in xhci_test_trb_in_td()
1966 return -1; in xhci_test_trb_in_td()
1972 static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci) in xhci_check_trb_in_td_math() argument
1981 { xhci->event_ring->first_seg->dma - 16, NULL }, in xhci_check_trb_in_td_math()
1983 { xhci->event_ring->first_seg->dma - 1, NULL }, in xhci_check_trb_in_td_math()
1985 { xhci->event_ring->first_seg->dma, xhci->event_ring->first_seg }, in xhci_check_trb_in_td_math()
1987 { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16, in xhci_check_trb_in_td_math()
1988 xhci->event_ring->first_seg }, in xhci_check_trb_in_td_math()
1990 { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16 + 1, NULL }, in xhci_check_trb_in_td_math()
1992 { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT)*16, NULL }, in xhci_check_trb_in_td_math()
2004 { .input_seg = xhci->event_ring->first_seg, in xhci_check_trb_in_td_math()
2005 .start_trb = xhci->event_ring->first_seg->trbs, in xhci_check_trb_in_td_math()
2006 .end_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1], in xhci_check_trb_in_td_math()
2007 .input_dma = xhci->cmd_ring->first_seg->dma, in xhci_check_trb_in_td_math()
2011 { .input_seg = xhci->event_ring->first_seg, in xhci_check_trb_in_td_math()
2012 .start_trb = xhci->event_ring->first_seg->trbs, in xhci_check_trb_in_td_math()
2013 .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1], in xhci_check_trb_in_td_math()
2014 .input_dma = xhci->cmd_ring->first_seg->dma, in xhci_check_trb_in_td_math()
2018 { .input_seg = xhci->event_ring->first_seg, in xhci_check_trb_in_td_math()
2019 .start_trb = xhci->cmd_ring->first_seg->trbs, in xhci_check_trb_in_td_math()
2020 .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1], in xhci_check_trb_in_td_math()
2021 .input_dma = xhci->cmd_ring->first_seg->dma, in xhci_check_trb_in_td_math()
2025 { .input_seg = xhci->event_ring->first_seg, in xhci_check_trb_in_td_math()
2026 .start_trb = &xhci->event_ring->first_seg->trbs[0], in xhci_check_trb_in_td_math()
2027 .end_trb = &xhci->event_ring->first_seg->trbs[3], in xhci_check_trb_in_td_math()
2028 .input_dma = xhci->event_ring->first_seg->dma + 4*16, in xhci_check_trb_in_td_math()
2032 { .input_seg = xhci->event_ring->first_seg, in xhci_check_trb_in_td_math()
2033 .start_trb = &xhci->event_ring->first_seg->trbs[3], in xhci_check_trb_in_td_math()
2034 .end_trb = &xhci->event_ring->first_seg->trbs[6], in xhci_check_trb_in_td_math()
2035 .input_dma = xhci->event_ring->first_seg->dma + 2*16, in xhci_check_trb_in_td_math()
2039 { .input_seg = xhci->event_ring->first_seg, in xhci_check_trb_in_td_math()
2040 .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3], in xhci_check_trb_in_td_math()
2041 .end_trb = &xhci->event_ring->first_seg->trbs[1], in xhci_check_trb_in_td_math()
2042 .input_dma = xhci->event_ring->first_seg->dma + 2*16, in xhci_check_trb_in_td_math()
2046 { .input_seg = xhci->event_ring->first_seg, in xhci_check_trb_in_td_math()
2047 .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3], in xhci_check_trb_in_td_math()
2048 .end_trb = &xhci->event_ring->first_seg->trbs[1], in xhci_check_trb_in_td_math()
2049 .input_dma = xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 4)*16, in xhci_check_trb_in_td_math()
2053 { .input_seg = xhci->event_ring->first_seg, in xhci_check_trb_in_td_math()
2054 .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3], in xhci_check_trb_in_td_math()
2055 .end_trb = &xhci->event_ring->first_seg->trbs[1], in xhci_check_trb_in_td_math()
2056 .input_dma = xhci->cmd_ring->first_seg->dma + 2*16, in xhci_check_trb_in_td_math()
2066 ret = xhci_test_trb_in_td(xhci, in xhci_check_trb_in_td_math()
2067 xhci->event_ring->first_seg, in xhci_check_trb_in_td_math()
2068 xhci->event_ring->first_seg->trbs, in xhci_check_trb_in_td_math()
2069 &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1], in xhci_check_trb_in_td_math()
2079 ret = xhci_test_trb_in_td(xhci, in xhci_check_trb_in_td_math()
2089 xhci_dbg(xhci, "TRB math tests passed.\n"); in xhci_check_trb_in_td_math()
2093 static void xhci_set_hc_event_deq(struct xhci_hcd *xhci) in xhci_set_hc_event_deq() argument
2098 deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, in xhci_set_hc_event_deq()
2099 xhci->event_ring->dequeue); in xhci_set_hc_event_deq()
2101 xhci_warn(xhci, "WARN something wrong with SW event ring " in xhci_set_hc_event_deq()
2104 temp = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); in xhci_set_hc_event_deq()
2110 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_set_hc_event_deq()
2113 xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK) | temp, in xhci_set_hc_event_deq()
2114 &xhci->ir_set->erst_dequeue); in xhci_set_hc_event_deq()
2117 static void xhci_add_in_port(struct xhci_hcd *xhci, unsigned int num_ports, in xhci_add_in_port() argument
2124 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_add_in_port()
2132 rhub = &xhci->usb3_rhub; in xhci_add_in_port()
2134 * Some hosts incorrectly use sub-minor version for minor in xhci_add_in_port()
2143 rhub = &xhci->usb2_rhub; in xhci_add_in_port()
2145 xhci_warn(xhci, "Ignoring unknown port speed, " in xhci_add_in_port()
2151 rhub->maj_rev = XHCI_EXT_PORT_MAJOR(temp); in xhci_add_in_port()
2153 if (rhub->min_rev < minor_revision) in xhci_add_in_port()
2154 rhub->min_rev = minor_revision; in xhci_add_in_port()
2160 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_add_in_port()
2165 if (port_offset == 0 || (port_offset + port_count - 1) > num_ports) in xhci_add_in_port()
2169 port_cap = &xhci->port_caps[xhci->num_port_caps++]; in xhci_add_in_port()
2170 if (xhci->num_port_caps > max_caps) in xhci_add_in_port()
2173 port_cap->maj_rev = major_revision; in xhci_add_in_port()
2174 port_cap->min_rev = minor_revision; in xhci_add_in_port()
2175 port_cap->psi_count = XHCI_EXT_PORT_PSIC(temp); in xhci_add_in_port()
2177 if (port_cap->psi_count) { in xhci_add_in_port()
2178 port_cap->psi = kcalloc_node(port_cap->psi_count, in xhci_add_in_port()
2179 sizeof(*port_cap->psi), in xhci_add_in_port()
2181 if (!port_cap->psi) in xhci_add_in_port()
2182 port_cap->psi_count = 0; in xhci_add_in_port()
2184 port_cap->psi_uid_count++; in xhci_add_in_port()
2185 for (i = 0; i < port_cap->psi_count; i++) { in xhci_add_in_port()
2186 port_cap->psi[i] = readl(addr + 4 + i); in xhci_add_in_port()
2191 if (i && (XHCI_EXT_PORT_PSIV(port_cap->psi[i]) != in xhci_add_in_port()
2192 XHCI_EXT_PORT_PSIV(port_cap->psi[i - 1]))) in xhci_add_in_port()
2193 port_cap->psi_uid_count++; in xhci_add_in_port()
2195 xhci_dbg(xhci, "PSIV:%d PSIE:%d PLT:%d PFD:%d LP:%d PSIM:%d\n", in xhci_add_in_port()
2196 XHCI_EXT_PORT_PSIV(port_cap->psi[i]), in xhci_add_in_port()
2197 XHCI_EXT_PORT_PSIE(port_cap->psi[i]), in xhci_add_in_port()
2198 XHCI_EXT_PORT_PLT(port_cap->psi[i]), in xhci_add_in_port()
2199 XHCI_EXT_PORT_PFD(port_cap->psi[i]), in xhci_add_in_port()
2200 XHCI_EXT_PORT_LP(port_cap->psi[i]), in xhci_add_in_port()
2201 XHCI_EXT_PORT_PSIM(port_cap->psi[i])); in xhci_add_in_port()
2205 if (major_revision < 0x03 && xhci->num_ext_caps < max_caps) in xhci_add_in_port()
2206 xhci->ext_caps[xhci->num_ext_caps++] = temp; in xhci_add_in_port()
2208 if ((xhci->hci_version >= 0x100) && (major_revision != 0x03) && in xhci_add_in_port()
2210 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_add_in_port()
2211 "xHCI 1.0: support USB2 hardware lpm"); in xhci_add_in_port()
2212 xhci->hw_lpm_support = 1; in xhci_add_in_port()
2215 port_offset--; in xhci_add_in_port()
2217 struct xhci_port *hw_port = &xhci->hw_ports[i]; in xhci_add_in_port()
2219 if (hw_port->rhub) { in xhci_add_in_port()
2220 xhci_warn(xhci, "Duplicate port entry, Ext Cap %p," in xhci_add_in_port()
2222 xhci_warn(xhci, "Port was marked as USB %u, " in xhci_add_in_port()
2224 hw_port->rhub->maj_rev, major_revision); in xhci_add_in_port()
2228 if (hw_port->rhub != rhub && in xhci_add_in_port()
2229 hw_port->hcd_portnum != DUPLICATE_ENTRY) { in xhci_add_in_port()
2230 hw_port->rhub->num_ports--; in xhci_add_in_port()
2231 hw_port->hcd_portnum = DUPLICATE_ENTRY; in xhci_add_in_port()
2235 hw_port->rhub = rhub; in xhci_add_in_port()
2236 hw_port->port_cap = port_cap; in xhci_add_in_port()
2237 rhub->num_ports++; in xhci_add_in_port()
2242 static void xhci_create_rhub_port_array(struct xhci_hcd *xhci, in xhci_create_rhub_port_array() argument
2247 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_create_rhub_port_array()
2249 if (!rhub->num_ports) in xhci_create_rhub_port_array()
2251 rhub->ports = kcalloc_node(rhub->num_ports, sizeof(*rhub->ports), in xhci_create_rhub_port_array()
2253 if (!rhub->ports) in xhci_create_rhub_port_array()
2256 for (i = 0; i < HCS_MAX_PORTS(xhci->hcs_params1); i++) { in xhci_create_rhub_port_array()
2257 if (xhci->hw_ports[i].rhub != rhub || in xhci_create_rhub_port_array()
2258 xhci->hw_ports[i].hcd_portnum == DUPLICATE_ENTRY) in xhci_create_rhub_port_array()
2260 xhci->hw_ports[i].hcd_portnum = port_index; in xhci_create_rhub_port_array()
2261 rhub->ports[port_index] = &xhci->hw_ports[i]; in xhci_create_rhub_port_array()
2263 if (port_index == rhub->num_ports) in xhci_create_rhub_port_array()
2275 static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags) in xhci_setup_port_arrays() argument
2283 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_setup_port_arrays()
2285 num_ports = HCS_MAX_PORTS(xhci->hcs_params1); in xhci_setup_port_arrays()
2286 xhci->hw_ports = kcalloc_node(num_ports, sizeof(*xhci->hw_ports), in xhci_setup_port_arrays()
2288 if (!xhci->hw_ports) in xhci_setup_port_arrays()
2289 return -ENOMEM; in xhci_setup_port_arrays()
2292 xhci->hw_ports[i].addr = &xhci->op_regs->port_status_base + in xhci_setup_port_arrays()
2294 xhci->hw_ports[i].hw_portnum = i; in xhci_setup_port_arrays()
2297 xhci->rh_bw = kcalloc_node(num_ports, sizeof(*xhci->rh_bw), flags, in xhci_setup_port_arrays()
2299 if (!xhci->rh_bw) in xhci_setup_port_arrays()
2300 return -ENOMEM; in xhci_setup_port_arrays()
2304 INIT_LIST_HEAD(&xhci->rh_bw[i].tts); in xhci_setup_port_arrays()
2305 bw_table = &xhci->rh_bw[i].bw_table; in xhci_setup_port_arrays()
2307 INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints); in xhci_setup_port_arrays()
2309 base = &xhci->cap_regs->hc_capbase; in xhci_setup_port_arrays()
2313 xhci_err(xhci, "No Extended Capability registers, unable to set up roothub\n"); in xhci_setup_port_arrays()
2314 return -ENODEV; in xhci_setup_port_arrays()
2325 xhci->ext_caps = kcalloc_node(cap_count, sizeof(*xhci->ext_caps), in xhci_setup_port_arrays()
2327 if (!xhci->ext_caps) in xhci_setup_port_arrays()
2328 return -ENOMEM; in xhci_setup_port_arrays()
2330 xhci->port_caps = kcalloc_node(cap_count, sizeof(*xhci->port_caps), in xhci_setup_port_arrays()
2332 if (!xhci->port_caps) in xhci_setup_port_arrays()
2333 return -ENOMEM; in xhci_setup_port_arrays()
2338 xhci_add_in_port(xhci, num_ports, base + offset, cap_count); in xhci_setup_port_arrays()
2339 if (xhci->usb2_rhub.num_ports + xhci->usb3_rhub.num_ports == in xhci_setup_port_arrays()
2345 if (xhci->usb2_rhub.num_ports == 0 && xhci->usb3_rhub.num_ports == 0) { in xhci_setup_port_arrays()
2346 xhci_warn(xhci, "No ports on the roothubs?\n"); in xhci_setup_port_arrays()
2347 return -ENODEV; in xhci_setup_port_arrays()
2349 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_setup_port_arrays()
2351 xhci->usb2_rhub.num_ports, xhci->usb3_rhub.num_ports); in xhci_setup_port_arrays()
2356 if (xhci->usb3_rhub.num_ports > USB_SS_MAXPORTS) { in xhci_setup_port_arrays()
2357 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_setup_port_arrays()
2360 xhci->usb3_rhub.num_ports = USB_SS_MAXPORTS; in xhci_setup_port_arrays()
2362 if (xhci->usb2_rhub.num_ports > USB_MAXCHILDREN) { in xhci_setup_port_arrays()
2363 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_setup_port_arrays()
2366 xhci->usb2_rhub.num_ports = USB_MAXCHILDREN; in xhci_setup_port_arrays()
2374 xhci_create_rhub_port_array(xhci, &xhci->usb2_rhub, flags); in xhci_setup_port_arrays()
2375 xhci_create_rhub_port_array(xhci, &xhci->usb3_rhub, flags); in xhci_setup_port_arrays()
2380 int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags) in xhci_mem_init() argument
2383 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_mem_init()
2389 INIT_LIST_HEAD(&xhci->cmd_list); in xhci_mem_init()
2392 INIT_DELAYED_WORK(&xhci->cmd_timer, xhci_handle_command_timeout); in xhci_mem_init()
2393 init_completion(&xhci->cmd_ring_stop_completion); in xhci_mem_init()
2395 page_size = readl(&xhci->op_regs->page_size); in xhci_mem_init()
2396 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2404 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2407 xhci_warn(xhci, "WARN: no supported page size\n"); in xhci_mem_init()
2409 xhci->page_shift = 12; in xhci_mem_init()
2410 xhci->page_size = 1 << xhci->page_shift; in xhci_mem_init()
2411 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2412 "HCD page size set to %iK", xhci->page_size / 1024); in xhci_mem_init()
2418 val = HCS_MAX_SLOTS(readl(&xhci->cap_regs->hcs_params1)); in xhci_mem_init()
2419 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2421 val2 = readl(&xhci->op_regs->config_reg); in xhci_mem_init()
2423 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2425 writel(val, &xhci->op_regs->config_reg); in xhci_mem_init()
2428 * xHCI section 5.4.6 - doorbell array must be in xhci_mem_init()
2429 * "physically contiguous and 64-byte (cache line) aligned". in xhci_mem_init()
2431 xhci->dcbaa = dma_alloc_coherent(dev, sizeof(*xhci->dcbaa), &dma, in xhci_mem_init()
2433 if (!xhci->dcbaa) in xhci_mem_init()
2435 xhci->dcbaa->dma = dma; in xhci_mem_init()
2436 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2438 (unsigned long long)xhci->dcbaa->dma, xhci->dcbaa); in xhci_mem_init()
2439 xhci_write_64(xhci, dma, &xhci->op_regs->dcbaa_ptr); in xhci_mem_init()
2444 * however, the command ring segment needs 64-byte aligned segments in xhci_mem_init()
2448 xhci->segment_pool = dma_pool_create("xHCI ring segments", dev, in xhci_mem_init()
2449 TRB_SEGMENT_SIZE, TRB_SEGMENT_SIZE, xhci->page_size); in xhci_mem_init()
2452 xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev, in xhci_mem_init()
2453 2112, 64, xhci->page_size); in xhci_mem_init()
2454 if (!xhci->segment_pool || !xhci->device_pool) in xhci_mem_init()
2458 * and only need to be 16-byte aligned. in xhci_mem_init()
2460 xhci->small_streams_pool = in xhci_mem_init()
2461 dma_pool_create("xHCI 256 byte stream ctx arrays", in xhci_mem_init()
2463 xhci->medium_streams_pool = in xhci_mem_init()
2464 dma_pool_create("xHCI 1KB stream ctx arrays", in xhci_mem_init()
2470 if (!xhci->small_streams_pool || !xhci->medium_streams_pool) in xhci_mem_init()
2474 xhci->cmd_ring = xhci_ring_alloc(xhci, 1, 1, TYPE_COMMAND, 0, flags); in xhci_mem_init()
2475 if (!xhci->cmd_ring) in xhci_mem_init()
2477 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2478 "Allocated command ring at %p", xhci->cmd_ring); in xhci_mem_init()
2479 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "First segment DMA is 0x%llx", in xhci_mem_init()
2480 (unsigned long long)xhci->cmd_ring->first_seg->dma); in xhci_mem_init()
2483 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring); in xhci_mem_init()
2485 (xhci->cmd_ring->first_seg->dma & (u64) ~CMD_RING_RSVD_BITS) | in xhci_mem_init()
2486 xhci->cmd_ring->cycle_state; in xhci_mem_init()
2487 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2489 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring); in xhci_mem_init()
2491 xhci->lpm_command = xhci_alloc_command_with_ctx(xhci, true, flags); in xhci_mem_init()
2492 if (!xhci->lpm_command) in xhci_mem_init()
2499 xhci->cmd_ring_reserved_trbs++; in xhci_mem_init()
2501 val = readl(&xhci->cap_regs->db_off); in xhci_mem_init()
2503 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2506 xhci->dba = (void __iomem *) xhci->cap_regs + val; in xhci_mem_init()
2508 xhci->ir_set = &xhci->run_regs->ir_set[0]; in xhci_mem_init()
2514 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Allocating event ring"); in xhci_mem_init()
2515 xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, 1, TYPE_EVENT, in xhci_mem_init()
2517 if (!xhci->event_ring) in xhci_mem_init()
2519 if (xhci_check_trb_in_td_math(xhci) < 0) in xhci_mem_init()
2522 ret = xhci_alloc_erst(xhci, xhci->event_ring, &xhci->erst, flags); in xhci_mem_init()
2527 val = readl(&xhci->ir_set->erst_size); in xhci_mem_init()
2530 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2533 writel(val, &xhci->ir_set->erst_size); in xhci_mem_init()
2535 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2538 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2540 (unsigned long long)xhci->erst.erst_dma_addr); in xhci_mem_init()
2541 val_64 = xhci_read_64(xhci, &xhci->ir_set->erst_base); in xhci_mem_init()
2543 val_64 |= (xhci->erst.erst_dma_addr & (u64) ~ERST_PTR_MASK); in xhci_mem_init()
2544 xhci_write_64(xhci, val_64, &xhci->ir_set->erst_base); in xhci_mem_init()
2547 xhci_set_hc_event_deq(xhci); in xhci_mem_init()
2548 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2551 xhci->isoc_bei_interval = AVOID_BEI_INTERVAL_MAX; in xhci_mem_init()
2559 xhci->devs[i] = NULL; in xhci_mem_init()
2561 xhci->usb2_rhub.bus_state.resume_done[i] = 0; in xhci_mem_init()
2562 xhci->usb3_rhub.bus_state.resume_done[i] = 0; in xhci_mem_init()
2564 init_completion(&xhci->usb2_rhub.bus_state.rexit_done[i]); in xhci_mem_init()
2565 init_completion(&xhci->usb3_rhub.bus_state.u3exit_done[i]); in xhci_mem_init()
2568 if (scratchpad_alloc(xhci, flags)) in xhci_mem_init()
2570 if (xhci_setup_port_arrays(xhci, flags)) in xhci_mem_init()
2577 temp = readl(&xhci->op_regs->dev_notification); in xhci_mem_init()
2580 writel(temp, &xhci->op_regs->dev_notification); in xhci_mem_init()
2585 xhci_halt(xhci); in xhci_mem_init()
2586 xhci_reset(xhci); in xhci_mem_init()
2587 xhci_mem_cleanup(xhci); in xhci_mem_init()
2588 return -ENOMEM; in xhci_mem_init()