ehci-sched.c 63.5 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 37 38 39 40 41 42 43 44 45 46
 * 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.
 */

static int ehci_get_frame (struct usb_hcd *hcd);

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

/*
 * 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 *
47 48
periodic_next_shadow(struct ehci_hcd *ehci, union ehci_shadow *periodic,
		__hc32 tag)
L
Linus Torvalds 已提交
49
{
50
	switch (hc32_to_cpu(ehci, tag)) {
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62
	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;
	// case Q_TYPE_SITD:
	default:
		return &periodic->sitd->sitd_next;
	}
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76
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 已提交
77 78 79
/* caller must hold ehci->lock */
static void periodic_unlink (struct ehci_hcd *ehci, unsigned frame, void *ptr)
{
80 81
	union ehci_shadow	*prev_p = &ehci->pshadow[frame];
	__hc32			*hw_p = &ehci->periodic[frame];
L
Linus Torvalds 已提交
82 83 84 85
	union ehci_shadow	here = *prev_p;

	/* find predecessor of "ptr"; hw and shadow lists are in sync */
	while (here.ptr && here.ptr != ptr) {
86 87
		prev_p = periodic_next_shadow(ehci, prev_p,
				Q_NEXT_TYPE(ehci, *hw_p));
88 89
		hw_p = shadow_next_periodic(ehci, &here,
				Q_NEXT_TYPE(ehci, *hw_p));
L
Linus Torvalds 已提交
90 91 92 93 94 95 96 97 98
		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.
	 */
99 100
	*prev_p = *periodic_next_shadow(ehci, &here,
			Q_NEXT_TYPE(ehci, *hw_p));
101
	*hw_p = *shadow_next_periodic(ehci, &here, Q_NEXT_TYPE(ehci, *hw_p));
L
Linus Torvalds 已提交
102 103 104 105 106 107
}

/* how many of the uframe's 125 usecs are allocated? */
static unsigned short
periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe)
{
108
	__hc32			*hw_p = &ehci->periodic [frame];
L
Linus Torvalds 已提交
109 110
	union ehci_shadow	*q = &ehci->pshadow [frame];
	unsigned		usecs = 0;
111
	struct ehci_qh_hw	*hw;
L
Linus Torvalds 已提交
112 113

	while (q->ptr) {
114
		switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
L
Linus Torvalds 已提交
115
		case Q_TYPE_QH:
116
			hw = q->qh->hw;
L
Linus Torvalds 已提交
117
			/* is it in the S-mask? */
118
			if (hw->hw_info2 & cpu_to_hc32(ehci, 1 << uframe))
L
Linus Torvalds 已提交
119 120
				usecs += q->qh->usecs;
			/* ... or C-mask? */
121
			if (hw->hw_info2 & cpu_to_hc32(ehci,
122
					1 << (8 + uframe)))
L
Linus Torvalds 已提交
123
				usecs += q->qh->c_usecs;
124
			hw_p = &hw->hw_next;
L
Linus Torvalds 已提交
125 126 127 128 129 130 131
			q = &q->qh->qh_next;
			break;
		// case Q_TYPE_FSTN:
		default:
			/* for "save place" FSTNs, count the relevant INTR
			 * bandwidth from the previous frame
			 */
132
			if (q->fstn->hw_prev != EHCI_LIST_END(ehci)) {
L
Linus Torvalds 已提交
133 134 135 136 137 138
				ehci_dbg (ehci, "ignoring FSTN cost ...\n");
			}
			hw_p = &q->fstn->hw_next;
			q = &q->fstn->fstn_next;
			break;
		case Q_TYPE_ITD:
139 140
			if (q->itd->hw_transaction[uframe])
				usecs += q->itd->stream->usecs;
L
Linus Torvalds 已提交
141 142 143 144 145
			hw_p = &q->itd->hw_next;
			q = &q->itd->itd_next;
			break;
		case Q_TYPE_SITD:
			/* is it in the S-mask?  (count SPLIT, DATA) */
146 147
			if (q->sitd->hw_uframe & cpu_to_hc32(ehci,
					1 << uframe)) {
L
Linus Torvalds 已提交
148
				if (q->sitd->hw_fullspeed_ep &
149
						cpu_to_hc32(ehci, 1<<31))
L
Linus Torvalds 已提交
150 151 152 153 154 155 156
					usecs += q->sitd->stream->usecs;
				else	/* worst case for OUT start-split */
					usecs += HS_USECS_ISO (188);
			}

			/* ... C-mask?  (count CSPLIT, DATA) */
			if (q->sitd->hw_uframe &
157
					cpu_to_hc32(ehci, 1 << (8 + uframe))) {
L
Linus Torvalds 已提交
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
				/* worst case for IN complete-split */
				usecs += q->sitd->stream->c_usecs;
			}

			hw_p = &q->sitd->hw_next;
			q = &q->sitd->sitd_next;
			break;
		}
	}
#ifdef	DEBUG
	if (usecs > 100)
		ehci_err (ehci, "uframe %d sched overrun: %d usecs\n",
			frame * 8 + uframe, usecs);
#endif
	return usecs;
}

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

static int same_tt (struct usb_device *dev1, struct usb_device *dev2)
{
	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;
}

189 190 191 192 193 194 195 196 197 198
#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.
 */
199
static inline unsigned char tt_start_uframe(struct ehci_hcd *ehci, __hc32 mask)
200
{
201
	unsigned char smask = QH_SMASK & hc32_to_cpu(ehci, mask);
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	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
max_tt_usecs[] = { 125, 125, 125, 125, 125, 125, 30, 0 };

/* carryover low/fullspeed bandwidth that crosses uframe boundries */
static inline void carryover_tt_bandwidth(unsigned short tt_usecs[8])
{
	int i;
	for (i=0; i<7; i++) {
		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];
		}
	}
}

/* How many of the tt's periodic downstream 1000 usecs are allocated?
 *
 * While this measures the bandwidth in terms of usecs/uframe,
 * the low/fullspeed bus has no notion of uframes, so any particular
 * low/fullspeed transfer can "carry over" from one uframe to the next,
 * since the TT just performs downstream transfers in sequence.
 *
J
Joe Perches 已提交
232
 * For example two separate 100 usec transfers can start in the same uframe,
233 234 235 236 237 238 239 240 241 242
 * and the second one would "carry over" 75 usecs into the next uframe.
 */
static void
periodic_tt_usecs (
	struct ehci_hcd *ehci,
	struct usb_device *dev,
	unsigned frame,
	unsigned short tt_usecs[8]
)
{
243
	__hc32			*hw_p = &ehci->periodic [frame];
244 245 246 247 248 249
	union ehci_shadow	*q = &ehci->pshadow [frame];
	unsigned char		uf;

	memset(tt_usecs, 0, 16);

	while (q->ptr) {
250
		switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
251 252 253 254 255 256
		case Q_TYPE_ITD:
			hw_p = &q->itd->hw_next;
			q = &q->itd->itd_next;
			continue;
		case Q_TYPE_QH:
			if (same_tt(dev, q->qh->dev)) {
257
				uf = tt_start_uframe(ehci, q->qh->hw->hw_info2);
258 259
				tt_usecs[uf] += q->qh->tt_usecs;
			}
260
			hw_p = &q->qh->hw->hw_next;
261 262 263 264 265 266 267 268 269 270 271 272
			q = &q->qh->qh_next;
			continue;
		case Q_TYPE_SITD:
			if (same_tt(dev, q->sitd->urb->dev)) {
				uf = tt_start_uframe(ehci, q->sitd->hw_uframe);
				tt_usecs[uf] += q->sitd->stream->tt_usecs;
			}
			hw_p = &q->sitd->hw_next;
			q = &q->sitd->sitd_next;
			continue;
		// case Q_TYPE_FSTN:
		default:
273 274
			ehci_dbg(ehci, "ignoring periodic frame %d FSTN\n",
					frame);
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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
			hw_p = &q->fstn->hw_next;
			q = &q->fstn->fstn_next;
		}
	}

	carryover_tt_bandwidth(tt_usecs);

	if (max_tt_usecs[7] < tt_usecs[7])
		ehci_err(ehci, "frame %d tt sched overrun: %d usecs\n",
			frame, tt_usecs[7] - max_tt_usecs[7]);
}

/*
 * 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.
 */
static int tt_available (
	struct ehci_hcd		*ehci,
	unsigned		period,
	struct usb_device	*dev,
	unsigned		frame,
	unsigned		uframe,
	u16			usecs
)
{
	if ((period == 0) || (uframe >= 7))	/* error */
		return 0;

	for (; frame < ehci->periodic_size; frame += period) {
		unsigned short tt_usecs[8];

		periodic_tt_usecs (ehci, dev, frame, tt_usecs);

		ehci_vdbg(ehci, "tt frame %d check %d usecs start uframe %d in"
			" schedule %d/%d/%d/%d/%d/%d/%d/%d\n",
			frame, usecs, uframe,
			tt_usecs[0], tt_usecs[1], tt_usecs[2], tt_usecs[3],
			tt_usecs[4], tt_usecs[5], tt_usecs[6], tt_usecs[7]);

		if (max_tt_usecs[uframe] <= tt_usecs[uframe]) {
			ehci_vdbg(ehci, "frame %d uframe %d fully scheduled\n",
				frame, uframe);
			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
		 */
		if (125 < usecs) {
343
			int ufs = (usecs / 125);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
			int i;
			for (i = uframe; i < (uframe + ufs) && i < 8; i++)
				if (0 < tt_usecs[i]) {
					ehci_vdbg(ehci,
						"multi-uframe xfer can't fit "
						"in frame %d uframe %d\n",
						frame, i);
					return 0;
				}
		}

		tt_usecs[uframe] += usecs;

		carryover_tt_bandwidth(tt_usecs);

		/* fail if the carryover pushed bw past the last uframe's limit */
		if (max_tt_usecs[7] < tt_usecs[7]) {
			ehci_vdbg(ehci,
				"tt unavailable usecs %d frame %d uframe %d\n",
				usecs, frame, uframe);
			return 0;
		}
	}

	return 1;
}

#else

L
Linus Torvalds 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
/* 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.
 */
static int tt_no_collision (
	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;
394
		__hc32			type;
395
		struct ehci_qh_hw	*hw;
L
Linus Torvalds 已提交
396 397

		here = ehci->pshadow [frame];
398
		type = Q_NEXT_TYPE(ehci, ehci->periodic [frame]);
L
Linus Torvalds 已提交
399
		while (here.ptr) {
400
			switch (hc32_to_cpu(ehci, type)) {
L
Linus Torvalds 已提交
401
			case Q_TYPE_ITD:
402
				type = Q_NEXT_TYPE(ehci, here.itd->hw_next);
L
Linus Torvalds 已提交
403 404 405
				here = here.itd->itd_next;
				continue;
			case Q_TYPE_QH:
406
				hw = here.qh->hw;
L
Linus Torvalds 已提交
407 408 409
				if (same_tt (dev, here.qh->dev)) {
					u32		mask;

410
					mask = hc32_to_cpu(ehci,
411
							hw->hw_info2);
L
Linus Torvalds 已提交
412 413 414 415 416
					/* "knows" no gap is needed */
					mask |= mask >> 8;
					if (mask & uf_mask)
						break;
				}
417
				type = Q_NEXT_TYPE(ehci, hw->hw_next);
L
Linus Torvalds 已提交
418 419 420 421 422 423
				here = here.qh->qh_next;
				continue;
			case Q_TYPE_SITD:
				if (same_tt (dev, here.sitd->urb->dev)) {
					u16		mask;

424
					mask = hc32_to_cpu(ehci, here.sitd
L
Linus Torvalds 已提交
425 426 427 428 429 430
								->hw_uframe);
					/* FIXME assumes no gap for IN! */
					mask |= mask >> 8;
					if (mask & uf_mask)
						break;
				}
431
				type = Q_NEXT_TYPE(ehci, here.sitd->hw_next);
L
Linus Torvalds 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
				here = here.sitd->sitd_next;
				continue;
			// case Q_TYPE_FSTN:
			default:
				ehci_dbg (ehci,
					"periodic frame %d bogus type %d\n",
					frame, type);
			}

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

	/* no collision */
	return 1;
}

450 451
#endif /* CONFIG_USB_EHCI_TT_NEWSCHED */

L
Linus Torvalds 已提交
452 453 454 455 456 457 458
/*-------------------------------------------------------------------------*/

static int enable_periodic (struct ehci_hcd *ehci)
{
	u32	cmd;
	int	status;

459 460 461
	if (ehci->periodic_sched++)
		return 0;

L
Linus Torvalds 已提交
462 463 464
	/* did clearing PSE did take effect yet?
	 * takes effect only at frame boundaries...
	 */
465 466 467
	status = handshake_on_error_set_halt(ehci, &ehci->regs->status,
					     STS_PSS, 0, 9 * 125);
	if (status)
L
Linus Torvalds 已提交
468 469
		return status;

470 471
	cmd = ehci_readl(ehci, &ehci->regs->command) | CMD_PSE;
	ehci_writel(ehci, cmd, &ehci->regs->command);
L
Linus Torvalds 已提交
472 473 474 475
	/* posted write ... PSS happens later */
	ehci_to_hcd(ehci)->state = HC_STATE_RUNNING;

	/* make sure ehci_work scans these */
476 477
	ehci->next_uframe = ehci_readl(ehci, &ehci->regs->frame_index)
		% (ehci->periodic_size << 3);
478 479
	if (unlikely(ehci->broken_periodic))
		ehci->last_periodic_enable = ktime_get_real();
L
Linus Torvalds 已提交
480 481 482 483 484 485 486 487
	return 0;
}

static int disable_periodic (struct ehci_hcd *ehci)
{
	u32	cmd;
	int	status;

488 489 490
	if (--ehci->periodic_sched)
		return 0;

491 492 493 494 495 496 497 498 499 500
	if (unlikely(ehci->broken_periodic)) {
		/* delay experimentally determined */
		ktime_t safe = ktime_add_us(ehci->last_periodic_enable, 1000);
		ktime_t now = ktime_get_real();
		s64 delay = ktime_us_delta(safe, now);

		if (unlikely(delay > 0))
			udelay(delay);
	}

L
Linus Torvalds 已提交
501 502 503
	/* did setting PSE not take effect yet?
	 * takes effect only at frame boundaries...
	 */
504 505 506
	status = handshake_on_error_set_halt(ehci, &ehci->regs->status,
					     STS_PSS, STS_PSS, 9 * 125);
	if (status)
L
Linus Torvalds 已提交
507 508
		return status;

509 510
	cmd = ehci_readl(ehci, &ehci->regs->command) & ~CMD_PSE;
	ehci_writel(ehci, cmd, &ehci->regs->command);
L
Linus Torvalds 已提交
511 512
	/* posted write ... */

513
	free_cached_lists(ehci);
514

L
Linus Torvalds 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
	ehci->next_uframe = -1;
	return 0;
}

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

/* 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+)
 */
static int qh_link_periodic (struct ehci_hcd *ehci, struct ehci_qh *qh)
{
	unsigned	i;
	unsigned	period = qh->period;

	dev_dbg (&qh->dev->dev,
		"link qh%d-%04x/%p start %d [%d/%d us]\n",
534 535
		period, hc32_to_cpup(ehci, &qh->hw->hw_info2)
			& (QH_CMASK | QH_SMASK),
L
Linus Torvalds 已提交
536 537 538 539 540 541 542
		qh, qh->start, qh->usecs, qh->c_usecs);

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

	for (i = qh->start; i < ehci->periodic_size; i += period) {
543 544
		union ehci_shadow	*prev = &ehci->pshadow[i];
		__hc32			*hw_p = &ehci->periodic[i];
L
Linus Torvalds 已提交
545
		union ehci_shadow	here = *prev;
546
		__hc32			type = 0;
L
Linus Torvalds 已提交
547 548 549

		/* skip the iso nodes at list head */
		while (here.ptr) {
550 551
			type = Q_NEXT_TYPE(ehci, *hw_p);
			if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
L
Linus Torvalds 已提交
552
				break;
553
			prev = periodic_next_shadow(ehci, prev, type);
554
			hw_p = shadow_next_periodic(ehci, &here, type);
L
Linus Torvalds 已提交
555 556 557 558 559 560 561 562 563 564
			here = *prev;
		}

		/* sorting each branch by period (slow-->fast)
		 * enables sharing interior tree nodes
		 */
		while (here.ptr && qh != here.qh) {
			if (qh->period > here.qh->period)
				break;
			prev = &here.qh->qh_next;
565
			hw_p = &here.qh->hw->hw_next;
L
Linus Torvalds 已提交
566 567 568 569 570 571
			here = *prev;
		}
		/* link in this qh, unless some earlier pass did that */
		if (qh != here.qh) {
			qh->qh_next = here;
			if (here.qh)
572
				qh->hw->hw_next = *hw_p;
L
Linus Torvalds 已提交
573 574
			wmb ();
			prev->qh = qh;
575
			*hw_p = QH_NEXT (ehci, qh->qh_dma);
L
Linus Torvalds 已提交
576 577 578
		}
	}
	qh->qh_state = QH_STATE_LINKED;
579
	qh->xacterrs = 0;
L
Linus Torvalds 已提交
580 581 582 583 584 585 586 587
	qh_get (qh);

	/* update per-qh bandwidth for usbfs */
	ehci_to_hcd(ehci)->self.bandwidth_allocated += qh->period
		? ((qh->usecs + qh->c_usecs) / qh->period)
		: (qh->usecs * 8);

	/* maybe enable periodic schedule processing */
588
	return enable_periodic(ehci);
L
Linus Torvalds 已提交
589 590
}

591
static int qh_unlink_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
L
Linus Torvalds 已提交
592 593 594 595 596 597 598 599 600
{
	unsigned	i;
	unsigned	period;

	// FIXME:
	// IF this isn't high speed
	//   and this qh is active in the current uframe
	//   (and overlay token SplitXstate is false?)
	// THEN
601
	//   qh->hw_info1 |= cpu_to_hc32(1 << 7 /* "ignore" */);
L
Linus Torvalds 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

	/* high bandwidth, or otherwise part of every microframe */
	if ((period = qh->period) == 0)
		period = 1;

	for (i = qh->start; i < ehci->periodic_size; i += period)
		periodic_unlink (ehci, i, qh);

	/* update per-qh bandwidth for usbfs */
	ehci_to_hcd(ehci)->self.bandwidth_allocated -= qh->period
		? ((qh->usecs + qh->c_usecs) / qh->period)
		: (qh->usecs * 8);

	dev_dbg (&qh->dev->dev,
		"unlink qh%d-%04x/%p start %d [%d/%d us]\n",
617
		qh->period,
618
		hc32_to_cpup(ehci, &qh->hw->hw_info2) & (QH_CMASK | QH_SMASK),
L
Linus Torvalds 已提交
619 620 621 622 623 624 625 626
		qh, qh->start, qh->usecs, qh->c_usecs);

	/* qh->qh_next still "live" to HC */
	qh->qh_state = QH_STATE_UNLINK;
	qh->qh_next.ptr = NULL;
	qh_put (qh);

	/* maybe turn off periodic schedule */
627
	return disable_periodic(ehci);
L
Linus Torvalds 已提交
628 629 630 631
}

static void intr_deschedule (struct ehci_hcd *ehci, struct ehci_qh *qh)
{
632 633 634 635 636 637 638 639 640 641 642 643 644
	unsigned		wait;
	struct ehci_qh_hw	*hw = qh->hw;
	int			rc;

	/* If the QH isn't linked then there's nothing we can do
	 * unless we were called during a giveback, in which case
	 * qh_completions() has to deal with it.
	 */
	if (qh->qh_state != QH_STATE_LINKED) {
		if (qh->qh_state == QH_STATE_COMPLETING)
			qh->needs_rescan = 1;
		return;
	}
L
Linus Torvalds 已提交
645 646 647 648 649 650 651 652 653

	qh_unlink_periodic (ehci, qh);

	/* simple/paranoid:  always delay, expecting the HC needs to read
	 * qh->hw_next or finish a writeback after SPLIT/CSPLIT ... and
	 * expect khubd to clean up after any CSPLITs we won't issue.
	 * active high speed queues may need bigger delays...
	 */
	if (list_empty (&qh->qtd_list)
654
			|| (cpu_to_hc32(ehci, QH_CMASK)
655
					& hw->hw_info2) != 0)
L
Linus Torvalds 已提交
656 657 658 659 660 661
		wait = 2;
	else
		wait = 55;	/* worst case: 3 * 1024 */

	udelay (wait);
	qh->qh_state = QH_STATE_IDLE;
662
	hw->hw_next = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
663
	wmb ();
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681

	qh_completions(ehci, qh);

	/* reschedule QH iff another request is queued */
	if (!list_empty(&qh->qtd_list) &&
			HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) {
		rc = qh_schedule(ehci, qh);

		/* 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
		 */
		if (rc != 0)
			ehci_err(ehci, "can't reschedule qh %p, err %d\n",
					qh, rc);
	}
L
Linus Torvalds 已提交
682 683 684 685 686
}

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

static int check_period (
687
	struct ehci_hcd *ehci,
L
Linus Torvalds 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
	unsigned	frame,
	unsigned	uframe,
	unsigned	period,
	unsigned	usecs
) {
	int		claimed;

	/* complete split running into next frame?
	 * given FSTN support, we could sometimes check...
	 */
	if (uframe >= 8)
		return 0;

	/*
	 * 80% periodic == 100 usec/uframe available
703
	 * convert "usecs we need" to "max already claimed"
L
Linus Torvalds 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
	 */
	usecs = 100 - usecs;

	/* we "know" 2 and 4 uframe intervals were rejected; so
	 * for period 0, check _every_ microframe in the schedule.
	 */
	if (unlikely (period == 0)) {
		do {
			for (uframe = 0; uframe < 7; uframe++) {
				claimed = periodic_usecs (ehci, frame, uframe);
				if (claimed > usecs)
					return 0;
			}
		} while ((frame += 1) < ehci->periodic_size);

	/* just check the specified uframe, at that period */
	} else {
		do {
			claimed = periodic_usecs (ehci, frame, uframe);
			if (claimed > usecs)
				return 0;
		} while ((frame += period) < ehci->periodic_size);
	}

	// success!
	return 1;
}

static int check_intr_schedule (
733
	struct ehci_hcd		*ehci,
L
Linus Torvalds 已提交
734 735 736
	unsigned		frame,
	unsigned		uframe,
	const struct ehci_qh	*qh,
737
	__hc32			*c_maskp
L
Linus Torvalds 已提交
738 739
)
{
740
	int		retval = -ENOSPC;
741
	u8		mask = 0;
L
Linus Torvalds 已提交
742 743 744 745 746 747 748 749 750 751 752 753

	if (qh->c_usecs && uframe >= 6)		/* FSTN territory? */
		goto done;

	if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
		goto done;
	if (!qh->c_usecs) {
		retval = 0;
		*c_maskp = 0;
		goto done;
	}

754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
	if (tt_available (ehci, qh->period, qh->dev, frame, uframe,
				qh->tt_usecs)) {
		unsigned i;

		/* TODO : this may need FSTN for SSPLIT in uframe 5. */
		for (i=uframe+1; i<8 && i<uframe+4; i++)
			if (!check_period (ehci, frame, i,
						qh->period, qh->c_usecs))
				goto done;
			else
				mask |= 1 << i;

		retval = 0;

769
		*c_maskp = cpu_to_hc32(ehci, mask << 8);
770 771
	}
#else
L
Linus Torvalds 已提交
772 773 774
	/* 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.
775
	 *
L
Linus Torvalds 已提交
776 777 778 779
	 * NOTE:  both SPLIT and CSPLIT could be checked in just
	 * one smart pass...
	 */
	mask = 0x03 << (uframe + qh->gap_uf);
780
	*c_maskp = cpu_to_hc32(ehci, mask << 8);
L
Linus Torvalds 已提交
781 782 783 784 785 786 787 788 789 790 791

	mask |= 1 << uframe;
	if (tt_no_collision (ehci, qh->period, qh->dev, frame, mask)) {
		if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
					qh->period, qh->c_usecs))
			goto done;
		if (!check_period (ehci, frame, uframe + qh->gap_uf,
					qh->period, qh->c_usecs))
			goto done;
		retval = 0;
	}
792
#endif
L
Linus Torvalds 已提交
793 794 795 796 797 798 799
done:
	return retval;
}

/* "first fit" scheduling policy used the first time through,
 * or when the previous schedule slot can't be re-used.
 */
800
static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
L
Linus Torvalds 已提交
801
{
802
	int		status;
L
Linus Torvalds 已提交
803
	unsigned	uframe;
804
	__hc32		c_mask;
L
Linus Torvalds 已提交
805
	unsigned	frame;		/* 0..(qh->period - 1), or NO_FRAME */
806
	struct ehci_qh_hw	*hw = qh->hw;
L
Linus Torvalds 已提交
807 808

	qh_refresh(ehci, qh);
809
	hw->hw_next = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
810 811 812 813
	frame = qh->start;

	/* reuse the previous schedule slots, if we can */
	if (frame < qh->period) {
814
		uframe = ffs(hc32_to_cpup(ehci, &hw->hw_info2) & QH_SMASK);
L
Linus Torvalds 已提交
815 816 817 818 819 820 821 822 823 824 825 826 827 828
		status = check_intr_schedule (ehci, frame, --uframe,
				qh, &c_mask);
	} else {
		uframe = 0;
		c_mask = 0;
		status = -ENOSPC;
	}

	/* else scan the schedule to find a group of slots such that all
	 * uframes have enough periodic bandwidth available.
	 */
	if (status) {
		/* "normal" case, uframing flexible except with splits */
		if (qh->period) {
829 830 831 832
			int		i;

			for (i = qh->period; status && i > 0; --i) {
				frame = ++ehci->random_frame % qh->period;
L
Linus Torvalds 已提交
833 834 835 836 837 838 839
				for (uframe = 0; uframe < 8; uframe++) {
					status = check_intr_schedule (ehci,
							frame, uframe, qh,
							&c_mask);
					if (status == 0)
						break;
				}
840
			}
L
Linus Torvalds 已提交
841 842 843 844 845 846 847 848 849 850 851

		/* qh->period == 0 means every uframe */
		} else {
			frame = 0;
			status = check_intr_schedule (ehci, 0, 0, qh, &c_mask);
		}
		if (status)
			goto done;
		qh->start = frame;

		/* reset S-frame and (maybe) C-frame masks */
852 853
		hw->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
		hw->hw_info2 |= qh->period
854 855
			? cpu_to_hc32(ehci, 1 << uframe)
			: cpu_to_hc32(ehci, QH_SMASK);
856
		hw->hw_info2 |= c_mask;
L
Linus Torvalds 已提交
857 858 859 860
	} else
		ehci_dbg (ehci, "reused qh %p schedule\n", qh);

	/* stuff into the periodic schedule */
861
	status = qh_link_periodic (ehci, qh);
L
Linus Torvalds 已提交
862 863 864 865 866 867 868 869
done:
	return status;
}

static int intr_submit (
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	struct list_head	*qtd_list,
A
Al Viro 已提交
870
	gfp_t			mem_flags
L
Linus Torvalds 已提交
871 872 873 874
) {
	unsigned		epnum;
	unsigned long		flags;
	struct ehci_qh		*qh;
875
	int			status;
L
Linus Torvalds 已提交
876 877 878
	struct list_head	empty;

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

	spin_lock_irqsave (&ehci->lock, flags);

883
	if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
884
		status = -ESHUTDOWN;
885
		goto done_not_linked;
886
	}
887 888 889
	status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
	if (unlikely(status))
		goto done_not_linked;
890

L
Linus Torvalds 已提交
891 892
	/* get qh and force any scheduling errors */
	INIT_LIST_HEAD (&empty);
893
	qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv);
L
Linus Torvalds 已提交
894 895 896 897 898 899 900 901 902 903
	if (qh == NULL) {
		status = -ENOMEM;
		goto done;
	}
	if (qh->qh_state == QH_STATE_IDLE) {
		if ((status = qh_schedule (ehci, qh)) != 0)
			goto done;
	}

	/* then queue the urb's tds to the qh */
904
	qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
L
Linus Torvalds 已提交
905 906 907 908 909 910
	BUG_ON (qh == NULL);

	/* ... update usbfs periodic stats */
	ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;

done:
911 912 913
	if (unlikely(status))
		usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
done_not_linked:
L
Linus Torvalds 已提交
914 915 916 917 918 919 920 921 922 923 924 925
	spin_unlock_irqrestore (&ehci->lock, flags);
	if (status)
		qtd_list_free (ehci, urb, qtd_list);

	return status;
}

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

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

static struct ehci_iso_stream *
A
Al Viro 已提交
926
iso_stream_alloc (gfp_t mem_flags)
L
Linus Torvalds 已提交
927 928 929
{
	struct ehci_iso_stream *stream;

930
	stream = kzalloc(sizeof *stream, mem_flags);
L
Linus Torvalds 已提交
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
	if (likely (stream != NULL)) {
		INIT_LIST_HEAD(&stream->td_list);
		INIT_LIST_HEAD(&stream->free_list);
		stream->next_uframe = -1;
		stream->refcount = 1;
	}
	return stream;
}

static void
iso_stream_init (
	struct ehci_hcd		*ehci,
	struct ehci_iso_stream	*stream,
	struct usb_device	*dev,
	int			pipe,
	unsigned		interval
)
{
	static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };

	u32			buf1;
	unsigned		epnum, maxp;
	int			is_input;
	long			bandwidth;

	/*
	 * this might be a "high bandwidth" highspeed endpoint,
	 * as encoded in the ep descriptor's wMaxPacket field
	 */
	epnum = usb_pipeendpoint (pipe);
	is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
	maxp = usb_maxpacket(dev, pipe, !is_input);
	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;

979 980 981
		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 已提交
982 983 984 985 986 987

		/* usbfs wants to report the average usecs per frame tied up
		 * when transfers on this endpoint are scheduled ...
		 */
		stream->usecs = HS_USECS_ISO (maxp);
		bandwidth = stream->usecs * 8;
A
Alan Stern 已提交
988
		bandwidth /= interval;
L
Linus Torvalds 已提交
989 990 991

	} else {
		u32		addr;
D
david-b@pacbell.net 已提交
992
		int		think_time;
993
		int		hs_transfers;
L
Linus Torvalds 已提交
994 995 996 997 998 999 1000 1001 1002

		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;
		stream->usecs = HS_USECS_ISO (maxp);
D
david-b@pacbell.net 已提交
1003 1004 1005
		think_time = dev->tt ? dev->tt->think_time : 0;
		stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
				dev->speed, is_input, 1, maxp));
1006
		hs_transfers = max (1u, (maxp + 187) / 188);
L
Linus Torvalds 已提交
1007 1008 1009 1010 1011 1012 1013 1014
		if (is_input) {
			u32	tmp;

			addr |= 1 << 31;
			stream->c_usecs = stream->usecs;
			stream->usecs = HS_USECS_ISO (1);
			stream->raw_mask = 1;

1015 1016 1017
			/* c-mask as specified in USB 2.0 11.18.4 3.c */
			tmp = (1 << (hs_transfers + 2)) - 1;
			stream->raw_mask |= tmp << (8 + 2);
L
Linus Torvalds 已提交
1018
		} else
1019
			stream->raw_mask = smask_out [hs_transfers - 1];
L
Linus Torvalds 已提交
1020
		bandwidth = stream->usecs + stream->c_usecs;
A
Alan Stern 已提交
1021
		bandwidth /= interval << 3;
L
Linus Torvalds 已提交
1022 1023

		/* stream->splits gets created from raw_mask later */
1024
		stream->address = cpu_to_hc32(ehci, addr);
L
Linus Torvalds 已提交
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
	}
	stream->bandwidth = bandwidth;

	stream->udev = dev;

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

static void
iso_stream_put(struct ehci_hcd *ehci, struct ehci_iso_stream *stream)
{
	stream->refcount--;

	/* free whenever just a dev->ep reference remains.
	 * not like a QH -- no persistent state (toggle, halt)
	 */
	if (stream->refcount == 1) {
		int		is_in;

		// BUG_ON (!list_empty(&stream->td_list));

		while (!list_empty (&stream->free_list)) {
			struct list_head	*entry;

			entry = stream->free_list.next;
			list_del (entry);

			/* knows about ITD vs SITD */
			if (stream->highspeed) {
				struct ehci_itd		*itd;

				itd = list_entry (entry, struct ehci_itd,
						itd_list);
				dma_pool_free (ehci->itd_pool, itd,
						itd->itd_dma);
			} else {
				struct ehci_sitd	*sitd;

				sitd = list_entry (entry, struct ehci_sitd,
						sitd_list);
				dma_pool_free (ehci->sitd_pool, sitd,
						sitd->sitd_dma);
			}
		}

		is_in = (stream->bEndpointAddress & USB_DIR_IN) ? 0x10 : 0;
		stream->bEndpointAddress &= 0x0f;
K
Karsten Wiese 已提交
1074 1075
		if (stream->ep)
			stream->ep->hcpriv = NULL;
L
Linus Torvalds 已提交
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124

		if (stream->rescheduled) {
			ehci_info (ehci, "ep%d%s-iso rescheduled "
				"%lu times in %lu seconds\n",
				stream->bEndpointAddress, is_in ? "in" : "out",
				stream->rescheduled,
				((jiffies - stream->start)/HZ)
				);
		}

		kfree(stream);
	}
}

static inline struct ehci_iso_stream *
iso_stream_get (struct ehci_iso_stream *stream)
{
	if (likely (stream != NULL))
		stream->refcount++;
	return stream;
}

static struct ehci_iso_stream *
iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
{
	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];

	spin_lock_irqsave (&ehci->lock, flags);
	stream = ep->hcpriv;

	if (unlikely (stream == NULL)) {
		stream = iso_stream_alloc(GFP_ATOMIC);
		if (likely (stream != NULL)) {
			/* dev->ep owns the initial refcount */
			ep->hcpriv = stream;
			stream->ep = ep;
			iso_stream_init(ehci, stream, urb->dev, urb->pipe,
					urb->interval);
		}

1125 1126
	/* if dev->ep [epnum] is a QH, hw is set */
	} else if (unlikely (stream->hw != NULL)) {
L
Linus Torvalds 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
		ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
			urb->dev->devpath, epnum,
			usb_pipein(urb->pipe) ? "in" : "out");
		stream = NULL;
	}

	/* caller guarantees an eventual matching iso_stream_put */
	stream = iso_stream_get (stream);

	spin_unlock_irqrestore (&ehci->lock, flags);
	return stream;
}

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

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

static struct ehci_iso_sched *
A
Al Viro 已提交
1145
iso_sched_alloc (unsigned packets, gfp_t mem_flags)
L
Linus Torvalds 已提交
1146 1147 1148 1149 1150
{
	struct ehci_iso_sched	*iso_sched;
	int			size = sizeof *iso_sched;

	size += packets * sizeof (struct ehci_iso_packet);
1151
	iso_sched = kzalloc(size, mem_flags);
L
Linus Torvalds 已提交
1152 1153 1154 1155 1156 1157 1158
	if (likely (iso_sched != NULL)) {
		INIT_LIST_HEAD (&iso_sched->td_list);
	}
	return iso_sched;
}

static inline void
1159 1160
itd_sched_init(
	struct ehci_hcd		*ehci,
L
Linus Torvalds 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
	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 */
	iso_sched->span = urb->number_of_packets * stream->interval;

	/* 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++) {
		struct ehci_iso_packet	*uframe = &iso_sched->packet [i];
		unsigned		length;
		dma_addr_t		buf;
		u32			trans;

		length = urb->iso_frame_desc [i].length;
		buf = dma + urb->iso_frame_desc [i].offset;

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

1192
		/* might need to cross a buffer page within a uframe */
L
Linus Torvalds 已提交
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
		uframe->bufp = (buf & ~(u64)0x0fff);
		buf += length;
		if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
			uframe->cross = 1;
	}
}

static void
iso_sched_free (
	struct ehci_iso_stream	*stream,
	struct ehci_iso_sched	*iso_sched
)
{
	if (!iso_sched)
		return;
	// caller must hold ehci->lock!
	list_splice (&iso_sched->td_list, &stream->free_list);
	kfree (iso_sched);
}

static int
itd_urb_transaction (
	struct ehci_iso_stream	*stream,
	struct ehci_hcd		*ehci,
	struct urb		*urb,
A
Al Viro 已提交
1218
	gfp_t			mem_flags
L
Linus Torvalds 已提交
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
)
{
	struct ehci_itd		*itd;
	dma_addr_t		itd_dma;
	int			i;
	unsigned		num_itds;
	struct ehci_iso_sched	*sched;
	unsigned long		flags;

	sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
	if (unlikely (sched == NULL))
		return -ENOMEM;

1232
	itd_sched_init(ehci, sched, stream, urb);
L
Linus Torvalds 已提交
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249

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

	/* allocate/init ITDs */
	spin_lock_irqsave (&ehci->lock, flags);
	for (i = 0; i < num_itds; i++) {

		/* free_list.next might be cache-hot ... but maybe
		 * the HC caches it too. avoid that issue for now.
		 */

		/* prefer previously-allocated itds */
		if (likely (!list_empty(&stream->free_list))) {
			itd = list_entry (stream->free_list.prev,
1250
					struct ehci_itd, itd_list);
L
Linus Torvalds 已提交
1251 1252
			list_del (&itd->itd_list);
			itd_dma = itd->itd_dma;
1253
		} else {
L
Linus Torvalds 已提交
1254 1255 1256 1257
			spin_unlock_irqrestore (&ehci->lock, flags);
			itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
					&itd_dma);
			spin_lock_irqsave (&ehci->lock, flags);
1258 1259 1260 1261 1262
			if (!itd) {
				iso_sched_free(stream, sched);
				spin_unlock_irqrestore(&ehci->lock, flags);
				return -ENOMEM;
			}
L
Linus Torvalds 已提交
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
		}

		memset (itd, 0, sizeof *itd);
		itd->itd_dma = itd_dma;
		list_add (&itd->itd_list, &sched->td_list);
	}
	spin_unlock_irqrestore (&ehci->lock, flags);

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

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

static inline int
itd_slot_ok (
	struct ehci_hcd		*ehci,
	u32			mod,
	u32			uframe,
	u8			usecs,
	u32			period
)
{
	uframe %= period;
	do {
		/* can't commit more than 80% periodic == 100 usec */
		if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
				> (100 - usecs))
			return 0;

		/* we know urb->interval is 2^N uframes */
		uframe += period;
	} while (uframe < mod);
	return 1;
}

static inline int
sitd_slot_ok (
	struct ehci_hcd		*ehci,
	u32			mod,
	struct ehci_iso_stream	*stream,
	u32			uframe,
	struct ehci_iso_sched	*sched,
	u32			period_uframes
)
{
	u32			mask, tmp;
	u32			frame, uf;

	mask = stream->raw_mask << (uframe & 7);

	/* for IN, don't wrap CSPLIT into the next frame */
	if (mask & ~0xffff)
		return 0;

	/* this multi-pass logic is simple, but performance may
	 * suffer when the schedule data isn't cached.
	 */

	/* check bandwidth */
	uframe %= period_uframes;
	do {
		u32		max_used;

		frame = uframe >> 3;
		uf = uframe & 7;

1332 1333 1334 1335 1336 1337 1338 1339
#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
		/* The tt's fullspeed bus bandwidth must be available.
		 * tt_available scheduling guarantees 10+% for control/bulk.
		 */
		if (!tt_available (ehci, period_uframes << 3,
				stream->udev, frame, uf, stream->tt_usecs))
			return 0;
#else
L
Linus Torvalds 已提交
1340 1341 1342 1343 1344 1345
		/* tt must be idle for start(s), any gap, and csplit.
		 * assume scheduling slop leaves 10+% for control/bulk.
		 */
		if (!tt_no_collision (ehci, period_uframes << 3,
				stream->udev, frame, mask))
			return 0;
1346
#endif
L
Linus Torvalds 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356

		/* check starts (OUT uses more than one) */
		max_used = 100 - stream->usecs;
		for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
			if (periodic_usecs (ehci, frame, uf) > max_used)
				return 0;
		}

		/* for IN, check CSPLIT */
		if (stream->c_usecs) {
1357
			uf = uframe & 7;
L
Linus Torvalds 已提交
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
			max_used = 100 - stream->c_usecs;
			do {
				tmp = 1 << uf;
				tmp <<= 8;
				if ((stream->raw_mask & tmp) == 0)
					continue;
				if (periodic_usecs (ehci, frame, uf)
						> max_used)
					return 0;
			} while (++uf < 8);
		}

		/* we know urb->interval is 2^N uframes */
		uframe += period_uframes;
	} while (uframe < mod);

1374
	stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));
L
Linus Torvalds 已提交
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
	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!
 */

1389
#define SCHEDULE_SLOP	80	/* microframes */
L
Linus Torvalds 已提交
1390 1391 1392 1393 1394 1395 1396 1397

static int
iso_stream_schedule (
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	struct ehci_iso_stream	*stream
)
{
1398
	u32			now, next, start, period;
L
Linus Torvalds 已提交
1399 1400 1401 1402
	int			status;
	unsigned		mod = ehci->periodic_size << 3;
	struct ehci_iso_sched	*sched = urb->hcpriv;

1403
	if (sched->span > (mod - SCHEDULE_SLOP)) {
L
Linus Torvalds 已提交
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
		ehci_dbg (ehci, "iso request %p too long\n", urb);
		status = -EFBIG;
		goto fail;
	}

	if ((stream->depth + sched->span) > mod) {
		ehci_dbg (ehci, "request %p would overflow (%d+%d>%d)\n",
			urb, stream->depth, sched->span, mod);
		status = -EFBIG;
		goto fail;
	}

1416 1417 1418 1419
	period = urb->interval;
	if (!stream->highspeed)
		period <<= 3;

1420
	now = ehci_readl(ehci, &ehci->regs->frame_index) % mod;
L
Linus Torvalds 已提交
1421

1422 1423 1424 1425
	/* Typical case: reuse current schedule, stream is still active.
	 * Hopefully there are no gaps from the host falling behind
	 * (irq delays etc), but if there are we'll take the next
	 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
L
Linus Torvalds 已提交
1426 1427 1428
	 */
	if (likely (!list_empty (&stream->td_list))) {
		start = stream->next_uframe;
1429 1430

		/* For high speed devices, allow scheduling within the
A
Alan Stern 已提交
1431 1432 1433
		 * isochronous scheduling threshold.  For full speed devices
		 * and Intel PCI-based controllers, don't (work around for
		 * Intel ICH9 bug).
1434
		 */
A
Alan Stern 已提交
1435
		if (!stream->highspeed && ehci->fs_i_thresh)
1436 1437 1438
			next = now + ehci->i_thresh;
		else
			next = now;
1439 1440

		/* Fell behind (by up to twice the slop amount)? */
1441 1442
		if (((start - next) & (mod - 1)) >=
				mod - 2 * SCHEDULE_SLOP)
1443
			start += period * DIV_ROUND_UP(
1444 1445
					(next - start) & (mod - 1),
					period);
1446 1447

		/* Tried to schedule too far into the future? */
1448 1449
		if (unlikely(((start - now) & (mod - 1)) + sched->span
					>= mod - 2 * SCHEDULE_SLOP)) {
1450 1451 1452
			status = -EFBIG;
			goto fail;
		}
1453
		stream->next_uframe = start;
1454
		goto ready;
L
Linus Torvalds 已提交
1455 1456 1457 1458 1459 1460 1461 1462
	}

	/* need to schedule; when's the next (u)frame we could start?
	 * this is bigger than ehci->i_thresh allows; scheduling itself
	 * isn't free, the slop should handle reasonably slow cpus.  it
	 * can also help high bandwidth if the dma and irq loads don't
	 * jump until after the queue is primed.
	 */
1463
	start = SCHEDULE_SLOP + (now & ~0x07);
L
Linus Torvalds 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
	start %= mod;
	stream->next_uframe = start;

	/* NOTE:  assumes URB_ISO_ASAP, to limit complexity/bugs */

	/* find a uframe slot with enough bandwidth */
	for (; start < (stream->next_uframe + period); start++) {
		int		enough_space;

		/* check schedule: enough space? */
		if (stream->highspeed)
			enough_space = itd_slot_ok (ehci, mod, start,
					stream->usecs, period);
		else {
			if ((start % 8) >= 6)
				continue;
			enough_space = sitd_slot_ok (ehci, mod, stream,
					start, sched, period);
		}

		/* schedule it here if there's enough bandwidth */
		if (enough_space) {
			stream->next_uframe = start % mod;
			goto ready;
		}
	}

	/* no room in the schedule */
	ehci_dbg (ehci, "iso %ssched full %p (now %d max %d)\n",
		list_empty (&stream->td_list) ? "" : "re",
1494
		urb, now, now + mod);
L
Linus Torvalds 已提交
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
	status = -ENOSPC;

fail:
	iso_sched_free (stream, sched);
	urb->hcpriv = NULL;
	return status;

ready:
	/* report high speed start in uframes; full speed, in frames */
	urb->start_frame = stream->next_uframe;
	if (!stream->highspeed)
		urb->start_frame >>= 3;
	return 0;
}

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

static inline void
1513 1514
itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
		struct ehci_itd *itd)
L
Linus Torvalds 已提交
1515 1516 1517
{
	int i;

1518
	/* it's been recently zeroed */
1519
	itd->hw_next = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
	itd->hw_bufp [0] = stream->buf0;
	itd->hw_bufp [1] = stream->buf1;
	itd->hw_bufp [2] = stream->buf2;

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

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

static inline void
1531 1532
itd_patch(
	struct ehci_hcd		*ehci,
L
Linus Torvalds 已提交
1533 1534 1535
	struct ehci_itd		*itd,
	struct ehci_iso_sched	*iso_sched,
	unsigned		index,
1536
	u16			uframe
L
Linus Torvalds 已提交
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
)
{
	struct ehci_iso_packet	*uf = &iso_sched->packet [index];
	unsigned		pg = itd->pg;

	// BUG_ON (pg == 6 && uf->cross);

	uframe &= 0x07;
	itd->index [uframe] = index;

1547 1548 1549 1550
	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 已提交
1551 1552

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

L
Linus Torvalds 已提交
1556
		itd->pg = ++pg;
1557 1558
		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 已提交
1559 1560 1561 1562 1563 1564
	}
}

static inline void
itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
{
C
Clemens Ladisch 已提交
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
	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 已提交
1583 1584
	itd->frame = frame;
	wmb ();
C
Clemens Ladisch 已提交
1585
	*hw_p = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
L
Linus Torvalds 已提交
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
}

/* fit urb's itds into the selected schedule slot; activate as needed */
static int
itd_link_urb (
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	unsigned		mod,
	struct ehci_iso_stream	*stream
)
{
1597
	int			packet;
L
Linus Torvalds 已提交
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
	unsigned		next_uframe, uframe, frame;
	struct ehci_iso_sched	*iso_sched = urb->hcpriv;
	struct ehci_itd		*itd;

	next_uframe = stream->next_uframe % mod;

	if (unlikely (list_empty(&stream->td_list))) {
		ehci_to_hcd(ehci)->self.bandwidth_allocated
				+= stream->bandwidth;
		ehci_vdbg (ehci,
			"schedule devp %s ep%d%s-iso period %d start %d.%d\n",
			urb->dev->devpath, stream->bEndpointAddress & 0x0f,
			(stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
			urb->interval,
			next_uframe >> 3, next_uframe & 0x7);
		stream->start = jiffies;
	}
	ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;

	/* fill iTDs uframe by uframe */
	for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
		if (itd == NULL) {
			/* ASSERT:  we have all necessary itds */
			// BUG_ON (list_empty (&iso_sched->td_list));

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

			itd = list_entry (iso_sched->td_list.next,
					struct ehci_itd, itd_list);
			list_move_tail (&itd->itd_list, &stream->td_list);
			itd->stream = iso_stream_get (stream);
1629
			itd->urb = urb;
1630
			itd_init (ehci, stream, itd);
L
Linus Torvalds 已提交
1631 1632 1633 1634 1635
		}

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

1636
		itd_patch(ehci, itd, iso_sched, packet, uframe);
L
Linus Torvalds 已提交
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656

		next_uframe += stream->interval;
		stream->depth += stream->interval;
		next_uframe %= mod;
		packet++;

		/* link completed itds into the schedule */
		if (((next_uframe >> 3) != frame)
				|| packet == urb->number_of_packets) {
			itd_link (ehci, frame % ehci->periodic_size, itd);
			itd = NULL;
		}
	}
	stream->next_uframe = next_uframe;

	/* don't need that schedule data any more */
	iso_sched_free (stream, iso_sched);
	urb->hcpriv = NULL;

	timer_action (ehci, TIMER_IO_WATCHDOG);
1657
	return enable_periodic(ehci);
L
Linus Torvalds 已提交
1658 1659 1660 1661
}

#define	ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)

1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
/* 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...
 */
L
Linus Torvalds 已提交
1672 1673 1674
static unsigned
itd_complete (
	struct ehci_hcd	*ehci,
1675
	struct ehci_itd	*itd
L
Linus Torvalds 已提交
1676 1677 1678 1679 1680 1681 1682 1683
) {
	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;
1684
	unsigned				retval = false;
L
Linus Torvalds 已提交
1685 1686 1687 1688 1689 1690 1691 1692

	/* for each uframe with a packet */
	for (uframe = 0; uframe < 8; uframe++) {
		if (likely (itd->index[uframe] == -1))
			continue;
		urb_index = itd->index[uframe];
		desc = &urb->iso_frame_desc [urb_index];

1693
		t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
L
Linus Torvalds 已提交
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
		itd->hw_transaction [uframe] = 0;
		stream->depth -= stream->interval;

		/* report transfer status */
		if (unlikely (t & ISO_ERRS)) {
			urb->error_count++;
			if (t & EHCI_ISOC_BUF_ERR)
				desc->status = usb_pipein (urb->pipe)
					? -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 */
1710 1711 1712 1713
			if (!(t & EHCI_ISOC_BABBLE)) {
				desc->actual_length = EHCI_ITD_LENGTH(t);
				urb->actual_length += desc->actual_length;
			}
L
Linus Torvalds 已提交
1714 1715
		} else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
			desc->status = 0;
1716 1717
			desc->actual_length = EHCI_ITD_LENGTH(t);
			urb->actual_length += desc->actual_length;
1718 1719 1720
		} else {
			/* URB was too late */
			desc->status = -EXDEV;
L
Linus Torvalds 已提交
1721 1722 1723 1724 1725
		}
	}

	/* handle completion now? */
	if (likely ((urb_index + 1) != urb->number_of_packets))
1726
		goto done;
L
Linus Torvalds 已提交
1727 1728 1729 1730 1731 1732

	/* 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);
	 */

1733
	/* give urb back to the driver; completion often (re)submits */
1734
	dev = urb->dev;
1735
	ehci_urb_done(ehci, urb, 0);
1736
	retval = true;
L
Linus Torvalds 已提交
1737
	urb = NULL;
1738
	(void) disable_periodic(ehci);
L
Linus Torvalds 已提交
1739 1740
	ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;

1741
	if (unlikely(list_is_singular(&stream->td_list))) {
L
Linus Torvalds 已提交
1742 1743 1744 1745 1746 1747 1748 1749
		ehci_to_hcd(ehci)->self.bandwidth_allocated
				-= stream->bandwidth;
		ehci_vdbg (ehci,
			"deschedule devp %s ep%d%s-iso\n",
			dev->devpath, stream->bEndpointAddress & 0x0f,
			(stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
	}
	iso_stream_put (ehci, stream);
K
Karsten Wiese 已提交
1750

1751 1752
done:
	itd->urb = NULL;
K
Karsten Wiese 已提交
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
	if (ehci->clock_frame != itd->frame || itd->index[7] != -1) {
		/* OK to recycle this ITD now. */
		itd->stream = NULL;
		list_move(&itd->itd_list, &stream->free_list);
		iso_stream_put(ehci, stream);
	} else {
		/* HW might remember this ITD, so we can't recycle it yet.
		 * Move it to a safe place until a new frame starts.
		 */
		list_move(&itd->itd_list, &ehci->cached_itd_list);
		if (stream->refcount == 2) {
			/* If iso_stream_put() were called here, stream
			 * would be freed.  Instead, just prevent reuse.
			 */
			stream->ep->hcpriv = NULL;
			stream->ep = NULL;
		}
	}
1771
	return retval;
L
Linus Torvalds 已提交
1772 1773 1774 1775
}

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

1776
static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
A
Al Viro 已提交
1777
	gfp_t mem_flags)
L
Linus Torvalds 已提交
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
{
	int			status = -EINVAL;
	unsigned long		flags;
	struct ehci_iso_stream	*stream;

	/* Get iso_stream head */
	stream = iso_stream_find (ehci, urb);
	if (unlikely (stream == NULL)) {
		ehci_dbg (ehci, "can't get iso stream\n");
		return -ENOMEM;
	}
	if (unlikely (urb->interval != stream->interval)) {
		ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
			stream->interval, urb->interval);
		goto done;
	}

#ifdef EHCI_URB_TRACE
	ehci_dbg (ehci,
		"%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
1798
		__func__, urb->dev->devpath, urb,
L
Linus Torvalds 已提交
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
		usb_pipeendpoint (urb->pipe),
		usb_pipein (urb->pipe) ? "in" : "out",
		urb->transfer_buffer_length,
		urb->number_of_packets, urb->interval,
		stream);
#endif

	/* allocate ITDs w/o locking anything */
	status = itd_urb_transaction (stream, ehci, urb, mem_flags);
	if (unlikely (status < 0)) {
		ehci_dbg (ehci, "can't init itds\n");
		goto done;
	}

	/* schedule ... need to lock */
	spin_lock_irqsave (&ehci->lock, flags);
1815
	if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
1816
		status = -ESHUTDOWN;
1817 1818 1819 1820 1821 1822
		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);
1823
	if (likely (status == 0))
L
Linus Torvalds 已提交
1824
		itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
1825 1826 1827
	else
		usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
done_not_linked:
L
Linus Torvalds 已提交
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
	spin_unlock_irqrestore (&ehci->lock, flags);

done:
	if (unlikely (status < 0))
		iso_stream_put (ehci, stream);
	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
1844 1845
sitd_sched_init(
	struct ehci_hcd		*ehci,
L
Linus Torvalds 已提交
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
	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 */
	iso_sched->span = urb->number_of_packets * stream->interval;

	/* 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++) {
		struct ehci_iso_packet	*packet = &iso_sched->packet [i];
		unsigned		length;
		dma_addr_t		buf;
		u32			trans;

		length = urb->iso_frame_desc [i].length & 0x03ff;
		buf = dma + urb->iso_frame_desc [i].offset;

		trans = SITD_STS_ACTIVE;
		if (((i + 1) == urb->number_of_packets)
				&& !(urb->transfer_flags & URB_NO_INTERRUPT))
			trans |= SITD_IOC;
		trans |= length << 16;
1874
		packet->transaction = cpu_to_hc32(ehci, trans);
L
Linus Torvalds 已提交
1875 1876 1877 1878 1879 1880 1881

		/* 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;

1882
		/* OUT uses multiple start-splits */
L
Linus Torvalds 已提交
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
		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
sitd_urb_transaction (
	struct ehci_iso_stream	*stream,
	struct ehci_hcd		*ehci,
	struct urb		*urb,
A
Al Viro 已提交
1897
	gfp_t			mem_flags
L
Linus Torvalds 已提交
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
)
{
	struct ehci_sitd	*sitd;
	dma_addr_t		sitd_dma;
	int			i;
	struct ehci_iso_sched	*iso_sched;
	unsigned long		flags;

	iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
	if (iso_sched == NULL)
		return -ENOMEM;

1910
	sitd_sched_init(ehci, iso_sched, stream, urb);
L
Linus Torvalds 已提交
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930

	/* allocate/init sITDs */
	spin_lock_irqsave (&ehci->lock, flags);
	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.
		 */

		/* free_list.next might be cache-hot ... but maybe
		 * the HC caches it too. avoid that issue for now.
		 */

		/* prefer previously-allocated sitds */
		if (!list_empty(&stream->free_list)) {
			sitd = list_entry (stream->free_list.prev,
					 struct ehci_sitd, sitd_list);
			list_del (&sitd->sitd_list);
			sitd_dma = sitd->sitd_dma;
1931
		} else {
L
Linus Torvalds 已提交
1932 1933 1934 1935
			spin_unlock_irqrestore (&ehci->lock, flags);
			sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
					&sitd_dma);
			spin_lock_irqsave (&ehci->lock, flags);
1936 1937 1938 1939 1940
			if (!sitd) {
				iso_sched_free(stream, iso_sched);
				spin_unlock_irqrestore(&ehci->lock, flags);
				return -ENOMEM;
			}
L
Linus Torvalds 已提交
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
		}

		memset (sitd, 0, sizeof *sitd);
		sitd->sitd_dma = sitd_dma;
		list_add (&sitd->sitd_list, &iso_sched->td_list);
	}

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

	spin_unlock_irqrestore (&ehci->lock, flags);
	return 0;
}

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

static inline void
1959 1960
sitd_patch(
	struct ehci_hcd		*ehci,
L
Linus Torvalds 已提交
1961 1962 1963 1964 1965 1966 1967 1968 1969
	struct ehci_iso_stream	*stream,
	struct ehci_sitd	*sitd,
	struct ehci_iso_sched	*iso_sched,
	unsigned		index
)
{
	struct ehci_iso_packet	*uf = &iso_sched->packet [index];
	u64			bufp = uf->bufp;

1970
	sitd->hw_next = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
1971 1972 1973
	sitd->hw_fullspeed_ep = stream->address;
	sitd->hw_uframe = stream->splits;
	sitd->hw_results = uf->transaction;
1974
	sitd->hw_backpointer = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
1975 1976

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

1980
	sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
L
Linus Torvalds 已提交
1981 1982
	if (uf->cross)
		bufp += 4096;
1983
	sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
L
Linus Torvalds 已提交
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
	sitd->index = index;
}

static inline void
sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
{
	/* note: sitd ordering could matter (CSPLIT then SSPLIT) */
	sitd->sitd_next = ehci->pshadow [frame];
	sitd->hw_next = ehci->periodic [frame];
	ehci->pshadow [frame].sitd = sitd;
	sitd->frame = frame;
	wmb ();
1996
	ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
L
Linus Torvalds 已提交
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
}

/* fit urb's sitds into the selected schedule slot; activate as needed */
static int
sitd_link_urb (
	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;

	if (list_empty(&stream->td_list)) {
		/* usbfs ignores TT bandwidth */
		ehci_to_hcd(ehci)->self.bandwidth_allocated
				+= stream->bandwidth;
		ehci_vdbg (ehci,
			"sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
			urb->dev->devpath, stream->bEndpointAddress & 0x0f,
			(stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
			(next_uframe >> 3) % ehci->periodic_size,
2024
			stream->interval, hc32_to_cpu(ehci, stream->splits));
L
Linus Torvalds 已提交
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
		stream->start = jiffies;
	}
	ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;

	/* fill sITDs frame by frame */
	for (packet = 0, sitd = NULL;
			packet < urb->number_of_packets;
			packet++) {

		/* ASSERT:  we have all necessary sitds */
		BUG_ON (list_empty (&sched->td_list));

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

		sitd = list_entry (sched->td_list.next,
				struct ehci_sitd, sitd_list);
		list_move_tail (&sitd->sitd_list, &stream->td_list);
		sitd->stream = iso_stream_get (stream);
2043
		sitd->urb = urb;
L
Linus Torvalds 已提交
2044

2045
		sitd_patch(ehci, stream, sitd, sched, packet);
L
Linus Torvalds 已提交
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058
		sitd_link (ehci, (next_uframe >> 3) % ehci->periodic_size,
				sitd);

		next_uframe += stream->interval << 3;
		stream->depth += stream->interval << 3;
	}
	stream->next_uframe = next_uframe % mod;

	/* don't need that schedule data any more */
	iso_sched_free (stream, sched);
	urb->hcpriv = NULL;

	timer_action (ehci, TIMER_IO_WATCHDOG);
2059
	return enable_periodic(ehci);
L
Linus Torvalds 已提交
2060 2061 2062 2063 2064
}

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

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

2067 2068 2069 2070 2071 2072 2073 2074 2075 2076
/* 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...
 */
L
Linus Torvalds 已提交
2077 2078 2079
static unsigned
sitd_complete (
	struct ehci_hcd		*ehci,
2080
	struct ehci_sitd	*sitd
L
Linus Torvalds 已提交
2081 2082 2083 2084 2085 2086 2087
) {
	struct urb				*urb = sitd->urb;
	struct usb_iso_packet_descriptor	*desc;
	u32					t;
	int					urb_index = -1;
	struct ehci_iso_stream			*stream = sitd->stream;
	struct usb_device			*dev;
2088
	unsigned				retval = false;
L
Linus Torvalds 已提交
2089 2090 2091

	urb_index = sitd->index;
	desc = &urb->iso_frame_desc [urb_index];
2092
	t = hc32_to_cpup(ehci, &sitd->hw_results);
L
Linus Torvalds 已提交
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106

	/* report transfer status */
	if (t & SITD_ERRS) {
		urb->error_count++;
		if (t & SITD_STS_DBE)
			desc->status = usb_pipein (urb->pipe)
				? -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;
	} else {
		desc->status = 0;
2107 2108
		desc->actual_length = desc->length - SITD_LENGTH(t);
		urb->actual_length += desc->actual_length;
L
Linus Torvalds 已提交
2109 2110 2111 2112 2113
	}
	stream->depth -= stream->interval << 3;

	/* handle completion now? */
	if ((urb_index + 1) != urb->number_of_packets)
2114
		goto done;
L
Linus Torvalds 已提交
2115 2116 2117 2118 2119 2120

	/* 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);
	 */

2121
	/* give urb back to the driver; completion often (re)submits */
2122
	dev = urb->dev;
2123
	ehci_urb_done(ehci, urb, 0);
2124
	retval = true;
L
Linus Torvalds 已提交
2125
	urb = NULL;
2126
	(void) disable_periodic(ehci);
L
Linus Torvalds 已提交
2127 2128
	ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;

2129
	if (list_is_singular(&stream->td_list)) {
L
Linus Torvalds 已提交
2130 2131 2132 2133 2134 2135 2136 2137
		ehci_to_hcd(ehci)->self.bandwidth_allocated
				-= stream->bandwidth;
		ehci_vdbg (ehci,
			"deschedule devp %s ep%d%s-iso\n",
			dev->devpath, stream->bEndpointAddress & 0x0f,
			(stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
	}
	iso_stream_put (ehci, stream);
2138

2139 2140
done:
	sitd->urb = NULL;
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
	if (ehci->clock_frame != sitd->frame) {
		/* OK to recycle this SITD now. */
		sitd->stream = NULL;
		list_move(&sitd->sitd_list, &stream->free_list);
		iso_stream_put(ehci, stream);
	} else {
		/* HW might remember this SITD, so we can't recycle it yet.
		 * Move it to a safe place until a new frame starts.
		 */
		list_move(&sitd->sitd_list, &ehci->cached_sitd_list);
		if (stream->refcount == 2) {
			/* If iso_stream_put() were called here, stream
			 * would be freed.  Instead, just prevent reuse.
			 */
			stream->ep->hcpriv = NULL;
			stream->ep = NULL;
		}
	}
2159
	return retval;
L
Linus Torvalds 已提交
2160 2161 2162
}


2163
static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
A
Al Viro 已提交
2164
	gfp_t mem_flags)
L
Linus Torvalds 已提交
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
{
	int			status = -EINVAL;
	unsigned long		flags;
	struct ehci_iso_stream	*stream;

	/* Get iso_stream head */
	stream = iso_stream_find (ehci, urb);
	if (stream == NULL) {
		ehci_dbg (ehci, "can't get iso stream\n");
		return -ENOMEM;
	}
	if (urb->interval != stream->interval) {
		ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
			stream->interval, urb->interval);
		goto done;
	}

#ifdef EHCI_URB_TRACE
	ehci_dbg (ehci,
		"submit %p dev%s ep%d%s-iso len %d\n",
		urb, urb->dev->devpath,
		usb_pipeendpoint (urb->pipe),
		usb_pipein (urb->pipe) ? "in" : "out",
		urb->transfer_buffer_length);
#endif

	/* allocate SITDs */
	status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
	if (status < 0) {
		ehci_dbg (ehci, "can't init sitds\n");
		goto done;
	}

	/* schedule ... need to lock */
	spin_lock_irqsave (&ehci->lock, flags);
2200
	if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
2201
		status = -ESHUTDOWN;
2202 2203 2204 2205 2206 2207
		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);
2208
	if (status == 0)
L
Linus Torvalds 已提交
2209
		sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
2210 2211 2212
	else
		usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
done_not_linked:
L
Linus Torvalds 已提交
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
	spin_unlock_irqrestore (&ehci->lock, flags);

done:
	if (status < 0)
		iso_stream_put (ehci, stream);
	return status;
}

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

2223
static void free_cached_lists(struct ehci_hcd *ehci)
K
Karsten Wiese 已提交
2224 2225
{
	struct ehci_itd *itd, *n;
2226
	struct ehci_sitd *sitd, *sn;
K
Karsten Wiese 已提交
2227 2228 2229 2230 2231 2232 2233

	list_for_each_entry_safe(itd, n, &ehci->cached_itd_list, itd_list) {
		struct ehci_iso_stream	*stream = itd->stream;
		itd->stream = NULL;
		list_move(&itd->itd_list, &stream->free_list);
		iso_stream_put(ehci, stream);
	}
2234 2235 2236 2237 2238 2239 2240

	list_for_each_entry_safe(sitd, sn, &ehci->cached_sitd_list, sitd_list) {
		struct ehci_iso_stream	*stream = sitd->stream;
		sitd->stream = NULL;
		list_move(&sitd->sitd_list, &stream->free_list);
		iso_stream_put(ehci, stream);
	}
K
Karsten Wiese 已提交
2241 2242 2243 2244
}

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

L
Linus Torvalds 已提交
2245
static void
2246
scan_periodic (struct ehci_hcd *ehci)
L
Linus Torvalds 已提交
2247
{
2248
	unsigned	now_uframe, frame, clock, clock_frame, mod;
L
Linus Torvalds 已提交
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258
	unsigned	modified;

	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.
	 */
	now_uframe = ehci->next_uframe;
K
Karsten Wiese 已提交
2259
	if (HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) {
2260
		clock = ehci_readl(ehci, &ehci->regs->frame_index);
K
Karsten Wiese 已提交
2261 2262
		clock_frame = (clock >> 3) % ehci->periodic_size;
	} else  {
L
Linus Torvalds 已提交
2263
		clock = now_uframe + mod - 1;
K
Karsten Wiese 已提交
2264 2265 2266
		clock_frame = -1;
	}
	if (ehci->clock_frame != clock_frame) {
2267
		free_cached_lists(ehci);
K
Karsten Wiese 已提交
2268 2269
		ehci->clock_frame = clock_frame;
	}
L
Linus Torvalds 已提交
2270
	clock %= mod;
2271
	clock_frame = clock >> 3;
L
Linus Torvalds 已提交
2272 2273 2274

	for (;;) {
		union ehci_shadow	q, *q_p;
2275
		__hc32			type, *hw_p;
2276
		unsigned		incomplete = false;
L
Linus Torvalds 已提交
2277 2278 2279 2280 2281 2282 2283 2284

		frame = now_uframe >> 3;

restart:
		/* scan each element in frame's queue for completions */
		q_p = &ehci->pshadow [frame];
		hw_p = &ehci->periodic [frame];
		q.ptr = q_p->ptr;
2285
		type = Q_NEXT_TYPE(ehci, *hw_p);
L
Linus Torvalds 已提交
2286 2287 2288 2289 2290 2291 2292 2293
		modified = 0;

		while (q.ptr != NULL) {
			unsigned		uf;
			union ehci_shadow	temp;
			int			live;

			live = HC_IS_RUNNING (ehci_to_hcd(ehci)->state);
2294
			switch (hc32_to_cpu(ehci, type)) {
L
Linus Torvalds 已提交
2295 2296 2297
			case Q_TYPE_QH:
				/* handle any completions */
				temp.qh = qh_get (q.qh);
2298
				type = Q_NEXT_TYPE(ehci, q.qh->hw->hw_next);
L
Linus Torvalds 已提交
2299
				q = q.qh->qh_next;
2300
				modified = qh_completions (ehci, temp.qh);
2301 2302
				if (unlikely(list_empty(&temp.qh->qtd_list) ||
						temp.qh->needs_rescan))
L
Linus Torvalds 已提交
2303 2304 2305 2306 2307 2308 2309
					intr_deschedule (ehci, temp.qh);
				qh_put (temp.qh);
				break;
			case Q_TYPE_FSTN:
				/* for "save place" FSTNs, look at QH entries
				 * in the previous frame for completions.
				 */
2310
				if (q.fstn->hw_prev != EHCI_LIST_END(ehci)) {
L
Linus Torvalds 已提交
2311 2312
					dbg ("ignoring completions from FSTNs");
				}
2313
				type = Q_NEXT_TYPE(ehci, q.fstn->hw_next);
L
Linus Torvalds 已提交
2314 2315 2316
				q = q.fstn->fstn_next;
				break;
			case Q_TYPE_ITD:
2317 2318
				/* If this ITD is still active, leave it for
				 * later processing ... check the next entry.
2319 2320
				 * No need to check for activity unless the
				 * frame is current.
2321
				 */
2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
				if (frame == clock_frame && live) {
					rmb();
					for (uf = 0; uf < 8; uf++) {
						if (q.itd->hw_transaction[uf] &
							    ITD_ACTIVE(ehci))
							break;
					}
					if (uf < 8) {
						incomplete = true;
						q_p = &q.itd->itd_next;
						hw_p = &q.itd->hw_next;
						type = Q_NEXT_TYPE(ehci,
2334
							q.itd->hw_next);
2335 2336 2337
						q = *q_p;
						break;
					}
L
Linus Torvalds 已提交
2338 2339
				}

2340 2341 2342
				/* Take finished ITDs out of the schedule
				 * and process them:  recycle, maybe report
				 * URB completion.  HC won't cache the
L
Linus Torvalds 已提交
2343 2344 2345 2346
				 * pointer for much longer, if at all.
				 */
				*q_p = q.itd->itd_next;
				*hw_p = q.itd->hw_next;
2347
				type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
L
Linus Torvalds 已提交
2348
				wmb();
2349
				modified = itd_complete (ehci, q.itd);
L
Linus Torvalds 已提交
2350 2351 2352
				q = *q_p;
				break;
			case Q_TYPE_SITD:
2353 2354
				/* If this SITD is still active, leave it for
				 * later processing ... check the next entry.
2355 2356
				 * No need to check for activity unless the
				 * frame is current.
2357
				 */
2358 2359 2360 2361 2362 2363 2364
				if (((frame == clock_frame) ||
				     (((frame + 1) % ehci->periodic_size)
				      == clock_frame))
				    && live
				    && (q.sitd->hw_results &
					SITD_ACTIVE(ehci))) {

2365
					incomplete = true;
L
Linus Torvalds 已提交
2366 2367
					q_p = &q.sitd->sitd_next;
					hw_p = &q.sitd->hw_next;
2368 2369
					type = Q_NEXT_TYPE(ehci,
							q.sitd->hw_next);
L
Linus Torvalds 已提交
2370 2371 2372
					q = *q_p;
					break;
				}
2373 2374 2375 2376 2377

				/* Take finished SITDs out of the schedule
				 * and process them:  recycle, maybe report
				 * URB completion.
				 */
L
Linus Torvalds 已提交
2378 2379
				*q_p = q.sitd->sitd_next;
				*hw_p = q.sitd->hw_next;
2380
				type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
L
Linus Torvalds 已提交
2381
				wmb();
2382
				modified = sitd_complete (ehci, q.sitd);
L
Linus Torvalds 已提交
2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
				q = *q_p;
				break;
			default:
				dbg ("corrupt type %d frame %d shadow %p",
					type, frame, q.ptr);
				// BUG ();
				q.ptr = NULL;
			}

			/* assume completion callbacks modify the queue */
2393 2394 2395
			if (unlikely (modified)) {
				if (likely(ehci->periodic_sched > 0))
					goto restart;
2396
				/* short-circuit this scan */
2397 2398 2399
				now_uframe = clock;
				break;
			}
L
Linus Torvalds 已提交
2400 2401
		}

2402 2403 2404 2405 2406 2407 2408 2409
		/* If we can tell we caught up to the hardware, stop now.
		 * We can't advance our scan without collecting the ISO
		 * transfers that are still pending in this frame.
		 */
		if (incomplete && HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) {
			ehci->next_uframe = now_uframe;
			break;
		}
L
Linus Torvalds 已提交
2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421

		// 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 (now_uframe == clock) {
			unsigned	now;

2422 2423
			if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state)
					|| ehci->periodic_sched == 0)
L
Linus Torvalds 已提交
2424 2425
				break;
			ehci->next_uframe = now_uframe;
2426
			now = ehci_readl(ehci, &ehci->regs->frame_index) % mod;
L
Linus Torvalds 已提交
2427 2428 2429 2430 2431
			if (now_uframe == now)
				break;

			/* rescan the rest of this frame, then ... */
			clock = now;
2432
			clock_frame = clock >> 3;
K
Karsten Wiese 已提交
2433
			if (ehci->clock_frame != clock_frame) {
2434
				free_cached_lists(ehci);
K
Karsten Wiese 已提交
2435 2436
				ehci->clock_frame = clock_frame;
			}
L
Linus Torvalds 已提交
2437 2438 2439 2440
		} else {
			now_uframe++;
			now_uframe %= mod;
		}
2441
	}
L
Linus Torvalds 已提交
2442
}