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
53304303fSsl  * Common Development and Distribution License (the "License").
63304303fSsl  * 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 /*
223304303fSsl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * EHCI Host Controller Driver (EHCI)
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * The EHCI driver is a software driver which interfaces to the Universal
327c478bd9Sstevel@tonic-gate  * Serial Bus layer (USBA) and the Host Controller (HC). The interface to
337c478bd9Sstevel@tonic-gate  * the Host Controller is defined by the EHCI Host Controller Interface.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * This module contains the main EHCI driver code which handles all USB
367c478bd9Sstevel@tonic-gate  * transfers, bandwidth allocations and other general functionalities.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <sys/usb/hcd/ehci/ehcid.h>
407c478bd9Sstevel@tonic-gate #include <sys/usb/hcd/ehci/ehci_intr.h>
417c478bd9Sstevel@tonic-gate #include <sys/usb/hcd/ehci/ehci_util.h>
427c478bd9Sstevel@tonic-gate #include <sys/usb/hcd/ehci/ehci_isoch.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /* Adjustable variables for the size of the pools */
457c478bd9Sstevel@tonic-gate extern int ehci_qh_pool_size;
467c478bd9Sstevel@tonic-gate extern int ehci_qtd_pool_size;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /* Endpoint Descriptor (QH) related functions */
507c478bd9Sstevel@tonic-gate ehci_qh_t	*ehci_alloc_qh(
517c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
527c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph,
537c478bd9Sstevel@tonic-gate 				uint_t			flag);
547c478bd9Sstevel@tonic-gate static void	ehci_unpack_endpoint(
557c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
567c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph,
577c478bd9Sstevel@tonic-gate 				ehci_qh_t		*qh);
587c478bd9Sstevel@tonic-gate void		ehci_insert_qh(
597c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
607c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph);
617c478bd9Sstevel@tonic-gate static void	ehci_insert_async_qh(
627c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
637c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp);
647c478bd9Sstevel@tonic-gate static void	ehci_insert_intr_qh(
657c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
667c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp);
677c478bd9Sstevel@tonic-gate static void	ehci_modify_qh_status_bit(
687c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
697c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
707c478bd9Sstevel@tonic-gate 				halt_bit_t		action);
717c478bd9Sstevel@tonic-gate static void	ehci_halt_hs_qh(
727c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
737c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
747c478bd9Sstevel@tonic-gate 				ehci_qh_t		*qh);
757c478bd9Sstevel@tonic-gate static void	ehci_halt_fls_ctrl_and_bulk_qh(
767c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
777c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
787c478bd9Sstevel@tonic-gate 				ehci_qh_t		*qh);
797c478bd9Sstevel@tonic-gate static void	ehci_clear_tt_buffer(
807c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
817c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph,
827c478bd9Sstevel@tonic-gate 				ehci_qh_t		*qh);
837c478bd9Sstevel@tonic-gate static void	ehci_halt_fls_intr_qh(
847c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
857c478bd9Sstevel@tonic-gate 				ehci_qh_t		*qh);
867c478bd9Sstevel@tonic-gate void		ehci_remove_qh(
877c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
887c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
897c478bd9Sstevel@tonic-gate 				boolean_t		reclaim);
907c478bd9Sstevel@tonic-gate static void	ehci_remove_async_qh(
917c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
927c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
937c478bd9Sstevel@tonic-gate 				boolean_t		reclaim);
947c478bd9Sstevel@tonic-gate static void	ehci_remove_intr_qh(
957c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
967c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
977c478bd9Sstevel@tonic-gate 				boolean_t		reclaim);
987c478bd9Sstevel@tonic-gate static void	ehci_insert_qh_on_reclaim_list(
997c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1007c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp);
1017c478bd9Sstevel@tonic-gate void		ehci_deallocate_qh(
1027c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1037c478bd9Sstevel@tonic-gate 				ehci_qh_t		*old_qh);
1047c478bd9Sstevel@tonic-gate uint32_t	ehci_qh_cpu_to_iommu(
1057c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1067c478bd9Sstevel@tonic-gate 				ehci_qh_t		*addr);
1077c478bd9Sstevel@tonic-gate ehci_qh_t	*ehci_qh_iommu_to_cpu(
1087c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1097c478bd9Sstevel@tonic-gate 				uintptr_t		addr);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /* Transfer Descriptor (QTD) related functions */
1127c478bd9Sstevel@tonic-gate static int	ehci_initialize_dummy(
1137c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1147c478bd9Sstevel@tonic-gate 				ehci_qh_t		*qh);
1157c478bd9Sstevel@tonic-gate ehci_trans_wrapper_t *ehci_allocate_ctrl_resources(
1167c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1177c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
1187c478bd9Sstevel@tonic-gate 				usb_ctrl_req_t		*ctrl_reqp,
1197c478bd9Sstevel@tonic-gate 				usb_flags_t		usb_flags);
1207c478bd9Sstevel@tonic-gate void		ehci_insert_ctrl_req(
1217c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1227c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph,
1237c478bd9Sstevel@tonic-gate 				usb_ctrl_req_t		*ctrl_reqp,
1247c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw,
1257c478bd9Sstevel@tonic-gate 				usb_flags_t		usb_flags);
1267c478bd9Sstevel@tonic-gate ehci_trans_wrapper_t *ehci_allocate_bulk_resources(
1277c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1287c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
1297c478bd9Sstevel@tonic-gate 				usb_bulk_req_t		*bulk_reqp,
1307c478bd9Sstevel@tonic-gate 				usb_flags_t		usb_flags);
1317c478bd9Sstevel@tonic-gate void		ehci_insert_bulk_req(
1327c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1337c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph,
1347c478bd9Sstevel@tonic-gate 				usb_bulk_req_t		*bulk_reqp,
1357c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw,
1367c478bd9Sstevel@tonic-gate 				usb_flags_t		flags);
1377c478bd9Sstevel@tonic-gate int		ehci_start_periodic_pipe_polling(
1387c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1397c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph,
1407c478bd9Sstevel@tonic-gate 				usb_opaque_t		periodic_in_reqp,
1417c478bd9Sstevel@tonic-gate 				usb_flags_t		flags);
1427c478bd9Sstevel@tonic-gate static int	ehci_start_pipe_polling(
1437c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1447c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph,
1457c478bd9Sstevel@tonic-gate 				usb_flags_t		flags);
1467c478bd9Sstevel@tonic-gate static int	ehci_start_intr_polling(
1477c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1487c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph,
1497c478bd9Sstevel@tonic-gate 				usb_flags_t		flags);
1507c478bd9Sstevel@tonic-gate static void	ehci_set_periodic_pipe_polling(
1517c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1527c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph);
1537c478bd9Sstevel@tonic-gate ehci_trans_wrapper_t *ehci_allocate_intr_resources(
1547c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1557c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph,
1567c478bd9Sstevel@tonic-gate 				usb_intr_req_t		*intr_reqp,
1577c478bd9Sstevel@tonic-gate 				usb_flags_t		usb_flags);
1587c478bd9Sstevel@tonic-gate void		ehci_insert_intr_req(
1597c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1607c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
1617c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw,
1627c478bd9Sstevel@tonic-gate 				usb_flags_t		flags);
1637c478bd9Sstevel@tonic-gate int		ehci_stop_periodic_pipe_polling(
1647c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1657c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph,
1667c478bd9Sstevel@tonic-gate 				usb_flags_t		flags);
1677c478bd9Sstevel@tonic-gate int		ehci_insert_qtd(
1687c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1697c478bd9Sstevel@tonic-gate 				uint32_t		qtd_ctrl,
1703304303fSsl 				size_t			qtd_dma_offs,
1717c478bd9Sstevel@tonic-gate 				size_t			qtd_length,
1727c478bd9Sstevel@tonic-gate 				uint32_t		qtd_ctrl_phase,
1737c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
1747c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw);
1757c478bd9Sstevel@tonic-gate static ehci_qtd_t *ehci_allocate_qtd_from_pool(
1767c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip);
1777c478bd9Sstevel@tonic-gate static void	ehci_fill_in_qtd(
1787c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1797c478bd9Sstevel@tonic-gate 				ehci_qtd_t		*qtd,
1807c478bd9Sstevel@tonic-gate 				uint32_t		qtd_ctrl,
1813304303fSsl 				size_t			qtd_dma_offs,
1827c478bd9Sstevel@tonic-gate 				size_t			qtd_length,
1837c478bd9Sstevel@tonic-gate 				uint32_t		qtd_ctrl_phase,
1847c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
1857c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw);
1867c478bd9Sstevel@tonic-gate static void	ehci_insert_qtd_on_tw(
1877c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1887c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw,
1897c478bd9Sstevel@tonic-gate 				ehci_qtd_t		*qtd);
1907c478bd9Sstevel@tonic-gate static void	ehci_insert_qtd_into_active_qtd_list(
1917c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1927c478bd9Sstevel@tonic-gate 				ehci_qtd_t		*curr_qtd);
1937c478bd9Sstevel@tonic-gate void		ehci_remove_qtd_from_active_qtd_list(
1947c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1957c478bd9Sstevel@tonic-gate 				ehci_qtd_t		*curr_qtd);
1967c478bd9Sstevel@tonic-gate static void	ehci_traverse_qtds(
1977c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
1987c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph);
1997c478bd9Sstevel@tonic-gate void		ehci_deallocate_qtd(
2007c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2017c478bd9Sstevel@tonic-gate 				ehci_qtd_t		*old_qtd);
2027c478bd9Sstevel@tonic-gate uint32_t	ehci_qtd_cpu_to_iommu(
2037c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2047c478bd9Sstevel@tonic-gate 				ehci_qtd_t		*addr);
2057c478bd9Sstevel@tonic-gate ehci_qtd_t	*ehci_qtd_iommu_to_cpu(
2067c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2077c478bd9Sstevel@tonic-gate 				uintptr_t		addr);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /* Transfer Wrapper (TW) functions */
2107c478bd9Sstevel@tonic-gate static ehci_trans_wrapper_t  *ehci_create_transfer_wrapper(
2117c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2127c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
2137c478bd9Sstevel@tonic-gate 				size_t			length,
2147c478bd9Sstevel@tonic-gate 				uint_t			usb_flags);
2157c478bd9Sstevel@tonic-gate int		ehci_allocate_tds_for_tw(
2167c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2177c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
2187c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw,
2197c478bd9Sstevel@tonic-gate 				size_t			qtd_count);
2207c478bd9Sstevel@tonic-gate static ehci_trans_wrapper_t  *ehci_allocate_tw_resources(
2217c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2227c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
2237c478bd9Sstevel@tonic-gate 				size_t			length,
2247c478bd9Sstevel@tonic-gate 				usb_flags_t		usb_flags,
2257c478bd9Sstevel@tonic-gate 				size_t			td_count);
2267c478bd9Sstevel@tonic-gate static void	ehci_free_tw_td_resources(
2277c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2287c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw);
2297c478bd9Sstevel@tonic-gate static void	ehci_start_xfer_timer(
2307c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2317c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
2327c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw);
2337c478bd9Sstevel@tonic-gate void		ehci_stop_xfer_timer(
2347c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2357c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw,
2367c478bd9Sstevel@tonic-gate 				uint_t			flag);
2377c478bd9Sstevel@tonic-gate static void	ehci_xfer_timeout_handler(void		*arg);
2387c478bd9Sstevel@tonic-gate static void	ehci_remove_tw_from_timeout_list(
2397c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2407c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw);
2417c478bd9Sstevel@tonic-gate static void	ehci_start_timer(ehci_state_t		*ehcip,
2427c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp);
2437c478bd9Sstevel@tonic-gate void		ehci_deallocate_tw(
2447c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2457c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
2467c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw);
2477c478bd9Sstevel@tonic-gate void		ehci_free_dma_resources(
2487c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2497c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph);
2507c478bd9Sstevel@tonic-gate static void	ehci_free_tw(
2517c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2527c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
2537c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate /* Miscellaneous functions */
2567c478bd9Sstevel@tonic-gate int		ehci_allocate_intr_in_resource(
2577c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2587c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
2597c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw,
2607c478bd9Sstevel@tonic-gate 				usb_flags_t		flags);
2617c478bd9Sstevel@tonic-gate void		ehci_pipe_cleanup(
2627c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2637c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph);
2647c478bd9Sstevel@tonic-gate static void	ehci_wait_for_transfers_completion(
2657c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2667c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp);
2677c478bd9Sstevel@tonic-gate void		ehci_check_for_transfers_completion(
2687c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2697c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp);
2707c478bd9Sstevel@tonic-gate static void	ehci_save_data_toggle(
2717c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2727c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph);
2737c478bd9Sstevel@tonic-gate void		ehci_restore_data_toggle(
2747c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2757c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph);
2767c478bd9Sstevel@tonic-gate void		ehci_handle_outstanding_requests(
2777c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2787c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp);
2797c478bd9Sstevel@tonic-gate void		ehci_deallocate_intr_in_resource(
2807c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2817c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
2827c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw);
2837c478bd9Sstevel@tonic-gate void		ehci_do_client_periodic_in_req_callback(
2847c478bd9Sstevel@tonic-gate 				ehci_state_t		*ehcip,
2857c478bd9Sstevel@tonic-gate 				ehci_pipe_private_t	*pp,
2867c478bd9Sstevel@tonic-gate 				usb_cr_t		completion_reason);
2877c478bd9Sstevel@tonic-gate void		ehci_hcdi_callback(
2887c478bd9Sstevel@tonic-gate 				usba_pipe_handle_data_t	*ph,
2897c478bd9Sstevel@tonic-gate 				ehci_trans_wrapper_t	*tw,
2907c478bd9Sstevel@tonic-gate 				usb_cr_t		completion_reason);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate /*
2947c478bd9Sstevel@tonic-gate  * Endpoint Descriptor (QH) manipulations functions
2957c478bd9Sstevel@tonic-gate  */
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate  * ehci_alloc_qh:
2997c478bd9Sstevel@tonic-gate  *
3007c478bd9Sstevel@tonic-gate  * Allocate an endpoint descriptor (QH)
3017c478bd9Sstevel@tonic-gate  *
3027c478bd9Sstevel@tonic-gate  * NOTE: This function is also called from POLLED MODE.
3037c478bd9Sstevel@tonic-gate  */
3047c478bd9Sstevel@tonic-gate ehci_qh_t *
3057c478bd9Sstevel@tonic-gate ehci_alloc_qh(
3067c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
3077c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
3087c478bd9Sstevel@tonic-gate 	uint_t			flag)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	int			i, state;
3117c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
3147c478bd9Sstevel@tonic-gate 	    "ehci_alloc_qh: ph = 0x%p flag = 0x%x", (void *)ph, flag);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/*
3197c478bd9Sstevel@tonic-gate 	 * If this is for a ISOC endpoint return null.
3207c478bd9Sstevel@tonic-gate 	 * Isochronous uses ITD put directly onto the PFL.
3217c478bd9Sstevel@tonic-gate 	 */
3227c478bd9Sstevel@tonic-gate 	if (ph) {
3237c478bd9Sstevel@tonic-gate 		if (EHCI_ISOC_ENDPOINT((&ph->p_ep))) {
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 			return (NULL);
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	/*
3307c478bd9Sstevel@tonic-gate 	 * The first 63 endpoints in the Endpoint Descriptor (QH)
3317c478bd9Sstevel@tonic-gate 	 * buffer pool are reserved for building interrupt lattice
3327c478bd9Sstevel@tonic-gate 	 * tree. Search for a blank endpoint descriptor in the QH
3337c478bd9Sstevel@tonic-gate 	 * buffer pool.
3347c478bd9Sstevel@tonic-gate 	 */
3357c478bd9Sstevel@tonic-gate 	for (i = EHCI_NUM_STATIC_NODES; i < ehci_qh_pool_size; i ++) {
3367c478bd9Sstevel@tonic-gate 		state = Get_QH(ehcip->ehci_qh_pool_addr[i].qh_state);
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 		if (state == EHCI_QH_FREE) {
3397c478bd9Sstevel@tonic-gate 			break;
3407c478bd9Sstevel@tonic-gate 		}
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
3447c478bd9Sstevel@tonic-gate 	    "ehci_alloc_qh: Allocated %d", i);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (i == ehci_qh_pool_size) {
3477c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALLOC,  ehcip->ehci_log_hdl,
3487c478bd9Sstevel@tonic-gate 		    "ehci_alloc_qh: QH exhausted");
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 		return (NULL);
3517c478bd9Sstevel@tonic-gate 	} else {
3527c478bd9Sstevel@tonic-gate 		qh = &ehcip->ehci_qh_pool_addr[i];
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
3557c478bd9Sstevel@tonic-gate 		    "ehci_alloc_qh: Allocated address 0x%p", (void *)qh);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 		/* Check polled mode flag */
3587c478bd9Sstevel@tonic-gate 		if (flag == EHCI_POLLED_MODE_FLAG) {
3597c478bd9Sstevel@tonic-gate 			Set_QH(qh->qh_link_ptr, EHCI_QH_LINK_PTR_VALID);
3607c478bd9Sstevel@tonic-gate 			Set_QH(qh->qh_ctrl, EHCI_QH_CTRL_ED_INACTIVATE);
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 		/* Unpack the endpoint descriptor into a control field */
3647c478bd9Sstevel@tonic-gate 		if (ph) {
3657c478bd9Sstevel@tonic-gate 			if ((ehci_initialize_dummy(ehcip,
3667c478bd9Sstevel@tonic-gate 			    qh)) == USB_NO_RESOURCES) {
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 				bzero((void *)qh, sizeof (ehci_qh_t));
3697c478bd9Sstevel@tonic-gate 				Set_QH(qh->qh_state, EHCI_QH_FREE);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 				return (NULL);
3727c478bd9Sstevel@tonic-gate 			}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 			ehci_unpack_endpoint(ehcip, ph, qh);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 			Set_QH(qh->qh_curr_qtd, NULL);
3777c478bd9Sstevel@tonic-gate 			Set_QH(qh->qh_alt_next_qtd,
3787c478bd9Sstevel@tonic-gate 			    EHCI_QH_ALT_NEXT_QTD_PTR_VALID);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 			/* Change QH's state Active */
3817c478bd9Sstevel@tonic-gate 			Set_QH(qh->qh_state, EHCI_QH_ACTIVE);
3827c478bd9Sstevel@tonic-gate 		} else {
3837c478bd9Sstevel@tonic-gate 			Set_QH(qh->qh_status, EHCI_QH_STS_HALTED);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 			/* Change QH's state Static */
3867c478bd9Sstevel@tonic-gate 			Set_QH(qh->qh_state, EHCI_QH_STATIC);
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 		ehci_print_qh(ehcip, qh);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		return (qh);
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate  * ehci_unpack_endpoint:
3987c478bd9Sstevel@tonic-gate  *
3997c478bd9Sstevel@tonic-gate  * Unpack the information in the pipe handle and create the first byte
4007c478bd9Sstevel@tonic-gate  * of the Host Controller's (HC) Endpoint Descriptor (QH).
4017c478bd9Sstevel@tonic-gate  */
4027c478bd9Sstevel@tonic-gate static void
4037c478bd9Sstevel@tonic-gate ehci_unpack_endpoint(
4047c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
4057c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
4067c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*endpoint = &ph->p_ep;
4097c478bd9Sstevel@tonic-gate 	uint_t			maxpacketsize, addr, xactions;
4107c478bd9Sstevel@tonic-gate 	uint_t			ctrl = 0, status = 0, split_ctrl = 0;
4117c478bd9Sstevel@tonic-gate 	usb_port_status_t	usb_port_status;
4127c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device = ph->p_usba_device;
4137c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4167c478bd9Sstevel@tonic-gate 	    "ehci_unpack_endpoint:");
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
4197c478bd9Sstevel@tonic-gate 	ctrl = usba_device->usb_addr;
4207c478bd9Sstevel@tonic-gate 	usb_port_status = usba_device->usb_port_status;
4217c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	addr = endpoint->bEndpointAddress;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/* Assign the endpoint's address */
4267c478bd9Sstevel@tonic-gate 	ctrl |= ((addr & USB_EP_NUM_MASK) << EHCI_QH_CTRL_ED_NUMBER_SHIFT);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/* Assign the speed */
4297c478bd9Sstevel@tonic-gate 	switch (usb_port_status) {
4307c478bd9Sstevel@tonic-gate 	case USBA_LOW_SPEED_DEV:
4317c478bd9Sstevel@tonic-gate 		ctrl |= EHCI_QH_CTRL_ED_LOW_SPEED;
4327c478bd9Sstevel@tonic-gate 		break;
4337c478bd9Sstevel@tonic-gate 	case USBA_FULL_SPEED_DEV:
4347c478bd9Sstevel@tonic-gate 		ctrl |= EHCI_QH_CTRL_ED_FULL_SPEED;
4357c478bd9Sstevel@tonic-gate 		break;
4367c478bd9Sstevel@tonic-gate 	case USBA_HIGH_SPEED_DEV:
4377c478bd9Sstevel@tonic-gate 		ctrl |= EHCI_QH_CTRL_ED_HIGH_SPEED;
4387c478bd9Sstevel@tonic-gate 		break;
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	switch (endpoint->bmAttributes & USB_EP_ATTR_MASK) {
4427c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_CONTROL:
4437c478bd9Sstevel@tonic-gate 		/* Assign data toggle information */
4447c478bd9Sstevel@tonic-gate 		ctrl |= EHCI_QH_CTRL_DATA_TOGGLE;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 		if (usb_port_status != USBA_HIGH_SPEED_DEV) {
4477c478bd9Sstevel@tonic-gate 			ctrl |= EHCI_QH_CTRL_CONTROL_ED_FLAG;
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
4507c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_BULK:
4517c478bd9Sstevel@tonic-gate 		/* Maximum nak counter */
4527c478bd9Sstevel@tonic-gate 		ctrl |= EHCI_QH_CTRL_MAX_NC;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 		if (usb_port_status == USBA_HIGH_SPEED_DEV) {
4557c478bd9Sstevel@tonic-gate 			/*
4567c478bd9Sstevel@tonic-gate 			 * Perform ping before executing control
4577c478bd9Sstevel@tonic-gate 			 * and bulk transactions.
4587c478bd9Sstevel@tonic-gate 			 */
4597c478bd9Sstevel@tonic-gate 			status = EHCI_QH_STS_DO_PING;
4607c478bd9Sstevel@tonic-gate 		}
4617c478bd9Sstevel@tonic-gate 		break;
4627c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_INTR:
4637c478bd9Sstevel@tonic-gate 		/* Set start split mask */
4647c478bd9Sstevel@tonic-gate 		split_ctrl = (pp->pp_smask & EHCI_QH_SPLIT_CTRL_INTR_MASK);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		/*
4677c478bd9Sstevel@tonic-gate 		 * Set complete split mask for low/full speed
4687c478bd9Sstevel@tonic-gate 		 * usb devices.
4697c478bd9Sstevel@tonic-gate 		 */
4707c478bd9Sstevel@tonic-gate 		if (usb_port_status != USBA_HIGH_SPEED_DEV) {
4717c478bd9Sstevel@tonic-gate 			split_ctrl |= ((pp->pp_cmask <<
4727c478bd9Sstevel@tonic-gate 			    EHCI_QH_SPLIT_CTRL_COMP_SHIFT) &
4737c478bd9Sstevel@tonic-gate 			    EHCI_QH_SPLIT_CTRL_COMP_MASK);
4747c478bd9Sstevel@tonic-gate 		}
4757c478bd9Sstevel@tonic-gate 		break;
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	/* Get the max transactions per microframe */
4797c478bd9Sstevel@tonic-gate 	xactions = (endpoint->wMaxPacketSize &
4807c478bd9Sstevel@tonic-gate 	    USB_EP_MAX_XACTS_MASK) >>  USB_EP_MAX_XACTS_SHIFT;
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	switch (xactions) {
4837c478bd9Sstevel@tonic-gate 	case 0:
4847c478bd9Sstevel@tonic-gate 		split_ctrl |= EHCI_QH_SPLIT_CTRL_1_XACTS;
4857c478bd9Sstevel@tonic-gate 		break;
4867c478bd9Sstevel@tonic-gate 	case 1:
4877c478bd9Sstevel@tonic-gate 		split_ctrl |= EHCI_QH_SPLIT_CTRL_2_XACTS;
4887c478bd9Sstevel@tonic-gate 		break;
4897c478bd9Sstevel@tonic-gate 	case 2:
4907c478bd9Sstevel@tonic-gate 		split_ctrl |= EHCI_QH_SPLIT_CTRL_3_XACTS;
4917c478bd9Sstevel@tonic-gate 		break;
4927c478bd9Sstevel@tonic-gate 	default:
4937c478bd9Sstevel@tonic-gate 		split_ctrl |= EHCI_QH_SPLIT_CTRL_1_XACTS;
4947c478bd9Sstevel@tonic-gate 		break;
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	/*
4987c478bd9Sstevel@tonic-gate 	 * For low/full speed devices, program high speed hub
4997c478bd9Sstevel@tonic-gate 	 * address and port number.
5007c478bd9Sstevel@tonic-gate 	 */
5017c478bd9Sstevel@tonic-gate 	if (usb_port_status != USBA_HIGH_SPEED_DEV) {
5027c478bd9Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
5037c478bd9Sstevel@tonic-gate 		split_ctrl |= ((usba_device->usb_hs_hub_addr
5047c478bd9Sstevel@tonic-gate 		    << EHCI_QH_SPLIT_CTRL_HUB_ADDR_SHIFT) &
5057c478bd9Sstevel@tonic-gate 		    EHCI_QH_SPLIT_CTRL_HUB_ADDR);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		split_ctrl |= ((usba_device->usb_hs_hub_port
5087c478bd9Sstevel@tonic-gate 		    << EHCI_QH_SPLIT_CTRL_HUB_PORT_SHIFT) &
5097c478bd9Sstevel@tonic-gate 		    EHCI_QH_SPLIT_CTRL_HUB_PORT);
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 		/* Set start split transaction state */
5147c478bd9Sstevel@tonic-gate 		status = EHCI_QH_STS_DO_START_SPLIT;
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	/* Assign endpoint's maxpacketsize */
5187c478bd9Sstevel@tonic-gate 	maxpacketsize = endpoint->wMaxPacketSize & USB_EP_MAX_PKTSZ_MASK;
5197c478bd9Sstevel@tonic-gate 	maxpacketsize = maxpacketsize << EHCI_QH_CTRL_MAXPKTSZ_SHIFT;
5207c478bd9Sstevel@tonic-gate 	ctrl |= (maxpacketsize & EHCI_QH_CTRL_MAXPKTSZ);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_ctrl, ctrl);
5237c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_split_ctrl, split_ctrl);
5247c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_status, status);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate /*
5297c478bd9Sstevel@tonic-gate  * ehci_insert_qh:
5307c478bd9Sstevel@tonic-gate  *
5317c478bd9Sstevel@tonic-gate  * Add the Endpoint Descriptor (QH) into the Host Controller's
5327c478bd9Sstevel@tonic-gate  * (HC) appropriate endpoint list.
5337c478bd9Sstevel@tonic-gate  */
5347c478bd9Sstevel@tonic-gate void
5357c478bd9Sstevel@tonic-gate ehci_insert_qh(
5367c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
5377c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
5427c478bd9Sstevel@tonic-gate 	    "ehci_insert_qh: qh=0x%p", pp->pp_qh);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	switch (ph->p_ep.bmAttributes & USB_EP_ATTR_MASK) {
5477c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_CONTROL:
5487c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_BULK:
5497c478bd9Sstevel@tonic-gate 		ehci_insert_async_qh(ehcip, pp);
5507c478bd9Sstevel@tonic-gate 		ehcip->ehci_open_async_count++;
5517c478bd9Sstevel@tonic-gate 		break;
5527c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_INTR:
5537c478bd9Sstevel@tonic-gate 		ehci_insert_intr_qh(ehcip, pp);
5547c478bd9Sstevel@tonic-gate 		ehcip->ehci_open_periodic_count++;
5557c478bd9Sstevel@tonic-gate 		break;
5567c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_ISOCH:
5577c478bd9Sstevel@tonic-gate 		/* ISOCH does not use QH, don't do anything but update count */
5587c478bd9Sstevel@tonic-gate 		ehcip->ehci_open_periodic_count++;
5597c478bd9Sstevel@tonic-gate 		break;
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 	ehci_toggle_scheduler(ehcip);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate  * ehci_insert_async_qh:
5677c478bd9Sstevel@tonic-gate  *
5687c478bd9Sstevel@tonic-gate  * Insert a control/bulk endpoint into the Host Controller's (HC)
5697c478bd9Sstevel@tonic-gate  * Asynchronous schedule endpoint list.
5707c478bd9Sstevel@tonic-gate  */
5717c478bd9Sstevel@tonic-gate static void
5727c478bd9Sstevel@tonic-gate ehci_insert_async_qh(
5737c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
5747c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh = pp->pp_qh;
5777c478bd9Sstevel@tonic-gate 	ehci_qh_t		*async_head_qh;
5787c478bd9Sstevel@tonic-gate 	ehci_qh_t		*next_qh;
5797c478bd9Sstevel@tonic-gate 	uintptr_t		qh_addr;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
5827c478bd9Sstevel@tonic-gate 	    "ehci_insert_async_qh:");
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/* Make sure this QH is not already in the list */
5877c478bd9Sstevel@tonic-gate 	ASSERT((Get_QH(qh->qh_prev) & EHCI_QH_LINK_PTR) == NULL);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	qh_addr = ehci_qh_cpu_to_iommu(ehcip, qh);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	/* Obtain a ptr to the head of the Async schedule list */
5927c478bd9Sstevel@tonic-gate 	async_head_qh = ehcip->ehci_head_of_async_sched_list;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	if (async_head_qh == NULL) {
5957c478bd9Sstevel@tonic-gate 		/* Set this QH to be the "head" of the circular list */
5967c478bd9Sstevel@tonic-gate 		Set_QH(qh->qh_ctrl,
5977c478bd9Sstevel@tonic-gate 		    (Get_QH(qh->qh_ctrl) | EHCI_QH_CTRL_RECLAIM_HEAD));
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		/* Set new QH's link and previous pointer to itself */
6007c478bd9Sstevel@tonic-gate 		Set_QH(qh->qh_link_ptr, qh_addr | EHCI_QH_LINK_REF_QH);
6017c478bd9Sstevel@tonic-gate 		Set_QH(qh->qh_prev, qh_addr);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		ehcip->ehci_head_of_async_sched_list = qh;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 		/* Set the head ptr to the new endpoint */
6067c478bd9Sstevel@tonic-gate 		Set_OpReg(ehci_async_list_addr, qh_addr);
607*921cd50dSgk 
608*921cd50dSgk 		/*
609*921cd50dSgk 		 * For some reason this register might get nulled out by
610*921cd50dSgk 		 * the Uli M1575 South Bridge. To workaround the hardware
611*921cd50dSgk 		 * problem, check the value after write and retry if the
612*921cd50dSgk 		 * last write fails.
613*921cd50dSgk 		 *
614*921cd50dSgk 		 * If the ASYNCLISTADDR remains "stuck" after
615*921cd50dSgk 		 * EHCI_MAX_RETRY retries, then the M1575 is broken
616*921cd50dSgk 		 * and is stuck in an inconsistent state and is about
617*921cd50dSgk 		 * to crash the machine with a trn_oor panic when it
618*921cd50dSgk 		 * does a DMA read from 0x0.  It is better to panic
619*921cd50dSgk 		 * now rather than wait for the trn_oor crash; this
620*921cd50dSgk 		 * way Customer Service will have a clean signature
621*921cd50dSgk 		 * that indicts the M1575 chip rather than a
622*921cd50dSgk 		 * mysterious and hard-to-diagnose trn_oor panic.
623*921cd50dSgk 		 */
624*921cd50dSgk 		if ((ehcip->ehci_vendor_id == PCI_VENDOR_ULi_M1575) &&
625*921cd50dSgk 		    (ehcip->ehci_device_id == PCI_DEVICE_ULi_M1575) &&
626*921cd50dSgk 		    (qh_addr != Get_OpReg(ehci_async_list_addr))) {
627*921cd50dSgk 			int retry = 0;
628*921cd50dSgk 
629*921cd50dSgk 			Set_OpRegRetry(ehci_async_list_addr, qh_addr, retry);
630*921cd50dSgk 			if (retry >= EHCI_MAX_RETRY)
631*921cd50dSgk 				cmn_err(CE_PANIC, "ehci_insert_async_qh:"
632*921cd50dSgk 				    " ASYNCLISTADDR write failed.");
633*921cd50dSgk 
634*921cd50dSgk 			USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
635*921cd50dSgk 			    "ehci_insert_async_qh: ASYNCLISTADDR "
636*921cd50dSgk 				"write failed, retry=%d", retry);
637*921cd50dSgk 		}
6387c478bd9Sstevel@tonic-gate 	} else {
6397c478bd9Sstevel@tonic-gate 		ASSERT(Get_QH(async_head_qh->qh_ctrl) &
6407c478bd9Sstevel@tonic-gate 		    EHCI_QH_CTRL_RECLAIM_HEAD);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		/* Ensure this QH's "H" bit is not set */
6437c478bd9Sstevel@tonic-gate 		Set_QH(qh->qh_ctrl,
6447c478bd9Sstevel@tonic-gate 		    (Get_QH(qh->qh_ctrl) & ~EHCI_QH_CTRL_RECLAIM_HEAD));
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 		next_qh = ehci_qh_iommu_to_cpu(ehcip,
6477c478bd9Sstevel@tonic-gate 		    Get_QH(async_head_qh->qh_link_ptr) & EHCI_QH_LINK_PTR);
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		/* Set new QH's link and previous pointers */
6507c478bd9Sstevel@tonic-gate 		Set_QH(qh->qh_link_ptr,
6517c478bd9Sstevel@tonic-gate 		    Get_QH(async_head_qh->qh_link_ptr) | EHCI_QH_LINK_REF_QH);
6527c478bd9Sstevel@tonic-gate 		Set_QH(qh->qh_prev, ehci_qh_cpu_to_iommu(ehcip, async_head_qh));
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		/* Set next QH's prev pointer */
6557c478bd9Sstevel@tonic-gate 		Set_QH(next_qh->qh_prev, ehci_qh_cpu_to_iommu(ehcip, qh));
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 		/* Set QH Head's link pointer points to new QH */
6587c478bd9Sstevel@tonic-gate 		Set_QH(async_head_qh->qh_link_ptr,
6597c478bd9Sstevel@tonic-gate 		    qh_addr | EHCI_QH_LINK_REF_QH);
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate /*
6657c478bd9Sstevel@tonic-gate  * ehci_insert_intr_qh:
6667c478bd9Sstevel@tonic-gate  *
6677c478bd9Sstevel@tonic-gate  * Insert a interrupt endpoint into the Host Controller's (HC) interrupt
6687c478bd9Sstevel@tonic-gate  * lattice tree.
6697c478bd9Sstevel@tonic-gate  */
6707c478bd9Sstevel@tonic-gate static void
6717c478bd9Sstevel@tonic-gate ehci_insert_intr_qh(
6727c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
6737c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp)
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh = pp->pp_qh;
6767c478bd9Sstevel@tonic-gate 	ehci_qh_t		*next_lattice_qh, *lattice_qh;
6777c478bd9Sstevel@tonic-gate 	uint_t			hnode;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
6807c478bd9Sstevel@tonic-gate 	    "ehci_insert_intr_qh:");
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	/* Make sure this QH is not already in the list */
6857c478bd9Sstevel@tonic-gate 	ASSERT((Get_QH(qh->qh_prev) & EHCI_QH_LINK_PTR) == NULL);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	/*
6887c478bd9Sstevel@tonic-gate 	 * The appropriate high speed node was found
6897c478bd9Sstevel@tonic-gate 	 * during the opening of the pipe.
6907c478bd9Sstevel@tonic-gate 	 */
6917c478bd9Sstevel@tonic-gate 	hnode = pp->pp_pnode;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	/* Find the lattice endpoint */
6947c478bd9Sstevel@tonic-gate 	lattice_qh = &ehcip->ehci_qh_pool_addr[hnode];
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	/* Find the next lattice endpoint */
6977c478bd9Sstevel@tonic-gate 	next_lattice_qh = ehci_qh_iommu_to_cpu(
6987c478bd9Sstevel@tonic-gate 	    ehcip, (Get_QH(lattice_qh->qh_link_ptr) & EHCI_QH_LINK_PTR));
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/* Update the previous pointer */
7017c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_prev, ehci_qh_cpu_to_iommu(ehcip, lattice_qh));
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	/* Check next_lattice_qh value */
7047c478bd9Sstevel@tonic-gate 	if (next_lattice_qh) {
7057c478bd9Sstevel@tonic-gate 		/* Update this qh to point to the next one in the lattice */
7067c478bd9Sstevel@tonic-gate 		Set_QH(qh->qh_link_ptr, Get_QH(lattice_qh->qh_link_ptr));
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 		/* Update the previous pointer of qh->qh_link_ptr */
7097c478bd9Sstevel@tonic-gate 		if (Get_QH(next_lattice_qh->qh_state) != EHCI_QH_STATIC) {
7107c478bd9Sstevel@tonic-gate 			Set_QH(next_lattice_qh->qh_prev,
7117c478bd9Sstevel@tonic-gate 			    ehci_qh_cpu_to_iommu(ehcip, qh));
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 	} else {
7147c478bd9Sstevel@tonic-gate 		/* Update qh's link pointer to terminate periodic list */
7157c478bd9Sstevel@tonic-gate 		Set_QH(qh->qh_link_ptr,
7167c478bd9Sstevel@tonic-gate 		    (Get_QH(lattice_qh->qh_link_ptr) | EHCI_QH_LINK_PTR_VALID));
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	/* Insert this endpoint into the lattice */
7207c478bd9Sstevel@tonic-gate 	Set_QH(lattice_qh->qh_link_ptr,
7217c478bd9Sstevel@tonic-gate 	    (ehci_qh_cpu_to_iommu(ehcip, qh) | EHCI_QH_LINK_REF_QH));
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate /*
7267c478bd9Sstevel@tonic-gate  * ehci_modify_qh_status_bit:
7277c478bd9Sstevel@tonic-gate  *
7287c478bd9Sstevel@tonic-gate  * Modify the halt bit on the Host Controller (HC) Endpoint Descriptor (QH).
7297c478bd9Sstevel@tonic-gate  *
7307c478bd9Sstevel@tonic-gate  * If several threads try to halt the same pipe, they will need to wait on
7317c478bd9Sstevel@tonic-gate  * a condition variable.  Only one thread is allowed to halt or unhalt the
7327c478bd9Sstevel@tonic-gate  * pipe at a time.
7337c478bd9Sstevel@tonic-gate  *
7347c478bd9Sstevel@tonic-gate  * Usually after a halt pipe, an unhalt pipe will follow soon after.  There
7357c478bd9Sstevel@tonic-gate  * is an assumption that an Unhalt pipe will never occur without a halt pipe.
7367c478bd9Sstevel@tonic-gate  */
7377c478bd9Sstevel@tonic-gate static void
7387c478bd9Sstevel@tonic-gate ehci_modify_qh_status_bit(
7397c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
7407c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
7417c478bd9Sstevel@tonic-gate 	halt_bit_t		action)
7427c478bd9Sstevel@tonic-gate {
7437c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh = pp->pp_qh;
7447c478bd9Sstevel@tonic-gate 	uint_t			smask, eps, split_intr_qh;
7457c478bd9Sstevel@tonic-gate 	uint_t			status;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
7487c478bd9Sstevel@tonic-gate 	    "ehci_modify_qh_status_bit: action=0x%x qh=0x%p",
7497c478bd9Sstevel@tonic-gate 	    action, qh);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	ehci_print_qh(ehcip, qh);
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	/*
7567c478bd9Sstevel@tonic-gate 	 * If this pipe is in the middle of halting don't allow another
7577c478bd9Sstevel@tonic-gate 	 * thread to come in and modify the same pipe.
7587c478bd9Sstevel@tonic-gate 	 */
7597c478bd9Sstevel@tonic-gate 	while (pp->pp_halt_state & EHCI_HALT_STATE_HALTING) {
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 		cv_wait(&pp->pp_halt_cmpl_cv,
7627c478bd9Sstevel@tonic-gate 			    &ehcip->ehci_int_mutex);
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	/* Sync the QH QTD pool to get up to date information */
7667c478bd9Sstevel@tonic-gate 	Sync_QH_QTD_Pool(ehcip);
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	if (action == CLEAR_HALT) {
7707c478bd9Sstevel@tonic-gate 		/*
7717c478bd9Sstevel@tonic-gate 		 * If the halt bit is to be cleared, just clear it.
7727c478bd9Sstevel@tonic-gate 		 * there shouldn't be any race condition problems.
7737c478bd9Sstevel@tonic-gate 		 * If the host controller reads the bit before the
7747c478bd9Sstevel@tonic-gate 		 * driver has a chance to set the bit, the bit will
7757c478bd9Sstevel@tonic-gate 		 * be reread on the next frame.
7767c478bd9Sstevel@tonic-gate 		 */
7777c478bd9Sstevel@tonic-gate 		Set_QH(qh->qh_ctrl,
7787c478bd9Sstevel@tonic-gate 		    (Get_QH(qh->qh_ctrl) & ~EHCI_QH_CTRL_ED_INACTIVATE));
7797c478bd9Sstevel@tonic-gate 		Set_QH(qh->qh_status,
7807c478bd9Sstevel@tonic-gate 		    Get_QH(qh->qh_status) & ~(EHCI_QH_STS_XACT_STATUS));
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 		goto success;
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	/* Halt the the QH, but first check to see if it is already halted */
7867c478bd9Sstevel@tonic-gate 	status = Get_QH(qh->qh_status);
7877c478bd9Sstevel@tonic-gate 	if (!(status & EHCI_QH_STS_HALTED)) {
7887c478bd9Sstevel@tonic-gate 		/* Indicate that this pipe is in the middle of halting. */
7897c478bd9Sstevel@tonic-gate 		pp->pp_halt_state |= EHCI_HALT_STATE_HALTING;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 		/*
7927c478bd9Sstevel@tonic-gate 		 * Find out if this is an full/low speed interrupt endpoint.
7937c478bd9Sstevel@tonic-gate 		 * A non-zero Cmask indicates that this QH is an interrupt
7947c478bd9Sstevel@tonic-gate 		 * endpoint.  Check the endpoint speed to see if it is either
7957c478bd9Sstevel@tonic-gate 		 * FULL or LOW .
7967c478bd9Sstevel@tonic-gate 		 */
7977c478bd9Sstevel@tonic-gate 		smask = Get_QH(qh->qh_split_ctrl) &
7987c478bd9Sstevel@tonic-gate 		    EHCI_QH_SPLIT_CTRL_INTR_MASK;
7997c478bd9Sstevel@tonic-gate 		eps = Get_QH(qh->qh_ctrl) & EHCI_QH_CTRL_ED_SPEED;
8007c478bd9Sstevel@tonic-gate 		split_intr_qh = ((smask != 0) &&
8017c478bd9Sstevel@tonic-gate 		    (eps != EHCI_QH_CTRL_ED_HIGH_SPEED));
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 		if (eps == EHCI_QH_CTRL_ED_HIGH_SPEED) {
8047c478bd9Sstevel@tonic-gate 			ehci_halt_hs_qh(ehcip, pp, qh);
8057c478bd9Sstevel@tonic-gate 		} else {
8067c478bd9Sstevel@tonic-gate 			if (split_intr_qh) {
8077c478bd9Sstevel@tonic-gate 				ehci_halt_fls_intr_qh(ehcip, qh);
8087c478bd9Sstevel@tonic-gate 			} else {
8097c478bd9Sstevel@tonic-gate 				ehci_halt_fls_ctrl_and_bulk_qh(ehcip, pp, qh);
8107c478bd9Sstevel@tonic-gate 			}
8117c478bd9Sstevel@tonic-gate 		}
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 		/* Indicate that this pipe is not in the middle of halting. */
8147c478bd9Sstevel@tonic-gate 		pp->pp_halt_state &= ~EHCI_HALT_STATE_HALTING;
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	/* Sync the QH QTD pool again to get the most up to date information */
8187c478bd9Sstevel@tonic-gate 	Sync_QH_QTD_Pool(ehcip);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	ehci_print_qh(ehcip, qh);
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	status = Get_QH(qh->qh_status);
8237c478bd9Sstevel@tonic-gate 	if (!(status & EHCI_QH_STS_HALTED)) {
8247c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L1(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
8257c478bd9Sstevel@tonic-gate 		    "ehci_modify_qh_status_bit: Failed to halt qh=0x%p", qh);
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 		ehci_print_qh(ehcip, qh);
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 		/* Set host controller soft state to error */
8307c478bd9Sstevel@tonic-gate 		ehcip->ehci_hc_soft_state = EHCI_CTLR_ERROR_STATE;
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 		ASSERT(status & EHCI_QH_STS_HALTED);
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate success:
8367c478bd9Sstevel@tonic-gate 	/* Wake up threads waiting for this pipe to be halted. */
8377c478bd9Sstevel@tonic-gate 	cv_signal(&pp->pp_halt_cmpl_cv);
8387c478bd9Sstevel@tonic-gate }
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate /*
8427c478bd9Sstevel@tonic-gate  * ehci_halt_hs_qh:
8437c478bd9Sstevel@tonic-gate  *
8447c478bd9Sstevel@tonic-gate  * Halts all types of HIGH SPEED QHs.
8457c478bd9Sstevel@tonic-gate  */
8467c478bd9Sstevel@tonic-gate static void
8477c478bd9Sstevel@tonic-gate ehci_halt_hs_qh(
8487c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
8497c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
8507c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh)
8517c478bd9Sstevel@tonic-gate {
8527c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph = pp->pp_pipe_handle;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
8557c478bd9Sstevel@tonic-gate 	    "ehci_halt_hs_qh:");
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	/* Remove this qh from the HCD's view, but do not reclaim it */
8587c478bd9Sstevel@tonic-gate 	ehci_remove_qh(ehcip, pp, B_FALSE);
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	/*
8617c478bd9Sstevel@tonic-gate 	 * Wait for atleast one SOF, just in case the HCD is in the
8627c478bd9Sstevel@tonic-gate 	 * middle accessing this QH.
8637c478bd9Sstevel@tonic-gate 	 */
8647c478bd9Sstevel@tonic-gate 	(void) ehci_wait_for_sof(ehcip);
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	/* Sync the QH QTD pool to get up to date information */
8677c478bd9Sstevel@tonic-gate 	Sync_QH_QTD_Pool(ehcip);
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	/* Modify the status bit and halt this QH. */
8707c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_status,
8717c478bd9Sstevel@tonic-gate 	    ((Get_QH(qh->qh_status) &
8727c478bd9Sstevel@tonic-gate 		~(EHCI_QH_STS_ACTIVE)) | EHCI_QH_STS_HALTED));
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	/* Insert this QH back into the HCD's view */
8757c478bd9Sstevel@tonic-gate 	ehci_insert_qh(ehcip, ph);
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate /*
8807c478bd9Sstevel@tonic-gate  * ehci_halt_fls_ctrl_and_bulk_qh:
8817c478bd9Sstevel@tonic-gate  *
8827c478bd9Sstevel@tonic-gate  * Halts FULL/LOW Ctrl and Bulk QHs only.
8837c478bd9Sstevel@tonic-gate  */
8847c478bd9Sstevel@tonic-gate static void
8857c478bd9Sstevel@tonic-gate ehci_halt_fls_ctrl_and_bulk_qh(
8867c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
8877c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
8887c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph = pp->pp_pipe_handle;
8917c478bd9Sstevel@tonic-gate 	uint_t			status, split_status, bytes_left;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
8957c478bd9Sstevel@tonic-gate 	    "ehci_halt_fls_ctrl_and_bulk_qh:");
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	/* Remove this qh from the HCD's view, but do not reclaim it */
8987c478bd9Sstevel@tonic-gate 	ehci_remove_qh(ehcip, pp, B_FALSE);
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	/*
9017c478bd9Sstevel@tonic-gate 	 * Wait for atleast one SOF, just in case the HCD is in the
9027c478bd9Sstevel@tonic-gate 	 * middle accessing this QH.
9037c478bd9Sstevel@tonic-gate 	 */
9047c478bd9Sstevel@tonic-gate 	(void) ehci_wait_for_sof(ehcip);
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	/* Sync the QH QTD pool to get up to date information */
9077c478bd9Sstevel@tonic-gate 	Sync_QH_QTD_Pool(ehcip);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	/* Modify the status bit and halt this QH. */
9107c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_status,
9117c478bd9Sstevel@tonic-gate 	    ((Get_QH(qh->qh_status) &
9127c478bd9Sstevel@tonic-gate 		~(EHCI_QH_STS_ACTIVE)) | EHCI_QH_STS_HALTED));
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	/* Check to see if the QH was in the middle of a transaction */
9157c478bd9Sstevel@tonic-gate 	status = Get_QH(qh->qh_status);
9167c478bd9Sstevel@tonic-gate 	split_status = status & EHCI_QH_STS_SPLIT_XSTATE;
9177c478bd9Sstevel@tonic-gate 	bytes_left = status & EHCI_QH_STS_BYTES_TO_XFER;
9187c478bd9Sstevel@tonic-gate 	if ((split_status == EHCI_QH_STS_DO_COMPLETE_SPLIT) &&
9197c478bd9Sstevel@tonic-gate 	    (bytes_left != 0)) {
9207c478bd9Sstevel@tonic-gate 		/* send ClearTTBuffer to this device's parent 2.0 hub */
9217c478bd9Sstevel@tonic-gate 		ehci_clear_tt_buffer(ehcip, ph, qh);
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	/* Insert this QH back into the HCD's view */
9257c478bd9Sstevel@tonic-gate 	ehci_insert_qh(ehcip, ph);
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate /*
9307c478bd9Sstevel@tonic-gate  * ehci_clear_tt_buffer
9317c478bd9Sstevel@tonic-gate  *
9327c478bd9Sstevel@tonic-gate  * This function will sent a Clear_TT_Buffer request to the pipe's
9337c478bd9Sstevel@tonic-gate  * parent 2.0 hub.
9347c478bd9Sstevel@tonic-gate  */
9357c478bd9Sstevel@tonic-gate static void
9367c478bd9Sstevel@tonic-gate ehci_clear_tt_buffer(
9377c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
9387c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
9397c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh)
9407c478bd9Sstevel@tonic-gate {
9417c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
9427c478bd9Sstevel@tonic-gate 	usba_device_t		*hub_usba_device;
9437c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	hub_def_ph;
9447c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*eptd;
9457c478bd9Sstevel@tonic-gate 	uchar_t			attributes;
9467c478bd9Sstevel@tonic-gate 	uint16_t		wValue;
9477c478bd9Sstevel@tonic-gate 	usb_ctrl_setup_t	setup;
9487c478bd9Sstevel@tonic-gate 	usb_cr_t		completion_reason;
9497c478bd9Sstevel@tonic-gate 	usb_cb_flags_t		cb_flags;
9507c478bd9Sstevel@tonic-gate 	int			retry;
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
9537c478bd9Sstevel@tonic-gate 	    "ehci_clear_tt_buffer: ");
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	/* Get some information about the current pipe */
9567c478bd9Sstevel@tonic-gate 	usba_device = ph->p_usba_device;
9577c478bd9Sstevel@tonic-gate 	eptd = &ph->p_ep;
9587c478bd9Sstevel@tonic-gate 	attributes = eptd->bmAttributes & USB_EP_ATTR_MASK;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	/*
9617c478bd9Sstevel@tonic-gate 	 * Create the wIndex for this request (usb spec 11.24.2.3)
9627c478bd9Sstevel@tonic-gate 	 * 3..0		Endpoint Number
9637c478bd9Sstevel@tonic-gate 	 * 10..4	Device Address
9647c478bd9Sstevel@tonic-gate 	 * 12..11	Endpoint Type
9657c478bd9Sstevel@tonic-gate 	 * 14..13	Reserved (must be 0)
9667c478bd9Sstevel@tonic-gate 	 * 15		Direction 1 = IN, 0 = OUT
9677c478bd9Sstevel@tonic-gate 	 */
9687c478bd9Sstevel@tonic-gate 	wValue = 0;
9697c478bd9Sstevel@tonic-gate 	if ((eptd->bEndpointAddress & USB_EP_DIR_MASK) == USB_EP_DIR_IN) {
9707c478bd9Sstevel@tonic-gate 		wValue |= 0x8000;
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate 	wValue |= attributes << 11;
9737c478bd9Sstevel@tonic-gate 	wValue |= (Get_QH(qh->qh_ctrl) & EHCI_QH_CTRL_DEVICE_ADDRESS) << 4;
9747c478bd9Sstevel@tonic-gate 	wValue |= (Get_QH(qh->qh_ctrl) & EHCI_QH_CTRL_ED_HIGH_SPEED) >>
9757c478bd9Sstevel@tonic-gate 	    EHCI_QH_CTRL_ED_NUMBER_SHIFT;
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	mutex_exit(&ehcip->ehci_int_mutex);
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	/* Manually fill in the request. */
9807c478bd9Sstevel@tonic-gate 	setup.bmRequestType = EHCI_CLEAR_TT_BUFFER_REQTYPE;
9817c478bd9Sstevel@tonic-gate 	setup.bRequest = EHCI_CLEAR_TT_BUFFER_BREQ;
9827c478bd9Sstevel@tonic-gate 	setup.wValue = wValue;
9837c478bd9Sstevel@tonic-gate 	setup.wIndex = 1;
9847c478bd9Sstevel@tonic-gate 	setup.wLength = 0;
9857c478bd9Sstevel@tonic-gate 	setup.attrs = USB_ATTRS_NONE;
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	/* Get the usba_device of the parent 2.0 hub. */
9887c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
9897c478bd9Sstevel@tonic-gate 	hub_usba_device = usba_device->usb_hs_hub_usba_dev;
9907c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	/* Get the default ctrl pipe for the parent 2.0 hub */
9937c478bd9Sstevel@tonic-gate 	mutex_enter(&hub_usba_device->usb_mutex);
9947c478bd9Sstevel@tonic-gate 	hub_def_ph = (usb_pipe_handle_t)&hub_usba_device->usb_ph_list[0];
9957c478bd9Sstevel@tonic-gate 	mutex_exit(&hub_usba_device->usb_mutex);
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	for (retry = 0; retry < 3; retry++) {
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 		/* sync send the request to the default pipe */
10007c478bd9Sstevel@tonic-gate 		if (usb_pipe_ctrl_xfer_wait(
10017c478bd9Sstevel@tonic-gate 		    hub_def_ph,
10027c478bd9Sstevel@tonic-gate 		    &setup,
10037c478bd9Sstevel@tonic-gate 		    NULL,
10047c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0) == USB_SUCCESS) {
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 			break;
10077c478bd9Sstevel@tonic-gate 		}
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
10107c478bd9Sstevel@tonic-gate 		    "ehci_clear_tt_buffer: Failed to clear tt buffer,"
10117c478bd9Sstevel@tonic-gate 		    "retry = %d, cr = %d, cb_flags = 0x%x\n",
10127c478bd9Sstevel@tonic-gate 		    retry, completion_reason, cb_flags);
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	if (retry >= 3) {
10167c478bd9Sstevel@tonic-gate 		char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
10177c478bd9Sstevel@tonic-gate 		dev_info_t *dip = hub_usba_device->usb_dip;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 		/*
10207c478bd9Sstevel@tonic-gate 		 * Ask the user to hotplug the 2.0 hub, to make sure that
10217c478bd9Sstevel@tonic-gate 		 * all the buffer is in sync since this command has failed.
10227c478bd9Sstevel@tonic-gate 		 */
10237c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L0(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
10247c478bd9Sstevel@tonic-gate 		    "Error recovery failure: Please hotplug the 2.0 hub at"
10257c478bd9Sstevel@tonic-gate 		    "%s", ddi_pathname(dip, path));
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	mutex_enter(&ehcip->ehci_int_mutex);
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate /*
10347c478bd9Sstevel@tonic-gate  * ehci_halt_fls_intr_qh:
10357c478bd9Sstevel@tonic-gate  *
10367c478bd9Sstevel@tonic-gate  * Halts FULL/LOW speed Intr QHs.
10377c478bd9Sstevel@tonic-gate  */
10387c478bd9Sstevel@tonic-gate static void
10397c478bd9Sstevel@tonic-gate ehci_halt_fls_intr_qh(
10407c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
10417c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh)
10427c478bd9Sstevel@tonic-gate {
10437c478bd9Sstevel@tonic-gate 	usb_frame_number_t	starting_frame;
10447c478bd9Sstevel@tonic-gate 	usb_frame_number_t	frames_past;
10457c478bd9Sstevel@tonic-gate 	uint_t			status, i;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
10487c478bd9Sstevel@tonic-gate 	    "ehci_halt_fls_intr_qh:");
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	/*
10517c478bd9Sstevel@tonic-gate 	 * Ask the HC to deactivate the QH in a
10527c478bd9Sstevel@tonic-gate 	 * full/low periodic QH.
10537c478bd9Sstevel@tonic-gate 	 */
10547c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_ctrl,
10557c478bd9Sstevel@tonic-gate 	    (Get_QH(qh->qh_ctrl) | EHCI_QH_CTRL_ED_INACTIVATE));
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	starting_frame = ehci_get_current_frame_number(ehcip);
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	/*
10607c478bd9Sstevel@tonic-gate 	 * Wait at least EHCI_NUM_INTR_QH_LISTS+2 frame or until
10617c478bd9Sstevel@tonic-gate 	 * the QH has been halted.
10627c478bd9Sstevel@tonic-gate 	 */
10637c478bd9Sstevel@tonic-gate 	Sync_QH_QTD_Pool(ehcip);
10647c478bd9Sstevel@tonic-gate 	frames_past = 0;
10657c478bd9Sstevel@tonic-gate 	status = Get_QH(qh->qh_status) & EHCI_QTD_CTRL_ACTIVE_XACT;
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	while ((frames_past <= (EHCI_NUM_INTR_QH_LISTS + 2)) &&
10687c478bd9Sstevel@tonic-gate 	    (status != 0)) {
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 		(void) ehci_wait_for_sof(ehcip);
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 		Sync_QH_QTD_Pool(ehcip);
10737c478bd9Sstevel@tonic-gate 		status = Get_QH(qh->qh_status) & EHCI_QTD_CTRL_ACTIVE_XACT;
10747c478bd9Sstevel@tonic-gate 		frames_past = ehci_get_current_frame_number(ehcip) -
10757c478bd9Sstevel@tonic-gate 		    starting_frame;
10767c478bd9Sstevel@tonic-gate 	}
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	/* Modify the status bit and halt this QH. */
10797c478bd9Sstevel@tonic-gate 	Sync_QH_QTD_Pool(ehcip);
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	status = Get_QH(qh->qh_status);
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	for (i = 0; i < EHCI_NUM_INTR_QH_LISTS; i++) {
10847c478bd9Sstevel@tonic-gate 		Set_QH(qh->qh_status,
10857c478bd9Sstevel@tonic-gate 			((Get_QH(qh->qh_status) &
10867c478bd9Sstevel@tonic-gate 			~(EHCI_QH_STS_ACTIVE)) | EHCI_QH_STS_HALTED));
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 		Sync_QH_QTD_Pool(ehcip);
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 		(void) ehci_wait_for_sof(ehcip);
10917c478bd9Sstevel@tonic-gate 		Sync_QH_QTD_Pool(ehcip);
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 		if (Get_QH(qh->qh_status) & EHCI_QH_STS_HALTED) {
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 			break;
10967c478bd9Sstevel@tonic-gate 		}
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	Sync_QH_QTD_Pool(ehcip);
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
11027c478bd9Sstevel@tonic-gate 	    "ehci_halt_fls_intr_qh: qh=0x%p frames past=%d, status=0x%x, 0x%x",
11037c478bd9Sstevel@tonic-gate 	    qh, ehci_get_current_frame_number(ehcip) - starting_frame,
11047c478bd9Sstevel@tonic-gate 	    status, Get_QH(qh->qh_status));
11057c478bd9Sstevel@tonic-gate }
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate /*
11097c478bd9Sstevel@tonic-gate  * ehci_remove_qh:
11107c478bd9Sstevel@tonic-gate  *
11117c478bd9Sstevel@tonic-gate  * Remove the Endpoint Descriptor (QH) from the Host Controller's appropriate
11127c478bd9Sstevel@tonic-gate  * endpoint list.
11137c478bd9Sstevel@tonic-gate  */
11147c478bd9Sstevel@tonic-gate void
11157c478bd9Sstevel@tonic-gate ehci_remove_qh(
11167c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
11177c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
11187c478bd9Sstevel@tonic-gate 	boolean_t		reclaim)
11197c478bd9Sstevel@tonic-gate {
11207c478bd9Sstevel@tonic-gate 	uchar_t			attributes;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
11257c478bd9Sstevel@tonic-gate 	    "ehci_remove_qh: qh=0x%p", pp->pp_qh);
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	attributes = pp->pp_pipe_handle->p_ep.bmAttributes & USB_EP_ATTR_MASK;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	switch (attributes) {
11307c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_CONTROL:
11317c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_BULK:
11327c478bd9Sstevel@tonic-gate 		ehci_remove_async_qh(ehcip, pp, reclaim);
11337c478bd9Sstevel@tonic-gate 		ehcip->ehci_open_async_count--;
11347c478bd9Sstevel@tonic-gate 		break;
11357c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_INTR:
11367c478bd9Sstevel@tonic-gate 		ehci_remove_intr_qh(ehcip, pp, reclaim);
11377c478bd9Sstevel@tonic-gate 		ehcip->ehci_open_periodic_count--;
11387c478bd9Sstevel@tonic-gate 		break;
11397c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_ISOCH:
11407c478bd9Sstevel@tonic-gate 		/* ISOCH does not use QH, don't do anything but update count */
11417c478bd9Sstevel@tonic-gate 		ehcip->ehci_open_periodic_count--;
11427c478bd9Sstevel@tonic-gate 		break;
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 	ehci_toggle_scheduler(ehcip);
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate /*
11497c478bd9Sstevel@tonic-gate  * ehci_remove_async_qh:
11507c478bd9Sstevel@tonic-gate  *
11517c478bd9Sstevel@tonic-gate  * Remove a control/bulk endpoint into the Host Controller's (HC)
11527c478bd9Sstevel@tonic-gate  * Asynchronous schedule endpoint list.
11537c478bd9Sstevel@tonic-gate  */
11547c478bd9Sstevel@tonic-gate static void
11557c478bd9Sstevel@tonic-gate ehci_remove_async_qh(
11567c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
11577c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
11587c478bd9Sstevel@tonic-gate 	boolean_t		reclaim)
11597c478bd9Sstevel@tonic-gate {
11607c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh = pp->pp_qh; /* qh to be removed */
11617c478bd9Sstevel@tonic-gate 	ehci_qh_t		*prev_qh, *next_qh;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
11647c478bd9Sstevel@tonic-gate 	    "ehci_remove_async_qh:");
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	prev_qh = ehci_qh_iommu_to_cpu(ehcip,
11697c478bd9Sstevel@tonic-gate 	    Get_QH(qh->qh_prev) & EHCI_QH_LINK_PTR);
11707c478bd9Sstevel@tonic-gate 	next_qh = ehci_qh_iommu_to_cpu(ehcip,
11717c478bd9Sstevel@tonic-gate 	    Get_QH(qh->qh_link_ptr) & EHCI_QH_LINK_PTR);
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	/* Make sure this QH is in the list */
11747c478bd9Sstevel@tonic-gate 	ASSERT(prev_qh != NULL);
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	/*
11777c478bd9Sstevel@tonic-gate 	 * If next QH and current QH are the same, then this is the last
11787c478bd9Sstevel@tonic-gate 	 * QH on the Asynchronous Schedule list.
11797c478bd9Sstevel@tonic-gate 	 */
11807c478bd9Sstevel@tonic-gate 	if (qh == next_qh) {
11817c478bd9Sstevel@tonic-gate 		ASSERT(Get_QH(qh->qh_ctrl) & EHCI_QH_CTRL_RECLAIM_HEAD);
11827c478bd9Sstevel@tonic-gate 		/*
11837c478bd9Sstevel@tonic-gate 		 * Null our pointer to the async sched list, but do not
11847c478bd9Sstevel@tonic-gate 		 * touch the host controller's list_addr.
11857c478bd9Sstevel@tonic-gate 		 */
11867c478bd9Sstevel@tonic-gate 		ehcip->ehci_head_of_async_sched_list = NULL;
11877c478bd9Sstevel@tonic-gate 		ASSERT(ehcip->ehci_open_async_count == 1);
11887c478bd9Sstevel@tonic-gate 	} else {
11897c478bd9Sstevel@tonic-gate 		/* If this QH is the HEAD then find another one to replace it */
11907c478bd9Sstevel@tonic-gate 		if (ehcip->ehci_head_of_async_sched_list == qh) {
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 			ASSERT(Get_QH(qh->qh_ctrl) & EHCI_QH_CTRL_RECLAIM_HEAD);
11937c478bd9Sstevel@tonic-gate 			ehcip->ehci_head_of_async_sched_list = next_qh;
11947c478bd9Sstevel@tonic-gate 			Set_QH(next_qh->qh_ctrl,
11957c478bd9Sstevel@tonic-gate 			    Get_QH(next_qh->qh_ctrl) |
11967c478bd9Sstevel@tonic-gate 			    EHCI_QH_CTRL_RECLAIM_HEAD);
11977c478bd9Sstevel@tonic-gate 		}
11987c478bd9Sstevel@tonic-gate 		Set_QH(prev_qh->qh_link_ptr, Get_QH(qh->qh_link_ptr));
11997c478bd9Sstevel@tonic-gate 		Set_QH(next_qh->qh_prev, Get_QH(qh->qh_prev));
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	/* qh_prev to indicate it is no longer in the circular list */
12037c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_prev, NULL);
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	if (reclaim) {
12067c478bd9Sstevel@tonic-gate 		ehci_insert_qh_on_reclaim_list(ehcip, pp);
12077c478bd9Sstevel@tonic-gate 	}
12087c478bd9Sstevel@tonic-gate }
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate /*
12127c478bd9Sstevel@tonic-gate  * ehci_remove_intr_qh:
12137c478bd9Sstevel@tonic-gate  *
12147c478bd9Sstevel@tonic-gate  * Set up an interrupt endpoint to be removed from the Host Controller's (HC)
12157c478bd9Sstevel@tonic-gate  * interrupt lattice tree. The Endpoint Descriptor (QH) will be freed in the
12167c478bd9Sstevel@tonic-gate  * interrupt handler.
12177c478bd9Sstevel@tonic-gate  */
12187c478bd9Sstevel@tonic-gate static void
12197c478bd9Sstevel@tonic-gate ehci_remove_intr_qh(
12207c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
12217c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
12227c478bd9Sstevel@tonic-gate 	boolean_t		reclaim)
12237c478bd9Sstevel@tonic-gate {
12247c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh = pp->pp_qh; /* qh to be removed */
12257c478bd9Sstevel@tonic-gate 	ehci_qh_t		*prev_qh, *next_qh;
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
12287c478bd9Sstevel@tonic-gate 	    "ehci_remove_intr_qh:");
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	prev_qh = ehci_qh_iommu_to_cpu(ehcip, Get_QH(qh->qh_prev));
12337c478bd9Sstevel@tonic-gate 	next_qh = ehci_qh_iommu_to_cpu(ehcip,
12347c478bd9Sstevel@tonic-gate 	    Get_QH(qh->qh_link_ptr) & EHCI_QH_LINK_PTR);
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	/* Make sure this QH is in the list */
12377c478bd9Sstevel@tonic-gate 	ASSERT(prev_qh != NULL);
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	if (next_qh) {
12407c478bd9Sstevel@tonic-gate 		/* Update previous qh's link pointer */
12417c478bd9Sstevel@tonic-gate 		Set_QH(prev_qh->qh_link_ptr, Get_QH(qh->qh_link_ptr));
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 		if (Get_QH(next_qh->qh_state) != EHCI_QH_STATIC) {
12447c478bd9Sstevel@tonic-gate 			/* Set the previous pointer of the next one */
12457c478bd9Sstevel@tonic-gate 			Set_QH(next_qh->qh_prev, Get_QH(qh->qh_prev));
12467c478bd9Sstevel@tonic-gate 		}
12477c478bd9Sstevel@tonic-gate 	} else {
12487c478bd9Sstevel@tonic-gate 		/* Update previous qh's link pointer */
12497c478bd9Sstevel@tonic-gate 		Set_QH(prev_qh->qh_link_ptr,
12507c478bd9Sstevel@tonic-gate 		    (Get_QH(qh->qh_link_ptr) | EHCI_QH_LINK_PTR_VALID));
12517c478bd9Sstevel@tonic-gate 	}
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	/* qh_prev to indicate it is no longer in the circular list */
12547c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_prev, NULL);
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	if (reclaim) {
12577c478bd9Sstevel@tonic-gate 		ehci_insert_qh_on_reclaim_list(ehcip, pp);
12587c478bd9Sstevel@tonic-gate 	}
12597c478bd9Sstevel@tonic-gate }
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate /*
12637c478bd9Sstevel@tonic-gate  * ehci_insert_qh_on_reclaim_list:
12647c478bd9Sstevel@tonic-gate  *
12657c478bd9Sstevel@tonic-gate  * Insert Endpoint onto the reclaim list
12667c478bd9Sstevel@tonic-gate  */
12677c478bd9Sstevel@tonic-gate static void
12687c478bd9Sstevel@tonic-gate ehci_insert_qh_on_reclaim_list(
12697c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
12707c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp)
12717c478bd9Sstevel@tonic-gate {
12727c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh = pp->pp_qh; /* qh to be removed */
12737c478bd9Sstevel@tonic-gate 	ehci_qh_t		*next_qh, *prev_qh;
12747c478bd9Sstevel@tonic-gate 	usb_frame_number_t	frame_number;
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	/*
12797c478bd9Sstevel@tonic-gate 	 * Read current usb frame number and add appropriate number of
12807c478bd9Sstevel@tonic-gate 	 * usb frames needs to wait before reclaiming current endpoint.
12817c478bd9Sstevel@tonic-gate 	 */
12827c478bd9Sstevel@tonic-gate 	frame_number =
12837c478bd9Sstevel@tonic-gate 	    ehci_get_current_frame_number(ehcip) + MAX_SOF_WAIT_COUNT;
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	/* Store 32-bit ID */
12867c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_reclaim_frame,
12877c478bd9Sstevel@tonic-gate 	    ((uint32_t)(EHCI_GET_ID((void *)(uintptr_t)frame_number))));
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	/* Insert the endpoint onto the reclamation list */
12907c478bd9Sstevel@tonic-gate 	if (ehcip->ehci_reclaim_list) {
12917c478bd9Sstevel@tonic-gate 		next_qh = ehcip->ehci_reclaim_list;
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 		while (next_qh) {
12947c478bd9Sstevel@tonic-gate 			prev_qh = next_qh;
12957c478bd9Sstevel@tonic-gate 			next_qh = ehci_qh_iommu_to_cpu(ehcip,
12967c478bd9Sstevel@tonic-gate 			    Get_QH(next_qh->qh_reclaim_next));
12977c478bd9Sstevel@tonic-gate 		}
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 		Set_QH(prev_qh->qh_reclaim_next,
13007c478bd9Sstevel@tonic-gate 		    ehci_qh_cpu_to_iommu(ehcip, qh));
13017c478bd9Sstevel@tonic-gate 	} else {
13027c478bd9Sstevel@tonic-gate 		ehcip->ehci_reclaim_list = qh;
13037c478bd9Sstevel@tonic-gate 	}
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	ASSERT(Get_QH(qh->qh_reclaim_next) == NULL);
13067c478bd9Sstevel@tonic-gate }
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate /*
13107c478bd9Sstevel@tonic-gate  * ehci_deallocate_qh:
13117c478bd9Sstevel@tonic-gate  *
13127c478bd9Sstevel@tonic-gate  * Deallocate a Host Controller's (HC) Endpoint Descriptor (QH).
13137c478bd9Sstevel@tonic-gate  *
13147c478bd9Sstevel@tonic-gate  * NOTE: This function is also called from POLLED MODE.
13157c478bd9Sstevel@tonic-gate  */
13167c478bd9Sstevel@tonic-gate void
13177c478bd9Sstevel@tonic-gate ehci_deallocate_qh(
13187c478bd9Sstevel@tonic-gate 	ehci_state_t	*ehcip,
13197c478bd9Sstevel@tonic-gate 	ehci_qh_t	*old_qh)
13207c478bd9Sstevel@tonic-gate {
13217c478bd9Sstevel@tonic-gate 	ehci_qtd_t	*first_dummy_qtd, *second_dummy_qtd;
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
13247c478bd9Sstevel@tonic-gate 	    "ehci_deallocate_qh:");
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	first_dummy_qtd = ehci_qtd_iommu_to_cpu(ehcip,
13297c478bd9Sstevel@tonic-gate 	    (Get_QH(old_qh->qh_next_qtd) & EHCI_QH_NEXT_QTD_PTR));
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	if (first_dummy_qtd) {
13327c478bd9Sstevel@tonic-gate 		ASSERT(Get_QTD(first_dummy_qtd->qtd_state) == EHCI_QTD_DUMMY);
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 		second_dummy_qtd = ehci_qtd_iommu_to_cpu(ehcip,
13357c478bd9Sstevel@tonic-gate 		    Get_QTD(first_dummy_qtd->qtd_next_qtd));
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 		if (second_dummy_qtd) {
13387c478bd9Sstevel@tonic-gate 			ASSERT(Get_QTD(second_dummy_qtd->qtd_state) ==
13397c478bd9Sstevel@tonic-gate 			    EHCI_QTD_DUMMY);
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 			ehci_deallocate_qtd(ehcip, second_dummy_qtd);
13427c478bd9Sstevel@tonic-gate 		}
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 		ehci_deallocate_qtd(ehcip, first_dummy_qtd);
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
13487c478bd9Sstevel@tonic-gate 	    "ehci_deallocate_qh: Deallocated 0x%p", (void *)old_qh);
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	bzero((void *)old_qh, sizeof (ehci_qh_t));
13517c478bd9Sstevel@tonic-gate 	Set_QH(old_qh->qh_state, EHCI_QH_FREE);
13527c478bd9Sstevel@tonic-gate }
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate /*
13567c478bd9Sstevel@tonic-gate  * ehci_qh_cpu_to_iommu:
13577c478bd9Sstevel@tonic-gate  *
13587c478bd9Sstevel@tonic-gate  * This function converts for the given Endpoint Descriptor (QH) CPU address
13597c478bd9Sstevel@tonic-gate  * to IO address.
13607c478bd9Sstevel@tonic-gate  *
13617c478bd9Sstevel@tonic-gate  * NOTE: This function is also called from POLLED MODE.
13627c478bd9Sstevel@tonic-gate  */
13637c478bd9Sstevel@tonic-gate uint32_t
13647c478bd9Sstevel@tonic-gate ehci_qh_cpu_to_iommu(
13657c478bd9Sstevel@tonic-gate 	ehci_state_t	*ehcip,
13667c478bd9Sstevel@tonic-gate 	ehci_qh_t	*addr)
13677c478bd9Sstevel@tonic-gate {
13687c478bd9Sstevel@tonic-gate 	uint32_t	qh;
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	qh = (uint32_t)ehcip->ehci_qh_pool_cookie.dmac_address +
13717c478bd9Sstevel@tonic-gate 	    (uint32_t)((uintptr_t)addr - (uintptr_t)(ehcip->ehci_qh_pool_addr));
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	ASSERT(qh >= ehcip->ehci_qh_pool_cookie.dmac_address);
13747c478bd9Sstevel@tonic-gate 	ASSERT(qh <= ehcip->ehci_qh_pool_cookie.dmac_address +
13757c478bd9Sstevel@tonic-gate 	    sizeof (ehci_qh_t) * ehci_qh_pool_size);
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	return (qh);
13787c478bd9Sstevel@tonic-gate }
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate /*
13827c478bd9Sstevel@tonic-gate  * ehci_qh_iommu_to_cpu:
13837c478bd9Sstevel@tonic-gate  *
13847c478bd9Sstevel@tonic-gate  * This function converts for the given Endpoint Descriptor (QH) IO address
13857c478bd9Sstevel@tonic-gate  * to CPU address.
13867c478bd9Sstevel@tonic-gate  */
13877c478bd9Sstevel@tonic-gate ehci_qh_t *
13887c478bd9Sstevel@tonic-gate ehci_qh_iommu_to_cpu(
13897c478bd9Sstevel@tonic-gate 	ehci_state_t	*ehcip,
13907c478bd9Sstevel@tonic-gate 	uintptr_t	addr)
13917c478bd9Sstevel@tonic-gate {
13927c478bd9Sstevel@tonic-gate 	ehci_qh_t	*qh;
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	if (addr == NULL) {
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 		return (NULL);
13977c478bd9Sstevel@tonic-gate 	}
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	qh = (ehci_qh_t *)((uintptr_t)
14007c478bd9Sstevel@tonic-gate 	    (addr - ehcip->ehci_qh_pool_cookie.dmac_address) +
14017c478bd9Sstevel@tonic-gate 	    (uintptr_t)ehcip->ehci_qh_pool_addr);
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	ASSERT(qh >= ehcip->ehci_qh_pool_addr);
14047c478bd9Sstevel@tonic-gate 	ASSERT((uintptr_t)qh <= (uintptr_t)ehcip->ehci_qh_pool_addr +
14057c478bd9Sstevel@tonic-gate 	    (uintptr_t)(sizeof (ehci_qh_t) * ehci_qh_pool_size));
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	return (qh);
14087c478bd9Sstevel@tonic-gate }
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate /*
14127c478bd9Sstevel@tonic-gate  * Transfer Descriptor manipulations functions
14137c478bd9Sstevel@tonic-gate  */
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate /*
14167c478bd9Sstevel@tonic-gate  * ehci_initialize_dummy:
14177c478bd9Sstevel@tonic-gate  *
14187c478bd9Sstevel@tonic-gate  * An Endpoint Descriptor (QH) has a  dummy Transfer Descriptor (QTD) on the
14197c478bd9Sstevel@tonic-gate  * end of its QTD list. Initially, both the head and tail pointers of the QH
14207c478bd9Sstevel@tonic-gate  * point to the dummy QTD.
14217c478bd9Sstevel@tonic-gate  */
14227c478bd9Sstevel@tonic-gate static int
14237c478bd9Sstevel@tonic-gate ehci_initialize_dummy(
14247c478bd9Sstevel@tonic-gate 	ehci_state_t	*ehcip,
14257c478bd9Sstevel@tonic-gate 	ehci_qh_t	*qh)
14267c478bd9Sstevel@tonic-gate {
14277c478bd9Sstevel@tonic-gate 	ehci_qtd_t	*first_dummy_qtd, *second_dummy_qtd;
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	/* Allocate first dummy QTD */
14307c478bd9Sstevel@tonic-gate 	first_dummy_qtd = ehci_allocate_qtd_from_pool(ehcip);
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	if (first_dummy_qtd == NULL) {
14337c478bd9Sstevel@tonic-gate 		return (USB_NO_RESOURCES);
14347c478bd9Sstevel@tonic-gate 	}
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	/* Allocate second dummy QTD */
14377c478bd9Sstevel@tonic-gate 	second_dummy_qtd = ehci_allocate_qtd_from_pool(ehcip);
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	if (second_dummy_qtd == NULL) {
14407c478bd9Sstevel@tonic-gate 		/* Deallocate first dummy QTD */
14417c478bd9Sstevel@tonic-gate 		ehci_deallocate_qtd(ehcip, first_dummy_qtd);
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 		return (USB_NO_RESOURCES);
14447c478bd9Sstevel@tonic-gate 	}
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	/* Next QTD pointer of an QH point to this new dummy QTD */
14477c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_next_qtd, ehci_qtd_cpu_to_iommu(ehcip,
14487c478bd9Sstevel@tonic-gate 	    first_dummy_qtd) & EHCI_QH_NEXT_QTD_PTR);
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	/* Set qh's dummy qtd field */
14517c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_dummy_qtd, ehci_qtd_cpu_to_iommu(ehcip, first_dummy_qtd));
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	/* Set first_dummy's next qtd pointer */
14547c478bd9Sstevel@tonic-gate 	Set_QTD(first_dummy_qtd->qtd_next_qtd,
14557c478bd9Sstevel@tonic-gate 	    ehci_qtd_cpu_to_iommu(ehcip, second_dummy_qtd));
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
14587c478bd9Sstevel@tonic-gate }
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate /*
14617c478bd9Sstevel@tonic-gate  * ehci_allocate_ctrl_resources:
14627c478bd9Sstevel@tonic-gate  *
14637c478bd9Sstevel@tonic-gate  * Calculates the number of tds necessary for a ctrl transfer, and allocates
14647c478bd9Sstevel@tonic-gate  * all the resources necessary.
14657c478bd9Sstevel@tonic-gate  *
14667c478bd9Sstevel@tonic-gate  * Returns NULL if there is insufficient resources otherwise TW.
14677c478bd9Sstevel@tonic-gate  */
14687c478bd9Sstevel@tonic-gate ehci_trans_wrapper_t *
14697c478bd9Sstevel@tonic-gate ehci_allocate_ctrl_resources(
14707c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
14717c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
14727c478bd9Sstevel@tonic-gate 	usb_ctrl_req_t		*ctrl_reqp,
14737c478bd9Sstevel@tonic-gate 	usb_flags_t		usb_flags)
14747c478bd9Sstevel@tonic-gate {
14757c478bd9Sstevel@tonic-gate 	size_t			qtd_count = 2;
14763304303fSsl 	size_t			ctrl_buf_size;
14777c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw;
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	/* Add one more td for data phase */
14807c478bd9Sstevel@tonic-gate 	if (ctrl_reqp->ctrl_wLength) {
14817c478bd9Sstevel@tonic-gate 		qtd_count += 1;
14827c478bd9Sstevel@tonic-gate 	}
14837c478bd9Sstevel@tonic-gate 
14843304303fSsl 	/*
14853304303fSsl 	 * If we have a control data phase, the data buffer starts
14863304303fSsl 	 * on the next 4K page boundary. So the TW buffer is allocated
14873304303fSsl 	 * to be larger than required. The buffer in the range of
14883304303fSsl 	 * [SETUP_SIZE, EHCI_MAX_QTD_BUF_SIZE) is just for padding
14893304303fSsl 	 * and not to be transferred.
14903304303fSsl 	 */
14913304303fSsl 	if (ctrl_reqp->ctrl_wLength) {
14923304303fSsl 		ctrl_buf_size = EHCI_MAX_QTD_BUF_SIZE +
14933304303fSsl 		    ctrl_reqp->ctrl_wLength;
14943304303fSsl 	} else {
14953304303fSsl 		ctrl_buf_size = SETUP_SIZE;
14963304303fSsl 	}
14973304303fSsl 
14983304303fSsl 	tw = ehci_allocate_tw_resources(ehcip, pp, ctrl_buf_size,
14997c478bd9Sstevel@tonic-gate 	    usb_flags, qtd_count);
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	return (tw);
15027c478bd9Sstevel@tonic-gate }
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate /*
15057c478bd9Sstevel@tonic-gate  * ehci_insert_ctrl_req:
15067c478bd9Sstevel@tonic-gate  *
15077c478bd9Sstevel@tonic-gate  * Create a Transfer Descriptor (QTD) and a data buffer for a control endpoint.
15087c478bd9Sstevel@tonic-gate  */
15097c478bd9Sstevel@tonic-gate /* ARGSUSED */
15107c478bd9Sstevel@tonic-gate void
15117c478bd9Sstevel@tonic-gate ehci_insert_ctrl_req(
15127c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
15137c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
15147c478bd9Sstevel@tonic-gate 	usb_ctrl_req_t		*ctrl_reqp,
15157c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw,
15167c478bd9Sstevel@tonic-gate 	usb_flags_t		usb_flags)
15177c478bd9Sstevel@tonic-gate {
15187c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
15197c478bd9Sstevel@tonic-gate 	uchar_t			bmRequestType = ctrl_reqp->ctrl_bmRequestType;
15207c478bd9Sstevel@tonic-gate 	uchar_t			bRequest = ctrl_reqp->ctrl_bRequest;
15217c478bd9Sstevel@tonic-gate 	uint16_t		wValue = ctrl_reqp->ctrl_wValue;
15227c478bd9Sstevel@tonic-gate 	uint16_t		wIndex = ctrl_reqp->ctrl_wIndex;
15237c478bd9Sstevel@tonic-gate 	uint16_t		wLength = ctrl_reqp->ctrl_wLength;
15247c478bd9Sstevel@tonic-gate 	mblk_t			*data = ctrl_reqp->ctrl_data;
15257c478bd9Sstevel@tonic-gate 	uint32_t		ctrl = 0;
15267c478bd9Sstevel@tonic-gate 	uint8_t			setup_packet[8];
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
15297c478bd9Sstevel@tonic-gate 	    "ehci_insert_ctrl_req:");
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	/*
15347c478bd9Sstevel@tonic-gate 	 * Save current control request pointer and timeout values
15357c478bd9Sstevel@tonic-gate 	 * in transfer wrapper.
15367c478bd9Sstevel@tonic-gate 	 */
15377c478bd9Sstevel@tonic-gate 	tw->tw_curr_xfer_reqp = (usb_opaque_t)ctrl_reqp;
15387c478bd9Sstevel@tonic-gate 	tw->tw_timeout = ctrl_reqp->ctrl_timeout ?
15397c478bd9Sstevel@tonic-gate 	    ctrl_reqp->ctrl_timeout : EHCI_DEFAULT_XFER_TIMEOUT;
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	/*
15427c478bd9Sstevel@tonic-gate 	 * Initialize the callback and any callback data for when
15437c478bd9Sstevel@tonic-gate 	 * the qtd completes.
15447c478bd9Sstevel@tonic-gate 	 */
15457c478bd9Sstevel@tonic-gate 	tw->tw_handle_qtd = ehci_handle_ctrl_qtd;
15467c478bd9Sstevel@tonic-gate 	tw->tw_handle_callback_value = NULL;
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	/*
15497c478bd9Sstevel@tonic-gate 	 * swap the setup bytes where necessary since we specified
15507c478bd9Sstevel@tonic-gate 	 * NEVERSWAP
15517c478bd9Sstevel@tonic-gate 	 */
15527c478bd9Sstevel@tonic-gate 	setup_packet[0] = bmRequestType;
15537c478bd9Sstevel@tonic-gate 	setup_packet[1] = bRequest;
15547c478bd9Sstevel@tonic-gate 	setup_packet[2] = wValue;
15557c478bd9Sstevel@tonic-gate 	setup_packet[3] = wValue >> 8;
15567c478bd9Sstevel@tonic-gate 	setup_packet[4] = wIndex;
15577c478bd9Sstevel@tonic-gate 	setup_packet[5] = wIndex >> 8;
15587c478bd9Sstevel@tonic-gate 	setup_packet[6] = wLength;
15597c478bd9Sstevel@tonic-gate 	setup_packet[7] = wLength >> 8;
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	bcopy(setup_packet, tw->tw_buf, SETUP_SIZE);
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 	Sync_IO_Buffer_for_device(tw->tw_dmahandle, SETUP_SIZE);
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 	ctrl = (EHCI_QTD_CTRL_DATA_TOGGLE_0 | EHCI_QTD_CTRL_SETUP_PID);
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	/*
15687c478bd9Sstevel@tonic-gate 	 * The QTD's are placed on the QH one at a time.
15697c478bd9Sstevel@tonic-gate 	 * Once this QTD is placed on the done list, the
15707c478bd9Sstevel@tonic-gate 	 * data or status phase QTD will be enqueued.
15717c478bd9Sstevel@tonic-gate 	 */
15723304303fSsl 	(void) ehci_insert_qtd(ehcip, ctrl, 0, SETUP_SIZE,
15737c478bd9Sstevel@tonic-gate 	    EHCI_CTRL_SETUP_PHASE, pp, tw);
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
15767c478bd9Sstevel@tonic-gate 	    "ehci_insert_ctrl_req: pp 0x%p", (void *)pp);
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	/*
15797c478bd9Sstevel@tonic-gate 	 * If this control transfer has a data phase, record the
15807c478bd9Sstevel@tonic-gate 	 * direction. If the data phase is an OUT transaction,
15817c478bd9Sstevel@tonic-gate 	 * copy the data into the buffer of the transfer wrapper.
15827c478bd9Sstevel@tonic-gate 	 */
15837c478bd9Sstevel@tonic-gate 	if (wLength != 0) {
15847c478bd9Sstevel@tonic-gate 		/* There is a data stage.  Find the direction */
15857c478bd9Sstevel@tonic-gate 		if (bmRequestType & USB_DEV_REQ_DEV_TO_HOST) {
15867c478bd9Sstevel@tonic-gate 			tw->tw_direction = EHCI_QTD_CTRL_IN_PID;
15877c478bd9Sstevel@tonic-gate 		} else {
15887c478bd9Sstevel@tonic-gate 			tw->tw_direction = EHCI_QTD_CTRL_OUT_PID;
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 			/* Copy the data into the message */
15913304303fSsl 			bcopy(data->b_rptr, tw->tw_buf + EHCI_MAX_QTD_BUF_SIZE,
15923304303fSsl 				wLength);
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 			Sync_IO_Buffer_for_device(tw->tw_dmahandle,
15953304303fSsl 				wLength + EHCI_MAX_QTD_BUF_SIZE);
15967c478bd9Sstevel@tonic-gate 		}
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 		ctrl = (EHCI_QTD_CTRL_DATA_TOGGLE_1 | tw->tw_direction);
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 		/*
16017c478bd9Sstevel@tonic-gate 		 * Create the QTD.  If this is an OUT transaction,
16027c478bd9Sstevel@tonic-gate 		 * the data is already in the buffer of the TW.
16033304303fSsl 		 * The transfer should start from EHCI_MAX_QTD_BUF_SIZE
16043304303fSsl 		 * which is 4K aligned, though the ctrl phase only
16053304303fSsl 		 * transfers a length of SETUP_SIZE. The padding data
16063304303fSsl 		 * in the TW buffer are discarded.
16077c478bd9Sstevel@tonic-gate 		 */
16083304303fSsl 		(void) ehci_insert_qtd(ehcip, ctrl, EHCI_MAX_QTD_BUF_SIZE,
16093304303fSsl 		    tw->tw_length - EHCI_MAX_QTD_BUF_SIZE,
16103304303fSsl 		    EHCI_CTRL_DATA_PHASE, pp, tw);
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 		/*
16137c478bd9Sstevel@tonic-gate 		 * The direction of the STATUS QTD depends  on
16147c478bd9Sstevel@tonic-gate 		 * the direction of the transfer.
16157c478bd9Sstevel@tonic-gate 		 */
16167c478bd9Sstevel@tonic-gate 		if (tw->tw_direction == EHCI_QTD_CTRL_IN_PID) {
16177c478bd9Sstevel@tonic-gate 			ctrl = (EHCI_QTD_CTRL_DATA_TOGGLE_1|
16187c478bd9Sstevel@tonic-gate 			    EHCI_QTD_CTRL_OUT_PID |
16197c478bd9Sstevel@tonic-gate 			    EHCI_QTD_CTRL_INTR_ON_COMPLETE);
16207c478bd9Sstevel@tonic-gate 		} else {
16217c478bd9Sstevel@tonic-gate 			ctrl = (EHCI_QTD_CTRL_DATA_TOGGLE_1|
16227c478bd9Sstevel@tonic-gate 			    EHCI_QTD_CTRL_IN_PID |
16237c478bd9Sstevel@tonic-gate 			    EHCI_QTD_CTRL_INTR_ON_COMPLETE);
16247c478bd9Sstevel@tonic-gate 		}
16257c478bd9Sstevel@tonic-gate 	} else {
16267c478bd9Sstevel@tonic-gate 		/*
16277c478bd9Sstevel@tonic-gate 		 * There is no data stage,  then initiate
16287c478bd9Sstevel@tonic-gate 		 * status phase from the host.
16297c478bd9Sstevel@tonic-gate 		 */
16307c478bd9Sstevel@tonic-gate 		ctrl = (EHCI_QTD_CTRL_DATA_TOGGLE_1 |
16317c478bd9Sstevel@tonic-gate 		    EHCI_QTD_CTRL_IN_PID |
16327c478bd9Sstevel@tonic-gate 		    EHCI_QTD_CTRL_INTR_ON_COMPLETE);
16337c478bd9Sstevel@tonic-gate 	}
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 
16363304303fSsl 	(void) ehci_insert_qtd(ehcip, ctrl, 0, 0,
16377c478bd9Sstevel@tonic-gate 	    EHCI_CTRL_STATUS_PHASE, pp,  tw);
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	/* Start the timer for this control transfer */
16407c478bd9Sstevel@tonic-gate 	ehci_start_xfer_timer(ehcip, pp, tw);
16417c478bd9Sstevel@tonic-gate }
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate /*
16457c478bd9Sstevel@tonic-gate  * ehci_allocate_bulk_resources:
16467c478bd9Sstevel@tonic-gate  *
16477c478bd9Sstevel@tonic-gate  * Calculates the number of tds necessary for a ctrl transfer, and allocates
16487c478bd9Sstevel@tonic-gate  * all the resources necessary.
16497c478bd9Sstevel@tonic-gate  *
16507c478bd9Sstevel@tonic-gate  * Returns NULL if there is insufficient resources otherwise TW.
16517c478bd9Sstevel@tonic-gate  */
16527c478bd9Sstevel@tonic-gate ehci_trans_wrapper_t *
16537c478bd9Sstevel@tonic-gate ehci_allocate_bulk_resources(
16547c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
16557c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
16567c478bd9Sstevel@tonic-gate 	usb_bulk_req_t		*bulk_reqp,
16577c478bd9Sstevel@tonic-gate 	usb_flags_t		usb_flags)
16587c478bd9Sstevel@tonic-gate {
16597c478bd9Sstevel@tonic-gate 	size_t			qtd_count = 0;
16607c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw;
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	/* Check the size of bulk request */
16637c478bd9Sstevel@tonic-gate 	if (bulk_reqp->bulk_len > EHCI_MAX_BULK_XFER_SIZE) {
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
16667c478bd9Sstevel@tonic-gate 		    "ehci_allocate_bulk_resources: Bulk request size 0x%x is "
16677c478bd9Sstevel@tonic-gate 		    "more than 0x%x", bulk_reqp->bulk_len,
16687c478bd9Sstevel@tonic-gate 		    EHCI_MAX_BULK_XFER_SIZE);
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 		return (NULL);
16717c478bd9Sstevel@tonic-gate 	}
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	/* Get the required bulk packet size */
16747c478bd9Sstevel@tonic-gate 	qtd_count = bulk_reqp->bulk_len / EHCI_MAX_QTD_XFER_SIZE;
16757c478bd9Sstevel@tonic-gate 	if (bulk_reqp->bulk_len % EHCI_MAX_QTD_XFER_SIZE) {
16767c478bd9Sstevel@tonic-gate 		qtd_count += 1;
16777c478bd9Sstevel@tonic-gate 	}
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	tw = ehci_allocate_tw_resources(ehcip, pp, bulk_reqp->bulk_len,
16807c478bd9Sstevel@tonic-gate 	    usb_flags, qtd_count);
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	return (tw);
16837c478bd9Sstevel@tonic-gate }
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate /*
16867c478bd9Sstevel@tonic-gate  * ehci_insert_bulk_req:
16877c478bd9Sstevel@tonic-gate  *
16887c478bd9Sstevel@tonic-gate  * Create a Transfer Descriptor (QTD) and a data buffer for a bulk
16897c478bd9Sstevel@tonic-gate  * endpoint.
16907c478bd9Sstevel@tonic-gate  */
16917c478bd9Sstevel@tonic-gate /* ARGSUSED */
16927c478bd9Sstevel@tonic-gate void
16937c478bd9Sstevel@tonic-gate ehci_insert_bulk_req(
16947c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
16957c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
16967c478bd9Sstevel@tonic-gate 	usb_bulk_req_t		*bulk_reqp,
16977c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw,
16987c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
16997c478bd9Sstevel@tonic-gate {
17007c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
17017c478bd9Sstevel@tonic-gate 	uint_t			bulk_pkt_size, count;
17027c478bd9Sstevel@tonic-gate 	size_t			residue = 0, len = 0;
17037c478bd9Sstevel@tonic-gate 	uint32_t		ctrl = 0;
17047c478bd9Sstevel@tonic-gate 	int			pipe_dir;
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
17077c478bd9Sstevel@tonic-gate 	    "ehci_insert_bulk_req: bulk_reqp = 0x%p flags = 0x%x",
17087c478bd9Sstevel@tonic-gate 	    bulk_reqp, flags);
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	/* Get the bulk pipe direction */
17137c478bd9Sstevel@tonic-gate 	pipe_dir = ph->p_ep.bEndpointAddress & USB_EP_DIR_MASK;
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	/* Get the required bulk packet size */
17167c478bd9Sstevel@tonic-gate 	bulk_pkt_size = min(bulk_reqp->bulk_len, EHCI_MAX_QTD_XFER_SIZE);
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 	residue = tw->tw_length % bulk_pkt_size;
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
17217c478bd9Sstevel@tonic-gate 	    "ehci_insert_bulk_req: bulk_pkt_size = %d", bulk_pkt_size);
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	/*
17247c478bd9Sstevel@tonic-gate 	 * Save current bulk request pointer and timeout values
17257c478bd9Sstevel@tonic-gate 	 * in transfer wrapper.
17267c478bd9Sstevel@tonic-gate 	 */
17277c478bd9Sstevel@tonic-gate 	tw->tw_curr_xfer_reqp = (usb_opaque_t)bulk_reqp;
17287c478bd9Sstevel@tonic-gate 	tw->tw_timeout = bulk_reqp->bulk_timeout;
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	/*
17317c478bd9Sstevel@tonic-gate 	 * Initialize the callback and any callback
17327c478bd9Sstevel@tonic-gate 	 * data required when the qtd completes.
17337c478bd9Sstevel@tonic-gate 	 */
17347c478bd9Sstevel@tonic-gate 	tw->tw_handle_qtd = ehci_handle_bulk_qtd;
17357c478bd9Sstevel@tonic-gate 	tw->tw_handle_callback_value = NULL;
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 	tw->tw_direction = (pipe_dir == USB_EP_DIR_OUT) ?
17387c478bd9Sstevel@tonic-gate 	    EHCI_QTD_CTRL_OUT_PID : EHCI_QTD_CTRL_IN_PID;
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	if (tw->tw_direction == EHCI_QTD_CTRL_OUT_PID) {
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 		ASSERT(bulk_reqp->bulk_data != NULL);
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 		bcopy(bulk_reqp->bulk_data->b_rptr, tw->tw_buf,
17457c478bd9Sstevel@tonic-gate 			bulk_reqp->bulk_len);
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 		Sync_IO_Buffer_for_device(tw->tw_dmahandle,
17487c478bd9Sstevel@tonic-gate 			bulk_reqp->bulk_len);
17497c478bd9Sstevel@tonic-gate 	}
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	ctrl = tw->tw_direction;
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	/* Insert all the bulk QTDs */
17547c478bd9Sstevel@tonic-gate 	for (count = 0; count < tw->tw_num_qtds; count++) {
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 		/* Check for last qtd */
17577c478bd9Sstevel@tonic-gate 		if (count == (tw->tw_num_qtds - 1)) {
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 			ctrl |= EHCI_QTD_CTRL_INTR_ON_COMPLETE;
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 			/* Check for inserting residue data */
17627c478bd9Sstevel@tonic-gate 			if (residue) {
17637c478bd9Sstevel@tonic-gate 				bulk_pkt_size = residue;
17647c478bd9Sstevel@tonic-gate 			}
17657c478bd9Sstevel@tonic-gate 		}
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 		/* Insert the QTD onto the endpoint */
17683304303fSsl 		(void) ehci_insert_qtd(ehcip, ctrl, len, bulk_pkt_size,
17693304303fSsl 		    0, pp, tw);
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 		len = len + bulk_pkt_size;
17727c478bd9Sstevel@tonic-gate 	}
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	/* Start the timer for this bulk transfer */
17757c478bd9Sstevel@tonic-gate 	ehci_start_xfer_timer(ehcip, pp, tw);
17767c478bd9Sstevel@tonic-gate }
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate /*
17807c478bd9Sstevel@tonic-gate  * ehci_start_periodic_pipe_polling:
17817c478bd9Sstevel@tonic-gate  *
17827c478bd9Sstevel@tonic-gate  * NOTE: This function is also called from POLLED MODE.
17837c478bd9Sstevel@tonic-gate  */
17847c478bd9Sstevel@tonic-gate int
17857c478bd9Sstevel@tonic-gate ehci_start_periodic_pipe_polling(
17867c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
17877c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
17887c478bd9Sstevel@tonic-gate 	usb_opaque_t		periodic_in_reqp,
17897c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
17907c478bd9Sstevel@tonic-gate {
17917c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
17927c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*eptd = &ph->p_ep;
17937c478bd9Sstevel@tonic-gate 	int			error = USB_SUCCESS;
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
17967c478bd9Sstevel@tonic-gate 	    "ehci_start_periodic_pipe_polling: ep%d",
17977c478bd9Sstevel@tonic-gate 	    ph->p_ep.bEndpointAddress & USB_EP_NUM_MASK);
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	/*
18027c478bd9Sstevel@tonic-gate 	 * Check and handle start polling on root hub interrupt pipe.
18037c478bd9Sstevel@tonic-gate 	 */
18047c478bd9Sstevel@tonic-gate 	if ((ph->p_usba_device->usb_addr == ROOT_HUB_ADDR) &&
18057c478bd9Sstevel@tonic-gate 	    ((eptd->bmAttributes & USB_EP_ATTR_MASK) ==
18067c478bd9Sstevel@tonic-gate 	    USB_EP_ATTR_INTR)) {
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 		error = ehci_handle_root_hub_pipe_start_intr_polling(ph,
18097c478bd9Sstevel@tonic-gate 		    (usb_intr_req_t *)periodic_in_reqp, flags);
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 		return (error);
18127c478bd9Sstevel@tonic-gate 	}
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	switch (pp->pp_state) {
18157c478bd9Sstevel@tonic-gate 	case EHCI_PIPE_STATE_IDLE:
18167c478bd9Sstevel@tonic-gate 		/* Save the Original client's Periodic IN request */
18177c478bd9Sstevel@tonic-gate 		pp->pp_client_periodic_in_reqp = periodic_in_reqp;
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 		/*
18207c478bd9Sstevel@tonic-gate 		 * This pipe is uninitialized or if a valid QTD is
18217c478bd9Sstevel@tonic-gate 		 * not found then insert a QTD on the interrupt IN
18227c478bd9Sstevel@tonic-gate 		 * endpoint.
18237c478bd9Sstevel@tonic-gate 		 */
18247c478bd9Sstevel@tonic-gate 		error = ehci_start_pipe_polling(ehcip, ph, flags);
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 		if (error != USB_SUCCESS) {
18277c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_INTR,
18287c478bd9Sstevel@tonic-gate 			    ehcip->ehci_log_hdl,
18297c478bd9Sstevel@tonic-gate 			    "ehci_start_periodic_pipe_polling: "
18307c478bd9Sstevel@tonic-gate 			    "Start polling failed");
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 			pp->pp_client_periodic_in_reqp = NULL;
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 			return (error);
18357c478bd9Sstevel@tonic-gate 		}
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
18387c478bd9Sstevel@tonic-gate 		    "ehci_start_periodic_pipe_polling: PP = 0x%p", pp);
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate #ifdef DEBUG
18417c478bd9Sstevel@tonic-gate 		switch (eptd->bmAttributes & USB_EP_ATTR_MASK) {
18427c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_INTR:
18437c478bd9Sstevel@tonic-gate 			ASSERT((pp->pp_tw_head != NULL) &&
18447c478bd9Sstevel@tonic-gate 			    (pp->pp_tw_tail != NULL));
18457c478bd9Sstevel@tonic-gate 			break;
18467c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_ISOCH:
18477c478bd9Sstevel@tonic-gate 			ASSERT((pp->pp_itw_head != NULL) &&
18487c478bd9Sstevel@tonic-gate 			    (pp->pp_itw_tail != NULL));
18497c478bd9Sstevel@tonic-gate 			break;
18507c478bd9Sstevel@tonic-gate 		}
18517c478bd9Sstevel@tonic-gate #endif
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 		break;
18547c478bd9Sstevel@tonic-gate 	case EHCI_PIPE_STATE_ACTIVE:
18557c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_INTR,
18567c478bd9Sstevel@tonic-gate 		    ehcip->ehci_log_hdl,
18577c478bd9Sstevel@tonic-gate 		    "ehci_start_periodic_pipe_polling: "
18587c478bd9Sstevel@tonic-gate 		    "Polling is already in progress");
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 		error = USB_FAILURE;
18617c478bd9Sstevel@tonic-gate 		break;
18627c478bd9Sstevel@tonic-gate 	case EHCI_PIPE_STATE_ERROR:
18637c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_INTR,
18647c478bd9Sstevel@tonic-gate 		    ehcip->ehci_log_hdl,
18657c478bd9Sstevel@tonic-gate 		    "ehci_start_periodic_pipe_polling: "
18667c478bd9Sstevel@tonic-gate 		    "Pipe is halted and perform reset"
18677c478bd9Sstevel@tonic-gate 		    "before restart polling");
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 		error = USB_FAILURE;
18707c478bd9Sstevel@tonic-gate 		break;
18717c478bd9Sstevel@tonic-gate 	default:
18727c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_INTR,
18737c478bd9Sstevel@tonic-gate 		    ehcip->ehci_log_hdl,
18747c478bd9Sstevel@tonic-gate 		    "ehci_start_periodic_pipe_polling: "
18757c478bd9Sstevel@tonic-gate 		    "Undefined state");
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 		error = USB_FAILURE;
18787c478bd9Sstevel@tonic-gate 		break;
18797c478bd9Sstevel@tonic-gate 	}
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	return (error);
18827c478bd9Sstevel@tonic-gate }
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate /*
18867c478bd9Sstevel@tonic-gate  * ehci_start_pipe_polling:
18877c478bd9Sstevel@tonic-gate  *
18887c478bd9Sstevel@tonic-gate  * Insert the number of periodic requests corresponding to polling
18897c478bd9Sstevel@tonic-gate  * interval as calculated during pipe open.
18907c478bd9Sstevel@tonic-gate  */
18917c478bd9Sstevel@tonic-gate static int
18927c478bd9Sstevel@tonic-gate ehci_start_pipe_polling(
18937c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
18947c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
18957c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
18967c478bd9Sstevel@tonic-gate {
18977c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
18987c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*eptd = &ph->p_ep;
18997c478bd9Sstevel@tonic-gate 	int			error = USB_FAILURE;
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
19027c478bd9Sstevel@tonic-gate 	    "ehci_start_pipe_polling:");
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	/*
19077c478bd9Sstevel@tonic-gate 	 * For the start polling, pp_max_periodic_req_cnt will be zero
19087c478bd9Sstevel@tonic-gate 	 * and for the restart polling request, it will be non zero.
19097c478bd9Sstevel@tonic-gate 	 *
19107c478bd9Sstevel@tonic-gate 	 * In case of start polling request, find out number of requests
19117c478bd9Sstevel@tonic-gate 	 * required for the Interrupt IN endpoints corresponding to the
19127c478bd9Sstevel@tonic-gate 	 * endpoint polling interval. For Isochronous IN endpoints, it is
19137c478bd9Sstevel@tonic-gate 	 * always fixed since its polling interval will be one ms.
19147c478bd9Sstevel@tonic-gate 	 */
19157c478bd9Sstevel@tonic-gate 	if (pp->pp_max_periodic_req_cnt == 0) {
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 		ehci_set_periodic_pipe_polling(ehcip, ph);
19187c478bd9Sstevel@tonic-gate 	}
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	ASSERT(pp->pp_max_periodic_req_cnt != 0);
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 	switch (eptd->bmAttributes & USB_EP_ATTR_MASK) {
19237c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_INTR:
19247c478bd9Sstevel@tonic-gate 		error = ehci_start_intr_polling(ehcip, ph, flags);
19257c478bd9Sstevel@tonic-gate 		break;
19267c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_ISOCH:
19277c478bd9Sstevel@tonic-gate 		error = ehci_start_isoc_polling(ehcip, ph, flags);
19287c478bd9Sstevel@tonic-gate 		break;
19297c478bd9Sstevel@tonic-gate 	}
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 	return (error);
19327c478bd9Sstevel@tonic-gate }
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate static int
19357c478bd9Sstevel@tonic-gate ehci_start_intr_polling(
19367c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
19377c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
19387c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
19397c478bd9Sstevel@tonic-gate {
19407c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
19417c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw_list, *tw;
19427c478bd9Sstevel@tonic-gate 	int			i, total_tws;
19437c478bd9Sstevel@tonic-gate 	int			error = USB_SUCCESS;
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 	/* Allocate all the necessary resources for the IN transfer */
19467c478bd9Sstevel@tonic-gate 	tw_list = NULL;
19477c478bd9Sstevel@tonic-gate 	total_tws = pp->pp_max_periodic_req_cnt - pp->pp_cur_periodic_req_cnt;
19487c478bd9Sstevel@tonic-gate 	for (i = 0; i < total_tws; i += 1) {
19497c478bd9Sstevel@tonic-gate 		tw = ehci_allocate_intr_resources(ehcip, ph, NULL, flags);
19507c478bd9Sstevel@tonic-gate 		if (tw == NULL) {
19517c478bd9Sstevel@tonic-gate 			error = USB_NO_RESOURCES;
19527c478bd9Sstevel@tonic-gate 			/* There are not enough resources, deallocate the TWs */
19537c478bd9Sstevel@tonic-gate 			tw = tw_list;
19547c478bd9Sstevel@tonic-gate 			while (tw != NULL) {
19557c478bd9Sstevel@tonic-gate 				tw_list = tw->tw_next;
19567c478bd9Sstevel@tonic-gate 				ehci_deallocate_intr_in_resource(
19577c478bd9Sstevel@tonic-gate 					ehcip, pp, tw);
19587c478bd9Sstevel@tonic-gate 				ehci_deallocate_tw(ehcip, pp, tw);
19597c478bd9Sstevel@tonic-gate 				tw = tw_list;
19607c478bd9Sstevel@tonic-gate 			}
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 			return (error);
19637c478bd9Sstevel@tonic-gate 		} else {
19647c478bd9Sstevel@tonic-gate 			if (tw_list == NULL) {
19657c478bd9Sstevel@tonic-gate 				tw_list = tw;
19667c478bd9Sstevel@tonic-gate 			}
19677c478bd9Sstevel@tonic-gate 		}
19687c478bd9Sstevel@tonic-gate 	}
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	while (pp->pp_cur_periodic_req_cnt < pp->pp_max_periodic_req_cnt) {
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
19737c478bd9Sstevel@tonic-gate 		    "ehci_start_pipe_polling: max = %d curr = %d tw = %p:",
19747c478bd9Sstevel@tonic-gate 		    pp->pp_max_periodic_req_cnt, pp->pp_cur_periodic_req_cnt,
19757c478bd9Sstevel@tonic-gate 		    tw_list);
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 		tw = tw_list;
19787c478bd9Sstevel@tonic-gate 		tw_list = tw->tw_next;
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 		ehci_insert_intr_req(ehcip, pp, tw, flags);
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 		pp->pp_cur_periodic_req_cnt++;
19837c478bd9Sstevel@tonic-gate 	}
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	return (error);
19867c478bd9Sstevel@tonic-gate }
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate /*
19907c478bd9Sstevel@tonic-gate  * ehci_set_periodic_pipe_polling:
19917c478bd9Sstevel@tonic-gate  *
19927c478bd9Sstevel@tonic-gate  * Calculate the number of periodic requests needed corresponding to the
19937c478bd9Sstevel@tonic-gate  * interrupt IN endpoints polling interval. Table below gives the number
19947c478bd9Sstevel@tonic-gate  * of periodic requests needed for the interrupt IN endpoints  according
19957c478bd9Sstevel@tonic-gate  * to endpoint polling interval.
19967c478bd9Sstevel@tonic-gate  *
19977c478bd9Sstevel@tonic-gate  * Polling interval		Number of periodic requests
19987c478bd9Sstevel@tonic-gate  *
19997c478bd9Sstevel@tonic-gate  * 1ms				4
20007c478bd9Sstevel@tonic-gate  * 2ms				2
20017c478bd9Sstevel@tonic-gate  * 4ms to 32ms			1
20027c478bd9Sstevel@tonic-gate  */
20037c478bd9Sstevel@tonic-gate static void
20047c478bd9Sstevel@tonic-gate ehci_set_periodic_pipe_polling(
20057c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
20067c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph)
20077c478bd9Sstevel@tonic-gate {
20087c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
20097c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*endpoint = &ph->p_ep;
20107c478bd9Sstevel@tonic-gate 	uchar_t			ep_attr = endpoint->bmAttributes;
20117c478bd9Sstevel@tonic-gate 	uint_t			interval;
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
20147c478bd9Sstevel@tonic-gate 	    "ehci_set_periodic_pipe_polling:");
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	pp->pp_cur_periodic_req_cnt = 0;
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	/*
20217c478bd9Sstevel@tonic-gate 	 * Check usb flag whether USB_FLAGS_ONE_TIME_POLL flag is
20227c478bd9Sstevel@tonic-gate 	 * set and if so, set pp->pp_max_periodic_req_cnt to one.
20237c478bd9Sstevel@tonic-gate 	 */
20247c478bd9Sstevel@tonic-gate 	if (((ep_attr & USB_EP_ATTR_MASK) == USB_EP_ATTR_INTR) &&
20257c478bd9Sstevel@tonic-gate 	    (pp->pp_client_periodic_in_reqp)) {
20267c478bd9Sstevel@tonic-gate 		usb_intr_req_t *intr_reqp = (usb_intr_req_t *)
20277c478bd9Sstevel@tonic-gate 					pp->pp_client_periodic_in_reqp;
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 		if (intr_reqp->intr_attributes &
20307c478bd9Sstevel@tonic-gate 		    USB_ATTRS_ONE_XFER) {
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 			pp->pp_max_periodic_req_cnt = EHCI_INTR_XMS_REQS;
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 			return;
20357c478bd9Sstevel@tonic-gate 		}
20367c478bd9Sstevel@tonic-gate 	}
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_usba_device->usb_mutex);
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	/*
20417c478bd9Sstevel@tonic-gate 	 * The ehci_adjust_polling_interval function will not fail
20427c478bd9Sstevel@tonic-gate 	 * at this instance since bandwidth allocation is already
20437c478bd9Sstevel@tonic-gate 	 * done. Here we are getting only the periodic interval.
20447c478bd9Sstevel@tonic-gate 	 */
20457c478bd9Sstevel@tonic-gate 	interval = ehci_adjust_polling_interval(ehcip, endpoint,
20467c478bd9Sstevel@tonic-gate 		ph->p_usba_device->usb_port_status);
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_usba_device->usb_mutex);
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 	switch (interval) {
20517c478bd9Sstevel@tonic-gate 	case EHCI_INTR_1MS_POLL:
20527c478bd9Sstevel@tonic-gate 		pp->pp_max_periodic_req_cnt = EHCI_INTR_1MS_REQS;
20537c478bd9Sstevel@tonic-gate 		break;
20547c478bd9Sstevel@tonic-gate 	case EHCI_INTR_2MS_POLL:
20557c478bd9Sstevel@tonic-gate 		pp->pp_max_periodic_req_cnt = EHCI_INTR_2MS_REQS;
20567c478bd9Sstevel@tonic-gate 		break;
20577c478bd9Sstevel@tonic-gate 	default:
20587c478bd9Sstevel@tonic-gate 		pp->pp_max_periodic_req_cnt = EHCI_INTR_XMS_REQS;
20597c478bd9Sstevel@tonic-gate 		break;
20607c478bd9Sstevel@tonic-gate 	}
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
20637c478bd9Sstevel@tonic-gate 	    "ehci_set_periodic_pipe_polling: Max periodic requests = %d",
20647c478bd9Sstevel@tonic-gate 	    pp->pp_max_periodic_req_cnt);
20657c478bd9Sstevel@tonic-gate }
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate /*
20687c478bd9Sstevel@tonic-gate  * ehci_allocate_intr_resources:
20697c478bd9Sstevel@tonic-gate  *
20707c478bd9Sstevel@tonic-gate  * Calculates the number of tds necessary for a intr transfer, and allocates
20717c478bd9Sstevel@tonic-gate  * all the necessary resources.
20727c478bd9Sstevel@tonic-gate  *
20737c478bd9Sstevel@tonic-gate  * Returns NULL if there is insufficient resources otherwise TW.
20747c478bd9Sstevel@tonic-gate  */
20757c478bd9Sstevel@tonic-gate ehci_trans_wrapper_t *
20767c478bd9Sstevel@tonic-gate ehci_allocate_intr_resources(
20777c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
20787c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
20797c478bd9Sstevel@tonic-gate 	usb_intr_req_t		*intr_reqp,
20807c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
20817c478bd9Sstevel@tonic-gate {
20827c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
20837c478bd9Sstevel@tonic-gate 	int			pipe_dir;
20847c478bd9Sstevel@tonic-gate 	size_t			qtd_count = 1;
20857c478bd9Sstevel@tonic-gate 	size_t			tw_length;
20867c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw;
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
20897c478bd9Sstevel@tonic-gate 	    "ehci_allocate_intr_resources:");
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	pipe_dir = ph->p_ep.bEndpointAddress & USB_EP_DIR_MASK;
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 	/* Get the length of interrupt transfer & alloc data */
20967c478bd9Sstevel@tonic-gate 	if (intr_reqp) {
20977c478bd9Sstevel@tonic-gate 		tw_length = intr_reqp->intr_len;
20987c478bd9Sstevel@tonic-gate 	} else {
20997c478bd9Sstevel@tonic-gate 		ASSERT(pipe_dir == USB_EP_DIR_IN);
21007c478bd9Sstevel@tonic-gate 		tw_length = (pp->pp_client_periodic_in_reqp) ?
21017c478bd9Sstevel@tonic-gate 		    (((usb_intr_req_t *)pp->
21027c478bd9Sstevel@tonic-gate 		    pp_client_periodic_in_reqp)->intr_len) :
21037c478bd9Sstevel@tonic-gate 		    ph->p_ep.wMaxPacketSize;
21047c478bd9Sstevel@tonic-gate 	}
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	/* Check the size of interrupt request */
21077c478bd9Sstevel@tonic-gate 	if (tw_length > EHCI_MAX_QTD_XFER_SIZE) {
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
21107c478bd9Sstevel@tonic-gate 		    "ehci_allocate_intr_resources: Intr request size 0x%lx is "
21117c478bd9Sstevel@tonic-gate 		    "more than 0x%x", tw_length, EHCI_MAX_QTD_XFER_SIZE);
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 		return (NULL);
21147c478bd9Sstevel@tonic-gate 	}
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 	if ((tw = ehci_allocate_tw_resources(ehcip, pp, tw_length, flags,
21177c478bd9Sstevel@tonic-gate 	    qtd_count)) == NULL) {
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate 		return (NULL);
21207c478bd9Sstevel@tonic-gate 	}
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 	if (pipe_dir == USB_EP_DIR_IN) {
21237c478bd9Sstevel@tonic-gate 		if (ehci_allocate_intr_in_resource(ehcip, pp, tw, flags) !=
21247c478bd9Sstevel@tonic-gate 		    USB_SUCCESS) {
21257c478bd9Sstevel@tonic-gate 			ehci_deallocate_tw(ehcip, pp, tw);
21267c478bd9Sstevel@tonic-gate 		}
21277c478bd9Sstevel@tonic-gate 		tw->tw_direction = EHCI_QTD_CTRL_IN_PID;
21287c478bd9Sstevel@tonic-gate 	} else {
21297c478bd9Sstevel@tonic-gate 		ASSERT(intr_reqp->intr_data != NULL);
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 		/* Copy the data into the buffer */
21327c478bd9Sstevel@tonic-gate 		bcopy(intr_reqp->intr_data->b_rptr, tw->tw_buf,
21337c478bd9Sstevel@tonic-gate 		    intr_reqp->intr_len);
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 		Sync_IO_Buffer_for_device(tw->tw_dmahandle,
21367c478bd9Sstevel@tonic-gate 		    intr_reqp->intr_len);
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 		tw->tw_curr_xfer_reqp = (usb_opaque_t)intr_reqp;
21397c478bd9Sstevel@tonic-gate 		tw->tw_direction = EHCI_QTD_CTRL_OUT_PID;
21407c478bd9Sstevel@tonic-gate 	}
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 	if (intr_reqp) {
21437c478bd9Sstevel@tonic-gate 		tw->tw_timeout = intr_reqp->intr_timeout;
21447c478bd9Sstevel@tonic-gate 	}
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 	/*
21477c478bd9Sstevel@tonic-gate 	 * Initialize the callback and any callback
21487c478bd9Sstevel@tonic-gate 	 * data required when the qtd completes.
21497c478bd9Sstevel@tonic-gate 	 */
21507c478bd9Sstevel@tonic-gate 	tw->tw_handle_qtd = ehci_handle_intr_qtd;
21517c478bd9Sstevel@tonic-gate 	tw->tw_handle_callback_value = NULL;
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 	return (tw);
21547c478bd9Sstevel@tonic-gate }
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate /*
21587c478bd9Sstevel@tonic-gate  * ehci_insert_intr_req:
21597c478bd9Sstevel@tonic-gate  *
21607c478bd9Sstevel@tonic-gate  * Insert an Interrupt request into the Host Controller's periodic list.
21617c478bd9Sstevel@tonic-gate  */
21627c478bd9Sstevel@tonic-gate /* ARGSUSED */
21637c478bd9Sstevel@tonic-gate void
21647c478bd9Sstevel@tonic-gate ehci_insert_intr_req(
21657c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
21667c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
21677c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw,
21687c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
21697c478bd9Sstevel@tonic-gate {
21707c478bd9Sstevel@tonic-gate 	uint_t			ctrl = 0;
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	ASSERT(tw->tw_curr_xfer_reqp != NULL);
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate 	ctrl = (tw->tw_direction | EHCI_QTD_CTRL_INTR_ON_COMPLETE);
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 	/* Insert another interrupt QTD */
21793304303fSsl 	(void) ehci_insert_qtd(ehcip, ctrl, 0, tw->tw_length, 0, pp, tw);
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 	/* Start the timer for this Interrupt transfer */
21827c478bd9Sstevel@tonic-gate 	ehci_start_xfer_timer(ehcip, pp, tw);
21837c478bd9Sstevel@tonic-gate }
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate /*
21877c478bd9Sstevel@tonic-gate  * ehci_stop_periodic_pipe_polling:
21887c478bd9Sstevel@tonic-gate  */
21897c478bd9Sstevel@tonic-gate /* ARGSUSED */
21907c478bd9Sstevel@tonic-gate int
21917c478bd9Sstevel@tonic-gate ehci_stop_periodic_pipe_polling(
21927c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
21937c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
21947c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
21957c478bd9Sstevel@tonic-gate {
21967c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
21977c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*eptd = &ph->p_ep;
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
22007c478bd9Sstevel@tonic-gate 	    "ehci_stop_periodic_pipe_polling: Flags = 0x%x", flags);
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 	/*
22057c478bd9Sstevel@tonic-gate 	 * Check and handle stop polling on root hub interrupt pipe.
22067c478bd9Sstevel@tonic-gate 	 */
22077c478bd9Sstevel@tonic-gate 	if ((ph->p_usba_device->usb_addr == ROOT_HUB_ADDR) &&
22087c478bd9Sstevel@tonic-gate 	    ((eptd->bmAttributes & USB_EP_ATTR_MASK) ==
22097c478bd9Sstevel@tonic-gate 	    USB_EP_ATTR_INTR)) {
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate 		ehci_handle_root_hub_pipe_stop_intr_polling(ph, flags);
22127c478bd9Sstevel@tonic-gate 
22137c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
22147c478bd9Sstevel@tonic-gate 	}
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	if (pp->pp_state != EHCI_PIPE_STATE_ACTIVE) {
22177c478bd9Sstevel@tonic-gate 
22187c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
22197c478bd9Sstevel@tonic-gate 		    "ehci_stop_periodic_pipe_polling: "
22207c478bd9Sstevel@tonic-gate 		    "Polling already stopped");
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
22237c478bd9Sstevel@tonic-gate 	}
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 	/* Set pipe state to pipe stop polling */
22267c478bd9Sstevel@tonic-gate 	pp->pp_state = EHCI_PIPE_STATE_STOP_POLLING;
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 	ehci_pipe_cleanup(ehcip, ph);
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
22317c478bd9Sstevel@tonic-gate }
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate /*
22357c478bd9Sstevel@tonic-gate  * ehci_insert_qtd:
22367c478bd9Sstevel@tonic-gate  *
22377c478bd9Sstevel@tonic-gate  * Insert a Transfer Descriptor (QTD) on an Endpoint Descriptor (QH).
22387c478bd9Sstevel@tonic-gate  * Always returns USB_SUCCESS for now.	Once Isoch has been implemented,
22397c478bd9Sstevel@tonic-gate  * it may return USB_FAILURE.
22407c478bd9Sstevel@tonic-gate  */
22417c478bd9Sstevel@tonic-gate int
22427c478bd9Sstevel@tonic-gate ehci_insert_qtd(
22437c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
22447c478bd9Sstevel@tonic-gate 	uint32_t		qtd_ctrl,
22453304303fSsl 	size_t			qtd_dma_offs,
22467c478bd9Sstevel@tonic-gate 	size_t			qtd_length,
22477c478bd9Sstevel@tonic-gate 	uint32_t		qtd_ctrl_phase,
22487c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
22497c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw)
22507c478bd9Sstevel@tonic-gate {
22517c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*curr_dummy_qtd, *next_dummy_qtd;
22527c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*new_dummy_qtd;
22537c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh = pp->pp_qh;
22547c478bd9Sstevel@tonic-gate 	int			error = USB_SUCCESS;
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate 	/* Allocate new dummy QTD */
22597c478bd9Sstevel@tonic-gate 	new_dummy_qtd = tw->tw_qtd_free_list;
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 	ASSERT(new_dummy_qtd != NULL);
22627c478bd9Sstevel@tonic-gate 	tw->tw_qtd_free_list = ehci_qtd_iommu_to_cpu(ehcip,
22637c478bd9Sstevel@tonic-gate 	    Get_QTD(new_dummy_qtd->qtd_tw_next_qtd));
22647c478bd9Sstevel@tonic-gate 	Set_QTD(new_dummy_qtd->qtd_tw_next_qtd, NULL);
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 	/* Get the current and next dummy QTDs */
22677c478bd9Sstevel@tonic-gate 	curr_dummy_qtd = ehci_qtd_iommu_to_cpu(ehcip,
22687c478bd9Sstevel@tonic-gate 	    Get_QH(qh->qh_dummy_qtd));
22697c478bd9Sstevel@tonic-gate 	next_dummy_qtd = ehci_qtd_iommu_to_cpu(ehcip,
22707c478bd9Sstevel@tonic-gate 	    Get_QTD(curr_dummy_qtd->qtd_next_qtd));
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	/* Update QH's dummy qtd field */
22737c478bd9Sstevel@tonic-gate 	Set_QH(qh->qh_dummy_qtd, ehci_qtd_cpu_to_iommu(ehcip, next_dummy_qtd));
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 	/* Update next dummy's next qtd pointer */
22767c478bd9Sstevel@tonic-gate 	Set_QTD(next_dummy_qtd->qtd_next_qtd,
22777c478bd9Sstevel@tonic-gate 	    ehci_qtd_cpu_to_iommu(ehcip, new_dummy_qtd));
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 	/*
22807c478bd9Sstevel@tonic-gate 	 * Fill in the current dummy qtd and
22817c478bd9Sstevel@tonic-gate 	 * add the new dummy to the end.
22827c478bd9Sstevel@tonic-gate 	 */
22837c478bd9Sstevel@tonic-gate 	ehci_fill_in_qtd(ehcip, curr_dummy_qtd, qtd_ctrl,
22843304303fSsl 	    qtd_dma_offs, qtd_length, qtd_ctrl_phase, pp, tw);
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 	/* Insert this qtd onto the tw */
22877c478bd9Sstevel@tonic-gate 	ehci_insert_qtd_on_tw(ehcip, tw, curr_dummy_qtd);
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 	/*
22907c478bd9Sstevel@tonic-gate 	 * Insert this qtd onto active qtd list.
22917c478bd9Sstevel@tonic-gate 	 * Don't insert polled mode qtd here.
22927c478bd9Sstevel@tonic-gate 	 */
22937c478bd9Sstevel@tonic-gate 	if (pp->pp_flag != EHCI_POLLED_MODE_FLAG) {
22947c478bd9Sstevel@tonic-gate 		/* Insert this qtd onto active qtd list */
22957c478bd9Sstevel@tonic-gate 		ehci_insert_qtd_into_active_qtd_list(ehcip, curr_dummy_qtd);
22967c478bd9Sstevel@tonic-gate 	}
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	/* Print qh and qtd */
22997c478bd9Sstevel@tonic-gate 	ehci_print_qh(ehcip, qh);
23007c478bd9Sstevel@tonic-gate 	ehci_print_qtd(ehcip, curr_dummy_qtd);
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate 	return (error);
23037c478bd9Sstevel@tonic-gate }
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate /*
23077c478bd9Sstevel@tonic-gate  * ehci_allocate_qtd_from_pool:
23087c478bd9Sstevel@tonic-gate  *
23097c478bd9Sstevel@tonic-gate  * Allocate a Transfer Descriptor (QTD) from the QTD buffer pool.
23107c478bd9Sstevel@tonic-gate  */
23117c478bd9Sstevel@tonic-gate static ehci_qtd_t *
23127c478bd9Sstevel@tonic-gate ehci_allocate_qtd_from_pool(ehci_state_t	*ehcip)
23137c478bd9Sstevel@tonic-gate {
23147c478bd9Sstevel@tonic-gate 	int		i, ctrl;
23157c478bd9Sstevel@tonic-gate 	ehci_qtd_t	*qtd;
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	/*
23207c478bd9Sstevel@tonic-gate 	 * Search for a blank Transfer Descriptor (QTD)
23217c478bd9Sstevel@tonic-gate 	 * in the QTD buffer pool.
23227c478bd9Sstevel@tonic-gate 	 */
23237c478bd9Sstevel@tonic-gate 	for (i = 0; i < ehci_qtd_pool_size; i ++) {
23247c478bd9Sstevel@tonic-gate 		ctrl = Get_QTD(ehcip->ehci_qtd_pool_addr[i].qtd_state);
23257c478bd9Sstevel@tonic-gate 		if (ctrl == EHCI_QTD_FREE) {
23267c478bd9Sstevel@tonic-gate 			break;
23277c478bd9Sstevel@tonic-gate 		}
23287c478bd9Sstevel@tonic-gate 	}
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 	if (i >= ehci_qtd_pool_size) {
23317c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
23327c478bd9Sstevel@tonic-gate 		    "ehci_allocate_qtd_from_pool: QTD exhausted");
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 		return (NULL);
23357c478bd9Sstevel@tonic-gate 	}
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
23387c478bd9Sstevel@tonic-gate 	    "ehci_allocate_qtd_from_pool: Allocated %d", i);
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 	/* Create a new dummy for the end of the QTD list */
23417c478bd9Sstevel@tonic-gate 	qtd = &ehcip->ehci_qtd_pool_addr[i];
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
23447c478bd9Sstevel@tonic-gate 	    "ehci_allocate_qtd_from_pool: qtd 0x%p", (void *)qtd);
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 	/* Mark the newly allocated QTD as a dummy */
23477c478bd9Sstevel@tonic-gate 	Set_QTD(qtd->qtd_state, EHCI_QTD_DUMMY);
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 	/* Mark the status of this new QTD to halted state */
23507c478bd9Sstevel@tonic-gate 	Set_QTD(qtd->qtd_ctrl, EHCI_QTD_CTRL_HALTED_XACT);
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 	/* Disable dummy QTD's next and alternate next pointers */
23537c478bd9Sstevel@tonic-gate 	Set_QTD(qtd->qtd_next_qtd, EHCI_QTD_NEXT_QTD_PTR_VALID);
23547c478bd9Sstevel@tonic-gate 	Set_QTD(qtd->qtd_alt_next_qtd, EHCI_QTD_ALT_NEXT_QTD_PTR_VALID);
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate 	return (qtd);
23577c478bd9Sstevel@tonic-gate }
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate /*
23617c478bd9Sstevel@tonic-gate  * ehci_fill_in_qtd:
23627c478bd9Sstevel@tonic-gate  *
23637c478bd9Sstevel@tonic-gate  * Fill in the fields of a Transfer Descriptor (QTD).
23643304303fSsl  * The "Buffer Pointer" fields of a QTD are retrieved from the TW
23653304303fSsl  * it is associated with.
23663304303fSsl  *
23673304303fSsl  * Note:
23683304303fSsl  * qtd_dma_offs - the starting offset into the TW buffer, where the QTD
23693304303fSsl  *                should transfer from. It should be 4K aligned. And when
23703304303fSsl  *                a TW has more than one QTDs, the QTDs must be filled in
23713304303fSsl  *                increasing order.
23723304303fSsl  * qtd_length - the total bytes to transfer.
23737c478bd9Sstevel@tonic-gate  */
23747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23757c478bd9Sstevel@tonic-gate static void
23767c478bd9Sstevel@tonic-gate ehci_fill_in_qtd(
23777c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
23787c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd,
23797c478bd9Sstevel@tonic-gate 	uint32_t		qtd_ctrl,
23803304303fSsl 	size_t			qtd_dma_offs,
23817c478bd9Sstevel@tonic-gate 	size_t			qtd_length,
23827c478bd9Sstevel@tonic-gate 	uint32_t		qtd_ctrl_phase,
23837c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
23847c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw)
23857c478bd9Sstevel@tonic-gate {
23863304303fSsl 	uint32_t		buf_addr;
23877c478bd9Sstevel@tonic-gate 	size_t			buf_len = qtd_length;
23887c478bd9Sstevel@tonic-gate 	uint32_t		ctrl = qtd_ctrl;
23897c478bd9Sstevel@tonic-gate 	uint_t			i = 0;
23903304303fSsl 	int			rem_len;
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
23933304303fSsl 	    "ehci_fill_in_qtd: qtd 0x%p ctrl 0x%x bufoffs 0x%lx "
23943304303fSsl 	    "len 0x%lx", qtd, qtd_ctrl, qtd_dma_offs, qtd_length);
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 	/* Assert that the qtd to be filled in is a dummy */
23977c478bd9Sstevel@tonic-gate 	ASSERT(Get_QTD(qtd->qtd_state) == EHCI_QTD_DUMMY);
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 	/* Change QTD's state Active */
24007c478bd9Sstevel@tonic-gate 	Set_QTD(qtd->qtd_state, EHCI_QTD_ACTIVE);
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate 	/* Set the total length data transfer */
24037c478bd9Sstevel@tonic-gate 	ctrl |= (((qtd_length << EHCI_QTD_CTRL_BYTES_TO_XFER_SHIFT)
24047c478bd9Sstevel@tonic-gate 	    & EHCI_QTD_CTRL_BYTES_TO_XFER) | EHCI_QTD_CTRL_MAX_ERR_COUNTS);
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	/*
24073304303fSsl 	 * QTDs must be filled in increasing DMA offset order.
24083304303fSsl 	 * tw_dma_offs is initialized to be 0 at TW creation and
24093304303fSsl 	 * is only increased in this function.
24103304303fSsl 	 */
24113304303fSsl 	ASSERT(buf_len == 0 || qtd_dma_offs >= tw->tw_dma_offs);
24123304303fSsl 
24133304303fSsl 	/*
24143304303fSsl 	 * Save the starting dma buffer offset used and
24157c478bd9Sstevel@tonic-gate 	 * length of data that will be transfered in
24167c478bd9Sstevel@tonic-gate 	 * the current QTD.
24177c478bd9Sstevel@tonic-gate 	 */
24183304303fSsl 	Set_QTD(qtd->qtd_xfer_offs, qtd_dma_offs);
24197c478bd9Sstevel@tonic-gate 	Set_QTD(qtd->qtd_xfer_len, buf_len);
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 	while (buf_len) {
24223304303fSsl 		/*
24233304303fSsl 		 * Advance to the next DMA cookie until finding the cookie
24243304303fSsl 		 * that qtd_dma_offs falls in.
24253304303fSsl 		 * It is very likely this loop will never repeat more than
24263304303fSsl 		 * once. It is here just to accommodate the case qtd_dma_offs
24273304303fSsl 		 * is increased by multiple cookies during two consecutive
24283304303fSsl 		 * calls into this function. In that case, the interim DMA
24293304303fSsl 		 * buffer is allowed to be skipped.
24303304303fSsl 		 */
24313304303fSsl 		while ((tw->tw_dma_offs + tw->tw_cookie.dmac_size) <=
24323304303fSsl 		    qtd_dma_offs) {
24333304303fSsl 			/*
24343304303fSsl 			 * tw_dma_offs always points to the starting offset
24353304303fSsl 			 * of a cookie
24363304303fSsl 			 */
24373304303fSsl 			tw->tw_dma_offs += tw->tw_cookie.dmac_size;
24383304303fSsl 			ddi_dma_nextcookie(tw->tw_dmahandle, &tw->tw_cookie);
24393304303fSsl 			tw->tw_cookie_idx++;
24403304303fSsl 			ASSERT(tw->tw_cookie_idx < tw->tw_ncookies);
24413304303fSsl 		}
24423304303fSsl 
24433304303fSsl 		/*
24443304303fSsl 		 * Counting the remained buffer length to be filled in
24453304303fSsl 		 * the QTD for current DMA cookie
24463304303fSsl 		 */
24473304303fSsl 		rem_len = (tw->tw_dma_offs + tw->tw_cookie.dmac_size) -
24483304303fSsl 		    qtd_dma_offs;
24493304303fSsl 
24507c478bd9Sstevel@tonic-gate 		/* Update the beginning of the buffer */
24513304303fSsl 		buf_addr = (qtd_dma_offs - tw->tw_dma_offs) +
24523304303fSsl 		    tw->tw_cookie.dmac_address;
24533304303fSsl 		ASSERT((buf_addr % EHCI_4K_ALIGN) == 0);
24547c478bd9Sstevel@tonic-gate 		Set_QTD(qtd->qtd_buf[i], buf_addr);
24557c478bd9Sstevel@tonic-gate 
24563304303fSsl 		USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
24573304303fSsl 		    "ehci_fill_in_qtd: dmac_addr 0x%p dmac_size "
24583304303fSsl 		    "0x%lx idx %d", buf_addr, tw->tw_cookie.dmac_size,
24593304303fSsl 		    tw->tw_cookie_idx);
24603304303fSsl 
24617c478bd9Sstevel@tonic-gate 		if (buf_len <= EHCI_MAX_QTD_BUF_SIZE) {
24623304303fSsl 			ASSERT(buf_len <= rem_len);
24637c478bd9Sstevel@tonic-gate 			break;
24647c478bd9Sstevel@tonic-gate 		} else {
24653304303fSsl 			ASSERT(rem_len >= EHCI_MAX_QTD_BUF_SIZE);
24667c478bd9Sstevel@tonic-gate 			buf_len -= EHCI_MAX_QTD_BUF_SIZE;
24673304303fSsl 			qtd_dma_offs += EHCI_MAX_QTD_BUF_SIZE;
24687c478bd9Sstevel@tonic-gate 		}
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate 		i++;
24717c478bd9Sstevel@tonic-gate 	}
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 	/*
24747c478bd9Sstevel@tonic-gate 	 * Setup the alternate next qTD pointer if appropriate.  The alternate
24757c478bd9Sstevel@tonic-gate 	 * qtd is currently pointing to a QTD that is not yet linked, but will
24767c478bd9Sstevel@tonic-gate 	 * be in the very near future.	If a short_xfer occurs in this
24777c478bd9Sstevel@tonic-gate 	 * situation , the HC will automatically skip this QH.	Eventually
24787c478bd9Sstevel@tonic-gate 	 * everything will be placed and the alternate_qtd will be valid QTD.
24797c478bd9Sstevel@tonic-gate 	 * For more information on alternate qtds look at section 3.5.2 in the
24807c478bd9Sstevel@tonic-gate 	 * EHCI spec.
24817c478bd9Sstevel@tonic-gate 	 */
24827c478bd9Sstevel@tonic-gate 	if (tw->tw_alt_qtd != NULL) {
24837c478bd9Sstevel@tonic-gate 		Set_QTD(qtd->qtd_alt_next_qtd,
24847c478bd9Sstevel@tonic-gate 		    (ehci_qtd_cpu_to_iommu(ehcip, tw->tw_alt_qtd) &
24857c478bd9Sstevel@tonic-gate 		    EHCI_QTD_ALT_NEXT_QTD_PTR));
24867c478bd9Sstevel@tonic-gate 	}
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate 	/*
24897c478bd9Sstevel@tonic-gate 	 * For control, bulk and interrupt QTD, now
24907c478bd9Sstevel@tonic-gate 	 * enable current QTD by setting active bit.
24917c478bd9Sstevel@tonic-gate 	 */
24927c478bd9Sstevel@tonic-gate 	Set_QTD(qtd->qtd_ctrl, (ctrl | EHCI_QTD_CTRL_ACTIVE_XACT));
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate 	/*
24957c478bd9Sstevel@tonic-gate 	 * For Control Xfer, qtd_ctrl_phase is a valid filed.
24967c478bd9Sstevel@tonic-gate 	 */
24977c478bd9Sstevel@tonic-gate 	if (qtd_ctrl_phase) {
24987c478bd9Sstevel@tonic-gate 		Set_QTD(qtd->qtd_ctrl_phase, qtd_ctrl_phase);
24997c478bd9Sstevel@tonic-gate 	}
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 	/* Set the transfer wrapper */
25027c478bd9Sstevel@tonic-gate 	ASSERT(tw != NULL);
25037c478bd9Sstevel@tonic-gate 	ASSERT(tw->tw_id != NULL);
25047c478bd9Sstevel@tonic-gate 
25057c478bd9Sstevel@tonic-gate 	Set_QTD(qtd->qtd_trans_wrapper, (uint32_t)tw->tw_id);
25067c478bd9Sstevel@tonic-gate }
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate /*
25107c478bd9Sstevel@tonic-gate  * ehci_insert_qtd_on_tw:
25117c478bd9Sstevel@tonic-gate  *
25127c478bd9Sstevel@tonic-gate  * The transfer wrapper keeps a list of all Transfer Descriptors (QTD) that
25137c478bd9Sstevel@tonic-gate  * are allocated for this transfer. Insert a QTD  onto this list. The  list
25147c478bd9Sstevel@tonic-gate  * of QTD's does not include the dummy QTD that is at the end of the list of
25157c478bd9Sstevel@tonic-gate  * QTD's for the endpoint.
25167c478bd9Sstevel@tonic-gate  */
25177c478bd9Sstevel@tonic-gate static void
25187c478bd9Sstevel@tonic-gate ehci_insert_qtd_on_tw(
25197c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
25207c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw,
25217c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd)
25227c478bd9Sstevel@tonic-gate {
25237c478bd9Sstevel@tonic-gate 	/*
25247c478bd9Sstevel@tonic-gate 	 * Set the next pointer to NULL because
25257c478bd9Sstevel@tonic-gate 	 * this is the last QTD on list.
25267c478bd9Sstevel@tonic-gate 	 */
25277c478bd9Sstevel@tonic-gate 	Set_QTD(qtd->qtd_tw_next_qtd, NULL);
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate 	if (tw->tw_qtd_head == NULL) {
25307c478bd9Sstevel@tonic-gate 		ASSERT(tw->tw_qtd_tail == NULL);
25317c478bd9Sstevel@tonic-gate 		tw->tw_qtd_head = qtd;
25327c478bd9Sstevel@tonic-gate 		tw->tw_qtd_tail = qtd;
25337c478bd9Sstevel@tonic-gate 	} else {
25347c478bd9Sstevel@tonic-gate 		ehci_qtd_t *dummy = (ehci_qtd_t *)tw->tw_qtd_tail;
25357c478bd9Sstevel@tonic-gate 
25367c478bd9Sstevel@tonic-gate 		ASSERT(dummy != NULL);
25377c478bd9Sstevel@tonic-gate 		ASSERT(dummy != qtd);
25387c478bd9Sstevel@tonic-gate 		ASSERT(Get_QTD(qtd->qtd_state) != EHCI_QTD_DUMMY);
25397c478bd9Sstevel@tonic-gate 
25407c478bd9Sstevel@tonic-gate 		/* Add the qtd to the end of the list */
25417c478bd9Sstevel@tonic-gate 		Set_QTD(dummy->qtd_tw_next_qtd,
25427c478bd9Sstevel@tonic-gate 		    ehci_qtd_cpu_to_iommu(ehcip, qtd));
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate 		tw->tw_qtd_tail = qtd;
25457c478bd9Sstevel@tonic-gate 
25467c478bd9Sstevel@tonic-gate 		ASSERT(Get_QTD(qtd->qtd_tw_next_qtd) == NULL);
25477c478bd9Sstevel@tonic-gate 	}
25487c478bd9Sstevel@tonic-gate }
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate /*
25527c478bd9Sstevel@tonic-gate  * ehci_insert_qtd_into_active_qtd_list:
25537c478bd9Sstevel@tonic-gate  *
25547c478bd9Sstevel@tonic-gate  * Insert current QTD into active QTD list.
25557c478bd9Sstevel@tonic-gate  */
25567c478bd9Sstevel@tonic-gate static void
25577c478bd9Sstevel@tonic-gate ehci_insert_qtd_into_active_qtd_list(
25587c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
25597c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd)
25607c478bd9Sstevel@tonic-gate {
25617c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*curr_qtd, *next_qtd;
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 	curr_qtd = ehcip->ehci_active_qtd_list;
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 	/* Insert this QTD into QTD Active List */
25687c478bd9Sstevel@tonic-gate 	if (curr_qtd) {
25697c478bd9Sstevel@tonic-gate 		next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
25707c478bd9Sstevel@tonic-gate 		    Get_QTD(curr_qtd->qtd_active_qtd_next));
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate 		while (next_qtd) {
25737c478bd9Sstevel@tonic-gate 			curr_qtd = next_qtd;
25747c478bd9Sstevel@tonic-gate 			next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
25757c478bd9Sstevel@tonic-gate 			    Get_QTD(curr_qtd->qtd_active_qtd_next));
25767c478bd9Sstevel@tonic-gate 		}
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate 		Set_QTD(qtd->qtd_active_qtd_prev,
25797c478bd9Sstevel@tonic-gate 		    ehci_qtd_cpu_to_iommu(ehcip, curr_qtd));
25807c478bd9Sstevel@tonic-gate 
25817c478bd9Sstevel@tonic-gate 		Set_QTD(curr_qtd->qtd_active_qtd_next,
25827c478bd9Sstevel@tonic-gate 		    ehci_qtd_cpu_to_iommu(ehcip, qtd));
25837c478bd9Sstevel@tonic-gate 	} else {
25847c478bd9Sstevel@tonic-gate 		ehcip->ehci_active_qtd_list = qtd;
25857c478bd9Sstevel@tonic-gate 		Set_QTD(qtd->qtd_active_qtd_next, NULL);
25867c478bd9Sstevel@tonic-gate 		Set_QTD(qtd->qtd_active_qtd_prev, NULL);
25877c478bd9Sstevel@tonic-gate 	}
25887c478bd9Sstevel@tonic-gate }
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 
25917c478bd9Sstevel@tonic-gate /*
25927c478bd9Sstevel@tonic-gate  * ehci_remove_qtd_from_active_qtd_list:
25937c478bd9Sstevel@tonic-gate  *
25947c478bd9Sstevel@tonic-gate  * Remove current QTD from the active QTD list.
25957c478bd9Sstevel@tonic-gate  *
25967c478bd9Sstevel@tonic-gate  * NOTE: This function is also called from POLLED MODE.
25977c478bd9Sstevel@tonic-gate  */
25987c478bd9Sstevel@tonic-gate void
25997c478bd9Sstevel@tonic-gate ehci_remove_qtd_from_active_qtd_list(
26007c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
26017c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd)
26027c478bd9Sstevel@tonic-gate {
26037c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*curr_qtd, *prev_qtd, *next_qtd;
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 	ASSERT(qtd != NULL);
26067c478bd9Sstevel@tonic-gate 
26077c478bd9Sstevel@tonic-gate 	curr_qtd = ehcip->ehci_active_qtd_list;
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 	while ((curr_qtd) && (curr_qtd != qtd)) {
26107c478bd9Sstevel@tonic-gate 		curr_qtd = ehci_qtd_iommu_to_cpu(ehcip,
26117c478bd9Sstevel@tonic-gate 		    Get_QTD(curr_qtd->qtd_active_qtd_next));
26127c478bd9Sstevel@tonic-gate 	}
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate 	if ((curr_qtd) && (curr_qtd == qtd)) {
26157c478bd9Sstevel@tonic-gate 		prev_qtd = ehci_qtd_iommu_to_cpu(ehcip,
26167c478bd9Sstevel@tonic-gate 		    Get_QTD(curr_qtd->qtd_active_qtd_prev));
26177c478bd9Sstevel@tonic-gate 		next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
26187c478bd9Sstevel@tonic-gate 		    Get_QTD(curr_qtd->qtd_active_qtd_next));
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 		if (prev_qtd) {
26217c478bd9Sstevel@tonic-gate 			Set_QTD(prev_qtd->qtd_active_qtd_next,
26227c478bd9Sstevel@tonic-gate 			    Get_QTD(curr_qtd->qtd_active_qtd_next));
26237c478bd9Sstevel@tonic-gate 		} else {
26247c478bd9Sstevel@tonic-gate 			ehcip->ehci_active_qtd_list = next_qtd;
26257c478bd9Sstevel@tonic-gate 		}
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 		if (next_qtd) {
26287c478bd9Sstevel@tonic-gate 			Set_QTD(next_qtd->qtd_active_qtd_prev,
26297c478bd9Sstevel@tonic-gate 			    Get_QTD(curr_qtd->qtd_active_qtd_prev));
26307c478bd9Sstevel@tonic-gate 		}
26317c478bd9Sstevel@tonic-gate 	} else {
26327c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
26337c478bd9Sstevel@tonic-gate 		    "ehci_remove_qtd_from_active_qtd_list: "
26347c478bd9Sstevel@tonic-gate 			"Unable to find QTD in active_qtd_list");
26357c478bd9Sstevel@tonic-gate 	}
26367c478bd9Sstevel@tonic-gate }
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 
26397c478bd9Sstevel@tonic-gate /*
26407c478bd9Sstevel@tonic-gate  * ehci_traverse_qtds:
26417c478bd9Sstevel@tonic-gate  *
26427c478bd9Sstevel@tonic-gate  * Traverse the list of QTDs for given pipe using transfer wrapper.  Since
26437c478bd9Sstevel@tonic-gate  * the endpoint is marked as Halted, the Host Controller (HC) is no longer
26447c478bd9Sstevel@tonic-gate  * accessing these QTDs. Remove all the QTDs that are attached to endpoint.
26457c478bd9Sstevel@tonic-gate  */
26467c478bd9Sstevel@tonic-gate static void
26477c478bd9Sstevel@tonic-gate ehci_traverse_qtds(
26487c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
26497c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph)
26507c478bd9Sstevel@tonic-gate {
26517c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
26527c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*next_tw;
26537c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd;
26547c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*next_qtd;
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
26597c478bd9Sstevel@tonic-gate 	    "ehci_traverse_qtds:");
26607c478bd9Sstevel@tonic-gate 
26617c478bd9Sstevel@tonic-gate 	/* Process the transfer wrappers for this pipe */
26627c478bd9Sstevel@tonic-gate 	next_tw = pp->pp_tw_head;
26637c478bd9Sstevel@tonic-gate 
26647c478bd9Sstevel@tonic-gate 	while (next_tw) {
26657c478bd9Sstevel@tonic-gate 		/* Stop the the transfer timer */
26667c478bd9Sstevel@tonic-gate 		ehci_stop_xfer_timer(ehcip, next_tw, EHCI_REMOVE_XFER_ALWAYS);
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 		qtd = (ehci_qtd_t *)next_tw->tw_qtd_head;
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate 		/* Walk through each QTD for this transfer wrapper */
26717c478bd9Sstevel@tonic-gate 		while (qtd) {
26727c478bd9Sstevel@tonic-gate 			/* Remove this QTD from active QTD list */
26737c478bd9Sstevel@tonic-gate 			ehci_remove_qtd_from_active_qtd_list(ehcip, qtd);
26747c478bd9Sstevel@tonic-gate 
26757c478bd9Sstevel@tonic-gate 			next_qtd = ehci_qtd_iommu_to_cpu(ehcip,
26767c478bd9Sstevel@tonic-gate 			    Get_QTD(qtd->qtd_tw_next_qtd));
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 			/* Deallocate this QTD */
26797c478bd9Sstevel@tonic-gate 			ehci_deallocate_qtd(ehcip, qtd);
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 			qtd = next_qtd;
26827c478bd9Sstevel@tonic-gate 		}
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 		next_tw = next_tw->tw_next;
26857c478bd9Sstevel@tonic-gate 	}
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 	/* Clear current qtd pointer */
26887c478bd9Sstevel@tonic-gate 	Set_QH(pp->pp_qh->qh_curr_qtd, (uint32_t)0x00000000);
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate 	/* Update the next qtd pointer in the QH */
26917c478bd9Sstevel@tonic-gate 	Set_QH(pp->pp_qh->qh_next_qtd, Get_QH(pp->pp_qh->qh_dummy_qtd));
26927c478bd9Sstevel@tonic-gate }
26937c478bd9Sstevel@tonic-gate 
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate /*
26967c478bd9Sstevel@tonic-gate  * ehci_deallocate_qtd:
26977c478bd9Sstevel@tonic-gate  *
26987c478bd9Sstevel@tonic-gate  * Deallocate a Host Controller's (HC) Transfer Descriptor (QTD).
26997c478bd9Sstevel@tonic-gate  *
27007c478bd9Sstevel@tonic-gate  * NOTE: This function is also called from POLLED MODE.
27017c478bd9Sstevel@tonic-gate  */
27027c478bd9Sstevel@tonic-gate void
27037c478bd9Sstevel@tonic-gate ehci_deallocate_qtd(
27047c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
27057c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*old_qtd)
27067c478bd9Sstevel@tonic-gate {
27077c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw = NULL;
27087c478bd9Sstevel@tonic-gate 
27097c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
27107c478bd9Sstevel@tonic-gate 	    "ehci_deallocate_qtd: old_qtd = 0x%p", (void *)old_qtd);
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate 	/*
27137c478bd9Sstevel@tonic-gate 	 * Obtain the transaction wrapper and tw will be
27147c478bd9Sstevel@tonic-gate 	 * NULL for the dummy QTDs.
27157c478bd9Sstevel@tonic-gate 	 */
27167c478bd9Sstevel@tonic-gate 	if (Get_QTD(old_qtd->qtd_state) != EHCI_QTD_DUMMY) {
27177c478bd9Sstevel@tonic-gate 		tw = (ehci_trans_wrapper_t *)
27187c478bd9Sstevel@tonic-gate 		EHCI_LOOKUP_ID((uint32_t)
27197c478bd9Sstevel@tonic-gate 		Get_QTD(old_qtd->qtd_trans_wrapper));
27207c478bd9Sstevel@tonic-gate 
27217c478bd9Sstevel@tonic-gate 		ASSERT(tw != NULL);
27227c478bd9Sstevel@tonic-gate 	}
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate 	/*
27257c478bd9Sstevel@tonic-gate 	 * If QTD's transfer wrapper is NULL, don't access its TW.
27267c478bd9Sstevel@tonic-gate 	 * Just free the QTD.
27277c478bd9Sstevel@tonic-gate 	 */
27287c478bd9Sstevel@tonic-gate 	if (tw) {
27297c478bd9Sstevel@tonic-gate 		ehci_qtd_t	*qtd, *next_qtd;
27307c478bd9Sstevel@tonic-gate 
27317c478bd9Sstevel@tonic-gate 		qtd = tw->tw_qtd_head;
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate 		if (old_qtd != qtd) {
27347c478bd9Sstevel@tonic-gate 			next_qtd = ehci_qtd_iommu_to_cpu(
27357c478bd9Sstevel@tonic-gate 				    ehcip, Get_QTD(qtd->qtd_tw_next_qtd));
27367c478bd9Sstevel@tonic-gate 
27377c478bd9Sstevel@tonic-gate 			while (next_qtd != old_qtd) {
27387c478bd9Sstevel@tonic-gate 				qtd = next_qtd;
27397c478bd9Sstevel@tonic-gate 				next_qtd = ehci_qtd_iommu_to_cpu(
27407c478bd9Sstevel@tonic-gate 				    ehcip, Get_QTD(qtd->qtd_tw_next_qtd));
27417c478bd9Sstevel@tonic-gate 			}
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate 			Set_QTD(qtd->qtd_tw_next_qtd, old_qtd->qtd_tw_next_qtd);
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate 			if (qtd->qtd_tw_next_qtd == NULL) {
27467c478bd9Sstevel@tonic-gate 				tw->tw_qtd_tail = qtd;
27477c478bd9Sstevel@tonic-gate 			}
27487c478bd9Sstevel@tonic-gate 		} else {
27497c478bd9Sstevel@tonic-gate 			tw->tw_qtd_head = ehci_qtd_iommu_to_cpu(
27507c478bd9Sstevel@tonic-gate 			    ehcip, Get_QTD(old_qtd->qtd_tw_next_qtd));
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate 			if (tw->tw_qtd_head == NULL) {
27537c478bd9Sstevel@tonic-gate 				tw->tw_qtd_tail = NULL;
27547c478bd9Sstevel@tonic-gate 			}
27557c478bd9Sstevel@tonic-gate 		}
27567c478bd9Sstevel@tonic-gate 	}
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate 	bzero((void *)old_qtd, sizeof (ehci_qtd_t));
27597c478bd9Sstevel@tonic-gate 	Set_QTD(old_qtd->qtd_state, EHCI_QTD_FREE);
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
27627c478bd9Sstevel@tonic-gate 	    "Dealloc_qtd: qtd 0x%p", (void *)old_qtd);
27637c478bd9Sstevel@tonic-gate }
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate 
27667c478bd9Sstevel@tonic-gate /*
27677c478bd9Sstevel@tonic-gate  * ehci_qtd_cpu_to_iommu:
27687c478bd9Sstevel@tonic-gate  *
27697c478bd9Sstevel@tonic-gate  * This function converts for the given Transfer Descriptor (QTD) CPU address
27707c478bd9Sstevel@tonic-gate  * to IO address.
27717c478bd9Sstevel@tonic-gate  *
27727c478bd9Sstevel@tonic-gate  * NOTE: This function is also called from POLLED MODE.
27737c478bd9Sstevel@tonic-gate  */
27747c478bd9Sstevel@tonic-gate uint32_t
27757c478bd9Sstevel@tonic-gate ehci_qtd_cpu_to_iommu(
27767c478bd9Sstevel@tonic-gate 	ehci_state_t	*ehcip,
27777c478bd9Sstevel@tonic-gate 	ehci_qtd_t	*addr)
27787c478bd9Sstevel@tonic-gate {
27797c478bd9Sstevel@tonic-gate 	uint32_t	td;
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate 	td  = (uint32_t)ehcip->ehci_qtd_pool_cookie.dmac_address +
27827c478bd9Sstevel@tonic-gate 	    (uint32_t)((uintptr_t)addr -
27837c478bd9Sstevel@tonic-gate 	    (uintptr_t)(ehcip->ehci_qtd_pool_addr));
27847c478bd9Sstevel@tonic-gate 
27857c478bd9Sstevel@tonic-gate 	ASSERT((ehcip->ehci_qtd_pool_cookie.dmac_address +
27867c478bd9Sstevel@tonic-gate 	    (uint32_t) (sizeof (ehci_qtd_t) *
27877c478bd9Sstevel@tonic-gate 	    (addr - ehcip->ehci_qtd_pool_addr))) ==
27887c478bd9Sstevel@tonic-gate 	    (ehcip->ehci_qtd_pool_cookie.dmac_address +
27897c478bd9Sstevel@tonic-gate 	    (uint32_t)((uintptr_t)addr - (uintptr_t)
27907c478bd9Sstevel@tonic-gate 	    (ehcip->ehci_qtd_pool_addr))));
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate 	ASSERT(td >= ehcip->ehci_qtd_pool_cookie.dmac_address);
27937c478bd9Sstevel@tonic-gate 	ASSERT(td <= ehcip->ehci_qtd_pool_cookie.dmac_address +
27947c478bd9Sstevel@tonic-gate 	    sizeof (ehci_qtd_t) * ehci_qtd_pool_size);
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate 	return (td);
27977c478bd9Sstevel@tonic-gate }
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 
28007c478bd9Sstevel@tonic-gate /*
28017c478bd9Sstevel@tonic-gate  * ehci_qtd_iommu_to_cpu:
28027c478bd9Sstevel@tonic-gate  *
28037c478bd9Sstevel@tonic-gate  * This function converts for the given Transfer Descriptor (QTD) IO address
28047c478bd9Sstevel@tonic-gate  * to CPU address.
28057c478bd9Sstevel@tonic-gate  *
28067c478bd9Sstevel@tonic-gate  * NOTE: This function is also called from POLLED MODE.
28077c478bd9Sstevel@tonic-gate  */
28087c478bd9Sstevel@tonic-gate ehci_qtd_t *
28097c478bd9Sstevel@tonic-gate ehci_qtd_iommu_to_cpu(
28107c478bd9Sstevel@tonic-gate 	ehci_state_t	*ehcip,
28117c478bd9Sstevel@tonic-gate 	uintptr_t	addr)
28127c478bd9Sstevel@tonic-gate {
28137c478bd9Sstevel@tonic-gate 	ehci_qtd_t	*qtd;
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	if (addr == NULL) {
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 		return (NULL);
28187c478bd9Sstevel@tonic-gate 	}
28197c478bd9Sstevel@tonic-gate 
28207c478bd9Sstevel@tonic-gate 	qtd = (ehci_qtd_t *)((uintptr_t)
28217c478bd9Sstevel@tonic-gate 	    (addr - ehcip->ehci_qtd_pool_cookie.dmac_address) +
28227c478bd9Sstevel@tonic-gate 	    (uintptr_t)ehcip->ehci_qtd_pool_addr);
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	ASSERT(qtd >= ehcip->ehci_qtd_pool_addr);
28257c478bd9Sstevel@tonic-gate 	ASSERT((uintptr_t)qtd <= (uintptr_t)ehcip->ehci_qtd_pool_addr +
28267c478bd9Sstevel@tonic-gate 	    (uintptr_t)(sizeof (ehci_qtd_t) * ehci_qtd_pool_size));
28277c478bd9Sstevel@tonic-gate 
28287c478bd9Sstevel@tonic-gate 	return (qtd);
28297c478bd9Sstevel@tonic-gate }
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate /*
28327c478bd9Sstevel@tonic-gate  * ehci_allocate_tds_for_tw_resources:
28337c478bd9Sstevel@tonic-gate  *
28347c478bd9Sstevel@tonic-gate  * Allocate n Transfer Descriptors (TD) from the TD buffer pool and places it
28357c478bd9Sstevel@tonic-gate  * into the TW.  Also chooses the correct alternate qtd when required.	It is
28367c478bd9Sstevel@tonic-gate  * used for hardware short transfer support.  For more information on
28377c478bd9Sstevel@tonic-gate  * alternate qtds look at section 3.5.2 in the EHCI spec.
28387c478bd9Sstevel@tonic-gate  * Here is how each alternate qtd's are used:
28397c478bd9Sstevel@tonic-gate  *
28407c478bd9Sstevel@tonic-gate  * Bulk: used fully.
28417c478bd9Sstevel@tonic-gate  * Intr: xfers only require 1 QTD, so alternate qtds are never used.
28427c478bd9Sstevel@tonic-gate  * Ctrl: Should not use alternate QTD
28437c478bd9Sstevel@tonic-gate  * Isoch: Doesn't support short_xfer nor does it use QTD
28447c478bd9Sstevel@tonic-gate  *
28457c478bd9Sstevel@tonic-gate  * Returns USB_NO_RESOURCES if it was not able to allocate all the requested TD
28467c478bd9Sstevel@tonic-gate  * otherwise USB_SUCCESS.
28477c478bd9Sstevel@tonic-gate  */
28487c478bd9Sstevel@tonic-gate int
28497c478bd9Sstevel@tonic-gate ehci_allocate_tds_for_tw(
28507c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
28517c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
28527c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw,
28537c478bd9Sstevel@tonic-gate 	size_t			qtd_count)
28547c478bd9Sstevel@tonic-gate {
28557c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*eptd = &pp->pp_pipe_handle->p_ep;
28567c478bd9Sstevel@tonic-gate 	uchar_t			attributes;
28577c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd;
28587c478bd9Sstevel@tonic-gate 	uint32_t		qtd_addr;
28597c478bd9Sstevel@tonic-gate 	int			i;
28607c478bd9Sstevel@tonic-gate 	int			error = USB_SUCCESS;
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate 	attributes = eptd->bmAttributes & USB_EP_ATTR_MASK;
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate 	for (i = 0; i < qtd_count; i += 1) {
28657c478bd9Sstevel@tonic-gate 		qtd = ehci_allocate_qtd_from_pool(ehcip);
28667c478bd9Sstevel@tonic-gate 		if (qtd == NULL) {
28677c478bd9Sstevel@tonic-gate 			error = USB_NO_RESOURCES;
28687c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
28697c478bd9Sstevel@tonic-gate 			    "ehci_allocate_qtds_for_tw: "
28707c478bd9Sstevel@tonic-gate 			    "Unable to allocate %lu QTDs",
28717c478bd9Sstevel@tonic-gate 			    qtd_count);
28727c478bd9Sstevel@tonic-gate 			break;
28737c478bd9Sstevel@tonic-gate 		}
28747c478bd9Sstevel@tonic-gate 		if (i > 0) {
28757c478bd9Sstevel@tonic-gate 			qtd_addr = ehci_qtd_cpu_to_iommu(ehcip,
28767c478bd9Sstevel@tonic-gate 			    tw->tw_qtd_free_list);
28777c478bd9Sstevel@tonic-gate 			Set_QTD(qtd->qtd_tw_next_qtd, qtd_addr);
28787c478bd9Sstevel@tonic-gate 		}
28797c478bd9Sstevel@tonic-gate 		tw->tw_qtd_free_list = qtd;
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate 		/*
28827c478bd9Sstevel@tonic-gate 		 * Save the second one as a pointer to the new dummy 1.
28837c478bd9Sstevel@tonic-gate 		 * It is used later for the alt_qtd_ptr.  Xfers with only
28847c478bd9Sstevel@tonic-gate 		 * one qtd do not need alt_qtd_ptr.
28857c478bd9Sstevel@tonic-gate 		 * The tds's are allocated and put into a stack, that is
28867c478bd9Sstevel@tonic-gate 		 * why the second qtd allocated will turn out to be the
28877c478bd9Sstevel@tonic-gate 		 * new dummy 1.
28887c478bd9Sstevel@tonic-gate 		 */
28897c478bd9Sstevel@tonic-gate 		if ((i == 1) && (attributes == USB_EP_ATTR_BULK)) {
28907c478bd9Sstevel@tonic-gate 			tw->tw_alt_qtd = qtd;
28917c478bd9Sstevel@tonic-gate 		}
28927c478bd9Sstevel@tonic-gate 	}
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 	return (error);
28957c478bd9Sstevel@tonic-gate }
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate /*
28987c478bd9Sstevel@tonic-gate  * ehci_allocate_tw_resources:
28997c478bd9Sstevel@tonic-gate  *
29007c478bd9Sstevel@tonic-gate  * Allocate a Transaction Wrapper (TW) and n Transfer Descriptors (QTD)
29017c478bd9Sstevel@tonic-gate  * from the QTD buffer pool and places it into the TW.	It does an all
29027c478bd9Sstevel@tonic-gate  * or nothing transaction.
29037c478bd9Sstevel@tonic-gate  *
29047c478bd9Sstevel@tonic-gate  * Returns NULL if there is insufficient resources otherwise TW.
29057c478bd9Sstevel@tonic-gate  */
29067c478bd9Sstevel@tonic-gate static ehci_trans_wrapper_t *
29077c478bd9Sstevel@tonic-gate ehci_allocate_tw_resources(
29087c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
29097c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
29107c478bd9Sstevel@tonic-gate 	size_t			tw_length,
29117c478bd9Sstevel@tonic-gate 	usb_flags_t		usb_flags,
29127c478bd9Sstevel@tonic-gate 	size_t			qtd_count)
29137c478bd9Sstevel@tonic-gate {
29147c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw;
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 	tw = ehci_create_transfer_wrapper(ehcip, pp, tw_length, usb_flags);
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 	if (tw == NULL) {
29197c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
29207c478bd9Sstevel@tonic-gate 		    "ehci_allocate_tw_resources: Unable to allocate TW");
29217c478bd9Sstevel@tonic-gate 	} else {
29227c478bd9Sstevel@tonic-gate 		if (ehci_allocate_tds_for_tw(ehcip, pp, tw, qtd_count) ==
29237c478bd9Sstevel@tonic-gate 		    USB_SUCCESS) {
29247c478bd9Sstevel@tonic-gate 			tw->tw_num_qtds = qtd_count;
29257c478bd9Sstevel@tonic-gate 		} else {
29267c478bd9Sstevel@tonic-gate 			ehci_deallocate_tw(ehcip, pp, tw);
29277c478bd9Sstevel@tonic-gate 			tw = NULL;
29287c478bd9Sstevel@tonic-gate 		}
29297c478bd9Sstevel@tonic-gate 	}
29307c478bd9Sstevel@tonic-gate 
29317c478bd9Sstevel@tonic-gate 	return (tw);
29327c478bd9Sstevel@tonic-gate }
29337c478bd9Sstevel@tonic-gate 
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate /*
29367c478bd9Sstevel@tonic-gate  * ehci_free_tw_td_resources:
29377c478bd9Sstevel@tonic-gate  *
29387c478bd9Sstevel@tonic-gate  * Free all allocated resources for Transaction Wrapper (TW).
29397c478bd9Sstevel@tonic-gate  * Does not free the TW itself.
29407c478bd9Sstevel@tonic-gate  *
29417c478bd9Sstevel@tonic-gate  * Returns NULL if there is insufficient resources otherwise TW.
29427c478bd9Sstevel@tonic-gate  */
29437c478bd9Sstevel@tonic-gate static void
29447c478bd9Sstevel@tonic-gate ehci_free_tw_td_resources(
29457c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
29467c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw)
29477c478bd9Sstevel@tonic-gate {
29487c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd = NULL;
29497c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*temp_qtd = NULL;
29507c478bd9Sstevel@tonic-gate 
29517c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
29527c478bd9Sstevel@tonic-gate 	    "ehci_free_tw_td_resources: tw = 0x%p", tw);
29537c478bd9Sstevel@tonic-gate 
29547c478bd9Sstevel@tonic-gate 	qtd = tw->tw_qtd_free_list;
29557c478bd9Sstevel@tonic-gate 	while (qtd != NULL) {
29567c478bd9Sstevel@tonic-gate 		/* Save the pointer to the next qtd before destroying it */
29577c478bd9Sstevel@tonic-gate 		temp_qtd = ehci_qtd_iommu_to_cpu(ehcip,
29587c478bd9Sstevel@tonic-gate 		    Get_QTD(qtd->qtd_tw_next_qtd));
29597c478bd9Sstevel@tonic-gate 		ehci_deallocate_qtd(ehcip, qtd);
29607c478bd9Sstevel@tonic-gate 		qtd = temp_qtd;
29617c478bd9Sstevel@tonic-gate 	}
29627c478bd9Sstevel@tonic-gate 	tw->tw_qtd_free_list = NULL;
29637c478bd9Sstevel@tonic-gate }
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate /*
29667c478bd9Sstevel@tonic-gate  * Transfer Wrapper functions
29677c478bd9Sstevel@tonic-gate  *
29687c478bd9Sstevel@tonic-gate  * ehci_create_transfer_wrapper:
29697c478bd9Sstevel@tonic-gate  *
29707c478bd9Sstevel@tonic-gate  * Create a Transaction Wrapper (TW) and this involves the allocating of DMA
29717c478bd9Sstevel@tonic-gate  * resources.
29727c478bd9Sstevel@tonic-gate  */
29737c478bd9Sstevel@tonic-gate static ehci_trans_wrapper_t *
29747c478bd9Sstevel@tonic-gate ehci_create_transfer_wrapper(
29757c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
29767c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
29777c478bd9Sstevel@tonic-gate 	size_t			length,
29787c478bd9Sstevel@tonic-gate 	uint_t			usb_flags)
29797c478bd9Sstevel@tonic-gate {
29807c478bd9Sstevel@tonic-gate 	ddi_device_acc_attr_t	dev_attr;
29813304303fSsl 	ddi_dma_attr_t		dma_attr;
29827c478bd9Sstevel@tonic-gate 	int			result;
29837c478bd9Sstevel@tonic-gate 	size_t			real_length;
29847c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw;
29853304303fSsl 	int			kmem_flag;
29863304303fSsl 	int			(*dmamem_wait)(caddr_t);
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
29897c478bd9Sstevel@tonic-gate 	    "ehci_create_transfer_wrapper: length = 0x%lx flags = 0x%x",
29907c478bd9Sstevel@tonic-gate 	    length, usb_flags);
29917c478bd9Sstevel@tonic-gate 
29927c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
29937c478bd9Sstevel@tonic-gate 
29943304303fSsl 	/* SLEEP flag should not be used in interrupt context */
29953304303fSsl 	if (servicing_interrupt()) {
29963304303fSsl 		kmem_flag = KM_NOSLEEP;
29973304303fSsl 		dmamem_wait = DDI_DMA_DONTWAIT;
29983304303fSsl 	} else {
29993304303fSsl 		kmem_flag = KM_SLEEP;
30003304303fSsl 		dmamem_wait = DDI_DMA_SLEEP;
30013304303fSsl 	}
30023304303fSsl 
30037c478bd9Sstevel@tonic-gate 	/* Allocate space for the transfer wrapper */
30043304303fSsl 	tw = kmem_zalloc(sizeof (ehci_trans_wrapper_t), kmem_flag);
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate 	if (tw == NULL) {
30077c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS,  ehcip->ehci_log_hdl,
30087c478bd9Sstevel@tonic-gate 		    "ehci_create_transfer_wrapper: kmem_zalloc failed");
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate 		return (NULL);
30117c478bd9Sstevel@tonic-gate 	}
30127c478bd9Sstevel@tonic-gate 
30133304303fSsl 	/* allow sg lists for transfer wrapper dma memory */
30143304303fSsl 	bcopy(&ehcip->ehci_dma_attr, &dma_attr, sizeof (ddi_dma_attr_t));
30153304303fSsl 	dma_attr.dma_attr_sgllen = EHCI_DMA_ATTR_TW_SGLLEN;
30163304303fSsl 	dma_attr.dma_attr_align = EHCI_DMA_ATTR_ALIGNMENT;
30177c478bd9Sstevel@tonic-gate 
30187c478bd9Sstevel@tonic-gate 	/* Allocate the DMA handle */
30197c478bd9Sstevel@tonic-gate 	result = ddi_dma_alloc_handle(ehcip->ehci_dip,
30203304303fSsl 	    &dma_attr, dmamem_wait, 0, &tw->tw_dmahandle);
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate 	if (result != DDI_SUCCESS) {
30237c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
30247c478bd9Sstevel@tonic-gate 		    "ehci_create_transfer_wrapper: Alloc handle failed");
30257c478bd9Sstevel@tonic-gate 
30267c478bd9Sstevel@tonic-gate 		kmem_free(tw, sizeof (ehci_trans_wrapper_t));
30277c478bd9Sstevel@tonic-gate 
30287c478bd9Sstevel@tonic-gate 		return (NULL);
30297c478bd9Sstevel@tonic-gate 	}
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate 	dev_attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
30327c478bd9Sstevel@tonic-gate 
30337c478bd9Sstevel@tonic-gate 	/* no need for swapping the raw data */
30347c478bd9Sstevel@tonic-gate 	dev_attr.devacc_attr_endian_flags  = DDI_NEVERSWAP_ACC;
30357c478bd9Sstevel@tonic-gate 	dev_attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate 	/* Allocate the memory */
30387c478bd9Sstevel@tonic-gate 	result = ddi_dma_mem_alloc(tw->tw_dmahandle, length,
30393304303fSsl 	    &dev_attr, DDI_DMA_CONSISTENT, dmamem_wait, NULL,
30407c478bd9Sstevel@tonic-gate 	    (caddr_t *)&tw->tw_buf, &real_length, &tw->tw_accesshandle);
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate 	if (result != DDI_SUCCESS) {
30437c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
30447c478bd9Sstevel@tonic-gate 		    "ehci_create_transfer_wrapper: dma_mem_alloc fail");
30457c478bd9Sstevel@tonic-gate 
30467c478bd9Sstevel@tonic-gate 		ddi_dma_free_handle(&tw->tw_dmahandle);
30477c478bd9Sstevel@tonic-gate 		kmem_free(tw, sizeof (ehci_trans_wrapper_t));
30487c478bd9Sstevel@tonic-gate 
30497c478bd9Sstevel@tonic-gate 		return (NULL);
30507c478bd9Sstevel@tonic-gate 	}
30517c478bd9Sstevel@tonic-gate 
30527c478bd9Sstevel@tonic-gate 	ASSERT(real_length >= length);
30537c478bd9Sstevel@tonic-gate 
30547c478bd9Sstevel@tonic-gate 	/* Bind the handle */
30557c478bd9Sstevel@tonic-gate 	result = ddi_dma_addr_bind_handle(tw->tw_dmahandle, NULL,
30567c478bd9Sstevel@tonic-gate 	    (caddr_t)tw->tw_buf, real_length, DDI_DMA_RDWR|DDI_DMA_CONSISTENT,
30573304303fSsl 	    dmamem_wait, NULL, &tw->tw_cookie, &tw->tw_ncookies);
30587c478bd9Sstevel@tonic-gate 
30593304303fSsl 	if (result != DDI_DMA_MAPPED) {
30607c478bd9Sstevel@tonic-gate 		ehci_decode_ddi_dma_addr_bind_handle_result(ehcip, result);
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate 		ddi_dma_mem_free(&tw->tw_accesshandle);
30637c478bd9Sstevel@tonic-gate 		ddi_dma_free_handle(&tw->tw_dmahandle);
30647c478bd9Sstevel@tonic-gate 		kmem_free(tw, sizeof (ehci_trans_wrapper_t));
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate 		return (NULL);
30677c478bd9Sstevel@tonic-gate 	}
30687c478bd9Sstevel@tonic-gate 
30693304303fSsl 	tw->tw_cookie_idx = 0;
30703304303fSsl 	tw->tw_dma_offs = 0;
30713304303fSsl 
30727c478bd9Sstevel@tonic-gate 	/*
30737c478bd9Sstevel@tonic-gate 	 * Only allow one wrapper to be added at a time. Insert the
30747c478bd9Sstevel@tonic-gate 	 * new transaction wrapper into the list for this pipe.
30757c478bd9Sstevel@tonic-gate 	 */
30767c478bd9Sstevel@tonic-gate 	if (pp->pp_tw_head == NULL) {
30777c478bd9Sstevel@tonic-gate 		pp->pp_tw_head = tw;
30787c478bd9Sstevel@tonic-gate 		pp->pp_tw_tail = tw;
30797c478bd9Sstevel@tonic-gate 	} else {
30807c478bd9Sstevel@tonic-gate 		pp->pp_tw_tail->tw_next = tw;
30817c478bd9Sstevel@tonic-gate 		pp->pp_tw_tail = tw;
30827c478bd9Sstevel@tonic-gate 	}
30837c478bd9Sstevel@tonic-gate 
30847c478bd9Sstevel@tonic-gate 	/* Store the transfer length */
30857c478bd9Sstevel@tonic-gate 	tw->tw_length = length;
30867c478bd9Sstevel@tonic-gate 
30877c478bd9Sstevel@tonic-gate 	/* Store a back pointer to the pipe private structure */
30887c478bd9Sstevel@tonic-gate 	tw->tw_pipe_private = pp;
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate 	/* Store the transfer type - synchronous or asynchronous */
30917c478bd9Sstevel@tonic-gate 	tw->tw_flags = usb_flags;
30927c478bd9Sstevel@tonic-gate 
30937c478bd9Sstevel@tonic-gate 	/* Get and Store 32bit ID */
30947c478bd9Sstevel@tonic-gate 	tw->tw_id = EHCI_GET_ID((void *)tw);
30957c478bd9Sstevel@tonic-gate 
30967c478bd9Sstevel@tonic-gate 	ASSERT(tw->tw_id != NULL);
30977c478bd9Sstevel@tonic-gate 
30987c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
30993304303fSsl 	    "ehci_create_transfer_wrapper: tw = 0x%p, ncookies = %u",
31003304303fSsl 	    tw, tw->tw_ncookies);
31017c478bd9Sstevel@tonic-gate 
31027c478bd9Sstevel@tonic-gate 	return (tw);
31037c478bd9Sstevel@tonic-gate }
31047c478bd9Sstevel@tonic-gate 
31057c478bd9Sstevel@tonic-gate 
31067c478bd9Sstevel@tonic-gate /*
31077c478bd9Sstevel@tonic-gate  * ehci_start_xfer_timer:
31087c478bd9Sstevel@tonic-gate  *
31097c478bd9Sstevel@tonic-gate  * Start the timer for the control, bulk and for one time interrupt
31107c478bd9Sstevel@tonic-gate  * transfers.
31117c478bd9Sstevel@tonic-gate  */
31127c478bd9Sstevel@tonic-gate /* ARGSUSED */
31137c478bd9Sstevel@tonic-gate static void
31147c478bd9Sstevel@tonic-gate ehci_start_xfer_timer(
31157c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
31167c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
31177c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw)
31187c478bd9Sstevel@tonic-gate {
31197c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS,  ehcip->ehci_log_hdl,
31207c478bd9Sstevel@tonic-gate 	    "ehci_start_xfer_timer: tw = 0x%p", tw);
31217c478bd9Sstevel@tonic-gate 
31227c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
31237c478bd9Sstevel@tonic-gate 
31247c478bd9Sstevel@tonic-gate 	/*
31257c478bd9Sstevel@tonic-gate 	 * The timeout handling is done only for control, bulk and for
31267c478bd9Sstevel@tonic-gate 	 * one time Interrupt transfers.
31277c478bd9Sstevel@tonic-gate 	 *
31287c478bd9Sstevel@tonic-gate 	 * NOTE: If timeout is zero; Assume infinite timeout and don't
31297c478bd9Sstevel@tonic-gate 	 * insert this transfer on the timeout list.
31307c478bd9Sstevel@tonic-gate 	 */
31317c478bd9Sstevel@tonic-gate 	if (tw->tw_timeout) {
31327c478bd9Sstevel@tonic-gate 		/*
31337c478bd9Sstevel@tonic-gate 		 * Add this transfer wrapper to the head of the pipe's
31347c478bd9Sstevel@tonic-gate 		 * tw timeout list.
31357c478bd9Sstevel@tonic-gate 		 */
31367c478bd9Sstevel@tonic-gate 		if (pp->pp_timeout_list) {
31377c478bd9Sstevel@tonic-gate 			tw->tw_timeout_next = pp->pp_timeout_list;
31387c478bd9Sstevel@tonic-gate 		}
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate 		pp->pp_timeout_list = tw;
31417c478bd9Sstevel@tonic-gate 		ehci_start_timer(ehcip, pp);
31427c478bd9Sstevel@tonic-gate 	}
31437c478bd9Sstevel@tonic-gate }
31447c478bd9Sstevel@tonic-gate 
31457c478bd9Sstevel@tonic-gate 
31467c478bd9Sstevel@tonic-gate /*
31477c478bd9Sstevel@tonic-gate  * ehci_stop_xfer_timer:
31487c478bd9Sstevel@tonic-gate  *
31497c478bd9Sstevel@tonic-gate  * Start the timer for the control, bulk and for one time interrupt
31507c478bd9Sstevel@tonic-gate  * transfers.
31517c478bd9Sstevel@tonic-gate  */
31527c478bd9Sstevel@tonic-gate void
31537c478bd9Sstevel@tonic-gate ehci_stop_xfer_timer(
31547c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
31557c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw,
31567c478bd9Sstevel@tonic-gate 	uint_t			flag)
31577c478bd9Sstevel@tonic-gate {
31587c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp;
31597c478bd9Sstevel@tonic-gate 	timeout_id_t		timer_id;
31607c478bd9Sstevel@tonic-gate 
31617c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS,  ehcip->ehci_log_hdl,
31627c478bd9Sstevel@tonic-gate 	    "ehci_stop_xfer_timer: tw = 0x%p", tw);
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate 	/* Obtain the pipe private structure */
31677c478bd9Sstevel@tonic-gate 	pp = tw->tw_pipe_private;
31687c478bd9Sstevel@tonic-gate 
31697c478bd9Sstevel@tonic-gate 	/* check if the timeout tw list is empty */
31707c478bd9Sstevel@tonic-gate 	if (pp->pp_timeout_list == NULL) {
31717c478bd9Sstevel@tonic-gate 
31727c478bd9Sstevel@tonic-gate 		return;
31737c478bd9Sstevel@tonic-gate 	}
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate 	switch (flag) {
31767c478bd9Sstevel@tonic-gate 	case EHCI_REMOVE_XFER_IFLAST:
31777c478bd9Sstevel@tonic-gate 		if (tw->tw_qtd_head != tw->tw_qtd_tail) {
31787c478bd9Sstevel@tonic-gate 			break;
31797c478bd9Sstevel@tonic-gate 		}
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
31827c478bd9Sstevel@tonic-gate 	case EHCI_REMOVE_XFER_ALWAYS:
31837c478bd9Sstevel@tonic-gate 		ehci_remove_tw_from_timeout_list(ehcip, tw);
31847c478bd9Sstevel@tonic-gate 
31857c478bd9Sstevel@tonic-gate 		if ((pp->pp_timeout_list == NULL) &&
31867c478bd9Sstevel@tonic-gate 		    (pp->pp_timer_id)) {
31877c478bd9Sstevel@tonic-gate 
31887c478bd9Sstevel@tonic-gate 			timer_id = pp->pp_timer_id;
31897c478bd9Sstevel@tonic-gate 
31907c478bd9Sstevel@tonic-gate 			/* Reset the timer id to zero */
31917c478bd9Sstevel@tonic-gate 			pp->pp_timer_id = 0;
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 			mutex_exit(&ehcip->ehci_int_mutex);
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate 			(void) untimeout(timer_id);
31967c478bd9Sstevel@tonic-gate 
31977c478bd9Sstevel@tonic-gate 			mutex_enter(&ehcip->ehci_int_mutex);
31987c478bd9Sstevel@tonic-gate 		}
31997c478bd9Sstevel@tonic-gate 		break;
32007c478bd9Sstevel@tonic-gate 	default:
32017c478bd9Sstevel@tonic-gate 		break;
32027c478bd9Sstevel@tonic-gate 	}
32037c478bd9Sstevel@tonic-gate }
32047c478bd9Sstevel@tonic-gate 
32057c478bd9Sstevel@tonic-gate 
32067c478bd9Sstevel@tonic-gate /*
32077c478bd9Sstevel@tonic-gate  * ehci_xfer_timeout_handler:
32087c478bd9Sstevel@tonic-gate  *
32097c478bd9Sstevel@tonic-gate  * Control or bulk transfer timeout handler.
32107c478bd9Sstevel@tonic-gate  */
32117c478bd9Sstevel@tonic-gate static void
32127c478bd9Sstevel@tonic-gate ehci_xfer_timeout_handler(void *arg)
32137c478bd9Sstevel@tonic-gate {
32147c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph = (usba_pipe_handle_data_t *)arg;
32157c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip = ehci_obtain_state(
32167c478bd9Sstevel@tonic-gate 				    ph->p_usba_device->usb_root_hub_dip);
32177c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
32187c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw, *next;
32197c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*expire_xfer_list = NULL;
32207c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd;
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS,  ehcip->ehci_log_hdl,
32237c478bd9Sstevel@tonic-gate 	    "ehci_xfer_timeout_handler: ehcip = 0x%p, ph = 0x%p", ehcip, ph);
32247c478bd9Sstevel@tonic-gate 
32257c478bd9Sstevel@tonic-gate 	mutex_enter(&ehcip->ehci_int_mutex);
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 	/*
32287c478bd9Sstevel@tonic-gate 	 * Check whether still timeout handler is valid.
32297c478bd9Sstevel@tonic-gate 	 */
32307c478bd9Sstevel@tonic-gate 	if (pp->pp_timer_id != 0) {
32317c478bd9Sstevel@tonic-gate 
32327c478bd9Sstevel@tonic-gate 		/* Reset the timer id to zero */
32337c478bd9Sstevel@tonic-gate 		pp->pp_timer_id = 0;
32347c478bd9Sstevel@tonic-gate 	} else {
32357c478bd9Sstevel@tonic-gate 		mutex_exit(&ehcip->ehci_int_mutex);
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate 		return;
32387c478bd9Sstevel@tonic-gate 	}
32397c478bd9Sstevel@tonic-gate 
32407c478bd9Sstevel@tonic-gate 	/* Get the transfer timeout list head */
32417c478bd9Sstevel@tonic-gate 	tw = pp->pp_timeout_list;
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate 	while (tw) {
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate 		/* Get the transfer on the timeout list */
32467c478bd9Sstevel@tonic-gate 		next = tw->tw_timeout_next;
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate 		tw->tw_timeout--;
32497c478bd9Sstevel@tonic-gate 
32507c478bd9Sstevel@tonic-gate 		if (tw->tw_timeout <= 0) {
32517c478bd9Sstevel@tonic-gate 
32527c478bd9Sstevel@tonic-gate 			/* remove the tw from the timeout list */
32537c478bd9Sstevel@tonic-gate 			ehci_remove_tw_from_timeout_list(ehcip, tw);
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 			/* remove QTDs from active QTD list */
32567c478bd9Sstevel@tonic-gate 			qtd = tw->tw_qtd_head;
32577c478bd9Sstevel@tonic-gate 			while (qtd) {
32587c478bd9Sstevel@tonic-gate 				ehci_remove_qtd_from_active_qtd_list(
32597c478bd9Sstevel@tonic-gate 					ehcip, qtd);
32607c478bd9Sstevel@tonic-gate 
32617c478bd9Sstevel@tonic-gate 				/* Get the next QTD from the wrapper */
32627c478bd9Sstevel@tonic-gate 				qtd = ehci_qtd_iommu_to_cpu(ehcip,
32637c478bd9Sstevel@tonic-gate 				    Get_QTD(qtd->qtd_tw_next_qtd));
32647c478bd9Sstevel@tonic-gate 			}
32657c478bd9Sstevel@tonic-gate 
32667c478bd9Sstevel@tonic-gate 			/*
32677c478bd9Sstevel@tonic-gate 			 * Preserve the order to the requests
32687c478bd9Sstevel@tonic-gate 			 * started time sequence.
32697c478bd9Sstevel@tonic-gate 			 */
32707c478bd9Sstevel@tonic-gate 			tw->tw_timeout_next = expire_xfer_list;
32717c478bd9Sstevel@tonic-gate 			expire_xfer_list = tw;
32727c478bd9Sstevel@tonic-gate 		}
32737c478bd9Sstevel@tonic-gate 
32747c478bd9Sstevel@tonic-gate 		tw = next;
32757c478bd9Sstevel@tonic-gate 	}
32767c478bd9Sstevel@tonic-gate 
32777c478bd9Sstevel@tonic-gate 	/*
32787c478bd9Sstevel@tonic-gate 	 * The timer should be started before the callbacks.
32797c478bd9Sstevel@tonic-gate 	 * There is always a chance that ehci interrupts come
32807c478bd9Sstevel@tonic-gate 	 * in when we release the mutex while calling the tw back.
32817c478bd9Sstevel@tonic-gate 	 * To keep an accurate timeout it should be restarted
32827c478bd9Sstevel@tonic-gate 	 * as soon as possible.
32837c478bd9Sstevel@tonic-gate 	 */
32847c478bd9Sstevel@tonic-gate 	ehci_start_timer(ehcip, pp);
32857c478bd9Sstevel@tonic-gate 
32867c478bd9Sstevel@tonic-gate 	/* Get the expired transfer timeout list head */
32877c478bd9Sstevel@tonic-gate 	tw = expire_xfer_list;
32887c478bd9Sstevel@tonic-gate 
32897c478bd9Sstevel@tonic-gate 	while (tw) {
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate 		/* Get the next tw on the expired transfer timeout list */
32927c478bd9Sstevel@tonic-gate 		next = tw->tw_timeout_next;
32937c478bd9Sstevel@tonic-gate 
32947c478bd9Sstevel@tonic-gate 		/*
32957c478bd9Sstevel@tonic-gate 		 * The error handle routine will release the mutex when
32967c478bd9Sstevel@tonic-gate 		 * calling back to USBA. But this will not cause any race.
32977c478bd9Sstevel@tonic-gate 		 * We do the callback and are relying on ehci_pipe_cleanup()
32987c478bd9Sstevel@tonic-gate 		 * to halt the queue head and clean up since we should not
32997c478bd9Sstevel@tonic-gate 		 * block in timeout context.
33007c478bd9Sstevel@tonic-gate 		 */
33017c478bd9Sstevel@tonic-gate 		ehci_handle_error(ehcip, tw->tw_qtd_head, USB_CR_TIMEOUT);
33027c478bd9Sstevel@tonic-gate 
33037c478bd9Sstevel@tonic-gate 		tw = next;
33047c478bd9Sstevel@tonic-gate 	}
33057c478bd9Sstevel@tonic-gate 	mutex_exit(&ehcip->ehci_int_mutex);
33067c478bd9Sstevel@tonic-gate }
33077c478bd9Sstevel@tonic-gate 
33087c478bd9Sstevel@tonic-gate 
33097c478bd9Sstevel@tonic-gate /*
33107c478bd9Sstevel@tonic-gate  * ehci_remove_tw_from_timeout_list:
33117c478bd9Sstevel@tonic-gate  *
33127c478bd9Sstevel@tonic-gate  * Remove Control or bulk transfer from the timeout list.
33137c478bd9Sstevel@tonic-gate  */
33147c478bd9Sstevel@tonic-gate static void
33157c478bd9Sstevel@tonic-gate ehci_remove_tw_from_timeout_list(
33167c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
33177c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw)
33187c478bd9Sstevel@tonic-gate {
33197c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp;
33207c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*prev, *next;
33217c478bd9Sstevel@tonic-gate 
33227c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS,  ehcip->ehci_log_hdl,
33237c478bd9Sstevel@tonic-gate 	    "ehci_remove_tw_from_timeout_list: tw = 0x%p", tw);
33247c478bd9Sstevel@tonic-gate 
33257c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
33267c478bd9Sstevel@tonic-gate 
33277c478bd9Sstevel@tonic-gate 	/* Obtain the pipe private structure */
33287c478bd9Sstevel@tonic-gate 	pp = tw->tw_pipe_private;
33297c478bd9Sstevel@tonic-gate 
33307c478bd9Sstevel@tonic-gate 	if (pp->pp_timeout_list) {
33317c478bd9Sstevel@tonic-gate 		if (pp->pp_timeout_list == tw) {
33327c478bd9Sstevel@tonic-gate 			pp->pp_timeout_list = tw->tw_timeout_next;
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 			tw->tw_timeout_next = NULL;
33357c478bd9Sstevel@tonic-gate 		} else {
33367c478bd9Sstevel@tonic-gate 			prev = pp->pp_timeout_list;
33377c478bd9Sstevel@tonic-gate 			next = prev->tw_timeout_next;
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate 			while (next && (next != tw)) {
33407c478bd9Sstevel@tonic-gate 				prev = next;
33417c478bd9Sstevel@tonic-gate 				next = next->tw_timeout_next;
33427c478bd9Sstevel@tonic-gate 			}
33437c478bd9Sstevel@tonic-gate 
33447c478bd9Sstevel@tonic-gate 			if (next == tw) {
33457c478bd9Sstevel@tonic-gate 				prev->tw_timeout_next =
33467c478bd9Sstevel@tonic-gate 					next->tw_timeout_next;
33477c478bd9Sstevel@tonic-gate 				tw->tw_timeout_next = NULL;
33487c478bd9Sstevel@tonic-gate 			}
33497c478bd9Sstevel@tonic-gate 		}
33507c478bd9Sstevel@tonic-gate 	}
33517c478bd9Sstevel@tonic-gate }
33527c478bd9Sstevel@tonic-gate 
33537c478bd9Sstevel@tonic-gate 
33547c478bd9Sstevel@tonic-gate /*
33557c478bd9Sstevel@tonic-gate  * ehci_start_timer:
33567c478bd9Sstevel@tonic-gate  *
33577c478bd9Sstevel@tonic-gate  * Start the pipe's timer
33587c478bd9Sstevel@tonic-gate  */
33597c478bd9Sstevel@tonic-gate static void
33607c478bd9Sstevel@tonic-gate ehci_start_timer(
33617c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
33627c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp)
33637c478bd9Sstevel@tonic-gate {
33647c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS,  ehcip->ehci_log_hdl,
33657c478bd9Sstevel@tonic-gate 	    "ehci_start_timer: ehcip = 0x%p, pp = 0x%p", ehcip, pp);
33667c478bd9Sstevel@tonic-gate 
33677c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate 	/*
33707c478bd9Sstevel@tonic-gate 	 * Start the pipe's timer only if currently timer is not
33717c478bd9Sstevel@tonic-gate 	 * running and if there are any transfers on the timeout
33727c478bd9Sstevel@tonic-gate 	 * list. This timer will be per pipe.
33737c478bd9Sstevel@tonic-gate 	 */
33747c478bd9Sstevel@tonic-gate 	if ((!pp->pp_timer_id) && (pp->pp_timeout_list)) {
33757c478bd9Sstevel@tonic-gate 		pp->pp_timer_id = timeout(ehci_xfer_timeout_handler,
33767c478bd9Sstevel@tonic-gate 		    (void *)(pp->pp_pipe_handle), drv_usectohz(1000000));
33777c478bd9Sstevel@tonic-gate 	}
33787c478bd9Sstevel@tonic-gate }
33797c478bd9Sstevel@tonic-gate 
33807c478bd9Sstevel@tonic-gate /*
33817c478bd9Sstevel@tonic-gate  * ehci_deallocate_tw:
33827c478bd9Sstevel@tonic-gate  *
33837c478bd9Sstevel@tonic-gate  * Deallocate of a Transaction Wrapper (TW) and this involves the freeing of
33847c478bd9Sstevel@tonic-gate  * of DMA resources.
33857c478bd9Sstevel@tonic-gate  */
33867c478bd9Sstevel@tonic-gate void
33877c478bd9Sstevel@tonic-gate ehci_deallocate_tw(
33887c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
33897c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
33907c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw)
33917c478bd9Sstevel@tonic-gate {
33927c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*prev, *next;
33937c478bd9Sstevel@tonic-gate 
33947c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
33957c478bd9Sstevel@tonic-gate 	    "ehci_deallocate_tw: tw = 0x%p", tw);
33967c478bd9Sstevel@tonic-gate 
33977c478bd9Sstevel@tonic-gate 	/*
33987c478bd9Sstevel@tonic-gate 	 * If the transfer wrapper has no Host Controller (HC)
33997c478bd9Sstevel@tonic-gate 	 * Transfer Descriptors (QTD) associated with it,  then
34007c478bd9Sstevel@tonic-gate 	 * remove the transfer wrapper.
34017c478bd9Sstevel@tonic-gate 	 */
34027c478bd9Sstevel@tonic-gate 	if (tw->tw_qtd_head) {
34037c478bd9Sstevel@tonic-gate 		ASSERT(tw->tw_qtd_tail != NULL);
34047c478bd9Sstevel@tonic-gate 
34057c478bd9Sstevel@tonic-gate 		return;
34067c478bd9Sstevel@tonic-gate 	}
34077c478bd9Sstevel@tonic-gate 
34087c478bd9Sstevel@tonic-gate 	ASSERT(tw->tw_qtd_tail == NULL);
34097c478bd9Sstevel@tonic-gate 
34107c478bd9Sstevel@tonic-gate 	/* Make sure we return all the unused qtd's to the pool as well */
34117c478bd9Sstevel@tonic-gate 	ehci_free_tw_td_resources(ehcip, tw);
34127c478bd9Sstevel@tonic-gate 
34137c478bd9Sstevel@tonic-gate 	/*
34147c478bd9Sstevel@tonic-gate 	 * If pp->pp_tw_head and pp->pp_tw_tail are pointing to
34157c478bd9Sstevel@tonic-gate 	 * given TW then set the head and  tail  equal to NULL.
34167c478bd9Sstevel@tonic-gate 	 * Otherwise search for this TW in the linked TW's list
34177c478bd9Sstevel@tonic-gate 	 * and then remove this TW from the list.
34187c478bd9Sstevel@tonic-gate 	 */
34197c478bd9Sstevel@tonic-gate 	if (pp->pp_tw_head == tw) {
34207c478bd9Sstevel@tonic-gate 		if (pp->pp_tw_tail == tw) {
34217c478bd9Sstevel@tonic-gate 			pp->pp_tw_head = NULL;
34227c478bd9Sstevel@tonic-gate 			pp->pp_tw_tail = NULL;
34237c478bd9Sstevel@tonic-gate 		} else {
34247c478bd9Sstevel@tonic-gate 			pp->pp_tw_head = tw->tw_next;
34257c478bd9Sstevel@tonic-gate 		}
34267c478bd9Sstevel@tonic-gate 	} else {
34277c478bd9Sstevel@tonic-gate 		prev = pp->pp_tw_head;
34287c478bd9Sstevel@tonic-gate 		next = prev->tw_next;
34297c478bd9Sstevel@tonic-gate 
34307c478bd9Sstevel@tonic-gate 		while (next && (next != tw)) {
34317c478bd9Sstevel@tonic-gate 			prev = next;
34327c478bd9Sstevel@tonic-gate 			next = next->tw_next;
34337c478bd9Sstevel@tonic-gate 		}
34347c478bd9Sstevel@tonic-gate 
34357c478bd9Sstevel@tonic-gate 		if (next == tw) {
34367c478bd9Sstevel@tonic-gate 			prev->tw_next = next->tw_next;
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate 			if (pp->pp_tw_tail == tw) {
34397c478bd9Sstevel@tonic-gate 				pp->pp_tw_tail = prev;
34407c478bd9Sstevel@tonic-gate 			}
34417c478bd9Sstevel@tonic-gate 		}
34427c478bd9Sstevel@tonic-gate 	}
34437c478bd9Sstevel@tonic-gate 
34447c478bd9Sstevel@tonic-gate 	/*
34457c478bd9Sstevel@tonic-gate 	 * Make sure that, this TW has been removed
34467c478bd9Sstevel@tonic-gate 	 * from the timeout list.
34477c478bd9Sstevel@tonic-gate 	 */
34487c478bd9Sstevel@tonic-gate 	ehci_remove_tw_from_timeout_list(ehcip, tw);
34497c478bd9Sstevel@tonic-gate 
34507c478bd9Sstevel@tonic-gate 	/* Deallocate this TW */
34517c478bd9Sstevel@tonic-gate 	ehci_free_tw(ehcip, pp, tw);
34527c478bd9Sstevel@tonic-gate }
34537c478bd9Sstevel@tonic-gate 
34547c478bd9Sstevel@tonic-gate 
34557c478bd9Sstevel@tonic-gate /*
34567c478bd9Sstevel@tonic-gate  * ehci_free_dma_resources:
34577c478bd9Sstevel@tonic-gate  *
34587c478bd9Sstevel@tonic-gate  * Free dma resources of a Transfer Wrapper (TW) and also free the TW.
34597c478bd9Sstevel@tonic-gate  *
34607c478bd9Sstevel@tonic-gate  * NOTE: This function is also called from POLLED MODE.
34617c478bd9Sstevel@tonic-gate  */
34627c478bd9Sstevel@tonic-gate void
34637c478bd9Sstevel@tonic-gate ehci_free_dma_resources(
34647c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
34657c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph)
34667c478bd9Sstevel@tonic-gate {
34677c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
34687c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*head_tw = pp->pp_tw_head;
34697c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*next_tw, *tw;
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
34727c478bd9Sstevel@tonic-gate 	    "ehci_free_dma_resources: ph = 0x%p", (void *)ph);
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
34757c478bd9Sstevel@tonic-gate 
34767c478bd9Sstevel@tonic-gate 	/* Process the Transfer Wrappers */
34777c478bd9Sstevel@tonic-gate 	next_tw = head_tw;
34787c478bd9Sstevel@tonic-gate 	while (next_tw) {
34797c478bd9Sstevel@tonic-gate 		tw = next_tw;
34807c478bd9Sstevel@tonic-gate 		next_tw = tw->tw_next;
34817c478bd9Sstevel@tonic-gate 
34827c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
34837c478bd9Sstevel@tonic-gate 		    "ehci_free_dma_resources: Free TW = 0x%p", (void *)tw);
34847c478bd9Sstevel@tonic-gate 
34857c478bd9Sstevel@tonic-gate 		ehci_free_tw(ehcip, pp, tw);
34867c478bd9Sstevel@tonic-gate 	}
34877c478bd9Sstevel@tonic-gate 
34887c478bd9Sstevel@tonic-gate 	/* Adjust the head and tail pointers */
34897c478bd9Sstevel@tonic-gate 	pp->pp_tw_head = NULL;
34907c478bd9Sstevel@tonic-gate 	pp->pp_tw_tail = NULL;
34917c478bd9Sstevel@tonic-gate }
34927c478bd9Sstevel@tonic-gate 
34937c478bd9Sstevel@tonic-gate 
34947c478bd9Sstevel@tonic-gate /*
34957c478bd9Sstevel@tonic-gate  * ehci_free_tw:
34967c478bd9Sstevel@tonic-gate  *
34977c478bd9Sstevel@tonic-gate  * Free the Transfer Wrapper (TW).
34987c478bd9Sstevel@tonic-gate  */
34997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
35007c478bd9Sstevel@tonic-gate static void
35017c478bd9Sstevel@tonic-gate ehci_free_tw(
35027c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
35037c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
35047c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw)
35057c478bd9Sstevel@tonic-gate {
35067c478bd9Sstevel@tonic-gate 	int	rval;
35077c478bd9Sstevel@tonic-gate 
35087c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
35097c478bd9Sstevel@tonic-gate 	    "ehci_free_tw: tw = 0x%p", tw);
35107c478bd9Sstevel@tonic-gate 
35117c478bd9Sstevel@tonic-gate 	ASSERT(tw != NULL);
35127c478bd9Sstevel@tonic-gate 	ASSERT(tw->tw_id != NULL);
35137c478bd9Sstevel@tonic-gate 
35147c478bd9Sstevel@tonic-gate 	/* Free 32bit ID */
35157c478bd9Sstevel@tonic-gate 	EHCI_FREE_ID((uint32_t)tw->tw_id);
35167c478bd9Sstevel@tonic-gate 
35177c478bd9Sstevel@tonic-gate 	rval = ddi_dma_unbind_handle(tw->tw_dmahandle);
35187c478bd9Sstevel@tonic-gate 	ASSERT(rval == DDI_SUCCESS);
35197c478bd9Sstevel@tonic-gate 
35207c478bd9Sstevel@tonic-gate 	ddi_dma_mem_free(&tw->tw_accesshandle);
35217c478bd9Sstevel@tonic-gate 	ddi_dma_free_handle(&tw->tw_dmahandle);
35227c478bd9Sstevel@tonic-gate 
35237c478bd9Sstevel@tonic-gate 	/* Free transfer wrapper */
35247c478bd9Sstevel@tonic-gate 	kmem_free(tw, sizeof (ehci_trans_wrapper_t));
35257c478bd9Sstevel@tonic-gate }
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate 
35287c478bd9Sstevel@tonic-gate /*
35297c478bd9Sstevel@tonic-gate  * Miscellaneous functions
35307c478bd9Sstevel@tonic-gate  */
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate /*
35337c478bd9Sstevel@tonic-gate  * ehci_allocate_intr_in_resource
35347c478bd9Sstevel@tonic-gate  *
35357c478bd9Sstevel@tonic-gate  * Allocate interrupt request structure for the interrupt IN transfer.
35367c478bd9Sstevel@tonic-gate  */
35377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
35387c478bd9Sstevel@tonic-gate int
35397c478bd9Sstevel@tonic-gate ehci_allocate_intr_in_resource(
35407c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
35417c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
35427c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw,
35437c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
35447c478bd9Sstevel@tonic-gate {
35457c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph = pp->pp_pipe_handle;
35467c478bd9Sstevel@tonic-gate 	usb_intr_req_t		*curr_intr_reqp;
35477c478bd9Sstevel@tonic-gate 	usb_opaque_t		client_periodic_in_reqp;
35487c478bd9Sstevel@tonic-gate 	size_t			length = 0;
35497c478bd9Sstevel@tonic-gate 
35507c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
35517c478bd9Sstevel@tonic-gate 	    "ehci_allocate_intr_in_resource:"
35527c478bd9Sstevel@tonic-gate 	    "pp = 0x%p tw = 0x%p flags = 0x%x", pp, tw, flags);
35537c478bd9Sstevel@tonic-gate 
35547c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
35557c478bd9Sstevel@tonic-gate 	ASSERT(tw->tw_curr_xfer_reqp == NULL);
35567c478bd9Sstevel@tonic-gate 
35577c478bd9Sstevel@tonic-gate 	/* Get the client periodic in request pointer */
35587c478bd9Sstevel@tonic-gate 	client_periodic_in_reqp = pp->pp_client_periodic_in_reqp;
35597c478bd9Sstevel@tonic-gate 
35607c478bd9Sstevel@tonic-gate 	/*
35617c478bd9Sstevel@tonic-gate 	 * If it a periodic IN request and periodic request is NULL,
35627c478bd9Sstevel@tonic-gate 	 * allocate corresponding usb periodic IN request for the
35637c478bd9Sstevel@tonic-gate 	 * current periodic polling request and copy the information
35647c478bd9Sstevel@tonic-gate 	 * from the saved periodic request structure.
35657c478bd9Sstevel@tonic-gate 	 */
35667c478bd9Sstevel@tonic-gate 	if (client_periodic_in_reqp) {
35677c478bd9Sstevel@tonic-gate 
35687c478bd9Sstevel@tonic-gate 		/* Get the interrupt transfer length */
35697c478bd9Sstevel@tonic-gate 		length = ((usb_intr_req_t *)
35707c478bd9Sstevel@tonic-gate 		    client_periodic_in_reqp)->intr_len;
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate 		curr_intr_reqp = usba_hcdi_dup_intr_req(ph->p_dip,
35737c478bd9Sstevel@tonic-gate 		    (usb_intr_req_t *)client_periodic_in_reqp, length, flags);
35747c478bd9Sstevel@tonic-gate 	} else {
35757c478bd9Sstevel@tonic-gate 		curr_intr_reqp = usb_alloc_intr_req(ph->p_dip, length, flags);
35767c478bd9Sstevel@tonic-gate 	}
35777c478bd9Sstevel@tonic-gate 
35787c478bd9Sstevel@tonic-gate 	if (curr_intr_reqp == NULL) {
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
35817c478bd9Sstevel@tonic-gate 		    "ehci_allocate_intr_in_resource: Interrupt"
35827c478bd9Sstevel@tonic-gate 		    "request structure allocation failed");
35837c478bd9Sstevel@tonic-gate 
35847c478bd9Sstevel@tonic-gate 		return (USB_NO_RESOURCES);
35857c478bd9Sstevel@tonic-gate 	}
35867c478bd9Sstevel@tonic-gate 
35877c478bd9Sstevel@tonic-gate 	/* For polled mode */
35887c478bd9Sstevel@tonic-gate 	if (client_periodic_in_reqp == NULL) {
35897c478bd9Sstevel@tonic-gate 		curr_intr_reqp->intr_attributes = USB_ATTRS_SHORT_XFER_OK;
35907c478bd9Sstevel@tonic-gate 		curr_intr_reqp->intr_len = ph->p_ep.wMaxPacketSize;
35917c478bd9Sstevel@tonic-gate 	} else {
35927c478bd9Sstevel@tonic-gate 		/* Check and save the timeout value */
35937c478bd9Sstevel@tonic-gate 		tw->tw_timeout = (curr_intr_reqp->intr_attributes &
35947c478bd9Sstevel@tonic-gate 		    USB_ATTRS_ONE_XFER) ? curr_intr_reqp->intr_timeout: 0;
35957c478bd9Sstevel@tonic-gate 	}
35967c478bd9Sstevel@tonic-gate 
35977c478bd9Sstevel@tonic-gate 	tw->tw_curr_xfer_reqp = (usb_opaque_t)curr_intr_reqp;
35987c478bd9Sstevel@tonic-gate 	tw->tw_length = curr_intr_reqp->intr_len;
35997c478bd9Sstevel@tonic-gate 
36007c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_mutex);
36017c478bd9Sstevel@tonic-gate 	ph->p_req_count++;
36027c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_mutex);
36037c478bd9Sstevel@tonic-gate 
36047c478bd9Sstevel@tonic-gate 	pp->pp_state = EHCI_PIPE_STATE_ACTIVE;
36057c478bd9Sstevel@tonic-gate 
36067c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
36077c478bd9Sstevel@tonic-gate }
36087c478bd9Sstevel@tonic-gate 
36097c478bd9Sstevel@tonic-gate /*
36107c478bd9Sstevel@tonic-gate  * ehci_pipe_cleanup
36117c478bd9Sstevel@tonic-gate  *
36127c478bd9Sstevel@tonic-gate  * Cleanup ehci pipe.
36137c478bd9Sstevel@tonic-gate  */
36147c478bd9Sstevel@tonic-gate void
36157c478bd9Sstevel@tonic-gate ehci_pipe_cleanup(
36167c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
36177c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph)
36187c478bd9Sstevel@tonic-gate {
36197c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
36207c478bd9Sstevel@tonic-gate 	uint_t			pipe_state = pp->pp_state;
36217c478bd9Sstevel@tonic-gate 	usb_cr_t		completion_reason;
36227c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*eptd = &ph->p_ep;
36237c478bd9Sstevel@tonic-gate 
36247c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
36257c478bd9Sstevel@tonic-gate 	    "ehci_pipe_cleanup: ph = 0x%p", ph);
36267c478bd9Sstevel@tonic-gate 
36277c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
36287c478bd9Sstevel@tonic-gate 
36297c478bd9Sstevel@tonic-gate 	if (EHCI_ISOC_ENDPOINT(eptd)) {
36307c478bd9Sstevel@tonic-gate 		ehci_isoc_pipe_cleanup(ehcip, ph);
36317c478bd9Sstevel@tonic-gate 
36327c478bd9Sstevel@tonic-gate 		return;
36337c478bd9Sstevel@tonic-gate 	}
36347c478bd9Sstevel@tonic-gate 
36357c478bd9Sstevel@tonic-gate 	ASSERT(!servicing_interrupt());
36367c478bd9Sstevel@tonic-gate 
36377c478bd9Sstevel@tonic-gate 	/*
36387c478bd9Sstevel@tonic-gate 	 * Set the QH's status to Halt condition.
36397c478bd9Sstevel@tonic-gate 	 * If another thread is halting this function will automatically
36407c478bd9Sstevel@tonic-gate 	 * wait. If a pipe close happens at this time
36417c478bd9Sstevel@tonic-gate 	 * we will be in lots of trouble.
36427c478bd9Sstevel@tonic-gate 	 * If we are in an interrupt thread, don't halt, because it may
36437c478bd9Sstevel@tonic-gate 	 * do a wait_for_sof.
36447c478bd9Sstevel@tonic-gate 	 */
36457c478bd9Sstevel@tonic-gate 	ehci_modify_qh_status_bit(ehcip, pp, SET_HALT);
36467c478bd9Sstevel@tonic-gate 
36477c478bd9Sstevel@tonic-gate 	/*
36487c478bd9Sstevel@tonic-gate 	 * Wait for processing all completed transfers and
36497c478bd9Sstevel@tonic-gate 	 * to send results to upstream.
36507c478bd9Sstevel@tonic-gate 	 */
36517c478bd9Sstevel@tonic-gate 	ehci_wait_for_transfers_completion(ehcip, pp);
36527c478bd9Sstevel@tonic-gate 
36537c478bd9Sstevel@tonic-gate 	/* Save the data toggle information */
36547c478bd9Sstevel@tonic-gate 	ehci_save_data_toggle(ehcip, ph);
36557c478bd9Sstevel@tonic-gate 
36567c478bd9Sstevel@tonic-gate 	/*
36577c478bd9Sstevel@tonic-gate 	 * Traverse the list of QTDs for this pipe using transfer
36587c478bd9Sstevel@tonic-gate 	 * wrapper. Process these QTDs depending on their status.
36597c478bd9Sstevel@tonic-gate 	 * And stop the timer of this pipe.
36607c478bd9Sstevel@tonic-gate 	 */
36617c478bd9Sstevel@tonic-gate 	ehci_traverse_qtds(ehcip, ph);
36627c478bd9Sstevel@tonic-gate 
36637c478bd9Sstevel@tonic-gate 	/* Make sure the timer is not running */
36647c478bd9Sstevel@tonic-gate 	ASSERT(pp->pp_timer_id == 0);
36657c478bd9Sstevel@tonic-gate 
36667c478bd9Sstevel@tonic-gate 	/* Do callbacks for all unfinished requests */
36677c478bd9Sstevel@tonic-gate 	ehci_handle_outstanding_requests(ehcip, pp);
36687c478bd9Sstevel@tonic-gate 
36697c478bd9Sstevel@tonic-gate 	/* Free DMA resources */
36707c478bd9Sstevel@tonic-gate 	ehci_free_dma_resources(ehcip, ph);
36717c478bd9Sstevel@tonic-gate 
36727c478bd9Sstevel@tonic-gate 	switch (pipe_state) {
36737c478bd9Sstevel@tonic-gate 	case EHCI_PIPE_STATE_CLOSE:
36747c478bd9Sstevel@tonic-gate 		completion_reason = USB_CR_PIPE_CLOSING;
36757c478bd9Sstevel@tonic-gate 		break;
36767c478bd9Sstevel@tonic-gate 	case EHCI_PIPE_STATE_RESET:
36777c478bd9Sstevel@tonic-gate 	case EHCI_PIPE_STATE_STOP_POLLING:
36787c478bd9Sstevel@tonic-gate 		/* Set completion reason */
36797c478bd9Sstevel@tonic-gate 		completion_reason = (pipe_state ==
36807c478bd9Sstevel@tonic-gate 		    EHCI_PIPE_STATE_RESET) ?
36817c478bd9Sstevel@tonic-gate 		    USB_CR_PIPE_RESET: USB_CR_STOPPED_POLLING;
36827c478bd9Sstevel@tonic-gate 
36837c478bd9Sstevel@tonic-gate 		/* Restore the data toggle information */
36847c478bd9Sstevel@tonic-gate 		ehci_restore_data_toggle(ehcip, ph);
36857c478bd9Sstevel@tonic-gate 
36867c478bd9Sstevel@tonic-gate 		/*
36877c478bd9Sstevel@tonic-gate 		 * Clear the halt bit to restart all the
36887c478bd9Sstevel@tonic-gate 		 * transactions on this pipe.
36897c478bd9Sstevel@tonic-gate 		 */
36907c478bd9Sstevel@tonic-gate 		ehci_modify_qh_status_bit(ehcip, pp, CLEAR_HALT);
36917c478bd9Sstevel@tonic-gate 
36927c478bd9Sstevel@tonic-gate 		/* Set pipe state to idle */
36937c478bd9Sstevel@tonic-gate 		pp->pp_state = EHCI_PIPE_STATE_IDLE;
36947c478bd9Sstevel@tonic-gate 
36957c478bd9Sstevel@tonic-gate 		break;
36967c478bd9Sstevel@tonic-gate 	}
36977c478bd9Sstevel@tonic-gate 
36987c478bd9Sstevel@tonic-gate 	/*
36997c478bd9Sstevel@tonic-gate 	 * Do the callback for the original client
37007c478bd9Sstevel@tonic-gate 	 * periodic IN request.
37017c478bd9Sstevel@tonic-gate 	 */
37027c478bd9Sstevel@tonic-gate 	if ((EHCI_PERIODIC_ENDPOINT(eptd)) &&
37037c478bd9Sstevel@tonic-gate 	    ((ph->p_ep.bEndpointAddress & USB_EP_DIR_MASK) ==
37047c478bd9Sstevel@tonic-gate 	    USB_EP_DIR_IN)) {
37057c478bd9Sstevel@tonic-gate 
37067c478bd9Sstevel@tonic-gate 		ehci_do_client_periodic_in_req_callback(
37077c478bd9Sstevel@tonic-gate 		    ehcip, pp, completion_reason);
37087c478bd9Sstevel@tonic-gate 	}
37097c478bd9Sstevel@tonic-gate }
37107c478bd9Sstevel@tonic-gate 
37117c478bd9Sstevel@tonic-gate 
37127c478bd9Sstevel@tonic-gate /*
37137c478bd9Sstevel@tonic-gate  * ehci_wait_for_transfers_completion:
37147c478bd9Sstevel@tonic-gate  *
37157c478bd9Sstevel@tonic-gate  * Wait for processing all completed transfers and to send results
37167c478bd9Sstevel@tonic-gate  * to upstream.
37177c478bd9Sstevel@tonic-gate  */
37187c478bd9Sstevel@tonic-gate static void
37197c478bd9Sstevel@tonic-gate ehci_wait_for_transfers_completion(
37207c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
37217c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp)
37227c478bd9Sstevel@tonic-gate {
37237c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*next_tw = pp->pp_tw_head;
37247c478bd9Sstevel@tonic-gate 	clock_t			xfer_cmpl_time_wait;
37257c478bd9Sstevel@tonic-gate 	ehci_qtd_t		*qtd;
37267c478bd9Sstevel@tonic-gate 
37277c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS,
37287c478bd9Sstevel@tonic-gate 	    ehcip->ehci_log_hdl,
37297c478bd9Sstevel@tonic-gate 	    "ehci_wait_for_transfers_completion: pp = 0x%p", pp);
37307c478bd9Sstevel@tonic-gate 
37317c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
37327c478bd9Sstevel@tonic-gate 
37337c478bd9Sstevel@tonic-gate 	if ((ehci_state_is_operational(ehcip)) != USB_SUCCESS) {
37347c478bd9Sstevel@tonic-gate 
37357c478bd9Sstevel@tonic-gate 		return;
37367c478bd9Sstevel@tonic-gate 	}
37377c478bd9Sstevel@tonic-gate 
37387c478bd9Sstevel@tonic-gate 	pp->pp_count_done_qtds = 0;
37397c478bd9Sstevel@tonic-gate 
37407c478bd9Sstevel@tonic-gate 	/* Process the transfer wrappers for this pipe */
37417c478bd9Sstevel@tonic-gate 	while (next_tw) {
37427c478bd9Sstevel@tonic-gate 		qtd = (ehci_qtd_t *)next_tw->tw_qtd_head;
37437c478bd9Sstevel@tonic-gate 
37447c478bd9Sstevel@tonic-gate 		/*
37457c478bd9Sstevel@tonic-gate 		 * Walk through each QTD for this transfer wrapper.
37467c478bd9Sstevel@tonic-gate 		 * If a QTD still exists, then it is either on done
37477c478bd9Sstevel@tonic-gate 		 * list or on the QH's list.
37487c478bd9Sstevel@tonic-gate 		 */
37497c478bd9Sstevel@tonic-gate 		while (qtd) {
37507c478bd9Sstevel@tonic-gate 			if (!(Get_QTD(qtd->qtd_ctrl) &
37517c478bd9Sstevel@tonic-gate 			    EHCI_QTD_CTRL_ACTIVE_XACT)) {
37527c478bd9Sstevel@tonic-gate 				pp->pp_count_done_qtds++;
37537c478bd9Sstevel@tonic-gate 			}
37547c478bd9Sstevel@tonic-gate 
37557c478bd9Sstevel@tonic-gate 			qtd = ehci_qtd_iommu_to_cpu(ehcip,
37567c478bd9Sstevel@tonic-gate 			    Get_QTD(qtd->qtd_tw_next_qtd));
37577c478bd9Sstevel@tonic-gate 		}
37587c478bd9Sstevel@tonic-gate 
37597c478bd9Sstevel@tonic-gate 		next_tw = next_tw->tw_next;
37607c478bd9Sstevel@tonic-gate 	}
37617c478bd9Sstevel@tonic-gate 
37627c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
37637c478bd9Sstevel@tonic-gate 	    "ehci_wait_for_transfers_completion: count_done_qtds = 0x%x",
37647c478bd9Sstevel@tonic-gate 	    pp->pp_count_done_qtds);
37657c478bd9Sstevel@tonic-gate 
37667c478bd9Sstevel@tonic-gate 	if (!pp->pp_count_done_qtds) {
37677c478bd9Sstevel@tonic-gate 
37687c478bd9Sstevel@tonic-gate 		return;
37697c478bd9Sstevel@tonic-gate 	}
37707c478bd9Sstevel@tonic-gate 
37717c478bd9Sstevel@tonic-gate 	/* Get the number of clock ticks to wait */
37727c478bd9Sstevel@tonic-gate 	xfer_cmpl_time_wait = drv_usectohz(EHCI_XFER_CMPL_TIMEWAIT * 1000000);
37737c478bd9Sstevel@tonic-gate 
37747c478bd9Sstevel@tonic-gate 	(void) cv_timedwait(&pp->pp_xfer_cmpl_cv,
37757c478bd9Sstevel@tonic-gate 	    &ehcip->ehci_int_mutex,
37767c478bd9Sstevel@tonic-gate 	    ddi_get_lbolt() + xfer_cmpl_time_wait);
37777c478bd9Sstevel@tonic-gate 
37787c478bd9Sstevel@tonic-gate 	if (pp->pp_count_done_qtds) {
37797c478bd9Sstevel@tonic-gate 
37807c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
37817c478bd9Sstevel@tonic-gate 		    "ehci_wait_for_transfers_completion:"
37827c478bd9Sstevel@tonic-gate 		    "No transfers completion confirmation received");
37837c478bd9Sstevel@tonic-gate 	}
37847c478bd9Sstevel@tonic-gate }
37857c478bd9Sstevel@tonic-gate 
37867c478bd9Sstevel@tonic-gate /*
37877c478bd9Sstevel@tonic-gate  * ehci_check_for_transfers_completion:
37887c478bd9Sstevel@tonic-gate  *
37897c478bd9Sstevel@tonic-gate  * Check whether anybody is waiting for transfers completion event. If so, send
37907c478bd9Sstevel@tonic-gate  * this event and also stop initiating any new transfers on this pipe.
37917c478bd9Sstevel@tonic-gate  */
37927c478bd9Sstevel@tonic-gate void
37937c478bd9Sstevel@tonic-gate ehci_check_for_transfers_completion(
37947c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
37957c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp)
37967c478bd9Sstevel@tonic-gate {
37977c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS,
37987c478bd9Sstevel@tonic-gate 	    ehcip->ehci_log_hdl,
37997c478bd9Sstevel@tonic-gate 	    "ehci_check_for_transfers_completion: pp = 0x%p", pp);
38007c478bd9Sstevel@tonic-gate 
38017c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
38027c478bd9Sstevel@tonic-gate 
38037c478bd9Sstevel@tonic-gate 	if ((pp->pp_state == EHCI_PIPE_STATE_STOP_POLLING) &&
38047c478bd9Sstevel@tonic-gate 	    (pp->pp_error == USB_CR_NO_RESOURCES) &&
38057c478bd9Sstevel@tonic-gate 	    (pp->pp_cur_periodic_req_cnt == 0)) {
38067c478bd9Sstevel@tonic-gate 
38077c478bd9Sstevel@tonic-gate 		/* Reset pipe error to zero */
38087c478bd9Sstevel@tonic-gate 		pp->pp_error = 0;
38097c478bd9Sstevel@tonic-gate 
38107c478bd9Sstevel@tonic-gate 		/* Do callback for original request */
38117c478bd9Sstevel@tonic-gate 		ehci_do_client_periodic_in_req_callback(
38127c478bd9Sstevel@tonic-gate 		    ehcip, pp, USB_CR_NO_RESOURCES);
38137c478bd9Sstevel@tonic-gate 	}
38147c478bd9Sstevel@tonic-gate 
38157c478bd9Sstevel@tonic-gate 	if (pp->pp_count_done_qtds) {
38167c478bd9Sstevel@tonic-gate 
38177c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
38187c478bd9Sstevel@tonic-gate 		    "ehci_check_for_transfers_completion:"
38197c478bd9Sstevel@tonic-gate 		    "count_done_qtds = 0x%x", pp->pp_count_done_qtds);
38207c478bd9Sstevel@tonic-gate 
38217c478bd9Sstevel@tonic-gate 		/* Decrement the done qtd count */
38227c478bd9Sstevel@tonic-gate 		pp->pp_count_done_qtds--;
38237c478bd9Sstevel@tonic-gate 
38247c478bd9Sstevel@tonic-gate 		if (!pp->pp_count_done_qtds) {
38257c478bd9Sstevel@tonic-gate 
38267c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
38277c478bd9Sstevel@tonic-gate 			    "ehci_check_for_transfers_completion:"
38287c478bd9Sstevel@tonic-gate 			    "Sent transfers completion event pp = 0x%p", pp);
38297c478bd9Sstevel@tonic-gate 
38307c478bd9Sstevel@tonic-gate 			/* Send the transfer completion signal */
38317c478bd9Sstevel@tonic-gate 			cv_signal(&pp->pp_xfer_cmpl_cv);
38327c478bd9Sstevel@tonic-gate 		}
38337c478bd9Sstevel@tonic-gate 	}
38347c478bd9Sstevel@tonic-gate }
38357c478bd9Sstevel@tonic-gate 
38367c478bd9Sstevel@tonic-gate 
38377c478bd9Sstevel@tonic-gate /*
38387c478bd9Sstevel@tonic-gate  * ehci_save_data_toggle:
38397c478bd9Sstevel@tonic-gate  *
38407c478bd9Sstevel@tonic-gate  * Save the data toggle information.
38417c478bd9Sstevel@tonic-gate  */
38427c478bd9Sstevel@tonic-gate static void
38437c478bd9Sstevel@tonic-gate ehci_save_data_toggle(
38447c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
38457c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph)
38467c478bd9Sstevel@tonic-gate {
38477c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
38487c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*eptd = &ph->p_ep;
38497c478bd9Sstevel@tonic-gate 	uint_t			data_toggle;
38507c478bd9Sstevel@tonic-gate 	usb_cr_t		error = pp->pp_error;
38517c478bd9Sstevel@tonic-gate 	ehci_qh_t		*qh = pp->pp_qh;
38527c478bd9Sstevel@tonic-gate 
38537c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS,
38547c478bd9Sstevel@tonic-gate 	    ehcip->ehci_log_hdl,
38557c478bd9Sstevel@tonic-gate 	    "ehci_save_data_toggle: ph = 0x%p", ph);
38567c478bd9Sstevel@tonic-gate 
38577c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
38587c478bd9Sstevel@tonic-gate 
38597c478bd9Sstevel@tonic-gate 	/* Reset the pipe error value */
38607c478bd9Sstevel@tonic-gate 	pp->pp_error = USB_CR_OK;
38617c478bd9Sstevel@tonic-gate 
38627c478bd9Sstevel@tonic-gate 	/* Return immediately if it is a control pipe */
38637c478bd9Sstevel@tonic-gate 	if ((eptd->bmAttributes & USB_EP_ATTR_MASK) ==
38647c478bd9Sstevel@tonic-gate 	    USB_EP_ATTR_CONTROL) {
38657c478bd9Sstevel@tonic-gate 
38667c478bd9Sstevel@tonic-gate 		return;
38677c478bd9Sstevel@tonic-gate 	}
38687c478bd9Sstevel@tonic-gate 
38697c478bd9Sstevel@tonic-gate 	/* Get the data toggle information from the endpoint (QH) */
38707c478bd9Sstevel@tonic-gate 	data_toggle = (Get_QH(qh->qh_status) &
38717c478bd9Sstevel@tonic-gate 	    EHCI_QH_STS_DATA_TOGGLE)? DATA1:DATA0;
38727c478bd9Sstevel@tonic-gate 
38737c478bd9Sstevel@tonic-gate 	/*
38747c478bd9Sstevel@tonic-gate 	 * If error is STALL, then, set
38757c478bd9Sstevel@tonic-gate 	 * data toggle to zero.
38767c478bd9Sstevel@tonic-gate 	 */
38777c478bd9Sstevel@tonic-gate 	if (error == USB_CR_STALL) {
38787c478bd9Sstevel@tonic-gate 		data_toggle = DATA0;
38797c478bd9Sstevel@tonic-gate 	}
38807c478bd9Sstevel@tonic-gate 
38817c478bd9Sstevel@tonic-gate 	/*
38827c478bd9Sstevel@tonic-gate 	 * Save the data toggle information
38837c478bd9Sstevel@tonic-gate 	 * in the usb device structure.
38847c478bd9Sstevel@tonic-gate 	 */
38857c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_mutex);
38867c478bd9Sstevel@tonic-gate 	usba_hcdi_set_data_toggle(ph->p_usba_device, ph->p_ep.bEndpointAddress,
38877c478bd9Sstevel@tonic-gate 	    data_toggle);
38887c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_mutex);
38897c478bd9Sstevel@tonic-gate }
38907c478bd9Sstevel@tonic-gate 
38917c478bd9Sstevel@tonic-gate 
38927c478bd9Sstevel@tonic-gate /*
38937c478bd9Sstevel@tonic-gate  * ehci_restore_data_toggle:
38947c478bd9Sstevel@tonic-gate  *
38957c478bd9Sstevel@tonic-gate  * Restore the data toggle information.
38967c478bd9Sstevel@tonic-gate  */
38977c478bd9Sstevel@tonic-gate void
38987c478bd9Sstevel@tonic-gate ehci_restore_data_toggle(
38997c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
39007c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph)
39017c478bd9Sstevel@tonic-gate {
39027c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
39037c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*eptd = &ph->p_ep;
39047c478bd9Sstevel@tonic-gate 	uint_t			data_toggle = 0;
39057c478bd9Sstevel@tonic-gate 
39067c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS,
39077c478bd9Sstevel@tonic-gate 	    ehcip->ehci_log_hdl,
39087c478bd9Sstevel@tonic-gate 	    "ehci_restore_data_toggle: ph = 0x%p", ph);
39097c478bd9Sstevel@tonic-gate 
39107c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
39117c478bd9Sstevel@tonic-gate 
39127c478bd9Sstevel@tonic-gate 	/* Return immediately if it is a control pipe */
39137c478bd9Sstevel@tonic-gate 	if ((eptd->bmAttributes & USB_EP_ATTR_MASK) ==
39147c478bd9Sstevel@tonic-gate 	    USB_EP_ATTR_CONTROL) {
39157c478bd9Sstevel@tonic-gate 
39167c478bd9Sstevel@tonic-gate 		return;
39177c478bd9Sstevel@tonic-gate 	}
39187c478bd9Sstevel@tonic-gate 
39197c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_mutex);
39207c478bd9Sstevel@tonic-gate 
39217c478bd9Sstevel@tonic-gate 	data_toggle = usba_hcdi_get_data_toggle(ph->p_usba_device,
39227c478bd9Sstevel@tonic-gate 	    ph->p_ep.bEndpointAddress);
39237c478bd9Sstevel@tonic-gate 	usba_hcdi_set_data_toggle(ph->p_usba_device, ph->p_ep.bEndpointAddress,
39247c478bd9Sstevel@tonic-gate 	    0);
39257c478bd9Sstevel@tonic-gate 
39267c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_mutex);
39277c478bd9Sstevel@tonic-gate 
39287c478bd9Sstevel@tonic-gate 	/*
39297c478bd9Sstevel@tonic-gate 	 * Restore the data toggle bit depending on the
39307c478bd9Sstevel@tonic-gate 	 * previous data toggle information.
39317c478bd9Sstevel@tonic-gate 	 */
39327c478bd9Sstevel@tonic-gate 	if (data_toggle) {
39337c478bd9Sstevel@tonic-gate 		Set_QH(pp->pp_qh->qh_status,
39347c478bd9Sstevel@tonic-gate 		    Get_QH(pp->pp_qh->qh_status) | EHCI_QH_STS_DATA_TOGGLE);
39357c478bd9Sstevel@tonic-gate 	} else {
39367c478bd9Sstevel@tonic-gate 		Set_QH(pp->pp_qh->qh_status,
39377c478bd9Sstevel@tonic-gate 		    Get_QH(pp->pp_qh->qh_status) & (~EHCI_QH_STS_DATA_TOGGLE));
39387c478bd9Sstevel@tonic-gate 	}
39397c478bd9Sstevel@tonic-gate }
39407c478bd9Sstevel@tonic-gate 
39417c478bd9Sstevel@tonic-gate 
39427c478bd9Sstevel@tonic-gate /*
39437c478bd9Sstevel@tonic-gate  * ehci_handle_outstanding_requests
39447c478bd9Sstevel@tonic-gate  *
39457c478bd9Sstevel@tonic-gate  * Deallocate interrupt request structure for the interrupt IN transfer.
39467c478bd9Sstevel@tonic-gate  * Do the callbacks for all unfinished requests.
39477c478bd9Sstevel@tonic-gate  *
39487c478bd9Sstevel@tonic-gate  * NOTE: This function is also called from POLLED MODE.
39497c478bd9Sstevel@tonic-gate  */
39507c478bd9Sstevel@tonic-gate void
39517c478bd9Sstevel@tonic-gate ehci_handle_outstanding_requests(
39527c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
39537c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp)
39547c478bd9Sstevel@tonic-gate {
39557c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph = pp->pp_pipe_handle;
39567c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*eptd = &ph->p_ep;
39577c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*curr_tw;
39587c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*next_tw;
39597c478bd9Sstevel@tonic-gate 	usb_opaque_t		curr_xfer_reqp;
39607c478bd9Sstevel@tonic-gate 
39617c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS,
39627c478bd9Sstevel@tonic-gate 	    ehcip->ehci_log_hdl,
39637c478bd9Sstevel@tonic-gate 	    "ehci_handle_outstanding_requests: pp = 0x%p", pp);
39647c478bd9Sstevel@tonic-gate 
39657c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
39667c478bd9Sstevel@tonic-gate 
39677c478bd9Sstevel@tonic-gate 	/* Deallocate all pre-allocated interrupt requests */
39687c478bd9Sstevel@tonic-gate 	next_tw = pp->pp_tw_head;
39697c478bd9Sstevel@tonic-gate 
39707c478bd9Sstevel@tonic-gate 	while (next_tw) {
39717c478bd9Sstevel@tonic-gate 		curr_tw = next_tw;
39727c478bd9Sstevel@tonic-gate 		next_tw = curr_tw->tw_next;
39737c478bd9Sstevel@tonic-gate 
39747c478bd9Sstevel@tonic-gate 		curr_xfer_reqp = curr_tw->tw_curr_xfer_reqp;
39757c478bd9Sstevel@tonic-gate 
39767c478bd9Sstevel@tonic-gate 		/* Deallocate current interrupt request */
39777c478bd9Sstevel@tonic-gate 		if (curr_xfer_reqp) {
39787c478bd9Sstevel@tonic-gate 
39797c478bd9Sstevel@tonic-gate 			if ((EHCI_PERIODIC_ENDPOINT(eptd)) &&
39807c478bd9Sstevel@tonic-gate 			    (curr_tw->tw_direction == EHCI_QTD_CTRL_IN_PID)) {
39817c478bd9Sstevel@tonic-gate 
39827c478bd9Sstevel@tonic-gate 				/* Decrement periodic in request count */
39837c478bd9Sstevel@tonic-gate 				pp->pp_cur_periodic_req_cnt--;
39847c478bd9Sstevel@tonic-gate 
39857c478bd9Sstevel@tonic-gate 				ehci_deallocate_intr_in_resource(
39867c478bd9Sstevel@tonic-gate 				    ehcip, pp, curr_tw);
39877c478bd9Sstevel@tonic-gate 			} else {
39887c478bd9Sstevel@tonic-gate 				ehci_hcdi_callback(ph, curr_tw, USB_CR_FLUSHED);
39897c478bd9Sstevel@tonic-gate 			}
39907c478bd9Sstevel@tonic-gate 		}
39917c478bd9Sstevel@tonic-gate 	}
39927c478bd9Sstevel@tonic-gate }
39937c478bd9Sstevel@tonic-gate 
39947c478bd9Sstevel@tonic-gate 
39957c478bd9Sstevel@tonic-gate /*
39967c478bd9Sstevel@tonic-gate  * ehci_deallocate_intr_in_resource
39977c478bd9Sstevel@tonic-gate  *
39987c478bd9Sstevel@tonic-gate  * Deallocate interrupt request structure for the interrupt IN transfer.
39997c478bd9Sstevel@tonic-gate  */
40007c478bd9Sstevel@tonic-gate void
40017c478bd9Sstevel@tonic-gate ehci_deallocate_intr_in_resource(
40027c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
40037c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
40047c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw)
40057c478bd9Sstevel@tonic-gate {
40067c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph = pp->pp_pipe_handle;
40077c478bd9Sstevel@tonic-gate 	uchar_t			ep_attr = ph->p_ep.bmAttributes;
40087c478bd9Sstevel@tonic-gate 	usb_opaque_t		curr_xfer_reqp;
40097c478bd9Sstevel@tonic-gate 
40107c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS,
40117c478bd9Sstevel@tonic-gate 	    ehcip->ehci_log_hdl,
40127c478bd9Sstevel@tonic-gate 	    "ehci_deallocate_intr_in_resource: "
40137c478bd9Sstevel@tonic-gate 	    "pp = 0x%p tw = 0x%p", pp, tw);
40147c478bd9Sstevel@tonic-gate 
40157c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
40167c478bd9Sstevel@tonic-gate 	ASSERT((ep_attr & USB_EP_ATTR_MASK) == USB_EP_ATTR_INTR);
40177c478bd9Sstevel@tonic-gate 
40187c478bd9Sstevel@tonic-gate 	curr_xfer_reqp = tw->tw_curr_xfer_reqp;
40197c478bd9Sstevel@tonic-gate 
40207c478bd9Sstevel@tonic-gate 	/* Check the current periodic in request pointer */
40217c478bd9Sstevel@tonic-gate 	if (curr_xfer_reqp) {
40227c478bd9Sstevel@tonic-gate 
40237c478bd9Sstevel@tonic-gate 		tw->tw_curr_xfer_reqp = NULL;
40247c478bd9Sstevel@tonic-gate 
40257c478bd9Sstevel@tonic-gate 		mutex_enter(&ph->p_mutex);
40267c478bd9Sstevel@tonic-gate 		ph->p_req_count--;
40277c478bd9Sstevel@tonic-gate 		mutex_exit(&ph->p_mutex);
40287c478bd9Sstevel@tonic-gate 
40297c478bd9Sstevel@tonic-gate 		/* Free pre-allocated interrupt requests */
40307c478bd9Sstevel@tonic-gate 		usb_free_intr_req((usb_intr_req_t *)curr_xfer_reqp);
40317c478bd9Sstevel@tonic-gate 
40327c478bd9Sstevel@tonic-gate 		/* Set periodic in pipe state to idle */
40337c478bd9Sstevel@tonic-gate 		pp->pp_state = EHCI_PIPE_STATE_IDLE;
40347c478bd9Sstevel@tonic-gate 	}
40357c478bd9Sstevel@tonic-gate }
40367c478bd9Sstevel@tonic-gate 
40377c478bd9Sstevel@tonic-gate 
40387c478bd9Sstevel@tonic-gate /*
40397c478bd9Sstevel@tonic-gate  * ehci_do_client_periodic_in_req_callback
40407c478bd9Sstevel@tonic-gate  *
40417c478bd9Sstevel@tonic-gate  * Do callback for the original client periodic IN request.
40427c478bd9Sstevel@tonic-gate  */
40437c478bd9Sstevel@tonic-gate void
40447c478bd9Sstevel@tonic-gate ehci_do_client_periodic_in_req_callback(
40457c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
40467c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
40477c478bd9Sstevel@tonic-gate 	usb_cr_t		completion_reason)
40487c478bd9Sstevel@tonic-gate {
40497c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph = pp->pp_pipe_handle;
40507c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*eptd = &ph->p_ep;
40517c478bd9Sstevel@tonic-gate 
40527c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS,
40537c478bd9Sstevel@tonic-gate 	    ehcip->ehci_log_hdl,
40547c478bd9Sstevel@tonic-gate 	    "ehci_do_client_periodic_in_req_callback: "
40557c478bd9Sstevel@tonic-gate 	    "pp = 0x%p cc = 0x%x", pp, completion_reason);
40567c478bd9Sstevel@tonic-gate 
40577c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
40587c478bd9Sstevel@tonic-gate 
40597c478bd9Sstevel@tonic-gate 	/*
40607c478bd9Sstevel@tonic-gate 	 * Check for Interrupt/Isochronous IN, whether we need to do
40617c478bd9Sstevel@tonic-gate 	 * callback for the original client's periodic IN request.
40627c478bd9Sstevel@tonic-gate 	 */
40637c478bd9Sstevel@tonic-gate 	if (pp->pp_client_periodic_in_reqp) {
40647c478bd9Sstevel@tonic-gate 		ASSERT(pp->pp_cur_periodic_req_cnt == 0);
40657c478bd9Sstevel@tonic-gate 		if (EHCI_ISOC_ENDPOINT(eptd)) {
40667c478bd9Sstevel@tonic-gate 			ehci_hcdi_isoc_callback(ph, NULL, completion_reason);
40677c478bd9Sstevel@tonic-gate 		} else {
40687c478bd9Sstevel@tonic-gate 			ehci_hcdi_callback(ph, NULL, completion_reason);
40697c478bd9Sstevel@tonic-gate 		}
40707c478bd9Sstevel@tonic-gate 	}
40717c478bd9Sstevel@tonic-gate }
40727c478bd9Sstevel@tonic-gate 
40737c478bd9Sstevel@tonic-gate 
40747c478bd9Sstevel@tonic-gate /*
40757c478bd9Sstevel@tonic-gate  * ehci_hcdi_callback()
40767c478bd9Sstevel@tonic-gate  *
40777c478bd9Sstevel@tonic-gate  * Convenience wrapper around usba_hcdi_cb() other than root hub.
40787c478bd9Sstevel@tonic-gate  */
40797c478bd9Sstevel@tonic-gate void
40807c478bd9Sstevel@tonic-gate ehci_hcdi_callback(
40817c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
40827c478bd9Sstevel@tonic-gate 	ehci_trans_wrapper_t	*tw,
40837c478bd9Sstevel@tonic-gate 	usb_cr_t		completion_reason)
40847c478bd9Sstevel@tonic-gate {
40857c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip = ehci_obtain_state(
40867c478bd9Sstevel@tonic-gate 				    ph->p_usba_device->usb_root_hub_dip);
40877c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
40887c478bd9Sstevel@tonic-gate 	usb_opaque_t		curr_xfer_reqp;
40897c478bd9Sstevel@tonic-gate 	uint_t			pipe_state = 0;
40907c478bd9Sstevel@tonic-gate 
40917c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
40927c478bd9Sstevel@tonic-gate 	    "ehci_hcdi_callback: ph = 0x%p, tw = 0x%p, cr = 0x%x",
40937c478bd9Sstevel@tonic-gate 	    ph, tw, completion_reason);
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
40967c478bd9Sstevel@tonic-gate 
40977c478bd9Sstevel@tonic-gate 	/* Set the pipe state as per completion reason */
40987c478bd9Sstevel@tonic-gate 	switch (completion_reason) {
40997c478bd9Sstevel@tonic-gate 	case USB_CR_OK:
41007c478bd9Sstevel@tonic-gate 		pipe_state = pp->pp_state;
41017c478bd9Sstevel@tonic-gate 		break;
41027c478bd9Sstevel@tonic-gate 	case USB_CR_NO_RESOURCES:
41037c478bd9Sstevel@tonic-gate 	case USB_CR_NOT_SUPPORTED:
41047c478bd9Sstevel@tonic-gate 	case USB_CR_PIPE_RESET:
41057c478bd9Sstevel@tonic-gate 	case USB_CR_STOPPED_POLLING:
41067c478bd9Sstevel@tonic-gate 		pipe_state = EHCI_PIPE_STATE_IDLE;
41077c478bd9Sstevel@tonic-gate 		break;
41087c478bd9Sstevel@tonic-gate 	case USB_CR_PIPE_CLOSING:
41097c478bd9Sstevel@tonic-gate 		break;
41107c478bd9Sstevel@tonic-gate 	default:
41117c478bd9Sstevel@tonic-gate 		/* Set the pipe state to error */
41127c478bd9Sstevel@tonic-gate 		pipe_state = EHCI_PIPE_STATE_ERROR;
41137c478bd9Sstevel@tonic-gate 		pp->pp_error = completion_reason;
41147c478bd9Sstevel@tonic-gate 		break;
41157c478bd9Sstevel@tonic-gate 
41167c478bd9Sstevel@tonic-gate 	}
41177c478bd9Sstevel@tonic-gate 
41187c478bd9Sstevel@tonic-gate 	pp->pp_state = pipe_state;
41197c478bd9Sstevel@tonic-gate 
41207c478bd9Sstevel@tonic-gate 	if (tw && tw->tw_curr_xfer_reqp) {
41217c478bd9Sstevel@tonic-gate 		curr_xfer_reqp = tw->tw_curr_xfer_reqp;
41227c478bd9Sstevel@tonic-gate 		tw->tw_curr_xfer_reqp = NULL;
41237c478bd9Sstevel@tonic-gate 	} else {
41247c478bd9Sstevel@tonic-gate 		ASSERT(pp->pp_client_periodic_in_reqp != NULL);
41257c478bd9Sstevel@tonic-gate 
41267c478bd9Sstevel@tonic-gate 		curr_xfer_reqp = pp->pp_client_periodic_in_reqp;
41277c478bd9Sstevel@tonic-gate 		pp->pp_client_periodic_in_reqp = NULL;
41287c478bd9Sstevel@tonic-gate 	}
41297c478bd9Sstevel@tonic-gate 
41307c478bd9Sstevel@tonic-gate 	ASSERT(curr_xfer_reqp != NULL);
41317c478bd9Sstevel@tonic-gate 
41327c478bd9Sstevel@tonic-gate 	mutex_exit(&ehcip->ehci_int_mutex);
41337c478bd9Sstevel@tonic-gate 
41347c478bd9Sstevel@tonic-gate 	usba_hcdi_cb(ph, curr_xfer_reqp, completion_reason);
41357c478bd9Sstevel@tonic-gate 
41367c478bd9Sstevel@tonic-gate 	mutex_enter(&ehcip->ehci_int_mutex);
41377c478bd9Sstevel@tonic-gate }
4138