1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * EHCI Host Controller Driver (EHCI)
30  *
31  * The EHCI driver is a software driver which interfaces to the Universal
32  * Serial Bus layer (USBA) and the Host Controller (HC). The interface to
33  * the Host Controller is defined by the EHCI Host Controller Interface.
34  *
35  * This module contains the EHCI driver interrupt code, which handles all
36  * Checking of status of USB transfers, error recovery and callbacks.
37  */
38 
39 #include <sys/usb/hcd/ehci/ehcid.h>
40 #include <sys/usb/hcd/ehci/ehci_xfer.h>
41 #include <sys/usb/hcd/ehci/ehci_util.h>
42 
43 /*
44  * EHCI Interrupt Handling functions.
45  */
46 void		ehci_handle_ue(ehci_state_t		*ehcip);
47 void		ehci_handle_frame_list_rollover(
48 				ehci_state_t		*ehcip);
49 void		ehci_handle_endpoint_reclaimation(
50 				ehci_state_t		*ehcip);
51 void		ehci_traverse_active_qtd_list(
52 				ehci_state_t		*ehcip);
53 static ehci_qtd_t *ehci_create_done_qtd_list(
54 				ehci_state_t		*ehcip);
55 static usb_cr_t ehci_parse_error(
56 				ehci_state_t		*ehcip,
57 				ehci_qtd_t		*qtd);
58 usb_cr_t	ehci_check_for_error(
59 				ehci_state_t		*ehcip,
60 				ehci_pipe_private_t	*pp,
61 				ehci_trans_wrapper_t	*tw,
62 				ehci_qtd_t		*qtd,
63 				uint_t			ctrl);
64 static usb_cr_t	ehci_check_for_short_xfer(
65 				ehci_state_t		*ehcip,
66 				ehci_pipe_private_t	*pp,
67 				ehci_trans_wrapper_t	*tw,
68 				ehci_qtd_t		*qtd);
69 void		ehci_handle_error(
70 				ehci_state_t		*ehcip,
71 				ehci_qtd_t		*qtd,
72 				usb_cr_t		error);
73 static void	ehci_cleanup_data_underrun(
74 				ehci_state_t		*ehcip,
75 				ehci_trans_wrapper_t	*tw,
76 				ehci_qtd_t		*qtd);
77 static void	ehci_handle_normal_qtd(
78 				ehci_state_t		*ehcip,
79 				ehci_qtd_t		*qtd,
80 				ehci_trans_wrapper_t	*tw);
81 void		ehci_handle_ctrl_qtd(
82 				ehci_state_t		*ehcip,
83 				ehci_pipe_private_t	*pp,
84 				ehci_trans_wrapper_t	*tw,
85 				ehci_qtd_t		*qtd,
86 				void			*);
87 void		ehci_handle_bulk_qtd(
88 				ehci_state_t		*ehcip,
89 				ehci_pipe_private_t	*pp,
90 				ehci_trans_wrapper_t	*tw,
91 				ehci_qtd_t		*qtd,
92 				void			*);
93 void		ehci_handle_intr_qtd(
94 				ehci_state_t		*ehcip,
95 				ehci_pipe_private_t	*pp,
96 				ehci_trans_wrapper_t	*tw,
97 				ehci_qtd_t		*qtd,
98 				void			*);
99 static void	ehci_handle_one_xfer_completion(
100 				ehci_state_t		*ehcip,
101 				ehci_trans_wrapper_t	*tw);
102 static void	ehci_sendup_qtd_message(
103 				ehci_state_t		*ehcip,
104 				ehci_pipe_private_t	*pp,
105 				ehci_trans_wrapper_t	*tw,
106 				ehci_qtd_t		*qtd,
107 				usb_cr_t		error);
108 
109 
110 /*
111  * Interrupt Handling functions
112  */
113 
114 /*
115  * ehci_handle_ue:
116  *
117  * Handling of Unrecoverable Error interrupt (UE).
118  */
119 void
120 ehci_handle_ue(ehci_state_t	*ehcip)
121 {
122 	usb_frame_number_t	before_frame_number, after_frame_number;
123 
124 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
125 
126 	USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
127 	    "ehci_handle_ue: Handling of UE interrupt");
128 
129 	/*
130 	 * First check whether current UE error occurred due to USB or
131 	 * due to some other subsystem. This can be verified by reading
132 	 * usb frame numbers before & after a delay of few milliseconds.
133 	 * If usb frame number read after delay is greater than the one
134 	 * read before delay, then, USB subsystem is fine. In this case,
135 	 * disable UE error interrupt and return without shutdowning the
136 	 * USB subsystem.
137 	 *
138 	 * Otherwise, if usb frame number read after delay is less than
139 	 * or equal to one read before the delay, then, current UE error
140 	 * occurred from USB subsystem. In this case,go ahead with actual
141 	 * UE error recovery procedure.
142 	 *
143 	 * Get the current usb frame number before waiting for few
144 	 * milliseconds.
145 	 */
146 	before_frame_number = ehci_get_current_frame_number(ehcip);
147 
148 	/* Wait for few milliseconds */
149 	drv_usecwait(EHCI_TIMEWAIT);
150 
151 	/*
152 	 * Get the current usb frame number after waiting for
153 	 * milliseconds.
154 	 */
155 	after_frame_number = ehci_get_current_frame_number(ehcip);
156 
157 	USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
158 	    "ehci_handle_ue: Before Frame Number 0x%llx "
159 	    "After Frame Number 0x%llx", before_frame_number,
160 	    after_frame_number);
161 
162 	if (after_frame_number > before_frame_number) {
163 
164 		/* Disable UE interrupt */
165 		Set_OpReg(ehci_interrupt, (Get_OpReg(ehci_interrupt) &
166 		    ~EHCI_INTR_HOST_SYSTEM_ERROR));
167 
168 		return;
169 	}
170 
171 	/*
172 	 * This UE is due to USB hardware error. Reset ehci controller
173 	 * and reprogram to bring it back to functional state.
174 	 */
175 	if ((ehci_do_soft_reset(ehcip)) != USB_SUCCESS) {
176 
177 		USB_DPRINTF_L0(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
178 		    "Unrecoverable USB Hardware Error");
179 
180 		/* Disable UE interrupt */
181 		Set_OpReg(ehci_interrupt, (Get_OpReg(ehci_interrupt) &
182 		    ~EHCI_INTR_HOST_SYSTEM_ERROR));
183 
184 		/* Route all Root hub ports to Classic host controller */
185 		Set_OpReg(ehci_config_flag, EHCI_CONFIG_FLAG_CLASSIC);
186 
187 		/* Set host controller soft state to error */
188 		ehcip->ehci_hc_soft_state = EHCI_CTLR_ERROR_STATE;
189 	}
190 }
191 
192 
193 /*
194  * ehci_handle_frame_list_rollover:
195  *
196  * Update software based usb frame number part on every frame number
197  * overflow interrupt.
198  *
199  * Refer ehci spec 1.0, section 2.3.2, page 21 for more details.
200  *
201  * NOTE: This function is also called from POLLED MODE.
202  */
203 void
204 ehci_handle_frame_list_rollover(ehci_state_t *ehcip)
205 {
206 	ehcip->ehci_fno += (0x4000 -
207 	    (((Get_OpReg(ehci_frame_index) & 0x3FFF) ^
208 	    ehcip->ehci_fno) & 0x2000));
209 
210 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
211 	    "ehci_handle_frame_list_rollover:"
212 	    "Frame Number Higher Part 0x%llx\n", ehcip->ehci_fno);
213 }
214 
215 
216 /*
217  * ehci_handle_endpoint_reclamation:
218  *
219  * Reclamation of Host Controller (HC) Endpoint Descriptors (QH).
220  */
221 void
222 ehci_handle_endpoint_reclaimation(ehci_state_t	*ehcip)
223 {
224 	usb_frame_number_t	current_frame_number;
225 	usb_frame_number_t	endpoint_frame_number;
226 	ehci_qh_t		*reclaim_qh;
227 
228 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
229 	    "ehci_handle_endpoint_reclamation:");
230 
231 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
232 
233 	current_frame_number = ehci_get_current_frame_number(ehcip);
234 
235 	/*
236 	 * Deallocate all Endpoint Descriptors (QH) which are on the
237 	 * reclamation list. These QH's are already removed from the
238 	 * interrupt lattice tree.
239 	 */
240 	while (ehcip->ehci_reclaim_list) {
241 
242 		reclaim_qh = ehcip->ehci_reclaim_list;
243 
244 		endpoint_frame_number = (usb_frame_number_t)(uintptr_t)
245 		    (EHCI_LOOKUP_ID(Get_QH(reclaim_qh->qh_reclaim_frame)));
246 
247 		USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
248 		    "ehci_handle_endpoint_reclamation:"
249 		    "current frame number 0x%llx endpoint frame number 0x%llx",
250 		    current_frame_number, endpoint_frame_number);
251 
252 		/*
253 		 * Deallocate current endpoint only if endpoint's usb frame
254 		 * number is less than or equal to current usb frame number.
255 		 *
256 		 * If endpoint's usb frame number is greater than the current
257 		 * usb frame number, ignore rest of the endpoints in the list
258 		 * since rest of the endpoints are inserted into the reclaim
259 		 * list later than the current reclaim endpoint.
260 		 */
261 		if (endpoint_frame_number > current_frame_number) {
262 			break;
263 		}
264 
265 		/* Get the next endpoint from the rec. list */
266 		ehcip->ehci_reclaim_list = ehci_qh_iommu_to_cpu(
267 		    ehcip, Get_QH(reclaim_qh->qh_reclaim_next));
268 
269 		/* Free 32bit ID */
270 		EHCI_FREE_ID((uint32_t)Get_QH(reclaim_qh->qh_reclaim_frame));
271 
272 		/* Deallocate the endpoint */
273 		ehci_deallocate_qh(ehcip, reclaim_qh);
274 	}
275 }
276 
277 
278 /*
279  * ehci_traverse_active_qtd_list:
280  */
281 void
282 ehci_traverse_active_qtd_list(
283 	ehci_state_t		*ehcip)
284 {
285 	uint_t			state;		/* QTD state */
286 	ehci_qtd_t		*curr_qtd = NULL; /* QTD pointers */
287 	ehci_qtd_t		*next_qtd = NULL; /* QTD pointers */
288 	usb_cr_t		error;		/* Error from QTD */
289 	ehci_trans_wrapper_t	*tw = NULL;	/* Transfer wrapper */
290 	ehci_pipe_private_t	*pp = NULL;	/* Pipe private field */
291 
292 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
293 	    "ehci_traverse_active_qtd_list:");
294 
295 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
296 
297 	/* Sync QH and QTD pool */
298 	Sync_QH_QTD_Pool(ehcip);
299 
300 	/* Create done qtd list */
301 	curr_qtd = ehci_create_done_qtd_list(ehcip);
302 
303 	/* Traverse the list of transfer descriptors */
304 	while (curr_qtd) {
305 		/* Get next qtd from the active qtd list */
306 		next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
307 		    Get_QTD(curr_qtd->qtd_active_qtd_next));
308 
309 		/* Check for QTD state */
310 		state = Get_QTD(curr_qtd->qtd_state);
311 
312 		USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
313 		    "ehci_traverse_active_qtd_list:\n\t"
314 		    "curr_qtd = 0x%p state = 0x%x", (void *)curr_qtd, state);
315 
316 		/* Obtain the  transfer wrapper  for this QTD */
317 		tw = (ehci_trans_wrapper_t *)EHCI_LOOKUP_ID(
318 		    (uint32_t)Get_QTD(curr_qtd->qtd_trans_wrapper));
319 
320 		ASSERT(tw != NULL);
321 
322 		pp = tw->tw_pipe_private;
323 
324 		USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
325 		    "ehci_traverse_active_qtd_list: "
326 		    "PP = 0x%p TW = 0x%p", pp, tw);
327 
328 		/*
329 		 * A QTD that is marked as RECLAIM has already been
330 		 * processed by QTD timeout handler & client driver
331 		 * has been informed through exception callback.
332 		 */
333 		if (state != EHCI_QTD_RECLAIM) {
334 			/* Look at the error status */
335 			error = ehci_parse_error(ehcip, curr_qtd);
336 
337 			if (error == USB_CR_OK) {
338 				ehci_handle_normal_qtd(ehcip, curr_qtd, tw);
339 			} else {
340 				/* handle the error condition */
341 				ehci_handle_error(ehcip, curr_qtd, error);
342 			}
343 		} else {
344 			USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
345 			    "ehci_traverse_active_qtd_list: "
346 			    "QTD State = %d", state);
347 		}
348 
349 		/* Deallocate this transfer descriptor */
350 		ehci_deallocate_qtd(ehcip, curr_qtd);
351 
352 		/*
353 		 * Deallocate the transfer wrapper if there are no more
354 		 * QTD's for the transfer wrapper.  ehci_deallocate_tw()
355 		 * will  not deallocate the tw for a periodic  endpoint
356 		 * since it will always have a QTD attached to it.
357 		 */
358 		ehci_deallocate_tw(ehcip, pp, tw);
359 
360 		curr_qtd = next_qtd;
361 	}
362 }
363 
364 
365 /*
366  * ehci_create_done_qtd_list:
367  *
368  * Create done qtd list from active qtd list.
369  */
370 ehci_qtd_t *
371 ehci_create_done_qtd_list(
372 	ehci_state_t		*ehcip)
373 {
374 	ehci_qtd_t		*curr_qtd = NULL, *next_qtd = NULL;
375 	ehci_qtd_t		*done_qtd_list = NULL, *last_done_qtd = NULL;
376 
377 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
378 	    "ehci_create_done_qtd_list:");
379 
380 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
381 
382 	curr_qtd = ehcip->ehci_active_qtd_list;
383 
384 	while (curr_qtd) {
385 
386 		/* Get next qtd from the active qtd list */
387 		next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
388 		    Get_QTD(curr_qtd->qtd_active_qtd_next));
389 
390 		/* Check this QTD has been processed by Host Controller */
391 		if (!(Get_QTD(curr_qtd->qtd_ctrl) &
392 		    EHCI_QTD_CTRL_ACTIVE_XACT)) {
393 
394 			/* Remove this QTD from active QTD list */
395 			ehci_remove_qtd_from_active_qtd_list(ehcip, curr_qtd);
396 
397 			Set_QTD(curr_qtd->qtd_active_qtd_next, NULL);
398 
399 			if (done_qtd_list) {
400 				Set_QTD(last_done_qtd->qtd_active_qtd_next,
401 				    ehci_qtd_cpu_to_iommu(ehcip, curr_qtd));
402 
403 				last_done_qtd = curr_qtd;
404 			} else {
405 				done_qtd_list = curr_qtd;
406 				last_done_qtd = curr_qtd;
407 			}
408 		}
409 
410 		curr_qtd = next_qtd;
411 	}
412 
413 	return (done_qtd_list);
414 }
415 
416 
417 /*
418  * ehci_parse_error:
419  *
420  * Parse the result for any errors.
421  */
422 static	usb_cr_t
423 ehci_parse_error(
424 	ehci_state_t		*ehcip,
425 	ehci_qtd_t		*qtd)
426 {
427 	uint_t			ctrl;
428 	ehci_trans_wrapper_t	*tw;
429 	ehci_pipe_private_t	*pp;
430 	uint_t			flag;
431 	usb_cr_t		error;
432 
433 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
434 	    "ehci_parse_error:");
435 
436 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
437 
438 	ASSERT(qtd != NULL);
439 
440 	/* Obtain the transfer wrapper from the QTD */
441 	tw = (ehci_trans_wrapper_t *)
442 	    EHCI_LOOKUP_ID((uint32_t)Get_QTD(qtd->qtd_trans_wrapper));
443 
444 	ASSERT(tw != NULL);
445 
446 	/* Obtain the pipe private structure */
447 	pp = tw->tw_pipe_private;
448 
449 	USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
450 	    "ehci_parse_error: PP 0x%p TW 0x%p", pp, tw);
451 
452 	ctrl = (uint_t)Get_QTD(qtd->qtd_ctrl);
453 
454 	/*
455 	 * Check the condition code of completed QTD and report errors
456 	 * if any. This checking will be done both for the general and
457 	 * the isochronous QTDs.
458 	 */
459 	if ((error = ehci_check_for_error(ehcip, pp, tw, qtd, ctrl)) !=
460 	    USB_CR_OK) {
461 		flag = EHCI_REMOVE_XFER_ALWAYS;
462 	} else {
463 		flag  = EHCI_REMOVE_XFER_IFLAST;
464 	}
465 
466 	/* Stop the transfer timer */
467 	ehci_stop_xfer_timer(ehcip, tw, flag);
468 
469 	return (error);
470 }
471 
472 
473 /*
474  * ehci_check_for_error:
475  *
476  * Check for any errors.
477  *
478  * NOTE: This function is also called from POLLED MODE.
479  */
480 usb_cr_t
481 ehci_check_for_error(
482 	ehci_state_t		*ehcip,
483 	ehci_pipe_private_t	*pp,
484 	ehci_trans_wrapper_t	*tw,
485 	ehci_qtd_t		*qtd,
486 	uint_t			ctrl)
487 {
488 	usb_cr_t		error = USB_CR_OK;
489 	uint_t			status, speed, mask;
490 
491 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
492 	    "ehci_check_for_error: qtd = 0x%p ctrl = 0x%x",
493 	    qtd, ctrl);
494 
495 	/*
496 	 * Find the usb device speed and get the corresponding
497 	 * error status mask.
498 	 */
499 	speed = Get_QH(pp->pp_qh->qh_ctrl) & EHCI_QH_CTRL_ED_SPEED;
500 	mask = (speed == EHCI_QH_CTRL_ED_HIGH_SPEED)?
501 	    EHCI_QTD_CTRL_HS_XACT_STATUS : EHCI_QTD_CTRL_NON_HS_XACT_STATUS;
502 
503 	/* Exclude halted transaction error condition */
504 	status = ctrl & EHCI_QTD_CTRL_XACT_STATUS & ~EHCI_QTD_CTRL_HALTED_XACT;
505 
506 	switch (status & mask) {
507 	case EHCI_QTD_CTRL_NO_ERROR:
508 		USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
509 		    "ehci_check_for_error: No Error");
510 		error = USB_CR_OK;
511 		break;
512 	case EHCI_QTD_CTRL_ACTIVE_XACT:
513 		USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
514 		    "ehci_check_for_error: Not accessed");
515 		error = USB_CR_NOT_ACCESSED;
516 		break;
517 	case EHCI_QTD_CTRL_HALTED_XACT:
518 		USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
519 		    "ehci_check_for_error: Halted");
520 		error = USB_CR_STALL;
521 		break;
522 	case EHCI_QTD_CTRL_BABBLE_DETECTED:
523 		USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
524 		    "ehci_check_for_error: Babble Detected");
525 		error = USB_CR_DATA_OVERRUN;
526 		break;
527 	case EHCI_QTD_CTRL_XACT_ERROR:
528 		/*
529 		 * An xacterr bit of one is not necessarily an error,
530 		 * the transaction might have completed successfully
531 		 * after some retries.
532 		 *
533 		 * Try to detect the case when the queue is halted,
534 		 * because the error counter was decremented from one
535 		 * down to zero after a transaction error.
536 		 */
537 		if (ctrl & EHCI_QTD_CTRL_HALTED_XACT && (ctrl &
538 		    EHCI_QTD_CTRL_ERR_COUNT_MASK) == 0) {
539 			USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
540 			    "ehci_check_for_error: Transaction Error");
541 			error = USB_CR_DEV_NOT_RESP;
542 		} else {
543 			USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
544 			    "ehci_check_for_error: No Error");
545 			error = USB_CR_OK;
546 		}
547 		break;
548 	case EHCI_QTD_CTRL_DATA_BUFFER_ERROR:
549 		/*
550 		 * Data buffer error is not necessarily an error,
551 		 * the transaction might have completed successfully
552 		 * after some retries. It can be ignored if the
553 		 * queue is not halted.
554 		 */
555 		if (!(ctrl & EHCI_QTD_CTRL_HALTED_XACT)) {
556 			USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
557 			    "ehci_check_for_error: Data buffer overrun or "
558 			    "underrun ignored");
559 			error = USB_CR_OK;
560 			break;
561 		}
562 
563 		if (tw->tw_direction == EHCI_QTD_CTRL_IN_PID) {
564 			USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
565 			    "ehci_check_for_error: Buffer Overrun");
566 			error = USB_CR_BUFFER_OVERRUN;
567 		} else	{
568 			USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
569 			    "ehci_check_for_error: Buffer Underrun");
570 			error = USB_CR_BUFFER_UNDERRUN;
571 		}
572 		break;
573 	case EHCI_QTD_CTRL_MISSED_uFRAME:
574 		USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
575 		    "ehci_check_for_error: Missed uFrame");
576 		error = USB_CR_NOT_ACCESSED;
577 		break;
578 	case EHCI_QTD_CTRL_PRD_SPLIT_XACT_ERR:
579 		USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
580 		    "ehci_check_for_error: Periodic split-transaction "
581 		    "receives an error handshake");
582 		error = USB_CR_UNSPECIFIED_ERR;
583 		break;
584 	default:
585 		USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
586 		    "ehci_check_for_error: Unspecified Error");
587 		error = USB_CR_UNSPECIFIED_ERR;
588 		break;
589 	}
590 
591 	/*
592 	 * Check for halted transaction error condition.
593 	 * Under short xfer conditions, EHCI HC will not return an error
594 	 * or halt the QH.  This is done manually later in
595 	 * ehci_check_for_short_xfer.
596 	 */
597 	if ((ctrl & EHCI_QTD_CTRL_HALTED_XACT) && (error == USB_CR_OK)) {
598 		USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
599 		    "ehci_check_for_error: Halted");
600 		error = USB_CR_STALL;
601 	}
602 
603 	if (error == USB_CR_OK) {
604 		error = ehci_check_for_short_xfer(ehcip, pp, tw, qtd);
605 	}
606 
607 	if (error) {
608 		uint_t qh_ctrl =  Get_QH(pp->pp_qh->qh_ctrl);
609 
610 		USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
611 		    "ehci_check_for_error: Error %d Device address %d "
612 		    "Endpoint number %d", error,
613 		    (qh_ctrl & EHCI_QH_CTRL_DEVICE_ADDRESS),
614 		    ((qh_ctrl & EHCI_QH_CTRL_ED_NUMBER) >>
615 		    EHCI_QH_CTRL_ED_NUMBER_SHIFT));
616 	}
617 
618 	return (error);
619 }
620 
621 /*
622  * ehci_check_for_short_xfer:
623  *
624  * Check to see if there was a short xfer condition.
625  *
626  * NOTE: This function is also called from POLLED MODE.
627  *	 But it doesn't do anything.
628  */
629 static usb_cr_t
630 ehci_check_for_short_xfer(
631 	ehci_state_t		*ehcip,
632 	ehci_pipe_private_t	*pp,
633 	ehci_trans_wrapper_t	*tw,
634 	ehci_qtd_t		*qtd)
635 {
636 	usb_cr_t		error = USB_CR_OK;
637 	usb_ep_descr_t		*eptd;
638 	uchar_t			attributes;
639 	uint32_t		residue = 0;
640 	usb_req_attrs_t		xfer_attrs;
641 	size_t			length;
642 	mblk_t			*mp = NULL;
643 	usb_opaque_t		xfer_reqp;
644 
645 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
646 	    "ehci_check_for_short_xfer:");
647 
648 	if (pp->pp_flag == EHCI_POLLED_MODE_FLAG) {
649 
650 		return (error);
651 	}
652 
653 	/*
654 	 * Check for short xfer error.	If this is a control pipe, only check
655 	 * if it is in the data phase.
656 	 */
657 	eptd = &pp->pp_pipe_handle->p_ep;
658 	attributes = eptd->bmAttributes & USB_EP_ATTR_MASK;
659 
660 	switch (attributes) {
661 	case USB_EP_ATTR_CONTROL:
662 		if (Get_QTD(qtd->qtd_ctrl_phase) !=
663 		    EHCI_CTRL_DATA_PHASE) {
664 
665 			break;
666 		}
667 		/* FALLTHROUGH */
668 	case USB_EP_ATTR_BULK:
669 	case USB_EP_ATTR_INTR:
670 		/*
671 		 * If "Total bytes of xfer" in control field of
672 		 * Transfer Descriptor (QTD) is not equal to zero,
673 		 * then, we sent/received less data from the usb
674 		 * device than requested. In that case, get the
675 		 * actual received data size.
676 		 */
677 		residue = (Get_QTD(qtd->qtd_ctrl) &
678 		    EHCI_QTD_CTRL_BYTES_TO_XFER) >>
679 		    EHCI_QTD_CTRL_BYTES_TO_XFER_SHIFT;
680 
681 		break;
682 	case USB_EP_ATTR_ISOCH:
683 
684 		break;
685 	}
686 
687 	if (residue) {
688 		USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
689 		    "ehci_check_for_short_xfer: residue=%d direction=0x%x",
690 		    residue, tw->tw_direction);
691 
692 		length = Get_QTD(qtd->qtd_xfer_offs) +
693 		    Get_QTD(qtd->qtd_xfer_len) - residue;
694 
695 		if (tw->tw_direction == EHCI_QTD_CTRL_IN_PID) {
696 			xfer_attrs = ehci_get_xfer_attrs(ehcip, pp, tw);
697 
698 			if (xfer_attrs & USB_ATTRS_SHORT_XFER_OK) {
699 				ehci_cleanup_data_underrun(ehcip, tw, qtd);
700 			} else {
701 				/* Halt the pipe to mirror OHCI behavior */
702 				Set_QH(pp->pp_qh->qh_status,
703 				    ((Get_QH(pp->pp_qh->qh_status) &
704 				    ~EHCI_QH_STS_ACTIVE) |
705 				    EHCI_QH_STS_HALTED));
706 				error = USB_CR_DATA_UNDERRUN;
707 			}
708 
709 			USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
710 			    "ehci_check_for_short_xfer: requested data=%lu "
711 			    "received data=%lu", tw->tw_length, length);
712 
713 			switch (attributes) {
714 			case USB_EP_ATTR_CONTROL:
715 			case USB_EP_ATTR_BULK:
716 			case USB_EP_ATTR_INTR:
717 				/* Save the actual received length */
718 				tw->tw_length = length;
719 
720 				break;
721 			case USB_EP_ATTR_ISOCH:
722 			default:
723 
724 				break;
725 			}
726 		} else {
727 			USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
728 			    "ehci_check_for_short_xfer: requested data=%lu "
729 			    "sent data=%lu", tw->tw_length, length);
730 
731 			xfer_reqp = tw->tw_curr_xfer_reqp;
732 
733 			switch (attributes) {
734 			case USB_EP_ATTR_CONTROL:
735 
736 				break;
737 			case USB_EP_ATTR_BULK:
738 				mp = (mblk_t *)((usb_bulk_req_t *)
739 				    (xfer_reqp))->bulk_data;
740 
741 				/* Increment the read pointer */
742 				mp->b_rptr = mp->b_rptr + length;
743 
744 				break;
745 			case USB_EP_ATTR_INTR:
746 				mp = (mblk_t *)((usb_intr_req_t *)
747 				    (xfer_reqp))->intr_data;
748 
749 				/* Increment the read pointer */
750 				mp->b_rptr = mp->b_rptr + length;
751 
752 				break;
753 			case USB_EP_ATTR_ISOCH:
754 			default:
755 
756 				break;
757 			}
758 		}
759 	}
760 
761 	return (error);
762 }
763 
764 /*
765  * ehci_handle_error:
766  *
767  * Inform USBA about occurred transaction errors by calling the USBA callback
768  * routine.
769  */
770 void
771 ehci_handle_error(
772 	ehci_state_t		*ehcip,
773 	ehci_qtd_t		*qtd,
774 	usb_cr_t		error)
775 {
776 	ehci_trans_wrapper_t	*tw;
777 	usba_pipe_handle_data_t	*ph;
778 	ehci_pipe_private_t	*pp;
779 	ehci_qtd_t		*tw_qtd = qtd;
780 	uchar_t			attributes;
781 	usb_intr_req_t		*curr_intr_reqp;
782 
783 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
784 	    "ehci_handle_error: error = 0x%x", error);
785 
786 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
787 
788 	ASSERT(qtd != NULL);
789 
790 	/* Print the values in the qtd */
791 	ehci_print_qtd(ehcip, qtd);
792 
793 	/* Obtain the transfer wrapper from the QTD */
794 	tw = (ehci_trans_wrapper_t *)
795 	    EHCI_LOOKUP_ID((uint32_t)Get_QTD(qtd->qtd_trans_wrapper));
796 
797 	ASSERT(tw != NULL);
798 
799 	/* Obtain the pipe private structure */
800 	pp = tw->tw_pipe_private;
801 
802 	ph = tw->tw_pipe_private->pp_pipe_handle;
803 	attributes = ph->p_ep.bmAttributes & USB_EP_ATTR_MASK;
804 
805 	/*
806 	 * Mark all QTDs belongs to this TW as RECLAIM
807 	 * so that we don't process them by mistake.
808 	 */
809 	while (tw_qtd) {
810 		/* Set QTD state to RECLAIM */
811 		Set_QTD(tw_qtd->qtd_state, EHCI_QTD_RECLAIM);
812 
813 		/* Get the next QTD from the wrapper */
814 		tw_qtd = ehci_qtd_iommu_to_cpu(ehcip,
815 		    Get_QTD(tw_qtd->qtd_tw_next_qtd));
816 	}
817 
818 	/*
819 	 * Special error handling
820 	 */
821 	if (tw->tw_direction == EHCI_QTD_CTRL_IN_PID) {
822 
823 		switch (attributes) {
824 		case USB_EP_ATTR_CONTROL:
825 			if (((ph->p_ep.bmAttributes &
826 			    USB_EP_ATTR_MASK) ==
827 			    USB_EP_ATTR_CONTROL) &&
828 			    (Get_QTD(qtd->qtd_ctrl_phase) ==
829 			    EHCI_CTRL_SETUP_PHASE)) {
830 
831 				break;
832 			}
833 			/* FALLTHROUGH */
834 		case USB_EP_ATTR_BULK:
835 			/*
836 			 * Call ehci_sendup_qtd_message
837 			 * to send message to upstream.
838 			 */
839 			ehci_sendup_qtd_message(ehcip, pp, tw, qtd, error);
840 
841 			return;
842 		case USB_EP_ATTR_INTR:
843 			curr_intr_reqp =
844 			    (usb_intr_req_t *)tw->tw_curr_xfer_reqp;
845 
846 			if (curr_intr_reqp->intr_attributes &
847 			    USB_ATTRS_ONE_XFER) {
848 
849 				ehci_handle_one_xfer_completion(ehcip, tw);
850 			}
851 
852 			/* Decrement periodic in request count */
853 			pp->pp_cur_periodic_req_cnt--;
854 			break;
855 		case USB_EP_ATTR_ISOCH:
856 			break;
857 		}
858 	}
859 
860 	ehci_hcdi_callback(ph, tw, error);
861 
862 	/* Check anybody is waiting for transfers completion event */
863 	ehci_check_for_transfers_completion(ehcip, pp);
864 }
865 
866 /*
867  * ehci_cleanup_data_underrun:
868  *
869  * Cleans up resources when a short xfer occurs.  Will only do cleanup if
870  * this pipe supports alternate_qtds.
871  *
872  * NOTE: This function is also called from POLLED MODE.
873  */
874 static void
875 ehci_cleanup_data_underrun(
876 	ehci_state_t		*ehcip,
877 	ehci_trans_wrapper_t	*tw,
878 	ehci_qtd_t		*qtd)
879 {
880 	ehci_qtd_t		*next_qtd, *temp_qtd;
881 
882 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
883 	    "ehci_cleanup_data_underrun: qtd=0x%p, tw=0x%p", qtd, tw);
884 
885 	/*
886 	 * Check if this transfer doesn't supports short_xfer or
887 	 * if this QTD is the last qtd in the tw.  If so there is
888 	 * no need for cleanup.
889 	 */
890 	if ((tw->tw_alt_qtd == NULL) || (qtd == tw->tw_qtd_tail)) {
891 		/* There is no need for cleanup */
892 		return;
893 	}
894 
895 	/* Start removing all the unused QTDs from the TW */
896 	next_qtd = (ehci_qtd_t *)ehci_qtd_iommu_to_cpu(ehcip,
897 	    Get_QTD(qtd->qtd_tw_next_qtd));
898 
899 	while (next_qtd) {
900 		tw->tw_num_qtds--;
901 
902 		ehci_remove_qtd_from_active_qtd_list(ehcip, next_qtd);
903 
904 		temp_qtd = next_qtd;
905 
906 		next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
907 		    Get_QTD(next_qtd->qtd_tw_next_qtd));
908 
909 		ehci_deallocate_qtd(ehcip, temp_qtd);
910 	}
911 
912 	ASSERT(tw->tw_num_qtds == 1);
913 }
914 
915 /*
916  * ehci_handle_normal_qtd:
917  */
918 static void
919 ehci_handle_normal_qtd(
920 	ehci_state_t		*ehcip,
921 	ehci_qtd_t		*qtd,
922 	ehci_trans_wrapper_t	*tw)
923 {
924 	ehci_pipe_private_t	*pp;	/* Pipe private field */
925 
926 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
927 	    "ehci_handle_normal_qtd:");
928 
929 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
930 	ASSERT(tw != NULL);
931 
932 	/* Obtain the pipe private structure */
933 	pp = tw->tw_pipe_private;
934 
935 	(*tw->tw_handle_qtd)(ehcip, pp, tw,
936 	    qtd, tw->tw_handle_callback_value);
937 
938 	/* Check anybody is waiting for transfers completion event */
939 	ehci_check_for_transfers_completion(ehcip, pp);
940 }
941 
942 
943 /*
944  * ehci_handle_ctrl_qtd:
945  *
946  * Handle a control Transfer Descriptor (QTD).
947  */
948 /* ARGSUSED */
949 void
950 ehci_handle_ctrl_qtd(
951 	ehci_state_t		*ehcip,
952 	ehci_pipe_private_t	*pp,
953 	ehci_trans_wrapper_t	*tw,
954 	ehci_qtd_t		*qtd,
955 	void			*tw_handle_callback_value)
956 {
957 	usba_pipe_handle_data_t	*ph = pp->pp_pipe_handle;
958 
959 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
960 	    "ehci_handle_ctrl_qtd: pp = 0x%p tw = 0x%p qtd = 0x%p state = 0x%x",
961 	    (void *)pp, (void *)tw, (void *)qtd, Get_QTD(qtd->qtd_ctrl_phase));
962 
963 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
964 
965 	/*
966 	 * A control transfer consists of three phases:
967 	 *
968 	 * Setup
969 	 * Data (optional)
970 	 * Status
971 	 *
972 	 * There is a QTD per phase. A QTD for a given phase isn't
973 	 * enqueued until the previous phase is finished. EHCI
974 	 * spec allows more than one  control transfer on a pipe
975 	 * within a frame. However, we've found that some devices
976 	 * can't handle this.
977 	 */
978 	tw->tw_num_qtds--;
979 	switch (Get_QTD(qtd->qtd_ctrl_phase)) {
980 	case EHCI_CTRL_SETUP_PHASE:
981 		USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
982 		    "Setup complete: pp 0x%p qtd 0x%p",
983 		    (void *)pp, (void *)qtd);
984 
985 		break;
986 	case EHCI_CTRL_DATA_PHASE:
987 		USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
988 		    "Data complete: pp 0x%p qtd 0x%p",
989 		    (void *)pp, (void *)qtd);
990 
991 		break;
992 	case EHCI_CTRL_STATUS_PHASE:
993 		/*
994 		 * On some particular hardware, status phase is seen to
995 		 * finish before data phase gets timeouted. Don't handle
996 		 * the transfer result here if not all qtds are finished.
997 		 * Let the timeout handler handle it.
998 		 */
999 		if (tw->tw_num_qtds != 0) {
1000 			USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
1001 			    "Status complete, but the transfer is not done: "
1002 			    "tw 0x%p, qtd 0x%p, tw_num_qtd 0x%d",
1003 			    (void *)tw, (void *)qtd, tw->tw_num_qtds);
1004 
1005 			ehci_print_qh(ehcip, pp->pp_qh);
1006 			ehci_print_qtd(ehcip, qtd);
1007 
1008 			break;
1009 		}
1010 
1011 		if ((tw->tw_length) &&
1012 		    (tw->tw_direction == EHCI_QTD_CTRL_IN_PID)) {
1013 			/*
1014 			 * Call ehci_sendup_qtd_message
1015 			 * to send message to upstream.
1016 			 */
1017 			ehci_sendup_qtd_message(ehcip,
1018 			    pp, tw, qtd, USB_CR_OK);
1019 		} else {
1020 			ehci_hcdi_callback(ph, tw, USB_CR_OK);
1021 		}
1022 
1023 		USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
1024 		    "Status complete: pp 0x%p qtd 0x%p",
1025 		    (void *)pp, (void *)qtd);
1026 
1027 		break;
1028 	}
1029 }
1030 
1031 
1032 /*
1033  * ehci_handle_bulk_qtd:
1034  *
1035  * Handle a bulk Transfer Descriptor (QTD).
1036  */
1037 /* ARGSUSED */
1038 void
1039 ehci_handle_bulk_qtd(
1040 	ehci_state_t		*ehcip,
1041 	ehci_pipe_private_t	*pp,
1042 	ehci_trans_wrapper_t	*tw,
1043 	ehci_qtd_t		*qtd,
1044 	void			*tw_handle_callback_value)
1045 {
1046 	usba_pipe_handle_data_t	*ph = pp->pp_pipe_handle;
1047 	usb_ep_descr_t		*eptd = &ph->p_ep;
1048 
1049 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
1050 	    "ehci_handle_bulk_qtd:");
1051 
1052 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
1053 
1054 	/*
1055 	 * Decrement the QTDs counter and check whether all the bulk
1056 	 * data has been send or received. If QTDs counter reaches
1057 	 * zero then inform client driver about completion current
1058 	 * bulk request. Other wise wait for completion of other bulk
1059 	 * QTDs or transactions on this pipe.
1060 	 */
1061 	if (--tw->tw_num_qtds != 0) {
1062 
1063 		USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
1064 		    "ehci_handle_bulk_qtd: Number of QTDs %d", tw->tw_num_qtds);
1065 
1066 		return;
1067 	}
1068 
1069 	/*
1070 	 * If this is a bulk in pipe, return the data to the client.
1071 	 * For a bulk out pipe, there is no need to do anything.
1072 	 */
1073 	if ((eptd->bEndpointAddress &
1074 	    USB_EP_DIR_MASK) == USB_EP_DIR_OUT) {
1075 
1076 		USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
1077 		    "ehci_handle_bulk_qtd: Bulk out pipe");
1078 
1079 		/* Do the callback */
1080 		ehci_hcdi_callback(ph, tw, USB_CR_OK);
1081 
1082 		return;
1083 	}
1084 
1085 	/* Call ehci_sendup_qtd_message to send message to upstream */
1086 	ehci_sendup_qtd_message(ehcip, pp, tw, qtd, USB_CR_OK);
1087 }
1088 
1089 
1090 /*
1091  * ehci_handle_intr_qtd:
1092  *
1093  * Handle a interrupt Transfer Descriptor (QTD).
1094  */
1095 /* ARGSUSED */
1096 void
1097 ehci_handle_intr_qtd(
1098 	ehci_state_t		*ehcip,
1099 	ehci_pipe_private_t	*pp,
1100 	ehci_trans_wrapper_t	*tw,
1101 	ehci_qtd_t		*qtd,
1102 	void			*tw_handle_callback_value)
1103 {
1104 	usb_intr_req_t		*curr_intr_reqp =
1105 	    (usb_intr_req_t *)tw->tw_curr_xfer_reqp;
1106 	usba_pipe_handle_data_t	*ph = pp->pp_pipe_handle;
1107 	usb_ep_descr_t		*eptd = &ph->p_ep;
1108 	usb_req_attrs_t		attrs;
1109 	int			error = USB_SUCCESS;
1110 
1111 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
1112 	    "ehci_handle_intr_qtd:");
1113 
1114 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
1115 
1116 	/* Get the interrupt xfer attributes */
1117 	attrs = curr_intr_reqp->intr_attributes;
1118 
1119 	/*
1120 	 * For a Interrupt OUT pipe, we just callback and we are done
1121 	 */
1122 	if ((eptd->bEndpointAddress & USB_EP_DIR_MASK) == USB_EP_DIR_OUT) {
1123 
1124 		USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
1125 		    "ehci_handle_intr_qtd: Intr out pipe, intr_reqp=0x%p,"
1126 		    "data=0x%p", curr_intr_reqp, curr_intr_reqp->intr_data);
1127 
1128 		/* Do the callback */
1129 		ehci_hcdi_callback(ph, tw, USB_CR_OK);
1130 
1131 		return;
1132 	}
1133 
1134 	/* Decrement number of interrupt request count */
1135 	pp->pp_cur_periodic_req_cnt--;
1136 
1137 	/*
1138 	 * Check usb flag whether USB_FLAGS_ONE_XFER flag is set
1139 	 * and if so, free duplicate request.
1140 	 */
1141 	if (attrs & USB_ATTRS_ONE_XFER) {
1142 		ehci_handle_one_xfer_completion(ehcip, tw);
1143 	}
1144 
1145 	/* Call ehci_sendup_qtd_message to callback into client */
1146 	ehci_sendup_qtd_message(ehcip, pp, tw, qtd, USB_CR_OK);
1147 
1148 	/*
1149 	 * If interrupt pipe state is still active, insert next Interrupt
1150 	 * request into the Host Controller's Interrupt list.  Otherwise
1151 	 * you are done.
1152 	 */
1153 	if ((pp->pp_state != EHCI_PIPE_STATE_ACTIVE) ||
1154 	    (ehci_state_is_operational(ehcip) != USB_SUCCESS)) {
1155 
1156 		return;
1157 	}
1158 
1159 	if ((error = ehci_allocate_intr_in_resource(ehcip, pp, tw, 0)) ==
1160 	    USB_SUCCESS) {
1161 		curr_intr_reqp = (usb_intr_req_t *)tw->tw_curr_xfer_reqp;
1162 
1163 		ASSERT(curr_intr_reqp != NULL);
1164 
1165 		tw->tw_num_qtds = 1;
1166 
1167 		if (ehci_allocate_tds_for_tw(ehcip, pp, tw, tw->tw_num_qtds) !=
1168 		    USB_SUCCESS) {
1169 			ehci_deallocate_intr_in_resource(ehcip, pp, tw);
1170 			error = USB_FAILURE;
1171 		}
1172 	}
1173 
1174 	if (error != USB_SUCCESS) {
1175 		/*
1176 		 * Set pipe state to stop polling and error to no
1177 		 * resource. Don't insert any more interrupt polling
1178 		 * requests.
1179 		 */
1180 		pp->pp_state = EHCI_PIPE_STATE_STOP_POLLING;
1181 		pp->pp_error = USB_CR_NO_RESOURCES;
1182 	} else {
1183 		ehci_insert_intr_req(ehcip, pp, tw, 0);
1184 
1185 		/* Increment number of interrupt request count */
1186 		pp->pp_cur_periodic_req_cnt++;
1187 
1188 		ASSERT(pp->pp_cur_periodic_req_cnt ==
1189 		    pp->pp_max_periodic_req_cnt);
1190 	}
1191 }
1192 
1193 
1194 /*
1195  * ehci_handle_one_xfer_completion:
1196  */
1197 static void
1198 ehci_handle_one_xfer_completion(
1199 	ehci_state_t		*ehcip,
1200 	ehci_trans_wrapper_t	*tw)
1201 {
1202 	usba_pipe_handle_data_t	*ph = tw->tw_pipe_private->pp_pipe_handle;
1203 	ehci_pipe_private_t	*pp = tw->tw_pipe_private;
1204 	usb_intr_req_t		*curr_intr_reqp =
1205 	    (usb_intr_req_t *)tw->tw_curr_xfer_reqp;
1206 
1207 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
1208 	    "ehci_handle_one_xfer_completion: tw = 0x%p", tw);
1209 
1210 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
1211 	ASSERT(curr_intr_reqp->intr_attributes & USB_ATTRS_ONE_XFER);
1212 
1213 	pp->pp_state = EHCI_PIPE_STATE_IDLE;
1214 
1215 	/*
1216 	 * For one xfer, we need to copy back data ptr
1217 	 * and free current request
1218 	 */
1219 	((usb_intr_req_t *)(pp->pp_client_periodic_in_reqp))->
1220 	    intr_data = ((usb_intr_req_t *)
1221 	    (tw->tw_curr_xfer_reqp))->intr_data;
1222 
1223 	((usb_intr_req_t *)tw->tw_curr_xfer_reqp)->intr_data = NULL;
1224 
1225 	/* Now free duplicate current request */
1226 	usb_free_intr_req((usb_intr_req_t *)tw-> tw_curr_xfer_reqp);
1227 
1228 	mutex_enter(&ph->p_mutex);
1229 	ph->p_req_count--;
1230 	mutex_exit(&ph->p_mutex);
1231 
1232 	/* Make client's request the current request */
1233 	tw->tw_curr_xfer_reqp = pp->pp_client_periodic_in_reqp;
1234 	pp->pp_client_periodic_in_reqp = NULL;
1235 }
1236 
1237 
1238 /*
1239  * ehci_sendup_qtd_message:
1240  *	copy data, if necessary and do callback
1241  */
1242 /* ARGSUSED */
1243 static void
1244 ehci_sendup_qtd_message(
1245 	ehci_state_t		*ehcip,
1246 	ehci_pipe_private_t	*pp,
1247 	ehci_trans_wrapper_t	*tw,
1248 	ehci_qtd_t		*qtd,
1249 	usb_cr_t		error)
1250 {
1251 	usb_ep_descr_t		*eptd = &pp->pp_pipe_handle->p_ep;
1252 	usba_pipe_handle_data_t	*ph = pp->pp_pipe_handle;
1253 	usb_opaque_t		curr_xfer_reqp = tw->tw_curr_xfer_reqp;
1254 	size_t			skip_len = 0;
1255 	size_t			length;
1256 	uchar_t			*buf;
1257 	mblk_t			*mp;
1258 
1259 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
1260 
1261 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
1262 	    "ehci_sendup_qtd_message:");
1263 
1264 	ASSERT(tw != NULL);
1265 
1266 	length = tw->tw_length;
1267 
1268 	if ((eptd->bmAttributes & USB_EP_ATTR_MASK) == USB_EP_ATTR_CONTROL) {
1269 		/* Get the correct length */
1270 		if (((usb_ctrl_req_t *)curr_xfer_reqp)->ctrl_wLength)
1271 			length = length - EHCI_MAX_QTD_BUF_SIZE;
1272 		else
1273 			length = length - SETUP_SIZE;
1274 
1275 		/* Set the length of the buffer to skip */
1276 		skip_len = EHCI_MAX_QTD_BUF_SIZE;
1277 	}
1278 
1279 	/* Copy the data into the mblk_t */
1280 	buf = (uchar_t *)tw->tw_buf + skip_len;
1281 
1282 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
1283 	    "ehci_sendup_qtd_message: length %ld error %d", length, error);
1284 
1285 	/* Get the message block */
1286 	switch (eptd->bmAttributes & USB_EP_ATTR_MASK) {
1287 	case USB_EP_ATTR_CONTROL:
1288 		mp = ((usb_ctrl_req_t *)curr_xfer_reqp)->ctrl_data;
1289 		break;
1290 	case USB_EP_ATTR_BULK:
1291 		mp = ((usb_bulk_req_t *)curr_xfer_reqp)->bulk_data;
1292 		break;
1293 	case USB_EP_ATTR_INTR:
1294 		mp = ((usb_intr_req_t *)curr_xfer_reqp)->intr_data;
1295 		break;
1296 	case USB_EP_ATTR_ISOCH:
1297 		/* Isoc messages must not go through this path */
1298 		mp = NULL;
1299 		break;
1300 	}
1301 
1302 	ASSERT(mp != NULL);
1303 
1304 	if (length) {
1305 		/*
1306 		 * Update kstat byte counts
1307 		 * The control endpoints don't have direction bits so in
1308 		 * order for control stats to be counted correctly an in
1309 		 * bit must be faked on a control read.
1310 		 */
1311 		if ((eptd->bmAttributes & USB_EP_ATTR_MASK) ==
1312 		    USB_EP_ATTR_CONTROL) {
1313 			ehci_do_byte_stats(ehcip, length,
1314 			    eptd->bmAttributes, USB_EP_DIR_IN);
1315 		} else {
1316 			ehci_do_byte_stats(ehcip, length,
1317 			    eptd->bmAttributes, eptd->bEndpointAddress);
1318 		}
1319 
1320 		/* Sync IO buffer */
1321 		Sync_IO_Buffer(tw->tw_dmahandle, (skip_len + length));
1322 
1323 		/* since we specified NEVERSWAP, we can just use bcopy */
1324 		bcopy(buf, mp->b_rptr, length);
1325 
1326 		/* Increment the write pointer */
1327 		mp->b_wptr = mp->b_wptr + length;
1328 	} else {
1329 		USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
1330 		    "ehci_sendup_qtd_message: Zero length packet");
1331 	}
1332 
1333 	ehci_hcdi_callback(ph, tw, error);
1334 }
1335