Commit e5ba5ec8 authored by Pratyush Anand's avatar Pratyush Anand Committed by Felipe Balbi

usb: dwc3: gadget: fix scatter gather implementation

To work with scatter gather properly, fixes have been done in number of
functions. I will explain requirement of each fixes one by one.

start_slot: used to retrieve all request of SG during cleanup

dwc3_gadget_giveback: We need to skip link TRB if it was one of the
intermediate TRB of SG.

dwc3_prepare_one_trb: We need to track all submitted TRBs during
cleanup. Since, all TRBs would be serially allocated, so we can just
keep starting slot info and we can always find rest of them. We need to
pass sg node number, so that we cab appropriately program ISOC_FIRST/ISOC,
Chain etc.

dwc3_prepare_trbs: last_one should be set when it is last node
of SG as well as last node of request_list.

__dwc3_cleanup_done_trbs: It has been prepared after re-factorization of
dwc3_cleanup_done_reqs. It is called for each TRB of SG.
Signed-off-by: default avatarPratyush Anand <pratyush.anand@st.com>
Signed-off-by: default avatarFelipe Balbi <balbi@ti.com>
parent 1877d6c9
...@@ -581,6 +581,7 @@ struct dwc3_request { ...@@ -581,6 +581,7 @@ struct dwc3_request {
struct usb_request request; struct usb_request request;
struct list_head list; struct list_head list;
struct dwc3_ep *dep; struct dwc3_ep *dep;
u32 start_slot;
u8 epnum; u8 epnum;
struct dwc3_trb *trb; struct dwc3_trb *trb;
......
...@@ -241,21 +241,22 @@ void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, ...@@ -241,21 +241,22 @@ void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
int status) int status)
{ {
struct dwc3 *dwc = dep->dwc; struct dwc3 *dwc = dep->dwc;
int i;
if (req->queued) { if (req->queued) {
if (req->request.num_mapped_sgs) i = 0;
dep->busy_slot += req->request.num_mapped_sgs; do {
else
dep->busy_slot++; dep->busy_slot++;
/* /*
* Skip LINK TRB. We can't use req->trb and check for * Skip LINK TRB. We can't use req->trb and check for
* DWC3_TRBCTL_LINK_TRB because it points the TRB we just * DWC3_TRBCTL_LINK_TRB because it points the TRB we
* completed (not the LINK TRB). * just completed (not the LINK TRB).
*/ */
if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) && if (((dep->busy_slot & DWC3_TRB_MASK) ==
DWC3_TRB_NUM- 1) &&
usb_endpoint_xfer_isoc(dep->endpoint.desc)) usb_endpoint_xfer_isoc(dep->endpoint.desc))
dep->busy_slot++; dep->busy_slot++;
} while(++i < req->request.num_mapped_sgs);
} }
list_del(&req->list); list_del(&req->list);
req->trb = NULL; req->trb = NULL;
...@@ -749,7 +750,7 @@ static void dwc3_gadget_ep_free_request(struct usb_ep *ep, ...@@ -749,7 +750,7 @@ static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
*/ */
static void dwc3_prepare_one_trb(struct dwc3_ep *dep, static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
struct dwc3_request *req, dma_addr_t dma, struct dwc3_request *req, dma_addr_t dma,
unsigned length, unsigned last, unsigned chain) unsigned length, unsigned last, unsigned chain, unsigned node)
{ {
struct dwc3 *dwc = dep->dwc; struct dwc3 *dwc = dep->dwc;
struct dwc3_trb *trb; struct dwc3_trb *trb;
...@@ -765,14 +766,16 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep, ...@@ -765,14 +766,16 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
dep->free_slot++; dep->free_slot++;
trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK]; trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
dep->free_slot++;
if (!req->trb) { if (!req->trb) {
dwc3_gadget_move_request_queued(req); dwc3_gadget_move_request_queued(req);
req->trb = trb; req->trb = trb;
req->trb_dma = dwc3_trb_dma_offset(dep, trb); req->trb_dma = dwc3_trb_dma_offset(dep, trb);
req->start_slot = dep->free_slot & DWC3_TRB_MASK;
} }
dep->free_slot++;
trb->size = DWC3_TRB_SIZE_LENGTH(length); trb->size = DWC3_TRB_SIZE_LENGTH(length);
trb->bpl = lower_32_bits(dma); trb->bpl = lower_32_bits(dma);
trb->bph = upper_32_bits(dma); trb->bph = upper_32_bits(dma);
...@@ -783,9 +786,12 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep, ...@@ -783,9 +786,12 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
break; break;
case USB_ENDPOINT_XFER_ISOC: case USB_ENDPOINT_XFER_ISOC:
if (!node)
trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
else
trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
if (!req->request.no_interrupt) if (!req->request.no_interrupt && !chain)
trb->ctrl |= DWC3_TRB_CTRL_IOC; trb->ctrl |= DWC3_TRB_CTRL_IOC;
break; break;
...@@ -804,14 +810,13 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep, ...@@ -804,14 +810,13 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
trb->ctrl |= DWC3_TRB_CTRL_CSP; trb->ctrl |= DWC3_TRB_CTRL_CSP;
} else { } else if (last) {
if (chain)
trb->ctrl |= DWC3_TRB_CTRL_CHN;
if (last)
trb->ctrl |= DWC3_TRB_CTRL_LST; trb->ctrl |= DWC3_TRB_CTRL_LST;
} }
if (chain)
trb->ctrl |= DWC3_TRB_CTRL_CHN;
if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id); trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
...@@ -882,6 +887,7 @@ static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting) ...@@ -882,6 +887,7 @@ static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
list_for_each_entry_safe(req, n, &dep->request_list, list) { list_for_each_entry_safe(req, n, &dep->request_list, list) {
unsigned length; unsigned length;
dma_addr_t dma; dma_addr_t dma;
last_one = false;
if (req->request.num_mapped_sgs > 0) { if (req->request.num_mapped_sgs > 0) {
struct usb_request *request = &req->request; struct usb_request *request = &req->request;
...@@ -897,6 +903,8 @@ static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting) ...@@ -897,6 +903,8 @@ static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
if (i == (request->num_mapped_sgs - 1) || if (i == (request->num_mapped_sgs - 1) ||
sg_is_last(s)) { sg_is_last(s)) {
if (list_is_last(&req->list,
&dep->request_list))
last_one = true; last_one = true;
chain = false; chain = false;
} }
...@@ -909,7 +917,7 @@ static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting) ...@@ -909,7 +917,7 @@ static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
chain = false; chain = false;
dwc3_prepare_one_trb(dep, req, dma, length, dwc3_prepare_one_trb(dep, req, dma, length,
last_one, chain); last_one, chain, i);
if (last_one) if (last_one)
break; break;
...@@ -927,7 +935,7 @@ static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting) ...@@ -927,7 +935,7 @@ static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
last_one = 1; last_one = 1;
dwc3_prepare_one_trb(dep, req, dma, length, dwc3_prepare_one_trb(dep, req, dma, length,
last_one, false); last_one, false, 0);
if (last_one) if (last_one)
break; break;
...@@ -1642,24 +1650,14 @@ static void dwc3_gadget_release(struct device *dev) ...@@ -1642,24 +1650,14 @@ static void dwc3_gadget_release(struct device *dev)
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep, static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
struct dwc3_request *req, struct dwc3_trb *trb,
const struct dwc3_event_depevt *event, int status) const struct dwc3_event_depevt *event, int status)
{ {
struct dwc3_request *req;
struct dwc3_trb *trb;
unsigned int count; unsigned int count;
unsigned int s_pkt = 0; unsigned int s_pkt = 0;
unsigned int trb_status; unsigned int trb_status;
do {
req = next_request(&dep->req_queued);
if (!req) {
WARN_ON_ONCE(1);
return 1;
}
trb = req->trb;
if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
/* /*
* We continue despite the error. There is not much we * We continue despite the error. There is not much we
...@@ -1670,7 +1668,7 @@ static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep, ...@@ -1670,7 +1668,7 @@ static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
* fixes the root cause instead of looking away :) * fixes the root cause instead of looking away :)
*/ */
dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n", dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
dep->name, req->trb); dep->name, trb);
count = trb->size & DWC3_TRB_SIZE_MASK; count = trb->size & DWC3_TRB_SIZE_MASK;
if (dep->direction) { if (dep->direction) {
...@@ -1716,15 +1714,51 @@ static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep, ...@@ -1716,15 +1714,51 @@ static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
* gadget driver for further processing. * gadget driver for further processing.
*/ */
req->request.actual += req->request.length - count; req->request.actual += req->request.length - count;
dwc3_gadget_giveback(dep, req, status);
if (s_pkt) if (s_pkt)
break; return 1;
if ((event->status & DEPEVT_STATUS_LST) && if ((event->status & DEPEVT_STATUS_LST) &&
(trb->ctrl & (DWC3_TRB_CTRL_LST | (trb->ctrl & (DWC3_TRB_CTRL_LST |
DWC3_TRB_CTRL_HWO))) DWC3_TRB_CTRL_HWO)))
break; return 1;
if ((event->status & DEPEVT_STATUS_IOC) && if ((event->status & DEPEVT_STATUS_IOC) &&
(trb->ctrl & DWC3_TRB_CTRL_IOC)) (trb->ctrl & DWC3_TRB_CTRL_IOC))
return 1;
return 0;
}
static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
const struct dwc3_event_depevt *event, int status)
{
struct dwc3_request *req;
struct dwc3_trb *trb;
unsigned int slot;
unsigned int i;
int ret;
do {
req = next_request(&dep->req_queued);
if (!req) {
WARN_ON_ONCE(1);
return 1;
}
i = 0;
do {
slot = req->start_slot + i;
if ((slot == DWC3_TRB_NUM - 1) &&
usb_endpoint_xfer_isoc(dep->endpoint.desc))
slot++;
slot %= DWC3_TRB_NUM;
trb = &dep->trb_pool[slot];
ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
event, status);
if (ret)
break;
}while (++i < req->request.num_mapped_sgs);
dwc3_gadget_giveback(dep, req, status);
if (ret)
break; break;
} while (1); } while (1);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment