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