Signed-off-by: Paul Zimmerman <pa...@synopsys.com>
---
 drivers/usb/dwc2/hcd_ddma.c | 1168 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 1168 insertions(+), 0 deletions(-)
 create mode 100644 drivers/usb/dwc2/hcd_ddma.c

diff --git a/drivers/usb/dwc2/hcd_ddma.c b/drivers/usb/dwc2/hcd_ddma.c
new file mode 100644
index 0000000..8e63dc3
--- /dev/null
+++ b/drivers/usb/dwc2/hcd_ddma.c
@@ -0,0 +1,1168 @@
+/*
+ * hcd_ddma.c - DesignWare HS OTG Controller descriptor DMA routines
+ *
+ * Copyright (C) 2004-2012 Synopsys, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The names of the above-listed copyright holders may not be used
+ *    to endorse or promote products derived from this software without
+ *    specific prior written permission.
+ *
+ * ALTERNATIVELY, this software may be distributed under the terms of the
+ * GNU General Public License ("GPL") as published by the Free Software
+ * Foundation; either version 2 of the License, or (at your option) any
+ * later version.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * This file contains the Descriptor DMA implementation for Host mode
+ */
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/spinlock.h>
+#include <linux/interrupt.h>
+#include <linux/dma-mapping.h>
+#include <linux/debugfs.h>
+#include <linux/seq_file.h>
+#include <linux/delay.h>
+#include <linux/io.h>
+#include <linux/slab.h>
+#include <linux/usb.h>
+
+#include <linux/usb/hcd.h>
+#include <linux/usb/ch11.h>
+#include <linux/usb/gadget.h>
+#include <linux/usb/ch9.h>
+
+#include "core.h"
+#include "hcd.h"
+
+static u16 frame_list_idx(u16 frame)
+{
+       return frame & (MAX_FRLIST_EN_NUM - 1);
+}
+
+static u16 desclist_idx_inc(u16 idx, u16 inc, u8 speed)
+{
+       return (idx + inc) &
+               ((speed == DWC2_EP_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
+                 MAX_DMA_DESC_NUM_GENERIC) - 1);
+}
+
+static u16 desclist_idx_dec(u16 idx, u16 inc, u8 speed)
+{
+       return (idx - inc) &
+               ((speed == DWC2_EP_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
+                 MAX_DMA_DESC_NUM_GENERIC) - 1);
+}
+
+static u16 max_desc_num(struct dwc2_qh *qh)
+{
+       return (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
+               qh->dev_speed == DWC2_EP_SPEED_HIGH)
+               ? MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC;
+}
+
+static u16 frame_incr_val(struct dwc2_qh *qh)
+{
+       return qh->dev_speed == DWC2_EP_SPEED_HIGH
+               ? (qh->interval + 8 - 1) / 8 : qh->interval;
+}
+
+static int desc_list_alloc(struct dwc2_hcd *hcd, struct dwc2_qh *qh,
+                          gfp_t flags)
+{
+       qh->desc_list = dma_alloc_coherent(hcd->dev,
+                               sizeof(struct dwc2_host_dma_desc) *
+                               max_desc_num(qh), &qh->desc_list_dma, flags);
+
+       if (!qh->desc_list) {
+               dev_err(hcd->dev, "%s: DMA descriptor list allocation failed\n",
+                       __func__);
+               return -ENOMEM;
+       }
+
+       memset(qh->desc_list, 0,
+              sizeof(struct dwc2_host_dma_desc) * max_desc_num(qh));
+
+       qh->n_bytes = kzalloc(sizeof(u32) * max_desc_num(qh), flags);
+       if (!qh->n_bytes) {
+               dev_err(hcd->dev,
+                       "%s: Failed to allocate array for descriptors' size 
actual values\n",
+                       __func__);
+               dma_free_coherent(hcd->dev, sizeof(struct dwc2_host_dma_desc) *
+                                 max_desc_num(qh), qh->desc_list,
+                                 qh->desc_list_dma);
+               qh->desc_list = NULL;
+               return -ENOMEM;
+       }
+
+       return 0;
+}
+
+static void desc_list_free(struct dwc2_hcd *hcd, struct dwc2_qh *qh)
+{
+       if (qh->desc_list) {
+               dma_free_coherent(hcd->dev, sizeof(struct dwc2_host_dma_desc) *
+                                 max_desc_num(qh), qh->desc_list,
+                                 qh->desc_list_dma);
+               qh->desc_list = NULL;
+       }
+
+       kfree(qh->n_bytes);
+       qh->n_bytes = NULL;
+}
+
+static int frame_list_alloc(struct dwc2_hcd *hcd, gfp_t mem_flags)
+{
+       if (hcd->frame_list)
+               return 0;
+
+       hcd->frame_list = dma_alloc_coherent(hcd->dev, 4 * MAX_FRLIST_EN_NUM,
+                                            &hcd->frame_list_dma, mem_flags);
+       if (!hcd->frame_list) {
+               dev_err(hcd->dev, "%s: Frame List allocation failed\n",
+                       __func__);
+               return -ENOMEM;
+       }
+
+       memset(hcd->frame_list, 0, 4 * MAX_FRLIST_EN_NUM);
+       return 0;
+}
+
+static void frame_list_free(struct dwc2_hcd *hcd)
+{
+       if (!hcd->frame_list)
+               return;
+
+       dma_free_coherent(hcd->dev, 4 * MAX_FRLIST_EN_NUM, hcd->frame_list,
+                         hcd->frame_list_dma);
+       hcd->frame_list = NULL;
+}
+
+static void per_sched_enable(struct dwc2_hcd *hcd, u16 fr_list_en)
+{
+       u32 hcfg = readl(hcd->regs + HCFG);
+       u32 frlisten;
+
+       if (hcfg & HCFG_PERSCHEDENA)
+               /* already enabled */
+               return;
+
+       writel(hcd->frame_list_dma, hcd->regs + HFLBADDR);
+
+       switch (fr_list_en) {
+       case 64:
+               frlisten = 3;
+               break;
+       case 32:
+               frlisten = 2;
+               break;
+       case 16:
+               frlisten = 1;
+               break;
+       case 8:
+       default:
+               frlisten = 0;
+               break;
+       }
+
+       hcfg &= ~HCFG_FRLISTEN_MASK;
+       hcfg |= frlisten << HCFG_FRLISTEN_SHIFT | HCFG_PERSCHEDENA;
+
+       dev_dbg(hcd->dev, "Enabling Periodic schedule\n");
+       writel(hcfg, hcd->regs + HCFG);
+}
+
+static void per_sched_disable(struct dwc2_hcd *hcd)
+{
+       u32 hcfg = readl(hcd->regs + HCFG);
+
+       if (!(hcfg & HCFG_PERSCHEDENA))
+               /* already disabled */
+               return;
+
+       hcfg &= ~HCFG_PERSCHEDENA;
+
+       dev_dbg(hcd->dev, "Disabling Periodic schedule\n");
+       writel(hcfg, hcd->regs + HCFG);
+}
+
+/*
+ * Activates/Deactivates FrameList entries for the channel based on endpoint
+ * servicing period
+ */
+static void update_frame_list(struct dwc2_hcd *hcd, struct dwc2_qh *qh,
+                             int enable)
+{
+       struct dwc2_hc *hc;
+       u16 i, j, inc;
+
+       if (!qh->channel) {
+               dev_err(hcd->dev, "qh->channel = %p", qh->channel);
+               return;
+       }
+
+       if (!hcd) {
+               dev_err(hcd->dev, "------hcd = %p", hcd);
+               return;
+       }
+
+       if (!hcd->frame_list) {
+               dev_err(hcd->dev, "-------hcd->frame_list = %p",
+                       hcd->frame_list);
+               return;
+       }
+
+       hc = qh->channel;
+       inc = frame_incr_val(qh);
+       if (qh->ep_type == USB_ENDPOINT_XFER_ISOC)
+               i = frame_list_idx(qh->sched_frame);
+       else
+               i = 0;
+
+       j = i;
+       do {
+               if (enable)
+                       hcd->frame_list[j] |= 1 << hc->hc_num;
+               else
+                       hcd->frame_list[j] &= ~(1 << hc->hc_num);
+               j = (j + inc) & (MAX_FRLIST_EN_NUM - 1);
+       } while (j != i);
+       if (!enable)
+               return;
+       hc->schinfo = 0;
+       if (qh->channel->speed == DWC2_EP_SPEED_HIGH) {
+               j = 1;
+               /* TODO - check this */
+               inc = (8 + qh->interval - 1) / qh->interval;
+               for (i = 0; i < inc; i++) {
+                       hc->schinfo |= j;
+                       j = j << qh->interval;
+               }
+       } else {
+               hc->schinfo = 0xff;
+       }
+}
+
+static void release_channel_ddma(struct dwc2_hcd *hcd, struct dwc2_qh *qh)
+{
+       struct dwc2_hc *hc = qh->channel;
+
+       if (dwc2_qh_is_non_per(qh))
+               hcd->non_periodic_channels--;
+       else
+               update_frame_list(hcd, qh, 0);
+
+       /*
+        * The condition is added to prevent double cleanup try in case of
+        * device disconnect. See channel cleanup in dwc2_hcd_disconnect().
+        */
+       if (hc->qh) {
+               dwc2_hc_cleanup(hcd, hc);
+               list_add_tail(&hc->hc_list_entry, &hcd->free_hc_list);
+               hc->qh = NULL;
+       }
+
+       qh->channel = NULL;
+       qh->ntd = 0;
+
+       if (qh->desc_list)
+               memset(qh->desc_list, 0,
+                      sizeof(struct dwc2_host_dma_desc) * max_desc_num(qh));
+}
+
+/**
+ * dwc2_hcd_qh_init_ddma() - Initializes a QH structure's Descriptor DMA
+ * related members
+ *
+ * @hcd: The HCD state structure for the DWC OTG controller
+ * @qh:  The QH to init
+ *
+ * Return: 0 if successful, negative error code otherwise
+ *
+ * Allocates memory for the descriptor list. For the first periodic QH,
+ * allocates memory for the FrameList and enables periodic scheduling.
+ */
+int dwc2_hcd_qh_init_ddma(struct dwc2_hcd *hcd, struct dwc2_qh *qh,
+                         gfp_t mem_flags)
+{
+       int retval = 0;
+
+       if (qh->do_split) {
+               dev_err(hcd->dev,
+                       "SPLIT Transfers are not supported in Descriptor 
DMA.\n");
+               return -1;
+       }
+
+       retval = desc_list_alloc(hcd, qh, mem_flags);
+
+       if (retval == 0 &&
+           (qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
+            qh->ep_type == USB_ENDPOINT_XFER_INT)) {
+               if (!hcd->frame_list) {
+                       retval = frame_list_alloc(hcd, mem_flags);
+                       /* Enable periodic schedule on first periodic QH */
+                       if (retval == 0)
+                               per_sched_enable(hcd, MAX_FRLIST_EN_NUM);
+               }
+       }
+
+       qh->ntd = 0;
+       return retval;
+}
+
+/**
+ * dwc2_hcd_qh_free_ddma() - Frees a QH structure's Descriptor DMA related
+ * members
+ *
+ * @hcd: The HCD state structure for the DWC OTG controller
+ * @qh:  The QH to free
+ *
+ * Frees descriptor list memory associated with the QH. If QH is periodic and
+ * the last, frees FrameList memory and disables periodic scheduling.
+ */
+void dwc2_hcd_qh_free_ddma(struct dwc2_hcd *hcd, struct dwc2_qh *qh)
+{
+       desc_list_free(hcd, qh);
+
+       /*
+        * Channel still assigned due to some reasons.
+        * Seen on Isoc URB dequeue. Channel halted but no subsequent
+        * ChHalted interrupt to release the channel. Afterwards
+        * when it comes here from endpoint disable routine
+        * channel remains assigned.
+        */
+       if (qh->channel)
+               release_channel_ddma(hcd, qh);
+
+       if ((qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
+            qh->ep_type == USB_ENDPOINT_XFER_INT) &&
+           !hcd->periodic_channels && hcd->frame_list) {
+               per_sched_disable(hcd);
+               frame_list_free(hcd);
+       }
+}
+
+static u8 frame_to_desc_idx(struct dwc2_qh *qh, u16 frame_idx)
+{
+       if (qh->dev_speed == DWC2_EP_SPEED_HIGH)
+               /* Descriptor set (8 descriptors) index which is 8-aligned */
+               return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
+       else
+               return frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1);
+}
+
+/*
+ * Determine starting frame for Isochronous transfer.
+ * Few frames skipped to prevent race condition with HC.
+ */
+static u16 calc_starting_frame(struct dwc2_hcd *hcd, struct dwc2_qh *qh,
+                              u16 *skip_frames)
+{
+       u16 frame;
+
+       hcd->frame_number = dwc2_hcd_get_frame_number(hcd);
+
+       /* sched_frame is always frame number (not uFrame) both in FS and HS! */
+
+       /*
+        * skip_frames is used to limit activated descriptors number
+        * to avoid the situation when HC services the last activated
+        * descriptor firstly.
+        * Example for FS:
+        * Current frame is 1, scheduled frame is 3. Since HC always fetches
+        * the descriptor corresponding to curr_frame+1, the descriptor
+        * corresponding to frame 2 will be fetched. If the number of
+        * descriptors is max=64 (or greather) the list will be fully programmed
+        * with Active descriptors and it is possible case (rare) that the
+        * latest descriptor(considering rollback) corresponding to frame 2 will
+        * be serviced first. HS case is more probable because, in fact, up to
+        * 11 uframes (16 in the code) may be skipped.
+        */
+       if (qh->dev_speed == DWC2_EP_SPEED_HIGH) {
+               /*
+                * Consider uframe counter also, to start xfer asap. If half of
+                * the frame elapsed skip 2 frames otherwise just 1 frame.
+                * Starting descriptor index must be 8-aligned, so if the
+                * current frame is near to complete the next one is skipped as
+                * well.
+                */
+               if (dwc2_micro_frame_num(hcd->frame_number) >= 5) {
+                       *skip_frames = 2 * 8;
+                       frame = dwc2_frame_num_inc(hcd->frame_number,
+                                                  *skip_frames);
+               } else {
+                       *skip_frames = 1 * 8;
+                       frame = dwc2_frame_num_inc(hcd->frame_number,
+                                                  *skip_frames);
+               }
+
+               frame = dwc2_full_frame_num(frame);
+       } else {
+               /*
+                * Two frames are skipped for FS - the current and the next.
+                * But for descriptor programming, 1 frame (descriptor) is
+                * enough, see example above.
+                */
+               *skip_frames = 1;
+               frame = dwc2_frame_num_inc(hcd->frame_number, 2);
+       }
+
+       return frame;
+}
+
+/*
+ * Calculate initial descriptor index for isochronous transfer based on
+ * scheduled frame
+ */
+static u16 recalc_initial_desc_idx(struct dwc2_hcd *hcd, struct dwc2_qh *qh)
+{
+       u16 frame, fr_idx, fr_idx_tmp, skip_frames;
+
+       /*
+        * With current ISOC processing algorithm the channel is being released
+        * when no more QTDs in the list (qh->ntd == 0). Thus this function is
+        * called only when qh->ntd == 0 and qh->channel == 0.
+        *
+        * So qh->channel != NULL branch is not used and just not removed from
+        * the source file. It is required for another possible approach which
+        * is, do not disable and release the channel when ISOC session
+        * completed, just move QH to inactive schedule until new QTD arrives.
+        * On new QTD, the QH moved back to 'ready' schedule, starting frame and
+        * therefore starting desc_index are recalculated. In this case channel
+        * is released only on ep_disable.
+        */
+
+       /*
+        * Calculate starting descriptor index. For INTERRUPT endpoint it is
+        * always 0.
+        */
+       if (qh->channel) {
+               frame = calc_starting_frame(hcd, qh, &skip_frames);
+               /*
+                * Calculate initial descriptor index based on FrameList current
+                * bitmap and servicing period
+                */
+               fr_idx_tmp = frame_list_idx(frame);
+               fr_idx = (MAX_FRLIST_EN_NUM + frame_list_idx(qh->sched_frame) -
+                         fr_idx_tmp)
+                        % frame_incr_val(qh);
+               fr_idx = (fr_idx + fr_idx_tmp) % MAX_FRLIST_EN_NUM;
+       } else {
+               qh->sched_frame = calc_starting_frame(hcd, qh, &skip_frames);
+               fr_idx = frame_list_idx(qh->sched_frame);
+       }
+
+       qh->td_first = qh->td_last = frame_to_desc_idx(qh, fr_idx);
+
+       return skip_frames;
+}
+
+#define ISOC_URB_GIVEBACK_ASAP
+
+#define MAX_ISOC_XFER_SIZE_FS  1023
+#define MAX_ISOC_XFER_SIZE_HS  3072
+#define DESCNUM_THRESHOLD      4
+
+static void fill_host_isoc_dma_desc(struct dwc2_hcd *hcd, struct dwc2_qtd *qtd,
+                                   struct dwc2_qh *qh, u32 max_xfer_size,
+                                   u16 idx)
+{
+       struct dwc2_host_dma_desc *dma_desc = &qh->desc_list[idx];
+       struct dwc2_hcd_iso_packet_desc *frame_desc;
+
+       memset(dma_desc, 0, sizeof(*dma_desc));
+       frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
+
+       if (frame_desc->length > max_xfer_size)
+               qh->n_bytes[idx] = max_xfer_size;
+       else
+               qh->n_bytes[idx] = frame_desc->length;
+
+       dma_desc->buf = qtd->urb->dma + frame_desc->offset;
+       dma_desc->status = qh->n_bytes[idx] << HOST_DMA_ISOC_NBYTES_SHIFT &
+                          HOST_DMA_ISOC_NBYTES_MASK;
+
+#ifdef ISOC_URB_GIVEBACK_ASAP
+       /* Set IOC for each descriptor corresponding to last frame of URB */
+       if (qtd->isoc_frame_index_last == qtd->urb->packet_count)
+               dma_desc->status |= HOST_DMA_IOC;
+#endif
+
+       qh->ntd++;
+       qtd->isoc_frame_index_last++;
+}
+
+static void init_isoc_dma_desc(struct dwc2_hcd *hcd, struct dwc2_qh *qh,
+                              u16 skip_frames)
+{
+       struct list_head *qtd_item;
+       struct dwc2_qtd *qtd;
+       u32 max_xfer_size;
+       u16 idx, inc, n_desc, ntd_max;
+
+       idx = qh->td_last;
+       inc = qh->interval;
+       n_desc = 0;
+
+       ntd_max = (max_desc_num(qh) + qh->interval - 1) / qh->interval;
+       if (skip_frames && !qh->channel)
+               ntd_max = ntd_max - skip_frames / qh->interval;
+
+       max_xfer_size = qh->dev_speed == DWC2_EP_SPEED_HIGH ?
+                       MAX_ISOC_XFER_SIZE_HS : MAX_ISOC_XFER_SIZE_FS;
+
+       list_for_each(qtd_item, &qh->qtd_list) {
+               qtd = list_entry(qtd_item, struct dwc2_qtd, qtd_list_entry);
+               while (qh->ntd < ntd_max && qtd->isoc_frame_index_last <
+                                               qtd->urb->packet_count) {
+                       if (n_desc > 1)
+                               qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
+                       fill_host_isoc_dma_desc(hcd, qtd, qh, max_xfer_size,
+                                               idx);
+                       idx = desclist_idx_inc(idx, inc, qh->dev_speed);
+                       n_desc++;
+               }
+               qtd->in_process = 1;
+       }
+
+       qh->td_last = idx;
+
+#ifdef ISOC_URB_GIVEBACK_ASAP
+       /* Set IOC for last descriptor if descriptor list is full */
+       if (qh->ntd == ntd_max) {
+               idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
+               qh->desc_list[idx].status |= HOST_DMA_IOC;
+       }
+#else
+       /*
+        * Set IOC bit only for one descriptor. Always try to be ahead of HW
+        * processing, i.e. on IOC generation driver activates next descriptor
+        * but core continues to process descriptors following the one with IOC
+        * set.
+        */
+
+       if (n_desc > DESCNUM_THRESHOLD)
+               /*
+                * Move IOC "up". Required even if there is only one QTD
+                * in the list, because QTDs might continue to be queued,
+                * but during the activation it was only one queued.
+                * Actually more than one QTD might be in the list if this
+                * function called from XferCompletion - QTDs was queued during
+                * HW processing of the previous descriptor chunk.
+                */
+               idx = desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2),
+                                      qh->dev_speed);
+       else
+               /*
+                * Set the IOC for the latest descriptor if either number of
+                * descriptors is not greater than threshold or no more new
+                * descriptors activated
+                */
+               idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
+
+       qh->desc_list[idx].status |= HOST_DMA_IOC;
+#endif
+
+       if (n_desc) {
+               qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
+               if (n_desc > 1)
+                       qh->desc_list[0].status |= HOST_DMA_A;
+       }
+}
+
+static void fill_host_dma_desc(struct dwc2_hcd *hcd, struct dwc2_hc *hc,
+                              struct dwc2_qtd *qtd, struct dwc2_qh *qh,
+                              int n_desc)
+{
+       struct dwc2_host_dma_desc *dma_desc = &qh->desc_list[n_desc];
+       int len = hc->xfer_len;
+
+       if (len > MAX_DMA_DESC_SIZE)
+               len = MAX_DMA_DESC_SIZE - hc->max_packet + 1;
+
+       if (hc->ep_is_in) {
+               int num_packets;
+
+               if (len > 0)
+                       num_packets = (len + hc->max_packet - 1)
+                                     / hc->max_packet;
+               else
+                       /* Need 1 packet for transfer length of 0 */
+                       num_packets = 1;
+
+               /* Always program an integral # of packets for IN transfers */
+               len = num_packets * hc->max_packet;
+       }
+
+       dma_desc->status = len << HOST_DMA_NBYTES_SHIFT & HOST_DMA_NBYTES_MASK;
+       qh->n_bytes[n_desc] = len;
+
+       if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL &&
+           qtd->control_phase == DWC2_CONTROL_SETUP)
+               dma_desc->status |= HOST_DMA_SUP;
+
+       dma_desc->buf = ((unsigned long)hc->xfer_buff & 0xffffffff);
+
+       /*
+        * Last (or only) descriptor of IN transfer with actual size less
+        * than MaxPacket
+        */
+       if (len > hc->xfer_len) {
+               hc->xfer_len = 0;
+       } else {
+               hc->xfer_buff += len;
+               hc->xfer_len -= len;
+       }
+}
+
+static void init_non_isoc_dma_desc(struct dwc2_hcd *hcd, struct dwc2_qh *qh)
+{
+       struct list_head *qtd_item;
+       struct dwc2_qtd *qtd;
+       struct dwc2_hc *hc = qh->channel;
+       int n_desc = 0;
+
+       dev_dbg(hcd->dev, "%s()\n", __func__);
+       dev_dbg(hcd->dev, "qh=%p\n", qh);
+       dev_dbg(hcd->dev, "buff=%p len=%d\n", hc->xfer_buff, hc->xfer_len);
+
+       /*
+        * Start with hc->xfer_buff initialized in assign_and_init_hc(), then if
+        * SG transfer consists of multiple URBs, this pointer re-assigned to
+        * the buffer of the currently processed QTD. For non-SG request there
+        * is always one QTD active.
+        */
+
+       list_for_each(qtd_item, &qh->qtd_list) {
+               qtd = list_entry(qtd_item, struct dwc2_qtd, qtd_list_entry);
+               dev_dbg(hcd->dev, "qtd=%p\n", qtd);
+
+               if (n_desc) {
+                       /* SG request - more than 1 QTD */
+                       hc->xfer_buff = (u8 *)qtd->urb->dma +
+                                       qtd->urb->actual_length;
+                       hc->xfer_len = qtd->urb->length -
+                                      qtd->urb->actual_length;
+                       dev_dbg(hcd->dev, "buf=%p len=%d\n", hc->xfer_buff,
+                               hc->xfer_len);
+               }
+
+               qtd->n_desc = 0;
+               do {
+                       if (n_desc > 1) {
+                               qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
+                               dev_dbg(hcd->dev, "set A bit in desc %d (%p)\n",
+                                       n_desc - 1, &qh->desc_list[n_desc - 1]);
+                       }
+                       fill_host_dma_desc(hcd, hc, qtd, qh, n_desc);
+                       dev_dbg(hcd->dev, "desc %d (%p) buf=%08x status=%08x\n",
+                               n_desc, &qh->desc_list[n_desc],
+                               qh->desc_list[n_desc].buf,
+                               qh->desc_list[n_desc].status);
+                       qtd->n_desc++;
+                       n_desc++;
+               } while (hc->xfer_len > 0 &&
+                        n_desc != MAX_DMA_DESC_NUM_GENERIC);
+
+               dev_dbg(hcd->dev, "n_desc=%d\n", n_desc);
+               qtd->in_process = 1;
+               if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL)
+                       break;
+               if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
+                       break;
+       }
+
+       if (n_desc) {
+               qh->desc_list[n_desc - 1].status |=
+                               HOST_DMA_IOC | HOST_DMA_EOL | HOST_DMA_A;
+               dev_dbg(hcd->dev, "set IOC/EOL/A bits in desc %d (%p)\n",
+                       n_desc - 1, &qh->desc_list[n_desc - 1]);
+               if (n_desc > 1) {
+                       qh->desc_list[0].status |= HOST_DMA_A;
+                       dev_dbg(hcd->dev, "set A bit in desc 0 (%p)\n",
+                               &qh->desc_list[0]);
+               }
+               hc->ntd = n_desc;
+       }
+}
+
+/**
+ * dwc2_hcd_start_xfer_ddma() - Starts a transfer in Descriptor DMA mode
+ *
+ * @hcd: The HCD state structure for the DWC OTG controller
+ * @qh:  The QH to init
+ *
+ * Return: 0 if successful, negative error code otherwise
+ *
+ * For Control and Bulk endpoints, initializes descriptor list and starts the
+ * transfer. For Interrupt and Isochronous endpoints, initializes descriptor
+ * list then updates FrameList, marking appropriate entries as active.
+ *
+ * For Isochronous endpoints the starting descriptor index is calculated based
+ * on the scheduled frame, but only on the first transfer descriptor within a
+ * session. Then the transfer is started via enabling the channel.
+ *
+ * For Isochronous endpoints the channel is not halted on XferComplete
+ * interrupt so remains assigned to the endpoint(QH) until session is done.
+ */
+void dwc2_hcd_start_xfer_ddma(struct dwc2_hcd *hcd, struct dwc2_qh *qh)
+{
+       /* Channel is already assigned */
+       struct dwc2_hc *hc = qh->channel;
+       u16 skip_frames = 0;
+
+       switch (hc->ep_type) {
+       case DWC2_EP_TYPE_CONTROL:
+       case DWC2_EP_TYPE_BULK:
+               init_non_isoc_dma_desc(hcd, qh);
+               dwc2_hc_start_transfer_ddma(hcd, hc);
+               break;
+       case DWC2_EP_TYPE_INTR:
+               init_non_isoc_dma_desc(hcd, qh);
+               update_frame_list(hcd, qh, 1);
+               dwc2_hc_start_transfer_ddma(hcd, hc);
+               break;
+       case DWC2_EP_TYPE_ISOC:
+               if (!qh->ntd)
+                       skip_frames = recalc_initial_desc_idx(hcd, qh);
+               init_isoc_dma_desc(hcd, qh, skip_frames);
+
+               if (!hc->xfer_started) {
+                       update_frame_list(hcd, qh, 1);
+
+                       /*
+                        * Always set to max, instead of actual size. Otherwise
+                        * ntd will be changed with channel being enabled. Not
+                        * recommended.
+                        */
+                       hc->ntd = max_desc_num(qh);
+
+                       /* Enable channel only once for ISOC */
+                       dwc2_hc_start_transfer_ddma(hcd, hc);
+               }
+
+               break;
+       default:
+               break;
+       }
+}
+
+#define DWC2_CMPL_DONE         1
+#define DWC2_CMPL_STOP         2
+
+static int cmpl_host_isoc_dma_desc(struct dwc2_hcd *hcd, struct dwc2_hc *hc,
+                                  struct dwc2_qtd *qtd, struct dwc2_qh *qh,
+                                  u16 idx)
+{
+       struct dwc2_host_dma_desc *dma_desc = &qh->desc_list[idx];
+       struct dwc2_hcd_iso_packet_desc *frame_desc;
+       u16 remain = 0;
+       int rc = 0;
+
+       frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
+       dma_desc->buf = qtd->urb->dma + frame_desc->offset;
+       if (hc->ep_is_in)
+               remain = dma_desc->status >> HOST_DMA_ISOC_NBYTES_SHIFT &
+                       HOST_DMA_ISOC_NBYTES_MASK >> HOST_DMA_ISOC_NBYTES_SHIFT;
+
+       if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
+               /*
+                * XactError, or unable to complete all the transactions
+                * in the scheduled micro-frame/frame, both indicated by
+                * HOST_DMA_STS_PKTERR
+                */
+               qtd->urb->error_count++;
+               frame_desc->actual_length = qh->n_bytes[idx] - remain;
+               frame_desc->status = -EPROTO;
+       } else {
+               /* Success */
+               frame_desc->actual_length = qh->n_bytes[idx] - remain;
+               frame_desc->status = 0;
+       }
+
+       if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
+               /*
+                * urb->status is not used for isoc transfers here. The
+                * individual frame_desc status are used instead.
+                */
+               dwc2_host_complete(hcd, qtd->urb->priv, qtd->urb, 0);
+               dwc2_hcd_qtd_remove_and_free(hcd, qtd, qh);
+
+               /*
+                * This check is necessary because urb_dequeue can be called
+                * from urb complete callback (sound driver for example). All
+                * pending URBs are dequeued there, so no need for further
+                * processing.
+                */
+               if (hc->halt_status == DWC2_HC_XFER_URB_DEQUEUE)
+                       return -1;
+               rc = DWC2_CMPL_DONE;
+       }
+
+       qh->ntd--;
+
+       /* Stop if IOC requested descriptor reached */
+       if (dma_desc->status & HOST_DMA_IOC)
+               rc = DWC2_CMPL_STOP;
+
+       return rc;
+}
+
+static void complete_isoc_xfer_ddma(struct dwc2_hcd *hcd, struct dwc2_hc *hc,
+                                   enum dwc2_halt_status halt_status)
+{
+       struct dwc2_hcd_iso_packet_desc *frame_desc;
+       struct list_head *qtd_item, *qtd_tmp;
+       struct dwc2_qtd *qtd;
+       struct dwc2_qh *qh;
+       u16 idx;
+       int rc;
+
+       qh = hc->qh;
+       idx = qh->td_first;
+
+       if (hc->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
+               list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
+                       qtd = list_entry(qtd_item, struct dwc2_qtd,
+                                        qtd_list_entry);
+                       qtd->in_process = 0;
+               }
+               return;
+       } else if (halt_status == DWC2_HC_XFER_AHB_ERR ||
+                  halt_status == DWC2_HC_XFER_BABBLE_ERR) {
+               /*
+                * Channel is halted in these error cases, considered as serious
+                * issues.
+                * Complete all URBs marking all frames as failed, irrespective
+                * whether some of the descriptors (frames) succeeded or not.
+                * Pass error code to completion routine as well, to update
+                * urb->status, some of class drivers might use it to stop
+                * queing transfer requests.
+                */
+               int err = halt_status == DWC2_HC_XFER_AHB_ERR ?
+                         -EIO : -EOVERFLOW;
+
+               list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
+                       qtd = list_entry(qtd_item, struct dwc2_qtd,
+                                        qtd_list_entry);
+                       for (idx = 0; idx < qtd->urb->packet_count; idx++) {
+                               frame_desc = &qtd->urb->iso_descs[idx];
+                               frame_desc->status = err;
+                       }
+                       dwc2_host_complete(hcd, qtd->urb->priv, qtd->urb, err);
+                       dwc2_hcd_qtd_remove_and_free(hcd, qtd, qh);
+               }
+               return;
+       }
+
+       list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
+               qtd = list_entry(qtd_item, struct dwc2_qtd, qtd_list_entry);
+               if (!qtd->in_process)
+                       break;
+               do {
+                       rc = cmpl_host_isoc_dma_desc(hcd, hc, qtd, qh, idx);
+                       if (rc < 0)
+                               return;
+                       idx = desclist_idx_inc(idx, qh->interval, hc->speed);
+                       if (rc == DWC2_CMPL_STOP)
+                               goto stop_scan;
+                       if (rc == DWC2_CMPL_DONE)
+                               break;
+               } while (idx != qh->td_first);
+       }
+
+stop_scan:
+       qh->td_first = idx;
+}
+
+static int update_non_isoc_urb_state_ddma(struct dwc2_hcd *hcd,
+                                         struct dwc2_hc *hc,
+                                         struct dwc2_qtd *qtd,
+                                         struct dwc2_host_dma_desc *dma_desc,
+                                         enum dwc2_halt_status halt_status,
+                                         u32 n_bytes, int *xfer_done)
+{
+       struct dwc2_hcd_urb *urb = qtd->urb;
+       u16 remain = 0;
+
+       if (hc->ep_is_in)
+               remain = dma_desc->status >> HOST_DMA_NBYTES_SHIFT &
+                        HOST_DMA_NBYTES_MASK >> HOST_DMA_NBYTES_SHIFT;
+
+       dev_dbg(hcd->dev, "remain=%d\n", remain);
+       dev_dbg(hcd->dev, "dwc2_urb=%p\n", urb);
+
+       if (halt_status == DWC2_HC_XFER_AHB_ERR) {
+               dev_err(hcd->dev, "EIO\n");
+               urb->status = -EIO;
+               return 1;
+       }
+
+       if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
+               switch (halt_status) {
+               case DWC2_HC_XFER_STALL:
+                       dev_dbg(hcd->dev, "Stall\n");
+                       urb->status = -EPIPE;
+                       break;
+               case DWC2_HC_XFER_BABBLE_ERR:
+                       dev_err(hcd->dev, "Babble\n");
+                       urb->status = -EOVERFLOW;
+                       break;
+               case DWC2_HC_XFER_XACT_ERR:
+                       dev_err(hcd->dev, "XactErr\n");
+                       urb->status = -EPROTO;
+                       break;
+               default:
+                       dev_err(hcd->dev,
+                               "%s: Unhandled descriptor error status (%d)\n",
+                               __func__, halt_status);
+                       break;
+               }
+               return 1;
+       }
+
+       if (dma_desc->status & HOST_DMA_A) {
+               dev_dbg(hcd->dev,
+                       "Active descriptor encountered on channel %d\n",
+                       hc->hc_num);
+               return 0;
+       }
+
+       if (hc->ep_type == DWC2_EP_TYPE_CONTROL) {
+               if (qtd->control_phase == DWC2_CONTROL_DATA) {
+                       urb->actual_length += n_bytes - remain;
+                       if (remain || urb->actual_length == urb->length) {
+                               /*
+                                * For Control Data stage do not set urb->status
+                                * to 0, to prevent URB callback. Set it when
+                                * Status phase is done. See below.
+                                */
+                               *xfer_done = 1;
+                       }
+               } else if (qtd->control_phase == DWC2_CONTROL_STATUS) {
+                       urb->status = 0;
+                       *xfer_done = 1;
+               }
+               /* No handling for SETUP stage */
+       } else {
+               /* BULK and INTR */
+               urb->actual_length += n_bytes - remain;
+               dev_dbg(hcd->dev, "length=%d\n", urb->length);
+               dev_dbg(hcd->dev, "actual=%d\n", urb->actual_length);
+               if (remain || urb->actual_length == urb->length) {
+                       urb->status = 0;
+                       *xfer_done = 1;
+               }
+       }
+
+       return 0;
+}
+
+static int process_non_isoc_desc(struct dwc2_hcd *hcd, struct dwc2_hc *hc,
+                                int chnum, struct dwc2_qtd *qtd, int desc_num,
+                                enum dwc2_halt_status halt_status,
+                                int *xfer_done)
+{
+       struct dwc2_qh *qh = hc->qh;
+       struct dwc2_hcd_urb *urb = qtd->urb;
+       struct dwc2_host_dma_desc *dma_desc;
+       u32 n_bytes;
+       int failed;
+
+       dev_dbg(hcd->dev, "%s()\n", __func__);
+
+       dma_desc = &qh->desc_list[desc_num];
+       n_bytes = qh->n_bytes[desc_num];
+       dev_dbg(hcd->dev,
+               "qtd=%p dwc2_urb=%p desc_num=%d desc=%p n_bytes=%d\n",
+               qtd, urb, desc_num, dma_desc, n_bytes);
+       failed = update_non_isoc_urb_state_ddma(hcd, hc, qtd, dma_desc,
+                                               halt_status, n_bytes,
+                                               xfer_done);
+       if (failed || (*xfer_done && urb->status != -EINPROGRESS)) {
+               dwc2_host_complete(hcd, urb->priv, urb, urb->status);
+               dwc2_hcd_qtd_remove_and_free(hcd, qtd, qh);
+               dev_dbg(hcd->dev, "failed=%1x xfer_done=%1x status=%08x\n",
+                       failed, *xfer_done, urb->status);
+               return failed;
+
+       } else if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL) {
+               if (qtd->control_phase == DWC2_CONTROL_SETUP) {
+                       if (urb->length > 0)
+                               qtd->control_phase = DWC2_CONTROL_DATA;
+                       else
+                               qtd->control_phase = DWC2_CONTROL_STATUS;
+                       dev_dbg(hcd->dev, "  Control setup transaction done\n");
+               } else if (qtd->control_phase == DWC2_CONTROL_DATA) {
+                       if (*xfer_done) {
+                               qtd->control_phase = DWC2_CONTROL_STATUS;
+                               dev_dbg(hcd->dev,
+                                       "  Control data transfer done\n");
+                       } else if (desc_num + 1 == qtd->n_desc) {
+                               /*
+                                * Last descriptor for Control data stage which
+                                * is not completed yet
+                                */
+                               dwc2_hcd_save_data_toggle(hcd, hc, chnum, qtd);
+                       }
+               }
+       }
+
+       return 0;
+}
+
+static void complete_non_isoc_xfer_ddma(struct dwc2_hcd *hcd,
+                                       struct dwc2_hc *hc, int chnum,
+                                       enum dwc2_halt_status halt_status)
+{
+       struct dwc2_qh *qh = hc->qh;
+       struct list_head *qtd_item, *qtd_tmp;
+       struct dwc2_qtd *qtd;
+       int xfer_done;
+       int desc_num = 0;
+
+       if (hc->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
+               list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
+                       qtd = list_entry(qtd_item, struct dwc2_qtd,
+                                        qtd_list_entry);
+                       qtd->in_process = 0;
+               }
+               return;
+       }
+
+       list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
+               int i;
+
+               qtd = list_entry(qtd_item, struct dwc2_qtd, qtd_list_entry);
+               xfer_done = 0;
+
+               for (i = 0; i < qtd->n_desc; i++) {
+                       if (process_non_isoc_desc(hcd, hc, chnum, qtd, desc_num,
+                                                 halt_status, &xfer_done))
+                               break;
+                       desc_num++;
+               }
+       }
+
+       if (qh->ep_type != USB_ENDPOINT_XFER_CONTROL) {
+               /*
+                * Resetting the data toggle for bulk and interrupt endpoints
+                * in case of stall. See handle_hc_stall_intr().
+                */
+               if (halt_status == DWC2_HC_XFER_STALL)
+                       qh->data_toggle = DWC2_HC_PID_DATA0;
+               else
+                       dwc2_hcd_save_data_toggle(hcd, hc, chnum, qtd);
+       }
+
+       if (halt_status == DWC2_HC_XFER_COMPLETE) {
+               u32 hcint = readl(hcd->regs + HCINT(chnum));
+
+               if (hcint & HCINTMSK_NYET) {
+                       /*
+                        * Got a NYET on the last transaction of the transfer.
+                        * It means that the endpoint should be in the PING
+                        * state at the beginning of the next transfer.
+                        */
+                       qh->ping_state = 1;
+                       clear_hc_int(hcd, chnum, HCINTMSK_NYET);
+               }
+       }
+}
+
+/**
+ * dwc2_hcd_complete_xfer_ddma() - Scans the descriptor list, updates URB's
+ * status and calls completion routine for the URB if it's done. Called from
+ * interrupt handlers.
+ *
+ * @hcd:         The HCD state structure for the DWC OTG controller
+ * @hc:          Host channel the transfer is completed on
+ * @chnum:       Host channel registers
+ * @halt_status: Reason the channel is being halted or just XferComplete
+ *               for isochronous transfers
+ *
+ * Releases the channel to be used by other transfers.
+ * In case of Isochronous endpoint the channel is not halted until the end of
+ * the session, i.e. QTD list is empty.
+ * If periodic channel released the FrameList is updated accordingly.
+ * Calls transaction selection routines to activate pending transfers.
+ */
+void dwc2_hcd_complete_xfer_ddma(struct dwc2_hcd *hcd, struct dwc2_hc *hc,
+                                int chnum, enum dwc2_halt_status halt_status)
+{
+       struct dwc2_qh *qh = hc->qh;
+       int continue_isoc_xfer = 0;
+       enum dwc2_transaction_type tr_type;
+
+       if (hc->ep_type == DWC2_EP_TYPE_ISOC) {
+               complete_isoc_xfer_ddma(hcd, hc, halt_status);
+
+               /* Release the channel if halted or session completed */
+               if (halt_status != DWC2_HC_XFER_COMPLETE ||
+                   list_empty(&qh->qtd_list)) {
+                       /* Halt the channel if session completed */
+                       if (halt_status == DWC2_HC_XFER_COMPLETE)
+                               dwc2_hc_halt(hcd, hc, halt_status);
+                       release_channel_ddma(hcd, qh);
+                       dwc2_hcd_qh_remove(hcd, qh);
+               } else {
+                       /* Keep in assigned schedule to continue transfer */
+                       list_move(&qh->qh_list_entry,
+                                 &hcd->periodic_sched_assigned);
+                       continue_isoc_xfer = 1;
+               }
+               /*
+                * Todo: Consider the case when period exceeds FrameList size.
+                * Frame Rollover interrupt should be used.
+                */
+       } else {
+               /*
+                * Scan descriptor list to complete the URB(s), then release
+                * the channel
+                */
+               complete_non_isoc_xfer_ddma(hcd, hc, chnum, halt_status);
+               release_channel_ddma(hcd, qh);
+               dwc2_hcd_qh_remove(hcd, qh);
+
+               if (!list_empty(&qh->qtd_list)) {
+                       /*
+                        * Add back to inactive non-periodic schedule on normal
+                        * completion
+                        */
+                       dwc2_hcd_qh_add(hcd, qh);
+               }
+       }
+       tr_type = dwc2_hcd_select_transactions(hcd);
+       if (tr_type != DWC2_TRANSACTION_NONE || continue_isoc_xfer) {
+               if (continue_isoc_xfer) {
+                       if (tr_type == DWC2_TRANSACTION_NONE)
+                               tr_type = DWC2_TRANSACTION_PERIODIC;
+                       else if (tr_type == DWC2_TRANSACTION_NON_PERIODIC)
+                               tr_type = DWC2_TRANSACTION_ALL;
+               }
+               dwc2_hcd_queue_transactions(hcd, tr_type);
+       }
+}
-- 
1.7.1

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

Reply via email to