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