17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3304303fSsl  * Common Development and Distribution License (the "License").
6*3304303fSsl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*3304303fSsl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * EHCI Host Controller Driver (EHCI)
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * The EHCI driver is a software driver which interfaces to the Universal
317c478bd9Sstevel@tonic-gate  * Serial Bus layer (USBA) and the Host Controller (HC). The interface to
327c478bd9Sstevel@tonic-gate  * the Host Controller is defined by the EHCI Host Controller Interface.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * This module contains the specific EHCI code used in POLLED mode. This
357c478bd9Sstevel@tonic-gate  * code is in a separate file since it will never become part of the EHCI
367c478bd9Sstevel@tonic-gate  * driver.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usbai_version.h>
407c478bd9Sstevel@tonic-gate #include <sys/usb/hcd/ehci/ehcid.h>
417c478bd9Sstevel@tonic-gate #include <sys/usb/hcd/ehci/ehci_xfer.h>
427c478bd9Sstevel@tonic-gate #include <sys/usb/hcd/ehci/ehci_intr.h>
437c478bd9Sstevel@tonic-gate #include <sys/usb/hcd/ehci/ehci_util.h>
447c478bd9Sstevel@tonic-gate #include <sys/usb/hcd/ehci/ehci_polled.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * Internal Function Prototypes
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* Polled initialization routines */
517c478bd9Sstevel@tonic-gate static int	ehci_polled_init(
527c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph,
537c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
547c478bd9Sstevel@tonic-gate 				usb_console_info_impl_t	*console_input_info);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* Polled deinitialization routines */
577c478bd9Sstevel@tonic-gate static int	ehci_polled_fini(ehci_polled_t		*ehci_polledp);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* Polled save state routines */
607c478bd9Sstevel@tonic-gate static void	ehci_polled_save_state(ehci_polled_t	*ehci_polledp);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /* Polled restore state routines */
637c478bd9Sstevel@tonic-gate static void	ehci_polled_restore_state(ehci_polled_t	*ehci_polledp);
647c478bd9Sstevel@tonic-gate static void	ehci_polled_stop_processing(
657c478bd9Sstevel@tonic-gate 				ehci_polled_t		*ehci_polledp);
667c478bd9Sstevel@tonic-gate static void	ehci_polled_start_processing(
677c478bd9Sstevel@tonic-gate 				ehci_polled_t		*ehci_polledp);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /* Polled read routines */
707c478bd9Sstevel@tonic-gate static int	ehci_polled_process_active_intr_qtd_list(
717c478bd9Sstevel@tonic-gate 				ehci_polled_t		*ehci_polledp);
727c478bd9Sstevel@tonic-gate static int	ehci_polled_handle_normal_qtd(
737c478bd9Sstevel@tonic-gate 				ehci_polled_t		*ehci_polledp,
747c478bd9Sstevel@tonic-gate 				ehci_qtd_t		*qtd);
757c478bd9Sstevel@tonic-gate static void	ehci_polled_insert_qtd(
767c478bd9Sstevel@tonic-gate 				ehci_polled_t		*ehci_polledp,
777c478bd9Sstevel@tonic-gate 				ehci_qtd_t		*qtd);
787c478bd9Sstevel@tonic-gate static void	ehci_polled_fill_in_qtd(
797c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
807c478bd9Sstevel@tonic-gate 				ehci_qtd_t		*qtd,
817c478bd9Sstevel@tonic-gate 				uint_t			qtd_ctrl,
82*3304303fSsl 				size_t			qtd_dma_offs,
837c478bd9Sstevel@tonic-gate 				size_t			qtd_length,
847c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw);
857c478bd9Sstevel@tonic-gate static void	ehci_polled_insert_qtd_on_tw(
867c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
877c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw,
887c478bd9Sstevel@tonic-gate 				ehci_qtd_t		*qtd);
897c478bd9Sstevel@tonic-gate static ehci_qtd_t *ehci_polled_create_done_qtd_list(
907c478bd9Sstevel@tonic-gate 				ehci_polled_t		*ehci_polledp);
917c478bd9Sstevel@tonic-gate static void	ehci_polled_insert_qtd_into_active_intr_qtd_list(
927c478bd9Sstevel@tonic-gate 				ehci_polled_t		*ehci_polledp,
937c478bd9Sstevel@tonic-gate 				ehci_qtd_t		*curr_qtd);
947c478bd9Sstevel@tonic-gate static void	ehci_polled_remove_qtd_from_active_intr_qtd_list(
957c478bd9Sstevel@tonic-gate 				ehci_polled_t		*ehci_polledp,
967c478bd9Sstevel@tonic-gate 				ehci_qtd_t		*curr_qtd);
977c478bd9Sstevel@tonic-gate static void	ehci_polled_traverse_qtds(
987c478bd9Sstevel@tonic-gate 				ehci_polled_t		*ehci_polledp,
997c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph);
1007c478bd9Sstevel@tonic-gate static void	ehci_polled_finish_interrupt(
1017c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1027c478bd9Sstevel@tonic-gate 				uint_t			intr);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * POLLED entry points
1067c478bd9Sstevel@tonic-gate  *
1077c478bd9Sstevel@tonic-gate  * These functions are entry points into the POLLED code.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * ehci_hcdi_polled_input_init:
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  * This is the initialization routine for handling the USB keyboard
1147c478bd9Sstevel@tonic-gate  * in POLLED mode.  This routine is not called from POLLED mode, so
1157c478bd9Sstevel@tonic-gate  * it is OK to acquire mutexes.
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate int
1187c478bd9Sstevel@tonic-gate ehci_hcdi_polled_input_init(
1197c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
1207c478bd9Sstevel@tonic-gate 	uchar_t			**polled_buf,
1217c478bd9Sstevel@tonic-gate 	usb_console_info_impl_t	*console_input_info)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	ehci_polled_t		*ehci_polledp;
1247c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip;
1257c478bd9Sstevel@tonic-gate 	int			ret;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	ehcip = ehci_obtain_state(ph->p_usba_device->usb_root_hub_dip);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * Grab the ehci_int_mutex so that things don't change on us
1317c478bd9Sstevel@tonic-gate 	 * if an interrupt comes in.
1327c478bd9Sstevel@tonic-gate 	 */
1337c478bd9Sstevel@tonic-gate 	mutex_enter(&ehcip->ehci_int_mutex);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	ret = ehci_polled_init(ph, ehcip, console_input_info);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if (ret != USB_SUCCESS) {
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		/* Allow interrupts to continue */
1407c478bd9Sstevel@tonic-gate 		mutex_exit(&ehcip->ehci_int_mutex);
1417c478bd9Sstevel@tonic-gate 		return (ret);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	ehci_polledp = (ehci_polled_t *)console_input_info->uci_private;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * Mark the structure so that if we are using it, we don't free
1487c478bd9Sstevel@tonic-gate 	 * the structures if one of them is unplugged.
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 	ehci_polledp->ehci_polled_flags |= POLLED_INPUT_MODE;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/* increase the counter for keyboard connected */
1537c478bd9Sstevel@tonic-gate 	ehcip->ehci_polled_kbd_count ++;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/*
1567c478bd9Sstevel@tonic-gate 	 * This is the buffer we will copy characters into. It will be
1577c478bd9Sstevel@tonic-gate 	 * copied into at this layer, so we need to keep track of it.
1587c478bd9Sstevel@tonic-gate 	 */
1597c478bd9Sstevel@tonic-gate 	ehci_polledp->ehci_polled_buf =
1607c478bd9Sstevel@tonic-gate 	    (uchar_t *)kmem_zalloc(POLLED_RAW_BUF_SIZE, KM_SLEEP);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	*polled_buf = ehci_polledp->ehci_polled_buf;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/*
1657c478bd9Sstevel@tonic-gate 	 * This is a software workaround to fix schizo hardware bug.
1667c478bd9Sstevel@tonic-gate 	 * Existence of "no-prom-cdma-sync"  property means consistent
1677c478bd9Sstevel@tonic-gate 	 * dma sync should not be done while in prom or polled mode.
1687c478bd9Sstevel@tonic-gate 	 */
1697c478bd9Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, ehcip->ehci_dip,
1707c478bd9Sstevel@tonic-gate 	    DDI_PROP_NOTPROM, "no-prom-cdma-sync")) {
1717c478bd9Sstevel@tonic-gate 		ehci_polledp->ehci_polled_no_sync_flag = B_TRUE;
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	/* Allow interrupts to continue */
1757c478bd9Sstevel@tonic-gate 	mutex_exit(&ehcip->ehci_int_mutex);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * ehci_hcdi_polled_input_fini:
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate int
1857c478bd9Sstevel@tonic-gate ehci_hcdi_polled_input_fini(usb_console_info_impl_t *info)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	ehci_polled_t		*ehci_polledp;
1887c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip;
1897c478bd9Sstevel@tonic-gate 	int			ret;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	ehci_polledp = (ehci_polled_t *)info->uci_private;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	ehcip = ehci_polledp->ehci_polled_ehcip;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	mutex_enter(&ehcip->ehci_int_mutex);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/*
1987c478bd9Sstevel@tonic-gate 	 * Reset the POLLED_INPUT_MODE flag so that we can tell if
1997c478bd9Sstevel@tonic-gate 	 * this structure is in use in the ehci_polled_fini routine.
2007c478bd9Sstevel@tonic-gate 	 */
2017c478bd9Sstevel@tonic-gate 	ehci_polledp->ehci_polled_flags &= ~POLLED_INPUT_MODE;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/* decrease the counter for keyboard disconnected */
2047c478bd9Sstevel@tonic-gate 	ehcip->ehci_polled_kbd_count --;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/* Free the buffer that we copied data into */
2077c478bd9Sstevel@tonic-gate 	kmem_free(ehci_polledp->ehci_polled_buf, POLLED_RAW_BUF_SIZE);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	ret = ehci_polled_fini(ehci_polledp);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	mutex_exit(&ehcip->ehci_int_mutex);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	return (ret);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate  * ehci_hcdi_polled_input_enter:
2197c478bd9Sstevel@tonic-gate  *
2207c478bd9Sstevel@tonic-gate  * This is where we enter into POLLED mode.  This routine sets up
2217c478bd9Sstevel@tonic-gate  * everything so that calls to	ehci_hcdi_polled_read will return
2227c478bd9Sstevel@tonic-gate  * characters.
2237c478bd9Sstevel@tonic-gate  */
2247c478bd9Sstevel@tonic-gate int
2257c478bd9Sstevel@tonic-gate ehci_hcdi_polled_input_enter(usb_console_info_impl_t *info)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	ehci_polled_t		*ehci_polledp;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	ehci_polledp = (ehci_polled_t *)info->uci_private;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	ehci_polledp->ehci_polled_entry++;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/*
2347c478bd9Sstevel@tonic-gate 	 * If the controller is already switched over, just return
2357c478bd9Sstevel@tonic-gate 	 */
2367c478bd9Sstevel@tonic-gate 	if (ehci_polledp->ehci_polled_entry > 1) {
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	ehci_polled_save_state(ehci_polledp);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	ehci_polledp->ehci_polled_flags |= POLLED_INPUT_MODE_INUSE;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * ehci_hcdi_polled_input_exit:
2517c478bd9Sstevel@tonic-gate  *
2527c478bd9Sstevel@tonic-gate  * This is where we exit POLLED mode. This routine restores
2537c478bd9Sstevel@tonic-gate  * everything that is needed to continue operation.
2547c478bd9Sstevel@tonic-gate  */
2557c478bd9Sstevel@tonic-gate int
2567c478bd9Sstevel@tonic-gate ehci_hcdi_polled_input_exit(usb_console_info_impl_t *info)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	ehci_polled_t		*ehci_polledp;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	ehci_polledp = (ehci_polled_t *)info->uci_private;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	ehci_polledp->ehci_polled_entry--;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/*
2657c478bd9Sstevel@tonic-gate 	 * If there are still outstanding "enters", just return
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 	if (ehci_polledp->ehci_polled_entry > 0) {
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	ehci_polledp->ehci_polled_flags &= ~POLLED_INPUT_MODE_INUSE;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	ehci_polled_restore_state(ehci_polledp);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * ehci_hcdi_polled_read:
2827c478bd9Sstevel@tonic-gate  *
2837c478bd9Sstevel@tonic-gate  * Get a key character
2847c478bd9Sstevel@tonic-gate  */
2857c478bd9Sstevel@tonic-gate int
2867c478bd9Sstevel@tonic-gate ehci_hcdi_polled_read(
2877c478bd9Sstevel@tonic-gate 	usb_console_info_impl_t	*info,
2887c478bd9Sstevel@tonic-gate 	uint_t			*num_characters)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip;
2917c478bd9Sstevel@tonic-gate 	ehci_polled_t		*ehci_polledp;
2927c478bd9Sstevel@tonic-gate 	uint_t			intr;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	ehci_polledp = (ehci_polled_t *)info->uci_private;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	ehcip = ehci_polledp->ehci_polled_ehcip;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate #ifndef lint
2997c478bd9Sstevel@tonic-gate 	_NOTE(NO_COMPETING_THREADS_NOW);
3007c478bd9Sstevel@tonic-gate #endif
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	*num_characters = 0;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	intr = ((Get_OpReg(ehci_status) & Get_OpReg(ehci_interrupt)) &
3057c478bd9Sstevel@tonic-gate 	    (EHCI_INTR_FRAME_LIST_ROLLOVER |
3067c478bd9Sstevel@tonic-gate 	    EHCI_INTR_USB | EHCI_INTR_USB_ERROR));
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	/*
3097c478bd9Sstevel@tonic-gate 	 * Check whether any frame list rollover interrupt is pending
3107c478bd9Sstevel@tonic-gate 	 * and if it is pending, process this interrupt.
3117c478bd9Sstevel@tonic-gate 	 */
3127c478bd9Sstevel@tonic-gate 	if (intr & EHCI_INTR_FRAME_LIST_ROLLOVER) {
3137c478bd9Sstevel@tonic-gate 		/* Check any frame list rollover interrupt is pending */
3147c478bd9Sstevel@tonic-gate 		ehci_handle_frame_list_rollover(ehcip);
3157c478bd9Sstevel@tonic-gate 		ehci_polled_finish_interrupt(ehcip,
3167c478bd9Sstevel@tonic-gate 		    EHCI_INTR_FRAME_LIST_ROLLOVER);
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/* Check for any USB transaction completion notification */
3207c478bd9Sstevel@tonic-gate 	if (intr & (EHCI_INTR_USB | EHCI_INTR_USB_ERROR)) {
3217c478bd9Sstevel@tonic-gate 		ehcip->ehci_polled_read_count ++;
3227c478bd9Sstevel@tonic-gate 		/* Process any QTD's on the active interrupt qtd list */
3237c478bd9Sstevel@tonic-gate 		*num_characters =
3247c478bd9Sstevel@tonic-gate 		    ehci_polled_process_active_intr_qtd_list(ehci_polledp);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		if (ehcip->ehci_polled_read_count ==
3277c478bd9Sstevel@tonic-gate 		    ehcip->ehci_polled_enter_count) {
3287c478bd9Sstevel@tonic-gate 			/* Acknowledge the frame list rollover interrupt */
3297c478bd9Sstevel@tonic-gate 			ehci_polled_finish_interrupt(ehcip,
3307c478bd9Sstevel@tonic-gate 			    intr & (EHCI_INTR_USB | EHCI_INTR_USB_ERROR));
3317c478bd9Sstevel@tonic-gate 			ehcip->ehci_polled_read_count = 0;
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate #ifndef lint
3367c478bd9Sstevel@tonic-gate 	_NOTE(COMPETING_THREADS_NOW);
3377c478bd9Sstevel@tonic-gate #endif
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate /*
3447c478bd9Sstevel@tonic-gate  * Internal Functions
3457c478bd9Sstevel@tonic-gate  */
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate  * Polled initialization routines
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate  * ehci_polled_init:
3547c478bd9Sstevel@tonic-gate  *
3557c478bd9Sstevel@tonic-gate  * Initialize generic information Uthat is needed to provide USB/POLLED
3567c478bd9Sstevel@tonic-gate  * support.
3577c478bd9Sstevel@tonic-gate  */
3587c478bd9Sstevel@tonic-gate static int
3597c478bd9Sstevel@tonic-gate ehci_polled_init(
3607c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
3617c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
3627c478bd9Sstevel@tonic-gate 	usb_console_info_impl_t	*console_info)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	ehci_polled_t		*ehci_polledp;
3657c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp;
3667c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	/*
3717c478bd9Sstevel@tonic-gate 	 * We have already initialized this structure. If the structure
3727c478bd9Sstevel@tonic-gate 	 * has already been initialized, then we don't need to redo it.
3737c478bd9Sstevel@tonic-gate 	 */
3747c478bd9Sstevel@tonic-gate 	if (console_info->uci_private) {
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/* Allocate and intitialize a state structure */
3807c478bd9Sstevel@tonic-gate 	ehci_polledp = (ehci_polled_t *)
3817c478bd9Sstevel@tonic-gate 	    kmem_zalloc(sizeof (ehci_polled_t), KM_SLEEP);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	console_info->uci_private = (usb_console_info_private_t)ehci_polledp;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/*
3867c478bd9Sstevel@tonic-gate 	 * Store away the ehcip so that we can get to it when we are in
3877c478bd9Sstevel@tonic-gate 	 * POLLED mode. We don't want to have to call ehci_obtain_state
3887c478bd9Sstevel@tonic-gate 	 * every time we want to access this structure.
3897c478bd9Sstevel@tonic-gate 	 */
3907c478bd9Sstevel@tonic-gate 	ehci_polledp->ehci_polled_ehcip = ehcip;
3917c478bd9Sstevel@tonic-gate 	/*
3927c478bd9Sstevel@tonic-gate 	 * Save usb device and endpoint number information from the usb
3937c478bd9Sstevel@tonic-gate 	 * pipe handle.
3947c478bd9Sstevel@tonic-gate 	 */
3957c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_mutex);
3967c478bd9Sstevel@tonic-gate 	ehci_polledp->ehci_polled_usb_dev = ph->p_usba_device;
3977c478bd9Sstevel@tonic-gate 	ehci_polledp->ehci_polled_ep_addr = ph->p_ep.bEndpointAddress;
3987c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_mutex);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	/*
4017c478bd9Sstevel@tonic-gate 	 * Allocate memory to make duplicate of original usb pipe handle.
4027c478bd9Sstevel@tonic-gate 	 */
4037c478bd9Sstevel@tonic-gate 	ehci_polledp->ehci_polled_input_pipe_handle =
4047c478bd9Sstevel@tonic-gate 	    kmem_zalloc(sizeof (usba_pipe_handle_data_t), KM_SLEEP);
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	/*
4077c478bd9Sstevel@tonic-gate 	 * Copy the USB handle into the new pipe handle. Also
4087c478bd9Sstevel@tonic-gate 	 * create new lock for the new pipe handle.
4097c478bd9Sstevel@tonic-gate 	 */
4107c478bd9Sstevel@tonic-gate 	bcopy((void *)ph,
4117c478bd9Sstevel@tonic-gate 	    (void *)ehci_polledp->ehci_polled_input_pipe_handle,
4127c478bd9Sstevel@tonic-gate 	    sizeof (usba_pipe_handle_data_t));
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/*
4157c478bd9Sstevel@tonic-gate 	 * uint64_t typecast to make sure amd64 can compile
4167c478bd9Sstevel@tonic-gate 	 */
4177c478bd9Sstevel@tonic-gate 	mutex_init(&ehci_polledp->ehci_polled_input_pipe_handle->p_mutex,
418a195726fSgovinda 	    NULL, MUTEX_DRIVER, DDI_INTR_PRI(ehcip->ehci_intr_pri));
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	/*
4217c478bd9Sstevel@tonic-gate 	 * Create a new ehci pipe private structure
4227c478bd9Sstevel@tonic-gate 	 */
4237c478bd9Sstevel@tonic-gate 	pp = (ehci_pipe_private_t *)
4247c478bd9Sstevel@tonic-gate 	    kmem_zalloc(sizeof (ehci_pipe_private_t), KM_SLEEP);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * Store the pointer in the pipe handle. This structure was also
4287c478bd9Sstevel@tonic-gate 	 * just allocated.
4297c478bd9Sstevel@tonic-gate 	 */
4307c478bd9Sstevel@tonic-gate 	mutex_enter(&ehci_polledp->ehci_polled_input_pipe_handle->p_mutex);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	ehci_polledp->ehci_polled_input_pipe_handle->
4337c478bd9Sstevel@tonic-gate 	    p_hcd_private = (usb_opaque_t)pp;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	mutex_exit(&ehci_polledp->ehci_polled_input_pipe_handle->p_mutex);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/*
4387c478bd9Sstevel@tonic-gate 	 * Store a pointer to the pipe handle. This structure was  just
4397c478bd9Sstevel@tonic-gate 	 * allocated and it is not in use yet.	The locking is there to
4407c478bd9Sstevel@tonic-gate 	 * satisfy warlock.
4417c478bd9Sstevel@tonic-gate 	 */
4427c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_mutex);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	bcopy(&ph->p_policy, &pp->pp_policy, sizeof (usb_pipe_policy_t));
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_mutex);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	pp->pp_pipe_handle = ehci_polledp->ehci_polled_input_pipe_handle;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/*
4517c478bd9Sstevel@tonic-gate 	 * Allocate a dummy for the interrupt table. This dummy will be
4527c478bd9Sstevel@tonic-gate 	 * put into the action when we	switch interrupt  tables during
4537c478bd9Sstevel@tonic-gate 	 * ehci_hcdi_polled_enter. Dummy is placed on the unused lattice
4547c478bd9Sstevel@tonic-gate 	 * entries. When the QH is allocated we will replace dummy QH by
4557c478bd9Sstevel@tonic-gate 	 * valid interrupt QH in one or more locations in the interrupt
4567c478bd9Sstevel@tonic-gate 	 * lattice depending on the requested polling interval. Also we
4577c478bd9Sstevel@tonic-gate 	 * will hang a dummy QTD to the QH & dummy QTD is used to indicate
4587c478bd9Sstevel@tonic-gate 	 * the end of the QTD chain.
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	ehci_polledp->ehci_polled_dummy_qh =
4617c478bd9Sstevel@tonic-gate 	    ehci_alloc_qh(ehcip, NULL, EHCI_POLLED_MODE_FLAG);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	if (ehci_polledp->ehci_polled_dummy_qh == NULL) {
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 		return (USB_NO_RESOURCES);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	/*
4697c478bd9Sstevel@tonic-gate 	 * Allocate the interrupt endpoint. This QH will be inserted in
4707c478bd9Sstevel@tonic-gate 	 * to the lattice chain for the  keyboard device. This endpoint
4717c478bd9Sstevel@tonic-gate 	 * will have the QTDs hanging off of it for the processing.
4727c478bd9Sstevel@tonic-gate 	 */
4737c478bd9Sstevel@tonic-gate 	ehci_polledp->ehci_polled_qh = ehci_alloc_qh(
4747c478bd9Sstevel@tonic-gate 	    ehcip, ph, EHCI_POLLED_MODE_FLAG);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	if (ehci_polledp->ehci_polled_qh == NULL) {
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 		return (USB_NO_RESOURCES);
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	/* Set the state of pipe as idle */
4827c478bd9Sstevel@tonic-gate 	pp->pp_state = EHCI_PIPE_STATE_IDLE;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/* Set polled mode flag */
4857c478bd9Sstevel@tonic-gate 	pp->pp_flag = EHCI_POLLED_MODE_FLAG;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/* Insert the endpoint onto the pipe handle */
4887c478bd9Sstevel@tonic-gate 	pp->pp_qh = ehci_polledp->ehci_polled_qh;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	/*
4917c478bd9Sstevel@tonic-gate 	 * Set soft interrupt handler flag in the normal mode usb
4927c478bd9Sstevel@tonic-gate 	 * pipe handle.
4937c478bd9Sstevel@tonic-gate 	 */
4947c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_mutex);
4957c478bd9Sstevel@tonic-gate 	ph->p_spec_flag |= USBA_PH_FLAG_USE_SOFT_INTR;
4967c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_mutex);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	/*
4997c478bd9Sstevel@tonic-gate 	 * Insert a Interrupt polling request onto the endpoint.
5007c478bd9Sstevel@tonic-gate 	 *
5017c478bd9Sstevel@tonic-gate 	 * There will now be two QTDs on the QH, one is the dummy QTD that
5027c478bd9Sstevel@tonic-gate 	 * was allocated above in the  ehci_alloc_qh and this new one.
5037c478bd9Sstevel@tonic-gate 	 */
5047c478bd9Sstevel@tonic-gate 	if ((ehci_start_periodic_pipe_polling(ehcip,
5057c478bd9Sstevel@tonic-gate 	    ehci_polledp->ehci_polled_input_pipe_handle,
5067c478bd9Sstevel@tonic-gate 	    NULL, USB_FLAGS_SLEEP)) != USB_SUCCESS) {
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		return (USB_NO_RESOURCES);
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	/* Get the given new interrupt qtd */
5127c478bd9Sstevel@tonic-gate 	qtd = (ehci_qtd_t *)(ehci_qtd_iommu_to_cpu(ehcip,
5137c478bd9Sstevel@tonic-gate 	    (Get_QH(pp->pp_qh->qh_next_qtd) & EHCI_QH_NEXT_QTD_PTR)));
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	/* Insert this qtd into active interrupt QTD list */
5167c478bd9Sstevel@tonic-gate 	ehci_polled_insert_qtd_into_active_intr_qtd_list(ehci_polledp, qtd);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate /*
5237c478bd9Sstevel@tonic-gate  * Polled deinitialization routines
5247c478bd9Sstevel@tonic-gate  */
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /*
5287c478bd9Sstevel@tonic-gate  * ehci_polled_fini:
5297c478bd9Sstevel@tonic-gate  */
5307c478bd9Sstevel@tonic-gate static int
5317c478bd9Sstevel@tonic-gate ehci_polled_fini(ehci_polled_t	*ehci_polledp)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
5347c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	/* If the structure is already in use, then don't free it */
5397c478bd9Sstevel@tonic-gate 	if (ehci_polledp->ehci_polled_flags & POLLED_INPUT_MODE) {
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	pp = (ehci_pipe_private_t *)
5457c478bd9Sstevel@tonic-gate 	    ehci_polledp->ehci_polled_input_pipe_handle->p_hcd_private;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/* Deallocate all the pre-allocated interrupt requests */
5487c478bd9Sstevel@tonic-gate 	ehci_handle_outstanding_requests(ehcip, pp);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	/*
5517c478bd9Sstevel@tonic-gate 	 * Traverse the list of QTD's on this endpoint and these QTD's
5527c478bd9Sstevel@tonic-gate 	 * have outstanding transfer requests. Since list processing
5537c478bd9Sstevel@tonic-gate 	 * is stopped, these QTDs can be deallocated.
5547c478bd9Sstevel@tonic-gate 	 */
5557c478bd9Sstevel@tonic-gate 	ehci_polled_traverse_qtds(ehci_polledp, pp->pp_pipe_handle);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	/* Free DMA resources */
5587c478bd9Sstevel@tonic-gate 	ehci_free_dma_resources(ehcip, pp->pp_pipe_handle);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	/*
5617c478bd9Sstevel@tonic-gate 	 * Deallocate the endpoint descriptors that we allocated
5627c478bd9Sstevel@tonic-gate 	 * with ehci_alloc_qh.
5637c478bd9Sstevel@tonic-gate 	 */
5647c478bd9Sstevel@tonic-gate 	if (ehci_polledp->ehci_polled_dummy_qh) {
5657c478bd9Sstevel@tonic-gate 		ehci_deallocate_qh(ehcip, ehci_polledp->ehci_polled_dummy_qh);
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	if (ehci_polledp->ehci_polled_qh) {
5697c478bd9Sstevel@tonic-gate 		ehci_deallocate_qh(ehcip, ehci_polledp->ehci_polled_qh);
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	mutex_destroy(&ehci_polledp->ehci_polled_input_pipe_handle->p_mutex);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	/*
5757c478bd9Sstevel@tonic-gate 	 * Destroy everything about the pipe that we allocated in
5767c478bd9Sstevel@tonic-gate 	 * ehci_polled_duplicate_pipe_handle
5777c478bd9Sstevel@tonic-gate 	 */
5787c478bd9Sstevel@tonic-gate 	kmem_free(pp, sizeof (ehci_pipe_private_t));
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	kmem_free(ehci_polledp->ehci_polled_input_pipe_handle,
5817c478bd9Sstevel@tonic-gate 	    sizeof (usba_pipe_handle_data_t));
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	/*
5847c478bd9Sstevel@tonic-gate 	 * We use this field to determine if a QTD is for input or not,
5857c478bd9Sstevel@tonic-gate 	 * so NULL the pointer so we don't check deallocated data.
5867c478bd9Sstevel@tonic-gate 	 */
5877c478bd9Sstevel@tonic-gate 	ehci_polledp->ehci_polled_input_pipe_handle = NULL;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	/*
5907c478bd9Sstevel@tonic-gate 	 * Finally, free off the structure that we use to keep track
5917c478bd9Sstevel@tonic-gate 	 * of all this.
5927c478bd9Sstevel@tonic-gate 	 */
5937c478bd9Sstevel@tonic-gate 	kmem_free(ehci_polledp, sizeof (ehci_polled_t));
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate /*
6007c478bd9Sstevel@tonic-gate  * Polled save state routines
6017c478bd9Sstevel@tonic-gate  */
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate /*
6057c478bd9Sstevel@tonic-gate  * ehci_polled_save_state:
6067c478bd9Sstevel@tonic-gate  */
6077c478bd9Sstevel@tonic-gate static void
6087c478bd9Sstevel@tonic-gate ehci_polled_save_state(ehci_polled_t	*ehci_polledp)
6097c478bd9Sstevel@tonic-gate {
6107c478bd9Sstevel@tonic-gate 	int				i;
6117c478bd9Sstevel@tonic-gate 	ehci_state_t			*ehcip;
6127c478bd9Sstevel@tonic-gate 	uint_t				polled_toggle;
6137c478bd9Sstevel@tonic-gate 	uint_t				real_toggle;
6147c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t		*pp = NULL; /* Normal mode Pipe */
6157c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t		*polled_pp; /* Polled mode Pipe */
6167c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t		*ph;
6177c478bd9Sstevel@tonic-gate 	uint8_t				ep_addr;
6187c478bd9Sstevel@tonic-gate 	ehci_regs_t			*ehci_polled_regsp;
6197c478bd9Sstevel@tonic-gate 	ehci_qh_t			*qh;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate #ifndef lint
6227c478bd9Sstevel@tonic-gate 	_NOTE(NO_COMPETING_THREADS_NOW);
6237c478bd9Sstevel@tonic-gate #endif
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/*
6267c478bd9Sstevel@tonic-gate 	 * If either of these two flags are set, then we have already
6277c478bd9Sstevel@tonic-gate 	 * saved off the state information and setup the controller.
6287c478bd9Sstevel@tonic-gate 	 */
6297c478bd9Sstevel@tonic-gate 	if (ehci_polledp->ehci_polled_flags & POLLED_INPUT_MODE_INUSE) {
6307c478bd9Sstevel@tonic-gate #ifndef lint
6317c478bd9Sstevel@tonic-gate 		_NOTE(COMPETING_THREADS_NOW);
6327c478bd9Sstevel@tonic-gate #endif
6337c478bd9Sstevel@tonic-gate 		return;
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	ehcip = ehci_polledp->ehci_polled_ehcip;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	/*
6397c478bd9Sstevel@tonic-gate 	 * Check if the number of keyboard reach the max number we can
6407c478bd9Sstevel@tonic-gate 	 * support in polled mode
6417c478bd9Sstevel@tonic-gate 	 */
6427c478bd9Sstevel@tonic-gate 	if (++ ehcip->ehci_polled_enter_count > MAX_NUM_FOR_KEYBOARD) {
6437c478bd9Sstevel@tonic-gate #ifndef lint
6447c478bd9Sstevel@tonic-gate 		_NOTE(COMPETING_THREADS_NOW);
6457c478bd9Sstevel@tonic-gate #endif
6467c478bd9Sstevel@tonic-gate 		return;
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate 	ehci_polled_regsp = &ehcip->ehci_polled_save_regs;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	/* Get the endpoint addr. */
6517c478bd9Sstevel@tonic-gate 	ep_addr = ehci_polledp->ehci_polled_ep_addr;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	/* Get the normal mode usb pipe handle */
6547c478bd9Sstevel@tonic-gate 	ph = usba_hcdi_get_ph_data(ehci_polledp->ehci_polled_usb_dev, ep_addr);
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	/*
6577c478bd9Sstevel@tonic-gate 	 * The first enter keyboard entry should save info of the normal mode,
6587c478bd9Sstevel@tonic-gate 	 * disable all list processing and interrupt, initialize the
6597c478bd9Sstevel@tonic-gate 	 * frame list table with dummy QHs.
6607c478bd9Sstevel@tonic-gate 	 */
6617c478bd9Sstevel@tonic-gate 	if (ehcip->ehci_polled_enter_count == 1) {
6627c478bd9Sstevel@tonic-gate 		/*
6637c478bd9Sstevel@tonic-gate 		 * Save the current normal mode ehci registers	and later this
6647c478bd9Sstevel@tonic-gate 		 * saved register copy is used to replace some of required ehci
6657c478bd9Sstevel@tonic-gate 		 * registers before switching from polled mode to normal mode.
6667c478bd9Sstevel@tonic-gate 		 */
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 		bzero((void *)ehci_polled_regsp, sizeof (ehci_regs_t));
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		/* Save current ehci registers */
6717c478bd9Sstevel@tonic-gate 		ehci_polled_regsp->ehci_command = Get_OpReg(ehci_command);
6727c478bd9Sstevel@tonic-gate 		ehci_polled_regsp->ehci_interrupt = Get_OpReg(ehci_interrupt);
6737c478bd9Sstevel@tonic-gate 		ehci_polled_regsp->ehci_ctrl_segment =
6747c478bd9Sstevel@tonic-gate 		    Get_OpReg(ehci_ctrl_segment);
6757c478bd9Sstevel@tonic-gate 		ehci_polled_regsp->
6767c478bd9Sstevel@tonic-gate 		    ehci_async_list_addr = Get_OpReg(ehci_async_list_addr);
6777c478bd9Sstevel@tonic-gate 		ehci_polled_regsp->ehci_config_flag =
6787c478bd9Sstevel@tonic-gate 		    Get_OpReg(ehci_config_flag);
6797c478bd9Sstevel@tonic-gate 		ehci_polled_regsp->ehci_periodic_list_base =
6807c478bd9Sstevel@tonic-gate 		    Get_OpReg(ehci_periodic_list_base);
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		/* Disable all list processing and interrupts */
6837c478bd9Sstevel@tonic-gate 		Set_OpReg(ehci_command, Get_OpReg(ehci_command) &
6847c478bd9Sstevel@tonic-gate 		    ~(EHCI_CMD_ASYNC_SCHED_ENABLE |
6857c478bd9Sstevel@tonic-gate 		    EHCI_CMD_PERIODIC_SCHED_ENABLE));
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 		/* Wait for few milliseconds */
6887c478bd9Sstevel@tonic-gate 		drv_usecwait(EHCI_POLLED_TIMEWAIT);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 		/* Save any unprocessed normal mode ehci interrupts */
6917c478bd9Sstevel@tonic-gate 		ehcip->ehci_missed_intr_sts = EHCI_INTR_USB;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		/*
6947c478bd9Sstevel@tonic-gate 		 * Save the current interrupt lattice and  replace this lattice
6957c478bd9Sstevel@tonic-gate 		 * with an lattice used in POLLED mode. We will restore lattice
6967c478bd9Sstevel@tonic-gate 		 * back when we exit from the POLLED mode.
6977c478bd9Sstevel@tonic-gate 		 */
6987c478bd9Sstevel@tonic-gate 		for (i = 0; i < EHCI_NUM_PERIODIC_FRAME_LISTS; i++) {
6997c478bd9Sstevel@tonic-gate 			ehcip->ehci_polled_frame_list_table[i] =
7007c478bd9Sstevel@tonic-gate 			    (ehci_qh_t *)(uintptr_t)Get_PFLT(ehcip->
7017c478bd9Sstevel@tonic-gate 			    ehci_periodic_frame_list_tablep->
7027c478bd9Sstevel@tonic-gate 			    ehci_periodic_frame_list_table[i]);
7037c478bd9Sstevel@tonic-gate 		}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 		/*
7067c478bd9Sstevel@tonic-gate 		 * Fill in the lattice with dummy QHs. These QHs are used so the
7077c478bd9Sstevel@tonic-gate 		 * controller can tell that it is at the end of the QH list.
7087c478bd9Sstevel@tonic-gate 		 */
7097c478bd9Sstevel@tonic-gate 		for (i = 0; i < EHCI_NUM_PERIODIC_FRAME_LISTS; i++) {
7107c478bd9Sstevel@tonic-gate 			Set_PFLT(ehcip->ehci_periodic_frame_list_tablep->
7117c478bd9Sstevel@tonic-gate 			    ehci_periodic_frame_list_table[i],
7127c478bd9Sstevel@tonic-gate 			    ehci_qh_cpu_to_iommu(ehcip,
7137c478bd9Sstevel@tonic-gate 			    ehci_polledp->ehci_polled_dummy_qh) |
7147c478bd9Sstevel@tonic-gate 			    (EHCI_QH_LINK_REF_QH | EHCI_QH_LINK_PTR_VALID));
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	/* Get the polled mode ehci pipe private structure */
7207c478bd9Sstevel@tonic-gate 	polled_pp = (ehci_pipe_private_t *)
7217c478bd9Sstevel@tonic-gate 	    ehci_polledp->ehci_polled_input_pipe_handle->p_hcd_private;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	/*
7247c478bd9Sstevel@tonic-gate 	 * Before replacing the lattice, adjust the data togggle on the
7257c478bd9Sstevel@tonic-gate 	 * on the ehci's interrupt ed
7267c478bd9Sstevel@tonic-gate 	 */
7277c478bd9Sstevel@tonic-gate 	polled_toggle = (Get_QH(polled_pp->pp_qh->qh_status) &
7287c478bd9Sstevel@tonic-gate 	    EHCI_QH_STS_DATA_TOGGLE) ? DATA1:DATA0;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	/*
7317c478bd9Sstevel@tonic-gate 	 * If normal mode interrupt pipe endpoint is active, get the data
7327c478bd9Sstevel@tonic-gate 	 * toggle from the this interrupt endpoint through the corresponding
7337c478bd9Sstevel@tonic-gate 	 * interrupt pipe handle. Else get the data toggle information from
7347c478bd9Sstevel@tonic-gate 	 * the usb device structure and this information is saved during the
7357c478bd9Sstevel@tonic-gate 	 * normal mode interrupt pipe close. Use this data toggle information
7367c478bd9Sstevel@tonic-gate 	 * to fix the data toggle of polled mode interrupt endpoint.
7377c478bd9Sstevel@tonic-gate 	 */
7387c478bd9Sstevel@tonic-gate 	if (ph) {
7397c478bd9Sstevel@tonic-gate 		/* Get the normal mode ehci pipe private structure */
7407c478bd9Sstevel@tonic-gate 		pp = (ehci_pipe_private_t *)ph->p_hcd_private;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		real_toggle = (Get_QH(pp->pp_qh->qh_status) &
7437c478bd9Sstevel@tonic-gate 		    EHCI_QH_STS_DATA_TOGGLE) ? DATA1:DATA0;
7447c478bd9Sstevel@tonic-gate 	} else {
7457c478bd9Sstevel@tonic-gate 		real_toggle = usba_hcdi_get_data_toggle(
7467c478bd9Sstevel@tonic-gate 		    ehci_polledp->ehci_polled_usb_dev, ep_addr);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	if (polled_toggle != real_toggle) {
7507c478bd9Sstevel@tonic-gate 		if (real_toggle == DATA0) {
7517c478bd9Sstevel@tonic-gate 			Set_QH(polled_pp->pp_qh->qh_status,
7527c478bd9Sstevel@tonic-gate 			    Get_QH(polled_pp->pp_qh->qh_status) &
7537c478bd9Sstevel@tonic-gate 			    ~EHCI_QH_STS_DATA_TOGGLE);
7547c478bd9Sstevel@tonic-gate 		} else {
7557c478bd9Sstevel@tonic-gate 			Set_QH(polled_pp->pp_qh->qh_status,
7567c478bd9Sstevel@tonic-gate 			    Get_QH(polled_pp->pp_qh->qh_status) |
7577c478bd9Sstevel@tonic-gate 			    EHCI_QH_STS_DATA_TOGGLE);
7587c478bd9Sstevel@tonic-gate 		}
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	/*
7627c478bd9Sstevel@tonic-gate 	 * Check whether Halt bit is set in the QH and if so  clear the
7637c478bd9Sstevel@tonic-gate 	 * halt bit.
7647c478bd9Sstevel@tonic-gate 	 */
7657c478bd9Sstevel@tonic-gate 	if (polled_pp->pp_qh->qh_status & EHCI_QH_STS_HALTED) {
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 		/* Clear the halt bit */
7687c478bd9Sstevel@tonic-gate 		Set_QH(polled_pp->pp_qh->qh_status,
7697c478bd9Sstevel@tonic-gate 		    (Get_QH(polled_pp->pp_qh->qh_status) &
7707c478bd9Sstevel@tonic-gate 		    ~EHCI_QH_STS_HALTED));
7717c478bd9Sstevel@tonic-gate 	}
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	/*
7747c478bd9Sstevel@tonic-gate 	 * Initialize the qh overlay area
7757c478bd9Sstevel@tonic-gate 	 */
7767c478bd9Sstevel@tonic-gate 	qh = ehci_polledp->ehci_polled_qh;
7777c478bd9Sstevel@tonic-gate 	for (i = 0; i < 5; i++) {
7787c478bd9Sstevel@tonic-gate 		Set_QH(qh->qh_buf[i], NULL);
7797c478bd9Sstevel@tonic-gate 		Set_QH(qh->qh_buf_high[i], NULL);
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_next_qtd, ehci_qtd_cpu_to_iommu(ehcip,
7827c478bd9Sstevel@tonic-gate 	    ehci_polledp->ehci_polled_active_intr_qtd_list));
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	/*
7857c478bd9Sstevel@tonic-gate 	 * Now, add the endpoint to the lattice that we will  hang  our
7867c478bd9Sstevel@tonic-gate 	 * QTD's off of.  We need to poll this device at  every 8 ms and
7877c478bd9Sstevel@tonic-gate 	 * hence add this QH needs 4 entries in interrupt lattice.
7887c478bd9Sstevel@tonic-gate 	 */
7897c478bd9Sstevel@tonic-gate 	for (i = ehcip->ehci_polled_enter_count - 1;
7907c478bd9Sstevel@tonic-gate 	    i < EHCI_NUM_PERIODIC_FRAME_LISTS;
7917c478bd9Sstevel@tonic-gate 	    i = i + LS_MIN_POLL_INTERVAL) {
7927c478bd9Sstevel@tonic-gate 		Set_PFLT(ehcip->ehci_periodic_frame_list_tablep->
7937c478bd9Sstevel@tonic-gate 		    ehci_periodic_frame_list_table[i],
7947c478bd9Sstevel@tonic-gate 		    ehci_qh_cpu_to_iommu(ehcip,
7957c478bd9Sstevel@tonic-gate 		    ehci_polledp->ehci_polled_qh) | EHCI_QH_LINK_REF_QH);
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate 	/* The first enter keyboard entry enable interrupts and periodic list */
7987c478bd9Sstevel@tonic-gate 	if (ehcip->ehci_polled_enter_count == 1) {
7997c478bd9Sstevel@tonic-gate 		/* Enable USB and Frame list rollover interrupts */
8007c478bd9Sstevel@tonic-gate 		Set_OpReg(ehci_interrupt, (EHCI_INTR_USB |
8017c478bd9Sstevel@tonic-gate 		    EHCI_INTR_USB_ERROR | EHCI_INTR_FRAME_LIST_ROLLOVER));
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 		/* Enable the periodic list */
8047c478bd9Sstevel@tonic-gate 		Set_OpReg(ehci_command,
8057c478bd9Sstevel@tonic-gate 		    (Get_OpReg(ehci_command) | EHCI_CMD_PERIODIC_SCHED_ENABLE));
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 		/* Wait for few milliseconds */
8087c478bd9Sstevel@tonic-gate 		drv_usecwait(EHCI_POLLED_TIMEWAIT);
8097c478bd9Sstevel@tonic-gate 	}
8107c478bd9Sstevel@tonic-gate #ifndef lint
8117c478bd9Sstevel@tonic-gate 	_NOTE(COMPETING_THREADS_NOW);
8127c478bd9Sstevel@tonic-gate #endif
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate /*
8177c478bd9Sstevel@tonic-gate  * Polled restore state routines
8187c478bd9Sstevel@tonic-gate  */
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate /*
8227c478bd9Sstevel@tonic-gate  * ehci_polled_restore_state:
8237c478bd9Sstevel@tonic-gate  */
8247c478bd9Sstevel@tonic-gate static void
8257c478bd9Sstevel@tonic-gate ehci_polled_restore_state(ehci_polled_t	*ehci_polledp)
8267c478bd9Sstevel@tonic-gate {
8277c478bd9Sstevel@tonic-gate 	ehci_state_t			*ehcip;
8287c478bd9Sstevel@tonic-gate 	int				i;
8297c478bd9Sstevel@tonic-gate 	uint_t				polled_toggle;
8307c478bd9Sstevel@tonic-gate 	uint_t				real_toggle;
8317c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t		*pp = NULL; /* Normal mode Pipe */
8327c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t		*polled_pp; /* Polled mode Pipe */
8337c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t		*ph;
8347c478bd9Sstevel@tonic-gate 	uint8_t				ep_addr;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate #ifndef lint
8377c478bd9Sstevel@tonic-gate 	_NOTE(NO_COMPETING_THREADS_NOW);
8387c478bd9Sstevel@tonic-gate #endif
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	/*
8417c478bd9Sstevel@tonic-gate 	 * If this flag is set, then we are still using this structure,
8427c478bd9Sstevel@tonic-gate 	 * so don't restore any controller state information yet.
8437c478bd9Sstevel@tonic-gate 	 */
8447c478bd9Sstevel@tonic-gate 	if (ehci_polledp->ehci_polled_flags & POLLED_INPUT_MODE_INUSE) {
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate #ifndef lint
8477c478bd9Sstevel@tonic-gate 		_NOTE(COMPETING_THREADS_NOW);
8487c478bd9Sstevel@tonic-gate #endif
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 		return;
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	ehcip = ehci_polledp->ehci_polled_ehcip;
8547c478bd9Sstevel@tonic-gate 	ehcip->ehci_polled_enter_count --;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	/* Get the endpoint addr */
8577c478bd9Sstevel@tonic-gate 	ep_addr = ehci_polledp->ehci_polled_ep_addr;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	/* Get the normal mode usb pipe handle */
8607c478bd9Sstevel@tonic-gate 	ph = usba_hcdi_get_ph_data(ehci_polledp->ehci_polled_usb_dev, ep_addr);
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	/* Disable list processing and other things */
8637c478bd9Sstevel@tonic-gate 	ehci_polled_stop_processing(ehci_polledp);
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	/* Get the polled mode ehci pipe private structure */
8667c478bd9Sstevel@tonic-gate 	polled_pp = (ehci_pipe_private_t *)
8677c478bd9Sstevel@tonic-gate 	    ehci_polledp->ehci_polled_input_pipe_handle->p_hcd_private;
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	/*
8707c478bd9Sstevel@tonic-gate 	 * Before replacing the lattice, adjust the data togggle
8717c478bd9Sstevel@tonic-gate 	 * on the on the ehci's interrupt ed
8727c478bd9Sstevel@tonic-gate 	 */
8737c478bd9Sstevel@tonic-gate 	polled_toggle = (Get_QH(polled_pp->pp_qh->qh_status) &
8747c478bd9Sstevel@tonic-gate 	    EHCI_QH_STS_DATA_TOGGLE) ? DATA1:DATA0;
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	/*
8777c478bd9Sstevel@tonic-gate 	 * If normal mode interrupt pipe endpoint is active, fix the
8787c478bd9Sstevel@tonic-gate 	 * data toggle for this interrupt endpoint by getting the data
8797c478bd9Sstevel@tonic-gate 	 * toggle information from the polled interrupt endpoint. Else
8807c478bd9Sstevel@tonic-gate 	 * save the data toggle information in usb device structure.
8817c478bd9Sstevel@tonic-gate 	 */
8827c478bd9Sstevel@tonic-gate 	if (ph) {
8837c478bd9Sstevel@tonic-gate 		/* Get the normal mode ehci pipe private structure */
8847c478bd9Sstevel@tonic-gate 		pp = (ehci_pipe_private_t *)ph->p_hcd_private;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 		real_toggle = (Get_QH(pp->pp_qh->qh_status) &
8877c478bd9Sstevel@tonic-gate 		    EHCI_QH_STS_DATA_TOGGLE) ? DATA1:DATA0;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 		if (polled_toggle != real_toggle) {
8907c478bd9Sstevel@tonic-gate 			if (polled_toggle == DATA0) {
8917c478bd9Sstevel@tonic-gate 				Set_QH(pp->pp_qh->qh_status,
8927c478bd9Sstevel@tonic-gate 				    Get_QH(pp->pp_qh->qh_status) &
8937c478bd9Sstevel@tonic-gate 				    ~EHCI_QH_STS_DATA_TOGGLE);
8947c478bd9Sstevel@tonic-gate 			} else {
8957c478bd9Sstevel@tonic-gate 				Set_QH(pp->pp_qh->qh_status,
8967c478bd9Sstevel@tonic-gate 				    Get_QH(pp->pp_qh->qh_status) |
8977c478bd9Sstevel@tonic-gate 				    EHCI_QH_STS_DATA_TOGGLE);
8987c478bd9Sstevel@tonic-gate 			}
8997c478bd9Sstevel@tonic-gate 		}
9007c478bd9Sstevel@tonic-gate 	} else {
9017c478bd9Sstevel@tonic-gate 		usba_hcdi_set_data_toggle(ehci_polledp->ehci_polled_usb_dev,
9027c478bd9Sstevel@tonic-gate 		    ep_addr, polled_toggle);
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	/*
9067c478bd9Sstevel@tonic-gate 	 * Only the last leave keyboard entry restore the save frame
9077c478bd9Sstevel@tonic-gate 	 * list table and start processing.
9087c478bd9Sstevel@tonic-gate 	 */
9097c478bd9Sstevel@tonic-gate 	if (ehcip->ehci_polled_enter_count == 0) {
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 		/* Replace the lattice */
9127c478bd9Sstevel@tonic-gate 		for (i = 0; i < EHCI_NUM_PERIODIC_FRAME_LISTS; i++) {
9137c478bd9Sstevel@tonic-gate 			Set_PFLT(ehcip->ehci_periodic_frame_list_tablep->
9147c478bd9Sstevel@tonic-gate 			    ehci_periodic_frame_list_table[i],
9157c478bd9Sstevel@tonic-gate 			    ehcip->ehci_polled_frame_list_table[i]);
9167c478bd9Sstevel@tonic-gate 		}
9177c478bd9Sstevel@tonic-gate 		ehci_polled_start_processing(ehci_polledp);
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate #ifndef lint
9217c478bd9Sstevel@tonic-gate 	_NOTE(COMPETING_THREADS_NOW);
9227c478bd9Sstevel@tonic-gate #endif
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate /*
9277c478bd9Sstevel@tonic-gate  * ehci_polled_stop_processing:
9287c478bd9Sstevel@tonic-gate  */
9297c478bd9Sstevel@tonic-gate static void
9307c478bd9Sstevel@tonic-gate ehci_polled_stop_processing(ehci_polled_t	*ehci_polledp)
9317c478bd9Sstevel@tonic-gate {
9327c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip;
9337c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh = ehci_polledp->ehci_polled_qh;
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	ehcip = ehci_polledp->ehci_polled_ehcip;
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	/* First inactive this QH */
9387c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_ctrl,
9397c478bd9Sstevel@tonic-gate 	    Get_QH(qh->qh_ctrl) | EHCI_QH_CTRL_ED_INACTIVATE);
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	/* Only first leave keyboard entry turn off periodic list processing */
9427c478bd9Sstevel@tonic-gate 	if (Get_OpReg(ehci_command) & EHCI_CMD_PERIODIC_SCHED_ENABLE) {
9437c478bd9Sstevel@tonic-gate 		Set_OpReg(ehci_command, (Get_OpReg(ehci_command) &
9447c478bd9Sstevel@tonic-gate 			~EHCI_CMD_PERIODIC_SCHED_ENABLE));
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 		/* Wait for few milliseconds */
9477c478bd9Sstevel@tonic-gate 		drv_usecwait(EHCI_POLLED_TIMEWAIT);
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 	/*
9507c478bd9Sstevel@tonic-gate 	 * Now clear all required fields of QH
9517c478bd9Sstevel@tonic-gate 	 * including inactive bit.
9527c478bd9Sstevel@tonic-gate 	 */
9537c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_ctrl,
9547c478bd9Sstevel@tonic-gate 	    Get_QH(qh->qh_ctrl) & ~(EHCI_QH_CTRL_ED_INACTIVATE));
9557c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_status,
9567c478bd9Sstevel@tonic-gate 	    Get_QH(qh->qh_status) & ~(EHCI_QH_STS_XACT_STATUS));
9577c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_curr_qtd, NULL);
9587c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_alt_next_qtd, EHCI_QH_ALT_NEXT_QTD_PTR_VALID);
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	/*
9617c478bd9Sstevel@tonic-gate 	 * Now look up at the QTD's that are in the active qtd list &
9627c478bd9Sstevel@tonic-gate 	 * re-insert them back into the QH's QTD list.
9637c478bd9Sstevel@tonic-gate 	 */
9647c478bd9Sstevel@tonic-gate 	(void) ehci_polled_process_active_intr_qtd_list(ehci_polledp);
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate /*
9697c478bd9Sstevel@tonic-gate  * ehci_polled_start_processing:
9707c478bd9Sstevel@tonic-gate  */
9717c478bd9Sstevel@tonic-gate static void
9727c478bd9Sstevel@tonic-gate ehci_polled_start_processing(ehci_polled_t	*ehci_polledp)
9737c478bd9Sstevel@tonic-gate {
9747c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip;
9757c478bd9Sstevel@tonic-gate 	uint32_t		mask;
9767c478bd9Sstevel@tonic-gate 	ehci_regs_t		*ehci_polled_regsp;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	ehcip = ehci_polledp->ehci_polled_ehcip;
9797c478bd9Sstevel@tonic-gate 	ehci_polled_regsp = &ehcip->ehci_polled_save_regs;
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	mask = ((uint32_t)ehci_polled_regsp->ehci_interrupt &
9827c478bd9Sstevel@tonic-gate 	    (EHCI_INTR_HOST_SYSTEM_ERROR | EHCI_INTR_FRAME_LIST_ROLLOVER |
9837c478bd9Sstevel@tonic-gate 	    EHCI_INTR_USB_ERROR | EHCI_INTR_USB | EHCI_INTR_ASYNC_ADVANCE));
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	/* Enable all required EHCI interrupts */
9867c478bd9Sstevel@tonic-gate 	Set_OpReg(ehci_interrupt, mask);
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	mask = ((uint32_t)ehci_polled_regsp->ehci_command &
9897c478bd9Sstevel@tonic-gate 	    (EHCI_CMD_ASYNC_SCHED_ENABLE | EHCI_CMD_PERIODIC_SCHED_ENABLE));
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	/* Enable all reuired list processing */
9927c478bd9Sstevel@tonic-gate 	Set_OpReg(ehci_command, (Get_OpReg(ehci_command) | mask));
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	/* Wait for few milliseconds */
9957c478bd9Sstevel@tonic-gate 	drv_usecwait(EHCI_POLLED_TIMEWAIT);
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate /*
10007c478bd9Sstevel@tonic-gate  * Polled read routines
10017c478bd9Sstevel@tonic-gate  */
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate /*
10057c478bd9Sstevel@tonic-gate  * ehci_polled_process_active_intr_qtd_list:
10067c478bd9Sstevel@tonic-gate  *
10077c478bd9Sstevel@tonic-gate  * This routine takes the QTD's off of the input done head and processes
10087c478bd9Sstevel@tonic-gate  * them.  It returns the number of characters that have been copied for
10097c478bd9Sstevel@tonic-gate  * input.
10107c478bd9Sstevel@tonic-gate  */
10117c478bd9Sstevel@tonic-gate static int
10127c478bd9Sstevel@tonic-gate ehci_polled_process_active_intr_qtd_list(ehci_polled_t	*ehci_polledp)
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
10157c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd, *next_qtd;
10167c478bd9Sstevel@tonic-gate 	uint_t			num_characters = 0;
10177c478bd9Sstevel@tonic-gate 	uint_t			ctrl;
10187c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw;
10197c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp;
10207c478bd9Sstevel@tonic-gate 	usb_cr_t 		error;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	/* Sync QH and QTD pool */
10237c478bd9Sstevel@tonic-gate 	if (ehci_polledp->ehci_polled_no_sync_flag == B_FALSE) {
10247c478bd9Sstevel@tonic-gate 		Sync_QH_QTD_Pool(ehcip);
10257c478bd9Sstevel@tonic-gate 	}
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	/* Create done qtd list */
10287c478bd9Sstevel@tonic-gate 	qtd = ehci_polled_create_done_qtd_list(ehci_polledp);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	/*
10317c478bd9Sstevel@tonic-gate 	 * Traverse the list of transfer descriptors.  We can't destroy
10327c478bd9Sstevel@tonic-gate 	 * the qtd_next pointers of these QTDs because we are using it
10337c478bd9Sstevel@tonic-gate 	 * to traverse the done list.  Therefore, we can not put these
10347c478bd9Sstevel@tonic-gate 	 * QTD's back on the QH until we are done processing all of them.
10357c478bd9Sstevel@tonic-gate 	 */
10367c478bd9Sstevel@tonic-gate 	while (qtd) {
10377c478bd9Sstevel@tonic-gate 		/* Get next active QTD from the active QTD list */
10387c478bd9Sstevel@tonic-gate 		next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
10397c478bd9Sstevel@tonic-gate 		    Get_QTD(qtd->qtd_active_qtd_next));
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 		/* Obtain the transfer wrapper from the QTD */
10427c478bd9Sstevel@tonic-gate 		tw = (ehci_trans_wrapper_t *)EHCI_LOOKUP_ID(
10437c478bd9Sstevel@tonic-gate 		    (uint32_t)Get_QTD(qtd->qtd_trans_wrapper));
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 		/* Get ohci pipe from transfer wrapper */
10467c478bd9Sstevel@tonic-gate 		pp = tw->tw_pipe_private;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 		/* Look at the status */
10497c478bd9Sstevel@tonic-gate 		ctrl = (uint_t)Get_QTD(qtd->qtd_ctrl) &
10507c478bd9Sstevel@tonic-gate 		    (uint32_t)EHCI_QTD_CTRL_XACT_STATUS;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 		error = ehci_check_for_error(ehcip, pp, tw, qtd, ctrl);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 		/*
10557c478bd9Sstevel@tonic-gate 		 * Check to see if there is an error. If there is error
10567c478bd9Sstevel@tonic-gate 		 * clear the halt condition in the Endpoint  Descriptor
10577c478bd9Sstevel@tonic-gate 		 * (QH) associated with this Transfer  Descriptor (QTD).
10587c478bd9Sstevel@tonic-gate 		 */
10597c478bd9Sstevel@tonic-gate 		if (error == USB_CR_OK) {
10607c478bd9Sstevel@tonic-gate 			num_characters +=
10617c478bd9Sstevel@tonic-gate 			    ehci_polled_handle_normal_qtd(ehci_polledp, qtd);
10627c478bd9Sstevel@tonic-gate 		} else {
10637c478bd9Sstevel@tonic-gate 			/* Clear the halt bit */
10647c478bd9Sstevel@tonic-gate 			Set_QH(pp->pp_qh->qh_status,
10657c478bd9Sstevel@tonic-gate 			    Get_QH(pp->pp_qh->qh_status) &
10667c478bd9Sstevel@tonic-gate 			    ~(EHCI_QH_STS_XACT_STATUS));
10677c478bd9Sstevel@tonic-gate 		}
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 		/* Insert this qtd back into QH's qtd list */
10707c478bd9Sstevel@tonic-gate 		ehci_polled_insert_qtd(ehci_polledp, qtd);
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 		qtd = next_qtd;
10737c478bd9Sstevel@tonic-gate 	}
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	return (num_characters);
10767c478bd9Sstevel@tonic-gate }
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate /*
10807c478bd9Sstevel@tonic-gate  * ehci_polled_handle_normal_qtd:
10817c478bd9Sstevel@tonic-gate  */
10827c478bd9Sstevel@tonic-gate static int
10837c478bd9Sstevel@tonic-gate ehci_polled_handle_normal_qtd(
10847c478bd9Sstevel@tonic-gate 	ehci_polled_t		*ehci_polledp,
10857c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd)
10867c478bd9Sstevel@tonic-gate {
10877c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
10887c478bd9Sstevel@tonic-gate 	uchar_t			*buf;
10897c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw;
10907c478bd9Sstevel@tonic-gate 	size_t			length;
10917c478bd9Sstevel@tonic-gate 	uint32_t		residue;
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	/* Obtain the transfer wrapper from the QTD */
10947c478bd9Sstevel@tonic-gate 	tw = (ehci_trans_wrapper_t *)EHCI_LOOKUP_ID((uint32_t)
10957c478bd9Sstevel@tonic-gate 		Get_QTD(qtd->qtd_trans_wrapper));
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	ASSERT(tw != NULL);
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	buf = (uchar_t *)tw->tw_buf;
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	length = tw->tw_length;
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	/*
11047c478bd9Sstevel@tonic-gate 	 * If "Total bytes of xfer" in control field of qtd is not equal to 0,
11057c478bd9Sstevel@tonic-gate 	 * then we received less data from the usb device than requested by us.
11067c478bd9Sstevel@tonic-gate 	 * In that case, get the actual received data size.
11077c478bd9Sstevel@tonic-gate 	 */
11087c478bd9Sstevel@tonic-gate 	residue = ((Get_QTD(qtd->qtd_ctrl) &
11097c478bd9Sstevel@tonic-gate 	    EHCI_QTD_CTRL_BYTES_TO_XFER) >> EHCI_QTD_CTRL_BYTES_TO_XFER_SHIFT);
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	if (residue) {
11127c478bd9Sstevel@tonic-gate 
1113*3304303fSsl 		length = Get_QTD(qtd->qtd_xfer_offs) +
1114*3304303fSsl 		    Get_QTD(qtd->qtd_xfer_len) - residue;
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	/* Sync IO buffer */
11187c478bd9Sstevel@tonic-gate 	if (ehci_polledp->ehci_polled_no_sync_flag == B_FALSE) {
11197c478bd9Sstevel@tonic-gate 		Sync_IO_Buffer(tw->tw_dmahandle, length);
11207c478bd9Sstevel@tonic-gate 	}
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	/* Copy the data into the message */
11237c478bd9Sstevel@tonic-gate 	bcopy(buf, ehci_polledp->ehci_polled_buf, length);
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	return (length);
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate /*
11307c478bd9Sstevel@tonic-gate  * ehci_polled_insert_qtd:
11317c478bd9Sstevel@tonic-gate  *
11327c478bd9Sstevel@tonic-gate  * Insert a Transfer Descriptor (QTD) on an Endpoint Descriptor (QH).
11337c478bd9Sstevel@tonic-gate  */
11347c478bd9Sstevel@tonic-gate static void
11357c478bd9Sstevel@tonic-gate ehci_polled_insert_qtd(
11367c478bd9Sstevel@tonic-gate 	ehci_polled_t		*ehci_polledp,
11377c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd)
11387c478bd9Sstevel@tonic-gate {
11397c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
11407c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*curr_dummy_qtd, *next_dummy_qtd;
11417c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*new_dummy_qtd;
11427c478bd9Sstevel@tonic-gate 	uint_t			qtd_control;
11437c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp;
11447c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh;
11457c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw;
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	/* Obtain the transfer wrapper from the QTD */
11487c478bd9Sstevel@tonic-gate 	tw = (ehci_trans_wrapper_t *)EHCI_LOOKUP_ID(
11497c478bd9Sstevel@tonic-gate 	    (uint32_t)Get_QTD(qtd->qtd_trans_wrapper));
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	pp = tw->tw_pipe_private;
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	/* Obtain the endpoint and interrupt request */
11547c478bd9Sstevel@tonic-gate 	qh = pp->pp_qh;
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	/*
11577c478bd9Sstevel@tonic-gate 	 * Take this QTD off the transfer wrapper's list since
11587c478bd9Sstevel@tonic-gate 	 * the pipe is FIFO, this must be the first QTD on the
11597c478bd9Sstevel@tonic-gate 	 * list.
11607c478bd9Sstevel@tonic-gate 	 */
11617c478bd9Sstevel@tonic-gate 	ASSERT((ehci_qtd_t *)tw->tw_qtd_head == qtd);
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	tw->tw_qtd_head = (ehci_qtd_t *)
11647c478bd9Sstevel@tonic-gate 	    ehci_qtd_iommu_to_cpu(ehcip, Get_QTD(qtd->qtd_tw_next_qtd));
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	/*
11677c478bd9Sstevel@tonic-gate 	 * If the head becomes NULL, then there are no more
11687c478bd9Sstevel@tonic-gate 	 * active QTD's for this transfer wrapper. Also	set
11697c478bd9Sstevel@tonic-gate 	 * the tail to NULL.
11707c478bd9Sstevel@tonic-gate 	 */
11717c478bd9Sstevel@tonic-gate 	if (tw->tw_qtd_head == NULL) {
11727c478bd9Sstevel@tonic-gate 		tw->tw_qtd_tail = NULL;
11737c478bd9Sstevel@tonic-gate 	}
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	/* Convert current valid QTD as new dummy QTD */
11767c478bd9Sstevel@tonic-gate 	bzero((char *)qtd, sizeof (ehci_qtd_t));
11777c478bd9Sstevel@tonic-gate 	Set_QTD(qtd->qtd_state, EHCI_QTD_DUMMY);
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	/* Rename qtd as new_dummy_qtd */
11807c478bd9Sstevel@tonic-gate 	new_dummy_qtd = qtd;
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	/* Get the current and next dummy QTDs */
11837c478bd9Sstevel@tonic-gate 	curr_dummy_qtd = ehci_qtd_iommu_to_cpu(ehcip,
11847c478bd9Sstevel@tonic-gate 	    Get_QH(qh->qh_dummy_qtd));
11857c478bd9Sstevel@tonic-gate 	next_dummy_qtd = ehci_qtd_iommu_to_cpu(ehcip,
11867c478bd9Sstevel@tonic-gate 	    Get_QTD(curr_dummy_qtd->qtd_next_qtd));
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	/* Update QH's dummy qtd field */
11897c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_dummy_qtd, ehci_qtd_cpu_to_iommu(ehcip, next_dummy_qtd));
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	/* Update next dummy's next qtd pointer */
11927c478bd9Sstevel@tonic-gate 	Set_QTD(next_dummy_qtd->qtd_next_qtd,
11937c478bd9Sstevel@tonic-gate 	    ehci_qtd_cpu_to_iommu(ehcip, new_dummy_qtd));
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	qtd_control = (tw->tw_direction | EHCI_QTD_CTRL_INTR_ON_COMPLETE);
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	/*
11987c478bd9Sstevel@tonic-gate 	 * Fill in the current dummy qtd and
11997c478bd9Sstevel@tonic-gate 	 * add the new dummy to the end.
12007c478bd9Sstevel@tonic-gate 	 */
12017c478bd9Sstevel@tonic-gate 	ehci_polled_fill_in_qtd(ehcip, curr_dummy_qtd, qtd_control,
1202*3304303fSsl 	    0, tw->tw_length, tw);
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	/* Insert this qtd onto the tw */
12057c478bd9Sstevel@tonic-gate 	ehci_polled_insert_qtd_on_tw(ehcip, tw, curr_dummy_qtd);
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	/* Insert this qtd into active interrupt QTD list */
12087c478bd9Sstevel@tonic-gate 	ehci_polled_insert_qtd_into_active_intr_qtd_list(
12097c478bd9Sstevel@tonic-gate 	    ehci_polledp, curr_dummy_qtd);
12107c478bd9Sstevel@tonic-gate }
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate /*
12147c478bd9Sstevel@tonic-gate  * ehci_polled_fill_in_qtd:
12157c478bd9Sstevel@tonic-gate  *
12167c478bd9Sstevel@tonic-gate  * Fill in the fields of a Transfer Descriptor (QTD).
1217*3304303fSsl  * The "Buffer Pointer" fields of a QTD are retrieved from the TW
1218*3304303fSsl  * it is associated with.
12197c478bd9Sstevel@tonic-gate  *
12207c478bd9Sstevel@tonic-gate  * Unlike the it's ehci_fill_in_qtd counterpart, we do not
12217c478bd9Sstevel@tonic-gate  * set the alternative ptr in polled mode.  There is not need
12227c478bd9Sstevel@tonic-gate  * for it in polled mode, because it doesn't need to cleanup
12237c478bd9Sstevel@tonic-gate  * short xfer conditions.
1224*3304303fSsl  *
1225*3304303fSsl  * Note:
1226*3304303fSsl  * qtd_dma_offs - the starting offset into the TW buffer, where the QTD
1227*3304303fSsl  *                should transfer from. It should be 4K aligned. And when
1228*3304303fSsl  *                a TW has more than one QTDs, the QTDs must be filled in
1229*3304303fSsl  *                increasing order.
1230*3304303fSsl  * qtd_length - the total bytes to transfer.
12317c478bd9Sstevel@tonic-gate  */
12327c478bd9Sstevel@tonic-gate static void
12337c478bd9Sstevel@tonic-gate ehci_polled_fill_in_qtd(
12347c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
12357c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd,
12367c478bd9Sstevel@tonic-gate 	uint_t			qtd_ctrl,
1237*3304303fSsl 	size_t			qtd_dma_offs,
12387c478bd9Sstevel@tonic-gate 	size_t			qtd_length,
12397c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw)
12407c478bd9Sstevel@tonic-gate {
1241*3304303fSsl 	uint32_t		buf_addr;
12427c478bd9Sstevel@tonic-gate 	size_t			buf_len = qtd_length;
12437c478bd9Sstevel@tonic-gate 	uint32_t		ctrl = qtd_ctrl;
12447c478bd9Sstevel@tonic-gate 	uint_t			i = 0;
1245*3304303fSsl 	int			rem_len;
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	/* Assert that the qtd to be filled in is a dummy */
12487c478bd9Sstevel@tonic-gate 	ASSERT(Get_QTD(qtd->qtd_state) == EHCI_QTD_DUMMY);
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	/* Change QTD's state Active */
12517c478bd9Sstevel@tonic-gate 	Set_QTD(qtd->qtd_state, EHCI_QTD_ACTIVE);
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	/* Set the total length data tarnsfer */
12547c478bd9Sstevel@tonic-gate 	ctrl |= (((qtd_length << EHCI_QTD_CTRL_BYTES_TO_XFER_SHIFT)
12557c478bd9Sstevel@tonic-gate 	    & EHCI_QTD_CTRL_BYTES_TO_XFER) | EHCI_QTD_CTRL_MAX_ERR_COUNTS);
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	/*
1258*3304303fSsl 	 * QTDs must be filled in increasing DMA offset order.
1259*3304303fSsl 	 * tw_dma_offs is initialized to be 0 at TW creation and
1260*3304303fSsl 	 * is only increased in this function.
1261*3304303fSsl 	 */
1262*3304303fSsl 	ASSERT(buf_len == 0 || qtd_dma_offs >= tw->tw_dma_offs);
1263*3304303fSsl 
1264*3304303fSsl 	/*
1265*3304303fSsl 	 * Save the starting dma buffer offset used and
12667c478bd9Sstevel@tonic-gate 	 * length of data that will be transfered in
12677c478bd9Sstevel@tonic-gate 	 * the current QTD.
12687c478bd9Sstevel@tonic-gate 	 */
1269*3304303fSsl 	Set_QTD(qtd->qtd_xfer_offs, qtd_dma_offs);
12707c478bd9Sstevel@tonic-gate 	Set_QTD(qtd->qtd_xfer_len, buf_len);
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	while (buf_len) {
1273*3304303fSsl 		/*
1274*3304303fSsl 		 * Advance to the next DMA cookie until finding the cookie
1275*3304303fSsl 		 * that qtd_dma_offs falls in.
1276*3304303fSsl 		 * It is very likely this loop will never repeat more than
1277*3304303fSsl 		 * once. It is here just to accommodate the case qtd_dma_offs
1278*3304303fSsl 		 * is increased by multiple cookies during two consecutive
1279*3304303fSsl 		 * calls into this function. In that case, the interim DMA
1280*3304303fSsl 		 * buffer is allowed to be skipped.
1281*3304303fSsl 		 */
1282*3304303fSsl 		while ((tw->tw_dma_offs + tw->tw_cookie.dmac_size) <=
1283*3304303fSsl 		    qtd_dma_offs) {
1284*3304303fSsl 			/*
1285*3304303fSsl 			 * tw_dma_offs always points to the starting offset
1286*3304303fSsl 			 * of a cookie
1287*3304303fSsl 			 */
1288*3304303fSsl 			tw->tw_dma_offs += tw->tw_cookie.dmac_size;
1289*3304303fSsl 			ddi_dma_nextcookie(tw->tw_dmahandle, &tw->tw_cookie);
1290*3304303fSsl 			tw->tw_cookie_idx++;
1291*3304303fSsl 			ASSERT(tw->tw_cookie_idx < tw->tw_ncookies);
1292*3304303fSsl 		}
1293*3304303fSsl 
1294*3304303fSsl 		/*
1295*3304303fSsl 		 * Counting the remained buffer length to be filled in
1296*3304303fSsl 		 * the QTD for current DMA cookie
1297*3304303fSsl 		 */
1298*3304303fSsl 		rem_len = (tw->tw_dma_offs + tw->tw_cookie.dmac_size) -
1299*3304303fSsl 		    qtd_dma_offs;
1300*3304303fSsl 
13017c478bd9Sstevel@tonic-gate 		/* Update the beginning of the buffer */
1302*3304303fSsl 		buf_addr = (qtd_dma_offs - tw->tw_dma_offs) +
1303*3304303fSsl 		    tw->tw_cookie.dmac_address;
1304*3304303fSsl 		ASSERT((buf_addr % EHCI_4K_ALIGN) == 0);
13057c478bd9Sstevel@tonic-gate 		Set_QTD(qtd->qtd_buf[i], buf_addr);
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 		if (buf_len <= EHCI_MAX_QTD_BUF_SIZE) {
1308*3304303fSsl 			ASSERT(buf_len <= rem_len);
13097c478bd9Sstevel@tonic-gate 			break;
13107c478bd9Sstevel@tonic-gate 		} else {
1311*3304303fSsl 			ASSERT(rem_len >= EHCI_MAX_QTD_BUF_SIZE);
13127c478bd9Sstevel@tonic-gate 			buf_len -= EHCI_MAX_QTD_BUF_SIZE;
1313*3304303fSsl 			qtd_dma_offs += EHCI_MAX_QTD_BUF_SIZE;
13147c478bd9Sstevel@tonic-gate 		}
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 		i++;
13177c478bd9Sstevel@tonic-gate 	}
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	/*
13207c478bd9Sstevel@tonic-gate 	 * For control, bulk and interrupt QTD, now
13217c478bd9Sstevel@tonic-gate 	 * enable current QTD by setting active bit.
13227c478bd9Sstevel@tonic-gate 	 */
13237c478bd9Sstevel@tonic-gate 	Set_QTD(qtd->qtd_ctrl, (ctrl | EHCI_QTD_CTRL_ACTIVE_XACT));
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	Set_QTD(qtd->qtd_trans_wrapper, (uint32_t)tw->tw_id);
13267c478bd9Sstevel@tonic-gate }
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate /*
13307c478bd9Sstevel@tonic-gate  * ehci_polled_insert_qtd_on_tw:
13317c478bd9Sstevel@tonic-gate  *
13327c478bd9Sstevel@tonic-gate  * The transfer wrapper keeps a list of all Transfer Descriptors (QTD) that
13337c478bd9Sstevel@tonic-gate  * are allocated for this transfer. Insert a QTD  onto this list. The  list
13347c478bd9Sstevel@tonic-gate  * of QTD's does not include the dummy QTD that is at the end of the list of
13357c478bd9Sstevel@tonic-gate  * QTD's for the endpoint.
13367c478bd9Sstevel@tonic-gate  */
13377c478bd9Sstevel@tonic-gate static void
13387c478bd9Sstevel@tonic-gate ehci_polled_insert_qtd_on_tw(
13397c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
13407c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw,
13417c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd)
13427c478bd9Sstevel@tonic-gate {
13437c478bd9Sstevel@tonic-gate 	/*
13447c478bd9Sstevel@tonic-gate 	 * Set the next pointer to NULL because
13457c478bd9Sstevel@tonic-gate 	 * this is the last QTD on list.
13467c478bd9Sstevel@tonic-gate 	 */
13477c478bd9Sstevel@tonic-gate 	Set_QTD(qtd->qtd_tw_next_qtd, NULL);
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	if (tw->tw_qtd_head == NULL) {
13507c478bd9Sstevel@tonic-gate 		ASSERT(tw->tw_qtd_tail == NULL);
13517c478bd9Sstevel@tonic-gate 		tw->tw_qtd_head = qtd;
13527c478bd9Sstevel@tonic-gate 		tw->tw_qtd_tail = qtd;
13537c478bd9Sstevel@tonic-gate 	} else {
13547c478bd9Sstevel@tonic-gate 		ehci_qtd_t *dummy = (ehci_qtd_t *)tw->tw_qtd_tail;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 		ASSERT(dummy != NULL);
13577c478bd9Sstevel@tonic-gate 		ASSERT(dummy != qtd);
13587c478bd9Sstevel@tonic-gate 		ASSERT(Get_QTD(qtd->qtd_state) != EHCI_QTD_DUMMY);
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 		/* Add the qtd to the end of the list */
13617c478bd9Sstevel@tonic-gate 		Set_QTD(dummy->qtd_tw_next_qtd,
13627c478bd9Sstevel@tonic-gate 		    ehci_qtd_cpu_to_iommu(ehcip, qtd));
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 		tw->tw_qtd_tail = qtd;
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 		ASSERT(Get_QTD(qtd->qtd_tw_next_qtd) == NULL);
13677c478bd9Sstevel@tonic-gate 	}
13687c478bd9Sstevel@tonic-gate }
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate /*
13727c478bd9Sstevel@tonic-gate  * ehci_polled_create_done_qtd_list:
13737c478bd9Sstevel@tonic-gate  *
13747c478bd9Sstevel@tonic-gate  * Create done qtd list from active qtd list.
13757c478bd9Sstevel@tonic-gate  */
13767c478bd9Sstevel@tonic-gate static ehci_qtd_t *
13777c478bd9Sstevel@tonic-gate ehci_polled_create_done_qtd_list(
13787c478bd9Sstevel@tonic-gate 	ehci_polled_t		*ehci_polledp)
13797c478bd9Sstevel@tonic-gate {
13807c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
13817c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*curr_qtd = NULL, *next_qtd = NULL;
13827c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*done_qtd_list = NULL, *last_done_qtd = NULL;
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
13857c478bd9Sstevel@tonic-gate 	    "ehci_polled_create_done_qtd_list:");
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	curr_qtd = ehci_polledp->ehci_polled_active_intr_qtd_list;
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	while (curr_qtd) {
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 		/* Get next qtd from the active qtd list */
13927c478bd9Sstevel@tonic-gate 		next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
13937c478bd9Sstevel@tonic-gate 		    Get_QTD(curr_qtd->qtd_active_qtd_next));
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 		/* Check this QTD has been processed by Host Controller */
13967c478bd9Sstevel@tonic-gate 		if (!(Get_QTD(curr_qtd->qtd_ctrl) &
13977c478bd9Sstevel@tonic-gate 		    EHCI_QTD_CTRL_ACTIVE_XACT)) {
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 			/* Remove this QTD from active QTD list */
14007c478bd9Sstevel@tonic-gate 			ehci_polled_remove_qtd_from_active_intr_qtd_list(
14017c478bd9Sstevel@tonic-gate 			    ehci_polledp, curr_qtd);
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 			Set_QTD(curr_qtd->qtd_active_qtd_next, NULL);
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 			if (done_qtd_list) {
14067c478bd9Sstevel@tonic-gate 				Set_QTD(last_done_qtd->qtd_active_qtd_next,
14077c478bd9Sstevel@tonic-gate 				    ehci_qtd_cpu_to_iommu(ehcip, curr_qtd));
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 				last_done_qtd = curr_qtd;
14107c478bd9Sstevel@tonic-gate 			} else {
14117c478bd9Sstevel@tonic-gate 				done_qtd_list = curr_qtd;
14127c478bd9Sstevel@tonic-gate 				last_done_qtd = curr_qtd;
14137c478bd9Sstevel@tonic-gate 			}
14147c478bd9Sstevel@tonic-gate 		}
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 		curr_qtd = next_qtd;
14177c478bd9Sstevel@tonic-gate 	}
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	return (done_qtd_list);
14207c478bd9Sstevel@tonic-gate }
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate /*
14247c478bd9Sstevel@tonic-gate  * ehci_polled_insert_qtd_into_active_intr_qtd_list:
14257c478bd9Sstevel@tonic-gate  *
14267c478bd9Sstevel@tonic-gate  * Insert current QTD into active interrupt QTD list.
14277c478bd9Sstevel@tonic-gate  */
14287c478bd9Sstevel@tonic-gate static void
14297c478bd9Sstevel@tonic-gate ehci_polled_insert_qtd_into_active_intr_qtd_list(
14307c478bd9Sstevel@tonic-gate 	ehci_polled_t		*ehci_polledp,
14317c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd)
14327c478bd9Sstevel@tonic-gate {
14337c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
14347c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*curr_qtd, *next_qtd;
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	curr_qtd = ehci_polledp->ehci_polled_active_intr_qtd_list;
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	/* Insert this qtd into active intr qtd list */
14397c478bd9Sstevel@tonic-gate 	if (curr_qtd) {
14407c478bd9Sstevel@tonic-gate 		next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
14417c478bd9Sstevel@tonic-gate 		    Get_QTD(curr_qtd->qtd_active_qtd_next));
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 		while (next_qtd) {
14447c478bd9Sstevel@tonic-gate 			curr_qtd = next_qtd;
14457c478bd9Sstevel@tonic-gate 			next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
14467c478bd9Sstevel@tonic-gate 			    Get_QTD(curr_qtd->qtd_active_qtd_next));
14477c478bd9Sstevel@tonic-gate 		}
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 		Set_QTD(qtd->qtd_active_qtd_prev,
14507c478bd9Sstevel@tonic-gate 		    ehci_qtd_cpu_to_iommu(ehcip, curr_qtd));
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 		Set_QTD(curr_qtd->qtd_active_qtd_next,
14537c478bd9Sstevel@tonic-gate 		    ehci_qtd_cpu_to_iommu(ehcip, qtd));
14547c478bd9Sstevel@tonic-gate 	} else {
14557c478bd9Sstevel@tonic-gate 		ehci_polledp->ehci_polled_active_intr_qtd_list = qtd;
14567c478bd9Sstevel@tonic-gate 		Set_QTD(qtd->qtd_active_qtd_next, NULL);
14577c478bd9Sstevel@tonic-gate 		Set_QTD(qtd->qtd_active_qtd_prev, NULL);
14587c478bd9Sstevel@tonic-gate 	}
14597c478bd9Sstevel@tonic-gate }
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate /*
14637c478bd9Sstevel@tonic-gate  * ehci_polled_remove_qtd_from_active_intr_qtd_list:
14647c478bd9Sstevel@tonic-gate  *
14657c478bd9Sstevel@tonic-gate  * Remove current QTD from the active QTD list.
14667c478bd9Sstevel@tonic-gate  */
14677c478bd9Sstevel@tonic-gate void
14687c478bd9Sstevel@tonic-gate ehci_polled_remove_qtd_from_active_intr_qtd_list(
14697c478bd9Sstevel@tonic-gate 	ehci_polled_t		*ehci_polledp,
14707c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd)
14717c478bd9Sstevel@tonic-gate {
14727c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
14737c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*curr_qtd, *prev_qtd, *next_qtd;
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 	ASSERT(qtd != NULL);
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 	curr_qtd = ehci_polledp->ehci_polled_active_intr_qtd_list;
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	while ((curr_qtd) && (curr_qtd != qtd)) {
14807c478bd9Sstevel@tonic-gate 		curr_qtd = ehci_qtd_iommu_to_cpu(ehcip,
14817c478bd9Sstevel@tonic-gate 		    Get_QTD(curr_qtd->qtd_active_qtd_next));
14827c478bd9Sstevel@tonic-gate 	}
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	if ((curr_qtd) && (curr_qtd == qtd)) {
14857c478bd9Sstevel@tonic-gate 		prev_qtd = ehci_qtd_iommu_to_cpu(ehcip,
14867c478bd9Sstevel@tonic-gate 		    Get_QTD(curr_qtd->qtd_active_qtd_prev));
14877c478bd9Sstevel@tonic-gate 		next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
14887c478bd9Sstevel@tonic-gate 		    Get_QTD(curr_qtd->qtd_active_qtd_next));
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 		if (prev_qtd) {
14917c478bd9Sstevel@tonic-gate 			Set_QTD(prev_qtd->qtd_active_qtd_next,
14927c478bd9Sstevel@tonic-gate 			    Get_QTD(curr_qtd->qtd_active_qtd_next));
14937c478bd9Sstevel@tonic-gate 		} else {
14947c478bd9Sstevel@tonic-gate 			ehci_polledp->
14957c478bd9Sstevel@tonic-gate 			    ehci_polled_active_intr_qtd_list = next_qtd;
14967c478bd9Sstevel@tonic-gate 		}
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 		if (next_qtd) {
14997c478bd9Sstevel@tonic-gate 			Set_QTD(next_qtd->qtd_active_qtd_prev,
15007c478bd9Sstevel@tonic-gate 			    Get_QTD(curr_qtd->qtd_active_qtd_prev));
15017c478bd9Sstevel@tonic-gate 		}
15027c478bd9Sstevel@tonic-gate 	}
15037c478bd9Sstevel@tonic-gate }
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate /*
15077c478bd9Sstevel@tonic-gate  * ehci_polled_traverse_qtds:
15087c478bd9Sstevel@tonic-gate  *
15097c478bd9Sstevel@tonic-gate  * Traverse the list of QTDs for given pipe using transfer wrapper.  Since
15107c478bd9Sstevel@tonic-gate  * the endpoint is marked as Halted, the Host Controller (HC) is no longer
15117c478bd9Sstevel@tonic-gate  * accessing these QTDs. Remove all the QTDs that are attached to endpoint.
15127c478bd9Sstevel@tonic-gate  */
15137c478bd9Sstevel@tonic-gate static void
15147c478bd9Sstevel@tonic-gate ehci_polled_traverse_qtds(
15157c478bd9Sstevel@tonic-gate 	ehci_polled_t		*ehci_polledp,
15167c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph)
15177c478bd9Sstevel@tonic-gate {
15187c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip = ehci_polledp->ehci_polled_ehcip;
15197c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
15207c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*next_tw;
15217c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd;
15227c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*next_qtd;
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	/* Process the transfer wrappers for this pipe */
15257c478bd9Sstevel@tonic-gate 	next_tw = pp->pp_tw_head;
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	while (next_tw) {
15287c478bd9Sstevel@tonic-gate 		qtd = (ehci_qtd_t *)next_tw->tw_qtd_head;
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 		/* Walk through each QTD for this transfer wrapper */
15317c478bd9Sstevel@tonic-gate 		while (qtd) {
15327c478bd9Sstevel@tonic-gate 			/* Remove this QTD from active QTD list */
15337c478bd9Sstevel@tonic-gate 			ehci_polled_remove_qtd_from_active_intr_qtd_list(
15347c478bd9Sstevel@tonic-gate 			    ehci_polledp, qtd);
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 			next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
15377c478bd9Sstevel@tonic-gate 			    Get_QTD(qtd->qtd_tw_next_qtd));
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 			/* Deallocate this QTD */
15407c478bd9Sstevel@tonic-gate 			ehci_deallocate_qtd(ehcip, qtd);
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 			qtd = next_qtd;
15437c478bd9Sstevel@tonic-gate 		}
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 		next_tw = next_tw->tw_next;
15467c478bd9Sstevel@tonic-gate 	}
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	/* Clear current qtd pointer */
15497c478bd9Sstevel@tonic-gate 	Set_QH(pp->pp_qh->qh_curr_qtd, (uint32_t)0x00000000);
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 	/* Update the next qtd pointer in the QH */
15527c478bd9Sstevel@tonic-gate 	Set_QH(pp->pp_qh->qh_next_qtd, Get_QH(pp->pp_qh->qh_dummy_qtd));
15537c478bd9Sstevel@tonic-gate }
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate /*
15577c478bd9Sstevel@tonic-gate  * ehci_polled_finish_interrupt:
15587c478bd9Sstevel@tonic-gate  */
15597c478bd9Sstevel@tonic-gate static void
15607c478bd9Sstevel@tonic-gate ehci_polled_finish_interrupt(
15617c478bd9Sstevel@tonic-gate 	ehci_state_t	*ehcip,
15627c478bd9Sstevel@tonic-gate 	uint_t		intr)
15637c478bd9Sstevel@tonic-gate {
15647c478bd9Sstevel@tonic-gate 	/* Acknowledge the interrupt */
15657c478bd9Sstevel@tonic-gate 	Set_OpReg(ehci_status, intr);
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	/*
15687c478bd9Sstevel@tonic-gate 	 * Read interrupt status register to make sure that any PIO
15697c478bd9Sstevel@tonic-gate 	 * store to clear the ISR has made it on the PCI bus before
15707c478bd9Sstevel@tonic-gate 	 * returning from its interrupt handler.
15717c478bd9Sstevel@tonic-gate 	 */
15727c478bd9Sstevel@tonic-gate 	(void) Get_OpReg(ehci_status);
15737c478bd9Sstevel@tonic-gate }
1574