Instead of having a limited number of usable tds in the udc we use a linked list to support dynamic amount of needed tds for all special gadget types. This improves throughput. This patch also adresses a possible momory leak in _ep_nuke found while porting the request handling to an linked list. - The call of _ep_nuke can lead to an memory leak, if it is called on an endpoint with currently dynamic allocated tds. This was aswell a problem in the special case before the dynamic td handling was implemented and an zero length packet was added to mark the end of the transfer. Signed-off-by: Michael Grzeschik <m.grzeschik@xxxxxxxxxxxxxx> --- drivers/usb/chipidea/debug.c | 8 ++- drivers/usb/chipidea/udc.c | 159 +++++++++++++++++++++++++++++------------- drivers/usb/chipidea/udc.h | 12 ++-- 3 files changed, 124 insertions(+), 55 deletions(-) diff --git a/drivers/usb/chipidea/debug.c b/drivers/usb/chipidea/debug.c index 3bc244d..c7514f9 100644 --- a/drivers/usb/chipidea/debug.c +++ b/drivers/usb/chipidea/debug.c @@ -694,6 +694,7 @@ static ssize_t show_requests(struct device *dev, struct device_attribute *attr, unsigned long flags; struct list_head *ptr = NULL; struct ci13xxx_req *req = NULL; + struct td_node *node_first; unsigned i, j, n = 0, qSize = sizeof(struct ci13xxx_td)/sizeof(u32); if (attr == NULL || buf == NULL) { @@ -707,15 +708,18 @@ static ssize_t show_requests(struct device *dev, struct device_attribute *attr, { req = list_entry(ptr, struct ci13xxx_req, queue); + node_first = list_first_entry(req->tds.prev, + struct td_node, td); + n += scnprintf(buf + n, PAGE_SIZE - n, "EP=%02i: TD=%08X %s\n", - i % ci->hw_ep_max/2, (u32)req->dma, + i % ci->hw_ep_max/2, (u32)node_first->dma, ((i < ci->hw_ep_max/2) ? "RX" : "TX")); for (j = 0; j < qSize; j++) n += scnprintf(buf + n, PAGE_SIZE - n, " %04X: %08X\n", j, - *((u32 *)req->ptr + j)); + *((u32 *)node_first->ptr + j)); } spin_unlock_irqrestore(&ci->lock, flags); diff --git a/drivers/usb/chipidea/udc.c b/drivers/usb/chipidea/udc.c index 0b80228..98433cd 100644 --- a/drivers/usb/chipidea/udc.c +++ b/drivers/usb/chipidea/udc.c @@ -405,6 +405,41 @@ static inline u8 _usb_addr(struct ci13xxx_ep *ep) return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num; } +static void setup_td_bits(struct td_node *tdnode, unsigned length) +{ + memset(tdnode->ptr, 0, sizeof(*tdnode->ptr)); + tdnode->ptr->token = length << ffs_nr(TD_TOTAL_BYTES); + tdnode->ptr->token &= TD_TOTAL_BYTES; + tdnode->ptr->token |= TD_STATUS_ACTIVE; +} + +static int add_td_to_list(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq, unsigned length) +{ + struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node), GFP_ATOMIC); + + if (node == NULL) + return -ENOMEM; + + node->ptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC, + &node->dma); + if (node->ptr == NULL) { + kfree(node); + return -ENOMEM; + } + + setup_td_bits(node, length); + + /* get the last entry */ + lastnode = list_entry(mReq->tds.prev, + struct td_node, td); + lastnode->ptr->next = node->dma; + + INIT_LIST_HEAD(&node->td); + list_add_tail(&node->td, &mReq->tds); + + return 0; +} + /** * _hardware_queue: configures a request at hardware level * @gadget: gadget @@ -418,6 +453,7 @@ static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq) unsigned i; int ret = 0; unsigned length = mReq->req.length; + struct td_node *reqnode, *lastnode; /* don't queue twice */ if (mReq->req.status == -EALREADY) @@ -425,53 +461,42 @@ static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq) mReq->req.status = -EALREADY; - if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) { - mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC, - &mReq->zdma); - if (mReq->zptr == NULL) - return -ENOMEM; - - memset(mReq->zptr, 0, sizeof(*mReq->zptr)); - mReq->zptr->next = TD_TERMINATE; - mReq->zptr->token = TD_STATUS_ACTIVE; - if (!mReq->req.no_interrupt) - mReq->zptr->token |= TD_IOC; - } ret = usb_gadget_map_request(&ci->gadget, &mReq->req, mEp->dir); if (ret) return ret; - /* - * TD configuration - * TODO - handle requests which spawns into several TDs - */ - memset(mReq->ptr, 0, sizeof(*mReq->ptr)); - mReq->ptr->token = length << ffs_nr(TD_TOTAL_BYTES); - mReq->ptr->token &= TD_TOTAL_BYTES; - mReq->ptr->token |= TD_STATUS_ACTIVE; - if (mReq->zptr) { - mReq->ptr->next = mReq->zdma; - } else { - mReq->ptr->next = TD_TERMINATE; - if (!mReq->req.no_interrupt) - mReq->ptr->token |= TD_IOC; - } - mReq->ptr->page[0] = mReq->req.dma; + reqnode = list_first_entry(&mReq->tds, + struct td_node, td); + + setup_td_bits(reqnode, length); + + reqnode->ptr->page[0] = mReq->req.dma; for (i = 1; i <= TD_COUNT; i++) - mReq->ptr->page[i] = + reqnode->ptr->page[i] = (mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK; + if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) + add_td_to_list(mEp, mReq, 0); + + lastnode = list_entry(mReq->tds.prev, + struct td_node, td); + + lastnode->ptr->next = TD_TERMINATE; + if (!mReq->req.no_interrupt) + lastnode->ptr->token |= TD_IOC; + if (!list_empty(&mEp->qh.queue)) { struct ci13xxx_req *mReqPrev; int n = hw_ep_bit(mEp->num, mEp->dir); int tmp_stat; + struct td_node *last; mReqPrev = list_entry(mEp->qh.queue.prev, struct ci13xxx_req, queue); - if (mReqPrev->zptr) - mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK; - else - mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK; + last = list_entry(mReqPrev->tds.prev, + struct td_node, td); + + last->ptr->next = reqnode->dma & TD_ADDR_MASK; wmb(); if (hw_read(ci, OP_ENDPTPRIME, BIT(n))) goto done; @@ -485,7 +510,7 @@ static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq) } /* QH configuration */ - mEp->qh.ptr->td.next = mReq->dma; /* TERMINATE = 0 */ + mEp->qh.ptr->td.next = reqnode->dma; /* TERMINATE = 0 */ mEp->qh.ptr->td.token &= ~TD_STATUS; /* clear status */ mEp->qh.ptr->cap |= QH_ZLT; @@ -506,24 +531,30 @@ done: */ static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq) { + struct td_node *node, *tmp_node, *node_first; + if (mReq->req.status != -EALREADY) return -EINVAL; - if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0) - return -EBUSY; + node_first = list_first_entry(&mReq->tds, + struct td_node, td); - if (mReq->zptr) { - if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0) + list_for_each_entry_safe(node, tmp_node, &mReq->tds, td) { + if ((TD_STATUS_ACTIVE & node->ptr->token) != 0) return -EBUSY; - dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma); - mReq->zptr = NULL; + if (node != node_first) { + dma_pool_free(mEp->td_pool, node->ptr, node->dma); + list_del_init(&node->td); + node->ptr = NULL; + kfree(node); + } } mReq->req.status = 0; usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir); - mReq->req.status = mReq->ptr->token & TD_STATUS; + mReq->req.status = node_first->ptr->token & TD_STATUS; if ((TD_STATUS_HALTED & mReq->req.status) != 0) mReq->req.status = -1; else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0) @@ -531,7 +562,7 @@ static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq) else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0) mReq->req.status = -1; - mReq->req.actual = mReq->ptr->token & TD_TOTAL_BYTES; + mReq->req.actual = node_first->ptr->token & TD_TOTAL_BYTES; mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES); mReq->req.actual = mReq->req.length - mReq->req.actual; mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual; @@ -550,6 +581,7 @@ static int _ep_nuke(struct ci13xxx_ep *mEp) __releases(mEp->lock) __acquires(mEp->lock) { + struct td_node *node, *tmp_node, *node_first; if (mEp == NULL) return -EINVAL; @@ -561,6 +593,19 @@ __acquires(mEp->lock) struct ci13xxx_req *mReq = \ list_entry(mEp->qh.queue.next, struct ci13xxx_req, queue); + + node_first = list_first_entry(&mReq->tds, + struct td_node, td); + + list_for_each_entry_safe(node, tmp_node, &mReq->tds, td) { + if (node != node_first) { + dma_pool_free(mEp->td_pool, node->ptr, node->dma); + list_del_init(&node->td); + node->ptr = NULL; + kfree(node); + } + } + list_del_init(&mReq->queue); mReq->req.status = -ESHUTDOWN; @@ -848,14 +893,19 @@ __acquires(mEp->lock) struct ci13xxx_req *mReq, *mReqTemp; struct ci13xxx_ep *mEpTemp = mEp; int retval = 0; + struct td_node *node_first; list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue, queue) { + + node_first = list_first_entry(&mReq->tds, + struct td_node, td); + retval = _hardware_dequeue(mEp, mReq); if (retval < 0) break; list_del_init(&mReq->queue); - dbg_done(_usb_addr(mEp), mReq->ptr->token, retval); + dbg_done(_usb_addr(mEp), node_first->ptr->token, retval); if (mReq->req.complete != NULL) { spin_unlock(mEp->lock); if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) && @@ -1175,19 +1225,26 @@ static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags) { struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); struct ci13xxx_req *mReq = NULL; + struct td_node *node; if (ep == NULL) return NULL; mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags); - if (mReq != NULL) { + node = kzalloc(sizeof(struct td_node), gfp_flags); + if (mReq != NULL && node != NULL) { INIT_LIST_HEAD(&mReq->queue); + INIT_LIST_HEAD(&mReq->tds); + INIT_LIST_HEAD(&node->td); - mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags, - &mReq->dma); - if (mReq->ptr == NULL) { + node->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags, + &node->dma); + if (node->ptr == NULL) { + kfree(node); kfree(mReq); mReq = NULL; + } else { + list_add_tail(&node->td, &mReq->tds); } } @@ -1205,6 +1262,7 @@ static void ep_free_request(struct usb_ep *ep, struct usb_request *req) { struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req); + struct td_node *node_first; unsigned long flags; if (ep == NULL || req == NULL) { @@ -1216,8 +1274,11 @@ static void ep_free_request(struct usb_ep *ep, struct usb_request *req) spin_lock_irqsave(mEp->lock, flags); - if (mReq->ptr) - dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma); + node_first = list_first_entry(&mReq->tds, + struct td_node, td); + + if (node_first->ptr) + dma_pool_free(mEp->td_pool, node_first->ptr, node_first->dma); kfree(mReq); dbg_event(_usb_addr(mEp), "FREE", 0); diff --git a/drivers/usb/chipidea/udc.h b/drivers/usb/chipidea/udc.h index 4ff2384d..1b4f218 100644 --- a/drivers/usb/chipidea/udc.h +++ b/drivers/usb/chipidea/udc.h @@ -59,6 +59,13 @@ struct ci13xxx_qh { struct usb_ctrlrequest setup; } __attribute__ ((packed)); + +struct td_node { + struct list_head td; + dma_addr_t dma; + struct ci13xxx_td *ptr; +}; + /** * struct ci13xxx_req - usb request representation * @req: request structure for gadget drivers @@ -71,10 +78,7 @@ struct ci13xxx_qh { struct ci13xxx_req { struct usb_request req; struct list_head queue; - struct ci13xxx_td *ptr; - dma_addr_t dma; - struct ci13xxx_td *zptr; - dma_addr_t zdma; + struct list_head tds; }; #ifdef CONFIG_USB_CHIPIDEA_UDC -- 1.7.10.4 -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html