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

idwc3_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 move req_queued only for the first TRB
of the SG list. We will need trb_dma corresponding to each TRB while
submitting it to core for UPDATE TRANSFER. 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_start_update_transfer: This function will be called one per
request. So, we need to iterate over all the TRBs corresponding to SG
and need to issue UPDATE TRANSFER. For START TRANSFER we do not need to
iterate, so break after first issue of START TRANSFER command.

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

__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: Pratyush Anand <[email protected]>
---
 drivers/usb/dwc3/core.h   |    2 +-
 drivers/usb/dwc3/gadget.c |  290 ++++++++++++++++++++++++++-------------------
 2 files changed, 170 insertions(+), 122 deletions(-)

diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
index 916a4bf..2b79364 100644
--- a/drivers/usb/dwc3/core.h
+++ b/drivers/usb/dwc3/core.h
@@ -580,10 +580,10 @@ struct dwc3_request {
        struct usb_request      request;
        struct list_head        list;
        struct dwc3_ep          *dep;
+       u32                     start_slot;
 
        u8                      epnum;
        struct dwc3_trb         *trb;
-       dma_addr_t              trb_dma;
 
        unsigned                direction:1;
        unsigned                mapped:1;
diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index 3e7f824..3e649e6 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -241,21 +241,22 @@ void dwc3_gadget_giveback(struct dwc3_ep *dep, struct 
dwc3_request *req,
                int status)
 {
        struct dwc3                     *dwc = dep->dwc;
+       int                             i;
 
        if (req->queued) {
-               if (req->request.num_mapped_sgs)
-                       dep->busy_slot += req->request.num_mapped_sgs;
-               else
+               i = 0;
+               do {
                        dep->busy_slot++;
-
-               /*
-                * Skip LINK TRB. We can't use req->trb and check for
-                * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
-                * completed (not the LINK TRB).
-                */
-               if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
+                       /*
+                        * Skip LINK TRB. We can't use req->trb and check for
+                        * DWC3_TRBCTL_LINK_TRB because it points the TRB we
+                        * just completed (not the LINK TRB).
+                        */
+                       if (((dep->busy_slot & DWC3_TRB_MASK) ==
+                               DWC3_TRB_NUM- 1) &&
                                usb_endpoint_xfer_isoc(dep->endpoint.desc))
-                       dep->busy_slot++;
+                               dep->busy_slot++;
+               } while(++i < req->request.num_mapped_sgs);
        }
        list_del(&req->list);
        req->trb = NULL;
@@ -768,7 +769,7 @@ static void dwc3_gadget_ep_free_buffer(struct usb_ep *ep, 
void *buf,
  */
 static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
                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_trb         *trb;
@@ -784,14 +785,15 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
                dep->free_slot++;
 
        trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
-       dep->free_slot++;
 
        if (!req->trb) {
                dwc3_gadget_move_request_queued(req);
                req->trb = 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->bpl = lower_32_bits(dma);
        trb->bph = upper_32_bits(dma);
@@ -802,13 +804,16 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
                break;
 
        case USB_ENDPOINT_XFER_ISOC:
-               trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
+               if (!node)
+                       trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
+               else
+                       trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
 
                /*
                 * FIXME: Forcing interrupt to be received at every req,
                 * untill gadget sets no_interrupt flag
                 */
-               if (!req->request.no_interrupt)
+               if (!req->request.no_interrupt && !chain)
                        trb->ctrl |= DWC3_TRB_CTRL_IOC;
                break;
 
@@ -827,14 +832,13 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
        if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
                trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
                trb->ctrl |= DWC3_TRB_CTRL_CSP;
-       } else {
-               if (chain)
-                       trb->ctrl |= DWC3_TRB_CTRL_CHN;
-
-               if (last)
-                       trb->ctrl |= DWC3_TRB_CTRL_LST;
+       } else if (last) {
+               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)
                trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
 
@@ -848,39 +852,54 @@ static int __dwc3_start_update_transfer(struct dwc3_ep 
*dep,
        struct dwc3_gadget_ep_cmd_params params;
        int                             ret;
        u32                             cmd;
+       u32                             i = 0;
+       u32                             slot;
+       struct dwc3_trb                 *trb;
+       dma_addr_t                      trb_dma;
 
-       memset(&params, 0, sizeof(params));
-       params.param0 = upper_32_bits(req->trb_dma);
-       params.param1 = lower_32_bits(req->trb_dma);
+       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];
+               trb_dma = dwc3_trb_dma_offset(dep, trb);
 
-       if (start_new)
-               cmd = DWC3_DEPCMD_STARTTRANSFER;
-       else
-               cmd = DWC3_DEPCMD_UPDATETRANSFER;
+               memset(&params, 0, sizeof(params));
+               params.param0 = upper_32_bits(trb_dma);
+               params.param1 = lower_32_bits(trb_dma);
 
-       cmd |= DWC3_DEPCMD_PARAM(cmd_param);
-       ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
-       if (ret < 0) {
-               dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
+               if (start_new)
+                       cmd = DWC3_DEPCMD_STARTTRANSFER;
+               else
+                       cmd = DWC3_DEPCMD_UPDATETRANSFER;
 
-               /*
-                * FIXME we need to iterate over the list of requests
-                * here and stop, unmap, free and del each of the linked
-                * requests instead of what we do now.
-                */
-               usb_gadget_unmap_request(&dwc->gadget, &req->request,
-                               req->direction);
-               list_del(&req->list);
-               return ret;
-       }
+               cmd |= DWC3_DEPCMD_PARAM(cmd_param);
+               ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
+               if (ret < 0) {
+                       dev_dbg(dwc->dev, "failed to send STARTTRANSFER 
command\n");
 
-       dep->flags |= DWC3_EP_BUSY;
+                       /*
+                        * FIXME we need to iterate over the list of requests
+                        * here and stop, unmap, free and del each of the linked
+                        * requests instead of what we do now.
+                        */
+                       usb_gadget_unmap_request(&dwc->gadget, &req->request,
+                                       req->direction);
+                       list_del(&req->list);
+                       return ret;
+               }
 
-       if (start_new) {
-               dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
-                               dep->number);
-               WARN_ON_ONCE(!dep->resource_index);
-       }
+               dep->flags |= DWC3_EP_BUSY;
+
+               if (start_new) {
+                       dep->resource_index = 
dwc3_gadget_ep_get_transfer_index(dwc,
+                                       dep->number);
+                       WARN_ON_ONCE(!dep->resource_index);
+                       break;
+               }
+       } while (++i < req->request.num_mapped_sgs);
 
        return 0;
 }
@@ -952,6 +971,7 @@ static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, 
u16 cmd_param,
        list_for_each_entry_safe(req, n, &dep->request_list, list) {
                unsigned        length;
                dma_addr_t      dma;
+               last_one = false;
 
                if (req->request.num_mapped_sgs > 0) {
                        struct usb_request *request = &req->request;
@@ -967,7 +987,9 @@ static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, 
u16 cmd_param,
 
                                if (i == (request->num_mapped_sgs - 1) ||
                                                sg_is_last(s)) {
-                                       last_one = true;
+                                       if (list_is_last(&req->list,
+                                                       &dep->request_list))
+                                               last_one = true;
                                        chain = false;
                                }
 
@@ -979,9 +1001,9 @@ static int __dwc3_gadget_kick_transfer(struct dwc3_ep 
*dep, u16 cmd_param,
                                        chain = false;
 
                                dwc3_prepare_one_trb(dep, req, dma, length,
-                                               last_one, chain);
+                                               last_one, chain, i);
 
-                               if (!start_new)
+                               if (!start_new && !chain)
                                        __dwc3_start_update_transfer(dep, req,
                                                start_new, cmd_param);
                                if (last_one)
@@ -1000,7 +1022,7 @@ static int __dwc3_gadget_kick_transfer(struct dwc3_ep 
*dep, u16 cmd_param,
                                last_one = 1;
 
                        dwc3_prepare_one_trb(dep, req, dma, length,
-                                       last_one, false);
+                                       last_one, false, 0);
 
                        if (!start_new)
                                __dwc3_start_update_transfer(dep, req,
@@ -1657,89 +1679,115 @@ 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)
 {
-       struct dwc3_request     *req;
-       struct dwc3_trb         *trb;
        unsigned int            count;
        unsigned int            s_pkt = 0;
        unsigned int            trb_status;
 
+       if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
+               /*
+                * We continue despite the error. There is not much we
+                * can do. If we don't clean it up we loop forever. If
+                * we skip the TRB then it gets overwritten after a
+                * while since we use them in a ring buffer. A BUG()
+                * would help. Lets hope that if this occurs, someone
+                * fixes the root cause instead of looking away :)
+                */
+               dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
+                               dep->name, trb);
+       count = trb->size & DWC3_TRB_SIZE_MASK;
+
+       if (dep->direction) {
+               if (count) {
+                       trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
+                       if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
+                               dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
+                                               dep->name);
+                               /*
+                                * If missed isoc occurred and there is
+                                * no request queued then issue END
+                                * TRANSFER, so that core generates
+                                * next xfernotready and we will issue
+                                * a fresh START TRANSFER.
+                                * If there are still queued request
+                                * then wait, do not issue either END
+                                * or UPDATE TRANSFER, just attach next
+                                * request in request_list during
+                                * giveback.If any future queued request
+                                * is successfully transferred then we
+                                * will issue UPDATE TRANSFER for all
+                                * request in the request_list.
+                                */
+                               dep->flags |= DWC3_EP_MISSED_ISOC;
+                       } else {
+                               dev_err(dwc->dev, "incomplete IN transfer %s\n",
+                                               dep->name);
+                               status = -ECONNRESET;
+                       }
+               } else {
+                       dep->flags &= ~DWC3_EP_MISSED_ISOC;
+               }
+       } else {
+               if (count && (event->status & DEPEVT_STATUS_SHORT))
+                       s_pkt = 1;
+       }
+
+       /*
+        * We assume here we will always receive the entire data block
+        * which we should receive. Meaning, if we program RX to
+        * receive 4K but we receive only 2K, we assume that's all we
+        * should receive and we simply bounce the request back to the
+        * gadget driver for further processing.
+        */
+       req->request.actual += req->request.length - count;
+       if (s_pkt)
+               return 1;
+       if ((event->status & DEPEVT_STATUS_LST) &&
+                       (trb->ctrl & (DWC3_TRB_CTRL_LST |
+                               DWC3_TRB_CTRL_HWO)))
+               return 1;
+       if ((event->status & DEPEVT_STATUS_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];
 
-               trb = req->trb;
-
-               if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
-                       /*
-                        * We continue despite the error. There is not much we
-                        * can do. If we don't clean it up we loop forever. If
-                        * we skip the TRB then it gets overwritten after a
-                        * while since we use them in a ring buffer. A BUG()
-                        * would help. Lets hope that if this occurs, someone
-                        * fixes the root cause instead of looking away :)
-                        */
-                       dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
-                                       dep->name, req->trb);
-               count = trb->size & DWC3_TRB_SIZE_MASK;
-
-               if (dep->direction) {
-                       if (count) {
-                               trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
-                               if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
-                                       dev_dbg(dwc->dev, "incomplete IN 
transfer %s\n",
-                                                       dep->name);
-                                       /*
-                                        * If missed isoc occurred and there is
-                                        * no request queued then issue END
-                                        * TRANSFER, so that core generates
-                                        * next xfernotready and we will issue
-                                        * a fresh START TRANSFER.
-                                        * If there are still queued request
-                                        * then wait, do not issue either END
-                                        * or UPDATE TRANSFER, just attach next
-                                        * request in request_list during
-                                        * giveback.If any future queued request
-                                        * is successfully transferred then we
-                                        * will issue UPDATE TRANSFER for all
-                                        * request in the request_list.
-                                        */
-                                       dep->flags |= DWC3_EP_MISSED_ISOC;
-                               } else {
-                                       dev_err(dwc->dev, "incomplete IN 
transfer %s\n",
-                                                       dep->name);
-                                       status = -ECONNRESET;
-                               }
-                       } else {
-                               dep->flags &= ~DWC3_EP_MISSED_ISOC;
-                       }
-               } else {
-                       if (count && (event->status & DEPEVT_STATUS_SHORT))
-                               s_pkt = 1;
-               }
+                       ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
+                                       event, status);
+                       if (ret)
+                               break;
+               }while (++i < req->request.num_mapped_sgs);
 
-               /*
-                * We assume here we will always receive the entire data block
-                * which we should receive. Meaning, if we program RX to
-                * receive 4K but we receive only 2K, we assume that's all we
-                * should receive and we simply bounce the request back to the
-                * gadget driver for further processing.
-                */
-               req->request.actual += req->request.length - count;
                dwc3_gadget_giveback(dep, req, status);
-               if (s_pkt)
-                       break;
-               if ((event->status & DEPEVT_STATUS_LST) &&
-                               (trb->ctrl & (DWC3_TRB_CTRL_LST |
-                                               DWC3_TRB_CTRL_HWO)))
-                       break;
-               if ((event->status & DEPEVT_STATUS_IOC) &&
-                               (trb->ctrl & DWC3_TRB_CTRL_IOC))
+
+               if (ret)
                        break;
        } while (1);
 
-- 
1.7.5.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to