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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
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 specific EHCI code used in POLLED mode. This
36  * code is in a separate file since it will never become part of the EHCI
37  * driver.
38  */
39 
40 #include <sys/usb/usba/usbai_version.h>
41 #include <sys/usb/hcd/ehci/ehcid.h>
42 #include <sys/usb/hcd/ehci/ehci_xfer.h>
43 #include <sys/usb/hcd/ehci/ehci_intr.h>
44 #include <sys/usb/hcd/ehci/ehci_util.h>
45 #include <sys/usb/hcd/ehci/ehci_polled.h>
46 
47 /*
48  * Internal Function Prototypes
49  */
50 
51 /* Polled initialization routines */
52 static int	ehci_polled_init(
53 				usba_pipe_handle_data_t	*ph,
54 				ehci_state_t		*ehcip,
55 				usb_console_info_impl_t	*console_input_info);
56 
57 /* Polled deinitialization routines */
58 static int	ehci_polled_fini(ehci_polled_t		*ehci_polledp);
59 
60 /* Polled save state routines */
61 static void	ehci_polled_save_state(ehci_polled_t	*ehci_polledp);
62 
63 /* Polled restore state routines */
64 static void	ehci_polled_restore_state(ehci_polled_t	*ehci_polledp);
65 static void	ehci_polled_stop_processing(
66 				ehci_polled_t		*ehci_polledp);
67 static void	ehci_polled_start_processing(
68 				ehci_polled_t		*ehci_polledp);
69 
70 /* Polled read routines */
71 static int	ehci_polled_process_active_intr_qtd_list(
72 				ehci_polled_t		*ehci_polledp);
73 static int	ehci_polled_handle_normal_qtd(
74 				ehci_polled_t		*ehci_polledp,
75 				ehci_qtd_t		*qtd);
76 static void	ehci_polled_insert_qtd(
77 				ehci_polled_t		*ehci_polledp,
78 				ehci_qtd_t		*qtd);
79 static void	ehci_polled_fill_in_qtd(
80 				ehci_state_t		*ehcip,
81 				ehci_qtd_t		*qtd,
82 				uint_t			qtd_ctrl,
83 				uint32_t		qtd_iommu_cbp,
84 				size_t			qtd_length,
85 				ehci_trans_wrapper_t	*tw);
86 static void	ehci_polled_insert_qtd_on_tw(
87 				ehci_state_t		*ehcip,
88 				ehci_trans_wrapper_t	*tw,
89 				ehci_qtd_t		*qtd);
90 static ehci_qtd_t *ehci_polled_create_done_qtd_list(
91 				ehci_polled_t		*ehci_polledp);
92 static void	ehci_polled_insert_qtd_into_active_intr_qtd_list(
93 				ehci_polled_t		*ehci_polledp,
94 				ehci_qtd_t		*curr_qtd);
95 static void	ehci_polled_remove_qtd_from_active_intr_qtd_list(
96 				ehci_polled_t		*ehci_polledp,
97 				ehci_qtd_t		*curr_qtd);
98 static void	ehci_polled_traverse_qtds(
99 				ehci_polled_t		*ehci_polledp,
100 				usba_pipe_handle_data_t	*ph);
101 static void	ehci_polled_finish_interrupt(
102 				ehci_state_t		*ehcip,
103 				uint_t			intr);
104 
105 /*
106  * POLLED entry points
107  *
108  * These functions are entry points into the POLLED code.
109  */
110 
111 /*
112  * ehci_hcdi_polled_input_init:
113  *
114  * This is the initialization routine for handling the USB keyboard
115  * in POLLED mode.  This routine is not called from POLLED mode, so
116  * it is OK to acquire mutexes.
117  */
118 int
119 ehci_hcdi_polled_input_init(
120 	usba_pipe_handle_data_t	*ph,
121 	uchar_t			**polled_buf,
122 	usb_console_info_impl_t	*console_input_info)
123 {
124 	ehci_polled_t		*ehci_polledp;
125 	ehci_state_t		*ehcip;
126 	int			ret;
127 
128 	ehcip = ehci_obtain_state(ph->p_usba_device->usb_root_hub_dip);
129 
130 	/*
131 	 * Grab the ehci_int_mutex so that things don't change on us
132 	 * if an interrupt comes in.
133 	 */
134 	mutex_enter(&ehcip->ehci_int_mutex);
135 
136 	ret = ehci_polled_init(ph, ehcip, console_input_info);
137 
138 	if (ret != USB_SUCCESS) {
139 
140 		/* Allow interrupts to continue */
141 		mutex_exit(&ehcip->ehci_int_mutex);
142 		return (ret);
143 	}
144 
145 	ehci_polledp = (ehci_polled_t *)console_input_info->uci_private;
146 
147 	/*
148 	 * Mark the structure so that if we are using it, we don't free
149 	 * the structures if one of them is unplugged.
150 	 */
151 	ehci_polledp->ehci_polled_flags |= POLLED_INPUT_MODE;
152 
153 	/* increase the counter for keyboard connected */
154 	ehcip->ehci_polled_kbd_count ++;
155 
156 	/*
157 	 * This is the buffer we will copy characters into. It will be
158 	 * copied into at this layer, so we need to keep track of it.
159 	 */
160 	ehci_polledp->ehci_polled_buf =
161 	    (uchar_t *)kmem_zalloc(POLLED_RAW_BUF_SIZE, KM_SLEEP);
162 
163 	*polled_buf = ehci_polledp->ehci_polled_buf;
164 
165 	/*
166 	 * This is a software workaround to fix schizo hardware bug.
167 	 * Existence of "no-prom-cdma-sync"  property means consistent
168 	 * dma sync should not be done while in prom or polled mode.
169 	 */
170 	if (ddi_prop_exists(DDI_DEV_T_ANY, ehcip->ehci_dip,
171 	    DDI_PROP_NOTPROM, "no-prom-cdma-sync")) {
172 		ehci_polledp->ehci_polled_no_sync_flag = B_TRUE;
173 	}
174 
175 	/* Allow interrupts to continue */
176 	mutex_exit(&ehcip->ehci_int_mutex);
177 
178 	return (USB_SUCCESS);
179 }
180 
181 
182 /*
183  * ehci_hcdi_polled_input_fini:
184  */
185 int
186 ehci_hcdi_polled_input_fini(usb_console_info_impl_t *info)
187 {
188 	ehci_polled_t		*ehci_polledp;
189 	ehci_state_t		*ehcip;
190 	int			ret;
191 
192 	ehci_polledp = (ehci_polled_t *)info->uci_private;
193 
194 	ehcip = ehci_polledp->ehci_polled_ehcip;
195 
196 	mutex_enter(&ehcip->ehci_int_mutex);
197 
198 	/*
199 	 * Reset the POLLED_INPUT_MODE flag so that we can tell if
200 	 * this structure is in use in the ehci_polled_fini routine.
201 	 */
202 	ehci_polledp->ehci_polled_flags &= ~POLLED_INPUT_MODE;
203 
204 	/* decrease the counter for keyboard disconnected */
205 	ehcip->ehci_polled_kbd_count --;
206 
207 	/* Free the buffer that we copied data into */
208 	kmem_free(ehci_polledp->ehci_polled_buf, POLLED_RAW_BUF_SIZE);
209 
210 	ret = ehci_polled_fini(ehci_polledp);
211 
212 	mutex_exit(&ehcip->ehci_int_mutex);
213 
214 	return (ret);
215 }
216 
217 
218 /*
219  * ehci_hcdi_polled_input_enter:
220  *
221  * This is where we enter into POLLED mode.  This routine sets up
222  * everything so that calls to	ehci_hcdi_polled_read will return
223  * characters.
224  */
225 int
226 ehci_hcdi_polled_input_enter(usb_console_info_impl_t *info)
227 {
228 	ehci_polled_t		*ehci_polledp;
229 
230 	ehci_polledp = (ehci_polled_t *)info->uci_private;
231 
232 	ehci_polledp->ehci_polled_entry++;
233 
234 	/*
235 	 * If the controller is already switched over, just return
236 	 */
237 	if (ehci_polledp->ehci_polled_entry > 1) {
238 
239 		return (USB_SUCCESS);
240 	}
241 
242 	ehci_polled_save_state(ehci_polledp);
243 
244 	ehci_polledp->ehci_polled_flags |= POLLED_INPUT_MODE_INUSE;
245 
246 	return (USB_SUCCESS);
247 }
248 
249 
250 /*
251  * ehci_hcdi_polled_input_exit:
252  *
253  * This is where we exit POLLED mode. This routine restores
254  * everything that is needed to continue operation.
255  */
256 int
257 ehci_hcdi_polled_input_exit(usb_console_info_impl_t *info)
258 {
259 	ehci_polled_t		*ehci_polledp;
260 
261 	ehci_polledp = (ehci_polled_t *)info->uci_private;
262 
263 	ehci_polledp->ehci_polled_entry--;
264 
265 	/*
266 	 * If there are still outstanding "enters", just return
267 	 */
268 	if (ehci_polledp->ehci_polled_entry > 0) {
269 
270 		return (USB_SUCCESS);
271 	}
272 
273 	ehci_polledp->ehci_polled_flags &= ~POLLED_INPUT_MODE_INUSE;
274 
275 	ehci_polled_restore_state(ehci_polledp);
276 
277 	return (USB_SUCCESS);
278 }
279 
280 
281 /*
282  * ehci_hcdi_polled_read:
283  *
284  * Get a key character
285  */
286 int
287 ehci_hcdi_polled_read(
288 	usb_console_info_impl_t	*info,
289 	uint_t			*num_characters)
290 {
291 	ehci_state_t		*ehcip;
292 	ehci_polled_t		*ehci_polledp;
293 	uint_t			intr;
294 
295 	ehci_polledp = (ehci_polled_t *)info->uci_private;
296 
297 	ehcip = ehci_polledp->ehci_polled_ehcip;
298 
299 #ifndef lint
300 	_NOTE(NO_COMPETING_THREADS_NOW);
301 #endif
302 
303 	*num_characters = 0;
304 
305 	intr = ((Get_OpReg(ehci_status) & Get_OpReg(ehci_interrupt)) &
306 	    (EHCI_INTR_FRAME_LIST_ROLLOVER |
307 	    EHCI_INTR_USB | EHCI_INTR_USB_ERROR));
308 
309 	/*
310 	 * Check whether any frame list rollover interrupt is pending
311 	 * and if it is pending, process this interrupt.
312 	 */
313 	if (intr & EHCI_INTR_FRAME_LIST_ROLLOVER) {
314 		/* Check any frame list rollover interrupt is pending */
315 		ehci_handle_frame_list_rollover(ehcip);
316 		ehci_polled_finish_interrupt(ehcip,
317 		    EHCI_INTR_FRAME_LIST_ROLLOVER);
318 	}
319 
320 	/* Check for any USB transaction completion notification */
321 	if (intr & (EHCI_INTR_USB | EHCI_INTR_USB_ERROR)) {
322 		ehcip->ehci_polled_read_count ++;
323 		/* Process any QTD's on the active interrupt qtd list */
324 		*num_characters =
325 		    ehci_polled_process_active_intr_qtd_list(ehci_polledp);
326 
327 		if (ehcip->ehci_polled_read_count ==
328 		    ehcip->ehci_polled_enter_count) {
329 			/* Acknowledge the frame list rollover interrupt */
330 			ehci_polled_finish_interrupt(ehcip,
331 			    intr & (EHCI_INTR_USB | EHCI_INTR_USB_ERROR));
332 			ehcip->ehci_polled_read_count = 0;
333 		}
334 	}
335 
336 #ifndef lint
337 	_NOTE(COMPETING_THREADS_NOW);
338 #endif
339 
340 	return (USB_SUCCESS);
341 }
342 
343 
344 /*
345  * Internal Functions
346  */
347 
348 /*
349  * Polled initialization routines
350  */
351 
352 
353 /*
354  * ehci_polled_init:
355  *
356  * Initialize generic information Uthat is needed to provide USB/POLLED
357  * support.
358  */
359 static int
360 ehci_polled_init(
361 	usba_pipe_handle_data_t	*ph,
362 	ehci_state_t		*ehcip,
363 	usb_console_info_impl_t	*console_info)
364 {
365 	ehci_polled_t		*ehci_polledp;
366 	ehci_pipe_private_t	*pp;
367 	ehci_qtd_t		*qtd;
368 
369 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
370 
371 	/*
372 	 * We have already initialized this structure. If the structure
373 	 * has already been initialized, then we don't need to redo it.
374 	 */
375 	if (console_info->uci_private) {
376 
377 		return (USB_SUCCESS);
378 	}
379 
380 	/* Allocate and intitialize a state structure */
381 	ehci_polledp = (ehci_polled_t *)
382 	    kmem_zalloc(sizeof (ehci_polled_t), KM_SLEEP);
383 
384 	console_info->uci_private = (usb_console_info_private_t)ehci_polledp;
385 
386 	/*
387 	 * Store away the ehcip so that we can get to it when we are in
388 	 * POLLED mode. We don't want to have to call ehci_obtain_state
389 	 * every time we want to access this structure.
390 	 */
391 	ehci_polledp->ehci_polled_ehcip = ehcip;
392 	/*
393 	 * Save usb device and endpoint number information from the usb
394 	 * pipe handle.
395 	 */
396 	mutex_enter(&ph->p_mutex);
397 	ehci_polledp->ehci_polled_usb_dev = ph->p_usba_device;
398 	ehci_polledp->ehci_polled_ep_addr = ph->p_ep.bEndpointAddress;
399 	mutex_exit(&ph->p_mutex);
400 
401 	/*
402 	 * Allocate memory to make duplicate of original usb pipe handle.
403 	 */
404 	ehci_polledp->ehci_polled_input_pipe_handle =
405 	    kmem_zalloc(sizeof (usba_pipe_handle_data_t), KM_SLEEP);
406 
407 	/*
408 	 * Copy the USB handle into the new pipe handle. Also
409 	 * create new lock for the new pipe handle.
410 	 */
411 	bcopy((void *)ph,
412 	    (void *)ehci_polledp->ehci_polled_input_pipe_handle,
413 	    sizeof (usba_pipe_handle_data_t));
414 
415 	/*
416 	 * uint64_t typecast to make sure amd64 can compile
417 	 */
418 	mutex_init(&ehci_polledp->ehci_polled_input_pipe_handle->p_mutex,
419 	    NULL, MUTEX_DRIVER, DDI_INTR_PRI(ehcip->ehci_intr_pri));
420 
421 	/*
422 	 * Create a new ehci pipe private structure
423 	 */
424 	pp = (ehci_pipe_private_t *)
425 	    kmem_zalloc(sizeof (ehci_pipe_private_t), KM_SLEEP);
426 
427 	/*
428 	 * Store the pointer in the pipe handle. This structure was also
429 	 * just allocated.
430 	 */
431 	mutex_enter(&ehci_polledp->ehci_polled_input_pipe_handle->p_mutex);
432 
433 	ehci_polledp->ehci_polled_input_pipe_handle->
434 	    p_hcd_private = (usb_opaque_t)pp;
435 
436 	mutex_exit(&ehci_polledp->ehci_polled_input_pipe_handle->p_mutex);
437 
438 	/*
439 	 * Store a pointer to the pipe handle. This structure was  just
440 	 * allocated and it is not in use yet.	The locking is there to
441 	 * satisfy warlock.
442 	 */
443 	mutex_enter(&ph->p_mutex);
444 
445 	bcopy(&ph->p_policy, &pp->pp_policy, sizeof (usb_pipe_policy_t));
446 
447 	mutex_exit(&ph->p_mutex);
448 
449 	pp->pp_pipe_handle = ehci_polledp->ehci_polled_input_pipe_handle;
450 
451 	/*
452 	 * Allocate a dummy for the interrupt table. This dummy will be
453 	 * put into the action when we	switch interrupt  tables during
454 	 * ehci_hcdi_polled_enter. Dummy is placed on the unused lattice
455 	 * entries. When the QH is allocated we will replace dummy QH by
456 	 * valid interrupt QH in one or more locations in the interrupt
457 	 * lattice depending on the requested polling interval. Also we
458 	 * will hang a dummy QTD to the QH & dummy QTD is used to indicate
459 	 * the end of the QTD chain.
460 	 */
461 	ehci_polledp->ehci_polled_dummy_qh =
462 	    ehci_alloc_qh(ehcip, NULL, EHCI_POLLED_MODE_FLAG);
463 
464 	if (ehci_polledp->ehci_polled_dummy_qh == NULL) {
465 
466 		return (USB_NO_RESOURCES);
467 	}
468 
469 	/*
470 	 * Allocate the interrupt endpoint. This QH will be inserted in
471 	 * to the lattice chain for the  keyboard device. This endpoint
472 	 * will have the QTDs hanging off of it for the processing.
473 	 */
474 	ehci_polledp->ehci_polled_qh = ehci_alloc_qh(
475 	    ehcip, ph, EHCI_POLLED_MODE_FLAG);
476 
477 	if (ehci_polledp->ehci_polled_qh == NULL) {
478 
479 		return (USB_NO_RESOURCES);
480 	}
481 
482 	/* Set the state of pipe as idle */
483 	pp->pp_state = EHCI_PIPE_STATE_IDLE;
484 
485 	/* Set polled mode flag */
486 	pp->pp_flag = EHCI_POLLED_MODE_FLAG;
487 
488 	/* Insert the endpoint onto the pipe handle */
489 	pp->pp_qh = ehci_polledp->ehci_polled_qh;
490 
491 	/*
492 	 * Set soft interrupt handler flag in the normal mode usb
493 	 * pipe handle.
494 	 */
495 	mutex_enter(&ph->p_mutex);
496 	ph->p_spec_flag |= USBA_PH_FLAG_USE_SOFT_INTR;
497 	mutex_exit(&ph->p_mutex);
498 
499 	/*
500 	 * Insert a Interrupt polling request onto the endpoint.
501 	 *
502 	 * There will now be two QTDs on the QH, one is the dummy QTD that
503 	 * was allocated above in the  ehci_alloc_qh and this new one.
504 	 */
505 	if ((ehci_start_periodic_pipe_polling(ehcip,
506 	    ehci_polledp->ehci_polled_input_pipe_handle,
507 	    NULL, USB_FLAGS_SLEEP)) != USB_SUCCESS) {
508 
509 		return (USB_NO_RESOURCES);
510 	}
511 
512 	/* Get the given new interrupt qtd */
513 	qtd = (ehci_qtd_t *)(ehci_qtd_iommu_to_cpu(ehcip,
514 	    (Get_QH(pp->pp_qh->qh_next_qtd) & EHCI_QH_NEXT_QTD_PTR)));
515 
516 	/* Insert this qtd into active interrupt QTD list */
517 	ehci_polled_insert_qtd_into_active_intr_qtd_list(ehci_polledp, qtd);
518 
519 	return (USB_SUCCESS);
520 }
521 
522 
523 /*
524  * Polled deinitialization routines
525  */
526 
527 
528 /*
529  * ehci_polled_fini:
530  */
531 static int
532 ehci_polled_fini(ehci_polled_t	*ehci_polledp)
533 {
534 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
535 	ehci_pipe_private_t	*pp;
536 
537 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
538 
539 	/* If the structure is already in use, then don't free it */
540 	if (ehci_polledp->ehci_polled_flags & POLLED_INPUT_MODE) {
541 
542 		return (USB_SUCCESS);
543 	}
544 
545 	pp = (ehci_pipe_private_t *)
546 	    ehci_polledp->ehci_polled_input_pipe_handle->p_hcd_private;
547 
548 	/* Deallocate all the pre-allocated interrupt requests */
549 	ehci_handle_outstanding_requests(ehcip, pp);
550 
551 	/*
552 	 * Traverse the list of QTD's on this endpoint and these QTD's
553 	 * have outstanding transfer requests. Since list processing
554 	 * is stopped, these QTDs can be deallocated.
555 	 */
556 	ehci_polled_traverse_qtds(ehci_polledp, pp->pp_pipe_handle);
557 
558 	/* Free DMA resources */
559 	ehci_free_dma_resources(ehcip, pp->pp_pipe_handle);
560 
561 	/*
562 	 * Deallocate the endpoint descriptors that we allocated
563 	 * with ehci_alloc_qh.
564 	 */
565 	if (ehci_polledp->ehci_polled_dummy_qh) {
566 		ehci_deallocate_qh(ehcip, ehci_polledp->ehci_polled_dummy_qh);
567 	}
568 
569 	if (ehci_polledp->ehci_polled_qh) {
570 		ehci_deallocate_qh(ehcip, ehci_polledp->ehci_polled_qh);
571 	}
572 
573 	mutex_destroy(&ehci_polledp->ehci_polled_input_pipe_handle->p_mutex);
574 
575 	/*
576 	 * Destroy everything about the pipe that we allocated in
577 	 * ehci_polled_duplicate_pipe_handle
578 	 */
579 	kmem_free(pp, sizeof (ehci_pipe_private_t));
580 
581 	kmem_free(ehci_polledp->ehci_polled_input_pipe_handle,
582 	    sizeof (usba_pipe_handle_data_t));
583 
584 	/*
585 	 * We use this field to determine if a QTD is for input or not,
586 	 * so NULL the pointer so we don't check deallocated data.
587 	 */
588 	ehci_polledp->ehci_polled_input_pipe_handle = NULL;
589 
590 	/*
591 	 * Finally, free off the structure that we use to keep track
592 	 * of all this.
593 	 */
594 	kmem_free(ehci_polledp, sizeof (ehci_polled_t));
595 
596 	return (USB_SUCCESS);
597 }
598 
599 
600 /*
601  * Polled save state routines
602  */
603 
604 
605 /*
606  * ehci_polled_save_state:
607  */
608 static void
609 ehci_polled_save_state(ehci_polled_t	*ehci_polledp)
610 {
611 	int				i;
612 	ehci_state_t			*ehcip;
613 	uint_t				polled_toggle;
614 	uint_t				real_toggle;
615 	ehci_pipe_private_t		*pp = NULL; /* Normal mode Pipe */
616 	ehci_pipe_private_t		*polled_pp; /* Polled mode Pipe */
617 	usba_pipe_handle_data_t		*ph;
618 	uint8_t				ep_addr;
619 	ehci_regs_t			*ehci_polled_regsp;
620 	ehci_qh_t			*qh;
621 
622 #ifndef lint
623 	_NOTE(NO_COMPETING_THREADS_NOW);
624 #endif
625 
626 	/*
627 	 * If either of these two flags are set, then we have already
628 	 * saved off the state information and setup the controller.
629 	 */
630 	if (ehci_polledp->ehci_polled_flags & POLLED_INPUT_MODE_INUSE) {
631 #ifndef lint
632 		_NOTE(COMPETING_THREADS_NOW);
633 #endif
634 		return;
635 	}
636 
637 	ehcip = ehci_polledp->ehci_polled_ehcip;
638 
639 	/*
640 	 * Check if the number of keyboard reach the max number we can
641 	 * support in polled mode
642 	 */
643 	if (++ ehcip->ehci_polled_enter_count > MAX_NUM_FOR_KEYBOARD) {
644 #ifndef lint
645 		_NOTE(COMPETING_THREADS_NOW);
646 #endif
647 		return;
648 	}
649 	ehci_polled_regsp = &ehcip->ehci_polled_save_regs;
650 
651 	/* Get the endpoint addr. */
652 	ep_addr = ehci_polledp->ehci_polled_ep_addr;
653 
654 	/* Get the normal mode usb pipe handle */
655 	ph = usba_hcdi_get_ph_data(ehci_polledp->ehci_polled_usb_dev, ep_addr);
656 
657 	/*
658 	 * The first enter keyboard entry should save info of the normal mode,
659 	 * disable all list processing and interrupt, initialize the
660 	 * frame list table with dummy QHs.
661 	 */
662 	if (ehcip->ehci_polled_enter_count == 1) {
663 		/*
664 		 * Save the current normal mode ehci registers	and later this
665 		 * saved register copy is used to replace some of required ehci
666 		 * registers before switching from polled mode to normal mode.
667 		 */
668 
669 		bzero((void *)ehci_polled_regsp, sizeof (ehci_regs_t));
670 
671 		/* Save current ehci registers */
672 		ehci_polled_regsp->ehci_command = Get_OpReg(ehci_command);
673 		ehci_polled_regsp->ehci_interrupt = Get_OpReg(ehci_interrupt);
674 		ehci_polled_regsp->ehci_ctrl_segment =
675 		    Get_OpReg(ehci_ctrl_segment);
676 		ehci_polled_regsp->
677 		    ehci_async_list_addr = Get_OpReg(ehci_async_list_addr);
678 		ehci_polled_regsp->ehci_config_flag =
679 		    Get_OpReg(ehci_config_flag);
680 		ehci_polled_regsp->ehci_periodic_list_base =
681 		    Get_OpReg(ehci_periodic_list_base);
682 
683 		/* Disable all list processing and interrupts */
684 		Set_OpReg(ehci_command, Get_OpReg(ehci_command) &
685 		    ~(EHCI_CMD_ASYNC_SCHED_ENABLE |
686 		    EHCI_CMD_PERIODIC_SCHED_ENABLE));
687 
688 		/* Wait for few milliseconds */
689 		drv_usecwait(EHCI_POLLED_TIMEWAIT);
690 
691 		/* Save any unprocessed normal mode ehci interrupts */
692 		ehcip->ehci_missed_intr_sts = EHCI_INTR_USB;
693 
694 		/*
695 		 * Save the current interrupt lattice and  replace this lattice
696 		 * with an lattice used in POLLED mode. We will restore lattice
697 		 * back when we exit from the POLLED mode.
698 		 */
699 		for (i = 0; i < EHCI_NUM_PERIODIC_FRAME_LISTS; i++) {
700 			ehcip->ehci_polled_frame_list_table[i] =
701 			    (ehci_qh_t *)(uintptr_t)Get_PFLT(ehcip->
702 			    ehci_periodic_frame_list_tablep->
703 			    ehci_periodic_frame_list_table[i]);
704 		}
705 
706 		/*
707 		 * Fill in the lattice with dummy QHs. These QHs are used so the
708 		 * controller can tell that it is at the end of the QH list.
709 		 */
710 		for (i = 0; i < EHCI_NUM_PERIODIC_FRAME_LISTS; i++) {
711 			Set_PFLT(ehcip->ehci_periodic_frame_list_tablep->
712 			    ehci_periodic_frame_list_table[i],
713 			    ehci_qh_cpu_to_iommu(ehcip,
714 			    ehci_polledp->ehci_polled_dummy_qh) |
715 			    (EHCI_QH_LINK_REF_QH | EHCI_QH_LINK_PTR_VALID));
716 		}
717 
718 	}
719 
720 	/* Get the polled mode ehci pipe private structure */
721 	polled_pp = (ehci_pipe_private_t *)
722 	    ehci_polledp->ehci_polled_input_pipe_handle->p_hcd_private;
723 
724 	/*
725 	 * Before replacing the lattice, adjust the data togggle on the
726 	 * on the ehci's interrupt ed
727 	 */
728 	polled_toggle = (Get_QH(polled_pp->pp_qh->qh_status) &
729 	    EHCI_QH_STS_DATA_TOGGLE) ? DATA1:DATA0;
730 
731 	/*
732 	 * If normal mode interrupt pipe endpoint is active, get the data
733 	 * toggle from the this interrupt endpoint through the corresponding
734 	 * interrupt pipe handle. Else get the data toggle information from
735 	 * the usb device structure and this information is saved during the
736 	 * normal mode interrupt pipe close. Use this data toggle information
737 	 * to fix the data toggle of polled mode interrupt endpoint.
738 	 */
739 	if (ph) {
740 		/* Get the normal mode ehci pipe private structure */
741 		pp = (ehci_pipe_private_t *)ph->p_hcd_private;
742 
743 		real_toggle = (Get_QH(pp->pp_qh->qh_status) &
744 		    EHCI_QH_STS_DATA_TOGGLE) ? DATA1:DATA0;
745 	} else {
746 		real_toggle = usba_hcdi_get_data_toggle(
747 		    ehci_polledp->ehci_polled_usb_dev, ep_addr);
748 	}
749 
750 	if (polled_toggle != real_toggle) {
751 		if (real_toggle == DATA0) {
752 			Set_QH(polled_pp->pp_qh->qh_status,
753 			    Get_QH(polled_pp->pp_qh->qh_status) &
754 			    ~EHCI_QH_STS_DATA_TOGGLE);
755 		} else {
756 			Set_QH(polled_pp->pp_qh->qh_status,
757 			    Get_QH(polled_pp->pp_qh->qh_status) |
758 			    EHCI_QH_STS_DATA_TOGGLE);
759 		}
760 	}
761 
762 	/*
763 	 * Check whether Halt bit is set in the QH and if so  clear the
764 	 * halt bit.
765 	 */
766 	if (polled_pp->pp_qh->qh_status & EHCI_QH_STS_HALTED) {
767 
768 		/* Clear the halt bit */
769 		Set_QH(polled_pp->pp_qh->qh_status,
770 		    (Get_QH(polled_pp->pp_qh->qh_status) &
771 		    ~EHCI_QH_STS_HALTED));
772 	}
773 
774 	/*
775 	 * Initialize the qh overlay area
776 	 */
777 	qh = ehci_polledp->ehci_polled_qh;
778 	for (i = 0; i < 5; i++) {
779 		Set_QH(qh->qh_buf[i], NULL);
780 		Set_QH(qh->qh_buf_high[i], NULL);
781 	}
782 	Set_QH(qh->qh_next_qtd, ehci_qtd_cpu_to_iommu(ehcip,
783 	    ehci_polledp->ehci_polled_active_intr_qtd_list));
784 
785 	/*
786 	 * Now, add the endpoint to the lattice that we will  hang  our
787 	 * QTD's off of.  We need to poll this device at  every 8 ms and
788 	 * hence add this QH needs 4 entries in interrupt lattice.
789 	 */
790 	for (i = ehcip->ehci_polled_enter_count - 1;
791 	    i < EHCI_NUM_PERIODIC_FRAME_LISTS;
792 	    i = i + LS_MIN_POLL_INTERVAL) {
793 		Set_PFLT(ehcip->ehci_periodic_frame_list_tablep->
794 		    ehci_periodic_frame_list_table[i],
795 		    ehci_qh_cpu_to_iommu(ehcip,
796 		    ehci_polledp->ehci_polled_qh) | EHCI_QH_LINK_REF_QH);
797 	}
798 	/* The first enter keyboard entry enable interrupts and periodic list */
799 	if (ehcip->ehci_polled_enter_count == 1) {
800 		/* Enable USB and Frame list rollover interrupts */
801 		Set_OpReg(ehci_interrupt, (EHCI_INTR_USB |
802 		    EHCI_INTR_USB_ERROR | EHCI_INTR_FRAME_LIST_ROLLOVER));
803 
804 		/* Enable the periodic list */
805 		Set_OpReg(ehci_command,
806 		    (Get_OpReg(ehci_command) | EHCI_CMD_PERIODIC_SCHED_ENABLE));
807 
808 		/* Wait for few milliseconds */
809 		drv_usecwait(EHCI_POLLED_TIMEWAIT);
810 	}
811 #ifndef lint
812 	_NOTE(COMPETING_THREADS_NOW);
813 #endif
814 }
815 
816 
817 /*
818  * Polled restore state routines
819  */
820 
821 
822 /*
823  * ehci_polled_restore_state:
824  */
825 static void
826 ehci_polled_restore_state(ehci_polled_t	*ehci_polledp)
827 {
828 	ehci_state_t			*ehcip;
829 	int				i;
830 	uint_t				polled_toggle;
831 	uint_t				real_toggle;
832 	ehci_pipe_private_t		*pp = NULL; /* Normal mode Pipe */
833 	ehci_pipe_private_t		*polled_pp; /* Polled mode Pipe */
834 	usba_pipe_handle_data_t		*ph;
835 	uint8_t				ep_addr;
836 
837 #ifndef lint
838 	_NOTE(NO_COMPETING_THREADS_NOW);
839 #endif
840 
841 	/*
842 	 * If this flag is set, then we are still using this structure,
843 	 * so don't restore any controller state information yet.
844 	 */
845 	if (ehci_polledp->ehci_polled_flags & POLLED_INPUT_MODE_INUSE) {
846 
847 #ifndef lint
848 		_NOTE(COMPETING_THREADS_NOW);
849 #endif
850 
851 		return;
852 	}
853 
854 	ehcip = ehci_polledp->ehci_polled_ehcip;
855 	ehcip->ehci_polled_enter_count --;
856 
857 	/* Get the endpoint addr */
858 	ep_addr = ehci_polledp->ehci_polled_ep_addr;
859 
860 	/* Get the normal mode usb pipe handle */
861 	ph = usba_hcdi_get_ph_data(ehci_polledp->ehci_polled_usb_dev, ep_addr);
862 
863 	/* Disable list processing and other things */
864 	ehci_polled_stop_processing(ehci_polledp);
865 
866 	/* Get the polled mode ehci pipe private structure */
867 	polled_pp = (ehci_pipe_private_t *)
868 	    ehci_polledp->ehci_polled_input_pipe_handle->p_hcd_private;
869 
870 	/*
871 	 * Before replacing the lattice, adjust the data togggle
872 	 * on the on the ehci's interrupt ed
873 	 */
874 	polled_toggle = (Get_QH(polled_pp->pp_qh->qh_status) &
875 	    EHCI_QH_STS_DATA_TOGGLE) ? DATA1:DATA0;
876 
877 	/*
878 	 * If normal mode interrupt pipe endpoint is active, fix the
879 	 * data toggle for this interrupt endpoint by getting the data
880 	 * toggle information from the polled interrupt endpoint. Else
881 	 * save the data toggle information in usb device structure.
882 	 */
883 	if (ph) {
884 		/* Get the normal mode ehci pipe private structure */
885 		pp = (ehci_pipe_private_t *)ph->p_hcd_private;
886 
887 		real_toggle = (Get_QH(pp->pp_qh->qh_status) &
888 		    EHCI_QH_STS_DATA_TOGGLE) ? DATA1:DATA0;
889 
890 		if (polled_toggle != real_toggle) {
891 			if (polled_toggle == DATA0) {
892 				Set_QH(pp->pp_qh->qh_status,
893 				    Get_QH(pp->pp_qh->qh_status) &
894 				    ~EHCI_QH_STS_DATA_TOGGLE);
895 			} else {
896 				Set_QH(pp->pp_qh->qh_status,
897 				    Get_QH(pp->pp_qh->qh_status) |
898 				    EHCI_QH_STS_DATA_TOGGLE);
899 			}
900 		}
901 	} else {
902 		usba_hcdi_set_data_toggle(ehci_polledp->ehci_polled_usb_dev,
903 		    ep_addr, polled_toggle);
904 	}
905 
906 	/*
907 	 * Only the last leave keyboard entry restore the save frame
908 	 * list table and start processing.
909 	 */
910 	if (ehcip->ehci_polled_enter_count == 0) {
911 
912 		/* Replace the lattice */
913 		for (i = 0; i < EHCI_NUM_PERIODIC_FRAME_LISTS; i++) {
914 			Set_PFLT(ehcip->ehci_periodic_frame_list_tablep->
915 			    ehci_periodic_frame_list_table[i],
916 			    ehcip->ehci_polled_frame_list_table[i]);
917 		}
918 		ehci_polled_start_processing(ehci_polledp);
919 	}
920 
921 #ifndef lint
922 	_NOTE(COMPETING_THREADS_NOW);
923 #endif
924 }
925 
926 
927 /*
928  * ehci_polled_stop_processing:
929  */
930 static void
931 ehci_polled_stop_processing(ehci_polled_t	*ehci_polledp)
932 {
933 	ehci_state_t		*ehcip;
934 	ehci_qh_t		*qh = ehci_polledp->ehci_polled_qh;
935 
936 	ehcip = ehci_polledp->ehci_polled_ehcip;
937 
938 	/* First inactive this QH */
939 	Set_QH(qh->qh_ctrl,
940 	    Get_QH(qh->qh_ctrl) | EHCI_QH_CTRL_ED_INACTIVATE);
941 
942 	/* Only first leave keyboard entry turn off periodic list processing */
943 	if (Get_OpReg(ehci_command) & EHCI_CMD_PERIODIC_SCHED_ENABLE) {
944 		Set_OpReg(ehci_command, (Get_OpReg(ehci_command) &
945 			~EHCI_CMD_PERIODIC_SCHED_ENABLE));
946 
947 		/* Wait for few milliseconds */
948 		drv_usecwait(EHCI_POLLED_TIMEWAIT);
949 	}
950 	/*
951 	 * Now clear all required fields of QH
952 	 * including inactive bit.
953 	 */
954 	Set_QH(qh->qh_ctrl,
955 	    Get_QH(qh->qh_ctrl) & ~(EHCI_QH_CTRL_ED_INACTIVATE));
956 	Set_QH(qh->qh_status,
957 	    Get_QH(qh->qh_status) & ~(EHCI_QH_STS_XACT_STATUS));
958 	Set_QH(qh->qh_curr_qtd, NULL);
959 	Set_QH(qh->qh_alt_next_qtd, EHCI_QH_ALT_NEXT_QTD_PTR_VALID);
960 
961 	/*
962 	 * Now look up at the QTD's that are in the active qtd list &
963 	 * re-insert them back into the QH's QTD list.
964 	 */
965 	(void) ehci_polled_process_active_intr_qtd_list(ehci_polledp);
966 }
967 
968 
969 /*
970  * ehci_polled_start_processing:
971  */
972 static void
973 ehci_polled_start_processing(ehci_polled_t	*ehci_polledp)
974 {
975 	ehci_state_t		*ehcip;
976 	uint32_t		mask;
977 	ehci_regs_t		*ehci_polled_regsp;
978 
979 	ehcip = ehci_polledp->ehci_polled_ehcip;
980 	ehci_polled_regsp = &ehcip->ehci_polled_save_regs;
981 
982 	mask = ((uint32_t)ehci_polled_regsp->ehci_interrupt &
983 	    (EHCI_INTR_HOST_SYSTEM_ERROR | EHCI_INTR_FRAME_LIST_ROLLOVER |
984 	    EHCI_INTR_USB_ERROR | EHCI_INTR_USB | EHCI_INTR_ASYNC_ADVANCE));
985 
986 	/* Enable all required EHCI interrupts */
987 	Set_OpReg(ehci_interrupt, mask);
988 
989 	mask = ((uint32_t)ehci_polled_regsp->ehci_command &
990 	    (EHCI_CMD_ASYNC_SCHED_ENABLE | EHCI_CMD_PERIODIC_SCHED_ENABLE));
991 
992 	/* Enable all reuired list processing */
993 	Set_OpReg(ehci_command, (Get_OpReg(ehci_command) | mask));
994 
995 	/* Wait for few milliseconds */
996 	drv_usecwait(EHCI_POLLED_TIMEWAIT);
997 }
998 
999 
1000 /*
1001  * Polled read routines
1002  */
1003 
1004 
1005 /*
1006  * ehci_polled_process_active_intr_qtd_list:
1007  *
1008  * This routine takes the QTD's off of the input done head and processes
1009  * them.  It returns the number of characters that have been copied for
1010  * input.
1011  */
1012 static int
1013 ehci_polled_process_active_intr_qtd_list(ehci_polled_t	*ehci_polledp)
1014 {
1015 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
1016 	ehci_qtd_t		*qtd, *next_qtd;
1017 	uint_t			num_characters = 0;
1018 	uint_t			ctrl;
1019 	ehci_trans_wrapper_t	*tw;
1020 	ehci_pipe_private_t	*pp;
1021 	usb_cr_t 		error;
1022 
1023 	/* Sync QH and QTD pool */
1024 	if (ehci_polledp->ehci_polled_no_sync_flag == B_FALSE) {
1025 		Sync_QH_QTD_Pool(ehcip);
1026 	}
1027 
1028 	/* Create done qtd list */
1029 	qtd = ehci_polled_create_done_qtd_list(ehci_polledp);
1030 
1031 	/*
1032 	 * Traverse the list of transfer descriptors.  We can't destroy
1033 	 * the qtd_next pointers of these QTDs because we are using it
1034 	 * to traverse the done list.  Therefore, we can not put these
1035 	 * QTD's back on the QH until we are done processing all of them.
1036 	 */
1037 	while (qtd) {
1038 		/* Get next active QTD from the active QTD list */
1039 		next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
1040 		    Get_QTD(qtd->qtd_active_qtd_next));
1041 
1042 		/* Obtain the transfer wrapper from the QTD */
1043 		tw = (ehci_trans_wrapper_t *)EHCI_LOOKUP_ID(
1044 		    (uint32_t)Get_QTD(qtd->qtd_trans_wrapper));
1045 
1046 		/* Get ohci pipe from transfer wrapper */
1047 		pp = tw->tw_pipe_private;
1048 
1049 		/* Look at the status */
1050 		ctrl = (uint_t)Get_QTD(qtd->qtd_ctrl) &
1051 		    (uint32_t)EHCI_QTD_CTRL_XACT_STATUS;
1052 
1053 		error = ehci_check_for_error(ehcip, pp, tw, qtd, ctrl);
1054 
1055 		/*
1056 		 * Check to see if there is an error. If there is error
1057 		 * clear the halt condition in the Endpoint  Descriptor
1058 		 * (QH) associated with this Transfer  Descriptor (QTD).
1059 		 */
1060 		if (error == USB_CR_OK) {
1061 			num_characters +=
1062 			    ehci_polled_handle_normal_qtd(ehci_polledp, qtd);
1063 		} else {
1064 			/* Clear the halt bit */
1065 			Set_QH(pp->pp_qh->qh_status,
1066 			    Get_QH(pp->pp_qh->qh_status) &
1067 			    ~(EHCI_QH_STS_XACT_STATUS));
1068 		}
1069 
1070 		/* Insert this qtd back into QH's qtd list */
1071 		ehci_polled_insert_qtd(ehci_polledp, qtd);
1072 
1073 		qtd = next_qtd;
1074 	}
1075 
1076 	return (num_characters);
1077 }
1078 
1079 
1080 /*
1081  * ehci_polled_handle_normal_qtd:
1082  */
1083 static int
1084 ehci_polled_handle_normal_qtd(
1085 	ehci_polled_t		*ehci_polledp,
1086 	ehci_qtd_t		*qtd)
1087 {
1088 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
1089 	uchar_t			*buf;
1090 	ehci_trans_wrapper_t	*tw;
1091 	size_t			length;
1092 	uint32_t		residue;
1093 
1094 	/* Obtain the transfer wrapper from the QTD */
1095 	tw = (ehci_trans_wrapper_t *)EHCI_LOOKUP_ID((uint32_t)
1096 		Get_QTD(qtd->qtd_trans_wrapper));
1097 
1098 	ASSERT(tw != NULL);
1099 
1100 	buf = (uchar_t *)tw->tw_buf;
1101 
1102 	length = tw->tw_length;
1103 
1104 	/*
1105 	 * If "Total bytes of xfer" in control field of qtd is not equal to 0,
1106 	 * then we received less data from the usb device than requested by us.
1107 	 * In that case, get the actual received data size.
1108 	 */
1109 	residue = ((Get_QTD(qtd->qtd_ctrl) &
1110 	    EHCI_QTD_CTRL_BYTES_TO_XFER) >> EHCI_QTD_CTRL_BYTES_TO_XFER_SHIFT);
1111 
1112 	if (residue) {
1113 
1114 		length = (Get_QTD(qtd->qtd_xfer_addr) +
1115 		    Get_QTD(qtd->qtd_xfer_len) - residue) -
1116 		    tw->tw_cookie.dmac_address;
1117 	}
1118 
1119 	/* Sync IO buffer */
1120 	if (ehci_polledp->ehci_polled_no_sync_flag == B_FALSE) {
1121 		Sync_IO_Buffer(tw->tw_dmahandle, length);
1122 	}
1123 
1124 	/* Copy the data into the message */
1125 	bcopy(buf, ehci_polledp->ehci_polled_buf, length);
1126 
1127 	return (length);
1128 }
1129 
1130 
1131 /*
1132  * ehci_polled_insert_qtd:
1133  *
1134  * Insert a Transfer Descriptor (QTD) on an Endpoint Descriptor (QH).
1135  */
1136 static void
1137 ehci_polled_insert_qtd(
1138 	ehci_polled_t		*ehci_polledp,
1139 	ehci_qtd_t		*qtd)
1140 {
1141 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
1142 	ehci_qtd_t		*curr_dummy_qtd, *next_dummy_qtd;
1143 	ehci_qtd_t		*new_dummy_qtd;
1144 	uint_t			qtd_control;
1145 	ehci_pipe_private_t	*pp;
1146 	ehci_qh_t		*qh;
1147 	ehci_trans_wrapper_t	*tw;
1148 
1149 	/* Obtain the transfer wrapper from the QTD */
1150 	tw = (ehci_trans_wrapper_t *)EHCI_LOOKUP_ID(
1151 	    (uint32_t)Get_QTD(qtd->qtd_trans_wrapper));
1152 
1153 	pp = tw->tw_pipe_private;
1154 
1155 	/* Obtain the endpoint and interrupt request */
1156 	qh = pp->pp_qh;
1157 
1158 	/*
1159 	 * Take this QTD off the transfer wrapper's list since
1160 	 * the pipe is FIFO, this must be the first QTD on the
1161 	 * list.
1162 	 */
1163 	ASSERT((ehci_qtd_t *)tw->tw_qtd_head == qtd);
1164 
1165 	tw->tw_qtd_head = (ehci_qtd_t *)
1166 	    ehci_qtd_iommu_to_cpu(ehcip, Get_QTD(qtd->qtd_tw_next_qtd));
1167 
1168 	/*
1169 	 * If the head becomes NULL, then there are no more
1170 	 * active QTD's for this transfer wrapper. Also	set
1171 	 * the tail to NULL.
1172 	 */
1173 	if (tw->tw_qtd_head == NULL) {
1174 		tw->tw_qtd_tail = NULL;
1175 	}
1176 
1177 	/* Convert current valid QTD as new dummy QTD */
1178 	bzero((char *)qtd, sizeof (ehci_qtd_t));
1179 	Set_QTD(qtd->qtd_state, EHCI_QTD_DUMMY);
1180 
1181 	/* Rename qtd as new_dummy_qtd */
1182 	new_dummy_qtd = qtd;
1183 
1184 	/* Get the current and next dummy QTDs */
1185 	curr_dummy_qtd = ehci_qtd_iommu_to_cpu(ehcip,
1186 	    Get_QH(qh->qh_dummy_qtd));
1187 	next_dummy_qtd = ehci_qtd_iommu_to_cpu(ehcip,
1188 	    Get_QTD(curr_dummy_qtd->qtd_next_qtd));
1189 
1190 	/* Update QH's dummy qtd field */
1191 	Set_QH(qh->qh_dummy_qtd, ehci_qtd_cpu_to_iommu(ehcip, next_dummy_qtd));
1192 
1193 	/* Update next dummy's next qtd pointer */
1194 	Set_QTD(next_dummy_qtd->qtd_next_qtd,
1195 	    ehci_qtd_cpu_to_iommu(ehcip, new_dummy_qtd));
1196 
1197 	qtd_control = (tw->tw_direction | EHCI_QTD_CTRL_INTR_ON_COMPLETE);
1198 
1199 	/*
1200 	 * Fill in the current dummy qtd and
1201 	 * add the new dummy to the end.
1202 	 */
1203 	ehci_polled_fill_in_qtd(ehcip, curr_dummy_qtd, qtd_control,
1204 	    tw->tw_cookie.dmac_address, tw->tw_length, tw);
1205 
1206 	/* Insert this qtd onto the tw */
1207 	ehci_polled_insert_qtd_on_tw(ehcip, tw, curr_dummy_qtd);
1208 
1209 	/* Insert this qtd into active interrupt QTD list */
1210 	ehci_polled_insert_qtd_into_active_intr_qtd_list(
1211 	    ehci_polledp, curr_dummy_qtd);
1212 }
1213 
1214 
1215 /*
1216  * ehci_polled_fill_in_qtd:
1217  *
1218  * Fill in the fields of a Transfer Descriptor (QTD).
1219  *
1220  * Unlike the it's ehci_fill_in_qtd counterpart, we do not
1221  * set the alternative ptr in polled mode.  There is not need
1222  * for it in polled mode, because it doesn't need to cleanup
1223  * short xfer conditions.
1224  */
1225 static void
1226 ehci_polled_fill_in_qtd(
1227 	ehci_state_t		*ehcip,
1228 	ehci_qtd_t		*qtd,
1229 	uint_t			qtd_ctrl,
1230 	uint32_t		qtd_iommu_cbp,
1231 	size_t			qtd_length,
1232 	ehci_trans_wrapper_t	*tw)
1233 {
1234 	uint32_t		buf_addr = qtd_iommu_cbp;
1235 	size_t			buf_len = qtd_length;
1236 	uint32_t		ctrl = qtd_ctrl;
1237 	uint_t			i = 0;
1238 
1239 	/* Assert that the qtd to be filled in is a dummy */
1240 	ASSERT(Get_QTD(qtd->qtd_state) == EHCI_QTD_DUMMY);
1241 
1242 	/* Change QTD's state Active */
1243 	Set_QTD(qtd->qtd_state, EHCI_QTD_ACTIVE);
1244 
1245 	/* Set the total length data tarnsfer */
1246 	ctrl |= (((qtd_length << EHCI_QTD_CTRL_BYTES_TO_XFER_SHIFT)
1247 	    & EHCI_QTD_CTRL_BYTES_TO_XFER) | EHCI_QTD_CTRL_MAX_ERR_COUNTS);
1248 
1249 	/*
1250 	 * Save the starting buffer address used and
1251 	 * length of data that will be transfered in
1252 	 * the current QTD.
1253 	 */
1254 	Set_QTD(qtd->qtd_xfer_addr, buf_addr);
1255 	Set_QTD(qtd->qtd_xfer_len, buf_len);
1256 
1257 	while (buf_len) {
1258 		/* Update the beginning of the buffer */
1259 		Set_QTD(qtd->qtd_buf[i], buf_addr);
1260 
1261 		if (buf_len <= EHCI_MAX_QTD_BUF_SIZE) {
1262 			break;
1263 		} else {
1264 			buf_len -= EHCI_MAX_QTD_BUF_SIZE;
1265 			buf_addr += EHCI_MAX_QTD_BUF_SIZE;
1266 		}
1267 
1268 		i++;
1269 	}
1270 
1271 	/*
1272 	 * For control, bulk and interrupt QTD, now
1273 	 * enable current QTD by setting active bit.
1274 	 */
1275 	Set_QTD(qtd->qtd_ctrl, (ctrl | EHCI_QTD_CTRL_ACTIVE_XACT));
1276 
1277 	Set_QTD(qtd->qtd_trans_wrapper, (uint32_t)tw->tw_id);
1278 }
1279 
1280 
1281 /*
1282  * ehci_polled_insert_qtd_on_tw:
1283  *
1284  * The transfer wrapper keeps a list of all Transfer Descriptors (QTD) that
1285  * are allocated for this transfer. Insert a QTD  onto this list. The  list
1286  * of QTD's does not include the dummy QTD that is at the end of the list of
1287  * QTD's for the endpoint.
1288  */
1289 static void
1290 ehci_polled_insert_qtd_on_tw(
1291 	ehci_state_t		*ehcip,
1292 	ehci_trans_wrapper_t	*tw,
1293 	ehci_qtd_t		*qtd)
1294 {
1295 	/*
1296 	 * Set the next pointer to NULL because
1297 	 * this is the last QTD on list.
1298 	 */
1299 	Set_QTD(qtd->qtd_tw_next_qtd, NULL);
1300 
1301 	if (tw->tw_qtd_head == NULL) {
1302 		ASSERT(tw->tw_qtd_tail == NULL);
1303 		tw->tw_qtd_head = qtd;
1304 		tw->tw_qtd_tail = qtd;
1305 	} else {
1306 		ehci_qtd_t *dummy = (ehci_qtd_t *)tw->tw_qtd_tail;
1307 
1308 		ASSERT(dummy != NULL);
1309 		ASSERT(dummy != qtd);
1310 		ASSERT(Get_QTD(qtd->qtd_state) != EHCI_QTD_DUMMY);
1311 
1312 		/* Add the qtd to the end of the list */
1313 		Set_QTD(dummy->qtd_tw_next_qtd,
1314 		    ehci_qtd_cpu_to_iommu(ehcip, qtd));
1315 
1316 		tw->tw_qtd_tail = qtd;
1317 
1318 		ASSERT(Get_QTD(qtd->qtd_tw_next_qtd) == NULL);
1319 	}
1320 }
1321 
1322 
1323 /*
1324  * ehci_polled_create_done_qtd_list:
1325  *
1326  * Create done qtd list from active qtd list.
1327  */
1328 static ehci_qtd_t *
1329 ehci_polled_create_done_qtd_list(
1330 	ehci_polled_t		*ehci_polledp)
1331 {
1332 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
1333 	ehci_qtd_t		*curr_qtd = NULL, *next_qtd = NULL;
1334 	ehci_qtd_t		*done_qtd_list = NULL, *last_done_qtd = NULL;
1335 
1336 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
1337 	    "ehci_polled_create_done_qtd_list:");
1338 
1339 	curr_qtd = ehci_polledp->ehci_polled_active_intr_qtd_list;
1340 
1341 	while (curr_qtd) {
1342 
1343 		/* Get next qtd from the active qtd list */
1344 		next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
1345 		    Get_QTD(curr_qtd->qtd_active_qtd_next));
1346 
1347 		/* Check this QTD has been processed by Host Controller */
1348 		if (!(Get_QTD(curr_qtd->qtd_ctrl) &
1349 		    EHCI_QTD_CTRL_ACTIVE_XACT)) {
1350 
1351 			/* Remove this QTD from active QTD list */
1352 			ehci_polled_remove_qtd_from_active_intr_qtd_list(
1353 			    ehci_polledp, curr_qtd);
1354 
1355 			Set_QTD(curr_qtd->qtd_active_qtd_next, NULL);
1356 
1357 			if (done_qtd_list) {
1358 				Set_QTD(last_done_qtd->qtd_active_qtd_next,
1359 				    ehci_qtd_cpu_to_iommu(ehcip, curr_qtd));
1360 
1361 				last_done_qtd = curr_qtd;
1362 			} else {
1363 				done_qtd_list = curr_qtd;
1364 				last_done_qtd = curr_qtd;
1365 			}
1366 		}
1367 
1368 		curr_qtd = next_qtd;
1369 	}
1370 
1371 	return (done_qtd_list);
1372 }
1373 
1374 
1375 /*
1376  * ehci_polled_insert_qtd_into_active_intr_qtd_list:
1377  *
1378  * Insert current QTD into active interrupt QTD list.
1379  */
1380 static void
1381 ehci_polled_insert_qtd_into_active_intr_qtd_list(
1382 	ehci_polled_t		*ehci_polledp,
1383 	ehci_qtd_t		*qtd)
1384 {
1385 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
1386 	ehci_qtd_t		*curr_qtd, *next_qtd;
1387 
1388 	curr_qtd = ehci_polledp->ehci_polled_active_intr_qtd_list;
1389 
1390 	/* Insert this qtd into active intr qtd list */
1391 	if (curr_qtd) {
1392 		next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
1393 		    Get_QTD(curr_qtd->qtd_active_qtd_next));
1394 
1395 		while (next_qtd) {
1396 			curr_qtd = next_qtd;
1397 			next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
1398 			    Get_QTD(curr_qtd->qtd_active_qtd_next));
1399 		}
1400 
1401 		Set_QTD(qtd->qtd_active_qtd_prev,
1402 		    ehci_qtd_cpu_to_iommu(ehcip, curr_qtd));
1403 
1404 		Set_QTD(curr_qtd->qtd_active_qtd_next,
1405 		    ehci_qtd_cpu_to_iommu(ehcip, qtd));
1406 	} else {
1407 		ehci_polledp->ehci_polled_active_intr_qtd_list = qtd;
1408 		Set_QTD(qtd->qtd_active_qtd_next, NULL);
1409 		Set_QTD(qtd->qtd_active_qtd_prev, NULL);
1410 	}
1411 }
1412 
1413 
1414 /*
1415  * ehci_polled_remove_qtd_from_active_intr_qtd_list:
1416  *
1417  * Remove current QTD from the active QTD list.
1418  */
1419 void
1420 ehci_polled_remove_qtd_from_active_intr_qtd_list(
1421 	ehci_polled_t		*ehci_polledp,
1422 	ehci_qtd_t		*qtd)
1423 {
1424 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
1425 	ehci_qtd_t		*curr_qtd, *prev_qtd, *next_qtd;
1426 
1427 	ASSERT(qtd != NULL);
1428 
1429 	curr_qtd = ehci_polledp->ehci_polled_active_intr_qtd_list;
1430 
1431 	while ((curr_qtd) && (curr_qtd != qtd)) {
1432 		curr_qtd = ehci_qtd_iommu_to_cpu(ehcip,
1433 		    Get_QTD(curr_qtd->qtd_active_qtd_next));
1434 	}
1435 
1436 	if ((curr_qtd) && (curr_qtd == qtd)) {
1437 		prev_qtd = ehci_qtd_iommu_to_cpu(ehcip,
1438 		    Get_QTD(curr_qtd->qtd_active_qtd_prev));
1439 		next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
1440 		    Get_QTD(curr_qtd->qtd_active_qtd_next));
1441 
1442 		if (prev_qtd) {
1443 			Set_QTD(prev_qtd->qtd_active_qtd_next,
1444 			    Get_QTD(curr_qtd->qtd_active_qtd_next));
1445 		} else {
1446 			ehci_polledp->
1447 			    ehci_polled_active_intr_qtd_list = next_qtd;
1448 		}
1449 
1450 		if (next_qtd) {
1451 			Set_QTD(next_qtd->qtd_active_qtd_prev,
1452 			    Get_QTD(curr_qtd->qtd_active_qtd_prev));
1453 		}
1454 	}
1455 }
1456 
1457 
1458 /*
1459  * ehci_polled_traverse_qtds:
1460  *
1461  * Traverse the list of QTDs for given pipe using transfer wrapper.  Since
1462  * the endpoint is marked as Halted, the Host Controller (HC) is no longer
1463  * accessing these QTDs. Remove all the QTDs that are attached to endpoint.
1464  */
1465 static void
1466 ehci_polled_traverse_qtds(
1467 	ehci_polled_t		*ehci_polledp,
1468 	usba_pipe_handle_data_t	*ph)
1469 {
1470 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
1471 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
1472 	ehci_trans_wrapper_t	*next_tw;
1473 	ehci_qtd_t		*qtd;
1474 	ehci_qtd_t		*next_qtd;
1475 
1476 	/* Process the transfer wrappers for this pipe */
1477 	next_tw = pp->pp_tw_head;
1478 
1479 	while (next_tw) {
1480 		qtd = (ehci_qtd_t *)next_tw->tw_qtd_head;
1481 
1482 		/* Walk through each QTD for this transfer wrapper */
1483 		while (qtd) {
1484 			/* Remove this QTD from active QTD list */
1485 			ehci_polled_remove_qtd_from_active_intr_qtd_list(
1486 			    ehci_polledp, qtd);
1487 
1488 			next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
1489 			    Get_QTD(qtd->qtd_tw_next_qtd));
1490 
1491 			/* Deallocate this QTD */
1492 			ehci_deallocate_qtd(ehcip, qtd);
1493 
1494 			qtd = next_qtd;
1495 		}
1496 
1497 		next_tw = next_tw->tw_next;
1498 	}
1499 
1500 	/* Clear current qtd pointer */
1501 	Set_QH(pp->pp_qh->qh_curr_qtd, (uint32_t)0x00000000);
1502 
1503 	/* Update the next qtd pointer in the QH */
1504 	Set_QH(pp->pp_qh->qh_next_qtd, Get_QH(pp->pp_qh->qh_dummy_qtd));
1505 }
1506 
1507 
1508 /*
1509  * ehci_polled_finish_interrupt:
1510  */
1511 static void
1512 ehci_polled_finish_interrupt(
1513 	ehci_state_t	*ehcip,
1514 	uint_t		intr)
1515 {
1516 	/* Acknowledge the interrupt */
1517 	Set_OpReg(ehci_status, intr);
1518 
1519 	/*
1520 	 * Read interrupt status register to make sure that any PIO
1521 	 * store to clear the ISR has made it on the PCI bus before
1522 	 * returning from its interrupt handler.
1523 	 */
1524 	(void) Get_OpReg(ehci_status);
1525 }
1526