ehci-q.c 38.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
D
David Brownell 已提交
2
 * Copyright (C) 2001-2004 by David Brownell
3
 *
L
Linus Torvalds 已提交
4 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
 * 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 hardware queue manipulation ... the core.  QH/QTD manipulation.
 *
 * Control, bulk, and interrupt traffic all use "qh" lists.  They list "qtd"
 * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
 * buffers needed for the larger number).  We use one QH per endpoint, queue
 * multiple urbs (all three types) per endpoint.  URBs may need several qtds.
 *
 * ISO traffic uses "ISO TD" (itd, and sitd) records, and (along with
 * interrupts) needs careful scheduling.  Performance improvements can be
 * an ongoing challenge.  That's in "ehci-sched.c".
34
 *
L
Linus Torvalds 已提交
35 36 37 38 39 40 41 42 43 44 45
 * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
 * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
 * (b) special fields in qh entries or (c) split iso entries.  TTs will
 * buffer low/full speed data so the host collects it at high speed.
 */

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

/* fill a qtd, returning how much of the buffer we were able to queue up */

static int
46 47
qtd_fill(struct ehci_hcd *ehci, struct ehci_qtd *qtd, dma_addr_t buf,
		  size_t len, int token, int maxpacket)
L
Linus Torvalds 已提交
48 49 50 51 52
{
	int	i, count;
	u64	addr = buf;

	/* one buffer entry per 4K ... first might be short or unaligned */
53 54
	qtd->hw_buf[0] = cpu_to_hc32(ehci, (u32)addr);
	qtd->hw_buf_hi[0] = cpu_to_hc32(ehci, (u32)(addr >> 32));
L
Linus Torvalds 已提交
55 56 57 58 59 60 61 62 63 64
	count = 0x1000 - (buf & 0x0fff);	/* rest of that page */
	if (likely (len < count))		/* ... iff needed */
		count = len;
	else {
		buf +=  0x1000;
		buf &= ~0x0fff;

		/* per-qtd limit: from 16K to 20K (best alignment) */
		for (i = 1; count < len && i < 5; i++) {
			addr = buf;
65 66 67
			qtd->hw_buf[i] = cpu_to_hc32(ehci, (u32)addr);
			qtd->hw_buf_hi[i] = cpu_to_hc32(ehci,
					(u32)(addr >> 32));
L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75 76 77 78
			buf += 0x1000;
			if ((count + 0x1000) < len)
				count += 0x1000;
			else
				count = len;
		}

		/* short packets may only terminate transfers */
		if (count != len)
			count -= (count % maxpacket);
	}
79
	qtd->hw_token = cpu_to_hc32(ehci, (count << 16) | token);
L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88 89
	qtd->length = count;

	return count;
}

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

static inline void
qh_update (struct ehci_hcd *ehci, struct ehci_qh *qh, struct ehci_qtd *qtd)
{
90 91
	struct ehci_qh_hw *hw = qh->hw;

L
Linus Torvalds 已提交
92 93 94
	/* writes to an active overlay are unsafe */
	BUG_ON(qh->qh_state != QH_STATE_IDLE);

95 96
	hw->hw_qtd_next = QTD_NEXT(ehci, qtd->qtd_dma);
	hw->hw_alt_next = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
97

98 99 100 101 102
	/* Except for control endpoints, we make hardware maintain data
	 * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
	 * and set the pseudo-toggle in udev. Only usb_clear_halt() will
	 * ever clear it.
	 */
103
	if (!(hw->hw_info1 & cpu_to_hc32(ehci, QH_TOGGLE_CTL))) {
104 105
		unsigned	is_out, epnum;

106
		is_out = qh->is_out;
107
		epnum = (hc32_to_cpup(ehci, &hw->hw_info1) >> 8) & 0x0f;
108
		if (unlikely (!usb_gettoggle (qh->dev, epnum, is_out))) {
109
			hw->hw_token &= ~cpu_to_hc32(ehci, QTD_TOGGLE);
110 111 112 113
			usb_settoggle (qh->dev, epnum, is_out, 1);
		}
	}

114
	hw->hw_token &= cpu_to_hc32(ehci, QTD_TOGGLE | QTD_STS_PING);
L
Linus Torvalds 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
}

/* if it weren't for a common silicon quirk (writing the dummy into the qh
 * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
 * recovery (including urb dequeue) would need software changes to a QH...
 */
static void
qh_refresh (struct ehci_hcd *ehci, struct ehci_qh *qh)
{
	struct ehci_qtd *qtd;

	if (list_empty (&qh->qtd_list))
		qtd = qh->dummy;
	else {
		qtd = list_entry (qh->qtd_list.next,
				struct ehci_qtd, qtd_list);
131 132 133 134 135 136 137
		/*
		 * first qtd may already be partially processed.
		 * If we come here during unlink, the QH overlay region
		 * might have reference to the just unlinked qtd. The
		 * qtd is updated in qh_completions(). Update the QH
		 * overlay here.
		 */
138
		if (qh->hw->hw_token & ACTIVE_BIT(ehci)) {
139
			qh->hw->hw_qtd_next = qtd->hw_next;
L
Linus Torvalds 已提交
140
			qtd = NULL;
141
		}
L
Linus Torvalds 已提交
142 143 144 145 146 147 148 149
	}

	if (qtd)
		qh_update (ehci, qh, qtd);
}

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

150 151 152 153 154 155 156 157 158 159 160 161
static void qh_link_async(struct ehci_hcd *ehci, struct ehci_qh *qh);

static void ehci_clear_tt_buffer_complete(struct usb_hcd *hcd,
		struct usb_host_endpoint *ep)
{
	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
	struct ehci_qh		*qh = ep->hcpriv;
	unsigned long		flags;

	spin_lock_irqsave(&ehci->lock, flags);
	qh->clearing_tt = 0;
	if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
162
			&& ehci->rh_state == EHCI_RH_RUNNING)
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
		qh_link_async(ehci, qh);
	spin_unlock_irqrestore(&ehci->lock, flags);
}

static void ehci_clear_tt_buffer(struct ehci_hcd *ehci, struct ehci_qh *qh,
		struct urb *urb, u32 token)
{

	/* If an async split transaction gets an error or is unlinked,
	 * the TT buffer may be left in an indeterminate state.  We
	 * have to clear the TT buffer.
	 *
	 * Note: this routine is never called for Isochronous transfers.
	 */
	if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
#ifdef DEBUG
		struct usb_device *tt = urb->dev->tt->hub;
		dev_dbg(&tt->dev,
			"clear tt buffer port %d, a%d ep%d t%08x\n",
			urb->dev->ttport, urb->dev->devnum,
			usb_pipeendpoint(urb->pipe), token);
#endif /* DEBUG */
		if (!ehci_is_TDI(ehci)
				|| urb->dev->tt->hub !=
				   ehci_to_hcd(ehci)->self.root_hub) {
			if (usb_hub_clear_tt_buffer(urb) == 0)
				qh->clearing_tt = 1;
		} else {

			/* REVISIT ARC-derived cores don't clear the root
			 * hub TT buffer in this way...
			 */
		}
	}
}

199
static int qtd_copy_status (
L
Linus Torvalds 已提交
200 201 202 203 204 205
	struct ehci_hcd *ehci,
	struct urb *urb,
	size_t length,
	u32 token
)
{
206 207
	int	status = -EINPROGRESS;

L
Linus Torvalds 已提交
208 209 210 211 212
	/* count IN/OUT bytes, not SETUP (even short packets) */
	if (likely (QTD_PID (token) != 2))
		urb->actual_length += length - QTD_LENGTH (token);

	/* don't modify error codes */
A
Alan Stern 已提交
213
	if (unlikely(urb->unlinked))
214
		return status;
L
Linus Torvalds 已提交
215 216 217

	/* force cleanup after short read; not always an error */
	if (unlikely (IS_SHORT_READ (token)))
218
		status = -EREMOTEIO;
L
Linus Torvalds 已提交
219 220 221 222 223

	/* serious "can't proceed" faults reported by the hardware */
	if (token & QTD_STS_HALT) {
		if (token & QTD_STS_BABBLE) {
			/* FIXME "must" disable babbling device's port too */
224
			status = -EOVERFLOW;
225 226 227 228 229 230 231 232
		/* CERR nonzero + halt --> stall */
		} else if (QTD_CERR(token)) {
			status = -EPIPE;

		/* In theory, more than one of the following bits can be set
		 * since they are sticky and the transaction is retried.
		 * Which to test first is rather arbitrary.
		 */
L
Linus Torvalds 已提交
233 234
		} else if (token & QTD_STS_MMF) {
			/* fs/ls interrupt xfer missed the complete-split */
235
			status = -EPROTO;
L
Linus Torvalds 已提交
236
		} else if (token & QTD_STS_DBE) {
237
			status = (QTD_PID (token) == 1) /* IN ? */
L
Linus Torvalds 已提交
238 239 240
				? -ENOSR  /* hc couldn't read data */
				: -ECOMM; /* hc couldn't write data */
		} else if (token & QTD_STS_XACT) {
241 242 243 244 245
			/* timeout, bad CRC, wrong PID, etc */
			ehci_dbg(ehci, "devpath %s ep%d%s 3strikes\n",
				urb->dev->devpath,
				usb_pipeendpoint(urb->pipe),
				usb_pipein(urb->pipe) ? "in" : "out");
246
			status = -EPROTO;
247 248 249
		} else {	/* unknown */
			status = -EPROTO;
		}
L
Linus Torvalds 已提交
250 251 252 253 254 255

		ehci_vdbg (ehci,
			"dev%d ep%d%s qtd token %08x --> status %d\n",
			usb_pipedevice (urb->pipe),
			usb_pipeendpoint (urb->pipe),
			usb_pipein (urb->pipe) ? "in" : "out",
256
			token, status);
L
Linus Torvalds 已提交
257
	}
258 259

	return status;
L
Linus Torvalds 已提交
260 261 262
}

static void
263
ehci_urb_done(struct ehci_hcd *ehci, struct urb *urb, int status)
L
Linus Torvalds 已提交
264 265 266
__releases(ehci->lock)
__acquires(ehci->lock)
{
267 268 269
	if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
		/* ... update hc-wide periodic stats */
		ehci_to_hcd(ehci)->self.bandwidth_int_reqs--;
L
Linus Torvalds 已提交
270 271
	}

A
Alan Stern 已提交
272 273 274
	if (unlikely(urb->unlinked)) {
		COUNT(ehci->stats.unlink);
	} else {
275 276
		/* report non-error and short read status as zero */
		if (status == -EINPROGRESS || status == -EREMOTEIO)
277
			status = 0;
A
Alan Stern 已提交
278
		COUNT(ehci->stats.complete);
L
Linus Torvalds 已提交
279 280 281 282 283
	}

#ifdef EHCI_URB_TRACE
	ehci_dbg (ehci,
		"%s %s urb %p ep%d%s status %d len %d/%d\n",
284
		__func__, urb->dev->devpath, urb,
L
Linus Torvalds 已提交
285 286
		usb_pipeendpoint (urb->pipe),
		usb_pipein (urb->pipe) ? "in" : "out",
287
		status,
L
Linus Torvalds 已提交
288 289 290 291
		urb->actual_length, urb->transfer_buffer_length);
#endif

	/* complete() can reenter this HCD */
292
	usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
L
Linus Torvalds 已提交
293
	spin_unlock (&ehci->lock);
A
Alan Stern 已提交
294
	usb_hcd_giveback_urb(ehci_to_hcd(ehci), urb, status);
L
Linus Torvalds 已提交
295 296 297 298 299 300 301 302 303 304 305
	spin_lock (&ehci->lock);
}

static int qh_schedule (struct ehci_hcd *ehci, struct ehci_qh *qh);

/*
 * Process and free completed qtds for a qh, returning URBs to drivers.
 * Chases up to qh->hw_current.  Returns number of completions called,
 * indicating how much "real" work we did.
 */
static unsigned
306
qh_completions (struct ehci_hcd *ehci, struct ehci_qh *qh)
L
Linus Torvalds 已提交
307
{
308
	struct ehci_qtd		*last, *end = qh->dummy;
L
Linus Torvalds 已提交
309
	struct list_head	*entry, *tmp;
310
	int			last_status;
L
Linus Torvalds 已提交
311 312 313
	int			stopped;
	unsigned		count = 0;
	u8			state;
314
	struct ehci_qh_hw	*hw = qh->hw;
L
Linus Torvalds 已提交
315 316 317 318 319 320 321 322 323

	if (unlikely (list_empty (&qh->qtd_list)))
		return count;

	/* completions (or tasks on other cpus) must never clobber HALT
	 * till we've gone through and cleaned everything up, even when
	 * they add urbs to this qh's queue or mark them for unlinking.
	 *
	 * NOTE:  unlinking expects to be done in queue order.
324 325 326
	 *
	 * It's a bug for qh->qh_state to be anything other than
	 * QH_STATE_IDLE, unless our caller is scan_async() or
327
	 * scan_intr().
L
Linus Torvalds 已提交
328 329 330 331 332
	 */
	state = qh->qh_state;
	qh->qh_state = QH_STATE_COMPLETING;
	stopped = (state == QH_STATE_IDLE);

333 334 335 336 337
 rescan:
	last = NULL;
	last_status = -EINPROGRESS;
	qh->needs_rescan = 0;

L
Linus Torvalds 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	/* remove de-activated QTDs from front of queue.
	 * after faults (including short reads), cleanup this urb
	 * then let the queue advance.
	 * if queue is stopped, handles unlinks.
	 */
	list_for_each_safe (entry, tmp, &qh->qtd_list) {
		struct ehci_qtd	*qtd;
		struct urb	*urb;
		u32		token = 0;

		qtd = list_entry (entry, struct ehci_qtd, qtd_list);
		urb = qtd->urb;

		/* clean up any state from previous QTD ...*/
		if (last) {
			if (likely (last->urb != urb)) {
354
				ehci_urb_done(ehci, last->urb, last_status);
L
Linus Torvalds 已提交
355
				count++;
356
				last_status = -EINPROGRESS;
L
Linus Torvalds 已提交
357 358 359 360 361 362 363 364 365 366 367
			}
			ehci_qtd_free (ehci, last);
			last = NULL;
		}

		/* ignore urbs submitted during completions we reported */
		if (qtd == end)
			break;

		/* hardware copies qtd out of qh overlay */
		rmb ();
368
		token = hc32_to_cpu(ehci, qtd->hw_token);
L
Linus Torvalds 已提交
369 370

		/* always clean up qtds the hc de-activated */
371
 retry_xacterr:
L
Linus Torvalds 已提交
372 373
		if ((token & QTD_STS_ACTIVE) == 0) {

374 375 376 377 378 379 380 381 382 383 384
			/* Report Data Buffer Error: non-fatal but useful */
			if (token & QTD_STS_DBE)
				ehci_dbg(ehci,
					"detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
					urb,
					usb_endpoint_num(&urb->ep->desc),
					usb_endpoint_dir_in(&urb->ep->desc) ? "in" : "out",
					urb->transfer_buffer_length,
					qtd,
					qh);

385 386 387
			/* on STALL, error, and short reads this urb must
			 * complete and all its qtds must be recycled.
			 */
L
Linus Torvalds 已提交
388
			if ((token & QTD_STS_HALT) != 0) {
389 390 391 392 393 394

				/* retry transaction errors until we
				 * reach the software xacterr limit
				 */
				if ((token & QTD_STS_XACT) &&
						QTD_CERR(token) == 0 &&
395
						++qh->xacterrs < QH_XACTERR_MAX &&
396 397
						!urb->unlinked) {
					ehci_dbg(ehci,
R
Randy Dunlap 已提交
398
	"detected XactErr len %zu/%zu retry %d\n",
399
	qtd->length - QTD_LENGTH(token), qtd->length, qh->xacterrs);
400 401 402 403 404 405 406 407 408 409 410 411

					/* reset the token in the qtd and the
					 * qh overlay (which still contains
					 * the qtd) so that we pick up from
					 * where we left off
					 */
					token &= ~QTD_STS_HALT;
					token |= QTD_STS_ACTIVE |
							(EHCI_TUNE_CERR << 10);
					qtd->hw_token = cpu_to_hc32(ehci,
							token);
					wmb();
412 413
					hw->hw_token = cpu_to_hc32(ehci,
							token);
414 415
					goto retry_xacterr;
				}
L
Linus Torvalds 已提交
416 417 418 419
				stopped = 1;

			/* magic dummy for some short reads; qh won't advance.
			 * that silicon quirk can kick in with this dummy too.
420 421 422 423 424 425
			 *
			 * other short reads won't stop the queue, including
			 * control transfers (status stage handles that) or
			 * most other single-qtd reads ... the queue stops if
			 * URB_SHORT_NOT_OK was set so the driver submitting
			 * the urbs could clean it up.
L
Linus Torvalds 已提交
426 427
			 */
			} else if (IS_SHORT_READ (token)
428 429
					&& !(qtd->hw_alt_next
						& EHCI_LIST_END(ehci))) {
L
Linus Torvalds 已提交
430 431 432 433 434
				stopped = 1;
			}

		/* stop scanning when we reach qtds the hc is using */
		} else if (likely (!stopped
435
				&& ehci->rh_state >= EHCI_RH_RUNNING)) {
L
Linus Torvalds 已提交
436 437
			break;

438
		/* scan the whole queue for unlinks whenever it stops */
L
Linus Torvalds 已提交
439 440 441
		} else {
			stopped = 1;

442
			/* cancel everything if we halt, suspend, etc */
443
			if (ehci->rh_state < EHCI_RH_RUNNING)
444
				last_status = -ESHUTDOWN;
L
Linus Torvalds 已提交
445

446 447
			/* this qtd is active; skip it unless a previous qtd
			 * for its urb faulted, or its urb was canceled.
L
Linus Torvalds 已提交
448
			 */
449
			else if (last_status == -EINPROGRESS && !urb->unlinked)
L
Linus Torvalds 已提交
450
				continue;
451

452 453 454 455 456 457 458 459 460 461 462
			/*
			 * If this was the active qtd when the qh was unlinked
			 * and the overlay's token is active, then the overlay
			 * hasn't been written back to the qtd yet so use its
			 * token instead of the qtd's.  After the qtd is
			 * processed and removed, the overlay won't be valid
			 * any more.
			 */
			if (state == QH_STATE_IDLE &&
					qh->qtd_list.next == &qtd->qtd_list &&
					(hw->hw_token & ACTIVE_BIT(ehci))) {
463
				token = hc32_to_cpu(ehci, hw->hw_token);
464
				hw->hw_token &= ~ACTIVE_BIT(ehci);
L
Linus Torvalds 已提交
465

466 467 468 469 470 471
				/* An unlink may leave an incomplete
				 * async transaction in the TT buffer.
				 * We have to clear it.
				 */
				ehci_clear_tt_buffer(ehci, qh, urb, token);
			}
L
Linus Torvalds 已提交
472
		}
473

474 475 476 477 478 479 480 481 482 483 484 485 486 487
		/* unless we already know the urb's status, collect qtd status
		 * and update count of bytes transferred.  in common short read
		 * cases with only one data qtd (including control transfers),
		 * queue processing won't halt.  but with two or more qtds (for
		 * example, with a 32 KB transfer), when the first qtd gets a
		 * short read the second must be removed by hand.
		 */
		if (last_status == -EINPROGRESS) {
			last_status = qtd_copy_status(ehci, urb,
					qtd->length, token);
			if (last_status == -EREMOTEIO
					&& (qtd->hw_alt_next
						& EHCI_LIST_END(ehci)))
				last_status = -EINPROGRESS;
488 489 490 491 492

			/* As part of low/full-speed endpoint-halt processing
			 * we must clear the TT buffer (11.17.5).
			 */
			if (unlikely(last_status != -EINPROGRESS &&
493 494 495 496 497 498 499 500 501 502 503 504 505 506
					last_status != -EREMOTEIO)) {
				/* The TT's in some hubs malfunction when they
				 * receive this request following a STALL (they
				 * stop sending isochronous packets).  Since a
				 * STALL can't leave the TT buffer in a busy
				 * state (if you believe Figures 11-48 - 11-51
				 * in the USB 2.0 spec), we won't clear the TT
				 * buffer in this case.  Strictly speaking this
				 * is a violation of the spec.
				 */
				if (last_status != -EPIPE)
					ehci_clear_tt_buffer(ehci, qh, urb,
							token);
			}
A
Alan Stern 已提交
507
		}
L
Linus Torvalds 已提交
508

509 510 511
		/* if we're removing something not at the queue head,
		 * patch the hardware queue pointer.
		 */
L
Linus Torvalds 已提交
512 513 514 515 516
		if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
			last = list_entry (qtd->qtd_list.prev,
					struct ehci_qtd, qtd_list);
			last->hw_next = qtd->hw_next;
		}
517 518

		/* remove qtd; it's recycled after possible urb completion */
L
Linus Torvalds 已提交
519 520
		list_del (&qtd->qtd_list);
		last = qtd;
521 522

		/* reinit the xacterr counter for the next qtd */
523
		qh->xacterrs = 0;
L
Linus Torvalds 已提交
524 525 526 527
	}

	/* last urb's completion might still need calling */
	if (likely (last != NULL)) {
528
		ehci_urb_done(ehci, last->urb, last_status);
L
Linus Torvalds 已提交
529 530 531 532
		count++;
		ehci_qtd_free (ehci, last);
	}

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
	/* Do we need to rescan for URBs dequeued during a giveback? */
	if (unlikely(qh->needs_rescan)) {
		/* If the QH is already unlinked, do the rescan now. */
		if (state == QH_STATE_IDLE)
			goto rescan;

		/* Otherwise we have to wait until the QH is fully unlinked.
		 * Our caller will start an unlink if qh->needs_rescan is
		 * set.  But if an unlink has already started, nothing needs
		 * to be done.
		 */
		if (state != QH_STATE_LINKED)
			qh->needs_rescan = 0;
	}

L
Linus Torvalds 已提交
548 549 550 551 552 553 554
	/* restore original state; caller must unlink or relink */
	qh->qh_state = state;

	/* be sure the hardware's done with the qh before refreshing
	 * it after fault cleanup, or recovering from silicon wrongly
	 * overlaying the dummy qtd (which reduces DMA chatter).
	 */
555
	if (stopped != 0 || hw->hw_qtd_next == EHCI_LIST_END(ehci)) {
L
Linus Torvalds 已提交
556 557 558 559 560
		switch (state) {
		case QH_STATE_IDLE:
			qh_refresh(ehci, qh);
			break;
		case QH_STATE_LINKED:
561 562 563 564 565 566 567 568 569
			/* We won't refresh a QH that's linked (after the HC
			 * stopped the queue).  That avoids a race:
			 *  - HC reads first part of QH;
			 *  - CPU updates that first part and the token;
			 *  - HC reads rest of that QH, including token
			 * Result:  HC gets an inconsistent image, and then
			 * DMAs to/from the wrong memory (corrupting it).
			 *
			 * That should be rare for interrupt transfers,
L
Linus Torvalds 已提交
570 571
			 * except maybe high bandwidth ...
			 */
572 573 574

			/* Tell the caller to start an unlink */
			qh->needs_rescan = 1;
L
Linus Torvalds 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
			break;
		/* otherwise, unlink already started */
		}
	}

	return count;
}

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

// high bandwidth multiplier, as encoded in highspeed endpoint descriptors
#define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
// ... and packet size, for any kind of endpoint descriptor
#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)

/*
 * reverse of qh_urb_transaction:  free a list of TDs.
 * used for cleanup after errors, before HC sees an URB's TDs.
 */
static void qtd_list_free (
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	struct list_head	*qtd_list
) {
	struct list_head	*entry, *temp;

	list_for_each_safe (entry, temp, qtd_list) {
		struct ehci_qtd	*qtd;

		qtd = list_entry (entry, struct ehci_qtd, qtd_list);
		list_del (&qtd->qtd_list);
		ehci_qtd_free (ehci, qtd);
	}
}

/*
 * create a list of filled qtds for this URB; won't link into qh.
 */
static struct list_head *
qh_urb_transaction (
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	struct list_head	*head,
A
Al Viro 已提交
618
	gfp_t			flags
L
Linus Torvalds 已提交
619 620 621
) {
	struct ehci_qtd		*qtd, *qtd_prev;
	dma_addr_t		buf;
622
	int			len, this_sg_len, maxpacket;
L
Linus Torvalds 已提交
623 624
	int			is_input;
	u32			token;
625 626
	int			i;
	struct scatterlist	*sg;
L
Linus Torvalds 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644

	/*
	 * URBs map to sequences of QTDs:  one logical transaction
	 */
	qtd = ehci_qtd_alloc (ehci, flags);
	if (unlikely (!qtd))
		return NULL;
	list_add_tail (&qtd->qtd_list, head);
	qtd->urb = urb;

	token = QTD_STS_ACTIVE;
	token |= (EHCI_TUNE_CERR << 10);
	/* for split transactions, SplitXState initialized to zero */

	len = urb->transfer_buffer_length;
	is_input = usb_pipein (urb->pipe);
	if (usb_pipecontrol (urb->pipe)) {
		/* SETUP pid */
645 646 647
		qtd_fill(ehci, qtd, urb->setup_dma,
				sizeof (struct usb_ctrlrequest),
				token | (2 /* "setup" */ << 8), 8);
L
Linus Torvalds 已提交
648 649 650 651 652 653 654 655

		/* ... and always at least one more pid */
		token ^= QTD_TOGGLE;
		qtd_prev = qtd;
		qtd = ehci_qtd_alloc (ehci, flags);
		if (unlikely (!qtd))
			goto cleanup;
		qtd->urb = urb;
656
		qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
L
Linus Torvalds 已提交
657
		list_add_tail (&qtd->qtd_list, head);
658 659 660 661

		/* for zero length DATA stages, STATUS is always IN */
		if (len == 0)
			token |= (1 /* "in" */ << 8);
662
	}
L
Linus Torvalds 已提交
663 664 665 666

	/*
	 * data transfer stage:  buffer setup
	 */
667
	i = urb->num_mapped_sgs;
668
	if (len > 0 && i > 0) {
669
		sg = urb->sg;
670 671 672 673 674 675 676 677 678 679 680
		buf = sg_dma_address(sg);

		/* urb->transfer_buffer_length may be smaller than the
		 * size of the scatterlist (or vice versa)
		 */
		this_sg_len = min_t(int, sg_dma_len(sg), len);
	} else {
		sg = NULL;
		buf = urb->transfer_dma;
		this_sg_len = len;
	}
L
Linus Torvalds 已提交
681

682
	if (is_input)
L
Linus Torvalds 已提交
683 684 685 686 687 688 689 690 691 692 693 694 695
		token |= (1 /* "in" */ << 8);
	/* else it's already initted to "out" pid (0 << 8) */

	maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));

	/*
	 * buffer gets wrapped in one or more qtds;
	 * last one may be "short" (including zero len)
	 * and may serve as a control status ack
	 */
	for (;;) {
		int this_qtd_len;

696 697 698
		this_qtd_len = qtd_fill(ehci, qtd, buf, this_sg_len, token,
				maxpacket);
		this_sg_len -= this_qtd_len;
L
Linus Torvalds 已提交
699 700
		len -= this_qtd_len;
		buf += this_qtd_len;
701 702 703 704 705 706

		/*
		 * short reads advance to a "magic" dummy instead of the next
		 * qtd ... that forces the queue to stop, for manual cleanup.
		 * (this will usually be overridden later.)
		 */
L
Linus Torvalds 已提交
707
		if (is_input)
708
			qtd->hw_alt_next = ehci->async->hw->hw_alt_next;
L
Linus Torvalds 已提交
709 710 711 712 713

		/* qh makes control packets use qtd toggle; maybe switch it */
		if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
			token ^= QTD_TOGGLE;

714 715 716 717 718 719 720
		if (likely(this_sg_len <= 0)) {
			if (--i <= 0 || len <= 0)
				break;
			sg = sg_next(sg);
			buf = sg_dma_address(sg);
			this_sg_len = min_t(int, sg_dma_len(sg), len);
		}
L
Linus Torvalds 已提交
721 722 723 724 725 726

		qtd_prev = qtd;
		qtd = ehci_qtd_alloc (ehci, flags);
		if (unlikely (!qtd))
			goto cleanup;
		qtd->urb = urb;
727
		qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
L
Linus Torvalds 已提交
728 729 730
		list_add_tail (&qtd->qtd_list, head);
	}

731 732 733 734
	/*
	 * unless the caller requires manual cleanup after short reads,
	 * have the alt_next mechanism keep the queue running after the
	 * last data qtd (the only one, for control and most other cases).
L
Linus Torvalds 已提交
735 736 737
	 */
	if (likely ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0
				|| usb_pipecontrol (urb->pipe)))
738
		qtd->hw_alt_next = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
739 740 741

	/*
	 * control requests may need a terminating data "status" ack;
742 743
	 * other OUT ones may need a terminating short packet
	 * (zero length).
L
Linus Torvalds 已提交
744
	 */
745
	if (likely (urb->transfer_buffer_length != 0)) {
L
Linus Torvalds 已提交
746 747 748 749 750 751
		int	one_more = 0;

		if (usb_pipecontrol (urb->pipe)) {
			one_more = 1;
			token ^= 0x0100;	/* "in" <--> "out"  */
			token |= QTD_TOGGLE;	/* force DATA1 */
752
		} else if (usb_pipeout(urb->pipe)
L
Linus Torvalds 已提交
753 754 755 756 757 758 759 760 761 762
				&& (urb->transfer_flags & URB_ZERO_PACKET)
				&& !(urb->transfer_buffer_length % maxpacket)) {
			one_more = 1;
		}
		if (one_more) {
			qtd_prev = qtd;
			qtd = ehci_qtd_alloc (ehci, flags);
			if (unlikely (!qtd))
				goto cleanup;
			qtd->urb = urb;
763
			qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
L
Linus Torvalds 已提交
764 765 766
			list_add_tail (&qtd->qtd_list, head);

			/* never any data in such packets */
767
			qtd_fill(ehci, qtd, 0, 0, token, 0);
L
Linus Torvalds 已提交
768 769 770 771 772
		}
	}

	/* by default, enable interrupt on urb completion */
	if (likely (!(urb->transfer_flags & URB_NO_INTERRUPT)))
773
		qtd->hw_token |= cpu_to_hc32(ehci, QTD_IOC);
L
Linus Torvalds 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
	return head;

cleanup:
	qtd_list_free (ehci, urb, head);
	return NULL;
}

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

// Would be best to create all qh's from config descriptors,
// when each interface/altsetting is established.  Unlink
// any previous qh and cancel its urbs first; endpoints are
// implicitly reset then (data toggle too).
// That'd mean updating how usbcore talks to HCDs. (2.7?)


/*
 * Each QH holds a qtd list; a QH is used for everything except iso.
 *
 * For interrupt urbs, the scheduler must set the microframe scheduling
 * mask(s) each time the QH gets scheduled.  For highspeed, that's
 * just one microframe in the s-mask.  For split interrupt transactions
 * there are additional complications: c-mask, maybe FSTNs.
 */
static struct ehci_qh *
qh_make (
	struct ehci_hcd		*ehci,
	struct urb		*urb,
A
Al Viro 已提交
802
	gfp_t			flags
L
Linus Torvalds 已提交
803 804 805 806 807
) {
	struct ehci_qh		*qh = ehci_qh_alloc (ehci, flags);
	u32			info1 = 0, info2 = 0;
	int			is_input, type;
	int			maxp = 0;
808
	struct usb_tt		*tt = urb->dev->tt;
809
	struct ehci_qh_hw	*hw;
L
Linus Torvalds 已提交
810 811 812 813 814 815 816 817 818 819 820 821 822 823

	if (!qh)
		return qh;

	/*
	 * init endpoint/device data for this QH
	 */
	info1 |= usb_pipeendpoint (urb->pipe) << 8;
	info1 |= usb_pipedevice (urb->pipe) << 0;

	is_input = usb_pipein (urb->pipe);
	type = usb_pipetype (urb->pipe);
	maxp = usb_maxpacket (urb->dev, urb->pipe, !is_input);

824 825 826 827 828 829 830 831
	/* 1024 byte maxpacket is a hardware ceiling.  High bandwidth
	 * acts like up to 3KB, but is built from smaller packets.
	 */
	if (max_packet(maxp) > 1024) {
		ehci_dbg(ehci, "bogus qh maxpacket %d\n", max_packet(maxp));
		goto done;
	}

L
Linus Torvalds 已提交
832 833 834 835 836 837 838 839 840
	/* Compute interrupt scheduling parameters just once, and save.
	 * - allowing for high bandwidth, how many nsec/uframe are used?
	 * - split transactions need a second CSPLIT uframe; same question
	 * - splits also need a schedule gap (for full/low speed I/O)
	 * - qh has a polling interval
	 *
	 * For control/bulk requests, the HC or TT handles these.
	 */
	if (type == PIPE_INTERRUPT) {
841 842 843
		qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
				is_input, 0,
				hb_mult(maxp) * max_packet(maxp)));
L
Linus Torvalds 已提交
844 845 846 847 848 849 850 851 852 853 854 855
		qh->start = NO_FRAME;

		if (urb->dev->speed == USB_SPEED_HIGH) {
			qh->c_usecs = 0;
			qh->gap_uf = 0;

			qh->period = urb->interval >> 3;
			if (qh->period == 0 && urb->interval != 1) {
				/* NOTE interval 2 or 4 uframes could work.
				 * But interval 1 scheduling is simpler, and
				 * includes high bandwidth.
				 */
856 857 858 859
				urb->interval = 1;
			} else if (qh->period > ehci->periodic_size) {
				qh->period = ehci->periodic_size;
				urb->interval = qh->period << 3;
L
Linus Torvalds 已提交
860 861
			}
		} else {
D
david-b@pacbell.net 已提交
862 863
			int		think_time;

L
Linus Torvalds 已提交
864 865 866 867 868 869 870 871 872 873 874 875 876
			/* gap is f(FS/LS transfer times) */
			qh->gap_uf = 1 + usb_calc_bus_time (urb->dev->speed,
					is_input, 0, maxp) / (125 * 1000);

			/* FIXME this just approximates SPLIT/CSPLIT times */
			if (is_input) {		// SPLIT, gap, CSPLIT+DATA
				qh->c_usecs = qh->usecs + HS_USECS (0);
				qh->usecs = HS_USECS (1);
			} else {		// SPLIT+DATA, gap, CSPLIT
				qh->usecs += HS_USECS (1);
				qh->c_usecs = HS_USECS (0);
			}

D
david-b@pacbell.net 已提交
877 878 879 880
			think_time = tt ? tt->think_time : 0;
			qh->tt_usecs = NS_TO_US (think_time +
					usb_calc_bus_time (urb->dev->speed,
					is_input, 0, max_packet (maxp)));
L
Linus Torvalds 已提交
881
			qh->period = urb->interval;
882 883 884 885
			if (qh->period > ehci->periodic_size) {
				qh->period = ehci->periodic_size;
				urb->interval = qh->period;
			}
L
Linus Torvalds 已提交
886 887 888 889
		}
	}

	/* support for tt scheduling, and access to toggles */
890
	qh->dev = urb->dev;
L
Linus Torvalds 已提交
891 892 893 894

	/* using TT? */
	switch (urb->dev->speed) {
	case USB_SPEED_LOW:
895
		info1 |= QH_LOW_SPEED;
L
Linus Torvalds 已提交
896 897 898 899 900 901 902
		/* FALL THROUGH */

	case USB_SPEED_FULL:
		/* EPS 0 means "full" */
		if (type != PIPE_INTERRUPT)
			info1 |= (EHCI_TUNE_RL_TT << 28);
		if (type == PIPE_CONTROL) {
903 904
			info1 |= QH_CONTROL_EP;		/* for TT */
			info1 |= QH_TOGGLE_CTL;		/* toggle from qtd */
L
Linus Torvalds 已提交
905 906 907 908
		}
		info1 |= maxp << 16;

		info2 |= (EHCI_TUNE_MULT_TT << 30);
909 910 911 912 913 914 915 916

		/* Some Freescale processors have an erratum in which the
		 * port number in the queue head was 0..N-1 instead of 1..N.
		 */
		if (ehci_has_fsl_portno_bug(ehci))
			info2 |= (urb->dev->ttport-1) << 23;
		else
			info2 |= urb->dev->ttport << 23;
L
Linus Torvalds 已提交
917 918 919 920

		/* set the address of the TT; for TDI's integrated
		 * root hub tt, leave it zeroed.
		 */
921 922
		if (tt && tt->hub != ehci_to_hcd(ehci)->self.root_hub)
			info2 |= tt->hub->devnum << 16;
L
Linus Torvalds 已提交
923 924 925 926 927 928

		/* NOTE:  if (PIPE_INTERRUPT) { scheduler sets c-mask } */

		break;

	case USB_SPEED_HIGH:		/* no TT involved */
929
		info1 |= QH_HIGH_SPEED;
L
Linus Torvalds 已提交
930 931 932
		if (type == PIPE_CONTROL) {
			info1 |= (EHCI_TUNE_RL_HS << 28);
			info1 |= 64 << 16;	/* usb2 fixed maxpacket */
933
			info1 |= QH_TOGGLE_CTL;	/* toggle from qtd */
L
Linus Torvalds 已提交
934 935 936
			info2 |= (EHCI_TUNE_MULT_HS << 30);
		} else if (type == PIPE_BULK) {
			info1 |= (EHCI_TUNE_RL_HS << 28);
937 938 939 940 941 942 943
			/* The USB spec says that high speed bulk endpoints
			 * always use 512 byte maxpacket.  But some device
			 * vendors decided to ignore that, and MSFT is happy
			 * to help them do so.  So now people expect to use
			 * such nonconformant devices with Linux too; sigh.
			 */
			info1 |= max_packet(maxp) << 16;
L
Linus Torvalds 已提交
944 945 946 947 948 949 950
			info2 |= (EHCI_TUNE_MULT_HS << 30);
		} else {		/* PIPE_INTERRUPT */
			info1 |= max_packet (maxp) << 16;
			info2 |= hb_mult (maxp) << 30;
		}
		break;
	default:
951 952
		ehci_dbg(ehci, "bogus dev %p speed %d\n", urb->dev,
			urb->dev->speed);
L
Linus Torvalds 已提交
953
done:
A
Alan Stern 已提交
954
		qh_destroy(ehci, qh);
L
Linus Torvalds 已提交
955 956 957 958 959 960 961
		return NULL;
	}

	/* NOTE:  if (PIPE_INTERRUPT) { scheduler sets s-mask } */

	/* init as live, toggle clear, advance to dummy */
	qh->qh_state = QH_STATE_IDLE;
962 963 964
	hw = qh->hw;
	hw->hw_info1 = cpu_to_hc32(ehci, info1);
	hw->hw_info2 = cpu_to_hc32(ehci, info2);
965
	qh->is_out = !is_input;
966
	usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), !is_input, 1);
L
Linus Torvalds 已提交
967 968 969 970 971 972
	qh_refresh (ehci, qh);
	return qh;
}

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

973 974 975 976 977 978 979 980 981 982
static void enable_async(struct ehci_hcd *ehci)
{
	if (ehci->async_count++)
		return;

	/* Stop waiting to turn off the async schedule */
	ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_DISABLE_ASYNC);

	/* Don't start the schedule until ASS is 0 */
	ehci_poll_ASS(ehci);
983
	turn_on_io_watchdog(ehci);
984 985 986 987 988 989 990 991 992 993 994 995 996 997
}

static void disable_async(struct ehci_hcd *ehci)
{
	if (--ehci->async_count)
		return;

	/* The async schedule and async_unlink list are supposed to be empty */
	WARN_ON(ehci->async->qh_next.qh || ehci->async_unlink);

	/* Don't turn off the schedule until ASS is 1 */
	ehci_poll_ASS(ehci);
}

L
Linus Torvalds 已提交
998 999 1000 1001
/* move qh (and its qtds) onto async queue; maybe enable queue.  */

static void qh_link_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
{
1002
	__hc32		dma = QH_NEXT(ehci, qh->qh_dma);
L
Linus Torvalds 已提交
1003 1004
	struct ehci_qh	*head;

1005 1006 1007 1008
	/* Don't link a QH if there's a Clear-TT-Buffer pending */
	if (unlikely(qh->clearing_tt))
		return;

1009 1010
	WARN_ON(qh->qh_state != QH_STATE_IDLE);

1011
	/* clear halt and/or toggle; and maybe recover from silicon quirk */
1012
	qh_refresh(ehci, qh);
L
Linus Torvalds 已提交
1013 1014

	/* splice right after start */
1015
	head = ehci->async;
L
Linus Torvalds 已提交
1016
	qh->qh_next = head->qh_next;
1017
	qh->hw->hw_next = head->hw->hw_next;
L
Linus Torvalds 已提交
1018 1019 1020
	wmb ();

	head->qh_next.qh = qh;
1021
	head->hw->hw_next = dma;
L
Linus Torvalds 已提交
1022

1023
	qh->xacterrs = 0;
L
Linus Torvalds 已提交
1024 1025
	qh->qh_state = QH_STATE_LINKED;
	/* qtd completions reported later by interrupt */
1026 1027

	enable_async(ehci);
L
Linus Torvalds 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
}

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

/*
 * For control/bulk/interrupt, return QH with these TDs appended.
 * Allocates and initializes the QH if necessary.
 * Returns null if it can't allocate a QH it needs to.
 * If the QH has TDs (urbs) already, that's great.
 */
static struct ehci_qh *qh_append_tds (
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	struct list_head	*qtd_list,
	int			epnum,
	void			**ptr
)
{
	struct ehci_qh		*qh = NULL;
A
Al Viro 已提交
1047
	__hc32			qh_addr_mask = cpu_to_hc32(ehci, 0x7f);
L
Linus Torvalds 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068

	qh = (struct ehci_qh *) *ptr;
	if (unlikely (qh == NULL)) {
		/* can't sleep here, we have ehci->lock... */
		qh = qh_make (ehci, urb, GFP_ATOMIC);
		*ptr = qh;
	}
	if (likely (qh != NULL)) {
		struct ehci_qtd	*qtd;

		if (unlikely (list_empty (qtd_list)))
			qtd = NULL;
		else
			qtd = list_entry (qtd_list->next, struct ehci_qtd,
					qtd_list);

		/* control qh may need patching ... */
		if (unlikely (epnum == 0)) {

                        /* usb_reset_device() briefly reverts to address 0 */
                        if (usb_pipedevice (urb->pipe) == 0)
1069
				qh->hw->hw_info1 &= ~qh_addr_mask;
L
Linus Torvalds 已提交
1070 1071 1072 1073 1074 1075 1076 1077
		}

		/* just one way to queue requests: swap with the dummy qtd.
		 * only hc or qh_refresh() ever modify the overlay.
		 */
		if (likely (qtd != NULL)) {
			struct ehci_qtd		*dummy;
			dma_addr_t		dma;
1078
			__hc32			token;
L
Linus Torvalds 已提交
1079 1080 1081 1082 1083 1084 1085

			/* to avoid racing the HC, use the dummy td instead of
			 * the first td of our list (becomes new dummy).  both
			 * tds stay deactivated until we're done, when the
			 * HC is allowed to fetch the old dummy (4.10.2).
			 */
			token = qtd->hw_token;
1086
			qtd->hw_token = HALT_BIT(ehci);
1087

L
Linus Torvalds 已提交
1088 1089 1090 1091 1092 1093 1094 1095
			dummy = qh->dummy;

			dma = dummy->qtd_dma;
			*dummy = *qtd;
			dummy->qtd_dma = dma;

			list_del (&qtd->qtd_list);
			list_add (&dummy->qtd_list, qtd_list);
1096
			list_splice_tail(qtd_list, &qh->qtd_list);
L
Linus Torvalds 已提交
1097

1098
			ehci_qtd_init(ehci, qtd, qtd->qtd_dma);
L
Linus Torvalds 已提交
1099 1100 1101 1102 1103 1104
			qh->dummy = qtd;

			/* hc must see the new dummy at list end */
			dma = qtd->qtd_dma;
			qtd = list_entry (qh->qtd_list.prev,
					struct ehci_qtd, qtd_list);
1105
			qtd->hw_next = QTD_NEXT(ehci, dma);
L
Linus Torvalds 已提交
1106 1107 1108 1109 1110

			/* let the hc process these next qtds */
			wmb ();
			dummy->hw_token = token;

A
Alan Stern 已提交
1111
			urb->hcpriv = qh;
L
Linus Torvalds 已提交
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
		}
	}
	return qh;
}

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

static int
submit_async (
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	struct list_head	*qtd_list,
A
Al Viro 已提交
1124
	gfp_t			mem_flags
L
Linus Torvalds 已提交
1125 1126 1127 1128
) {
	int			epnum;
	unsigned long		flags;
	struct ehci_qh		*qh = NULL;
1129
	int			rc;
L
Linus Torvalds 已提交
1130

1131
	epnum = urb->ep->desc.bEndpointAddress;
L
Linus Torvalds 已提交
1132 1133

#ifdef EHCI_URB_TRACE
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
	{
		struct ehci_qtd *qtd;
		qtd = list_entry(qtd_list->next, struct ehci_qtd, qtd_list);
		ehci_dbg(ehci,
			 "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
			 __func__, urb->dev->devpath, urb,
			 epnum & 0x0f, (epnum & USB_DIR_IN) ? "in" : "out",
			 urb->transfer_buffer_length,
			 qtd, urb->ep->hcpriv);
	}
L
Linus Torvalds 已提交
1144 1145 1146
#endif

	spin_lock_irqsave (&ehci->lock, flags);
1147
	if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
1148 1149 1150
		rc = -ESHUTDOWN;
		goto done;
	}
1151 1152 1153
	rc = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
	if (unlikely(rc))
		goto done;
1154

1155
	qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
1156
	if (unlikely(qh == NULL)) {
1157
		usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
1158 1159 1160
		rc = -ENOMEM;
		goto done;
	}
L
Linus Torvalds 已提交
1161 1162 1163 1164

	/* Control/bulk operations through TTs don't need scheduling,
	 * the HC and TT handle it when the TT has a buffer ready.
	 */
1165
	if (likely (qh->qh_state == QH_STATE_IDLE))
1166
		qh_link_async(ehci, qh);
1167
 done:
L
Linus Torvalds 已提交
1168
	spin_unlock_irqrestore (&ehci->lock, flags);
1169
	if (unlikely (qh == NULL))
L
Linus Torvalds 已提交
1170
		qtd_list_free (ehci, urb, qtd_list);
1171
	return rc;
L
Linus Torvalds 已提交
1172 1173 1174 1175
}

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

1176
static void single_unlink_async(struct ehci_hcd *ehci, struct ehci_qh *qh)
L
Linus Torvalds 已提交
1177
{
1178
	struct ehci_qh		*prev;
L
Linus Torvalds 已提交
1179

1180
	/* Add to the end of the list of QHs waiting for the next IAAD */
1181
	qh->qh_state = QH_STATE_UNLINK_WAIT;
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
	if (ehci->async_unlink)
		ehci->async_unlink_last->unlink_next = qh;
	else
		ehci->async_unlink = qh;
	ehci->async_unlink_last = qh;

	/* Unlink it from the schedule */
	prev = ehci->async;
	while (prev->qh_next.qh != qh)
		prev = prev->qh_next.qh;

	prev->hw->hw_next = qh->hw->hw_next;
	prev->qh_next = qh->qh_next;
	if (ehci->qh_scan_next == qh)
		ehci->qh_scan_next = qh->qh_next.qh;
}
L
Linus Torvalds 已提交
1198

1199 1200 1201 1202 1203 1204 1205 1206
static void start_iaa_cycle(struct ehci_hcd *ehci, bool nested)
{
	/*
	 * Do nothing if an IAA cycle is already running or
	 * if one will be started shortly.
	 */
	if (ehci->async_iaa || ehci->async_unlinking)
		return;
L
Linus Torvalds 已提交
1207

1208 1209
	/* If the controller isn't running, we don't have to wait for it */
	if (unlikely(ehci->rh_state < EHCI_RH_RUNNING)) {
1210 1211 1212 1213 1214

		/* Do all the waiting QHs */
		ehci->async_iaa = ehci->async_unlink;
		ehci->async_unlink = NULL;

1215 1216
		if (!nested)		/* Avoid recursion */
			end_unlink_async(ehci);
1217

1218
	/* Otherwise start a new IAA cycle */
1219
	} else if (likely(ehci->rh_state == EHCI_RH_RUNNING)) {
1220 1221 1222 1223
		struct ehci_qh		*qh;

		/* Do only the first waiting QH (nVidia bug?) */
		qh = ehci->async_unlink;
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236

		/*
		 * Intel (?) bug: The HC can write back the overlay region
		 * even after the IAA interrupt occurs.  In self-defense,
		 * always go through two IAA cycles for each QH.
		 */
		if (qh->qh_state == QH_STATE_UNLINK_WAIT) {
			qh->qh_state = QH_STATE_UNLINK;
		} else {
			ehci->async_iaa = qh;
			ehci->async_unlink = qh->unlink_next;
			qh->unlink_next = NULL;
		}
1237

1238 1239
		/* Make sure the unlinks are all visible to the hardware */
		wmb();
L
Linus Torvalds 已提交
1240

1241 1242 1243 1244
		ehci_writel(ehci, ehci->command | CMD_IAAD,
				&ehci->regs->command);
		ehci_readl(ehci, &ehci->regs->command);
		ehci_enable_event(ehci, EHCI_HRTIMER_IAA_WATCHDOG, true);
L
Linus Torvalds 已提交
1245
	}
1246 1247 1248 1249 1250 1251 1252
}

/* the async qh for the qtds being unlinked are now gone from the HC */

static void end_unlink_async(struct ehci_hcd *ehci)
{
	struct ehci_qh		*qh;
1253 1254 1255 1256

	if (ehci->has_synopsys_hc_bug)
		ehci_writel(ehci, (u32) ehci->async->qh_dma,
			    &ehci->regs->async_next);
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282

	/* Process the idle QHs */
 restart:
	ehci->async_unlinking = true;
	while (ehci->async_iaa) {
		qh = ehci->async_iaa;
		ehci->async_iaa = qh->unlink_next;
		qh->unlink_next = NULL;

		qh->qh_state = QH_STATE_IDLE;
		qh->qh_next.qh = NULL;

		qh_completions(ehci, qh);
		if (!list_empty(&qh->qtd_list) &&
				ehci->rh_state == EHCI_RH_RUNNING)
			qh_link_async(ehci, qh);
		disable_async(ehci);
	}
	ehci->async_unlinking = false;

	/* Start a new IAA cycle if any QHs are waiting for it */
	if (ehci->async_unlink) {
		start_iaa_cycle(ehci, true);
		if (unlikely(ehci->rh_state < EHCI_RH_RUNNING))
			goto restart;
	}
L
Linus Torvalds 已提交
1283 1284
}

1285 1286
static void start_unlink_async(struct ehci_hcd *ehci, struct ehci_qh *qh);

1287 1288
static void unlink_empty_async(struct ehci_hcd *ehci)
{
1289 1290
	struct ehci_qh		*qh;
	struct ehci_qh		*qh_to_unlink = NULL;
1291
	bool			check_unlinks_later = false;
1292
	int			count = 0;
1293

1294 1295
	/* Find the last async QH which has been empty for a timer cycle */
	for (qh = ehci->async->qh_next.qh; qh; qh = qh->qh_next.qh) {
1296 1297
		if (list_empty(&qh->qtd_list) &&
				qh->qh_state == QH_STATE_LINKED) {
1298 1299
			++count;
			if (qh->unlink_cycle == ehci->async_unlink_cycle)
1300 1301
				check_unlinks_later = true;
			else
1302
				qh_to_unlink = qh;
1303 1304 1305
		}
	}

1306 1307 1308 1309 1310
	/* If nothing else is being unlinked, unlink the last empty QH */
	if (!ehci->async_iaa && !ehci->async_unlink && qh_to_unlink) {
		start_unlink_async(ehci, qh_to_unlink);
		--count;
	}
1311

1312 1313
	/* Other QHs will be handled later */
	if (count > 0) {
1314 1315 1316 1317 1318
		ehci_enable_event(ehci, EHCI_HRTIMER_ASYNC_UNLINKS, true);
		++ehci->async_unlink_cycle;
	}
}

1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
/* The root hub is suspended; unlink all the async QHs */
static void unlink_empty_async_suspended(struct ehci_hcd *ehci)
{
	struct ehci_qh		*qh;

	while (ehci->async->qh_next.qh) {
		qh = ehci->async->qh_next.qh;
		WARN_ON(!list_empty(&qh->qtd_list));
		single_unlink_async(ehci, qh);
	}
	start_iaa_cycle(ehci, false);
}

L
Linus Torvalds 已提交
1332 1333 1334
/* makes sure the async qh will become idle */
/* caller must own ehci->lock */

1335
static void start_unlink_async(struct ehci_hcd *ehci, struct ehci_qh *qh)
L
Linus Torvalds 已提交
1336
{
1337 1338 1339 1340 1341 1342 1343 1344
	/*
	 * 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;
L
Linus Torvalds 已提交
1345 1346 1347
		return;
	}

1348 1349
	single_unlink_async(ehci, qh);
	start_iaa_cycle(ehci, false);
L
Linus Torvalds 已提交
1350 1351 1352 1353
}

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

1354
static void scan_async (struct ehci_hcd *ehci)
L
Linus Torvalds 已提交
1355 1356
{
	struct ehci_qh		*qh;
1357
	bool			check_unlinks_later = false;
L
Linus Torvalds 已提交
1358

1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
	ehci->qh_scan_next = ehci->async->qh_next.qh;
	while (ehci->qh_scan_next) {
		qh = ehci->qh_scan_next;
		ehci->qh_scan_next = qh->qh_next.qh;
 rescan:
		/* 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
1373
			 * in single_unlink_async().
L
Linus Torvalds 已提交
1374
			 */
1375
			temp = qh_completions(ehci, qh);
1376
			if (qh->needs_rescan) {
1377
				start_unlink_async(ehci, qh);
1378 1379 1380 1381 1382
			} else if (list_empty(&qh->qtd_list)
					&& qh->qh_state == QH_STATE_LINKED) {
				qh->unlink_cycle = ehci->async_unlink_cycle;
				check_unlinks_later = true;
			} else if (temp != 0)
1383 1384
				goto rescan;
		}
1385
	}
L
Linus Torvalds 已提交
1386

1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
	/*
	 * Unlink empty entries, reducing DMA usage as well
	 * as HCD schedule-scanning costs.  Delay for any qh
	 * we just scanned, there's a not-unusual case that it
	 * doesn't stay idle for long.
	 */
	if (check_unlinks_later && ehci->rh_state == EHCI_RH_RUNNING &&
			!(ehci->enabled_hrtimer_events &
				BIT(EHCI_HRTIMER_ASYNC_UNLINKS))) {
		ehci_enable_event(ehci, EHCI_HRTIMER_ASYNC_UNLINKS, true);
		++ehci->async_unlink_cycle;
L
Linus Torvalds 已提交
1398 1399
	}
}