📄 ehci-sched.c
字号:
for (i = 0; i < 2; i++) { itd->hw_bufp [i] = cpu_to_le32 ((u32) temp); itd->hw_bufp_hi [i] = cpu_to_le32 ((u32)(temp >> 32)); temp += 0x1000; } itd->buf_dma = dma; /* * this might be a "high bandwidth" highspeed endpoint, * as encoded in the ep descriptor's maxpacket field */ epnum = usb_pipeendpoint (urb->pipe); is_input = usb_pipein (urb->pipe); if (is_input) { maxp = urb->dev->epmaxpacketin [epnum]; buf1 = (1 << 11); } else { maxp = urb->dev->epmaxpacketout [epnum]; buf1 = 0; } buf1 |= (maxp & 0x03ff); multi = 1; multi += (maxp >> 11) & 0x03; maxp &= 0x03ff; maxp *= multi; /* transfer can't fit in any uframe? */ if (length < 0 || maxp < length) { dbg ("BAD iso packet: %d bytes, max %d, urb %p [%d] (of %d)", length, maxp, urb, index, urb->iso_frame_desc [index].length); return -ENOSPC; } itd->usecs = usb_calc_bus_time (USB_SPEED_HIGH, is_input, 1, length); /* "plus" info in low order bits of buffer pointers */ itd->hw_bufp [0] |= cpu_to_le32 ((epnum << 8) | urb->dev->devnum); itd->hw_bufp [1] |= cpu_to_le32 (buf1); itd->hw_bufp [2] |= cpu_to_le32 (multi); /* figure hw_transaction[] value (it's scheduled later) */ itd->transaction = EHCI_ISOC_ACTIVE; itd->transaction |= dma & 0x0fff; /* offset; buffer=0 */ if ((index + 1) == urb->number_of_packets) itd->transaction |= EHCI_ITD_IOC; /* end-of-urb irq */ itd->transaction |= length << 16; cpu_to_le32s (&itd->transaction); return 0;}static intitd_urb_transaction ( struct ehci_hcd *ehci, struct urb *urb, int mem_flags) { int frame_index; struct ehci_itd *first_itd, *itd; int status; dma_addr_t itd_dma; /* allocate/init ITDs */ for (frame_index = 0, first_itd = 0; frame_index < urb->number_of_packets; frame_index++) { itd = pci_pool_alloc (ehci->itd_pool, mem_flags, &itd_dma); if (!itd) { status = -ENOMEM; goto fail; } memset (itd, 0, sizeof *itd); itd->itd_dma = itd_dma; status = itd_fill (ehci, itd, urb, frame_index, urb->transfer_dma); if (status != 0) goto fail; if (first_itd) list_add_tail (&itd->itd_list, &first_itd->itd_list); else { INIT_LIST_HEAD (&itd->itd_list); urb->hcpriv = first_itd = itd; } } urb->error_count = 0; return 0;fail: if (urb->hcpriv) itd_free_list (ehci, urb); return status;}/*-------------------------------------------------------------------------*/static inline voiditd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd){ /* always prepend ITD/SITD ... only QH tree is order-sensitive */ itd->itd_next = ehci->pshadow [frame]; itd->hw_next = ehci->periodic [frame]; ehci->pshadow [frame].itd = itd; ehci->periodic [frame] = cpu_to_le32 (itd->itd_dma) | Q_TYPE_ITD;}/* * return zero on success, else -errno * - start holds first uframe to start scheduling into * - max is the first uframe it's NOT (!) OK to start scheduling into * math to be done modulo "mod" (ehci->periodic_size << 3) */static int get_iso_range ( struct ehci_hcd *ehci, struct urb *urb, unsigned *start, unsigned *max, unsigned mod) { struct list_head *lh; struct hcd_dev *dev = urb->dev->hcpriv; int last = -1; unsigned now, span, end; span = urb->interval * urb->number_of_packets; /* first see if we know when the next transfer SHOULD happen */ list_for_each (lh, &dev->urb_list) { struct urb *u; struct ehci_itd *itd; unsigned s; u = list_entry (lh, struct urb, urb_list); if (u == urb || u->pipe != urb->pipe) continue; if (u->interval != urb->interval) { /* must not change! */ dbg ("urb %p interval %d ... != %p interval %d", u, u->interval, urb, urb->interval); return -EINVAL; } /* URB for this endpoint... covers through when? */ itd = urb->hcpriv; s = itd->uframe + u->interval * u->number_of_packets; if (last < 0) last = s; else { /* * So far we can only queue two ISO URBs... * * FIXME do interval math, figure out whether * this URB is "before" or not ... also, handle * the case where the URB might have completed, * but hasn't yet been processed. */ dbg ("NYET: queue >2 URBs per ISO endpoint"); return -EDOM; } } /* calculate the legal range [start,max) */ now = readl (&ehci->regs->frame_index) + 1; /* next uframe */ if (!ehci->periodic_sched) now += 8; /* startup delay */ now %= mod; end = now + mod; if (last < 0) { *start = now + ehci->i_thresh + /* paranoia */ 1; *max = end - span; if (*max < *start + 1) *max = *start + 1; } else { *start = last % mod; *max = (last + 1) % mod; } /* explicit start frame? */ if (!(urb->transfer_flags & URB_ISO_ASAP)) { unsigned temp; /* sanity check: must be in range */ urb->start_frame %= ehci->periodic_size; temp = urb->start_frame << 3; if (temp < *start) temp += mod; if (temp > *max) return -EDOM; /* use that explicit start frame */ *start = urb->start_frame << 3; temp += 8; if (temp < *max) *max = temp; } // FIXME minimize wraparound to "now" ... insist max+span // (and start+span) remains a few frames short of "end" *max %= ehci->periodic_size; if ((*start + span) < end) return 0; return -EFBIG;}static intitd_schedule (struct ehci_hcd *ehci, struct urb *urb){ unsigned start, max, i; int status; unsigned mod = ehci->periodic_size << 3; for (i = 0; i < urb->number_of_packets; i++) { urb->iso_frame_desc [i].status = -EINPROGRESS; urb->iso_frame_desc [i].actual_length = 0; } if ((status = get_iso_range (ehci, urb, &start, &max, mod)) != 0) return status; do { unsigned uframe; unsigned usecs; struct ehci_itd *itd; /* check schedule: enough space? */ itd = urb->hcpriv; uframe = start; for (i = 0, uframe = start; i < urb->number_of_packets; i++, uframe += urb->interval) { uframe %= mod; /* can't commit more than 80% periodic == 100 usec */ if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7) > (100 - itd->usecs)) { itd = 0; break; } itd = list_entry (itd->itd_list.next, struct ehci_itd, itd_list); } if (!itd) continue; /* that's where we'll schedule this! */ itd = urb->hcpriv; urb->start_frame = start >> 3; vdbg ("ISO urb %p (%d packets period %d) starting %d.%d", urb, urb->number_of_packets, urb->interval, urb->start_frame, start & 0x7); for (i = 0, uframe = start, usecs = 0; i < urb->number_of_packets; i++, uframe += urb->interval) { uframe %= mod; itd->uframe = uframe; itd->hw_transaction [uframe & 0x07] = itd->transaction; itd_link (ehci, (uframe >> 3) % ehci->periodic_size, itd); wmb (); usecs += itd->usecs; itd = list_entry (itd->itd_list.next, struct ehci_itd, itd_list); } /* update bandwidth utilization records (for usbfs) * * FIXME This claims each URB queued to an endpoint, as if * transfers were concurrent, not sequential. So bandwidth * typically gets double-billed ... comes from tying it to * URBs rather than endpoints in the schedule. Luckily we * don't use this usbfs data for serious decision making. */ usecs /= urb->number_of_packets; usecs /= urb->interval; usecs >>= 3; if (usecs < 1) usecs = 1; usb_claim_bandwidth (urb->dev, urb, usecs, 1); /* maybe enable periodic schedule processing */ if (!ehci->periodic_sched++) { if ((status = enable_periodic (ehci)) != 0) { // FIXME deschedule right away err ("itd_schedule, enable = %d", status); } } return 0; } while ((start = ++start % mod) != max); /* no room in the schedule */ dbg ("urb %p, CAN'T SCHEDULE", urb); return -ENOSPC;}/*-------------------------------------------------------------------------*/#define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)static unsigneditd_complete ( struct ehci_hcd *ehci, struct ehci_itd *itd, unsigned uframe, struct pt_regs *regs) { struct urb *urb = itd->urb; struct usb_iso_packet_descriptor *desc; u32 t; /* update status for this uframe's transfers */ desc = &urb->iso_frame_desc [itd->index]; t = itd->hw_transaction [uframe]; itd->hw_transaction [uframe] = 0; if (t & EHCI_ISOC_ACTIVE) desc->status = -EXDEV; else if (t & ISO_ERRS) { urb->error_count++; if (t & EHCI_ISOC_BUF_ERR) desc->status = usb_pipein (urb->pipe) ? -ENOSR /* couldn't read */ : -ECOMM; /* couldn't write */ else if (t & EHCI_ISOC_BABBLE) desc->status = -EOVERFLOW; else /* (t & EHCI_ISOC_XACTERR) */ desc->status = -EPROTO; /* HC need not update length with this error */ if (!(t & EHCI_ISOC_BABBLE)) desc->actual_length += EHCI_ITD_LENGTH (t); } else { desc->status = 0; desc->actual_length += EHCI_ITD_LENGTH (t); } vdbg ("itd %p urb %p packet %d/%d trans %x status %d len %d", itd, urb, itd->index + 1, urb->number_of_packets, t, desc->status, desc->actual_length); /* handle completion now? */ if ((itd->index + 1) != urb->number_of_packets) return 0; /* * Always give the urb back to the driver ... expect it to submit * a new urb (or resubmit this), and to have another already queued * when un-interrupted transfers are needed. * * NOTE that for now we don't accelerate ISO unlinks; they just * happen according to the current schedule. Means a delay of * up to about a second (max). */ itd_free_list (ehci, urb); if (urb->status == -EINPROGRESS) urb->status = 0; /* complete() can reenter this HCD */ spin_unlock (&ehci->lock); usb_hcd_giveback_urb (&ehci->hcd, urb, regs); spin_lock (&ehci->lock); /* defer stopping schedule; completion can submit */ ehci->periodic_sched--; if (!ehci->periodic_sched) (void) disable_periodic (ehci); return 1;}/*-------------------------------------------------------------------------*/static int itd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags){ int status; unsigned long flags; dbg ("itd_submit urb %p", urb); /* allocate ITDs w/o locking anything */ status = itd_urb_transaction (ehci, urb, mem_flags); if (status < 0) return status; /* schedule ... need to lock */ spin_lock_irqsave (&ehci->lock, flags); status = itd_schedule (ehci, urb); spin_unlock_irqrestore (&ehci->lock, flags); if (status < 0) itd_free_list (ehci, urb); return status;}#ifdef have_split_iso/*-------------------------------------------------------------------------*//* * "Split ISO TDs" ... used for USB 1.1 devices going through * the TTs in USB 2.0 hubs. * * FIXME not yet implemented */#endif /* have_split_iso *//*-------------------------------------------------------------------------*/static voidscan_periodic (struct ehci_hcd *ehci, struct pt_regs *regs){ unsigned frame, clock, now_uframe, mod; unsigned count = 0; mod = ehci->periodic_size << 3; /* * When running, scan from last scan point up to "now" * else clean up by scanning everything that's left. * Touches as few pages as possible: cache-friendly. * Don't scan ISO entries more than once, though. */ frame = ehci->next_uframe >> 3; if (HCD_IS_RUNNING (ehci->hcd.state)) now_uframe = readl (&ehci->regs->frame_index); else now_uframe = (frame << 3) - 1; now_uframe %= mod; clock = now_uframe >> 3; for (;;) { union ehci_shadow q, *q_p; u32 type, *hw_p; unsigned uframes;restart: /* scan schedule to _before_ current frame index */ if (frame == clock) uframes = now_uframe & 0x07; else uframes = 8; q_p = &ehci->pshadow [frame]; hw_p = &ehci->periodic [frame]; q.ptr = q_p->ptr; type = Q_NEXT_TYPE (*hw_p); /* scan each element in frame's queue for completions */ while (q.ptr != 0) { int last; unsigned uf; union ehci_shadow temp; switch (type) { case Q_TYPE_QH: last = (q.qh->hw_next == EHCI_LIST_END); temp = q.qh->qh_next; type = Q_NEXT_TYPE (q.qh->hw_next); count += intr_complete (ehci, frame, qh_get (q.qh), regs); qh_put (ehci, q.qh); q = temp; break; case Q_TYPE_FSTN: last = (q.fstn->hw_next == EHCI_LIST_END); /* for "save place" FSTNs, look at QH entries * in the previous frame for completions. */ if (q.fstn->hw_prev != EHCI_LIST_END) { dbg ("ignoring completions from FSTNs"); } type = Q_NEXT_TYPE (q.fstn->hw_next); q = q.fstn->fstn_next; break; case Q_TYPE_ITD: last = (q.itd->hw_next == EHCI_LIST_END); /* Unlink each (S)ITD we see, since the ISO * URB model forces constant rescheduling. * That complicates sharing uframes in ITDs, * and means we need to skip uframes the HC * hasn't yet processed. */ for (uf = 0; uf < uframes; uf++) { if (q.itd->hw_transaction [uf] != 0) { temp = q; *q_p = q.itd->itd_next; *hw_p = q.itd->hw_next; type = Q_NEXT_TYPE (*hw_p); /* might free q.itd ... */ count += itd_complete (ehci, temp.itd, uf, regs); break; } } /* we might skip this ITD's uframe ... */ if (uf == uframes) { q_p = &q.itd->itd_next; hw_p = &q.itd->hw_next; type = Q_NEXT_TYPE (q.itd->hw_next); } q = *q_p; break;#ifdef have_split_iso case Q_TYPE_SITD: last = (q.sitd->hw_next == EHCI_LIST_END); sitd_complete (ehci, q.sitd); type = Q_NEXT_TYPE (q.sitd->hw_next); // FIXME unlink SITD after split completes q = q.sitd->sitd_next; break;#endif /* have_split_iso */ default: dbg ("corrupt type %d frame %d shadow %p", type, frame, q.ptr); // BUG (); last = 1; q.ptr = 0; } /* did completion remove an interior q entry? */ if (unlikely (q.ptr == 0 && !last)) goto restart; } /* stop when we catch up to the HC */ // FIXME: this assumes we won't get lapped when // latencies climb; that should be rare, but... // detect it, and just go all the way around. // FLR might help detect this case, so long as latencies // don't exceed periodic_size msec (default 1.024 sec). // FIXME: likewise assumes HC doesn't halt mid-scan if (frame == clock) { unsigned now; if (!HCD_IS_RUNNING (ehci->hcd.state)) break; ehci->next_uframe = now_uframe; now = readl (&ehci->regs->frame_index) % mod; if (now_uframe == now) break; /* rescan the rest of this frame, then ... */ now_uframe = now; clock = now_uframe >> 3; } else frame = (frame + 1) % ehci->periodic_size; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -