Lines Matching full:xhci
3 * xHCI host controller driver
18 #include "xhci.h"
19 #include "xhci-trace.h"
20 #include "xhci-debugfs.h"
29 static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci, in xhci_segment_alloc() argument
38 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_segment_alloc()
44 seg->trbs = dma_pool_zalloc(xhci->segment_pool, flags, &dma); in xhci_segment_alloc()
54 dma_pool_free(xhci->segment_pool, seg->trbs, dma); in xhci_segment_alloc()
71 static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg) in xhci_segment_free() argument
74 dma_pool_free(xhci->segment_pool, seg->trbs, seg->dma); in xhci_segment_free()
81 static void xhci_free_segments_for_ring(struct xhci_hcd *xhci, in xhci_free_segments_for_ring() argument
89 xhci_segment_free(xhci, seg); in xhci_free_segments_for_ring()
92 xhci_segment_free(xhci, first); in xhci_free_segments_for_ring()
129 static void xhci_link_rings(struct xhci_hcd *xhci, struct xhci_ring *ring, in xhci_link_rings() argument
140 chain_links = !!(xhci_link_trb_quirk(xhci) || in xhci_link_rings()
142 (xhci->quirks & XHCI_AMD_0x96_HOST))); in xhci_link_rings()
282 void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring) in xhci_ring_free() argument
292 xhci_free_segments_for_ring(xhci, ring->first_seg); in xhci_ring_free()
324 static int xhci_alloc_segments_for_ring(struct xhci_hcd *xhci, in xhci_alloc_segments_for_ring() argument
334 chain_links = !!(xhci_link_trb_quirk(xhci) || in xhci_alloc_segments_for_ring()
336 (xhci->quirks & XHCI_AMD_0x96_HOST))); in xhci_alloc_segments_for_ring()
338 prev = xhci_segment_alloc(xhci, cycle_state, max_packet, num, flags); in xhci_alloc_segments_for_ring()
347 next = xhci_segment_alloc(xhci, cycle_state, max_packet, num, in xhci_alloc_segments_for_ring()
353 xhci_segment_free(xhci, prev); in xhci_alloc_segments_for_ring()
376 struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci, in xhci_ring_alloc() argument
382 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_ring_alloc()
395 ret = xhci_alloc_segments_for_ring(xhci, &ring->first_seg, in xhci_ring_alloc()
416 void xhci_free_endpoint_ring(struct xhci_hcd *xhci, in xhci_free_endpoint_ring() argument
420 xhci_ring_free(xhci, virt_dev->eps[ep_index].ring); in xhci_free_endpoint_ring()
428 int xhci_ring_expansion(struct xhci_hcd *xhci, struct xhci_ring *ring, in xhci_ring_expansion() argument
435 ret = xhci_alloc_segments_for_ring(xhci, &first, &last, in xhci_ring_expansion()
448 xhci_segment_free(xhci, first); in xhci_ring_expansion()
456 xhci_link_rings(xhci, ring, first, last, num_new_segs); in xhci_ring_expansion()
458 xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion, in xhci_ring_expansion()
465 struct xhci_container_ctx *xhci_alloc_container_ctx(struct xhci_hcd *xhci, in xhci_alloc_container_ctx() argument
469 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_alloc_container_ctx()
479 ctx->size = HCC_64BYTE_CONTEXT(xhci->hcc_params) ? 2048 : 1024; in xhci_alloc_container_ctx()
481 ctx->size += CTX_SIZE(xhci->hcc_params); in xhci_alloc_container_ctx()
483 ctx->bytes = dma_pool_zalloc(xhci->device_pool, flags, &ctx->dma); in xhci_alloc_container_ctx()
491 void xhci_free_container_ctx(struct xhci_hcd *xhci, in xhci_free_container_ctx() argument
496 dma_pool_free(xhci->device_pool, ctx->bytes, ctx->dma); in xhci_free_container_ctx()
509 struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_hcd *xhci, in xhci_get_slot_ctx() argument
516 (ctx->bytes + CTX_SIZE(xhci->hcc_params)); in xhci_get_slot_ctx()
519 struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_hcd *xhci, in xhci_get_ep_ctx() argument
529 (ctx->bytes + (ep_index * CTX_SIZE(xhci->hcc_params))); in xhci_get_ep_ctx()
535 static void xhci_free_stream_ctx(struct xhci_hcd *xhci, in xhci_free_stream_ctx() argument
539 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_free_stream_ctx()
545 dma_pool_free(xhci->medium_streams_pool, stream_ctx, dma); in xhci_free_stream_ctx()
547 dma_pool_free(xhci->small_streams_pool, stream_ctx, dma); in xhci_free_stream_ctx()
560 static struct xhci_stream_ctx *xhci_alloc_stream_ctx(struct xhci_hcd *xhci, in xhci_alloc_stream_ctx() argument
564 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_alloc_stream_ctx()
570 return dma_pool_zalloc(xhci->medium_streams_pool, mem_flags, dma); in xhci_alloc_stream_ctx()
572 return dma_pool_zalloc(xhci->small_streams_pool, mem_flags, dma); in xhci_alloc_stream_ctx()
594 struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci, in xhci_alloc_stream_info() argument
604 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_alloc_stream_info()
606 xhci_dbg(xhci, "Allocating %u streams and %u stream context array entries.\n", in xhci_alloc_stream_info()
608 if (xhci->cmd_ring_reserved_trbs == MAX_RSVD_CMD_TRBS) { in xhci_alloc_stream_info()
609 xhci_dbg(xhci, "Command ring has no reserved TRBs available\n"); in xhci_alloc_stream_info()
612 xhci->cmd_ring_reserved_trbs++; in xhci_alloc_stream_info()
630 stream_info->stream_ctx_array = xhci_alloc_stream_ctx(xhci, in xhci_alloc_stream_info()
638 xhci_alloc_command_with_ctx(xhci, true, mem_flags); in xhci_alloc_stream_info()
651 xhci_ring_alloc(xhci, 2, 1, TYPE_STREAM, max_packet, in xhci_alloc_stream_info()
664 xhci_dbg(xhci, "Setting stream %d ring ptr to 0x%08llx\n", cur_stream, addr); in xhci_alloc_stream_info()
668 xhci_ring_free(xhci, cur_ring); in xhci_alloc_stream_info()
686 xhci_ring_free(xhci, cur_ring); in xhci_alloc_stream_info()
690 xhci_free_command(xhci, stream_info->free_streams_command); in xhci_alloc_stream_info()
692 xhci_free_stream_ctx(xhci, in xhci_alloc_stream_info()
701 xhci->cmd_ring_reserved_trbs--; in xhci_alloc_stream_info()
708 void xhci_setup_streams_ep_input_ctx(struct xhci_hcd *xhci, in xhci_setup_streams_ep_input_ctx() argument
718 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_setup_streams_ep_input_ctx()
745 void xhci_free_stream_info(struct xhci_hcd *xhci, in xhci_free_stream_info() argument
758 xhci_ring_free(xhci, cur_ring); in xhci_free_stream_info()
762 xhci_free_command(xhci, stream_info->free_streams_command); in xhci_free_stream_info()
763 xhci->cmd_ring_reserved_trbs--; in xhci_free_stream_info()
765 xhci_free_stream_ctx(xhci, in xhci_free_stream_info()
777 static void xhci_free_tt_info(struct xhci_hcd *xhci, in xhci_free_tt_info() argument
789 virt_dev->real_port > HCS_MAX_PORTS(xhci->hcs_params1)) { in xhci_free_tt_info()
790 xhci_dbg(xhci, "Bad real port.\n"); in xhci_free_tt_info()
794 tt_list_head = &(xhci->rh_bw[virt_dev->real_port - 1].tts); in xhci_free_tt_info()
807 int xhci_alloc_tt_info(struct xhci_hcd *xhci, in xhci_alloc_tt_info() argument
815 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_alloc_tt_info()
831 &xhci->rh_bw[virt_dev->real_port - 1].tts); in xhci_alloc_tt_info()
842 xhci_free_tt_info(xhci, virt_dev, virt_dev->udev->slot_id); in xhci_alloc_tt_info()
848 * Should be called with xhci->lock held if there is any chance the TT lists
852 void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id) in xhci_free_virt_device() argument
859 if (slot_id == 0 || !xhci->devs[slot_id]) in xhci_free_virt_device()
862 dev = xhci->devs[slot_id]; in xhci_free_virt_device()
864 xhci->dcbaa->dev_context_ptrs[slot_id] = 0; in xhci_free_virt_device()
875 xhci_ring_free(xhci, dev->eps[i].ring); in xhci_free_virt_device()
877 xhci_free_stream_info(xhci, in xhci_free_virt_device()
889 xhci_dbg(xhci, "Slot %u endpoint %u not removed from BW list!\n", in xhci_free_virt_device()
894 xhci_free_tt_info(xhci, dev, slot_id); in xhci_free_virt_device()
896 xhci_update_tt_active_eps(xhci, dev, old_active_eps); in xhci_free_virt_device()
899 xhci_free_container_ctx(xhci, dev->in_ctx); in xhci_free_virt_device()
901 xhci_free_container_ctx(xhci, dev->out_ctx); in xhci_free_virt_device()
905 kfree(xhci->devs[slot_id]); in xhci_free_virt_device()
906 xhci->devs[slot_id] = NULL; in xhci_free_virt_device()
915 static void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id) in xhci_free_virt_devices_depth_first() argument
922 vdev = xhci->devs[slot_id]; in xhci_free_virt_devices_depth_first()
927 vdev->real_port > HCS_MAX_PORTS(xhci->hcs_params1)) { in xhci_free_virt_devices_depth_first()
928 xhci_dbg(xhci, "Bad vdev->real_port.\n"); in xhci_free_virt_devices_depth_first()
932 tt_list_head = &(xhci->rh_bw[vdev->real_port - 1].tts); in xhci_free_virt_devices_depth_first()
937 for (i = 1; i < HCS_MAX_SLOTS(xhci->hcs_params1); i++) { in xhci_free_virt_devices_depth_first()
938 vdev = xhci->devs[i]; in xhci_free_virt_devices_depth_first()
941 xhci, i); in xhci_free_virt_devices_depth_first()
947 xhci_debugfs_remove_slot(xhci, slot_id); in xhci_free_virt_devices_depth_first()
948 xhci_free_virt_device(xhci, slot_id); in xhci_free_virt_devices_depth_first()
951 int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id, in xhci_alloc_virt_device() argument
958 if (slot_id == 0 || xhci->devs[slot_id]) { in xhci_alloc_virt_device()
959 xhci_warn(xhci, "Bad Slot ID %d\n", slot_id); in xhci_alloc_virt_device()
970 dev->out_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_DEVICE, flags); in xhci_alloc_virt_device()
974 xhci_dbg(xhci, "Slot %d output ctx = 0x%pad (dma)\n", slot_id, &dev->out_ctx->dma); in xhci_alloc_virt_device()
977 dev->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, flags); in xhci_alloc_virt_device()
981 xhci_dbg(xhci, "Slot %d input ctx = 0x%pad (dma)\n", slot_id, &dev->in_ctx->dma); in xhci_alloc_virt_device()
987 dev->eps[i].xhci = xhci; in xhci_alloc_virt_device()
993 dev->eps[0].ring = xhci_ring_alloc(xhci, 2, 1, TYPE_CTRL, 0, flags); in xhci_alloc_virt_device()
1000 xhci->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(dev->out_ctx->dma); in xhci_alloc_virt_device()
1001 xhci_dbg(xhci, "Set slot id %d dcbaa entry %p to 0x%llx\n", in xhci_alloc_virt_device()
1003 &xhci->dcbaa->dev_context_ptrs[slot_id], in xhci_alloc_virt_device()
1004 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[slot_id])); in xhci_alloc_virt_device()
1008 xhci->devs[slot_id] = dev; in xhci_alloc_virt_device()
1014 xhci_free_container_ctx(xhci, dev->in_ctx); in xhci_alloc_virt_device()
1016 xhci_free_container_ctx(xhci, dev->out_ctx); in xhci_alloc_virt_device()
1022 void xhci_copy_ep0_dequeue_into_input_ctx(struct xhci_hcd *xhci, in xhci_copy_ep0_dequeue_into_input_ctx() argument
1029 virt_dev = xhci->devs[udev->slot_id]; in xhci_copy_ep0_dequeue_into_input_ctx()
1030 ep0_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, 0); in xhci_copy_ep0_dequeue_into_input_ctx()
1045 * The xHCI roothub may have ports of differing speeds in any order in the port
1048 * The xHCI hardware wants to know the roothub port number that the USB device
1054 static u32 xhci_find_real_port_number(struct xhci_hcd *xhci, in xhci_find_real_port_number() argument
1061 hcd = xhci_get_usb3_hcd(xhci); in xhci_find_real_port_number()
1063 hcd = xhci->main_hcd; in xhci_find_real_port_number()
1072 /* Setup an xHCI virtual device for a Set Address command */
1073 int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev) in xhci_setup_addressable_virt_dev() argument
1082 dev = xhci->devs[udev->slot_id]; in xhci_setup_addressable_virt_dev()
1085 xhci_warn(xhci, "Slot ID %d is not assigned to this device\n", in xhci_setup_addressable_virt_dev()
1089 ep0_ctx = xhci_get_ep_ctx(xhci, dev->in_ctx, 0); in xhci_setup_addressable_virt_dev()
1090 slot_ctx = xhci_get_slot_ctx(xhci, dev->in_ctx); in xhci_setup_addressable_virt_dev()
1121 port_num = xhci_find_real_port_number(xhci, udev); in xhci_setup_addressable_virt_dev()
1131 xhci_dbg(xhci, "Set root hub portnum to %d\n", port_num); in xhci_setup_addressable_virt_dev()
1132 xhci_dbg(xhci, "Set fake root hub portnum to %d\n", dev->fake_port); in xhci_setup_addressable_virt_dev()
1141 dev->bw_table = &xhci->rh_bw[port_num - 1].bw_table; in xhci_setup_addressable_virt_dev()
1146 rh_bw = &xhci->rh_bw[port_num - 1]; in xhci_setup_addressable_virt_dev()
1161 xhci_warn(xhci, "WARN: Didn't find a matching TT\n"); in xhci_setup_addressable_virt_dev()
1171 xhci_dbg(xhci, "udev->tt = %p\n", udev->tt); in xhci_setup_addressable_virt_dev()
1172 xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport); in xhci_setup_addressable_virt_dev()
1263 * The polling interval is expressed in "microframes". If xHCI's Interval field
1399 int xhci_endpoint_init(struct xhci_hcd *xhci, in xhci_endpoint_init() argument
1419 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index); in xhci_endpoint_init()
1431 * set it to max available. See xHCI 1.1 spec 4.14.1.1 for details. in xhci_endpoint_init()
1439 if ((xhci->quirks & XHCI_LIMIT_ENDPOINT_INTERVAL_9) && in xhci_endpoint_init()
1443 if ((xhci->quirks & XHCI_LIMIT_ENDPOINT_INTERVAL_7) && in xhci_endpoint_init()
1469 /* xHCI 1.0 and 1.1 indicates that ctrl ep avg TRB Length should be 8 */ in xhci_endpoint_init()
1470 if (usb_endpoint_xfer_control(&ep->desc) && xhci->hci_version >= 0x100) in xhci_endpoint_init()
1472 /* xhci 1.1 with LEC support doesn't use mult field, use RsvdZ */ in xhci_endpoint_init()
1473 if ((xhci->hci_version > 0x100) && HCC2_LEC(xhci->hcc_params2)) in xhci_endpoint_init()
1478 xhci_ring_alloc(xhci, 2, 1, ring_type, max_packet, mem_flags); in xhci_endpoint_init()
1502 void xhci_endpoint_zero(struct xhci_hcd *xhci, in xhci_endpoint_zero() argument
1510 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index); in xhci_endpoint_zero()
1531 void xhci_update_bw_info(struct xhci_hcd *xhci, in xhci_update_bw_info() argument
1556 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, i); in xhci_update_bw_info()
1589 void xhci_endpoint_copy(struct xhci_hcd *xhci, in xhci_endpoint_copy() argument
1597 out_ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); in xhci_endpoint_copy()
1598 in_ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index); in xhci_endpoint_copy()
1604 if (xhci->quirks & XHCI_MTK_HOST) { in xhci_endpoint_copy()
1615 void xhci_slot_copy(struct xhci_hcd *xhci, in xhci_slot_copy() argument
1622 in_slot_ctx = xhci_get_slot_ctx(xhci, in_ctx); in xhci_slot_copy()
1623 out_slot_ctx = xhci_get_slot_ctx(xhci, out_ctx); in xhci_slot_copy()
1632 static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags) in scratchpad_alloc() argument
1635 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in scratchpad_alloc()
1636 int num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2); in scratchpad_alloc()
1638 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in scratchpad_alloc()
1644 xhci->scratchpad = kzalloc_node(sizeof(*xhci->scratchpad), flags, in scratchpad_alloc()
1646 if (!xhci->scratchpad) in scratchpad_alloc()
1649 xhci->scratchpad->sp_array = dma_alloc_coherent(dev, in scratchpad_alloc()
1651 &xhci->scratchpad->sp_dma, flags); in scratchpad_alloc()
1652 if (!xhci->scratchpad->sp_array) in scratchpad_alloc()
1655 xhci->scratchpad->sp_buffers = kcalloc_node(num_sp, sizeof(void *), in scratchpad_alloc()
1657 if (!xhci->scratchpad->sp_buffers) in scratchpad_alloc()
1660 xhci->dcbaa->dev_context_ptrs[0] = cpu_to_le64(xhci->scratchpad->sp_dma); in scratchpad_alloc()
1663 void *buf = dma_alloc_coherent(dev, xhci->page_size, &dma, in scratchpad_alloc()
1668 xhci->scratchpad->sp_array[i] = dma; in scratchpad_alloc()
1669 xhci->scratchpad->sp_buffers[i] = buf; in scratchpad_alloc()
1676 dma_free_coherent(dev, xhci->page_size, in scratchpad_alloc()
1677 xhci->scratchpad->sp_buffers[i], in scratchpad_alloc()
1678 xhci->scratchpad->sp_array[i]); in scratchpad_alloc()
1680 kfree(xhci->scratchpad->sp_buffers); in scratchpad_alloc()
1684 xhci->scratchpad->sp_array, in scratchpad_alloc()
1685 xhci->scratchpad->sp_dma); in scratchpad_alloc()
1688 kfree(xhci->scratchpad); in scratchpad_alloc()
1689 xhci->scratchpad = NULL; in scratchpad_alloc()
1695 static void scratchpad_free(struct xhci_hcd *xhci) in scratchpad_free() argument
1699 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in scratchpad_free()
1701 if (!xhci->scratchpad) in scratchpad_free()
1704 num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2); in scratchpad_free()
1707 dma_free_coherent(dev, xhci->page_size, in scratchpad_free()
1708 xhci->scratchpad->sp_buffers[i], in scratchpad_free()
1709 xhci->scratchpad->sp_array[i]); in scratchpad_free()
1711 kfree(xhci->scratchpad->sp_buffers); in scratchpad_free()
1713 xhci->scratchpad->sp_array, in scratchpad_free()
1714 xhci->scratchpad->sp_dma); in scratchpad_free()
1715 kfree(xhci->scratchpad); in scratchpad_free()
1716 xhci->scratchpad = NULL; in scratchpad_free()
1719 struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci, in xhci_alloc_command() argument
1723 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_alloc_command()
1747 struct xhci_command *xhci_alloc_command_with_ctx(struct xhci_hcd *xhci, in xhci_alloc_command_with_ctx() argument
1752 command = xhci_alloc_command(xhci, allocate_completion, mem_flags); in xhci_alloc_command_with_ctx()
1756 command->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, in xhci_alloc_command_with_ctx()
1771 void xhci_free_command(struct xhci_hcd *xhci, in xhci_free_command() argument
1774 xhci_free_container_ctx(xhci, in xhci_free_command()
1780 int xhci_alloc_erst(struct xhci_hcd *xhci, in xhci_alloc_erst() argument
1791 erst->entries = dma_alloc_coherent(xhci_to_hcd(xhci)->self.sysdev, in xhci_alloc_erst()
1811 xhci_remove_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir) in xhci_remove_interrupter() argument
1828 xhci_write_64(xhci, ERST_EHB, &ir->ir_set->erst_dequeue); in xhci_remove_interrupter()
1833 xhci_free_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir) in xhci_free_interrupter() argument
1835 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_free_interrupter()
1850 xhci_ring_free(xhci, ir->event_ring); in xhci_free_interrupter()
1859 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_remove_secondary_interrupter() local
1862 spin_lock_irq(&xhci->lock); in xhci_remove_secondary_interrupter()
1865 if (!ir || !ir->intr_num || ir->intr_num >= xhci->max_interrupters) { in xhci_remove_secondary_interrupter()
1866 xhci_dbg(xhci, "Invalid secondary interrupter, can't remove\n"); in xhci_remove_secondary_interrupter()
1867 spin_unlock_irq(&xhci->lock); in xhci_remove_secondary_interrupter()
1873 xhci_remove_interrupter(xhci, ir); in xhci_remove_secondary_interrupter()
1874 xhci->interrupters[intr_num] = NULL; in xhci_remove_secondary_interrupter()
1876 spin_unlock_irq(&xhci->lock); in xhci_remove_secondary_interrupter()
1878 xhci_free_interrupter(xhci, ir); in xhci_remove_secondary_interrupter()
1882 void xhci_mem_cleanup(struct xhci_hcd *xhci) in xhci_mem_cleanup() argument
1884 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_mem_cleanup()
1887 cancel_delayed_work_sync(&xhci->cmd_timer); in xhci_mem_cleanup()
1889 for (i = 0; xhci->interrupters && i < xhci->max_interrupters; i++) { in xhci_mem_cleanup()
1890 if (xhci->interrupters[i]) { in xhci_mem_cleanup()
1891 xhci_remove_interrupter(xhci, xhci->interrupters[i]); in xhci_mem_cleanup()
1892 xhci_free_interrupter(xhci, xhci->interrupters[i]); in xhci_mem_cleanup()
1893 xhci->interrupters[i] = NULL; in xhci_mem_cleanup()
1896 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed interrupters"); in xhci_mem_cleanup()
1898 if (xhci->cmd_ring) in xhci_mem_cleanup()
1899 xhci_ring_free(xhci, xhci->cmd_ring); in xhci_mem_cleanup()
1900 xhci->cmd_ring = NULL; in xhci_mem_cleanup()
1901 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed command ring"); in xhci_mem_cleanup()
1902 xhci_cleanup_command_queue(xhci); in xhci_mem_cleanup()
1904 num_ports = HCS_MAX_PORTS(xhci->hcs_params1); in xhci_mem_cleanup()
1905 for (i = 0; i < num_ports && xhci->rh_bw; i++) { in xhci_mem_cleanup()
1906 struct xhci_interval_bw_table *bwt = &xhci->rh_bw[i].bw_table; in xhci_mem_cleanup()
1914 for (i = HCS_MAX_SLOTS(xhci->hcs_params1); i > 0; i--) in xhci_mem_cleanup()
1915 xhci_free_virt_devices_depth_first(xhci, i); in xhci_mem_cleanup()
1917 dma_pool_destroy(xhci->segment_pool); in xhci_mem_cleanup()
1918 xhci->segment_pool = NULL; in xhci_mem_cleanup()
1919 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed segment pool"); in xhci_mem_cleanup()
1921 dma_pool_destroy(xhci->device_pool); in xhci_mem_cleanup()
1922 xhci->device_pool = NULL; in xhci_mem_cleanup()
1923 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed device context pool"); in xhci_mem_cleanup()
1925 dma_pool_destroy(xhci->small_streams_pool); in xhci_mem_cleanup()
1926 xhci->small_streams_pool = NULL; in xhci_mem_cleanup()
1927 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_cleanup()
1930 dma_pool_destroy(xhci->medium_streams_pool); in xhci_mem_cleanup()
1931 xhci->medium_streams_pool = NULL; in xhci_mem_cleanup()
1932 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_cleanup()
1935 if (xhci->dcbaa) in xhci_mem_cleanup()
1936 dma_free_coherent(dev, sizeof(*xhci->dcbaa), in xhci_mem_cleanup()
1937 xhci->dcbaa, xhci->dcbaa->dma); in xhci_mem_cleanup()
1938 xhci->dcbaa = NULL; in xhci_mem_cleanup()
1940 scratchpad_free(xhci); in xhci_mem_cleanup()
1942 if (!xhci->rh_bw) in xhci_mem_cleanup()
1947 list_for_each_entry_safe(tt, n, &xhci->rh_bw[i].tts, tt_list) { in xhci_mem_cleanup()
1954 xhci->cmd_ring_reserved_trbs = 0; in xhci_mem_cleanup()
1955 xhci->usb2_rhub.num_ports = 0; in xhci_mem_cleanup()
1956 xhci->usb3_rhub.num_ports = 0; in xhci_mem_cleanup()
1957 xhci->num_active_eps = 0; in xhci_mem_cleanup()
1958 kfree(xhci->usb2_rhub.ports); in xhci_mem_cleanup()
1959 kfree(xhci->usb3_rhub.ports); in xhci_mem_cleanup()
1960 kfree(xhci->hw_ports); in xhci_mem_cleanup()
1961 kfree(xhci->rh_bw); in xhci_mem_cleanup()
1962 kfree(xhci->ext_caps); in xhci_mem_cleanup()
1963 for (i = 0; i < xhci->num_port_caps; i++) in xhci_mem_cleanup()
1964 kfree(xhci->port_caps[i].psi); in xhci_mem_cleanup()
1965 kfree(xhci->port_caps); in xhci_mem_cleanup()
1966 kfree(xhci->interrupters); in xhci_mem_cleanup()
1967 xhci->num_port_caps = 0; in xhci_mem_cleanup()
1969 xhci->usb2_rhub.ports = NULL; in xhci_mem_cleanup()
1970 xhci->usb3_rhub.ports = NULL; in xhci_mem_cleanup()
1971 xhci->hw_ports = NULL; in xhci_mem_cleanup()
1972 xhci->rh_bw = NULL; in xhci_mem_cleanup()
1973 xhci->ext_caps = NULL; in xhci_mem_cleanup()
1974 xhci->port_caps = NULL; in xhci_mem_cleanup()
1975 xhci->interrupters = NULL; in xhci_mem_cleanup()
1977 xhci->page_size = 0; in xhci_mem_cleanup()
1978 xhci->page_shift = 0; in xhci_mem_cleanup()
1979 xhci->usb2_rhub.bus_state.bus_suspended = 0; in xhci_mem_cleanup()
1980 xhci->usb3_rhub.bus_state.bus_suspended = 0; in xhci_mem_cleanup()
1983 static void xhci_set_hc_event_deq(struct xhci_hcd *xhci, struct xhci_interrupter *ir) in xhci_set_hc_event_deq() argument
1990 xhci_warn(xhci, "WARN something wrong with SW event ring dequeue ptr.\n"); in xhci_set_hc_event_deq()
1995 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_set_hc_event_deq()
1997 xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK), in xhci_set_hc_event_deq()
2001 static void xhci_add_in_port(struct xhci_hcd *xhci, unsigned int num_ports, in xhci_add_in_port() argument
2008 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_add_in_port()
2016 rhub = &xhci->usb3_rhub; in xhci_add_in_port()
2027 * Some zhaoxin's xHCI controller that follow usb3.1 spec in xhci_add_in_port()
2030 if (xhci->quirks & XHCI_ZHAOXIN_HOST) { in xhci_add_in_port()
2036 rhub = &xhci->usb2_rhub; in xhci_add_in_port()
2038 xhci_warn(xhci, "Ignoring unknown port speed, Ext Cap %p, revision = 0x%x\n", in xhci_add_in_port()
2048 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_add_in_port()
2056 port_cap = &xhci->port_caps[xhci->num_port_caps++]; in xhci_add_in_port()
2057 if (xhci->num_port_caps > max_caps) in xhci_add_in_port()
2080 if (xhci->quirks & XHCI_ZHAOXIN_HOST && in xhci_add_in_port()
2085 xhci_dbg(xhci, "PSIV:%d PSIE:%d PLT:%d PFD:%d LP:%d PSIM:%d\n", in xhci_add_in_port()
2104 if (major_revision < 0x03 && xhci->num_ext_caps < max_caps) in xhci_add_in_port()
2105 xhci->ext_caps[xhci->num_ext_caps++] = temp; in xhci_add_in_port()
2107 if ((xhci->hci_version >= 0x100) && (major_revision != 0x03) && in xhci_add_in_port()
2109 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_add_in_port()
2110 "xHCI 1.0: support USB2 hardware lpm"); in xhci_add_in_port()
2111 xhci->hw_lpm_support = 1; in xhci_add_in_port()
2116 struct xhci_port *hw_port = &xhci->hw_ports[i]; in xhci_add_in_port()
2119 xhci_warn(xhci, "Duplicate port entry, Ext Cap %p, port %u\n", addr, i); in xhci_add_in_port()
2120 xhci_warn(xhci, "Port was marked as USB %u, duplicated as USB %u\n", in xhci_add_in_port()
2139 static void xhci_create_rhub_port_array(struct xhci_hcd *xhci, in xhci_create_rhub_port_array() argument
2144 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_create_rhub_port_array()
2153 for (i = 0; i < HCS_MAX_PORTS(xhci->hcs_params1); i++) { in xhci_create_rhub_port_array()
2154 if (xhci->hw_ports[i].rhub != rhub || in xhci_create_rhub_port_array()
2155 xhci->hw_ports[i].hcd_portnum == DUPLICATE_ENTRY) in xhci_create_rhub_port_array()
2157 xhci->hw_ports[i].hcd_portnum = port_index; in xhci_create_rhub_port_array()
2158 rhub->ports[port_index] = &xhci->hw_ports[i]; in xhci_create_rhub_port_array()
2172 static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags) in xhci_setup_port_arrays() argument
2180 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_setup_port_arrays()
2182 num_ports = HCS_MAX_PORTS(xhci->hcs_params1); in xhci_setup_port_arrays()
2183 xhci->hw_ports = kcalloc_node(num_ports, sizeof(*xhci->hw_ports), in xhci_setup_port_arrays()
2185 if (!xhci->hw_ports) in xhci_setup_port_arrays()
2189 xhci->hw_ports[i].addr = &xhci->op_regs->port_status_base + in xhci_setup_port_arrays()
2191 xhci->hw_ports[i].hw_portnum = i; in xhci_setup_port_arrays()
2193 init_completion(&xhci->hw_ports[i].rexit_done); in xhci_setup_port_arrays()
2194 init_completion(&xhci->hw_ports[i].u3exit_done); in xhci_setup_port_arrays()
2197 xhci->rh_bw = kcalloc_node(num_ports, sizeof(*xhci->rh_bw), flags, in xhci_setup_port_arrays()
2199 if (!xhci->rh_bw) in xhci_setup_port_arrays()
2204 INIT_LIST_HEAD(&xhci->rh_bw[i].tts); in xhci_setup_port_arrays()
2205 bw_table = &xhci->rh_bw[i].bw_table; in xhci_setup_port_arrays()
2209 base = &xhci->cap_regs->hc_capbase; in xhci_setup_port_arrays()
2213 xhci_err(xhci, "No Extended Capability registers, unable to set up roothub\n"); in xhci_setup_port_arrays()
2225 xhci->ext_caps = kcalloc_node(cap_count, sizeof(*xhci->ext_caps), in xhci_setup_port_arrays()
2227 if (!xhci->ext_caps) in xhci_setup_port_arrays()
2230 xhci->port_caps = kcalloc_node(cap_count, sizeof(*xhci->port_caps), in xhci_setup_port_arrays()
2232 if (!xhci->port_caps) in xhci_setup_port_arrays()
2238 xhci_add_in_port(xhci, num_ports, base + offset, cap_count); in xhci_setup_port_arrays()
2239 if (xhci->usb2_rhub.num_ports + xhci->usb3_rhub.num_ports == in xhci_setup_port_arrays()
2245 if (xhci->usb2_rhub.num_ports == 0 && xhci->usb3_rhub.num_ports == 0) { in xhci_setup_port_arrays()
2246 xhci_warn(xhci, "No ports on the roothubs?\n"); in xhci_setup_port_arrays()
2249 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_setup_port_arrays()
2251 xhci->usb2_rhub.num_ports, xhci->usb3_rhub.num_ports); in xhci_setup_port_arrays()
2256 if (xhci->usb3_rhub.num_ports > USB_SS_MAXPORTS) { in xhci_setup_port_arrays()
2257 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_setup_port_arrays()
2260 xhci->usb3_rhub.num_ports = USB_SS_MAXPORTS; in xhci_setup_port_arrays()
2262 if (xhci->usb2_rhub.num_ports > USB_MAXCHILDREN) { in xhci_setup_port_arrays()
2263 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_setup_port_arrays()
2266 xhci->usb2_rhub.num_ports = USB_MAXCHILDREN; in xhci_setup_port_arrays()
2269 if (!xhci->usb2_rhub.num_ports) in xhci_setup_port_arrays()
2270 xhci_info(xhci, "USB2 root hub has no ports\n"); in xhci_setup_port_arrays()
2272 if (!xhci->usb3_rhub.num_ports) in xhci_setup_port_arrays()
2273 xhci_info(xhci, "USB3 root hub has no ports\n"); in xhci_setup_port_arrays()
2275 xhci_create_rhub_port_array(xhci, &xhci->usb2_rhub, flags); in xhci_setup_port_arrays()
2276 xhci_create_rhub_port_array(xhci, &xhci->usb3_rhub, flags); in xhci_setup_port_arrays()
2282 xhci_alloc_interrupter(struct xhci_hcd *xhci, unsigned int segs, gfp_t flags) in xhci_alloc_interrupter() argument
2284 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_alloc_interrupter()
2292 max_segs = BIT(HCS_ERST_MAX(xhci->hcs_params2)); in xhci_alloc_interrupter()
2299 ir->event_ring = xhci_ring_alloc(xhci, segs, 1, TYPE_EVENT, 0, flags); in xhci_alloc_interrupter()
2301 xhci_warn(xhci, "Failed to allocate interrupter event ring\n"); in xhci_alloc_interrupter()
2306 ret = xhci_alloc_erst(xhci, ir->event_ring, &ir->erst, flags); in xhci_alloc_interrupter()
2308 xhci_warn(xhci, "Failed to allocate interrupter erst\n"); in xhci_alloc_interrupter()
2309 xhci_ring_free(xhci, ir->event_ring); in xhci_alloc_interrupter()
2318 xhci_add_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir, in xhci_add_interrupter() argument
2324 if (intr_num > xhci->max_interrupters) { in xhci_add_interrupter()
2325 xhci_warn(xhci, "Can't add interrupter %d, max interrupters %d\n", in xhci_add_interrupter()
2326 intr_num, xhci->max_interrupters); in xhci_add_interrupter()
2330 if (xhci->interrupters[intr_num]) { in xhci_add_interrupter()
2331 xhci_warn(xhci, "Interrupter %d\n already set up", intr_num); in xhci_add_interrupter()
2335 xhci->interrupters[intr_num] = ir; in xhci_add_interrupter()
2337 ir->ir_set = &xhci->run_regs->ir_set[intr_num]; in xhci_add_interrupter()
2345 erst_base = xhci_read_64(xhci, &ir->ir_set->erst_base); in xhci_add_interrupter()
2348 if (xhci->quirks & XHCI_WRITE_64_HI_LO) in xhci_add_interrupter()
2351 xhci_write_64(xhci, erst_base, &ir->ir_set->erst_base); in xhci_add_interrupter()
2354 xhci_set_hc_event_deq(xhci, ir); in xhci_add_interrupter()
2363 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_create_secondary_interrupter() local
2368 if (!xhci->interrupters || xhci->max_interrupters <= 1) in xhci_create_secondary_interrupter()
2371 ir = xhci_alloc_interrupter(xhci, segs, GFP_KERNEL); in xhci_create_secondary_interrupter()
2375 spin_lock_irq(&xhci->lock); in xhci_create_secondary_interrupter()
2378 for (i = 1; i < xhci->max_interrupters; i++) { in xhci_create_secondary_interrupter()
2379 if (xhci->interrupters[i] == NULL) { in xhci_create_secondary_interrupter()
2380 err = xhci_add_interrupter(xhci, ir, i); in xhci_create_secondary_interrupter()
2385 spin_unlock_irq(&xhci->lock); in xhci_create_secondary_interrupter()
2388 xhci_warn(xhci, "Failed to add secondary interrupter, max interrupters %d\n", in xhci_create_secondary_interrupter()
2389 xhci->max_interrupters); in xhci_create_secondary_interrupter()
2390 xhci_free_interrupter(xhci, ir); in xhci_create_secondary_interrupter()
2396 xhci_warn(xhci, "Failed to set interrupter %d moderation to %uns\n", in xhci_create_secondary_interrupter()
2399 xhci_dbg(xhci, "Add secondary interrupter %d, max interrupters %d\n", in xhci_create_secondary_interrupter()
2400 i, xhci->max_interrupters); in xhci_create_secondary_interrupter()
2406 int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags) in xhci_mem_init() argument
2409 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_mem_init()
2416 INIT_LIST_HEAD(&xhci->cmd_list); in xhci_mem_init()
2419 INIT_DELAYED_WORK(&xhci->cmd_timer, xhci_handle_command_timeout); in xhci_mem_init()
2420 init_completion(&xhci->cmd_ring_stop_completion); in xhci_mem_init()
2422 page_size = readl(&xhci->op_regs->page_size); in xhci_mem_init()
2423 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2427 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2430 xhci_warn(xhci, "WARN: no supported page size\n"); in xhci_mem_init()
2432 xhci->page_shift = 12; in xhci_mem_init()
2433 xhci->page_size = 1 << xhci->page_shift; in xhci_mem_init()
2434 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2435 "HCD page size set to %iK", xhci->page_size / 1024); in xhci_mem_init()
2441 val = HCS_MAX_SLOTS(readl(&xhci->cap_regs->hcs_params1)); in xhci_mem_init()
2442 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2444 val2 = readl(&xhci->op_regs->config_reg); in xhci_mem_init()
2446 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2448 writel(val, &xhci->op_regs->config_reg); in xhci_mem_init()
2451 * xHCI section 5.4.6 - Device Context array must be in xhci_mem_init()
2454 xhci->dcbaa = dma_alloc_coherent(dev, sizeof(*xhci->dcbaa), &dma, in xhci_mem_init()
2456 if (!xhci->dcbaa) in xhci_mem_init()
2458 xhci->dcbaa->dma = dma; in xhci_mem_init()
2459 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2461 &xhci->dcbaa->dma, xhci->dcbaa); in xhci_mem_init()
2462 xhci_write_64(xhci, dma, &xhci->op_regs->dcbaa_ptr); in xhci_mem_init()
2471 if (xhci->quirks & XHCI_TRB_OVERFETCH) in xhci_mem_init()
2473 xhci->segment_pool = dma_pool_create("xHCI ring segments", dev, in xhci_mem_init()
2474 TRB_SEGMENT_SIZE * 2, TRB_SEGMENT_SIZE * 2, xhci->page_size * 2); in xhci_mem_init()
2476 xhci->segment_pool = dma_pool_create("xHCI ring segments", dev, in xhci_mem_init()
2477 TRB_SEGMENT_SIZE, TRB_SEGMENT_SIZE, xhci->page_size); in xhci_mem_init()
2480 xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev, in xhci_mem_init()
2481 2112, 64, xhci->page_size); in xhci_mem_init()
2482 if (!xhci->segment_pool || !xhci->device_pool) in xhci_mem_init()
2488 xhci->small_streams_pool = in xhci_mem_init()
2489 dma_pool_create("xHCI 256 byte stream ctx arrays", in xhci_mem_init()
2491 xhci->medium_streams_pool = in xhci_mem_init()
2492 dma_pool_create("xHCI 1KB stream ctx arrays", in xhci_mem_init()
2498 if (!xhci->small_streams_pool || !xhci->medium_streams_pool) in xhci_mem_init()
2502 xhci->cmd_ring = xhci_ring_alloc(xhci, 1, 1, TYPE_COMMAND, 0, flags); in xhci_mem_init()
2503 if (!xhci->cmd_ring) in xhci_mem_init()
2505 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2506 "Allocated command ring at %p", xhci->cmd_ring); in xhci_mem_init()
2507 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "First segment DMA is 0x%pad", in xhci_mem_init()
2508 &xhci->cmd_ring->first_seg->dma); in xhci_mem_init()
2511 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring); in xhci_mem_init()
2513 (xhci->cmd_ring->first_seg->dma & (u64) ~CMD_RING_RSVD_BITS) | in xhci_mem_init()
2514 xhci->cmd_ring->cycle_state; in xhci_mem_init()
2515 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2517 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring); in xhci_mem_init()
2523 xhci->cmd_ring_reserved_trbs++; in xhci_mem_init()
2525 val = readl(&xhci->cap_regs->db_off); in xhci_mem_init()
2527 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2530 xhci->dba = (void __iomem *) xhci->cap_regs + val; in xhci_mem_init()
2533 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_mem_init()
2535 xhci->interrupters = kcalloc_node(xhci->max_interrupters, sizeof(*xhci->interrupters), in xhci_mem_init()
2538 ir = xhci_alloc_interrupter(xhci, 0, flags); in xhci_mem_init()
2542 if (xhci_add_interrupter(xhci, ir, 0)) in xhci_mem_init()
2545 xhci->isoc_bei_interval = AVOID_BEI_INTERVAL_MAX; in xhci_mem_init()
2553 xhci->devs[i] = NULL; in xhci_mem_init()
2555 if (scratchpad_alloc(xhci, flags)) in xhci_mem_init()
2557 if (xhci_setup_port_arrays(xhci, flags)) in xhci_mem_init()
2564 temp = readl(&xhci->op_regs->dev_notification); in xhci_mem_init()
2567 writel(temp, &xhci->op_regs->dev_notification); in xhci_mem_init()
2572 xhci_halt(xhci); in xhci_mem_init()
2573 xhci_reset(xhci, XHCI_RESET_SHORT_USEC); in xhci_mem_init()
2574 xhci_mem_cleanup(xhci); in xhci_mem_init()