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

1 // SPDX-License-Identifier: GPL-2.0+
3 * USB HOST XHCI Controller stack
5 * Based on xHCI host controller driver in linux-kernel
22 #include "xhci.h"
38 if (ring == ctrl->event_ring) in last_trb()
39 return trb == &seg->trbs[TRBS_PER_SEGMENT]; in last_trb()
41 return TRB_TYPE_LINK_LE32(trb->link.control); in last_trb()
59 if (ring == ctrl->event_ring) in last_trb_on_last_seg()
60 return ((trb == &seg->trbs[TRBS_PER_SEGMENT]) && in last_trb_on_last_seg()
61 (seg->next == ring->first_seg)); in last_trb_on_last_seg()
63 return le32_to_cpu(trb->link.control) & LINK_TOGGLE; in last_trb_on_last_seg()
78 * xHCI hardware can't handle the chain bit being cleared on a link TRB.
94 chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN; in inc_enq()
95 next = ++(ring->enqueue); in inc_enq()
101 while (last_trb(ctrl, ring, ring->enq_seg, next)) { in inc_enq()
102 if (ring != ctrl->event_ring) { in inc_enq()
120 next->link.control &= cpu_to_le32(~TRB_CHAIN); in inc_enq()
121 next->link.control |= cpu_to_le32(chain); in inc_enq()
123 next->link.control ^= cpu_to_le32(TRB_CYCLE); in inc_enq()
129 ring->enq_seg, next)) in inc_enq()
130 ring->cycle_state = (ring->cycle_state ? 0 : 1); in inc_enq()
132 ring->enq_seg = ring->enq_seg->next; in inc_enq()
133 ring->enqueue = ring->enq_seg->trbs; in inc_enq()
134 next = ring->enqueue; in inc_enq()
154 if (last_trb(ctrl, ring, ring->deq_seg, ring->dequeue)) { in inc_deq()
155 if (ring == ctrl->event_ring && in inc_deq()
157 ring->deq_seg, ring->dequeue)) { in inc_deq()
158 ring->cycle_state = (ring->cycle_state ? 0 : 1); in inc_deq()
160 ring->deq_seg = ring->deq_seg->next; in inc_deq()
161 ring->dequeue = ring->deq_seg->trbs; in inc_deq()
163 ring->dequeue++; in inc_deq()
165 } while (last_trb(ctrl, ring, ring->deq_seg, ring->dequeue)); in inc_deq()
169 * Generic function for queueing a TRB on a ring.
188 trb = &ring->enqueue->generic; in queue_trb()
191 trb->field[i] = cpu_to_le32(trb_fields[i]); in queue_trb()
212 union xhci_trb *next = ep_ring->enqueue; in prepare_ring()
222 return -ENOENT; in prepare_ring()
225 return -EINVAL; in prepare_ring()
234 return -EINVAL; in prepare_ring()
237 while (last_trb(ctrl, ep_ring, ep_ring->enq_seg, next)) { in prepare_ring()
242 next->link.control &= cpu_to_le32(~TRB_CHAIN); in prepare_ring()
244 next->link.control ^= cpu_to_le32(TRB_CYCLE); in prepare_ring()
250 ep_ring->enq_seg, next)) in prepare_ring()
251 ep_ring->cycle_state = (ep_ring->cycle_state ? 0 : 1); in prepare_ring()
252 ep_ring->enq_seg = ep_ring->enq_seg->next; in prepare_ring()
253 ep_ring->enqueue = ep_ring->enq_seg->trbs; in prepare_ring()
254 next = ep_ring->enqueue; in prepare_ring()
261 * Generic function for queueing a command TRB on the command ring.
277 BUG_ON(prepare_ring(ctrl, ctrl->cmd_ring, EP_STATE_RUNNING)); in xhci_queue_command()
283 ctrl->cmd_ring->cycle_state; in xhci_queue_command()
292 queue_trb(ctrl, ctrl->cmd_ring, false, fields); in xhci_queue_command()
295 xhci_writel(&ctrl->dba->doorbell[0], DB_VALUE_HOST); in xhci_queue_command()
308 u32 max = (1 << (21 - 17 + 1)) - 1; in xhci_td_remainder()
334 /* One TRB with a zero-length data packet. */ in xhci_v1_0_td_remainder()
344 if ((total_packet_count - packets_transferred) > 31) in xhci_v1_0_td_remainder()
346 return (total_packet_count - packets_transferred) << 17; in xhci_v1_0_td_remainder()
369 start_trb->field[3] |= cpu_to_le32(start_cycle); in giveback_first_trb()
371 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE); in giveback_first_trb()
376 xhci_writel(&ctrl->dba->doorbell[udev->slot_id], in giveback_first_trb()
382 /**** POLLING mechanism for XHCI ****/
395 inc_deq(ctrl, ctrl->event_ring); in xhci_acknowledge_event()
398 xhci_writeq(&ctrl->ir_set->erst_dequeue, in xhci_acknowledge_event()
399 (uintptr_t)ctrl->event_ring->dequeue | ERST_EHB); in xhci_acknowledge_event()
412 xhci_inval_cache((uintptr_t)ctrl->event_ring->dequeue, in event_ready()
415 event = ctrl->event_ring->dequeue; in event_ready()
418 if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) != in event_ready()
419 ctrl->event_ring->cycle_state) in event_ready()
440 union xhci_trb *event = ctrl->event_ring->dequeue; in xhci_wait_for_event()
445 type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags)); in xhci_wait_for_event()
456 le32_to_cpu(event->generic.field[2])) != in xhci_wait_for_event()
459 printf("Unexpected XHCI event TRB, skipping... " in xhci_wait_for_event()
461 le32_to_cpu(event->generic.field[0]), in xhci_wait_for_event()
462 le32_to_cpu(event->generic.field[1]), in xhci_wait_for_event()
463 le32_to_cpu(event->generic.field[2]), in xhci_wait_for_event()
464 le32_to_cpu(event->generic.field[3])); in xhci_wait_for_event()
472 printf("XHCI timeout on event type %d... cannot recover.\n", expected); in xhci_wait_for_event()
487 struct xhci_ring *ring = ctrl->devs[udev->slot_id]->eps[ep_index].ring; in abort_td()
491 xhci_queue_command(ctrl, NULL, udev->slot_id, ep_index, TRB_STOP_RING); in abort_td()
494 field = le32_to_cpu(event->trans_event.flags); in abort_td()
495 BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id); in abort_td()
497 BUG_ON(GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len in abort_td()
502 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) in abort_td()
503 != udev->slot_id || GET_COMP_CODE(le32_to_cpu( in abort_td()
504 event->event_cmd.status)) != COMP_SUCCESS); in abort_td()
507 xhci_queue_command(ctrl, (void *)((uintptr_t)ring->enqueue | in abort_td()
508 ring->cycle_state), udev->slot_id, ep_index, TRB_SET_DEQ); in abort_td()
510 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) in abort_td()
511 != udev->slot_id || GET_COMP_CODE(le32_to_cpu( in abort_td()
512 event->event_cmd.status)) != COMP_SUCCESS); in abort_td()
519 udev->act_len = min(length, length - in record_transfer_result()
520 (int)EVENT_TRB_LEN(le32_to_cpu(event->trans_event.transfer_len))); in record_transfer_result()
522 switch (GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len))) { in record_transfer_result()
524 BUG_ON(udev->act_len != length); in record_transfer_result()
527 udev->status = 0; in record_transfer_result()
530 udev->status = USB_ST_STALLED; in record_transfer_result()
534 udev->status = USB_ST_BUF_ERR; in record_transfer_result()
537 udev->status = USB_ST_BABBLE_DET; in record_transfer_result()
540 udev->status = 0x80; /* USB_ST_TOO_LAZY_TO_MAKE_A_NEW_MACRO */ in record_transfer_result()
552 * @return returns 0 if successful else -1 on failure
564 int slot_id = udev->slot_id; in xhci_bulk_tx()
583 virt_dev = ctrl->devs[slot_id]; in xhci_bulk_tx()
585 xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes, in xhci_bulk_tx()
586 virt_dev->out_ctx->size); in xhci_bulk_tx()
588 ep_ctx = xhci_get_ep_ctx(ctrl, virt_dev->out_ctx, ep_index); in xhci_bulk_tx()
590 ring = virt_dev->eps[ep_index].ring; in xhci_bulk_tx()
593 * XHCI Spec puts restriction( TABLE 49 and 6.4.1 section of XHCI Spec) in xhci_bulk_tx()
597 running_total = TRB_MAX_BUFF_SIZE - in xhci_bulk_tx()
598 (lower_32_bits(val_64) & (TRB_MAX_BUFF_SIZE - 1)); in xhci_bulk_tx()
600 running_total &= TRB_MAX_BUFF_SIZE - 1; in xhci_bulk_tx()
604 * zero-length transfer, we need at least one TRB in xhci_bulk_tx()
621 le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK); in xhci_bulk_tx()
630 start_trb = &ring->enqueue->generic; in xhci_bulk_tx()
631 start_cycle = ring->cycle_state; in xhci_bulk_tx()
641 * XHCI Spec puts restriction( TABLE 49 and 6.4.1 section of XHCI Spec) in xhci_bulk_tx()
655 /* Queue the first TRB, even if it's zero-length */ in xhci_bulk_tx()
665 field |= ring->cycle_state; in xhci_bulk_tx()
682 if (HC_VERSION(xhci_readl(&ctrl->hccr->cr_capbase)) < 0x100) in xhci_bulk_tx()
683 remainder = xhci_td_remainder(length - running_total); in xhci_bulk_tx()
689 num_trbs - 1); in xhci_bulk_tx()
703 --num_trbs; in xhci_bulk_tx()
709 trb_buff_len = min((length - running_total), TRB_MAX_BUFF_SIZE); in xhci_bulk_tx()
716 debug("XHCI bulk transfer timed out, aborting...\n"); in xhci_bulk_tx()
718 udev->status = USB_ST_NAK_REC; /* closest thing to a timeout */ in xhci_bulk_tx()
719 udev->act_len = 0; in xhci_bulk_tx()
720 return -ETIMEDOUT; in xhci_bulk_tx()
722 field = le32_to_cpu(event->trans_event.flags); in xhci_bulk_tx()
726 BUG_ON(*(void **)(uintptr_t)le64_to_cpu(event->trans_event.buffer) - in xhci_bulk_tx()
733 return (udev->status != USB_ST_NOT_PROC) ? 0 : -1; in xhci_bulk_tx()
758 int slot_id = udev->slot_id; in xhci_ctrl_tx()
761 struct xhci_virt_device *virt_dev = ctrl->devs[slot_id]; in xhci_ctrl_tx()
766 req->request, req->request, in xhci_ctrl_tx()
767 req->requesttype, req->requesttype, in xhci_ctrl_tx()
768 le16_to_cpu(req->value), le16_to_cpu(req->value), in xhci_ctrl_tx()
769 le16_to_cpu(req->index)); in xhci_ctrl_tx()
773 ep_ring = virt_dev->eps[ep_index].ring; in xhci_ctrl_tx()
779 if (udev->speed == USB_SPEED_FULL) { in xhci_ctrl_tx()
785 xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes, in xhci_ctrl_tx()
786 virt_dev->out_ctx->size); in xhci_ctrl_tx()
789 ep_ctx = xhci_get_ep_ctx(ctrl, virt_dev->out_ctx, ep_index); in xhci_ctrl_tx()
807 le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK); in xhci_ctrl_tx()
817 start_trb = &ep_ring->enqueue->generic; in xhci_ctrl_tx()
818 start_cycle = ep_ring->cycle_state; in xhci_ctrl_tx()
822 /* Queue setup TRB - see section 6.4.1.2.1 */ in xhci_ctrl_tx()
829 /* xHCI 1.0 6.4.1.2.1: Transfer Type field */ in xhci_ctrl_tx()
830 if (HC_VERSION(xhci_readl(&ctrl->hccr->cr_capbase)) == 0x100) { in xhci_ctrl_tx()
832 if (req->requesttype & USB_DIR_IN) in xhci_ctrl_tx()
839 debug("req->requesttype = %d, req->request = %d," in xhci_ctrl_tx()
840 "le16_to_cpu(req->value) = %d," in xhci_ctrl_tx()
841 "le16_to_cpu(req->index) = %d," in xhci_ctrl_tx()
842 "le16_to_cpu(req->length) = %d\n", in xhci_ctrl_tx()
843 req->requesttype, req->request, le16_to_cpu(req->value), in xhci_ctrl_tx()
844 le16_to_cpu(req->index), le16_to_cpu(req->length)); in xhci_ctrl_tx()
846 trb_fields[0] = req->requesttype | req->request << 8 | in xhci_ctrl_tx()
847 le16_to_cpu(req->value) << 16; in xhci_ctrl_tx()
848 trb_fields[1] = le16_to_cpu(req->index) | in xhci_ctrl_tx()
849 le16_to_cpu(req->length) << 16; in xhci_ctrl_tx()
857 /* Re-initializing field to zero */ in xhci_ctrl_tx()
874 if (req->requesttype & USB_DIR_IN) in xhci_ctrl_tx()
881 trb_fields[3] = field | ep_ring->cycle_state; in xhci_ctrl_tx()
888 * Queue status TRB - in xhci_ctrl_tx()
894 if (length > 0 && req->requesttype & USB_DIR_IN) in xhci_ctrl_tx()
905 ep_ring->cycle_state; in xhci_ctrl_tx()
914 field = le32_to_cpu(event->trans_event.flags); in xhci_ctrl_tx()
922 /* Invalidate buffer to make it available to usb-core */ in xhci_ctrl_tx()
926 if (GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len)) in xhci_ctrl_tx()
937 return (udev->status != USB_ST_NOT_PROC) ? 0 : -1; in xhci_ctrl_tx()
940 debug("XHCI control transfer timed out, aborting...\n"); in xhci_ctrl_tx()
942 udev->status = USB_ST_NAK_REC; in xhci_ctrl_tx()
943 udev->act_len = 0; in xhci_ctrl_tx()
944 return -ETIMEDOUT; in xhci_ctrl_tx()