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