blob: 85d7816d29f1d71c5cfdceb18f3401e4690a1af5 [file] [log] [blame]
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001/*
2 * hcd_ddma.c - DesignWare HS OTG Controller descriptor DMA routines
3 *
4 * Copyright (C) 2004-2013 Synopsys, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the above-listed copyright holders may not be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * ALTERNATIVELY, this software may be distributed under the terms of the
20 * GNU General Public License ("GPL") as published by the Free Software
21 * Foundation; either version 2 of the License, or (at your option) any
22 * later version.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37/*
38 * This file contains the Descriptor DMA implementation for Host mode
39 */
40#include <linux/kernel.h>
41#include <linux/module.h>
42#include <linux/spinlock.h>
43#include <linux/interrupt.h>
44#include <linux/dma-mapping.h>
45#include <linux/io.h>
46#include <linux/slab.h>
47#include <linux/usb.h>
48
49#include <linux/usb/hcd.h>
50#include <linux/usb/ch11.h>
51
52#include "core.h"
53#include "hcd.h"
54
55static u16 dwc2_frame_list_idx(u16 frame)
56{
57 return frame & (FRLISTEN_64_SIZE - 1);
58}
59
60static u16 dwc2_desclist_idx_inc(u16 idx, u16 inc, u8 speed)
61{
62 return (idx + inc) &
63 ((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
64 MAX_DMA_DESC_NUM_GENERIC) - 1);
65}
66
67static u16 dwc2_desclist_idx_dec(u16 idx, u16 inc, u8 speed)
68{
69 return (idx - inc) &
70 ((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
71 MAX_DMA_DESC_NUM_GENERIC) - 1);
72}
73
74static u16 dwc2_max_desc_num(struct dwc2_qh *qh)
75{
76 return (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
77 qh->dev_speed == USB_SPEED_HIGH) ?
78 MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC;
79}
80
81static u16 dwc2_frame_incr_val(struct dwc2_qh *qh)
82{
83 return qh->dev_speed == USB_SPEED_HIGH ?
84 (qh->interval + 8 - 1) / 8 : qh->interval;
85}
86
87static int dwc2_desc_list_alloc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
88 gfp_t flags)
89{
Gregory Herrero95105a92015-11-20 11:49:29 +010090 qh->desc_list_sz = sizeof(struct dwc2_hcd_dma_desc) *
91 dwc2_max_desc_num(qh);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -070092
Gregory Herrero95105a92015-11-20 11:49:29 +010093 qh->desc_list = kzalloc(qh->desc_list_sz, flags | GFP_DMA);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -070094 if (!qh->desc_list)
95 return -ENOMEM;
96
Gregory Herrero95105a92015-11-20 11:49:29 +010097 qh->desc_list_dma = dma_map_single(hsotg->dev, qh->desc_list,
98 qh->desc_list_sz,
99 DMA_TO_DEVICE);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700100
101 qh->n_bytes = kzalloc(sizeof(u32) * dwc2_max_desc_num(qh), flags);
102 if (!qh->n_bytes) {
Gregory Herrero95105a92015-11-20 11:49:29 +0100103 dma_unmap_single(hsotg->dev, qh->desc_list_dma,
104 qh->desc_list_sz,
105 DMA_FROM_DEVICE);
106 kfree(qh->desc_list);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700107 qh->desc_list = NULL;
108 return -ENOMEM;
109 }
110
111 return 0;
112}
113
114static void dwc2_desc_list_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
115{
116 if (qh->desc_list) {
Gregory Herrero95105a92015-11-20 11:49:29 +0100117 dma_unmap_single(hsotg->dev, qh->desc_list_dma,
118 qh->desc_list_sz, DMA_FROM_DEVICE);
119 kfree(qh->desc_list);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700120 qh->desc_list = NULL;
121 }
122
123 kfree(qh->n_bytes);
124 qh->n_bytes = NULL;
125}
126
127static int dwc2_frame_list_alloc(struct dwc2_hsotg *hsotg, gfp_t mem_flags)
128{
129 if (hsotg->frame_list)
130 return 0;
131
Gregory Herrero95105a92015-11-20 11:49:29 +0100132 hsotg->frame_list_sz = 4 * FRLISTEN_64_SIZE;
133 hsotg->frame_list = kzalloc(hsotg->frame_list_sz, GFP_ATOMIC | GFP_DMA);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700134 if (!hsotg->frame_list)
135 return -ENOMEM;
136
Gregory Herrero95105a92015-11-20 11:49:29 +0100137 hsotg->frame_list_dma = dma_map_single(hsotg->dev, hsotg->frame_list,
138 hsotg->frame_list_sz,
139 DMA_TO_DEVICE);
140
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700141 return 0;
142}
143
144static void dwc2_frame_list_free(struct dwc2_hsotg *hsotg)
145{
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700146 unsigned long flags;
147
148 spin_lock_irqsave(&hsotg->lock, flags);
149
150 if (!hsotg->frame_list) {
151 spin_unlock_irqrestore(&hsotg->lock, flags);
152 return;
153 }
154
Gregory Herrero95105a92015-11-20 11:49:29 +0100155 dma_unmap_single(hsotg->dev, hsotg->frame_list_dma,
156 hsotg->frame_list_sz, DMA_FROM_DEVICE);
157
158 kfree(hsotg->frame_list);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700159 hsotg->frame_list = NULL;
160
161 spin_unlock_irqrestore(&hsotg->lock, flags);
162
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700163}
164
165static void dwc2_per_sched_enable(struct dwc2_hsotg *hsotg, u32 fr_list_en)
166{
167 u32 hcfg;
168 unsigned long flags;
169
170 spin_lock_irqsave(&hsotg->lock, flags);
171
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300172 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700173 if (hcfg & HCFG_PERSCHEDENA) {
174 /* already enabled */
175 spin_unlock_irqrestore(&hsotg->lock, flags);
176 return;
177 }
178
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300179 dwc2_writel(hsotg->frame_list_dma, hsotg->regs + HFLBADDR);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700180
181 hcfg &= ~HCFG_FRLISTEN_MASK;
182 hcfg |= fr_list_en | HCFG_PERSCHEDENA;
183 dev_vdbg(hsotg->dev, "Enabling Periodic schedule\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300184 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700185
186 spin_unlock_irqrestore(&hsotg->lock, flags);
187}
188
189static void dwc2_per_sched_disable(struct dwc2_hsotg *hsotg)
190{
191 u32 hcfg;
192 unsigned long flags;
193
194 spin_lock_irqsave(&hsotg->lock, flags);
195
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300196 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700197 if (!(hcfg & HCFG_PERSCHEDENA)) {
198 /* already disabled */
199 spin_unlock_irqrestore(&hsotg->lock, flags);
200 return;
201 }
202
203 hcfg &= ~HCFG_PERSCHEDENA;
204 dev_vdbg(hsotg->dev, "Disabling Periodic schedule\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300205 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700206
207 spin_unlock_irqrestore(&hsotg->lock, flags);
208}
209
210/*
211 * Activates/Deactivates FrameList entries for the channel based on endpoint
212 * servicing period
213 */
214static void dwc2_update_frame_list(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
215 int enable)
216{
217 struct dwc2_host_chan *chan;
218 u16 i, j, inc;
219
Paul Zimmermanddf58462013-04-22 14:00:16 -0700220 if (!hsotg) {
Paul Zimmermande9169a2013-04-22 14:00:17 -0700221 pr_err("hsotg = %p\n", hsotg);
Paul Zimmermanddf58462013-04-22 14:00:16 -0700222 return;
223 }
224
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700225 if (!qh->channel) {
Paul Zimmermande9169a2013-04-22 14:00:17 -0700226 dev_err(hsotg->dev, "qh->channel = %p\n", qh->channel);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700227 return;
228 }
229
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700230 if (!hsotg->frame_list) {
Paul Zimmermande9169a2013-04-22 14:00:17 -0700231 dev_err(hsotg->dev, "hsotg->frame_list = %p\n",
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700232 hsotg->frame_list);
233 return;
234 }
235
236 chan = qh->channel;
237 inc = dwc2_frame_incr_val(qh);
238 if (qh->ep_type == USB_ENDPOINT_XFER_ISOC)
239 i = dwc2_frame_list_idx(qh->sched_frame);
240 else
241 i = 0;
242
243 j = i;
244 do {
245 if (enable)
246 hsotg->frame_list[j] |= 1 << chan->hc_num;
247 else
248 hsotg->frame_list[j] &= ~(1 << chan->hc_num);
249 j = (j + inc) & (FRLISTEN_64_SIZE - 1);
250 } while (j != i);
251
Gregory Herrero95105a92015-11-20 11:49:29 +0100252 /*
253 * Sync frame list since controller will access it if periodic
254 * channel is currently enabled.
255 */
256 dma_sync_single_for_device(hsotg->dev,
257 hsotg->frame_list_dma,
258 hsotg->frame_list_sz,
259 DMA_TO_DEVICE);
260
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700261 if (!enable)
262 return;
263
264 chan->schinfo = 0;
265 if (chan->speed == USB_SPEED_HIGH && qh->interval) {
266 j = 1;
267 /* TODO - check this */
268 inc = (8 + qh->interval - 1) / qh->interval;
269 for (i = 0; i < inc; i++) {
270 chan->schinfo |= j;
271 j = j << qh->interval;
272 }
273 } else {
274 chan->schinfo = 0xff;
275 }
276}
277
278static void dwc2_release_channel_ddma(struct dwc2_hsotg *hsotg,
279 struct dwc2_qh *qh)
280{
281 struct dwc2_host_chan *chan = qh->channel;
282
Dom Cobley20f2eb92013-09-23 14:23:34 -0700283 if (dwc2_qh_is_non_per(qh)) {
284 if (hsotg->core_params->uframe_sched > 0)
285 hsotg->available_host_channels++;
286 else
287 hsotg->non_periodic_channels--;
288 } else {
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700289 dwc2_update_frame_list(hsotg, qh, 0);
Gregory Herrero3f808bd2015-11-05 09:41:44 +0100290 hsotg->available_host_channels++;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700291 }
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700292
293 /*
294 * The condition is added to prevent double cleanup try in case of
295 * device disconnect. See channel cleanup in dwc2_hcd_disconnect().
296 */
297 if (chan->qh) {
298 if (!list_empty(&chan->hc_list_entry))
299 list_del(&chan->hc_list_entry);
300 dwc2_hc_cleanup(hsotg, chan);
301 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
302 chan->qh = NULL;
303 }
304
305 qh->channel = NULL;
306 qh->ntd = 0;
307
308 if (qh->desc_list)
309 memset(qh->desc_list, 0, sizeof(struct dwc2_hcd_dma_desc) *
310 dwc2_max_desc_num(qh));
311}
312
313/**
314 * dwc2_hcd_qh_init_ddma() - Initializes a QH structure's Descriptor DMA
315 * related members
316 *
317 * @hsotg: The HCD state structure for the DWC OTG controller
318 * @qh: The QH to init
319 *
320 * Return: 0 if successful, negative error code otherwise
321 *
322 * Allocates memory for the descriptor list. For the first periodic QH,
323 * allocates memory for the FrameList and enables periodic scheduling.
324 */
325int dwc2_hcd_qh_init_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
326 gfp_t mem_flags)
327{
328 int retval;
329
330 if (qh->do_split) {
331 dev_err(hsotg->dev,
332 "SPLIT Transfers are not supported in Descriptor DMA mode.\n");
333 retval = -EINVAL;
334 goto err0;
335 }
336
337 retval = dwc2_desc_list_alloc(hsotg, qh, mem_flags);
338 if (retval)
339 goto err0;
340
341 if (qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
342 qh->ep_type == USB_ENDPOINT_XFER_INT) {
343 if (!hsotg->frame_list) {
344 retval = dwc2_frame_list_alloc(hsotg, mem_flags);
345 if (retval)
346 goto err1;
347 /* Enable periodic schedule on first periodic QH */
348 dwc2_per_sched_enable(hsotg, HCFG_FRLISTEN_64);
349 }
350 }
351
352 qh->ntd = 0;
353 return 0;
354
355err1:
356 dwc2_desc_list_free(hsotg, qh);
357err0:
358 return retval;
359}
360
361/**
362 * dwc2_hcd_qh_free_ddma() - Frees a QH structure's Descriptor DMA related
363 * members
364 *
365 * @hsotg: The HCD state structure for the DWC OTG controller
366 * @qh: The QH to free
367 *
368 * Frees descriptor list memory associated with the QH. If QH is periodic and
369 * the last, frees FrameList memory and disables periodic scheduling.
370 */
371void dwc2_hcd_qh_free_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
372{
Gregory Herrero2b046bc2015-11-05 09:41:41 +0100373 unsigned long flags;
374
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700375 dwc2_desc_list_free(hsotg, qh);
376
377 /*
378 * Channel still assigned due to some reasons.
379 * Seen on Isoc URB dequeue. Channel halted but no subsequent
380 * ChHalted interrupt to release the channel. Afterwards
381 * when it comes here from endpoint disable routine
382 * channel remains assigned.
383 */
Gregory Herrero2b046bc2015-11-05 09:41:41 +0100384 spin_lock_irqsave(&hsotg->lock, flags);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700385 if (qh->channel)
386 dwc2_release_channel_ddma(hsotg, qh);
Gregory Herrero2b046bc2015-11-05 09:41:41 +0100387 spin_unlock_irqrestore(&hsotg->lock, flags);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700388
389 if ((qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
390 qh->ep_type == USB_ENDPOINT_XFER_INT) &&
Dom Cobley20f2eb92013-09-23 14:23:34 -0700391 (hsotg->core_params->uframe_sched > 0 ||
392 !hsotg->periodic_channels) && hsotg->frame_list) {
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700393 dwc2_per_sched_disable(hsotg);
394 dwc2_frame_list_free(hsotg);
395 }
396}
397
398static u8 dwc2_frame_to_desc_idx(struct dwc2_qh *qh, u16 frame_idx)
399{
400 if (qh->dev_speed == USB_SPEED_HIGH)
401 /* Descriptor set (8 descriptors) index which is 8-aligned */
402 return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
403 else
404 return frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1);
405}
406
407/*
408 * Determine starting frame for Isochronous transfer.
409 * Few frames skipped to prevent race condition with HC.
410 */
411static u16 dwc2_calc_starting_frame(struct dwc2_hsotg *hsotg,
412 struct dwc2_qh *qh, u16 *skip_frames)
413{
414 u16 frame;
415
416 hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
417
418 /* sched_frame is always frame number (not uFrame) both in FS and HS! */
419
420 /*
421 * skip_frames is used to limit activated descriptors number
422 * to avoid the situation when HC services the last activated
423 * descriptor firstly.
424 * Example for FS:
425 * Current frame is 1, scheduled frame is 3. Since HC always fetches
426 * the descriptor corresponding to curr_frame+1, the descriptor
427 * corresponding to frame 2 will be fetched. If the number of
428 * descriptors is max=64 (or greather) the list will be fully programmed
429 * with Active descriptors and it is possible case (rare) that the
430 * latest descriptor(considering rollback) corresponding to frame 2 will
431 * be serviced first. HS case is more probable because, in fact, up to
432 * 11 uframes (16 in the code) may be skipped.
433 */
434 if (qh->dev_speed == USB_SPEED_HIGH) {
435 /*
436 * Consider uframe counter also, to start xfer asap. If half of
437 * the frame elapsed skip 2 frames otherwise just 1 frame.
438 * Starting descriptor index must be 8-aligned, so if the
439 * current frame is near to complete the next one is skipped as
440 * well.
441 */
442 if (dwc2_micro_frame_num(hsotg->frame_number) >= 5) {
443 *skip_frames = 2 * 8;
444 frame = dwc2_frame_num_inc(hsotg->frame_number,
445 *skip_frames);
446 } else {
447 *skip_frames = 1 * 8;
448 frame = dwc2_frame_num_inc(hsotg->frame_number,
449 *skip_frames);
450 }
451
452 frame = dwc2_full_frame_num(frame);
453 } else {
454 /*
455 * Two frames are skipped for FS - the current and the next.
456 * But for descriptor programming, 1 frame (descriptor) is
457 * enough, see example above.
458 */
459 *skip_frames = 1;
460 frame = dwc2_frame_num_inc(hsotg->frame_number, 2);
461 }
462
463 return frame;
464}
465
466/*
467 * Calculate initial descriptor index for isochronous transfer based on
468 * scheduled frame
469 */
470static u16 dwc2_recalc_initial_desc_idx(struct dwc2_hsotg *hsotg,
471 struct dwc2_qh *qh)
472{
473 u16 frame, fr_idx, fr_idx_tmp, skip_frames;
474
475 /*
476 * With current ISOC processing algorithm the channel is being released
477 * when no more QTDs in the list (qh->ntd == 0). Thus this function is
478 * called only when qh->ntd == 0 and qh->channel == 0.
479 *
480 * So qh->channel != NULL branch is not used and just not removed from
481 * the source file. It is required for another possible approach which
482 * is, do not disable and release the channel when ISOC session
483 * completed, just move QH to inactive schedule until new QTD arrives.
484 * On new QTD, the QH moved back to 'ready' schedule, starting frame and
485 * therefore starting desc_index are recalculated. In this case channel
486 * is released only on ep_disable.
487 */
488
489 /*
490 * Calculate starting descriptor index. For INTERRUPT endpoint it is
491 * always 0.
492 */
493 if (qh->channel) {
494 frame = dwc2_calc_starting_frame(hsotg, qh, &skip_frames);
495 /*
496 * Calculate initial descriptor index based on FrameList current
497 * bitmap and servicing period
498 */
499 fr_idx_tmp = dwc2_frame_list_idx(frame);
500 fr_idx = (FRLISTEN_64_SIZE +
501 dwc2_frame_list_idx(qh->sched_frame) - fr_idx_tmp)
502 % dwc2_frame_incr_val(qh);
503 fr_idx = (fr_idx + fr_idx_tmp) % FRLISTEN_64_SIZE;
504 } else {
505 qh->sched_frame = dwc2_calc_starting_frame(hsotg, qh,
506 &skip_frames);
507 fr_idx = dwc2_frame_list_idx(qh->sched_frame);
508 }
509
510 qh->td_first = qh->td_last = dwc2_frame_to_desc_idx(qh, fr_idx);
511
512 return skip_frames;
513}
514
515#define ISOC_URB_GIVEBACK_ASAP
516
517#define MAX_ISOC_XFER_SIZE_FS 1023
518#define MAX_ISOC_XFER_SIZE_HS 3072
519#define DESCNUM_THRESHOLD 4
520
521static void dwc2_fill_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
522 struct dwc2_qtd *qtd,
523 struct dwc2_qh *qh, u32 max_xfer_size,
524 u16 idx)
525{
526 struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[idx];
527 struct dwc2_hcd_iso_packet_desc *frame_desc;
528
529 memset(dma_desc, 0, sizeof(*dma_desc));
530 frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
531
532 if (frame_desc->length > max_xfer_size)
533 qh->n_bytes[idx] = max_xfer_size;
534 else
535 qh->n_bytes[idx] = frame_desc->length;
536
537 dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
538 dma_desc->status = qh->n_bytes[idx] << HOST_DMA_ISOC_NBYTES_SHIFT &
539 HOST_DMA_ISOC_NBYTES_MASK;
540
Gregory Herrerodde4c1b2015-11-05 09:41:38 +0100541 /* Set active bit */
542 dma_desc->status |= HOST_DMA_A;
543
Gregory Herrero3ac38d22015-11-05 09:41:37 +0100544 qh->ntd++;
545 qtd->isoc_frame_index_last++;
546
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700547#ifdef ISOC_URB_GIVEBACK_ASAP
548 /* Set IOC for each descriptor corresponding to last frame of URB */
549 if (qtd->isoc_frame_index_last == qtd->urb->packet_count)
550 dma_desc->status |= HOST_DMA_IOC;
551#endif
552
Gregory Herrero95105a92015-11-20 11:49:29 +0100553 dma_sync_single_for_device(hsotg->dev,
554 qh->desc_list_dma +
555 (idx * sizeof(struct dwc2_hcd_dma_desc)),
556 sizeof(struct dwc2_hcd_dma_desc),
557 DMA_TO_DEVICE);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700558}
559
560static void dwc2_init_isoc_dma_desc(struct dwc2_hsotg *hsotg,
561 struct dwc2_qh *qh, u16 skip_frames)
562{
563 struct dwc2_qtd *qtd;
564 u32 max_xfer_size;
Gregory Herreroc17b3372015-11-05 09:41:43 +0100565 u16 idx, inc, n_desc = 0, ntd_max = 0;
566 u16 cur_idx;
567 u16 next_idx;
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700568
569 idx = qh->td_last;
570 inc = qh->interval;
Gregory Herreroc17b3372015-11-05 09:41:43 +0100571 hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
572 cur_idx = dwc2_frame_list_idx(hsotg->frame_number);
573 next_idx = dwc2_desclist_idx_inc(qh->td_last, inc, qh->dev_speed);
574
575 /*
576 * Ensure current frame number didn't overstep last scheduled
577 * descriptor. If it happens, the only way to recover is to move
578 * qh->td_last to current frame number + 1.
579 * So that next isoc descriptor will be scheduled on frame number + 1
580 * and not on a past frame.
581 */
582 if (dwc2_frame_idx_num_gt(cur_idx, next_idx) || (cur_idx == next_idx)) {
583 if (inc < 32) {
584 dev_vdbg(hsotg->dev,
585 "current frame number overstep last descriptor\n");
586 qh->td_last = dwc2_desclist_idx_inc(cur_idx, inc,
587 qh->dev_speed);
588 idx = qh->td_last;
589 }
590 }
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700591
592 if (qh->interval) {
593 ntd_max = (dwc2_max_desc_num(qh) + qh->interval - 1) /
594 qh->interval;
595 if (skip_frames && !qh->channel)
596 ntd_max -= skip_frames / qh->interval;
597 }
598
599 max_xfer_size = qh->dev_speed == USB_SPEED_HIGH ?
600 MAX_ISOC_XFER_SIZE_HS : MAX_ISOC_XFER_SIZE_FS;
601
602 list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
Gregory Herreroc17b3372015-11-05 09:41:43 +0100603 if (qtd->in_process &&
604 qtd->isoc_frame_index_last ==
605 qtd->urb->packet_count)
606 continue;
607
608 qtd->isoc_td_first = idx;
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700609 while (qh->ntd < ntd_max && qtd->isoc_frame_index_last <
610 qtd->urb->packet_count) {
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700611 dwc2_fill_host_isoc_dma_desc(hsotg, qtd, qh,
612 max_xfer_size, idx);
613 idx = dwc2_desclist_idx_inc(idx, inc, qh->dev_speed);
614 n_desc++;
615 }
Gregory Herreroc17b3372015-11-05 09:41:43 +0100616 qtd->isoc_td_last = idx;
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700617 qtd->in_process = 1;
618 }
619
620 qh->td_last = idx;
621
622#ifdef ISOC_URB_GIVEBACK_ASAP
623 /* Set IOC for last descriptor if descriptor list is full */
624 if (qh->ntd == ntd_max) {
625 idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
626 qh->desc_list[idx].status |= HOST_DMA_IOC;
Gregory Herrero95105a92015-11-20 11:49:29 +0100627 dma_sync_single_for_device(hsotg->dev,
628 qh->desc_list_dma + (idx *
629 sizeof(struct dwc2_hcd_dma_desc)),
630 sizeof(struct dwc2_hcd_dma_desc),
631 DMA_TO_DEVICE);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700632 }
633#else
634 /*
635 * Set IOC bit only for one descriptor. Always try to be ahead of HW
636 * processing, i.e. on IOC generation driver activates next descriptor
637 * but core continues to process descriptors following the one with IOC
638 * set.
639 */
640
641 if (n_desc > DESCNUM_THRESHOLD)
642 /*
643 * Move IOC "up". Required even if there is only one QTD
644 * in the list, because QTDs might continue to be queued,
645 * but during the activation it was only one queued.
646 * Actually more than one QTD might be in the list if this
647 * function called from XferCompletion - QTDs was queued during
648 * HW processing of the previous descriptor chunk.
649 */
650 idx = dwc2_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2),
651 qh->dev_speed);
652 else
653 /*
654 * Set the IOC for the latest descriptor if either number of
655 * descriptors is not greater than threshold or no more new
656 * descriptors activated
657 */
658 idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
659
660 qh->desc_list[idx].status |= HOST_DMA_IOC;
Gregory Herrero95105a92015-11-20 11:49:29 +0100661 dma_sync_single_for_device(hsotg->dev,
662 qh->desc_list_dma +
663 (idx * sizeof(struct dwc2_hcd_dma_desc)),
664 sizeof(struct dwc2_hcd_dma_desc),
665 DMA_TO_DEVICE);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700666#endif
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700667}
668
669static void dwc2_fill_host_dma_desc(struct dwc2_hsotg *hsotg,
670 struct dwc2_host_chan *chan,
671 struct dwc2_qtd *qtd, struct dwc2_qh *qh,
672 int n_desc)
673{
674 struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[n_desc];
675 int len = chan->xfer_len;
676
Paul Zimmerman0b851be2013-11-25 13:42:45 -0800677 if (len > MAX_DMA_DESC_SIZE - (chan->max_packet - 1))
678 len = MAX_DMA_DESC_SIZE - (chan->max_packet - 1);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700679
680 if (chan->ep_is_in) {
681 int num_packets;
682
683 if (len > 0 && chan->max_packet)
684 num_packets = (len + chan->max_packet - 1)
685 / chan->max_packet;
686 else
687 /* Need 1 packet for transfer length of 0 */
688 num_packets = 1;
689
690 /* Always program an integral # of packets for IN transfers */
691 len = num_packets * chan->max_packet;
692 }
693
694 dma_desc->status = len << HOST_DMA_NBYTES_SHIFT & HOST_DMA_NBYTES_MASK;
695 qh->n_bytes[n_desc] = len;
696
697 if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL &&
698 qtd->control_phase == DWC2_CONTROL_SETUP)
699 dma_desc->status |= HOST_DMA_SUP;
700
701 dma_desc->buf = (u32)chan->xfer_dma;
702
Gregory Herrero95105a92015-11-20 11:49:29 +0100703 dma_sync_single_for_device(hsotg->dev,
704 qh->desc_list_dma +
705 (n_desc * sizeof(struct dwc2_hcd_dma_desc)),
706 sizeof(struct dwc2_hcd_dma_desc),
707 DMA_TO_DEVICE);
708
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700709 /*
710 * Last (or only) descriptor of IN transfer with actual size less
711 * than MaxPacket
712 */
713 if (len > chan->xfer_len) {
714 chan->xfer_len = 0;
715 } else {
716 chan->xfer_dma += len;
717 chan->xfer_len -= len;
718 }
719}
720
721static void dwc2_init_non_isoc_dma_desc(struct dwc2_hsotg *hsotg,
722 struct dwc2_qh *qh)
723{
724 struct dwc2_qtd *qtd;
725 struct dwc2_host_chan *chan = qh->channel;
726 int n_desc = 0;
727
728 dev_vdbg(hsotg->dev, "%s(): qh=%p dma=%08lx len=%d\n", __func__, qh,
729 (unsigned long)chan->xfer_dma, chan->xfer_len);
730
731 /*
732 * Start with chan->xfer_dma initialized in assign_and_init_hc(), then
733 * if SG transfer consists of multiple URBs, this pointer is re-assigned
734 * to the buffer of the currently processed QTD. For non-SG request
735 * there is always one QTD active.
736 */
737
738 list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
739 dev_vdbg(hsotg->dev, "qtd=%p\n", qtd);
740
741 if (n_desc) {
742 /* SG request - more than 1 QTD */
743 chan->xfer_dma = qtd->urb->dma +
744 qtd->urb->actual_length;
745 chan->xfer_len = qtd->urb->length -
746 qtd->urb->actual_length;
747 dev_vdbg(hsotg->dev, "buf=%08lx len=%d\n",
748 (unsigned long)chan->xfer_dma, chan->xfer_len);
749 }
750
751 qtd->n_desc = 0;
752 do {
753 if (n_desc > 1) {
754 qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
755 dev_vdbg(hsotg->dev,
756 "set A bit in desc %d (%p)\n",
757 n_desc - 1,
758 &qh->desc_list[n_desc - 1]);
Gregory Herrero95105a92015-11-20 11:49:29 +0100759 dma_sync_single_for_device(hsotg->dev,
760 qh->desc_list_dma +
761 ((n_desc - 1) *
762 sizeof(struct dwc2_hcd_dma_desc)),
763 sizeof(struct dwc2_hcd_dma_desc),
764 DMA_TO_DEVICE);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700765 }
766 dwc2_fill_host_dma_desc(hsotg, chan, qtd, qh, n_desc);
767 dev_vdbg(hsotg->dev,
768 "desc %d (%p) buf=%08x status=%08x\n",
769 n_desc, &qh->desc_list[n_desc],
770 qh->desc_list[n_desc].buf,
771 qh->desc_list[n_desc].status);
772 qtd->n_desc++;
773 n_desc++;
774 } while (chan->xfer_len > 0 &&
775 n_desc != MAX_DMA_DESC_NUM_GENERIC);
776
777 dev_vdbg(hsotg->dev, "n_desc=%d\n", n_desc);
778 qtd->in_process = 1;
779 if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL)
780 break;
781 if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
782 break;
783 }
784
785 if (n_desc) {
786 qh->desc_list[n_desc - 1].status |=
787 HOST_DMA_IOC | HOST_DMA_EOL | HOST_DMA_A;
788 dev_vdbg(hsotg->dev, "set IOC/EOL/A bits in desc %d (%p)\n",
789 n_desc - 1, &qh->desc_list[n_desc - 1]);
Gregory Herrero95105a92015-11-20 11:49:29 +0100790 dma_sync_single_for_device(hsotg->dev,
791 qh->desc_list_dma + (n_desc - 1) *
792 sizeof(struct dwc2_hcd_dma_desc),
793 sizeof(struct dwc2_hcd_dma_desc),
794 DMA_TO_DEVICE);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700795 if (n_desc > 1) {
796 qh->desc_list[0].status |= HOST_DMA_A;
797 dev_vdbg(hsotg->dev, "set A bit in desc 0 (%p)\n",
798 &qh->desc_list[0]);
Gregory Herrero95105a92015-11-20 11:49:29 +0100799 dma_sync_single_for_device(hsotg->dev,
800 qh->desc_list_dma,
801 sizeof(struct dwc2_hcd_dma_desc),
802 DMA_TO_DEVICE);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700803 }
804 chan->ntd = n_desc;
805 }
806}
807
808/**
809 * dwc2_hcd_start_xfer_ddma() - Starts a transfer in Descriptor DMA mode
810 *
811 * @hsotg: The HCD state structure for the DWC OTG controller
812 * @qh: The QH to init
813 *
814 * Return: 0 if successful, negative error code otherwise
815 *
816 * For Control and Bulk endpoints, initializes descriptor list and starts the
817 * transfer. For Interrupt and Isochronous endpoints, initializes descriptor
818 * list then updates FrameList, marking appropriate entries as active.
819 *
820 * For Isochronous endpoints the starting descriptor index is calculated based
821 * on the scheduled frame, but only on the first transfer descriptor within a
822 * session. Then the transfer is started via enabling the channel.
823 *
824 * For Isochronous endpoints the channel is not halted on XferComplete
825 * interrupt so remains assigned to the endpoint(QH) until session is done.
826 */
827void dwc2_hcd_start_xfer_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
828{
829 /* Channel is already assigned */
830 struct dwc2_host_chan *chan = qh->channel;
831 u16 skip_frames = 0;
832
833 switch (chan->ep_type) {
834 case USB_ENDPOINT_XFER_CONTROL:
835 case USB_ENDPOINT_XFER_BULK:
836 dwc2_init_non_isoc_dma_desc(hsotg, qh);
837 dwc2_hc_start_transfer_ddma(hsotg, chan);
838 break;
839 case USB_ENDPOINT_XFER_INT:
840 dwc2_init_non_isoc_dma_desc(hsotg, qh);
841 dwc2_update_frame_list(hsotg, qh, 1);
842 dwc2_hc_start_transfer_ddma(hsotg, chan);
843 break;
844 case USB_ENDPOINT_XFER_ISOC:
845 if (!qh->ntd)
846 skip_frames = dwc2_recalc_initial_desc_idx(hsotg, qh);
847 dwc2_init_isoc_dma_desc(hsotg, qh, skip_frames);
848
849 if (!chan->xfer_started) {
850 dwc2_update_frame_list(hsotg, qh, 1);
851
852 /*
853 * Always set to max, instead of actual size. Otherwise
854 * ntd will be changed with channel being enabled. Not
855 * recommended.
856 */
857 chan->ntd = dwc2_max_desc_num(qh);
858
859 /* Enable channel only once for ISOC */
860 dwc2_hc_start_transfer_ddma(hsotg, chan);
861 }
862
863 break;
864 default:
865 break;
866 }
867}
868
869#define DWC2_CMPL_DONE 1
870#define DWC2_CMPL_STOP 2
871
872static int dwc2_cmpl_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
873 struct dwc2_host_chan *chan,
874 struct dwc2_qtd *qtd,
875 struct dwc2_qh *qh, u16 idx)
876{
Gregory Herrero95105a92015-11-20 11:49:29 +0100877 struct dwc2_hcd_dma_desc *dma_desc;
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700878 struct dwc2_hcd_iso_packet_desc *frame_desc;
879 u16 remain = 0;
880 int rc = 0;
881
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700882 if (!qtd->urb)
883 return -EINVAL;
884
Gregory Herrero95105a92015-11-20 11:49:29 +0100885 dma_sync_single_for_cpu(hsotg->dev, qh->desc_list_dma + (idx *
886 sizeof(struct dwc2_hcd_dma_desc)),
887 sizeof(struct dwc2_hcd_dma_desc),
888 DMA_FROM_DEVICE);
889
890 dma_desc = &qh->desc_list[idx];
891
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700892 frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
893 dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
894 if (chan->ep_is_in)
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +0200895 remain = (dma_desc->status & HOST_DMA_ISOC_NBYTES_MASK) >>
896 HOST_DMA_ISOC_NBYTES_SHIFT;
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700897
898 if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
899 /*
900 * XactError, or unable to complete all the transactions
901 * in the scheduled micro-frame/frame, both indicated by
902 * HOST_DMA_STS_PKTERR
903 */
904 qtd->urb->error_count++;
905 frame_desc->actual_length = qh->n_bytes[idx] - remain;
906 frame_desc->status = -EPROTO;
907 } else {
908 /* Success */
909 frame_desc->actual_length = qh->n_bytes[idx] - remain;
910 frame_desc->status = 0;
911 }
912
913 if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
914 /*
915 * urb->status is not used for isoc transfers here. The
916 * individual frame_desc status are used instead.
917 */
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700918 dwc2_host_complete(hsotg, qtd, 0);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700919 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
920
921 /*
922 * This check is necessary because urb_dequeue can be called
923 * from urb complete callback (sound driver for example). All
924 * pending URBs are dequeued there, so no need for further
925 * processing.
926 */
927 if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE)
928 return -1;
929 rc = DWC2_CMPL_DONE;
930 }
931
932 qh->ntd--;
933
934 /* Stop if IOC requested descriptor reached */
935 if (dma_desc->status & HOST_DMA_IOC)
936 rc = DWC2_CMPL_STOP;
937
938 return rc;
939}
940
941static void dwc2_complete_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
942 struct dwc2_host_chan *chan,
943 enum dwc2_halt_status halt_status)
944{
945 struct dwc2_hcd_iso_packet_desc *frame_desc;
946 struct dwc2_qtd *qtd, *qtd_tmp;
947 struct dwc2_qh *qh;
948 u16 idx;
949 int rc;
950
951 qh = chan->qh;
952 idx = qh->td_first;
953
954 if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
955 list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
956 qtd->in_process = 0;
957 return;
958 }
959
960 if (halt_status == DWC2_HC_XFER_AHB_ERR ||
961 halt_status == DWC2_HC_XFER_BABBLE_ERR) {
962 /*
963 * Channel is halted in these error cases, considered as serious
964 * issues.
965 * Complete all URBs marking all frames as failed, irrespective
966 * whether some of the descriptors (frames) succeeded or not.
967 * Pass error code to completion routine as well, to update
968 * urb->status, some of class drivers might use it to stop
969 * queing transfer requests.
970 */
971 int err = halt_status == DWC2_HC_XFER_AHB_ERR ?
972 -EIO : -EOVERFLOW;
973
974 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
975 qtd_list_entry) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700976 if (qtd->urb) {
977 for (idx = 0; idx < qtd->urb->packet_count;
978 idx++) {
979 frame_desc = &qtd->urb->iso_descs[idx];
980 frame_desc->status = err;
981 }
982
983 dwc2_host_complete(hsotg, qtd, err);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700984 }
985
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700986 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
987 }
988
989 return;
990 }
991
992 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
993 if (!qtd->in_process)
994 break;
Gregory Herrero762d3a12015-11-05 09:41:45 +0100995
996 /*
997 * Ensure idx corresponds to descriptor where first urb of this
998 * qtd was added. In fact, during isoc desc init, dwc2 may skip
999 * an index if current frame number is already over this index.
1000 */
1001 if (idx != qtd->isoc_td_first) {
1002 dev_vdbg(hsotg->dev,
1003 "try to complete %d instead of %d\n",
1004 idx, qtd->isoc_td_first);
1005 idx = qtd->isoc_td_first;
1006 }
1007
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001008 do {
Gregory Herrero762d3a12015-11-05 09:41:45 +01001009 struct dwc2_qtd *qtd_next;
1010 u16 cur_idx;
1011
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001012 rc = dwc2_cmpl_host_isoc_dma_desc(hsotg, chan, qtd, qh,
1013 idx);
1014 if (rc < 0)
1015 return;
1016 idx = dwc2_desclist_idx_inc(idx, qh->interval,
1017 chan->speed);
Gregory Herrero762d3a12015-11-05 09:41:45 +01001018 if (!rc)
1019 continue;
1020
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001021 if (rc == DWC2_CMPL_DONE)
1022 break;
Gregory Herrero762d3a12015-11-05 09:41:45 +01001023
1024 /* rc == DWC2_CMPL_STOP */
1025
1026 if (qh->interval >= 32)
1027 goto stop_scan;
1028
1029 qh->td_first = idx;
1030 cur_idx = dwc2_frame_list_idx(hsotg->frame_number);
1031 qtd_next = list_first_entry(&qh->qtd_list,
1032 struct dwc2_qtd,
1033 qtd_list_entry);
1034 if (dwc2_frame_idx_num_gt(cur_idx,
1035 qtd_next->isoc_td_last))
1036 break;
1037
1038 goto stop_scan;
1039
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001040 } while (idx != qh->td_first);
1041 }
1042
1043stop_scan:
1044 qh->td_first = idx;
1045}
1046
1047static int dwc2_update_non_isoc_urb_state_ddma(struct dwc2_hsotg *hsotg,
1048 struct dwc2_host_chan *chan,
1049 struct dwc2_qtd *qtd,
1050 struct dwc2_hcd_dma_desc *dma_desc,
1051 enum dwc2_halt_status halt_status,
1052 u32 n_bytes, int *xfer_done)
1053{
1054 struct dwc2_hcd_urb *urb = qtd->urb;
1055 u16 remain = 0;
1056
1057 if (chan->ep_is_in)
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001058 remain = (dma_desc->status & HOST_DMA_NBYTES_MASK) >>
1059 HOST_DMA_NBYTES_SHIFT;
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001060
1061 dev_vdbg(hsotg->dev, "remain=%d dwc2_urb=%p\n", remain, urb);
1062
1063 if (halt_status == DWC2_HC_XFER_AHB_ERR) {
1064 dev_err(hsotg->dev, "EIO\n");
1065 urb->status = -EIO;
1066 return 1;
1067 }
1068
1069 if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
1070 switch (halt_status) {
1071 case DWC2_HC_XFER_STALL:
1072 dev_vdbg(hsotg->dev, "Stall\n");
1073 urb->status = -EPIPE;
1074 break;
1075 case DWC2_HC_XFER_BABBLE_ERR:
1076 dev_err(hsotg->dev, "Babble\n");
1077 urb->status = -EOVERFLOW;
1078 break;
1079 case DWC2_HC_XFER_XACT_ERR:
1080 dev_err(hsotg->dev, "XactErr\n");
1081 urb->status = -EPROTO;
1082 break;
1083 default:
1084 dev_err(hsotg->dev,
1085 "%s: Unhandled descriptor error status (%d)\n",
1086 __func__, halt_status);
1087 break;
1088 }
1089 return 1;
1090 }
1091
1092 if (dma_desc->status & HOST_DMA_A) {
1093 dev_vdbg(hsotg->dev,
1094 "Active descriptor encountered on channel %d\n",
1095 chan->hc_num);
1096 return 0;
1097 }
1098
1099 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL) {
1100 if (qtd->control_phase == DWC2_CONTROL_DATA) {
1101 urb->actual_length += n_bytes - remain;
1102 if (remain || urb->actual_length >= urb->length) {
1103 /*
1104 * For Control Data stage do not set urb->status
1105 * to 0, to prevent URB callback. Set it when
1106 * Status phase is done. See below.
1107 */
1108 *xfer_done = 1;
1109 }
1110 } else if (qtd->control_phase == DWC2_CONTROL_STATUS) {
1111 urb->status = 0;
1112 *xfer_done = 1;
1113 }
1114 /* No handling for SETUP stage */
1115 } else {
1116 /* BULK and INTR */
1117 urb->actual_length += n_bytes - remain;
1118 dev_vdbg(hsotg->dev, "length=%d actual=%d\n", urb->length,
1119 urb->actual_length);
1120 if (remain || urb->actual_length >= urb->length) {
1121 urb->status = 0;
1122 *xfer_done = 1;
1123 }
1124 }
1125
1126 return 0;
1127}
1128
1129static int dwc2_process_non_isoc_desc(struct dwc2_hsotg *hsotg,
1130 struct dwc2_host_chan *chan,
1131 int chnum, struct dwc2_qtd *qtd,
1132 int desc_num,
1133 enum dwc2_halt_status halt_status,
1134 int *xfer_done)
1135{
1136 struct dwc2_qh *qh = chan->qh;
1137 struct dwc2_hcd_urb *urb = qtd->urb;
1138 struct dwc2_hcd_dma_desc *dma_desc;
1139 u32 n_bytes;
1140 int failed;
1141
1142 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1143
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001144 if (!urb)
1145 return -EINVAL;
1146
Gregory Herrero95105a92015-11-20 11:49:29 +01001147 dma_sync_single_for_cpu(hsotg->dev,
1148 qh->desc_list_dma + (desc_num *
1149 sizeof(struct dwc2_hcd_dma_desc)),
1150 sizeof(struct dwc2_hcd_dma_desc),
1151 DMA_FROM_DEVICE);
1152
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001153 dma_desc = &qh->desc_list[desc_num];
1154 n_bytes = qh->n_bytes[desc_num];
1155 dev_vdbg(hsotg->dev,
1156 "qtd=%p dwc2_urb=%p desc_num=%d desc=%p n_bytes=%d\n",
1157 qtd, urb, desc_num, dma_desc, n_bytes);
1158 failed = dwc2_update_non_isoc_urb_state_ddma(hsotg, chan, qtd, dma_desc,
1159 halt_status, n_bytes,
1160 xfer_done);
Gregory Herrero26a19ea2015-11-05 09:41:40 +01001161 if (*xfer_done && urb->status != -EINPROGRESS)
1162 failed = 1;
1163
1164 if (failed) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001165 dwc2_host_complete(hsotg, qtd, urb->status);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001166 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1167 dev_vdbg(hsotg->dev, "failed=%1x xfer_done=%1x status=%08x\n",
1168 failed, *xfer_done, urb->status);
1169 return failed;
1170 }
1171
1172 if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL) {
1173 switch (qtd->control_phase) {
1174 case DWC2_CONTROL_SETUP:
1175 if (urb->length > 0)
1176 qtd->control_phase = DWC2_CONTROL_DATA;
1177 else
1178 qtd->control_phase = DWC2_CONTROL_STATUS;
1179 dev_vdbg(hsotg->dev,
1180 " Control setup transaction done\n");
1181 break;
1182 case DWC2_CONTROL_DATA:
1183 if (*xfer_done) {
1184 qtd->control_phase = DWC2_CONTROL_STATUS;
1185 dev_vdbg(hsotg->dev,
1186 " Control data transfer done\n");
1187 } else if (desc_num + 1 == qtd->n_desc) {
1188 /*
1189 * Last descriptor for Control data stage which
1190 * is not completed yet
1191 */
1192 dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
1193 qtd);
1194 }
1195 break;
1196 default:
1197 break;
1198 }
1199 }
1200
1201 return 0;
1202}
1203
1204static void dwc2_complete_non_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
1205 struct dwc2_host_chan *chan,
1206 int chnum,
1207 enum dwc2_halt_status halt_status)
1208{
1209 struct list_head *qtd_item, *qtd_tmp;
1210 struct dwc2_qh *qh = chan->qh;
1211 struct dwc2_qtd *qtd = NULL;
1212 int xfer_done;
1213 int desc_num = 0;
1214
1215 if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
1216 list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
1217 qtd->in_process = 0;
1218 return;
1219 }
1220
1221 list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
1222 int i;
1223
1224 qtd = list_entry(qtd_item, struct dwc2_qtd, qtd_list_entry);
1225 xfer_done = 0;
1226
1227 for (i = 0; i < qtd->n_desc; i++) {
1228 if (dwc2_process_non_isoc_desc(hsotg, chan, chnum, qtd,
1229 desc_num, halt_status,
Paul Zimmermanfbd1cd22013-11-22 16:43:46 -08001230 &xfer_done)) {
1231 qtd = NULL;
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001232 break;
Paul Zimmermanfbd1cd22013-11-22 16:43:46 -08001233 }
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001234 desc_num++;
1235 }
1236 }
1237
1238 if (qh->ep_type != USB_ENDPOINT_XFER_CONTROL) {
1239 /*
1240 * Resetting the data toggle for bulk and interrupt endpoints
1241 * in case of stall. See handle_hc_stall_intr().
1242 */
1243 if (halt_status == DWC2_HC_XFER_STALL)
1244 qh->data_toggle = DWC2_HC_PID_DATA0;
1245 else if (qtd)
1246 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1247 }
1248
1249 if (halt_status == DWC2_HC_XFER_COMPLETE) {
1250 if (chan->hcint & HCINTMSK_NYET) {
1251 /*
1252 * Got a NYET on the last transaction of the transfer.
1253 * It means that the endpoint should be in the PING
1254 * state at the beginning of the next transfer.
1255 */
1256 qh->ping_state = 1;
1257 }
1258 }
1259}
1260
1261/**
1262 * dwc2_hcd_complete_xfer_ddma() - Scans the descriptor list, updates URB's
1263 * status and calls completion routine for the URB if it's done. Called from
1264 * interrupt handlers.
1265 *
1266 * @hsotg: The HCD state structure for the DWC OTG controller
1267 * @chan: Host channel the transfer is completed on
1268 * @chnum: Index of Host channel registers
1269 * @halt_status: Reason the channel is being halted or just XferComplete
1270 * for isochronous transfers
1271 *
1272 * Releases the channel to be used by other transfers.
1273 * In case of Isochronous endpoint the channel is not halted until the end of
1274 * the session, i.e. QTD list is empty.
1275 * If periodic channel released the FrameList is updated accordingly.
1276 * Calls transaction selection routines to activate pending transfers.
1277 */
1278void dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg *hsotg,
1279 struct dwc2_host_chan *chan, int chnum,
1280 enum dwc2_halt_status halt_status)
1281{
1282 struct dwc2_qh *qh = chan->qh;
1283 int continue_isoc_xfer = 0;
1284 enum dwc2_transaction_type tr_type;
1285
1286 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1287 dwc2_complete_isoc_xfer_ddma(hsotg, chan, halt_status);
1288
1289 /* Release the channel if halted or session completed */
1290 if (halt_status != DWC2_HC_XFER_COMPLETE ||
1291 list_empty(&qh->qtd_list)) {
Gregory Herreroc503b382015-11-05 09:41:39 +01001292 struct dwc2_qtd *qtd, *qtd_tmp;
1293
1294 /*
1295 * Kill all remainings QTDs since channel has been
1296 * halted.
1297 */
1298 list_for_each_entry_safe(qtd, qtd_tmp,
1299 &qh->qtd_list,
1300 qtd_list_entry) {
1301 dwc2_host_complete(hsotg, qtd,
1302 -ECONNRESET);
1303 dwc2_hcd_qtd_unlink_and_free(hsotg,
1304 qtd, qh);
1305 }
1306
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001307 /* Halt the channel if session completed */
1308 if (halt_status == DWC2_HC_XFER_COMPLETE)
1309 dwc2_hc_halt(hsotg, chan, halt_status);
1310 dwc2_release_channel_ddma(hsotg, qh);
1311 dwc2_hcd_qh_unlink(hsotg, qh);
1312 } else {
1313 /* Keep in assigned schedule to continue transfer */
1314 list_move(&qh->qh_list_entry,
1315 &hsotg->periodic_sched_assigned);
Gregory Herreroc503b382015-11-05 09:41:39 +01001316 /*
1317 * If channel has been halted during giveback of urb
1318 * then prevent any new scheduling.
1319 */
1320 if (!chan->halt_status)
1321 continue_isoc_xfer = 1;
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001322 }
1323 /*
1324 * Todo: Consider the case when period exceeds FrameList size.
1325 * Frame Rollover interrupt should be used.
1326 */
1327 } else {
1328 /*
1329 * Scan descriptor list to complete the URB(s), then release
1330 * the channel
1331 */
1332 dwc2_complete_non_isoc_xfer_ddma(hsotg, chan, chnum,
1333 halt_status);
1334 dwc2_release_channel_ddma(hsotg, qh);
1335 dwc2_hcd_qh_unlink(hsotg, qh);
1336
1337 if (!list_empty(&qh->qtd_list)) {
1338 /*
1339 * Add back to inactive non-periodic schedule on normal
1340 * completion
1341 */
1342 dwc2_hcd_qh_add(hsotg, qh);
1343 }
1344 }
1345
1346 tr_type = dwc2_hcd_select_transactions(hsotg);
1347 if (tr_type != DWC2_TRANSACTION_NONE || continue_isoc_xfer) {
1348 if (continue_isoc_xfer) {
1349 if (tr_type == DWC2_TRANSACTION_NONE)
1350 tr_type = DWC2_TRANSACTION_PERIODIC;
1351 else if (tr_type == DWC2_TRANSACTION_NON_PERIODIC)
1352 tr_type = DWC2_TRANSACTION_ALL;
1353 }
1354 dwc2_hcd_queue_transactions(hsotg, tr_type);
1355 }
1356}