ehci-sched.c 64.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * Copyright (c) 2001-2004 by David Brownell
 * Copyright (c) 2003 Michal Sojka, for high-speed iso transfers
4
 *
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* this file is part of ehci-hcd.c */

/*-------------------------------------------------------------------------*/

/*
 * EHCI scheduled transaction support:  interrupt, iso, split iso
 * These are called "periodic" transactions in the EHCI spec.
 *
 * Note that for interrupt transfers, the QH/QTD manipulation is shared
 * with the "asynchronous" transaction support (control/bulk transfers).
 * The only real difference is in how interrupt transfers are scheduled.
 *
 * For ISO, we make an "iso_stream" head to serve the same role as a QH.
 * It keeps track of every ITD (or SITD) that's linked, and holds enough
 * pre-calculated schedule data to make appending to the queue be quick.
 */

37
static int ehci_get_frame(struct usb_hcd *hcd);
L
Linus Torvalds 已提交
38 39 40 41 42 43 44

/*
 * periodic_next_shadow - return "next" pointer on shadow list
 * @periodic: host pointer to qh/itd/sitd
 * @tag: hardware tag for type of this record
 */
static union ehci_shadow *
45 46
periodic_next_shadow(struct ehci_hcd *ehci, union ehci_shadow *periodic,
		__hc32 tag)
L
Linus Torvalds 已提交
47
{
48
	switch (hc32_to_cpu(ehci, tag)) {
L
Linus Torvalds 已提交
49 50 51 52 53 54
	case Q_TYPE_QH:
		return &periodic->qh->qh_next;
	case Q_TYPE_FSTN:
		return &periodic->fstn->fstn_next;
	case Q_TYPE_ITD:
		return &periodic->itd->itd_next;
55
	/* case Q_TYPE_SITD: */
L
Linus Torvalds 已提交
56 57 58 59 60
	default:
		return &periodic->sitd->sitd_next;
	}
}

61 62 63 64 65 66 67 68 69 70 71 72 73 74
static __hc32 *
shadow_next_periodic(struct ehci_hcd *ehci, union ehci_shadow *periodic,
		__hc32 tag)
{
	switch (hc32_to_cpu(ehci, tag)) {
	/* our ehci_shadow.qh is actually software part */
	case Q_TYPE_QH:
		return &periodic->qh->hw->hw_next;
	/* others are hw parts */
	default:
		return periodic->hw_next;
	}
}

L
Linus Torvalds 已提交
75
/* caller must hold ehci->lock */
76
static void periodic_unlink(struct ehci_hcd *ehci, unsigned frame, void *ptr)
L
Linus Torvalds 已提交
77
{
78 79
	union ehci_shadow	*prev_p = &ehci->pshadow[frame];
	__hc32			*hw_p = &ehci->periodic[frame];
L
Linus Torvalds 已提交
80 81 82 83
	union ehci_shadow	here = *prev_p;

	/* find predecessor of "ptr"; hw and shadow lists are in sync */
	while (here.ptr && here.ptr != ptr) {
84 85
		prev_p = periodic_next_shadow(ehci, prev_p,
				Q_NEXT_TYPE(ehci, *hw_p));
86 87
		hw_p = shadow_next_periodic(ehci, &here,
				Q_NEXT_TYPE(ehci, *hw_p));
L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96
		here = *prev_p;
	}
	/* an interrupt entry (at list end) could have been shared */
	if (!here.ptr)
		return;

	/* update shadow and hardware lists ... the old "next" pointers
	 * from ptr may still be in use, the caller updates them.
	 */
97 98
	*prev_p = *periodic_next_shadow(ehci, &here,
			Q_NEXT_TYPE(ehci, *hw_p));
99 100 101 102 103 104 105

	if (!ehci->use_dummy_qh ||
	    *shadow_next_periodic(ehci, &here, Q_NEXT_TYPE(ehci, *hw_p))
			!= EHCI_LIST_END(ehci))
		*hw_p = *shadow_next_periodic(ehci, &here,
				Q_NEXT_TYPE(ehci, *hw_p));
	else
A
Alan Stern 已提交
106
		*hw_p = cpu_to_hc32(ehci, ehci->dummy->qh_dma);
L
Linus Torvalds 已提交
107 108
}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
/*-------------------------------------------------------------------------*/

/* Bandwidth and TT management */

/* Find the TT data structure for this device; create it if necessary */
static struct ehci_tt *find_tt(struct usb_device *udev)
{
	struct usb_tt		*utt = udev->tt;
	struct ehci_tt		*tt, **tt_index, **ptt;
	unsigned		port;
	bool			allocated_index = false;

	if (!utt)
		return NULL;		/* Not below a TT */

	/*
	 * Find/create our data structure.
	 * For hubs with a single TT, we get it directly.
	 * For hubs with multiple TTs, there's an extra level of pointers.
	 */
	tt_index = NULL;
	if (utt->multi) {
		tt_index = utt->hcpriv;
		if (!tt_index) {		/* Create the index array */
			tt_index = kzalloc(utt->hub->maxchild *
					sizeof(*tt_index), GFP_ATOMIC);
			if (!tt_index)
				return ERR_PTR(-ENOMEM);
			utt->hcpriv = tt_index;
			allocated_index = true;
		}
		port = udev->ttport - 1;
		ptt = &tt_index[port];
	} else {
		port = 0;
		ptt = (struct ehci_tt **) &utt->hcpriv;
	}

	tt = *ptt;
	if (!tt) {				/* Create the ehci_tt */
		struct ehci_hcd		*ehci =
				hcd_to_ehci(bus_to_hcd(udev->bus));

		tt = kzalloc(sizeof(*tt), GFP_ATOMIC);
		if (!tt) {
			if (allocated_index) {
				utt->hcpriv = NULL;
				kfree(tt_index);
			}
			return ERR_PTR(-ENOMEM);
		}
		list_add_tail(&tt->tt_list, &ehci->tt_list);
		INIT_LIST_HEAD(&tt->ps_list);
		tt->usb_tt = utt;
		tt->tt_port = port;
		*ptt = tt;
	}

	return tt;
}

/* Release the TT above udev, if it's not in use */
static void drop_tt(struct usb_device *udev)
{
	struct usb_tt		*utt = udev->tt;
	struct ehci_tt		*tt, **tt_index, **ptt;
	int			cnt, i;

	if (!utt || !utt->hcpriv)
		return;		/* Not below a TT, or never allocated */

	cnt = 0;
	if (utt->multi) {
		tt_index = utt->hcpriv;
		ptt = &tt_index[udev->ttport - 1];

		/* How many entries are left in tt_index? */
		for (i = 0; i < utt->hub->maxchild; ++i)
			cnt += !!tt_index[i];
	} else {
		tt_index = NULL;
		ptt = (struct ehci_tt **) &utt->hcpriv;
	}

	tt = *ptt;
	if (!tt || !list_empty(&tt->ps_list))
		return;		/* never allocated, or still in use */

	list_del(&tt->tt_list);
	*ptt = NULL;
	kfree(tt);
	if (cnt == 1) {
		utt->hcpriv = NULL;
		kfree(tt_index);
	}
}

206 207
static void bandwidth_dbg(struct ehci_hcd *ehci, int sign, char *type,
		struct ehci_per_sched *ps)
L
Linus Torvalds 已提交
208
{
209 210 211 212 213 214 215 216
	dev_dbg(&ps->udev->dev,
			"ep %02x: %s %s @ %u+%u (%u.%u+%u) [%u/%u us] mask %04x\n",
			ps->ep->desc.bEndpointAddress,
			(sign >= 0 ? "reserve" : "release"), type,
			(ps->bw_phase << 3) + ps->phase_uf, ps->bw_uperiod,
			ps->phase, ps->phase_uf, ps->period,
			ps->usecs, ps->c_usecs, ps->cs_mask);
}
L
Linus Torvalds 已提交
217

218 219 220 221 222 223 224
static void reserve_release_intr_bandwidth(struct ehci_hcd *ehci,
		struct ehci_qh *qh, int sign)
{
	unsigned		start_uf;
	unsigned		i, j, m;
	int			usecs = qh->ps.usecs;
	int			c_usecs = qh->ps.c_usecs;
225 226
	int			tt_usecs = qh->ps.tt_usecs;
	struct ehci_tt		*tt;
L
Linus Torvalds 已提交
227

228 229 230
	if (qh->ps.phase == NO_FRAME)	/* Bandwidth wasn't reserved */
		return;
	start_uf = qh->ps.bw_phase << 3;
L
Linus Torvalds 已提交
231

232 233 234 235 236
	bandwidth_dbg(ehci, sign, "intr", &qh->ps);

	if (sign < 0) {		/* Release bandwidth */
		usecs = -usecs;
		c_usecs = -c_usecs;
237
		tt_usecs = -tt_usecs;
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
	}

	/* Entire transaction (high speed) or start-split (full/low speed) */
	for (i = start_uf + qh->ps.phase_uf; i < EHCI_BANDWIDTH_SIZE;
			i += qh->ps.bw_uperiod)
		ehci->bandwidth[i] += usecs;

	/* Complete-split (full/low speed) */
	if (qh->ps.c_usecs) {
		/* NOTE: adjustments needed for FSTN */
		for (i = start_uf; i < EHCI_BANDWIDTH_SIZE;
				i += qh->ps.bw_uperiod) {
			for ((j = 2, m = 1 << (j+8)); j < 8; (++j, m <<= 1)) {
				if (qh->ps.cs_mask & m)
					ehci->bandwidth[i+j] += c_usecs;
			}
L
Linus Torvalds 已提交
254 255
		}
	}
256 257 258 259 260 261 262 263 264 265 266 267 268

	/* FS/LS bus bandwidth */
	if (tt_usecs) {
		tt = find_tt(qh->ps.udev);
		if (sign > 0)
			list_add_tail(&qh->ps.ps_list, &tt->ps_list);
		else
			list_del(&qh->ps.ps_list);

		for (i = start_uf >> 3; i < EHCI_BANDWIDTH_FRAMES;
				i += qh->ps.bw_period)
			tt->bandwidth[i] += tt_usecs;
	}
L
Linus Torvalds 已提交
269 270 271 272
}

/*-------------------------------------------------------------------------*/

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
static void compute_tt_budget(u8 budget_table[EHCI_BANDWIDTH_SIZE],
		struct ehci_tt *tt)
{
	struct ehci_per_sched	*ps;
	unsigned		uframe, uf, x;
	u8			*budget_line;

	if (!tt)
		return;
	memset(budget_table, 0, EHCI_BANDWIDTH_SIZE);

	/* Add up the contributions from all the endpoints using this TT */
	list_for_each_entry(ps, &tt->ps_list, ps_list) {
		for (uframe = ps->bw_phase << 3; uframe < EHCI_BANDWIDTH_SIZE;
				uframe += ps->bw_uperiod) {
			budget_line = &budget_table[uframe];
			x = ps->tt_usecs;

			/* propagate the time forward */
			for (uf = ps->phase_uf; uf < 8; ++uf) {
				x += budget_line[uf];

				/* Each microframe lasts 125 us */
				if (x <= 125) {
					budget_line[uf] = x;
					break;
				}
300 301
				budget_line[uf] = 125;
				x -= 125;
302 303 304 305 306 307 308
			}
		}
	}
}

static int __maybe_unused same_tt(struct usb_device *dev1,
		struct usb_device *dev2)
L
Linus Torvalds 已提交
309 310 311 312 313 314 315 316 317 318 319
{
	if (!dev1->tt || !dev2->tt)
		return 0;
	if (dev1->tt != dev2->tt)
		return 0;
	if (dev1->tt->multi)
		return dev1->ttport == dev2->ttport;
	else
		return 1;
}

320 321 322 323 324 325 326 327 328 329
#ifdef CONFIG_USB_EHCI_TT_NEWSCHED

/* Which uframe does the low/fullspeed transfer start in?
 *
 * The parameter is the mask of ssplits in "H-frame" terms
 * and this returns the transfer start uframe in "B-frame" terms,
 * which allows both to match, e.g. a ssplit in "H-frame" uframe 0
 * will cause a transfer in "B-frame" uframe 0.  "B-frames" lag
 * "H-frames" by 1 uframe.  See the EHCI spec sec 4.5 and figure 4.7.
 */
330
static inline unsigned char tt_start_uframe(struct ehci_hcd *ehci, __hc32 mask)
331
{
332
	unsigned char smask = hc32_to_cpu(ehci, mask) & QH_SMASK;
333

334 335 336 337 338 339 340 341 342
	if (!smask) {
		ehci_err(ehci, "invalid empty smask!\n");
		/* uframe 7 can't have bw so this will indicate failure */
		return 7;
	}
	return ffs(smask) - 1;
}

static const unsigned char
343
max_tt_usecs[] = { 125, 125, 125, 125, 125, 125, 30, 0 };
344 345 346 347 348

/* carryover low/fullspeed bandwidth that crosses uframe boundries */
static inline void carryover_tt_bandwidth(unsigned short tt_usecs[8])
{
	int i;
349

350
	for (i = 0; i < 7; i++) {
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
		if (max_tt_usecs[i] < tt_usecs[i]) {
			tt_usecs[i+1] += tt_usecs[i] - max_tt_usecs[i];
			tt_usecs[i] = max_tt_usecs[i];
		}
	}
}

/*
 * Return true if the device's tt's downstream bus is available for a
 * periodic transfer of the specified length (usecs), starting at the
 * specified frame/uframe.  Note that (as summarized in section 11.19
 * of the usb 2.0 spec) TTs can buffer multiple transactions for each
 * uframe.
 *
 * The uframe parameter is when the fullspeed/lowspeed transfer
 * should be executed in "B-frame" terms, which is the same as the
 * highspeed ssplit's uframe (which is in "H-frame" terms).  For example
 * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0.
 * See the EHCI spec sec 4.5 and fig 4.7.
 *
 * This checks if the full/lowspeed bus, at the specified starting uframe,
 * has the specified bandwidth available, according to rules listed
 * in USB 2.0 spec section 11.18.1 fig 11-60.
 *
 * This does not check if the transfer would exceed the max ssplit
 * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4,
 * since proper scheduling limits ssplits to less than 16 per uframe.
 */
379
static int tt_available(
380
	struct ehci_hcd		*ehci,
381 382
	struct ehci_per_sched	*ps,
	struct ehci_tt		*tt,
383
	unsigned		frame,
384
	unsigned		uframe
385 386
)
{
387 388 389
	unsigned		period = ps->bw_period;
	unsigned		usecs = ps->tt_usecs;

390 391 392
	if ((period == 0) || (uframe >= 7))	/* error */
		return 0;

393 394 395 396
	for (frame &= period - 1; frame < EHCI_BANDWIDTH_FRAMES;
			frame += period) {
		unsigned	i, uf;
		unsigned short	tt_usecs[8];
397

398 399 400 401 402 403
		if (tt->bandwidth[frame] + usecs > 900)
			return 0;

		uf = frame << 3;
		for (i = 0; i < 8; (++i, ++uf))
			tt_usecs[i] = ehci->tt_budget[uf];
404

405
		if (max_tt_usecs[uframe] <= tt_usecs[uframe])
406 407 408 409 410 411 412
			return 0;

		/* special case for isoc transfers larger than 125us:
		 * the first and each subsequent fully used uframe
		 * must be empty, so as to not illegally delay
		 * already scheduled transactions
		 */
413
		if (usecs > 125) {
414
			int ufs = (usecs / 125);
415

416
			for (i = uframe; i < (uframe + ufs) && i < 8; i++)
417
				if (tt_usecs[i] > 0)
418 419 420 421 422 423 424 425
					return 0;
		}

		tt_usecs[uframe] += usecs;

		carryover_tt_bandwidth(tt_usecs);

		/* fail if the carryover pushed bw past the last uframe's limit */
426
		if (max_tt_usecs[7] < tt_usecs[7])
427 428 429 430 431 432 433 434
			return 0;
	}

	return 1;
}

#else

L
Linus Torvalds 已提交
435 436 437 438
/* return true iff the device's transaction translator is available
 * for a periodic transfer starting at the specified frame, using
 * all the uframes in the mask.
 */
439
static int tt_no_collision(
L
Linus Torvalds 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
	struct ehci_hcd		*ehci,
	unsigned		period,
	struct usb_device	*dev,
	unsigned		frame,
	u32			uf_mask
)
{
	if (period == 0)	/* error */
		return 0;

	/* note bandwidth wastage:  split never follows csplit
	 * (different dev or endpoint) until the next uframe.
	 * calling convention doesn't make that distinction.
	 */
	for (; frame < ehci->periodic_size; frame += period) {
		union ehci_shadow	here;
456
		__hc32			type;
457
		struct ehci_qh_hw	*hw;
L
Linus Torvalds 已提交
458

459 460
		here = ehci->pshadow[frame];
		type = Q_NEXT_TYPE(ehci, ehci->periodic[frame]);
L
Linus Torvalds 已提交
461
		while (here.ptr) {
462
			switch (hc32_to_cpu(ehci, type)) {
L
Linus Torvalds 已提交
463
			case Q_TYPE_ITD:
464
				type = Q_NEXT_TYPE(ehci, here.itd->hw_next);
L
Linus Torvalds 已提交
465 466 467
				here = here.itd->itd_next;
				continue;
			case Q_TYPE_QH:
468
				hw = here.qh->hw;
469
				if (same_tt(dev, here.qh->ps.udev)) {
L
Linus Torvalds 已提交
470 471
					u32		mask;

472
					mask = hc32_to_cpu(ehci,
473
							hw->hw_info2);
L
Linus Torvalds 已提交
474 475 476 477 478
					/* "knows" no gap is needed */
					mask |= mask >> 8;
					if (mask & uf_mask)
						break;
				}
479
				type = Q_NEXT_TYPE(ehci, hw->hw_next);
L
Linus Torvalds 已提交
480 481 482
				here = here.qh->qh_next;
				continue;
			case Q_TYPE_SITD:
483
				if (same_tt(dev, here.sitd->urb->dev)) {
L
Linus Torvalds 已提交
484 485
					u16		mask;

486
					mask = hc32_to_cpu(ehci, here.sitd
L
Linus Torvalds 已提交
487 488 489 490 491 492
								->hw_uframe);
					/* FIXME assumes no gap for IN! */
					mask |= mask >> 8;
					if (mask & uf_mask)
						break;
				}
493
				type = Q_NEXT_TYPE(ehci, here.sitd->hw_next);
L
Linus Torvalds 已提交
494 495
				here = here.sitd->sitd_next;
				continue;
496
			/* case Q_TYPE_FSTN: */
L
Linus Torvalds 已提交
497
			default:
498
				ehci_dbg(ehci,
L
Linus Torvalds 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511
					"periodic frame %d bogus type %d\n",
					frame, type);
			}

			/* collision or error */
			return 0;
		}
	}

	/* no collision */
	return 1;
}

512 513
#endif /* CONFIG_USB_EHCI_TT_NEWSCHED */

L
Linus Torvalds 已提交
514 515
/*-------------------------------------------------------------------------*/

A
Alan Stern 已提交
516
static void enable_periodic(struct ehci_hcd *ehci)
L
Linus Torvalds 已提交
517
{
518
	if (ehci->periodic_count++)
A
Alan Stern 已提交
519
		return;
520

521 522
	/* Stop waiting to turn off the periodic schedule */
	ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_DISABLE_PERIODIC);
L
Linus Torvalds 已提交
523

524 525
	/* Don't start the schedule until PSS is 0 */
	ehci_poll_PSS(ehci);
526
	turn_on_io_watchdog(ehci);
L
Linus Torvalds 已提交
527 528
}

A
Alan Stern 已提交
529
static void disable_periodic(struct ehci_hcd *ehci)
L
Linus Torvalds 已提交
530
{
531
	if (--ehci->periodic_count)
A
Alan Stern 已提交
532
		return;
533

534 535
	/* Don't turn off the schedule until PSS is 1 */
	ehci_poll_PSS(ehci);
L
Linus Torvalds 已提交
536 537 538 539 540 541 542 543 544 545
}

/*-------------------------------------------------------------------------*/

/* periodic schedule slots have iso tds (normal or split) first, then a
 * sparse tree for active interrupt transfers.
 *
 * this just links in a qh; caller guarantees uframe masks are set right.
 * no FSTN support (yet; ehci 0.96+)
 */
A
Alan Stern 已提交
546
static void qh_link_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
L
Linus Torvalds 已提交
547 548
{
	unsigned	i;
549
	unsigned	period = qh->ps.period;
L
Linus Torvalds 已提交
550

551
	dev_dbg(&qh->ps.udev->dev,
L
Linus Torvalds 已提交
552
		"link qh%d-%04x/%p start %d [%d/%d us]\n",
553 554
		period, hc32_to_cpup(ehci, &qh->hw->hw_info2)
			& (QH_CMASK | QH_SMASK),
555
		qh, qh->ps.phase, qh->ps.usecs, qh->ps.c_usecs);
L
Linus Torvalds 已提交
556 557 558 559 560

	/* high bandwidth, or otherwise every microframe */
	if (period == 0)
		period = 1;

561
	for (i = qh->ps.phase; i < ehci->periodic_size; i += period) {
562 563
		union ehci_shadow	*prev = &ehci->pshadow[i];
		__hc32			*hw_p = &ehci->periodic[i];
L
Linus Torvalds 已提交
564
		union ehci_shadow	here = *prev;
565
		__hc32			type = 0;
L
Linus Torvalds 已提交
566 567 568

		/* skip the iso nodes at list head */
		while (here.ptr) {
569 570
			type = Q_NEXT_TYPE(ehci, *hw_p);
			if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
L
Linus Torvalds 已提交
571
				break;
572
			prev = periodic_next_shadow(ehci, prev, type);
573
			hw_p = shadow_next_periodic(ehci, &here, type);
L
Linus Torvalds 已提交
574 575 576 577 578 579 580
			here = *prev;
		}

		/* sorting each branch by period (slow-->fast)
		 * enables sharing interior tree nodes
		 */
		while (here.ptr && qh != here.qh) {
581
			if (qh->ps.period > here.qh->ps.period)
L
Linus Torvalds 已提交
582 583
				break;
			prev = &here.qh->qh_next;
584
			hw_p = &here.qh->hw->hw_next;
L
Linus Torvalds 已提交
585 586 587 588 589 590
			here = *prev;
		}
		/* link in this qh, unless some earlier pass did that */
		if (qh != here.qh) {
			qh->qh_next = here;
			if (here.qh)
591
				qh->hw->hw_next = *hw_p;
592
			wmb();
L
Linus Torvalds 已提交
593
			prev->qh = qh;
594
			*hw_p = QH_NEXT(ehci, qh->qh_dma);
L
Linus Torvalds 已提交
595 596 597
		}
	}
	qh->qh_state = QH_STATE_LINKED;
598
	qh->xacterrs = 0;
599
	qh->unlink_reason = 0;
L
Linus Torvalds 已提交
600

601
	/* update per-qh bandwidth for debugfs */
602 603
	ehci_to_hcd(ehci)->self.bandwidth_allocated += qh->ps.bw_period
		? ((qh->ps.usecs + qh->ps.c_usecs) / qh->ps.bw_period)
604
		: (qh->ps.usecs * 8);
L
Linus Torvalds 已提交
605

606 607
	list_add(&qh->intr_node, &ehci->intr_qh_list);

L
Linus Torvalds 已提交
608
	/* maybe enable periodic schedule processing */
609
	++ehci->intr_count;
A
Alan Stern 已提交
610
	enable_periodic(ehci);
L
Linus Torvalds 已提交
611 612
}

A
Alan Stern 已提交
613
static void qh_unlink_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
L
Linus Torvalds 已提交
614 615 616 617
{
	unsigned	i;
	unsigned	period;

618 619 620 621 622 623 624 625 626 627 628 629 630 631
	/*
	 * If qh is for a low/full-speed device, simply unlinking it
	 * could interfere with an ongoing split transaction.  To unlink
	 * it safely would require setting the QH_INACTIVATE bit and
	 * waiting at least one frame, as described in EHCI 4.12.2.5.
	 *
	 * We won't bother with any of this.  Instead, we assume that the
	 * only reason for unlinking an interrupt QH while the current URB
	 * is still active is to dequeue all the URBs (flush the whole
	 * endpoint queue).
	 *
	 * If rebalancing the periodic schedule is ever implemented, this
	 * approach will no longer be valid.
	 */
L
Linus Torvalds 已提交
632 633

	/* high bandwidth, or otherwise part of every microframe */
634
	period = qh->ps.period ? : 1;
L
Linus Torvalds 已提交
635

636
	for (i = qh->ps.phase; i < ehci->periodic_size; i += period)
637
		periodic_unlink(ehci, i, qh);
L
Linus Torvalds 已提交
638

639
	/* update per-qh bandwidth for debugfs */
640 641
	ehci_to_hcd(ehci)->self.bandwidth_allocated -= qh->ps.bw_period
		? ((qh->ps.usecs + qh->ps.c_usecs) / qh->ps.bw_period)
642
		: (qh->ps.usecs * 8);
L
Linus Torvalds 已提交
643

644
	dev_dbg(&qh->ps.udev->dev,
L
Linus Torvalds 已提交
645
		"unlink qh%d-%04x/%p start %d [%d/%d us]\n",
646
		qh->ps.period,
647
		hc32_to_cpup(ehci, &qh->hw->hw_info2) & (QH_CMASK | QH_SMASK),
648
		qh, qh->ps.phase, qh->ps.usecs, qh->ps.c_usecs);
L
Linus Torvalds 已提交
649 650 651 652

	/* qh->qh_next still "live" to HC */
	qh->qh_state = QH_STATE_UNLINK;
	qh->qh_next.ptr = NULL;
653 654 655 656 657

	if (ehci->qh_scan_next == qh)
		ehci->qh_scan_next = list_entry(qh->intr_node.next,
				struct ehci_qh, intr_node);
	list_del(&qh->intr_node);
L
Linus Torvalds 已提交
658 659
}

660 661 662 663 664 665 666 667 668 669 670 671 672 673
static void cancel_unlink_wait_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
{
	if (qh->qh_state != QH_STATE_LINKED ||
			list_empty(&qh->unlink_node))
		return;

	list_del_init(&qh->unlink_node);

	/*
	 * TODO: disable the event of EHCI_HRTIMER_START_UNLINK_INTR for
	 * avoiding unnecessary CPU wakeup
	 */
}

674
static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
L
Linus Torvalds 已提交
675
{
676 677
	/* If the QH isn't linked then there's nothing we can do. */
	if (qh->qh_state != QH_STATE_LINKED)
678
		return;
L
Linus Torvalds 已提交
679

680 681 682
	/* if the qh is waiting for unlink, cancel it now */
	cancel_unlink_wait_intr(ehci, qh);

683
	qh_unlink_periodic(ehci, qh);
L
Linus Torvalds 已提交
684

685 686 687 688 689 690 691
	/* Make sure the unlinks are visible before starting the timer */
	wmb();

	/*
	 * The EHCI spec doesn't say how long it takes the controller to
	 * stop accessing an unlinked interrupt QH.  The timer delay is
	 * 9 uframes; presumably that will be long enough.
L
Linus Torvalds 已提交
692
	 */
693 694 695
	qh->unlink_cycle = ehci->intr_unlink_cycle;

	/* New entries go at the end of the intr_unlink list */
696
	list_add_tail(&qh->unlink_node, &ehci->intr_unlink);
697 698 699 700 701

	if (ehci->intr_unlinking)
		;	/* Avoid recursive calls */
	else if (ehci->rh_state < EHCI_RH_RUNNING)
		ehci_handle_intr_unlinks(ehci);
702
	else if (ehci->intr_unlink.next == &qh->unlink_node) {
703 704 705 706 707
		ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true);
		++ehci->intr_unlink_cycle;
	}
}

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
/*
 * It is common only one intr URB is scheduled on one qh, and
 * given complete() is run in tasklet context, introduce a bit
 * delay to avoid unlink qh too early.
 */
static void start_unlink_intr_wait(struct ehci_hcd *ehci,
				   struct ehci_qh *qh)
{
	qh->unlink_cycle = ehci->intr_unlink_wait_cycle;

	/* New entries go at the end of the intr_unlink_wait list */
	list_add_tail(&qh->unlink_node, &ehci->intr_unlink_wait);

	if (ehci->rh_state < EHCI_RH_RUNNING)
		ehci_handle_start_intr_unlinks(ehci);
	else if (ehci->intr_unlink_wait.next == &qh->unlink_node) {
		ehci_enable_event(ehci, EHCI_HRTIMER_START_UNLINK_INTR, true);
		++ehci->intr_unlink_wait_cycle;
	}
}

729 730 731 732
static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
{
	struct ehci_qh_hw	*hw = qh->hw;
	int			rc;
L
Linus Torvalds 已提交
733 734

	qh->qh_state = QH_STATE_IDLE;
735
	hw->hw_next = EHCI_LIST_END(ehci);
736

737 738
	if (!list_empty(&qh->qtd_list))
		qh_completions(ehci, qh);
739 740

	/* reschedule QH iff another request is queued */
741
	if (!list_empty(&qh->qtd_list) && ehci->rh_state == EHCI_RH_RUNNING) {
742
		rc = qh_schedule(ehci, qh);
743 744 745 746
		if (rc == 0) {
			qh_refresh(ehci, qh);
			qh_link_periodic(ehci, qh);
		}
747 748 749 750 751 752 753

		/* An error here likely indicates handshake failure
		 * or no space left in the schedule.  Neither fault
		 * should happen often ...
		 *
		 * FIXME kill the now-dysfunctional queued urbs
		 */
754
		else {
755 756
			ehci_err(ehci, "can't reschedule qh %p, err %d\n",
					qh, rc);
757
		}
758
	}
759 760

	/* maybe turn off periodic schedule */
761
	--ehci->intr_count;
762
	disable_periodic(ehci);
L
Linus Torvalds 已提交
763 764 765 766
}

/*-------------------------------------------------------------------------*/

767
static int check_period(
768
	struct ehci_hcd *ehci,
L
Linus Torvalds 已提交
769 770
	unsigned	frame,
	unsigned	uframe,
771
	unsigned	uperiod,
L
Linus Torvalds 已提交
772 773 774 775 776 777 778 779
	unsigned	usecs
) {
	/* complete split running into next frame?
	 * given FSTN support, we could sometimes check...
	 */
	if (uframe >= 8)
		return 0;

780 781
	/* convert "usecs we need" to "max already claimed" */
	usecs = ehci->uframe_periodic_max - usecs;
L
Linus Torvalds 已提交
782

783 784 785 786
	for (uframe += frame << 3; uframe < EHCI_BANDWIDTH_SIZE;
			uframe += uperiod) {
		if (ehci->bandwidth[uframe] > usecs)
			return 0;
L
Linus Torvalds 已提交
787 788
	}

789
	/* success! */
L
Linus Torvalds 已提交
790 791 792
	return 1;
}

793
static int check_intr_schedule(
794
	struct ehci_hcd		*ehci,
L
Linus Torvalds 已提交
795 796
	unsigned		frame,
	unsigned		uframe,
797
	struct ehci_qh		*qh,
798
	unsigned		*c_maskp,
799
	struct ehci_tt		*tt
L
Linus Torvalds 已提交
800 801
)
{
802
	int		retval = -ENOSPC;
803
	u8		mask = 0;
L
Linus Torvalds 已提交
804

805
	if (qh->ps.c_usecs && uframe >= 6)	/* FSTN territory? */
L
Linus Torvalds 已提交
806 807
		goto done;

808
	if (!check_period(ehci, frame, uframe, qh->ps.bw_uperiod, qh->ps.usecs))
L
Linus Torvalds 已提交
809
		goto done;
810
	if (!qh->ps.c_usecs) {
L
Linus Torvalds 已提交
811 812 813 814 815
		retval = 0;
		*c_maskp = 0;
		goto done;
	}

816
#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
817
	if (tt_available(ehci, &qh->ps, tt, frame, uframe)) {
818 819 820
		unsigned i;

		/* TODO : this may need FSTN for SSPLIT in uframe 5. */
821
		for (i = uframe+2; i < 8 && i <= uframe+4; i++)
822
			if (!check_period(ehci, frame, i,
823
					qh->ps.bw_uperiod, qh->ps.c_usecs))
824 825 826 827 828 829
				goto done;
			else
				mask |= 1 << i;

		retval = 0;

830
		*c_maskp = mask;
831 832
	}
#else
L
Linus Torvalds 已提交
833 834 835
	/* Make sure this tt's buffer is also available for CSPLITs.
	 * We pessimize a bit; probably the typical full speed case
	 * doesn't need the second CSPLIT.
836
	 *
L
Linus Torvalds 已提交
837 838 839 840
	 * NOTE:  both SPLIT and CSPLIT could be checked in just
	 * one smart pass...
	 */
	mask = 0x03 << (uframe + qh->gap_uf);
841
	*c_maskp = mask;
L
Linus Torvalds 已提交
842 843

	mask |= 1 << uframe;
844
	if (tt_no_collision(ehci, qh->ps.bw_period, qh->ps.udev, frame, mask)) {
845
		if (!check_period(ehci, frame, uframe + qh->gap_uf + 1,
846
				qh->ps.bw_uperiod, qh->ps.c_usecs))
L
Linus Torvalds 已提交
847
			goto done;
848
		if (!check_period(ehci, frame, uframe + qh->gap_uf,
849
				qh->ps.bw_uperiod, qh->ps.c_usecs))
L
Linus Torvalds 已提交
850 851 852
			goto done;
		retval = 0;
	}
853
#endif
L
Linus Torvalds 已提交
854 855 856 857 858 859 860
done:
	return retval;
}

/* "first fit" scheduling policy used the first time through,
 * or when the previous schedule slot can't be re-used.
 */
861
static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
L
Linus Torvalds 已提交
862
{
863
	int		status = 0;
L
Linus Torvalds 已提交
864
	unsigned	uframe;
865
	unsigned	c_mask;
866
	struct ehci_qh_hw	*hw = qh->hw;
867
	struct ehci_tt		*tt;
L
Linus Torvalds 已提交
868

869
	hw->hw_next = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
870 871

	/* reuse the previous schedule slots, if we can */
872 873 874
	if (qh->ps.phase != NO_FRAME) {
		ehci_dbg(ehci, "reused qh %p schedule\n", qh);
		return 0;
L
Linus Torvalds 已提交
875 876
	}

877 878
	uframe = 0;
	c_mask = 0;
879 880 881 882 883 884
	tt = find_tt(qh->ps.udev);
	if (IS_ERR(tt)) {
		status = PTR_ERR(tt);
		goto done;
	}
	compute_tt_budget(ehci->tt_budget, tt);
885

L
Linus Torvalds 已提交
886 887 888
	/* else scan the schedule to find a group of slots such that all
	 * uframes have enough periodic bandwidth available.
	 */
889 890 891 892 893
	/* "normal" case, uframing flexible except with splits */
	if (qh->ps.bw_period) {
		int		i;
		unsigned	frame;

894
		for (i = qh->ps.bw_period; i > 0; --i) {
895 896 897
			frame = ++ehci->random_frame & (qh->ps.bw_period - 1);
			for (uframe = 0; uframe < 8; uframe++) {
				status = check_intr_schedule(ehci,
898
						frame, uframe, qh, &c_mask, tt);
899
				if (status == 0)
900
					goto got_it;
901
			}
L
Linus Torvalds 已提交
902 903
		}

904 905
	/* qh->ps.bw_period == 0 means every uframe */
	} else {
906
		status = check_intr_schedule(ehci, 0, 0, qh, &c_mask, tt);
907 908 909
	}
	if (status)
		goto done;
910 911

 got_it:
912 913 914 915 916 917 918 919 920 921 922 923
	qh->ps.phase = (qh->ps.period ? ehci->random_frame &
			(qh->ps.period - 1) : 0);
	qh->ps.bw_phase = qh->ps.phase & (qh->ps.bw_period - 1);
	qh->ps.phase_uf = uframe;
	qh->ps.cs_mask = qh->ps.period ?
			(c_mask << 8) | (1 << uframe) :
			QH_SMASK;

	/* reset S-frame and (maybe) C-frame masks */
	hw->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
	hw->hw_info2 |= cpu_to_hc32(ehci, qh->ps.cs_mask);
	reserve_release_intr_bandwidth(ehci, qh, 1);
L
Linus Torvalds 已提交
924 925 926 927 928

done:
	return status;
}

929
static int intr_submit(
L
Linus Torvalds 已提交
930 931 932
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	struct list_head	*qtd_list,
A
Al Viro 已提交
933
	gfp_t			mem_flags
L
Linus Torvalds 已提交
934 935 936 937
) {
	unsigned		epnum;
	unsigned long		flags;
	struct ehci_qh		*qh;
938
	int			status;
L
Linus Torvalds 已提交
939 940 941
	struct list_head	empty;

	/* get endpoint and transfer/schedule data */
942
	epnum = urb->ep->desc.bEndpointAddress;
L
Linus Torvalds 已提交
943

944
	spin_lock_irqsave(&ehci->lock, flags);
L
Linus Torvalds 已提交
945

946
	if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
947
		status = -ESHUTDOWN;
948
		goto done_not_linked;
949
	}
950 951 952
	status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
	if (unlikely(status))
		goto done_not_linked;
953

L
Linus Torvalds 已提交
954
	/* get qh and force any scheduling errors */
955
	INIT_LIST_HEAD(&empty);
956
	qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv);
L
Linus Torvalds 已提交
957 958 959 960 961
	if (qh == NULL) {
		status = -ENOMEM;
		goto done;
	}
	if (qh->qh_state == QH_STATE_IDLE) {
962 963
		status = qh_schedule(ehci, qh);
		if (status)
L
Linus Torvalds 已提交
964 965 966 967
			goto done;
	}

	/* then queue the urb's tds to the qh */
968
	qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
969
	BUG_ON(qh == NULL);
L
Linus Torvalds 已提交
970

971 972 973 974
	/* stuff into the periodic schedule */
	if (qh->qh_state == QH_STATE_IDLE) {
		qh_refresh(ehci, qh);
		qh_link_periodic(ehci, qh);
975 976 977
	} else {
		/* cancel unlink wait for the qh */
		cancel_unlink_wait_intr(ehci, qh);
978 979
	}

L
Linus Torvalds 已提交
980 981 982 983
	/* ... update usbfs periodic stats */
	ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;

done:
984 985 986
	if (unlikely(status))
		usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
done_not_linked:
987
	spin_unlock_irqrestore(&ehci->lock, flags);
L
Linus Torvalds 已提交
988
	if (status)
989
		qtd_list_free(ehci, urb, qtd_list);
L
Linus Torvalds 已提交
990 991 992 993

	return status;
}

994 995 996 997 998 999
static void scan_intr(struct ehci_hcd *ehci)
{
	struct ehci_qh		*qh;

	list_for_each_entry_safe(qh, ehci->qh_scan_next, &ehci->intr_qh_list,
			intr_node) {
1000

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
		/* clean any finished work for this qh */
		if (!list_empty(&qh->qtd_list)) {
			int temp;

			/*
			 * Unlinks could happen here; completion reporting
			 * drops the lock.  That's why ehci->qh_scan_next
			 * always holds the next qh to scan; if the next qh
			 * gets unlinked then ehci->qh_scan_next is adjusted
			 * in qh_unlink_periodic().
			 */
			temp = qh_completions(ehci, qh);
1013
			if (unlikely(temp))
1014
				start_unlink_intr(ehci, qh);
1015 1016 1017
			else if (unlikely(list_empty(&qh->qtd_list) &&
					qh->qh_state == QH_STATE_LINKED))
				start_unlink_intr_wait(ehci, qh);
1018 1019 1020 1021
		}
	}
}

L
Linus Torvalds 已提交
1022 1023 1024 1025 1026
/*-------------------------------------------------------------------------*/

/* ehci_iso_stream ops work with both ITD and SITD */

static struct ehci_iso_stream *
1027
iso_stream_alloc(gfp_t mem_flags)
L
Linus Torvalds 已提交
1028 1029 1030
{
	struct ehci_iso_stream *stream;

1031
	stream = kzalloc(sizeof(*stream), mem_flags);
1032
	if (likely(stream != NULL)) {
L
Linus Torvalds 已提交
1033 1034
		INIT_LIST_HEAD(&stream->td_list);
		INIT_LIST_HEAD(&stream->free_list);
1035
		stream->next_uframe = NO_FRAME;
1036
		stream->ps.phase = NO_FRAME;
L
Linus Torvalds 已提交
1037 1038 1039 1040 1041
	}
	return stream;
}

static void
1042
iso_stream_init(
L
Linus Torvalds 已提交
1043 1044
	struct ehci_hcd		*ehci,
	struct ehci_iso_stream	*stream,
1045
	struct urb		*urb
L
Linus Torvalds 已提交
1046 1047
)
{
1048
	static const u8 smask_out[] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
L
Linus Torvalds 已提交
1049

1050
	struct usb_device	*dev = urb->dev;
L
Linus Torvalds 已提交
1051 1052 1053
	u32			buf1;
	unsigned		epnum, maxp;
	int			is_input;
1054
	unsigned		tmp;
L
Linus Torvalds 已提交
1055 1056 1057 1058 1059

	/*
	 * this might be a "high bandwidth" highspeed endpoint,
	 * as encoded in the ep descriptor's wMaxPacket field
	 */
1060 1061 1062
	epnum = usb_pipeendpoint(urb->pipe);
	is_input = usb_pipein(urb->pipe) ? USB_DIR_IN : 0;
	maxp = usb_endpoint_maxp(&urb->ep->desc);
L
Linus Torvalds 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
	if (is_input) {
		buf1 = (1 << 11);
	} else {
		buf1 = 0;
	}

	/* knows about ITD vs SITD */
	if (dev->speed == USB_SPEED_HIGH) {
		unsigned multi = hb_mult(maxp);

		stream->highspeed = 1;

		maxp = max_packet(maxp);
		buf1 |= maxp;
		maxp *= multi;

1079 1080 1081
		stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
		stream->buf1 = cpu_to_hc32(ehci, buf1);
		stream->buf2 = cpu_to_hc32(ehci, multi);
L
Linus Torvalds 已提交
1082 1083 1084 1085

		/* usbfs wants to report the average usecs per frame tied up
		 * when transfers on this endpoint are scheduled ...
		 */
1086 1087
		stream->ps.usecs = HS_USECS_ISO(maxp);

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
		/* period for bandwidth allocation */
		tmp = min_t(unsigned, EHCI_BANDWIDTH_SIZE,
				1 << (urb->ep->desc.bInterval - 1));

		/* Allow urb->interval to override */
		stream->ps.bw_uperiod = min_t(unsigned, tmp, urb->interval);

		stream->uperiod = urb->interval;
		stream->ps.period = urb->interval >> 3;
		stream->bandwidth = stream->ps.usecs * 8 /
				stream->ps.bw_uperiod;
L
Linus Torvalds 已提交
1099 1100 1101

	} else {
		u32		addr;
D
david-b@pacbell.net 已提交
1102
		int		think_time;
1103
		int		hs_transfers;
L
Linus Torvalds 已提交
1104 1105 1106 1107 1108 1109 1110 1111

		addr = dev->ttport << 24;
		if (!ehci_is_TDI(ehci)
				|| (dev->tt->hub !=
					ehci_to_hcd(ehci)->self.root_hub))
			addr |= dev->tt->hub->devnum << 16;
		addr |= epnum << 8;
		addr |= dev->devnum;
1112
		stream->ps.usecs = HS_USECS_ISO(maxp);
D
david-b@pacbell.net 已提交
1113
		think_time = dev->tt ? dev->tt->think_time : 0;
1114
		stream->ps.tt_usecs = NS_TO_US(think_time + usb_calc_bus_time(
D
david-b@pacbell.net 已提交
1115
				dev->speed, is_input, 1, maxp));
1116
		hs_transfers = max(1u, (maxp + 187) / 188);
L
Linus Torvalds 已提交
1117 1118 1119 1120
		if (is_input) {
			u32	tmp;

			addr |= 1 << 31;
1121 1122
			stream->ps.c_usecs = stream->ps.usecs;
			stream->ps.usecs = HS_USECS_ISO(1);
1123
			stream->ps.cs_mask = 1;
L
Linus Torvalds 已提交
1124

1125 1126
			/* c-mask as specified in USB 2.0 11.18.4 3.c */
			tmp = (1 << (hs_transfers + 2)) - 1;
1127
			stream->ps.cs_mask |= tmp << (8 + 2);
L
Linus Torvalds 已提交
1128
		} else
1129
			stream->ps.cs_mask = smask_out[hs_transfers - 1];
1130

1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
		/* period for bandwidth allocation */
		tmp = min_t(unsigned, EHCI_BANDWIDTH_FRAMES,
				1 << (urb->ep->desc.bInterval - 1));

		/* Allow urb->interval to override */
		stream->ps.bw_period = min_t(unsigned, tmp, urb->interval);
		stream->ps.bw_uperiod = stream->ps.bw_period << 3;

		stream->ps.period = urb->interval;
		stream->uperiod = urb->interval << 3;
1141
		stream->bandwidth = (stream->ps.usecs + stream->ps.c_usecs) /
1142
				stream->ps.bw_period;
L
Linus Torvalds 已提交
1143

1144
		/* stream->splits gets created from cs_mask later */
1145
		stream->address = cpu_to_hc32(ehci, addr);
L
Linus Torvalds 已提交
1146 1147
	}

1148 1149
	stream->ps.udev = dev;
	stream->ps.ep = urb->ep;
L
Linus Torvalds 已提交
1150 1151 1152 1153 1154 1155

	stream->bEndpointAddress = is_input | epnum;
	stream->maxp = maxp;
}

static struct ehci_iso_stream *
1156
iso_stream_find(struct ehci_hcd *ehci, struct urb *urb)
L
Linus Torvalds 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
{
	unsigned		epnum;
	struct ehci_iso_stream	*stream;
	struct usb_host_endpoint *ep;
	unsigned long		flags;

	epnum = usb_pipeendpoint (urb->pipe);
	if (usb_pipein(urb->pipe))
		ep = urb->dev->ep_in[epnum];
	else
		ep = urb->dev->ep_out[epnum];

1169
	spin_lock_irqsave(&ehci->lock, flags);
L
Linus Torvalds 已提交
1170 1171
	stream = ep->hcpriv;

1172
	if (unlikely(stream == NULL)) {
L
Linus Torvalds 已提交
1173
		stream = iso_stream_alloc(GFP_ATOMIC);
1174
		if (likely(stream != NULL)) {
L
Linus Torvalds 已提交
1175
			ep->hcpriv = stream;
1176
			iso_stream_init(ehci, stream, urb);
L
Linus Torvalds 已提交
1177 1178
		}

1179
	/* if dev->ep [epnum] is a QH, hw is set */
1180 1181
	} else if (unlikely(stream->hw != NULL)) {
		ehci_dbg(ehci, "dev %s ep%d%s, not iso??\n",
L
Linus Torvalds 已提交
1182 1183 1184 1185 1186
			urb->dev->devpath, epnum,
			usb_pipein(urb->pipe) ? "in" : "out");
		stream = NULL;
	}

1187
	spin_unlock_irqrestore(&ehci->lock, flags);
L
Linus Torvalds 已提交
1188 1189 1190 1191 1192 1193 1194 1195
	return stream;
}

/*-------------------------------------------------------------------------*/

/* ehci_iso_sched ops can be ITD-only or SITD-only */

static struct ehci_iso_sched *
1196
iso_sched_alloc(unsigned packets, gfp_t mem_flags)
L
Linus Torvalds 已提交
1197 1198
{
	struct ehci_iso_sched	*iso_sched;
1199
	int			size = sizeof(*iso_sched);
L
Linus Torvalds 已提交
1200

1201
	size += packets * sizeof(struct ehci_iso_packet);
1202
	iso_sched = kzalloc(size, mem_flags);
1203 1204 1205
	if (likely(iso_sched != NULL))
		INIT_LIST_HEAD(&iso_sched->td_list);

L
Linus Torvalds 已提交
1206 1207 1208 1209
	return iso_sched;
}

static inline void
1210 1211
itd_sched_init(
	struct ehci_hcd		*ehci,
L
Linus Torvalds 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220
	struct ehci_iso_sched	*iso_sched,
	struct ehci_iso_stream	*stream,
	struct urb		*urb
)
{
	unsigned	i;
	dma_addr_t	dma = urb->transfer_dma;

	/* how many uframes are needed for these transfers */
1221
	iso_sched->span = urb->number_of_packets * stream->uperiod;
L
Linus Torvalds 已提交
1222 1223 1224 1225 1226

	/* figure out per-uframe itd fields that we'll need later
	 * when we fit new itds into the schedule.
	 */
	for (i = 0; i < urb->number_of_packets; i++) {
1227
		struct ehci_iso_packet	*uframe = &iso_sched->packet[i];
L
Linus Torvalds 已提交
1228 1229 1230 1231
		unsigned		length;
		dma_addr_t		buf;
		u32			trans;

1232 1233
		length = urb->iso_frame_desc[i].length;
		buf = dma + urb->iso_frame_desc[i].offset;
L
Linus Torvalds 已提交
1234 1235 1236

		trans = EHCI_ISOC_ACTIVE;
		trans |= buf & 0x0fff;
1237
		if (unlikely(((i + 1) == urb->number_of_packets))
L
Linus Torvalds 已提交
1238 1239 1240
				&& !(urb->transfer_flags & URB_NO_INTERRUPT))
			trans |= EHCI_ITD_IOC;
		trans |= length << 16;
1241
		uframe->transaction = cpu_to_hc32(ehci, trans);
L
Linus Torvalds 已提交
1242

1243
		/* might need to cross a buffer page within a uframe */
L
Linus Torvalds 已提交
1244 1245
		uframe->bufp = (buf & ~(u64)0x0fff);
		buf += length;
1246
		if (unlikely((uframe->bufp != (buf & ~(u64)0x0fff))))
L
Linus Torvalds 已提交
1247 1248 1249 1250 1251
			uframe->cross = 1;
	}
}

static void
1252
iso_sched_free(
L
Linus Torvalds 已提交
1253 1254 1255 1256 1257 1258
	struct ehci_iso_stream	*stream,
	struct ehci_iso_sched	*iso_sched
)
{
	if (!iso_sched)
		return;
1259
	/* caller must hold ehci->lock! */
1260 1261
	list_splice(&iso_sched->td_list, &stream->free_list);
	kfree(iso_sched);
L
Linus Torvalds 已提交
1262 1263 1264
}

static int
1265
itd_urb_transaction(
L
Linus Torvalds 已提交
1266 1267 1268
	struct ehci_iso_stream	*stream,
	struct ehci_hcd		*ehci,
	struct urb		*urb,
A
Al Viro 已提交
1269
	gfp_t			mem_flags
L
Linus Torvalds 已提交
1270 1271 1272 1273 1274 1275 1276 1277 1278
)
{
	struct ehci_itd		*itd;
	dma_addr_t		itd_dma;
	int			i;
	unsigned		num_itds;
	struct ehci_iso_sched	*sched;
	unsigned long		flags;

1279 1280
	sched = iso_sched_alloc(urb->number_of_packets, mem_flags);
	if (unlikely(sched == NULL))
L
Linus Torvalds 已提交
1281 1282
		return -ENOMEM;

1283
	itd_sched_init(ehci, sched, stream, urb);
L
Linus Torvalds 已提交
1284 1285 1286 1287 1288 1289 1290

	if (urb->interval < 8)
		num_itds = 1 + (sched->span + 7) / 8;
	else
		num_itds = urb->number_of_packets;

	/* allocate/init ITDs */
1291
	spin_lock_irqsave(&ehci->lock, flags);
L
Linus Torvalds 已提交
1292 1293
	for (i = 0; i < num_itds; i++) {

1294 1295 1296
		/*
		 * Use iTDs from the free list, but not iTDs that may
		 * still be in use by the hardware.
L
Linus Torvalds 已提交
1297
		 */
1298 1299
		if (likely(!list_empty(&stream->free_list))) {
			itd = list_first_entry(&stream->free_list,
1300
					struct ehci_itd, itd_list);
1301
			if (itd->frame == ehci->now_frame)
1302
				goto alloc_itd;
1303
			list_del(&itd->itd_list);
L
Linus Torvalds 已提交
1304
			itd_dma = itd->itd_dma;
1305
		} else {
1306
 alloc_itd:
1307 1308
			spin_unlock_irqrestore(&ehci->lock, flags);
			itd = dma_pool_alloc(ehci->itd_pool, mem_flags,
L
Linus Torvalds 已提交
1309
					&itd_dma);
1310
			spin_lock_irqsave(&ehci->lock, flags);
1311 1312 1313 1314 1315
			if (!itd) {
				iso_sched_free(stream, sched);
				spin_unlock_irqrestore(&ehci->lock, flags);
				return -ENOMEM;
			}
L
Linus Torvalds 已提交
1316 1317
		}

1318
		memset(itd, 0, sizeof(*itd));
L
Linus Torvalds 已提交
1319
		itd->itd_dma = itd_dma;
1320
		itd->frame = NO_FRAME;
1321
		list_add(&itd->itd_list, &sched->td_list);
L
Linus Torvalds 已提交
1322
	}
1323
	spin_unlock_irqrestore(&ehci->lock, flags);
L
Linus Torvalds 已提交
1324 1325 1326 1327 1328 1329 1330 1331 1332

	/* temporarily store schedule info in hcpriv */
	urb->hcpriv = sched;
	urb->error_count = 0;
	return 0;
}

/*-------------------------------------------------------------------------*/

1333 1334 1335 1336 1337 1338 1339 1340
static void reserve_release_iso_bandwidth(struct ehci_hcd *ehci,
		struct ehci_iso_stream *stream, int sign)
{
	unsigned		uframe;
	unsigned		i, j;
	unsigned		s_mask, c_mask, m;
	int			usecs = stream->ps.usecs;
	int			c_usecs = stream->ps.c_usecs;
1341 1342
	int			tt_usecs = stream->ps.tt_usecs;
	struct ehci_tt		*tt;
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352

	if (stream->ps.phase == NO_FRAME)	/* Bandwidth wasn't reserved */
		return;
	uframe = stream->ps.bw_phase << 3;

	bandwidth_dbg(ehci, sign, "iso", &stream->ps);

	if (sign < 0) {		/* Release bandwidth */
		usecs = -usecs;
		c_usecs = -c_usecs;
1353
		tt_usecs = -tt_usecs;
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
	}

	if (!stream->splits) {		/* High speed */
		for (i = uframe + stream->ps.phase_uf; i < EHCI_BANDWIDTH_SIZE;
				i += stream->ps.bw_uperiod)
			ehci->bandwidth[i] += usecs;

	} else {			/* Full speed */
		s_mask = stream->ps.cs_mask;
		c_mask = s_mask >> 8;

		/* NOTE: adjustment needed for frame overflow */
		for (i = uframe; i < EHCI_BANDWIDTH_SIZE;
				i += stream->ps.bw_uperiod) {
			for ((j = stream->ps.phase_uf, m = 1 << j); j < 8;
					(++j, m <<= 1)) {
				if (s_mask & m)
					ehci->bandwidth[i+j] += usecs;
				else if (c_mask & m)
					ehci->bandwidth[i+j] += c_usecs;
			}
		}
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385

		tt = find_tt(stream->ps.udev);
		if (sign > 0)
			list_add_tail(&stream->ps.ps_list, &tt->ps_list);
		else
			list_del(&stream->ps.ps_list);

		for (i = uframe >> 3; i < EHCI_BANDWIDTH_FRAMES;
				i += stream->ps.bw_period)
			tt->bandwidth[i] += tt_usecs;
1386 1387 1388
	}
}

L
Linus Torvalds 已提交
1389
static inline int
1390
itd_slot_ok(
L
Linus Torvalds 已提交
1391
	struct ehci_hcd		*ehci,
1392 1393
	struct ehci_iso_stream	*stream,
	unsigned		uframe
L
Linus Torvalds 已提交
1394 1395
)
{
1396 1397 1398 1399
	unsigned		usecs;

	/* convert "usecs we need" to "max already claimed" */
	usecs = ehci->uframe_periodic_max - stream->ps.usecs;
L
Linus Torvalds 已提交
1400

1401 1402 1403 1404 1405
	for (uframe &= stream->ps.bw_uperiod - 1; uframe < EHCI_BANDWIDTH_SIZE;
			uframe += stream->ps.bw_uperiod) {
		if (ehci->bandwidth[uframe] > usecs)
			return 0;
	}
L
Linus Torvalds 已提交
1406 1407 1408 1409
	return 1;
}

static inline int
1410
sitd_slot_ok(
L
Linus Torvalds 已提交
1411 1412
	struct ehci_hcd		*ehci,
	struct ehci_iso_stream	*stream,
1413
	unsigned		uframe,
1414 1415
	struct ehci_iso_sched	*sched,
	struct ehci_tt		*tt
L
Linus Torvalds 已提交
1416 1417
)
{
1418 1419
	unsigned		mask, tmp;
	unsigned		frame, uf;
L
Linus Torvalds 已提交
1420

1421
	mask = stream->ps.cs_mask << (uframe & 7);
L
Linus Torvalds 已提交
1422

1423
	/* for OUT, don't wrap SSPLIT into H-microframe 7 */
1424
	if (((stream->ps.cs_mask & 0xff) << (uframe & 7)) >= (1 << 7))
1425 1426
		return 0;

L
Linus Torvalds 已提交
1427 1428 1429 1430
	/* for IN, don't wrap CSPLIT into the next frame */
	if (mask & ~0xffff)
		return 0;

1431
	/* check bandwidth */
1432
	uframe &= stream->ps.bw_uperiod - 1;
1433 1434 1435 1436 1437 1438 1439
	frame = uframe >> 3;

#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
	/* The tt's fullspeed bus bandwidth must be available.
	 * tt_available scheduling guarantees 10+% for control/bulk.
	 */
	uf = uframe & 7;
1440
	if (!tt_available(ehci, &stream->ps, tt, frame, uf))
1441 1442 1443 1444 1445
		return 0;
#else
	/* tt must be idle for start(s), any gap, and csplit.
	 * assume scheduling slop leaves 10+% for control/bulk.
	 */
1446
	if (!tt_no_collision(ehci, stream->ps.bw_period,
1447
			stream->ps.udev, frame, mask))
1448 1449 1450
		return 0;
#endif

L
Linus Torvalds 已提交
1451
	do {
1452 1453
		unsigned	max_used;
		unsigned	i;
L
Linus Torvalds 已提交
1454 1455

		/* check starts (OUT uses more than one) */
1456
		uf = uframe;
1457
		max_used = ehci->uframe_periodic_max - stream->ps.usecs;
1458 1459
		for (tmp = stream->ps.cs_mask & 0xff; tmp; tmp >>= 1, uf++) {
			if (ehci->bandwidth[uf] > max_used)
L
Linus Torvalds 已提交
1460 1461 1462 1463
				return 0;
		}

		/* for IN, check CSPLIT */
1464 1465 1466
		if (stream->ps.c_usecs) {
			max_used = ehci->uframe_periodic_max -
					stream->ps.c_usecs;
1467 1468 1469 1470
			uf = uframe & ~7;
			tmp = 1 << (2+8);
			for (i = (uframe & 7) + 2; i < 8; (++i, tmp <<= 1)) {
				if ((stream->ps.cs_mask & tmp) == 0)
L
Linus Torvalds 已提交
1471
					continue;
1472
				if (ehci->bandwidth[uf+i] > max_used)
L
Linus Torvalds 已提交
1473
					return 0;
1474
			}
L
Linus Torvalds 已提交
1475 1476
		}

1477 1478
		uframe += stream->ps.bw_uperiod;
	} while (uframe < EHCI_BANDWIDTH_SIZE);
L
Linus Torvalds 已提交
1479

1480 1481
	stream->ps.cs_mask <<= uframe & 7;
	stream->splits = cpu_to_hc32(ehci, stream->ps.cs_mask);
L
Linus Torvalds 已提交
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
	return 1;
}

/*
 * This scheduler plans almost as far into the future as it has actual
 * periodic schedule slots.  (Affected by TUNE_FLS, which defaults to
 * "as small as possible" to be cache-friendlier.)  That limits the size
 * transfers you can stream reliably; avoid more than 64 msec per urb.
 * Also avoid queue depths of less than ehci's worst irq latency (affected
 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
 * and other factors); or more than about 230 msec total (for portability,
 * given EHCI_TUNE_FLS and the slop).  Or, write a smarter scheduler!
 */

static int
1497
iso_stream_schedule(
L
Linus Torvalds 已提交
1498 1499 1500 1501 1502
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	struct ehci_iso_stream	*stream
)
{
1503 1504 1505
	u32			now, base, next, start, period, span, now2;
	u32			wrap = 0, skip = 0;
	int			status = 0;
L
Linus Torvalds 已提交
1506 1507
	unsigned		mod = ehci->periodic_size << 3;
	struct ehci_iso_sched	*sched = urb->hcpriv;
1508
	bool			empty = list_empty(&stream->td_list);
1509
	bool			new_stream = false;
L
Linus Torvalds 已提交
1510

1511
	period = stream->uperiod;
1512
	span = sched->span;
1513
	if (!stream->highspeed)
1514 1515
		span <<= 3;

1516
	/* Start a new isochronous stream? */
1517 1518
	if (unlikely(empty && !hcd_periodic_completion_in_progress(
			ehci_to_hcd(ehci), urb->ep))) {
1519

1520 1521 1522
		/* Schedule the endpoint */
		if (stream->ps.phase == NO_FRAME) {
			int		done = 0;
1523 1524 1525 1526 1527 1528 1529
			struct ehci_tt	*tt = find_tt(stream->ps.udev);

			if (IS_ERR(tt)) {
				status = PTR_ERR(tt);
				goto fail;
			}
			compute_tt_budget(ehci->tt_budget, tt);
1530

1531
			start = ((-(++ehci->random_frame)) << 3) & (period - 1);
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549

			/* find a uframe slot with enough bandwidth.
			 * Early uframes are more precious because full-speed
			 * iso IN transfers can't use late uframes,
			 * and therefore they should be allocated last.
			 */
			next = start;
			start += period;
			do {
				start--;
				/* check schedule: enough space? */
				if (stream->highspeed) {
					if (itd_slot_ok(ehci, stream, start))
						done = 1;
				} else {
					if ((start % 8) >= 6)
						continue;
					if (sitd_slot_ok(ehci, stream, start,
1550
							sched, tt))
1551 1552 1553 1554 1555 1556 1557 1558 1559
						done = 1;
				}
			} while (start > next && !done);

			/* no room in the schedule */
			if (!done) {
				ehci_dbg(ehci, "iso sched full %p", urb);
				status = -ENOSPC;
				goto fail;
1560
			}
1561 1562 1563 1564 1565 1566 1567
			stream->ps.phase = (start >> 3) &
					(stream->ps.period - 1);
			stream->ps.bw_phase = stream->ps.phase &
					(stream->ps.bw_period - 1);
			stream->ps.phase_uf = start & 7;
			reserve_release_iso_bandwidth(ehci, stream, 1);
		}
1568

1569 1570 1571
		/* New stream is already scheduled; use the upcoming slot */
		else {
			start = (stream->ps.phase << 3) + stream->ps.phase_uf;
1572
		}
1573

1574 1575
		stream->next_uframe = start;
		new_stream = true;
1576 1577
	}

1578 1579 1580 1581 1582 1583 1584 1585
	now = ehci_read_frame_index(ehci) & (mod - 1);

	/* Take the isochronous scheduling threshold into account */
	if (ehci->i_thresh)
		next = now + ehci->i_thresh;	/* uframe cache */
	else
		next = (now + 2 + 7) & ~0x07;	/* full frame cache */

1586 1587 1588 1589
	/* If needed, initialize last_iso_frame so that this URB will be seen */
	if (ehci->isoc_count == 0)
		ehci->last_iso_frame = now >> 3;

1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
	/*
	 * Use ehci->last_iso_frame as the base.  There can't be any
	 * TDs scheduled for earlier than that.
	 */
	base = ehci->last_iso_frame << 3;
	next = (next - base) & (mod - 1);
	start = (stream->next_uframe - base) & (mod - 1);

	if (unlikely(new_stream))
		goto do_ASAP;

1601
	/*
1602
	 * Typical case: reuse current schedule, stream may still be active.
1603
	 * Hopefully there are no gaps from the host falling behind
1604 1605
	 * (irq delays etc).  If there are, the behavior depends on
	 * whether URB_ISO_ASAP is set.
L
Linus Torvalds 已提交
1606
	 */
1607
	now2 = (now - base) & (mod - 1);
1608

A
Alan Stern 已提交
1609
	/* Is the schedule about to wrap around? */
1610
	if (unlikely(!empty && start < period)) {
A
Alan Stern 已提交
1611
		ehci_dbg(ehci, "request %p would overflow (%u-%u < %u mod %u)\n",
1612
				urb, stream->next_uframe, base, period, mod);
A
Alan Stern 已提交
1613
		status = -EFBIG;
1614 1615
		goto fail;
	}
1616

1617 1618
	/* Is the next packet scheduled after the base time? */
	if (likely(!empty || start <= now2 + period)) {
1619

1620 1621 1622 1623
		/* URB_ISO_ASAP: make sure that start >= next */
		if (unlikely(start < next &&
				(urb->transfer_flags & URB_ISO_ASAP)))
			goto do_ASAP;
1624

1625 1626 1627
		/* Otherwise use start, if it's not in the past */
		if (likely(start >= now2))
			goto use_start;
1628

1629 1630 1631 1632 1633 1634 1635
	/* Otherwise we got an underrun while the queue was empty */
	} else {
		if (urb->transfer_flags & URB_ISO_ASAP)
			goto do_ASAP;
		wrap = mod;
		now2 += mod;
	}
1636

1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
	/* How many uframes and packets do we need to skip? */
	skip = (now2 - start + period - 1) & -period;
	if (skip >= span) {		/* Entirely in the past? */
		ehci_dbg(ehci, "iso underrun %p (%u+%u < %u) [%u]\n",
				urb, start + base, span - period, now2 + base,
				base);

		/* Try to keep the last TD intact for scanning later */
		skip = span - period;

		/* Will it come before the current scan position? */
		if (empty) {
			skip = span;	/* Skip the entire URB */
			status = 1;	/* and give it back immediately */
			iso_sched_free(stream, sched);
			sched = NULL;
		}
L
Linus Torvalds 已提交
1654
	}
1655 1656 1657 1658
	urb->error_count = skip / period;
	if (sched)
		sched->first_packet = urb->error_count;
	goto use_start;
L
Linus Torvalds 已提交
1659

1660 1661 1662 1663 1664
 do_ASAP:
	/* Use the first slot after "next" */
	start = next + ((start - next) & (period - 1));

 use_start:
1665
	/* Tried to schedule too far into the future? */
1666
	if (unlikely(start + span - period >= mod + wrap)) {
1667
		ehci_dbg(ehci, "request %p would overflow (%u+%u >= %u)\n",
1668
				urb, start, span - period, mod + wrap);
1669 1670 1671
		status = -EFBIG;
		goto fail;
	}
L
Linus Torvalds 已提交
1672

1673 1674
	start += base;
	stream->next_uframe = (start + skip) & (mod - 1);
L
Linus Torvalds 已提交
1675 1676

	/* report high speed start in uframes; full speed, in frames */
1677
	urb->start_frame = start & (mod - 1);
L
Linus Torvalds 已提交
1678 1679
	if (!stream->highspeed)
		urb->start_frame >>= 3;
1680
	return status;
1681 1682 1683 1684 1685

 fail:
	iso_sched_free(stream, sched);
	urb->hcpriv = NULL;
	return status;
L
Linus Torvalds 已提交
1686 1687 1688 1689 1690
}

/*-------------------------------------------------------------------------*/

static inline void
1691 1692
itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
		struct ehci_itd *itd)
L
Linus Torvalds 已提交
1693 1694 1695
{
	int i;

1696
	/* it's been recently zeroed */
1697
	itd->hw_next = EHCI_LIST_END(ehci);
1698 1699 1700
	itd->hw_bufp[0] = stream->buf0;
	itd->hw_bufp[1] = stream->buf1;
	itd->hw_bufp[2] = stream->buf2;
L
Linus Torvalds 已提交
1701 1702 1703 1704 1705 1706 1707 1708

	for (i = 0; i < 8; i++)
		itd->index[i] = -1;

	/* All other fields are filled when scheduling */
}

static inline void
1709 1710
itd_patch(
	struct ehci_hcd		*ehci,
L
Linus Torvalds 已提交
1711 1712 1713
	struct ehci_itd		*itd,
	struct ehci_iso_sched	*iso_sched,
	unsigned		index,
1714
	u16			uframe
L
Linus Torvalds 已提交
1715 1716
)
{
1717
	struct ehci_iso_packet	*uf = &iso_sched->packet[index];
L
Linus Torvalds 已提交
1718 1719
	unsigned		pg = itd->pg;

1720
	/* BUG_ON(pg == 6 && uf->cross); */
L
Linus Torvalds 已提交
1721 1722

	uframe &= 0x07;
1723
	itd->index[uframe] = index;
L
Linus Torvalds 已提交
1724

1725 1726 1727 1728
	itd->hw_transaction[uframe] = uf->transaction;
	itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
	itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
	itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
L
Linus Torvalds 已提交
1729 1730

	/* iso_frame_desc[].offset must be strictly increasing */
1731
	if (unlikely(uf->cross)) {
L
Linus Torvalds 已提交
1732
		u64	bufp = uf->bufp + 4096;
1733

L
Linus Torvalds 已提交
1734
		itd->pg = ++pg;
1735 1736
		itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
		itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
L
Linus Torvalds 已提交
1737 1738 1739 1740
	}
}

static inline void
1741
itd_link(struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
L
Linus Torvalds 已提交
1742
{
C
Clemens Ladisch 已提交
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
	union ehci_shadow	*prev = &ehci->pshadow[frame];
	__hc32			*hw_p = &ehci->periodic[frame];
	union ehci_shadow	here = *prev;
	__hc32			type = 0;

	/* skip any iso nodes which might belong to previous microframes */
	while (here.ptr) {
		type = Q_NEXT_TYPE(ehci, *hw_p);
		if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
			break;
		prev = periodic_next_shadow(ehci, prev, type);
		hw_p = shadow_next_periodic(ehci, &here, type);
		here = *prev;
	}

	itd->itd_next = here;
	itd->hw_next = *hw_p;
	prev->itd = itd;
L
Linus Torvalds 已提交
1761
	itd->frame = frame;
1762
	wmb();
C
Clemens Ladisch 已提交
1763
	*hw_p = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
L
Linus Torvalds 已提交
1764 1765 1766
}

/* fit urb's itds into the selected schedule slot; activate as needed */
A
Alan Stern 已提交
1767
static void itd_link_urb(
L
Linus Torvalds 已提交
1768 1769 1770 1771 1772 1773
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	unsigned		mod,
	struct ehci_iso_stream	*stream
)
{
1774
	int			packet;
L
Linus Torvalds 已提交
1775 1776 1777 1778
	unsigned		next_uframe, uframe, frame;
	struct ehci_iso_sched	*iso_sched = urb->hcpriv;
	struct ehci_itd		*itd;

1779
	next_uframe = stream->next_uframe & (mod - 1);
L
Linus Torvalds 已提交
1780

1781
	if (unlikely(list_empty(&stream->td_list)))
L
Linus Torvalds 已提交
1782 1783
		ehci_to_hcd(ehci)->self.bandwidth_allocated
				+= stream->bandwidth;
1784 1785

	if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
1786 1787
		if (ehci->amd_pll_fix == 1)
			usb_amd_quirk_pll_disable();
1788 1789
	}

L
Linus Torvalds 已提交
1790 1791 1792
	ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;

	/* fill iTDs uframe by uframe */
1793 1794
	for (packet = iso_sched->first_packet, itd = NULL;
			packet < urb->number_of_packets;) {
L
Linus Torvalds 已提交
1795 1796
		if (itd == NULL) {
			/* ASSERT:  we have all necessary itds */
1797
			/* BUG_ON(list_empty(&iso_sched->td_list)); */
L
Linus Torvalds 已提交
1798 1799 1800

			/* ASSERT:  no itds for this endpoint in this uframe */

1801
			itd = list_entry(iso_sched->td_list.next,
L
Linus Torvalds 已提交
1802
					struct ehci_itd, itd_list);
1803
			list_move_tail(&itd->itd_list, &stream->td_list);
1804
			itd->stream = stream;
1805
			itd->urb = urb;
1806
			itd_init(ehci, stream, itd);
L
Linus Torvalds 已提交
1807 1808 1809 1810 1811
		}

		uframe = next_uframe & 0x07;
		frame = next_uframe >> 3;

1812
		itd_patch(ehci, itd, iso_sched, packet, uframe);
L
Linus Torvalds 已提交
1813

1814
		next_uframe += stream->uperiod;
1815
		next_uframe &= mod - 1;
L
Linus Torvalds 已提交
1816 1817 1818 1819 1820
		packet++;

		/* link completed itds into the schedule */
		if (((next_uframe >> 3) != frame)
				|| packet == urb->number_of_packets) {
1821
			itd_link(ehci, frame & (ehci->periodic_size - 1), itd);
L
Linus Torvalds 已提交
1822 1823 1824 1825 1826 1827
			itd = NULL;
		}
	}
	stream->next_uframe = next_uframe;

	/* don't need that schedule data any more */
1828
	iso_sched_free(stream, iso_sched);
1829
	urb->hcpriv = stream;
L
Linus Torvalds 已提交
1830

1831
	++ehci->isoc_count;
A
Alan Stern 已提交
1832
	enable_periodic(ehci);
L
Linus Torvalds 已提交
1833 1834 1835 1836
}

#define	ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)

1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
/* Process and recycle a completed ITD.  Return true iff its urb completed,
 * and hence its completion callback probably added things to the hardware
 * schedule.
 *
 * Note that we carefully avoid recycling this descriptor until after any
 * completion callback runs, so that it won't be reused quickly.  That is,
 * assuming (a) no more than two urbs per frame on this endpoint, and also
 * (b) only this endpoint's completions submit URBs.  It seems some silicon
 * corrupts things if you reuse completed descriptors very quickly...
 */
1847 1848
static bool itd_complete(struct ehci_hcd *ehci, struct ehci_itd *itd)
{
L
Linus Torvalds 已提交
1849 1850 1851 1852 1853 1854 1855
	struct urb				*urb = itd->urb;
	struct usb_iso_packet_descriptor	*desc;
	u32					t;
	unsigned				uframe;
	int					urb_index = -1;
	struct ehci_iso_stream			*stream = itd->stream;
	struct usb_device			*dev;
1856
	bool					retval = false;
L
Linus Torvalds 已提交
1857 1858 1859

	/* for each uframe with a packet */
	for (uframe = 0; uframe < 8; uframe++) {
1860
		if (likely(itd->index[uframe] == -1))
L
Linus Torvalds 已提交
1861 1862
			continue;
		urb_index = itd->index[uframe];
1863
		desc = &urb->iso_frame_desc[urb_index];
L
Linus Torvalds 已提交
1864

1865 1866
		t = hc32_to_cpup(ehci, &itd->hw_transaction[uframe]);
		itd->hw_transaction[uframe] = 0;
L
Linus Torvalds 已提交
1867 1868

		/* report transfer status */
1869
		if (unlikely(t & ISO_ERRS)) {
L
Linus Torvalds 已提交
1870 1871
			urb->error_count++;
			if (t & EHCI_ISOC_BUF_ERR)
1872
				desc->status = usb_pipein(urb->pipe)
L
Linus Torvalds 已提交
1873 1874 1875 1876 1877 1878 1879 1880
					? -ENOSR  /* hc couldn't read */
					: -ECOMM; /* hc 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 */
1881 1882 1883 1884
			if (!(t & EHCI_ISOC_BABBLE)) {
				desc->actual_length = EHCI_ITD_LENGTH(t);
				urb->actual_length += desc->actual_length;
			}
1885
		} else if (likely((t & EHCI_ISOC_ACTIVE) == 0)) {
L
Linus Torvalds 已提交
1886
			desc->status = 0;
1887 1888
			desc->actual_length = EHCI_ITD_LENGTH(t);
			urb->actual_length += desc->actual_length;
1889 1890
		} else {
			/* URB was too late */
1891
			urb->error_count++;
L
Linus Torvalds 已提交
1892 1893 1894 1895
		}
	}

	/* handle completion now? */
1896
	if (likely((urb_index + 1) != urb->number_of_packets))
1897
		goto done;
L
Linus Torvalds 已提交
1898

1899 1900 1901 1902
	/*
	 * ASSERT: it's really the last itd for this urb
	 * list_for_each_entry (itd, &stream->td_list, itd_list)
	 *	 BUG_ON(itd->urb == urb);
L
Linus Torvalds 已提交
1903 1904
	 */

1905
	/* give urb back to the driver; completion often (re)submits */
1906
	dev = urb->dev;
1907
	ehci_urb_done(ehci, urb, 0);
1908
	retval = true;
L
Linus Torvalds 已提交
1909
	urb = NULL;
1910 1911

	--ehci->isoc_count;
A
Alan Stern 已提交
1912
	disable_periodic(ehci);
L
Linus Torvalds 已提交
1913

1914
	ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
1915
	if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
1916 1917
		if (ehci->amd_pll_fix == 1)
			usb_amd_quirk_pll_enable();
1918 1919
	}

1920
	if (unlikely(list_is_singular(&stream->td_list)))
L
Linus Torvalds 已提交
1921 1922
		ehci_to_hcd(ehci)->self.bandwidth_allocated
				-= stream->bandwidth;
K
Karsten Wiese 已提交
1923

1924 1925
done:
	itd->urb = NULL;
1926 1927 1928 1929 1930 1931 1932 1933 1934

	/* Add to the end of the free list for later reuse */
	list_move_tail(&itd->itd_list, &stream->free_list);

	/* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
	if (list_empty(&stream->td_list)) {
		list_splice_tail_init(&stream->free_list,
				&ehci->cached_itd_list);
		start_free_itds(ehci);
K
Karsten Wiese 已提交
1935
	}
1936

1937
	return retval;
L
Linus Torvalds 已提交
1938 1939 1940 1941
}

/*-------------------------------------------------------------------------*/

1942
static int itd_submit(struct ehci_hcd *ehci, struct urb *urb,
A
Al Viro 已提交
1943
	gfp_t mem_flags)
L
Linus Torvalds 已提交
1944 1945 1946 1947 1948 1949
{
	int			status = -EINVAL;
	unsigned long		flags;
	struct ehci_iso_stream	*stream;

	/* Get iso_stream head */
1950 1951 1952
	stream = iso_stream_find(ehci, urb);
	if (unlikely(stream == NULL)) {
		ehci_dbg(ehci, "can't get iso stream\n");
L
Linus Torvalds 已提交
1953 1954
		return -ENOMEM;
	}
1955
	if (unlikely(urb->interval != stream->uperiod)) {
1956
		ehci_dbg(ehci, "can't change iso interval %d --> %d\n",
1957
			stream->uperiod, urb->interval);
L
Linus Torvalds 已提交
1958 1959 1960 1961
		goto done;
	}

#ifdef EHCI_URB_TRACE
1962
	ehci_dbg(ehci,
L
Linus Torvalds 已提交
1963
		"%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
1964
		__func__, urb->dev->devpath, urb,
1965 1966
		usb_pipeendpoint(urb->pipe),
		usb_pipein(urb->pipe) ? "in" : "out",
L
Linus Torvalds 已提交
1967 1968 1969 1970 1971 1972
		urb->transfer_buffer_length,
		urb->number_of_packets, urb->interval,
		stream);
#endif

	/* allocate ITDs w/o locking anything */
1973 1974 1975
	status = itd_urb_transaction(stream, ehci, urb, mem_flags);
	if (unlikely(status < 0)) {
		ehci_dbg(ehci, "can't init itds\n");
L
Linus Torvalds 已提交
1976 1977 1978 1979
		goto done;
	}

	/* schedule ... need to lock */
1980
	spin_lock_irqsave(&ehci->lock, flags);
1981
	if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
1982
		status = -ESHUTDOWN;
1983 1984 1985 1986 1987 1988
		goto done_not_linked;
	}
	status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
	if (unlikely(status))
		goto done_not_linked;
	status = iso_stream_schedule(ehci, urb, stream);
1989
	if (likely(status == 0)) {
1990
		itd_link_urb(ehci, urb, ehci->periodic_size << 3, stream);
1991 1992 1993 1994
	} else if (status > 0) {
		status = 0;
		ehci_urb_done(ehci, urb, 0);
	} else {
1995
		usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
1996
	}
1997
 done_not_linked:
1998
	spin_unlock_irqrestore(&ehci->lock, flags);
1999
 done:
L
Linus Torvalds 已提交
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
	return status;
}

/*-------------------------------------------------------------------------*/

/*
 * "Split ISO TDs" ... used for USB 1.1 devices going through the
 * TTs in USB 2.0 hubs.  These need microframe scheduling.
 */

static inline void
2011 2012
sitd_sched_init(
	struct ehci_hcd		*ehci,
L
Linus Torvalds 已提交
2013 2014 2015 2016 2017 2018 2019 2020 2021
	struct ehci_iso_sched	*iso_sched,
	struct ehci_iso_stream	*stream,
	struct urb		*urb
)
{
	unsigned	i;
	dma_addr_t	dma = urb->transfer_dma;

	/* how many frames are needed for these transfers */
2022
	iso_sched->span = urb->number_of_packets * stream->ps.period;
L
Linus Torvalds 已提交
2023 2024 2025 2026 2027

	/* figure out per-frame sitd fields that we'll need later
	 * when we fit new sitds into the schedule.
	 */
	for (i = 0; i < urb->number_of_packets; i++) {
2028
		struct ehci_iso_packet	*packet = &iso_sched->packet[i];
L
Linus Torvalds 已提交
2029 2030 2031 2032
		unsigned		length;
		dma_addr_t		buf;
		u32			trans;

2033 2034
		length = urb->iso_frame_desc[i].length & 0x03ff;
		buf = dma + urb->iso_frame_desc[i].offset;
L
Linus Torvalds 已提交
2035 2036 2037 2038 2039 2040

		trans = SITD_STS_ACTIVE;
		if (((i + 1) == urb->number_of_packets)
				&& !(urb->transfer_flags & URB_NO_INTERRUPT))
			trans |= SITD_IOC;
		trans |= length << 16;
2041
		packet->transaction = cpu_to_hc32(ehci, trans);
L
Linus Torvalds 已提交
2042 2043 2044 2045 2046 2047 2048

		/* might need to cross a buffer page within a td */
		packet->bufp = buf;
		packet->buf1 = (buf + length) & ~0x0fff;
		if (packet->buf1 != (buf & ~(u64)0x0fff))
			packet->cross = 1;

2049
		/* OUT uses multiple start-splits */
L
Linus Torvalds 已提交
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
		if (stream->bEndpointAddress & USB_DIR_IN)
			continue;
		length = (length + 187) / 188;
		if (length > 1) /* BEGIN vs ALL */
			length |= 1 << 3;
		packet->buf1 |= length;
	}
}

static int
2060
sitd_urb_transaction(
L
Linus Torvalds 已提交
2061 2062 2063
	struct ehci_iso_stream	*stream,
	struct ehci_hcd		*ehci,
	struct urb		*urb,
A
Al Viro 已提交
2064
	gfp_t			mem_flags
L
Linus Torvalds 已提交
2065 2066 2067 2068 2069 2070 2071 2072
)
{
	struct ehci_sitd	*sitd;
	dma_addr_t		sitd_dma;
	int			i;
	struct ehci_iso_sched	*iso_sched;
	unsigned long		flags;

2073
	iso_sched = iso_sched_alloc(urb->number_of_packets, mem_flags);
L
Linus Torvalds 已提交
2074 2075 2076
	if (iso_sched == NULL)
		return -ENOMEM;

2077
	sitd_sched_init(ehci, iso_sched, stream, urb);
L
Linus Torvalds 已提交
2078 2079

	/* allocate/init sITDs */
2080
	spin_lock_irqsave(&ehci->lock, flags);
L
Linus Torvalds 已提交
2081 2082 2083 2084 2085 2086 2087
	for (i = 0; i < urb->number_of_packets; i++) {

		/* NOTE:  for now, we don't try to handle wraparound cases
		 * for IN (using sitd->hw_backpointer, like a FSTN), which
		 * means we never need two sitds for full speed packets.
		 */

2088 2089 2090
		/*
		 * Use siTDs from the free list, but not siTDs that may
		 * still be in use by the hardware.
L
Linus Torvalds 已提交
2091
		 */
2092 2093
		if (likely(!list_empty(&stream->free_list))) {
			sitd = list_first_entry(&stream->free_list,
L
Linus Torvalds 已提交
2094
					 struct ehci_sitd, sitd_list);
2095
			if (sitd->frame == ehci->now_frame)
2096
				goto alloc_sitd;
2097
			list_del(&sitd->sitd_list);
L
Linus Torvalds 已提交
2098
			sitd_dma = sitd->sitd_dma;
2099
		} else {
2100
 alloc_sitd:
2101 2102
			spin_unlock_irqrestore(&ehci->lock, flags);
			sitd = dma_pool_alloc(ehci->sitd_pool, mem_flags,
L
Linus Torvalds 已提交
2103
					&sitd_dma);
2104
			spin_lock_irqsave(&ehci->lock, flags);
2105 2106 2107 2108 2109
			if (!sitd) {
				iso_sched_free(stream, iso_sched);
				spin_unlock_irqrestore(&ehci->lock, flags);
				return -ENOMEM;
			}
L
Linus Torvalds 已提交
2110 2111
		}

2112
		memset(sitd, 0, sizeof(*sitd));
L
Linus Torvalds 已提交
2113
		sitd->sitd_dma = sitd_dma;
2114
		sitd->frame = NO_FRAME;
2115
		list_add(&sitd->sitd_list, &iso_sched->td_list);
L
Linus Torvalds 已提交
2116 2117 2118 2119 2120 2121
	}

	/* temporarily store schedule info in hcpriv */
	urb->hcpriv = iso_sched;
	urb->error_count = 0;

2122
	spin_unlock_irqrestore(&ehci->lock, flags);
L
Linus Torvalds 已提交
2123 2124 2125 2126 2127 2128
	return 0;
}

/*-------------------------------------------------------------------------*/

static inline void
2129 2130
sitd_patch(
	struct ehci_hcd		*ehci,
L
Linus Torvalds 已提交
2131 2132 2133 2134 2135 2136
	struct ehci_iso_stream	*stream,
	struct ehci_sitd	*sitd,
	struct ehci_iso_sched	*iso_sched,
	unsigned		index
)
{
2137
	struct ehci_iso_packet	*uf = &iso_sched->packet[index];
2138
	u64			bufp;
L
Linus Torvalds 已提交
2139

2140
	sitd->hw_next = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
2141 2142 2143
	sitd->hw_fullspeed_ep = stream->address;
	sitd->hw_uframe = stream->splits;
	sitd->hw_results = uf->transaction;
2144
	sitd->hw_backpointer = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
2145 2146

	bufp = uf->bufp;
2147 2148
	sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp);
	sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32);
L
Linus Torvalds 已提交
2149

2150
	sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
L
Linus Torvalds 已提交
2151 2152
	if (uf->cross)
		bufp += 4096;
2153
	sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
L
Linus Torvalds 已提交
2154 2155 2156 2157
	sitd->index = index;
}

static inline void
2158
sitd_link(struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
L
Linus Torvalds 已提交
2159 2160
{
	/* note: sitd ordering could matter (CSPLIT then SSPLIT) */
2161 2162 2163
	sitd->sitd_next = ehci->pshadow[frame];
	sitd->hw_next = ehci->periodic[frame];
	ehci->pshadow[frame].sitd = sitd;
L
Linus Torvalds 已提交
2164
	sitd->frame = frame;
2165
	wmb();
2166
	ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
L
Linus Torvalds 已提交
2167 2168 2169
}

/* fit urb's sitds into the selected schedule slot; activate as needed */
A
Alan Stern 已提交
2170
static void sitd_link_urb(
L
Linus Torvalds 已提交
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	unsigned		mod,
	struct ehci_iso_stream	*stream
)
{
	int			packet;
	unsigned		next_uframe;
	struct ehci_iso_sched	*sched = urb->hcpriv;
	struct ehci_sitd	*sitd;

	next_uframe = stream->next_uframe;

2184
	if (list_empty(&stream->td_list))
L
Linus Torvalds 已提交
2185 2186 2187
		/* usbfs ignores TT bandwidth */
		ehci_to_hcd(ehci)->self.bandwidth_allocated
				+= stream->bandwidth;
2188 2189

	if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
2190 2191
		if (ehci->amd_pll_fix == 1)
			usb_amd_quirk_pll_disable();
2192 2193
	}

L
Linus Torvalds 已提交
2194 2195 2196
	ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;

	/* fill sITDs frame by frame */
2197
	for (packet = sched->first_packet, sitd = NULL;
L
Linus Torvalds 已提交
2198 2199 2200 2201
			packet < urb->number_of_packets;
			packet++) {

		/* ASSERT:  we have all necessary sitds */
2202
		BUG_ON(list_empty(&sched->td_list));
L
Linus Torvalds 已提交
2203 2204 2205

		/* ASSERT:  no itds for this endpoint in this frame */

2206
		sitd = list_entry(sched->td_list.next,
L
Linus Torvalds 已提交
2207
				struct ehci_sitd, sitd_list);
2208
		list_move_tail(&sitd->sitd_list, &stream->td_list);
2209
		sitd->stream = stream;
2210
		sitd->urb = urb;
L
Linus Torvalds 已提交
2211

2212
		sitd_patch(ehci, stream, sitd, sched, packet);
2213
		sitd_link(ehci, (next_uframe >> 3) & (ehci->periodic_size - 1),
L
Linus Torvalds 已提交
2214 2215
				sitd);

2216
		next_uframe += stream->uperiod;
L
Linus Torvalds 已提交
2217
	}
2218
	stream->next_uframe = next_uframe & (mod - 1);
L
Linus Torvalds 已提交
2219 2220

	/* don't need that schedule data any more */
2221
	iso_sched_free(stream, sched);
2222
	urb->hcpriv = stream;
L
Linus Torvalds 已提交
2223

2224
	++ehci->isoc_count;
A
Alan Stern 已提交
2225
	enable_periodic(ehci);
L
Linus Torvalds 已提交
2226 2227 2228 2229 2230
}

/*-------------------------------------------------------------------------*/

#define	SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
2231
				| SITD_STS_XACT | SITD_STS_MMF)
L
Linus Torvalds 已提交
2232

2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
/* Process and recycle a completed SITD.  Return true iff its urb completed,
 * and hence its completion callback probably added things to the hardware
 * schedule.
 *
 * Note that we carefully avoid recycling this descriptor until after any
 * completion callback runs, so that it won't be reused quickly.  That is,
 * assuming (a) no more than two urbs per frame on this endpoint, and also
 * (b) only this endpoint's completions submit URBs.  It seems some silicon
 * corrupts things if you reuse completed descriptors very quickly...
 */
2243 2244
static bool sitd_complete(struct ehci_hcd *ehci, struct ehci_sitd *sitd)
{
L
Linus Torvalds 已提交
2245 2246 2247
	struct urb				*urb = sitd->urb;
	struct usb_iso_packet_descriptor	*desc;
	u32					t;
2248
	int					urb_index;
L
Linus Torvalds 已提交
2249 2250
	struct ehci_iso_stream			*stream = sitd->stream;
	struct usb_device			*dev;
2251
	bool					retval = false;
L
Linus Torvalds 已提交
2252 2253

	urb_index = sitd->index;
2254
	desc = &urb->iso_frame_desc[urb_index];
2255
	t = hc32_to_cpup(ehci, &sitd->hw_results);
L
Linus Torvalds 已提交
2256 2257

	/* report transfer status */
2258
	if (unlikely(t & SITD_ERRS)) {
L
Linus Torvalds 已提交
2259 2260
		urb->error_count++;
		if (t & SITD_STS_DBE)
2261
			desc->status = usb_pipein(urb->pipe)
L
Linus Torvalds 已提交
2262 2263 2264 2265 2266 2267
				? -ENOSR  /* hc couldn't read */
				: -ECOMM; /* hc couldn't write */
		else if (t & SITD_STS_BABBLE)
			desc->status = -EOVERFLOW;
		else /* XACT, MMF, etc */
			desc->status = -EPROTO;
2268 2269 2270
	} else if (unlikely(t & SITD_STS_ACTIVE)) {
		/* URB was too late */
		urb->error_count++;
L
Linus Torvalds 已提交
2271 2272
	} else {
		desc->status = 0;
2273 2274
		desc->actual_length = desc->length - SITD_LENGTH(t);
		urb->actual_length += desc->actual_length;
L
Linus Torvalds 已提交
2275 2276 2277 2278
	}

	/* handle completion now? */
	if ((urb_index + 1) != urb->number_of_packets)
2279
		goto done;
L
Linus Torvalds 已提交
2280

2281 2282 2283 2284
	/*
	 * ASSERT: it's really the last sitd for this urb
	 * list_for_each_entry (sitd, &stream->td_list, sitd_list)
	 *	 BUG_ON(sitd->urb == urb);
L
Linus Torvalds 已提交
2285 2286
	 */

2287
	/* give urb back to the driver; completion often (re)submits */
2288
	dev = urb->dev;
2289
	ehci_urb_done(ehci, urb, 0);
2290
	retval = true;
L
Linus Torvalds 已提交
2291
	urb = NULL;
2292 2293

	--ehci->isoc_count;
A
Alan Stern 已提交
2294
	disable_periodic(ehci);
L
Linus Torvalds 已提交
2295

2296
	ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
2297
	if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
2298 2299
		if (ehci->amd_pll_fix == 1)
			usb_amd_quirk_pll_enable();
2300 2301
	}

2302
	if (list_is_singular(&stream->td_list))
L
Linus Torvalds 已提交
2303 2304
		ehci_to_hcd(ehci)->self.bandwidth_allocated
				-= stream->bandwidth;
2305

2306 2307
done:
	sitd->urb = NULL;
2308 2309 2310 2311 2312 2313 2314 2315 2316

	/* Add to the end of the free list for later reuse */
	list_move_tail(&sitd->sitd_list, &stream->free_list);

	/* Recycle the siTDs when the pipeline is empty (ep no longer in use) */
	if (list_empty(&stream->td_list)) {
		list_splice_tail_init(&stream->free_list,
				&ehci->cached_sitd_list);
		start_free_itds(ehci);
2317
	}
2318

2319
	return retval;
L
Linus Torvalds 已提交
2320 2321 2322
}


2323
static int sitd_submit(struct ehci_hcd *ehci, struct urb *urb,
A
Al Viro 已提交
2324
	gfp_t mem_flags)
L
Linus Torvalds 已提交
2325 2326 2327 2328 2329 2330
{
	int			status = -EINVAL;
	unsigned long		flags;
	struct ehci_iso_stream	*stream;

	/* Get iso_stream head */
2331
	stream = iso_stream_find(ehci, urb);
L
Linus Torvalds 已提交
2332
	if (stream == NULL) {
2333
		ehci_dbg(ehci, "can't get iso stream\n");
L
Linus Torvalds 已提交
2334 2335
		return -ENOMEM;
	}
2336
	if (urb->interval != stream->ps.period) {
2337
		ehci_dbg(ehci, "can't change iso interval %d --> %d\n",
2338
			stream->ps.period, urb->interval);
L
Linus Torvalds 已提交
2339 2340 2341 2342
		goto done;
	}

#ifdef EHCI_URB_TRACE
2343
	ehci_dbg(ehci,
L
Linus Torvalds 已提交
2344 2345
		"submit %p dev%s ep%d%s-iso len %d\n",
		urb, urb->dev->devpath,
2346 2347
		usb_pipeendpoint(urb->pipe),
		usb_pipein(urb->pipe) ? "in" : "out",
L
Linus Torvalds 已提交
2348 2349 2350 2351
		urb->transfer_buffer_length);
#endif

	/* allocate SITDs */
2352
	status = sitd_urb_transaction(stream, ehci, urb, mem_flags);
L
Linus Torvalds 已提交
2353
	if (status < 0) {
2354
		ehci_dbg(ehci, "can't init sitds\n");
L
Linus Torvalds 已提交
2355 2356 2357 2358
		goto done;
	}

	/* schedule ... need to lock */
2359
	spin_lock_irqsave(&ehci->lock, flags);
2360
	if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
2361
		status = -ESHUTDOWN;
2362 2363 2364 2365 2366 2367
		goto done_not_linked;
	}
	status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
	if (unlikely(status))
		goto done_not_linked;
	status = iso_stream_schedule(ehci, urb, stream);
2368
	if (likely(status == 0)) {
2369
		sitd_link_urb(ehci, urb, ehci->periodic_size << 3, stream);
2370 2371 2372 2373
	} else if (status > 0) {
		status = 0;
		ehci_urb_done(ehci, urb, 0);
	} else {
2374
		usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
2375
	}
2376
 done_not_linked:
2377
	spin_unlock_irqrestore(&ehci->lock, flags);
2378
 done:
L
Linus Torvalds 已提交
2379 2380 2381 2382 2383
	return status;
}

/*-------------------------------------------------------------------------*/

2384
static void scan_isoc(struct ehci_hcd *ehci)
L
Linus Torvalds 已提交
2385
{
2386 2387 2388 2389 2390
	unsigned		uf, now_frame, frame;
	unsigned		fmask = ehci->periodic_size - 1;
	bool			modified, live;
	union ehci_shadow	q, *q_p;
	__hc32			type, *hw_p;
L
Linus Torvalds 已提交
2391 2392 2393 2394 2395 2396

	/*
	 * 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.
	 */
2397
	if (ehci->rh_state >= EHCI_RH_RUNNING) {
2398 2399 2400
		uf = ehci_read_frame_index(ehci);
		now_frame = (uf >> 3) & fmask;
		live = true;
K
Karsten Wiese 已提交
2401
	} else  {
2402
		now_frame = (ehci->last_iso_frame - 1) & fmask;
2403
		live = false;
K
Karsten Wiese 已提交
2404
	}
2405
	ehci->now_frame = now_frame;
L
Linus Torvalds 已提交
2406

2407
	frame = ehci->last_iso_frame;
L
Linus Torvalds 已提交
2408 2409

restart:
2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
	/* Scan each element in frame's queue for completions */
	q_p = &ehci->pshadow[frame];
	hw_p = &ehci->periodic[frame];
	q.ptr = q_p->ptr;
	type = Q_NEXT_TYPE(ehci, *hw_p);
	modified = false;

	while (q.ptr != NULL) {
		switch (hc32_to_cpu(ehci, type)) {
		case Q_TYPE_ITD:
			/*
			 * If this ITD is still active, leave it for
			 * later processing ... check the next entry.
			 * No need to check for activity unless the
			 * frame is current.
			 */
			if (frame == now_frame && live) {
				rmb();
				for (uf = 0; uf < 8; uf++) {
					if (q.itd->hw_transaction[uf] &
							ITD_ACTIVE(ehci))
2431
						break;
L
Linus Torvalds 已提交
2432
				}
2433 2434 2435
				if (uf < 8) {
					q_p = &q.itd->itd_next;
					hw_p = &q.itd->hw_next;
2436
					type = Q_NEXT_TYPE(ehci,
2437
							q.itd->hw_next);
L
Linus Torvalds 已提交
2438 2439 2440
					q = *q_p;
					break;
				}
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
			}

			/*
			 * Take finished ITDs out of the schedule
			 * and process them:  recycle, maybe report
			 * URB completion.  HC won't cache the
			 * pointer for much longer, if at all.
			 */
			*q_p = q.itd->itd_next;
			if (!ehci->use_dummy_qh ||
					q.itd->hw_next != EHCI_LIST_END(ehci))
				*hw_p = q.itd->hw_next;
			else
				*hw_p = cpu_to_hc32(ehci, ehci->dummy->qh_dma);
			type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
			wmb();
			modified = itd_complete(ehci, q.itd);
			q = *q_p;
			break;
		case Q_TYPE_SITD:
			/*
			 * If this SITD is still active, leave it for
			 * later processing ... check the next entry.
			 * No need to check for activity unless the
			 * frame is current.
			 */
			if (((frame == now_frame) ||
					(((frame + 1) & fmask) == now_frame))
				&& live
				&& (q.sitd->hw_results & SITD_ACTIVE(ehci))) {
2471

2472 2473
				q_p = &q.sitd->sitd_next;
				hw_p = &q.sitd->hw_next;
2474
				type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
L
Linus Torvalds 已提交
2475 2476 2477 2478
				q = *q_p;
				break;
			}

2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493
			/*
			 * Take finished SITDs out of the schedule
			 * and process them:  recycle, maybe report
			 * URB completion.
			 */
			*q_p = q.sitd->sitd_next;
			if (!ehci->use_dummy_qh ||
					q.sitd->hw_next != EHCI_LIST_END(ehci))
				*hw_p = q.sitd->hw_next;
			else
				*hw_p = cpu_to_hc32(ehci, ehci->dummy->qh_dma);
			type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
			wmb();
			modified = sitd_complete(ehci, q.sitd);
			q = *q_p;
2494
			break;
2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505
		default:
			ehci_dbg(ehci, "corrupt type %d frame %d shadow %p\n",
					type, frame, q.ptr);
			/* BUG(); */
			/* FALL THROUGH */
		case Q_TYPE_QH:
		case Q_TYPE_FSTN:
			/* End of the iTDs and siTDs */
			q.ptr = NULL;
			break;
		}
2506

2507 2508 2509
		/* Assume completion callbacks modify the queue */
		if (unlikely(modified && ehci->isoc_count > 0))
			goto restart;
2510
	}
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520

	/* Stop when we have reached the current frame */
	if (frame == now_frame)
		return;

	/* The last frame may still have active siTDs */
	ehci->last_iso_frame = frame;
	frame = (frame + 1) & fmask;

	goto restart;
L
Linus Torvalds 已提交
2521
}