ehci-q.c 30.4 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 90 91 92
	qtd->length = count;

	return count;
}

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

static inline void
qh_update (struct ehci_hcd *ehci, struct ehci_qh *qh, struct ehci_qtd *qtd)
{
	/* writes to an active overlay are unsafe */
	BUG_ON(qh->qh_state != QH_STATE_IDLE);

93 94
	qh->hw_qtd_next = QTD_NEXT(ehci, qtd->qtd_dma);
	qh->hw_alt_next = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
95 96 97 98 99 100

	/* 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.
	 */
101
	if (!(qh->hw_info1 & cpu_to_hc32(ehci, 1 << 14))) {
L
Linus Torvalds 已提交
102 103
		unsigned	is_out, epnum;

104 105
		is_out = !(qtd->hw_token & cpu_to_hc32(ehci, 1 << 8));
		epnum = (hc32_to_cpup(ehci, &qh->hw_info1) >> 8) & 0x0f;
L
Linus Torvalds 已提交
106
		if (unlikely (!usb_gettoggle (qh->dev, epnum, is_out))) {
107
			qh->hw_token &= ~cpu_to_hc32(ehci, QTD_TOGGLE);
L
Linus Torvalds 已提交
108 109 110 111 112 113
			usb_settoggle (qh->dev, epnum, is_out, 1);
		}
	}

	/* HC must see latest qtd and qh data before we clear ACTIVE+HALT */
	wmb ();
114
	qh->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 131
}

/* 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);
		/* first qtd may already be partially processed */
132
		if (cpu_to_hc32(ehci, qtd->qtd_dma) == qh->hw_current)
L
Linus Torvalds 已提交
133 134 135 136 137 138 139 140 141
			qtd = NULL;
	}

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

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

142
static int qtd_copy_status (
L
Linus Torvalds 已提交
143 144 145 146 147 148
	struct ehci_hcd *ehci,
	struct urb *urb,
	size_t length,
	u32 token
)
{
149 150
	int	status = -EINPROGRESS;

L
Linus Torvalds 已提交
151 152 153 154 155
	/* 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 已提交
156
	if (unlikely(urb->unlinked))
157
		return status;
L
Linus Torvalds 已提交
158 159 160

	/* force cleanup after short read; not always an error */
	if (unlikely (IS_SHORT_READ (token)))
161
		status = -EREMOTEIO;
L
Linus Torvalds 已提交
162 163 164 165 166

	/* 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 */
167
			status = -EOVERFLOW;
L
Linus Torvalds 已提交
168 169
		} else if (token & QTD_STS_MMF) {
			/* fs/ls interrupt xfer missed the complete-split */
170
			status = -EPROTO;
L
Linus Torvalds 已提交
171
		} else if (token & QTD_STS_DBE) {
172
			status = (QTD_PID (token) == 1) /* IN ? */
L
Linus Torvalds 已提交
173 174 175 176 177
				? -ENOSR  /* hc couldn't read data */
				: -ECOMM; /* hc couldn't write data */
		} else if (token & QTD_STS_XACT) {
			/* timeout, bad crc, wrong PID, etc; retried */
			if (QTD_CERR (token))
178
				status = -EPIPE;
L
Linus Torvalds 已提交
179 180 181 182 183
			else {
				ehci_dbg (ehci, "devpath %s ep%d%s 3strikes\n",
					urb->dev->devpath,
					usb_pipeendpoint (urb->pipe),
					usb_pipein (urb->pipe) ? "in" : "out");
184
				status = -EPROTO;
L
Linus Torvalds 已提交
185 186 187
			}
		/* CERR nonzero + no errors + halt --> stall */
		} else if (QTD_CERR (token))
188
			status = -EPIPE;
L
Linus Torvalds 已提交
189
		else	/* unknown */
190
			status = -EPROTO;
L
Linus Torvalds 已提交
191 192 193 194 195 196

		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",
197
			token, status);
L
Linus Torvalds 已提交
198 199

		/* if async CSPLIT failed, try cleaning out the TT buffer */
200
		if (status != -EPIPE
L
Linus Torvalds 已提交
201 202 203 204
				&& urb->dev->tt && !usb_pipeint (urb->pipe)
				&& ((token & QTD_STS_MMF) != 0
					|| QTD_CERR(token) == 0)
				&& (!ehci_is_TDI(ehci)
205
			                || urb->dev->tt->hub !=
L
Linus Torvalds 已提交
206 207 208 209 210 211 212 213 214 215 216
					   ehci_to_hcd(ehci)->self.root_hub)) {
#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 */
			usb_hub_tt_clear_buffer (urb->dev, urb->pipe);
		}
	}
217 218

	return status;
L
Linus Torvalds 已提交
219 220 221
}

static void
222
ehci_urb_done(struct ehci_hcd *ehci, struct urb *urb, int status)
L
Linus Torvalds 已提交
223 224 225 226 227 228 229
__releases(ehci->lock)
__acquires(ehci->lock)
{
	if (likely (urb->hcpriv != NULL)) {
		struct ehci_qh	*qh = (struct ehci_qh *) urb->hcpriv;

		/* S-mask in a QH means it's an interrupt urb */
230
		if ((qh->hw_info2 & cpu_to_hc32(ehci, QH_SMASK)) != 0) {
L
Linus Torvalds 已提交
231 232 233 234 235 236 237

			/* ... update hc-wide periodic stats (for usbfs) */
			ehci_to_hcd(ehci)->self.bandwidth_int_reqs--;
		}
		qh_put (qh);
	}

A
Alan Stern 已提交
238 239 240
	if (unlikely(urb->unlinked)) {
		COUNT(ehci->stats.unlink);
	} else {
241 242
		if (likely(status == -EINPROGRESS))
			status = 0;
A
Alan Stern 已提交
243
		COUNT(ehci->stats.complete);
L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251
	}

#ifdef EHCI_URB_TRACE
	ehci_dbg (ehci,
		"%s %s urb %p ep%d%s status %d len %d/%d\n",
		__FUNCTION__, urb->dev->devpath, urb,
		usb_pipeendpoint (urb->pipe),
		usb_pipein (urb->pipe) ? "in" : "out",
252
		status,
L
Linus Torvalds 已提交
253 254 255 256
		urb->actual_length, urb->transfer_buffer_length);
#endif

	/* complete() can reenter this HCD */
257
	usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
L
Linus Torvalds 已提交
258
	spin_unlock (&ehci->lock);
A
Alan Stern 已提交
259
	usb_hcd_giveback_urb(ehci_to_hcd(ehci), urb, status);
L
Linus Torvalds 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	spin_lock (&ehci->lock);
}

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

static void intr_deschedule (struct ehci_hcd *ehci, struct ehci_qh *qh);
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
275
qh_completions (struct ehci_hcd *ehci, struct ehci_qh *qh)
L
Linus Torvalds 已提交
276 277 278
{
	struct ehci_qtd		*last = NULL, *end = qh->dummy;
	struct list_head	*entry, *tmp;
279
	int			last_status = -EINPROGRESS;
L
Linus Torvalds 已提交
280 281 282 283
	int			stopped;
	unsigned		count = 0;
	int			do_status = 0;
	u8			state;
284
	u32			halt = HALT_BIT(ehci);
L
Linus Torvalds 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

	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.
	 */
	state = qh->qh_state;
	qh->qh_state = QH_STATE_COMPLETING;
	stopped = (state == QH_STATE_IDLE);

	/* 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;
308
		int		qtd_status;
L
Linus Torvalds 已提交
309 310 311 312 313 314 315

		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)) {
316
				ehci_urb_done(ehci, last->urb, last_status);
L
Linus Torvalds 已提交
317 318 319 320
				count++;
			}
			ehci_qtd_free (ehci, last);
			last = NULL;
321
			last_status = -EINPROGRESS;
L
Linus Torvalds 已提交
322 323 324 325 326 327 328 329
		}

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

		/* hardware copies qtd out of qh overlay */
		rmb ();
330
		token = hc32_to_cpu(ehci, qtd->hw_token);
L
Linus Torvalds 已提交
331 332 333 334 335 336 337 338 339 340 341

		/* always clean up qtds the hc de-activated */
		if ((token & QTD_STS_ACTIVE) == 0) {

			if ((token & QTD_STS_HALT) != 0) {
				stopped = 1;

			/* magic dummy for some short reads; qh won't advance.
			 * that silicon quirk can kick in with this dummy too.
			 */
			} else if (IS_SHORT_READ (token)
342 343
					&& !(qtd->hw_alt_next
						& EHCI_LIST_END(ehci))) {
L
Linus Torvalds 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356
				stopped = 1;
				goto halt;
			}

		/* stop scanning when we reach qtds the hc is using */
		} else if (likely (!stopped
				&& HC_IS_RUNNING (ehci_to_hcd(ehci)->state))) {
			break;

		} else {
			stopped = 1;

			if (unlikely (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state)))
357
				last_status = -ESHUTDOWN;
L
Linus Torvalds 已提交
358 359 360 361 362

			/* ignore active urbs unless some previous qtd
			 * for the urb faulted (including short read) or
			 * its urb was canceled.  we may patch qh or qtds.
			 */
363
			if (likely(last_status == -EINPROGRESS &&
A
Alan Stern 已提交
364
					!urb->unlinked))
L
Linus Torvalds 已提交
365
				continue;
366

L
Linus Torvalds 已提交
367 368 369 370 371 372 373 374 375
			/* issue status after short control reads */
			if (unlikely (do_status != 0)
					&& QTD_PID (token) == 0 /* OUT */) {
				do_status = 0;
				continue;
			}

			/* token in overlay may be most current */
			if (state == QH_STATE_IDLE
376
					&& cpu_to_hc32(ehci, qtd->qtd_dma)
L
Linus Torvalds 已提交
377
						== qh->hw_current)
378
				token = hc32_to_cpu(ehci, qh->hw_token);
L
Linus Torvalds 已提交
379 380 381 382 383

			/* force halt for unlinked or blocked qh, so we'll
			 * patch the qh later and so that completions can't
			 * activate it while we "know" it's stopped.
			 */
384
			if ((halt & qh->hw_token) == 0) {
L
Linus Torvalds 已提交
385
halt:
386
				qh->hw_token |= halt;
L
Linus Torvalds 已提交
387 388 389
				wmb ();
			}
		}
390

L
Linus Torvalds 已提交
391
		/* remove it from the queue */
392 393
		qtd_status = qtd_copy_status(ehci, urb, qtd->length, token);
		if (unlikely(qtd_status == -EREMOTEIO)) {
A
Alan Stern 已提交
394 395
			do_status = (!urb->unlinked &&
					usb_pipecontrol(urb->pipe));
396
			qtd_status = 0;
A
Alan Stern 已提交
397
		}
398 399
		if (likely(last_status == -EINPROGRESS))
			last_status = qtd_status;
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408 409 410 411

		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;
		}
		list_del (&qtd->qtd_list);
		last = qtd;
	}

	/* last urb's completion might still need calling */
	if (likely (last != NULL)) {
412
		ehci_urb_done(ehci, last->urb, last_status);
L
Linus Torvalds 已提交
413 414 415 416 417 418 419 420 421 422 423
		count++;
		ehci_qtd_free (ehci, last);
	}

	/* 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).
	 */
424
	if (stopped != 0 || qh->hw_qtd_next == EHCI_LIST_END(ehci)) {
L
Linus Torvalds 已提交
425 426 427 428 429 430 431 432
		switch (state) {
		case QH_STATE_IDLE:
			qh_refresh(ehci, qh);
			break;
		case QH_STATE_LINKED:
			/* should be rare for periodic transfers,
			 * except maybe high bandwidth ...
			 */
433
			if ((cpu_to_hc32(ehci, QH_SMASK)
434
					& qh->hw_info2) != 0) {
L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
				intr_deschedule (ehci, qh);
				(void) qh_schedule (ehci, qh);
			} else
				unlink_async (ehci, qh);
			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 已提交
482
	gfp_t			flags
L
Linus Torvalds 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
) {
	struct ehci_qtd		*qtd, *qtd_prev;
	dma_addr_t		buf;
	int			len, maxpacket;
	int			is_input;
	u32			token;

	/*
	 * 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 */
507 508 509
		qtd_fill(ehci, qtd, urb->setup_dma,
				sizeof (struct usb_ctrlrequest),
				token | (2 /* "setup" */ << 8), 8);
L
Linus Torvalds 已提交
510 511 512 513 514 515 516 517

		/* ... 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;
518
		qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
L
Linus Torvalds 已提交
519
		list_add_tail (&qtd->qtd_list, head);
520 521 522 523

		/* for zero length DATA stages, STATUS is always IN */
		if (len == 0)
			token |= (1 /* "in" */ << 8);
524
	}
L
Linus Torvalds 已提交
525 526 527 528

	/*
	 * data transfer stage:  buffer setup
	 */
529
	buf = urb->transfer_dma;
L
Linus Torvalds 已提交
530

531
	if (is_input)
L
Linus Torvalds 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544
		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;

545
		this_qtd_len = qtd_fill(ehci, qtd, buf, len, token, maxpacket);
L
Linus Torvalds 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
		len -= this_qtd_len;
		buf += this_qtd_len;
		if (is_input)
			qtd->hw_alt_next = ehci->async->hw_alt_next;

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

		if (likely (len <= 0))
			break;

		qtd_prev = qtd;
		qtd = ehci_qtd_alloc (ehci, flags);
		if (unlikely (!qtd))
			goto cleanup;
		qtd->urb = urb;
563
		qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
L
Linus Torvalds 已提交
564 565 566 567 568 569 570 571
		list_add_tail (&qtd->qtd_list, head);
	}

	/* unless the bulk/interrupt caller wants a chance to clean
	 * up after short reads, hc should advance qh past this urb
	 */
	if (likely ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0
				|| usb_pipecontrol (urb->pipe)))
572
		qtd->hw_alt_next = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
573 574 575 576 577

	/*
	 * control requests may need a terminating data "status" ack;
	 * bulk ones may need a terminating short packet (zero length).
	 */
578
	if (likely (urb->transfer_buffer_length != 0)) {
L
Linus Torvalds 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
		int	one_more = 0;

		if (usb_pipecontrol (urb->pipe)) {
			one_more = 1;
			token ^= 0x0100;	/* "in" <--> "out"  */
			token |= QTD_TOGGLE;	/* force DATA1 */
		} else if (usb_pipebulk (urb->pipe)
				&& (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;
596
			qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
L
Linus Torvalds 已提交
597 598 599
			list_add_tail (&qtd->qtd_list, head);

			/* never any data in such packets */
600
			qtd_fill(ehci, qtd, 0, 0, token, 0);
L
Linus Torvalds 已提交
601 602 603 604 605
		}
	}

	/* by default, enable interrupt on urb completion */
	if (likely (!(urb->transfer_flags & URB_NO_INTERRUPT)))
606
		qtd->hw_token |= cpu_to_hc32(ehci, QTD_IOC);
L
Linus Torvalds 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
	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 已提交
635
	gfp_t			flags
L
Linus Torvalds 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
) {
	struct ehci_qh		*qh = ehci_qh_alloc (ehci, flags);
	u32			info1 = 0, info2 = 0;
	int			is_input, type;
	int			maxp = 0;

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

	/* 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) {
664 665
		qh->usecs = NS_TO_US (usb_calc_bus_time (USB_SPEED_HIGH, is_input, 0,
				hb_mult (maxp) * max_packet (maxp)));
L
Linus Torvalds 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
		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.
				 */
				dbg ("intr period %d uframes, NYET!",
						urb->interval);
				goto done;
			}
		} else {
D
david-b@pacbell.net 已提交
683 684 685
			struct usb_tt	*tt = urb->dev->tt;
			int		think_time;

L
Linus Torvalds 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698
			/* 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 已提交
699 700 701 702
			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 已提交
703 704 705 706 707
			qh->period = urb->interval;
		}
	}

	/* support for tt scheduling, and access to toggles */
708
	qh->dev = urb->dev;
L
Linus Torvalds 已提交
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726

	/* using TT? */
	switch (urb->dev->speed) {
	case USB_SPEED_LOW:
		info1 |= (1 << 12);	/* EPS "low" */
		/* FALL THROUGH */

	case USB_SPEED_FULL:
		/* EPS 0 means "full" */
		if (type != PIPE_INTERRUPT)
			info1 |= (EHCI_TUNE_RL_TT << 28);
		if (type == PIPE_CONTROL) {
			info1 |= (1 << 27);	/* for TT */
			info1 |= 1 << 14;	/* toggle from qtd */
		}
		info1 |= maxp << 16;

		info2 |= (EHCI_TUNE_MULT_TT << 30);
727 728 729 730 731 732 733 734

		/* 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 已提交
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764

		/* set the address of the TT; for TDI's integrated
		 * root hub tt, leave it zeroed.
		 */
		if (!ehci_is_TDI(ehci)
				|| urb->dev->tt->hub !=
					ehci_to_hcd(ehci)->self.root_hub)
			info2 |= urb->dev->tt->hub->devnum << 16;

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

		break;

	case USB_SPEED_HIGH:		/* no TT involved */
		info1 |= (2 << 12);	/* EPS "high" */
		if (type == PIPE_CONTROL) {
			info1 |= (EHCI_TUNE_RL_HS << 28);
			info1 |= 64 << 16;	/* usb2 fixed maxpacket */
			info1 |= 1 << 14;	/* toggle from qtd */
			info2 |= (EHCI_TUNE_MULT_HS << 30);
		} else if (type == PIPE_BULK) {
			info1 |= (EHCI_TUNE_RL_HS << 28);
			info1 |= 512 << 16;	/* usb2 fixed maxpacket */
			info2 |= (EHCI_TUNE_MULT_HS << 30);
		} else {		/* PIPE_INTERRUPT */
			info1 |= max_packet (maxp) << 16;
			info2 |= hb_mult (maxp) << 30;
		}
		break;
	default:
765
		dbg ("bogus dev %p speed %d", urb->dev, urb->dev->speed);
L
Linus Torvalds 已提交
766 767 768 769 770 771 772 773 774
done:
		qh_put (qh);
		return NULL;
	}

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

	/* init as live, toggle clear, advance to dummy */
	qh->qh_state = QH_STATE_IDLE;
775 776
	qh->hw_info1 = cpu_to_hc32(ehci, info1);
	qh->hw_info2 = cpu_to_hc32(ehci, info2);
L
Linus Torvalds 已提交
777 778 779 780 781 782 783 784 785 786 787
	usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), !is_input, 1);
	qh_refresh (ehci, qh);
	return qh;
}

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

/* move qh (and its qtds) onto async queue; maybe enable queue.  */

static void qh_link_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
{
788
	__hc32		dma = QH_NEXT(ehci, qh->qh_dma);
L
Linus Torvalds 已提交
789 790 791 792 793 794
	struct ehci_qh	*head;

	/* (re)start the async schedule? */
	head = ehci->async;
	timer_action_done (ehci, TIMER_ASYNC_OFF);
	if (!head->qh_next.qh) {
795
		u32	cmd = ehci_readl(ehci, &ehci->regs->command);
L
Linus Torvalds 已提交
796 797 798

		if (!(cmd & CMD_ASE)) {
			/* in case a clear of CMD_ASE didn't take yet */
799 800
			(void)handshake(ehci, &ehci->regs->status,
					STS_ASS, 0, 150);
L
Linus Torvalds 已提交
801
			cmd |= CMD_ASE | CMD_RUN;
802
			ehci_writel(ehci, cmd, &ehci->regs->command);
L
Linus Torvalds 已提交
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
			ehci_to_hcd(ehci)->state = HC_STATE_RUNNING;
			/* posted write need not be known to HC yet ... */
		}
	}

	/* clear halt and/or toggle; and maybe recover from silicon quirk */
	if (qh->qh_state == QH_STATE_IDLE)
		qh_refresh (ehci, qh);

	/* splice right after start */
	qh->qh_next = head->qh_next;
	qh->hw_next = head->hw_next;
	wmb ();

	head->qh_next.qh = qh;
	head->hw_next = dma;

	qh->qh_state = QH_STATE_LINKED;
	/* qtd completions reported later by interrupt */
}

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

/*
 * 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;
841
	u32			qh_addr_mask = cpu_to_hc32(ehci, 0x7f);
L
Linus Torvalds 已提交
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862

	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)
863
                                qh->hw_info1 &= ~qh_addr_mask;
L
Linus Torvalds 已提交
864 865 866 867 868 869 870 871
		}

		/* 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;
872
			__hc32			token;
L
Linus Torvalds 已提交
873 874 875 876 877 878 879

			/* 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;
880
			qtd->hw_token = HALT_BIT(ehci);
L
Linus Torvalds 已提交
881 882 883 884 885 886 887 888 889 890 891
			wmb ();
			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);
			__list_splice (qtd_list, qh->qtd_list.prev);

892
			ehci_qtd_init(ehci, qtd, qtd->qtd_dma);
L
Linus Torvalds 已提交
893 894 895 896 897 898
			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);
899
			qtd->hw_next = QTD_NEXT(ehci, dma);
L
Linus Torvalds 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917

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

			urb->hcpriv = qh_get (qh);
		}
	}
	return qh;
}

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

static int
submit_async (
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	struct list_head	*qtd_list,
A
Al Viro 已提交
918
	gfp_t			mem_flags
L
Linus Torvalds 已提交
919 920 921 922 923
) {
	struct ehci_qtd		*qtd;
	int			epnum;
	unsigned long		flags;
	struct ehci_qh		*qh = NULL;
924
	int			rc;
L
Linus Torvalds 已提交
925 926

	qtd = list_entry (qtd_list->next, struct ehci_qtd, qtd_list);
927
	epnum = urb->ep->desc.bEndpointAddress;
L
Linus Torvalds 已提交
928 929 930 931 932 933 934

#ifdef EHCI_URB_TRACE
	ehci_dbg (ehci,
		"%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
		__FUNCTION__, urb->dev->devpath, urb,
		epnum & 0x0f, (epnum & USB_DIR_IN) ? "in" : "out",
		urb->transfer_buffer_length,
935
		qtd, urb->ep->hcpriv);
L
Linus Torvalds 已提交
936 937 938
#endif

	spin_lock_irqsave (&ehci->lock, flags);
939 940 941 942 943
	if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
			       &ehci_to_hcd(ehci)->flags))) {
		rc = -ESHUTDOWN;
		goto done;
	}
944 945 946
	rc = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
	if (unlikely(rc))
		goto done;
947

948
	qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
949
	if (unlikely(qh == NULL)) {
950
		usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
951 952 953
		rc = -ENOMEM;
		goto done;
	}
L
Linus Torvalds 已提交
954 955 956 957

	/* Control/bulk operations through TTs don't need scheduling,
	 * the HC and TT handle it when the TT has a buffer ready.
	 */
958 959 960
	if (likely (qh->qh_state == QH_STATE_IDLE))
		qh_link_async (ehci, qh_get (qh));
 done:
L
Linus Torvalds 已提交
961
	spin_unlock_irqrestore (&ehci->lock, flags);
962
	if (unlikely (qh == NULL))
L
Linus Torvalds 已提交
963
		qtd_list_free (ehci, urb, qtd_list);
964
	return rc;
L
Linus Torvalds 已提交
965 966 967 968 969 970
}

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

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

971
static void end_unlink_async (struct ehci_hcd *ehci)
L
Linus Torvalds 已提交
972 973 974 975
{
	struct ehci_qh		*qh = ehci->reclaim;
	struct ehci_qh		*next;

976
	iaa_watchdog_done(ehci);
L
Linus Torvalds 已提交
977

978
	// qh->hw_next = cpu_to_hc32(qh->qh_dma);
L
Linus Torvalds 已提交
979 980
	qh->qh_state = QH_STATE_IDLE;
	qh->qh_next.qh = NULL;
981
	qh_put (qh);			// refcount from reclaim
L
Linus Torvalds 已提交
982 983 984 985 986 987

	/* other unlink(s) may be pending (in QH_STATE_UNLINK_WAIT) */
	next = qh->reclaim;
	ehci->reclaim = next;
	qh->reclaim = NULL;

988
	qh_completions (ehci, qh);
L
Linus Torvalds 已提交
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014

	if (!list_empty (&qh->qtd_list)
			&& HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
		qh_link_async (ehci, qh);
	else {
		qh_put (qh);		// refcount from async list

		/* it's not free to turn the async schedule on/off; leave it
		 * active but idle for a while once it empties.
		 */
		if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state)
				&& ehci->async->qh_next.qh == NULL)
			timer_action (ehci, TIMER_ASYNC_OFF);
	}

	if (next) {
		ehci->reclaim = NULL;
		start_unlink_async (ehci, next);
	}
}

/* makes sure the async qh will become idle */
/* caller must own ehci->lock */

static void start_unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
{
1015
	int		cmd = ehci_readl(ehci, &ehci->regs->command);
L
Linus Torvalds 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
	struct ehci_qh	*prev;

#ifdef DEBUG
	assert_spin_locked(&ehci->lock);
	if (ehci->reclaim
			|| (qh->qh_state != QH_STATE_LINKED
				&& qh->qh_state != QH_STATE_UNLINK_WAIT)
			)
		BUG ();
#endif

	/* stop async schedule right now? */
	if (unlikely (qh == ehci->async)) {
		/* can't get here without STS_ASS set */
D
David Brownell 已提交
1030 1031 1032
		if (ehci_to_hcd(ehci)->state != HC_STATE_HALT
				&& !ehci->reclaim) {
			/* ... and CMD_IAAD clear */
1033 1034
			ehci_writel(ehci, cmd & ~CMD_ASE,
				    &ehci->regs->command);
L
Linus Torvalds 已提交
1035 1036
			wmb ();
			// handshake later, if we need to
D
David Brownell 已提交
1037
			timer_action_done (ehci, TIMER_ASYNC_OFF);
L
Linus Torvalds 已提交
1038 1039
		}
		return;
1040
	}
L
Linus Torvalds 已提交
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

	qh->qh_state = QH_STATE_UNLINK;
	ehci->reclaim = qh = qh_get (qh);

	prev = ehci->async;
	while (prev->qh_next.qh != qh)
		prev = prev->qh_next.qh;

	prev->hw_next = qh->hw_next;
	prev->qh_next = qh->qh_next;
	wmb ();

	if (unlikely (ehci_to_hcd(ehci)->state == HC_STATE_HALT)) {
		/* if (unlikely (qh->reclaim != 0))
1055
		 *	this will recurse, probably not much
L
Linus Torvalds 已提交
1056
		 */
1057
		end_unlink_async (ehci);
L
Linus Torvalds 已提交
1058 1059 1060 1061
		return;
	}

	cmd |= CMD_IAAD;
1062 1063
	ehci_writel(ehci, cmd, &ehci->regs->command);
	(void)ehci_readl(ehci, &ehci->regs->command);
1064
	iaa_watchdog_start(ehci);
L
Linus Torvalds 已提交
1065 1066 1067 1068
}

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

1069
static void scan_async (struct ehci_hcd *ehci)
L
Linus Torvalds 已提交
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
{
	struct ehci_qh		*qh;
	enum ehci_timer_action	action = TIMER_IO_WATCHDOG;

	if (!++(ehci->stamp))
		ehci->stamp++;
	timer_action_done (ehci, TIMER_ASYNC_SHRINK);
rescan:
	qh = ehci->async->qh_next.qh;
	if (likely (qh != NULL)) {
		do {
			/* clean any finished work for this qh */
			if (!list_empty (&qh->qtd_list)
					&& qh->stamp != ehci->stamp) {
				int temp;

				/* unlinks could happen here; completion
				 * reporting drops the lock.  rescan using
				 * the latest schedule, but don't rescan
				 * qhs we already finished (no looping).
				 */
				qh = qh_get (qh);
				qh->stamp = ehci->stamp;
1093
				temp = qh_completions (ehci, qh);
L
Linus Torvalds 已提交
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
				qh_put (qh);
				if (temp != 0) {
					goto rescan;
				}
			}

			/* unlink idle entries, reducing HC PCI 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.
			 * (plus, avoids some kind of re-activation race.)
			 */
			if (list_empty (&qh->qtd_list)) {
				if (qh->stamp == ehci->stamp)
					action = TIMER_ASYNC_SHRINK;
				else if (!ehci->reclaim
					    && qh->qh_state == QH_STATE_LINKED)
					start_unlink_async (ehci, qh);
			}

			qh = qh->qh_next.qh;
		} while (qh);
	}
	if (action == TIMER_ASYNC_SHRINK)
		timer_action (ehci, TIMER_ASYNC_SHRINK);
}