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
5b3001defSlg  * Common Development and Distribution License (the "License").
6b3001defSlg  * 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 /*
22112116d8Sfb  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * EHCI Host Controller Driver (EHCI)
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * The EHCI driver is a software driver which interfaces to the Universal
317c478bd9Sstevel@tonic-gate  * Serial Bus layer (USBA) and the Host Controller (HC). The interface to
327c478bd9Sstevel@tonic-gate  * the Host Controller is defined by the EHCI Host Controller Interface.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * This module contains the EHCI driver isochronous code, which handles all
357c478bd9Sstevel@tonic-gate  * Checking of status of USB transfers, error recovery and callbacks.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate #include <sys/usb/hcd/ehci/ehcid.h>
387c478bd9Sstevel@tonic-gate #include <sys/usb/hcd/ehci/ehci_xfer.h>
397c478bd9Sstevel@tonic-gate #include <sys/usb/hcd/ehci/ehci_util.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /* Adjustable variables for the size of isoc pools */
427c478bd9Sstevel@tonic-gate int ehci_itd_pool_size = EHCI_ITD_POOL_SIZE;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * pool functions
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate int ehci_allocate_isoc_pools(
487c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip);
497c478bd9Sstevel@tonic-gate int ehci_get_itd_pool_size();
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Isochronous Transfer Wrapper Functions
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate ehci_isoc_xwrapper_t *ehci_allocate_itw_resources(
550a05e705Slc 	ehci_state_t		*ehcip,
567c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
577c478bd9Sstevel@tonic-gate 	size_t			itw_length,
587c478bd9Sstevel@tonic-gate 	usb_flags_t		usb_flags,
590a05e705Slc 	size_t			pkt_count);
607c478bd9Sstevel@tonic-gate static ehci_isoc_xwrapper_t *ehci_allocate_itw(
617c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
627c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
637c478bd9Sstevel@tonic-gate 	size_t			length,
647c478bd9Sstevel@tonic-gate 	usb_flags_t		usb_flags);
657c478bd9Sstevel@tonic-gate void ehci_deallocate_itw(
667c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
677c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
687c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw);
697c478bd9Sstevel@tonic-gate static void ehci_free_itw_dma(
707c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
717c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
727c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * transfer descriptor functions
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate static ehci_itd_t *ehci_allocate_itd(
787c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip);
797c478bd9Sstevel@tonic-gate void ehci_deallocate_itd(
807c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
817c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
827c478bd9Sstevel@tonic-gate 	ehci_itd_t		*old_itd);
837c478bd9Sstevel@tonic-gate uint_t ehci_calc_num_itds(
847c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
850a05e705Slc 	size_t			pkt_count);
867c478bd9Sstevel@tonic-gate int ehci_allocate_itds_for_itw(
877c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
887c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
897c478bd9Sstevel@tonic-gate 	uint_t			itd_count);
907c478bd9Sstevel@tonic-gate static void ehci_deallocate_itds_for_itw(
917c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
927c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw);
937c478bd9Sstevel@tonic-gate void ehci_insert_itd_on_itw(
947c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
957c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
967c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd);
977c478bd9Sstevel@tonic-gate void ehci_insert_itd_into_active_list(
987c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
997c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd);
1007c478bd9Sstevel@tonic-gate void ehci_remove_itd_from_active_list(
1017c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
1027c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd);
1037c478bd9Sstevel@tonic-gate ehci_itd_t *ehci_create_done_itd_list(
1047c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip);
1057c478bd9Sstevel@tonic-gate int ehci_insert_isoc_to_pfl(
1067c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
1077c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
1087c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw);
1097c478bd9Sstevel@tonic-gate void ehci_remove_isoc_from_pfl(
1107c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
1117c478bd9Sstevel@tonic-gate 	ehci_itd_t		*curr_itd);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * Isochronous in resource functions
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate int ehci_allocate_isoc_in_resource(
1187c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
1197c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
1207c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*tw,
1217c478bd9Sstevel@tonic-gate 	usb_flags_t		flags);
1227c478bd9Sstevel@tonic-gate void ehci_deallocate_isoc_in_resource(
1237c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
1247c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
1257c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * memory addr functions
1297c478bd9Sstevel@tonic-gate  */
1307c478bd9Sstevel@tonic-gate uint32_t ehci_itd_cpu_to_iommu(
1317c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
1327c478bd9Sstevel@tonic-gate 	ehci_itd_t		*addr);
1337c478bd9Sstevel@tonic-gate ehci_itd_t *ehci_itd_iommu_to_cpu(
1347c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
1357c478bd9Sstevel@tonic-gate 	uintptr_t		addr);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * Error parsing functions
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate void ehci_parse_isoc_error(
1417c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
1427c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
1437c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd);
1447c478bd9Sstevel@tonic-gate static usb_cr_t ehci_parse_itd_error(
1457c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
1467c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
1477c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd);
1487c478bd9Sstevel@tonic-gate static usb_cr_t ehci_parse_sitd_error(
1497c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
1507c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
1517c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * print functions
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate void ehci_print_itd(
1577c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
1587c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd);
159b3001defSlg void ehci_print_sitd(
160b3001defSlg 	ehci_state_t		*ehcip,
161b3001defSlg 	ehci_itd_t		*itd);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  * ehci_allocate_isoc_pools:
1667c478bd9Sstevel@tonic-gate  *
1677c478bd9Sstevel@tonic-gate  * Allocate the system memory for itd which are for low/full/high speed
1687c478bd9Sstevel@tonic-gate  * Transfer Descriptors. Must be aligned to a 32 byte boundary.
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate int
1717c478bd9Sstevel@tonic-gate ehci_allocate_isoc_pools(ehci_state_t	*ehcip)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	ddi_device_acc_attr_t		dev_attr;
1747c478bd9Sstevel@tonic-gate 	size_t				real_length;
1757c478bd9Sstevel@tonic-gate 	int				result;
1767c478bd9Sstevel@tonic-gate 	uint_t				ccount;
1777c478bd9Sstevel@tonic-gate 	int				i;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1807c478bd9Sstevel@tonic-gate 	    "ehci_allocate_isoc_pools:");
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/* Byte alignment */
1837c478bd9Sstevel@tonic-gate 	ehcip->ehci_dma_attr.dma_attr_align = EHCI_DMA_ATTR_TD_QH_ALIGNMENT;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	/* Allocate the itd pool DMA handle */
1867c478bd9Sstevel@tonic-gate 	result = ddi_dma_alloc_handle(ehcip->ehci_dip,
1877c478bd9Sstevel@tonic-gate 	    &ehcip->ehci_dma_attr,
1887c478bd9Sstevel@tonic-gate 	    DDI_DMA_SLEEP,
1897c478bd9Sstevel@tonic-gate 	    0,
1907c478bd9Sstevel@tonic-gate 	    &ehcip->ehci_itd_pool_dma_handle);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (result != DDI_SUCCESS) {
1937c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1947c478bd9Sstevel@tonic-gate 		    "ehci_allocate_isoc_pools: Alloc handle failed");
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/* The host controller will be little endian */
2007c478bd9Sstevel@tonic-gate 	dev_attr.devacc_attr_version		= DDI_DEVICE_ATTR_V0;
2017c478bd9Sstevel@tonic-gate 	dev_attr.devacc_attr_endian_flags	= DDI_STRUCTURE_LE_ACC;
2027c478bd9Sstevel@tonic-gate 	dev_attr.devacc_attr_dataorder		= DDI_STRICTORDER_ACC;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/* Allocate the memory */
2057c478bd9Sstevel@tonic-gate 	result = ddi_dma_mem_alloc(ehcip->ehci_itd_pool_dma_handle,
2067c478bd9Sstevel@tonic-gate 	    ehci_itd_pool_size * sizeof (ehci_itd_t),
2077c478bd9Sstevel@tonic-gate 	    &dev_attr,
2087c478bd9Sstevel@tonic-gate 	    DDI_DMA_CONSISTENT,
2097c478bd9Sstevel@tonic-gate 	    DDI_DMA_SLEEP,
2107c478bd9Sstevel@tonic-gate 	    0,
2117c478bd9Sstevel@tonic-gate 	    (caddr_t *)&ehcip->ehci_itd_pool_addr,
2127c478bd9Sstevel@tonic-gate 	    &real_length,
2137c478bd9Sstevel@tonic-gate 	    &ehcip->ehci_itd_pool_mem_handle);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (result != DDI_SUCCESS) {
2167c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
2177c478bd9Sstevel@tonic-gate 		    "ehci_allocate_isoc_pools: Alloc memory failed");
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/* Map the ITD pool into the I/O address space */
2237c478bd9Sstevel@tonic-gate 	result = ddi_dma_addr_bind_handle(
2240a05e705Slc 	    ehcip->ehci_itd_pool_dma_handle,
2250a05e705Slc 	    NULL,
2260a05e705Slc 	    (caddr_t)ehcip->ehci_itd_pool_addr,
2270a05e705Slc 	    real_length,
2280a05e705Slc 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
2290a05e705Slc 	    DDI_DMA_SLEEP,
2300a05e705Slc 	    NULL,
2310a05e705Slc 	    &ehcip->ehci_itd_pool_cookie,
2320a05e705Slc 	    &ccount);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	bzero((void *)ehcip->ehci_itd_pool_addr,
2350a05e705Slc 	    ehci_itd_pool_size * sizeof (ehci_itd_t));
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/* Process the result */
2387c478bd9Sstevel@tonic-gate 	if (result == DDI_DMA_MAPPED) {
2397c478bd9Sstevel@tonic-gate 		/* The cookie count should be 1 */
2407c478bd9Sstevel@tonic-gate 		if (ccount != 1) {
2417c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
2427c478bd9Sstevel@tonic-gate 			    "ehci_allocate_isoc_pools: More than 1 cookie");
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
2457c478bd9Sstevel@tonic-gate 		}
2467c478bd9Sstevel@tonic-gate 	} else {
2477c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
2487c478bd9Sstevel@tonic-gate 		    "ehci_allocate_isoc_pools: Result = %d", result);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 		ehci_decode_ddi_dma_addr_bind_handle_result(ehcip, result);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * DMA addresses for ITD pools are bound
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 	ehcip->ehci_dma_addr_bind_flag |= EHCI_ITD_POOL_BOUND;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	/* Initialize the ITD pool */
2617c478bd9Sstevel@tonic-gate 	for (i = 0; i < ehci_itd_pool_size; i++) {
2627c478bd9Sstevel@tonic-gate 		Set_ITD(ehcip->ehci_itd_pool_addr[i].itd_state,
2637c478bd9Sstevel@tonic-gate 		    EHCI_ITD_FREE);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate int
2717c478bd9Sstevel@tonic-gate ehci_get_itd_pool_size()
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	return (ehci_itd_pool_size);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * Isochronous Transfer Wrapper Functions
2797c478bd9Sstevel@tonic-gate  */
2807c478bd9Sstevel@tonic-gate /*
281b3001defSlg  * ehci_allocate_itw_resources:
2827c478bd9Sstevel@tonic-gate  *
2837c478bd9Sstevel@tonic-gate  * Allocate an iTW and n iTD from the iTD buffer pool and places it into the
2847c478bd9Sstevel@tonic-gate  * ITW.  It does an all or nothing transaction.
2857c478bd9Sstevel@tonic-gate  *
2867c478bd9Sstevel@tonic-gate  * Calculates the number of iTD needed based on pipe speed.
2877c478bd9Sstevel@tonic-gate  * For LOW/FULL speed devices, 1 iTD is needed for each packet.
2887c478bd9Sstevel@tonic-gate  * For HIGH speed device, 1 iTD is needed for 8 to 24 packets, depending on
2897c478bd9Sstevel@tonic-gate  *     the multiplier for "HIGH BANDWIDTH" transfers look at 4.7 in EHCI spec.
2907c478bd9Sstevel@tonic-gate  *
2917c478bd9Sstevel@tonic-gate  * Returns NULL if there is insufficient resources otherwise ITW.
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate ehci_isoc_xwrapper_t *
2947c478bd9Sstevel@tonic-gate ehci_allocate_itw_resources(
2950a05e705Slc 	ehci_state_t		*ehcip,
2967c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
2977c478bd9Sstevel@tonic-gate 	size_t			itw_length,
2987c478bd9Sstevel@tonic-gate 	usb_flags_t		usb_flags,
2990a05e705Slc 	size_t			pkt_count)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	uint_t			itd_count;
3027c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	itw = ehci_allocate_itw(ehcip, pp, itw_length, usb_flags);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (itw == NULL) {
3077c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
3087c478bd9Sstevel@tonic-gate 		    "ehci_allocate_itw_resources: Unable to allocate ITW");
3097c478bd9Sstevel@tonic-gate 	} else {
3107c478bd9Sstevel@tonic-gate 		itd_count = ehci_calc_num_itds(itw, pkt_count);
311b3001defSlg 		USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
312b3001defSlg 		    "ehci_allocate_itw_resources: itd_count = 0x%d", itd_count);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		if (ehci_allocate_itds_for_itw(ehcip, itw, itd_count) ==
3157c478bd9Sstevel@tonic-gate 		    USB_SUCCESS) {
3167c478bd9Sstevel@tonic-gate 			itw->itw_num_itds = itd_count;
3177c478bd9Sstevel@tonic-gate 		} else {
3187c478bd9Sstevel@tonic-gate 			ehci_deallocate_itw(ehcip, pp, itw);
3197c478bd9Sstevel@tonic-gate 			itw = NULL;
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	return (itw);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate  * ehci_allocate_itw:
3297c478bd9Sstevel@tonic-gate  *
3307c478bd9Sstevel@tonic-gate  * Creates a Isochronous Transfer Wrapper (itw) and populate it with this
3317c478bd9Sstevel@tonic-gate  * endpoint's data.  This involves the allocation of DMA resources.
3327c478bd9Sstevel@tonic-gate  *
3337c478bd9Sstevel@tonic-gate  * ITW Fields not set by this function:
3347c478bd9Sstevel@tonic-gate  * - will be populated itds are allocated
3357c478bd9Sstevel@tonic-gate  *   num_ids
3367c478bd9Sstevel@tonic-gate  *   itd_head
3377c478bd9Sstevel@tonic-gate  *   itd_tail
3387c478bd9Sstevel@tonic-gate  *   curr_xfer_reqp
3397c478bd9Sstevel@tonic-gate  *   curr_isoc_pktp
3407c478bd9Sstevel@tonic-gate  *   itw_itd_free_list
3417c478bd9Sstevel@tonic-gate  * - Should be set by the calling function
3427c478bd9Sstevel@tonic-gate  *   itw_handle_callback_value
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate static ehci_isoc_xwrapper_t *
3457c478bd9Sstevel@tonic-gate ehci_allocate_itw(
3467c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
3477c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
3487c478bd9Sstevel@tonic-gate 	size_t			length,
3497c478bd9Sstevel@tonic-gate 	usb_flags_t		usb_flags)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	ddi_device_acc_attr_t	dev_attr;
3527c478bd9Sstevel@tonic-gate 	int			result;
3537c478bd9Sstevel@tonic-gate 	size_t			real_length;
3547c478bd9Sstevel@tonic-gate 	uint_t			ccount;	/* Cookie count */
3557c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph = pp->pp_pipe_handle;
3567c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device = ph->p_usba_device;
3577c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*endpoint = &ph->p_ep;
3587c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
361112116d8Sfb 	    "ehci_allocate_itw: length = 0x%lx flags = 0x%x",
3627c478bd9Sstevel@tonic-gate 	    length, usb_flags);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/* Allocate space for the transfer wrapper */
3677c478bd9Sstevel@tonic-gate 	itw = kmem_zalloc(sizeof (ehci_isoc_xwrapper_t), KM_NOSLEEP);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	if (itw == NULL) {
3707c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
3717c478bd9Sstevel@tonic-gate 		    "ehci_allocate_itw: kmem_zalloc failed");
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 		return (NULL);
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	ehcip->ehci_dma_attr.dma_attr_align = EHCI_DMA_ATTR_ALIGNMENT;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/* Allocate the DMA handle */
3797c478bd9Sstevel@tonic-gate 	result = ddi_dma_alloc_handle(ehcip->ehci_dip,
3807c478bd9Sstevel@tonic-gate 	    &ehcip->ehci_dma_attr,
3817c478bd9Sstevel@tonic-gate 	    DDI_DMA_DONTWAIT,
3827c478bd9Sstevel@tonic-gate 	    0,
3837c478bd9Sstevel@tonic-gate 	    &itw->itw_dmahandle);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (result != DDI_SUCCESS) {
3867c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
3877c478bd9Sstevel@tonic-gate 		    "ehci_create_transfer_wrapper: Alloc handle failed");
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 		kmem_free(itw, sizeof (ehci_isoc_xwrapper_t));
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		return (NULL);
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/* no need for swapping the raw data in the buffers */
3957c478bd9Sstevel@tonic-gate 	dev_attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
3967c478bd9Sstevel@tonic-gate 	dev_attr.devacc_attr_endian_flags  = DDI_NEVERSWAP_ACC;
3977c478bd9Sstevel@tonic-gate 	dev_attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	/* Allocate the memory */
4007c478bd9Sstevel@tonic-gate 	result = ddi_dma_mem_alloc(itw->itw_dmahandle,
4017c478bd9Sstevel@tonic-gate 	    length,
4027c478bd9Sstevel@tonic-gate 	    &dev_attr,
4037c478bd9Sstevel@tonic-gate 	    DDI_DMA_CONSISTENT,
4047c478bd9Sstevel@tonic-gate 	    DDI_DMA_DONTWAIT,
4057c478bd9Sstevel@tonic-gate 	    NULL,
4067c478bd9Sstevel@tonic-gate 	    (caddr_t *)&itw->itw_buf,
4077c478bd9Sstevel@tonic-gate 	    &real_length,
4087c478bd9Sstevel@tonic-gate 	    &itw->itw_accesshandle);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if (result != DDI_SUCCESS) {
4117c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4127c478bd9Sstevel@tonic-gate 		    "ehci_create_transfer_wrapper: dma_mem_alloc fail");
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 		ddi_dma_free_handle(&itw->itw_dmahandle);
4157c478bd9Sstevel@tonic-gate 		kmem_free(itw, sizeof (ehci_isoc_xwrapper_t));
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		return (NULL);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	ASSERT(real_length >= length);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	/* Bind the handle */
4237c478bd9Sstevel@tonic-gate 	result = ddi_dma_addr_bind_handle(itw->itw_dmahandle,
4247c478bd9Sstevel@tonic-gate 	    NULL,
4257c478bd9Sstevel@tonic-gate 	    (caddr_t)itw->itw_buf,
4267c478bd9Sstevel@tonic-gate 	    real_length,
4277c478bd9Sstevel@tonic-gate 	    DDI_DMA_RDWR|DDI_DMA_CONSISTENT,
4287c478bd9Sstevel@tonic-gate 	    DDI_DMA_DONTWAIT,
4297c478bd9Sstevel@tonic-gate 	    NULL,
4307c478bd9Sstevel@tonic-gate 	    &itw->itw_cookie,
4317c478bd9Sstevel@tonic-gate 	    &ccount);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	if (result == DDI_DMA_MAPPED) {
4347c478bd9Sstevel@tonic-gate 		/* The cookie count should be 1 */
4357c478bd9Sstevel@tonic-gate 		if (ccount != 1) {
4367c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4377c478bd9Sstevel@tonic-gate 			    "ehci_create_transfer_wrapper: More than 1 cookie");
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 			result = ddi_dma_unbind_handle(itw->itw_dmahandle);
4407c478bd9Sstevel@tonic-gate 			ASSERT(result == DDI_SUCCESS);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 			ddi_dma_mem_free(&itw->itw_accesshandle);
4437c478bd9Sstevel@tonic-gate 			ddi_dma_free_handle(&itw->itw_dmahandle);
4447c478bd9Sstevel@tonic-gate 			kmem_free(itw, sizeof (ehci_isoc_xwrapper_t));
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 			return (NULL);
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 	} else {
4497c478bd9Sstevel@tonic-gate 		ehci_decode_ddi_dma_addr_bind_handle_result(ehcip, result);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 		ddi_dma_mem_free(&itw->itw_accesshandle);
4527c478bd9Sstevel@tonic-gate 		ddi_dma_free_handle(&itw->itw_dmahandle);
4537c478bd9Sstevel@tonic-gate 		kmem_free(itw, sizeof (ehci_isoc_xwrapper_t));
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		return (NULL);
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	/* Store a back pointer to the pipe private structure */
4597c478bd9Sstevel@tonic-gate 	itw->itw_pipe_private = pp;
4607c478bd9Sstevel@tonic-gate 	if (pp->pp_itw_head == NULL) {
4617c478bd9Sstevel@tonic-gate 		pp->pp_itw_head = itw;
4627c478bd9Sstevel@tonic-gate 		pp->pp_itw_tail = itw;
4637c478bd9Sstevel@tonic-gate 	} else {
4647c478bd9Sstevel@tonic-gate 		pp->pp_itw_tail->itw_next = itw;
4657c478bd9Sstevel@tonic-gate 		pp->pp_itw_tail = itw;
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	/*
4697c478bd9Sstevel@tonic-gate 	 * Store transfer information
4707c478bd9Sstevel@tonic-gate 	 * itw_buf has been allocated and will be set later
4717c478bd9Sstevel@tonic-gate 	 */
4727c478bd9Sstevel@tonic-gate 	itw->itw_length = length;
4737c478bd9Sstevel@tonic-gate 	itw->itw_flags = usb_flags;
4747c478bd9Sstevel@tonic-gate 	itw->itw_port_status = usba_device->usb_port_status;
4757c478bd9Sstevel@tonic-gate 	itw->itw_direction = endpoint->bEndpointAddress & USB_EP_DIR_MASK;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 * Store the endpoint information that will be used by the
4797c478bd9Sstevel@tonic-gate 	 * transfer descriptors later.
4807c478bd9Sstevel@tonic-gate 	 */
4817c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
4827c478bd9Sstevel@tonic-gate 	itw->itw_hub_addr = usba_device->usb_hs_hub_addr;
4837c478bd9Sstevel@tonic-gate 	itw->itw_hub_port = usba_device->usb_hs_hub_port;
4847c478bd9Sstevel@tonic-gate 	itw->itw_endpoint_num = endpoint->bEndpointAddress & USB_EP_NUM_MASK;
4857c478bd9Sstevel@tonic-gate 	itw->itw_device_addr = usba_device->usb_addr;
4867c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	/* Get and Store 32bit ID */
4897c478bd9Sstevel@tonic-gate 	itw->itw_id = EHCI_GET_ID((void *)itw);
4907c478bd9Sstevel@tonic-gate 	ASSERT(itw->itw_id != NULL);
4917c478bd9Sstevel@tonic-gate 
492b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
493112116d8Sfb 	    "ehci_create_itw: itw = 0x%p real_length = 0x%lx",
494112116d8Sfb 	    (void *)itw, real_length);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	return (itw);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate  * ehci_deallocate_itw:
5027c478bd9Sstevel@tonic-gate  *
5037c478bd9Sstevel@tonic-gate  * Deallocate of a Isochronous Transaction Wrapper (TW) and this involves the
5047c478bd9Sstevel@tonic-gate  * freeing of DMA resources.
5057c478bd9Sstevel@tonic-gate  */
5067c478bd9Sstevel@tonic-gate void
5077c478bd9Sstevel@tonic-gate ehci_deallocate_itw(
5087c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
5097c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
5107c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw)
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*prev, *next;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
515112116d8Sfb 	    "ehci_deallocate_itw: itw = 0x%p", (void *)itw);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	/*
5187c478bd9Sstevel@tonic-gate 	 * If the transfer wrapper has no Host Controller (HC)
5197c478bd9Sstevel@tonic-gate 	 * Transfer Descriptors (ITD) associated with it,  then
5207c478bd9Sstevel@tonic-gate 	 * remove the transfer wrapper.
5217c478bd9Sstevel@tonic-gate 	 */
5227c478bd9Sstevel@tonic-gate 	if (itw->itw_itd_head) {
5237c478bd9Sstevel@tonic-gate 		ASSERT(itw->itw_itd_tail != NULL);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		return;
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	ASSERT(itw->itw_itd_tail == NULL);
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	/* Make sure we return all the unused itd's to the pool as well */
5317c478bd9Sstevel@tonic-gate 	ehci_deallocate_itds_for_itw(ehcip, itw);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	/*
5347c478bd9Sstevel@tonic-gate 	 * If pp->pp_tw_head and pp->pp_tw_tail are pointing to
5357c478bd9Sstevel@tonic-gate 	 * given TW then set the head and  tail  equal to NULL.
5367c478bd9Sstevel@tonic-gate 	 * Otherwise search for this TW in the linked TW's list
5377c478bd9Sstevel@tonic-gate 	 * and then remove this TW from the list.
5387c478bd9Sstevel@tonic-gate 	 */
5397c478bd9Sstevel@tonic-gate 	if (pp->pp_itw_head == itw) {
5407c478bd9Sstevel@tonic-gate 		if (pp->pp_itw_tail == itw) {
5417c478bd9Sstevel@tonic-gate 			pp->pp_itw_head = NULL;
5427c478bd9Sstevel@tonic-gate 			pp->pp_itw_tail = NULL;
5437c478bd9Sstevel@tonic-gate 		} else {
5447c478bd9Sstevel@tonic-gate 			pp->pp_itw_head = itw->itw_next;
5457c478bd9Sstevel@tonic-gate 		}
5467c478bd9Sstevel@tonic-gate 	} else {
5477c478bd9Sstevel@tonic-gate 		prev = pp->pp_itw_head;
5487c478bd9Sstevel@tonic-gate 		next = prev->itw_next;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 		while (next && (next != itw)) {
5517c478bd9Sstevel@tonic-gate 			prev = next;
5527c478bd9Sstevel@tonic-gate 			next = next->itw_next;
5537c478bd9Sstevel@tonic-gate 		}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 		if (next == itw) {
5567c478bd9Sstevel@tonic-gate 			prev->itw_next = next->itw_next;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 			if (pp->pp_itw_tail == itw) {
5597c478bd9Sstevel@tonic-gate 				pp->pp_itw_tail = prev;
5607c478bd9Sstevel@tonic-gate 			}
5617c478bd9Sstevel@tonic-gate 		}
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/* Free this iTWs dma resources */
5657c478bd9Sstevel@tonic-gate 	ehci_free_itw_dma(ehcip, pp, itw);
5667c478bd9Sstevel@tonic-gate }
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate /*
5707c478bd9Sstevel@tonic-gate  * ehci_free_itw_dma:
5717c478bd9Sstevel@tonic-gate  *
5727c478bd9Sstevel@tonic-gate  * Free the Isochronous Transfer Wrapper dma resources.
5737c478bd9Sstevel@tonic-gate  */
5747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5757c478bd9Sstevel@tonic-gate static void
5767c478bd9Sstevel@tonic-gate ehci_free_itw_dma(
5777c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
5787c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
5797c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate 	int	rval;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
584112116d8Sfb 	    "ehci_free_itw_dma: itw = 0x%p", (void *)itw);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	ASSERT(itw != NULL);
5877c478bd9Sstevel@tonic-gate 	ASSERT(itw->itw_id != NULL);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	/* Free 32bit ID */
5907c478bd9Sstevel@tonic-gate 	EHCI_FREE_ID((uint32_t)itw->itw_id);
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	rval = ddi_dma_unbind_handle(itw->itw_dmahandle);
5937c478bd9Sstevel@tonic-gate 	ASSERT(rval == DDI_SUCCESS);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	ddi_dma_mem_free(&itw->itw_accesshandle);
5967c478bd9Sstevel@tonic-gate 	ddi_dma_free_handle(&itw->itw_dmahandle);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	/* Free transfer wrapper */
5997c478bd9Sstevel@tonic-gate 	kmem_free(itw, sizeof (ehci_isoc_xwrapper_t));
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate /*
6047c478bd9Sstevel@tonic-gate  * transfer descriptor functions
6057c478bd9Sstevel@tonic-gate  */
6067c478bd9Sstevel@tonic-gate /*
6077c478bd9Sstevel@tonic-gate  * ehci_allocate_itd:
6087c478bd9Sstevel@tonic-gate  *
6097c478bd9Sstevel@tonic-gate  * Allocate a Transfer Descriptor (iTD) from the iTD buffer pool.
6107c478bd9Sstevel@tonic-gate  */
6117c478bd9Sstevel@tonic-gate static ehci_itd_t *
6127c478bd9Sstevel@tonic-gate ehci_allocate_itd(ehci_state_t	*ehcip)
6137c478bd9Sstevel@tonic-gate {
6147c478bd9Sstevel@tonic-gate 	int		i, state;
6157c478bd9Sstevel@tonic-gate 	ehci_itd_t	*itd;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	/*
6207c478bd9Sstevel@tonic-gate 	 * Search for a blank Transfer Descriptor (iTD)
6217c478bd9Sstevel@tonic-gate 	 * in the iTD buffer pool.
6227c478bd9Sstevel@tonic-gate 	 */
6237c478bd9Sstevel@tonic-gate 	for (i = 0; i < ehci_itd_pool_size; i ++) {
6247c478bd9Sstevel@tonic-gate 		state = Get_ITD(ehcip->ehci_itd_pool_addr[i].itd_state);
6257c478bd9Sstevel@tonic-gate 		if (state == EHCI_ITD_FREE) {
6267c478bd9Sstevel@tonic-gate 			break;
6277c478bd9Sstevel@tonic-gate 		}
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	if (i >= ehci_itd_pool_size) {
6317c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
6327c478bd9Sstevel@tonic-gate 		    "ehci_allocate_itd: ITD exhausted");
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 		return (NULL);
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
6387c478bd9Sstevel@tonic-gate 	    "ehci_allocate_itd: Allocated %d", i);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	/* Create a new dummy for the end of the ITD list */
6417c478bd9Sstevel@tonic-gate 	itd = &ehcip->ehci_itd_pool_addr[i];
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
6447c478bd9Sstevel@tonic-gate 	    "ehci_allocate_itd: itd 0x%p", (void *)itd);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	/* Mark the newly allocated ITD as a empty */
6477c478bd9Sstevel@tonic-gate 	Set_ITD(itd->itd_state, EHCI_ITD_DUMMY);
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	return (itd);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate /*
6547c478bd9Sstevel@tonic-gate  * ehci_deallocate_itd:
6557c478bd9Sstevel@tonic-gate  *
6567c478bd9Sstevel@tonic-gate  * Deallocate a Host Controller's (HC) Transfer Descriptor (ITD).
6577c478bd9Sstevel@tonic-gate  *
6587c478bd9Sstevel@tonic-gate  */
6597c478bd9Sstevel@tonic-gate void
6607c478bd9Sstevel@tonic-gate ehci_deallocate_itd(
6617c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
6627c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
6637c478bd9Sstevel@tonic-gate 	ehci_itd_t		*old_itd)
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate 	ehci_itd_t	*itd, *next_itd;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
6687c478bd9Sstevel@tonic-gate 	    "ehci_deallocate_itd: old_itd = 0x%p", (void *)old_itd);
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
6717c478bd9Sstevel@tonic-gate 	ASSERT(Get_ITD(old_itd->itd_trans_wrapper) == itw->itw_id);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	/* If it has been marked RECLAIM it has already been removed */
6747c478bd9Sstevel@tonic-gate 	if (Get_ITD(old_itd->itd_state) != EHCI_ITD_RECLAIM) {
6757c478bd9Sstevel@tonic-gate 		ehci_remove_isoc_from_pfl(ehcip, old_itd);
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	/* Make sure the ITD is not in the PFL */
6797c478bd9Sstevel@tonic-gate 	ASSERT(Get_ITD_FRAME(old_itd->itd_frame_number) == 0);
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	/* Remove the itd from the itw */
6827c478bd9Sstevel@tonic-gate 	itd = itw->itw_itd_head;
6837c478bd9Sstevel@tonic-gate 	if (old_itd != itd) {
6847c478bd9Sstevel@tonic-gate 		next_itd = ehci_itd_iommu_to_cpu(ehcip,
6857c478bd9Sstevel@tonic-gate 		    Get_ITD(itd->itd_itw_next_itd));
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 		while (next_itd != old_itd) {
6887c478bd9Sstevel@tonic-gate 			itd = next_itd;
6897c478bd9Sstevel@tonic-gate 			next_itd = ehci_itd_iommu_to_cpu(ehcip,
6907c478bd9Sstevel@tonic-gate 			    Get_ITD(itd->itd_itw_next_itd));
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		Set_ITD(itd->itd_itw_next_itd, old_itd->itd_itw_next_itd);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 		if (itd->itd_itw_next_itd == NULL) {
6967c478bd9Sstevel@tonic-gate 			itw->itw_itd_tail = itd;
6977c478bd9Sstevel@tonic-gate 		}
6987c478bd9Sstevel@tonic-gate 	} else {
6997c478bd9Sstevel@tonic-gate 		itw->itw_itd_head = ehci_itd_iommu_to_cpu(
7000a05e705Slc 		    ehcip, Get_ITD(old_itd->itd_itw_next_itd));
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 		if (itw->itw_itd_head == NULL) {
7037c478bd9Sstevel@tonic-gate 			itw->itw_itd_tail = NULL;
7047c478bd9Sstevel@tonic-gate 		}
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	bzero((char *)old_itd, sizeof (ehci_itd_t));
7087c478bd9Sstevel@tonic-gate 	Set_ITD(old_itd->itd_state, EHCI_ITD_FREE);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
7117c478bd9Sstevel@tonic-gate 	    "Dealloc_itd: itd 0x%p", (void *)old_itd);
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate /*
7167c478bd9Sstevel@tonic-gate  * ehci_calc_num_itds:
7177c478bd9Sstevel@tonic-gate  *
7187c478bd9Sstevel@tonic-gate  * Calculates how many ITDs are needed for this request.
7197c478bd9Sstevel@tonic-gate  * The calculation is based on weather it is an HIGH speed
7207c478bd9Sstevel@tonic-gate  * transaction of a FULL/LOW transaction.
7217c478bd9Sstevel@tonic-gate  *
7227c478bd9Sstevel@tonic-gate  * For FULL/LOW transaction more itds are necessary if it
7237c478bd9Sstevel@tonic-gate  * spans frames.
7247c478bd9Sstevel@tonic-gate  */
7257c478bd9Sstevel@tonic-gate uint_t
7267c478bd9Sstevel@tonic-gate ehci_calc_num_itds(
7277c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
7280a05e705Slc 	size_t			pkt_count)
7297c478bd9Sstevel@tonic-gate {
7307c478bd9Sstevel@tonic-gate 	uint_t			multiplier, itd_count;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/* Allocate the appropriate isoc resources */
7337c478bd9Sstevel@tonic-gate 	if (itw->itw_port_status == USBA_HIGH_SPEED_DEV) {
7347c478bd9Sstevel@tonic-gate 		/* Multiplier needs to be passed in somehow */
7357c478bd9Sstevel@tonic-gate 		multiplier = 1 * 8;
7367c478bd9Sstevel@tonic-gate 		itd_count = pkt_count / multiplier;
7377c478bd9Sstevel@tonic-gate 		if (pkt_count % multiplier) {
7387c478bd9Sstevel@tonic-gate 			itd_count++;
7397c478bd9Sstevel@tonic-gate 		}
7407c478bd9Sstevel@tonic-gate 	} else {
741*d29f5a71Szhigang lu - Sun Microsystems - Beijing China 		itd_count = (uint_t)pkt_count;
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
744b3001defSlg 	return (itd_count);
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate /*
7487c478bd9Sstevel@tonic-gate  * ehci_allocate_itds_for_itw:
7497c478bd9Sstevel@tonic-gate  *
7507c478bd9Sstevel@tonic-gate  * Allocate n Transfer Descriptors (TD) from the TD buffer pool and places it
7517c478bd9Sstevel@tonic-gate  * into the TW.
7527c478bd9Sstevel@tonic-gate  *
7537c478bd9Sstevel@tonic-gate  * Returns USB_NO_RESOURCES if it was not able to allocate all the requested TD
7547c478bd9Sstevel@tonic-gate  * otherwise USB_SUCCESS.
7557c478bd9Sstevel@tonic-gate  */
7567c478bd9Sstevel@tonic-gate int
7577c478bd9Sstevel@tonic-gate ehci_allocate_itds_for_itw(
7587c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
7597c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
7607c478bd9Sstevel@tonic-gate 	uint_t			itd_count)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd;
7637c478bd9Sstevel@tonic-gate 	uint32_t		itd_addr;
7647c478bd9Sstevel@tonic-gate 	int			i;
7657c478bd9Sstevel@tonic-gate 	int			error = USB_SUCCESS;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	for (i = 0; i < itd_count; i += 1) {
7687c478bd9Sstevel@tonic-gate 		itd = ehci_allocate_itd(ehcip);
7697c478bd9Sstevel@tonic-gate 		if (itd == NULL) {
7707c478bd9Sstevel@tonic-gate 			error = USB_NO_RESOURCES;
7717c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
7727c478bd9Sstevel@tonic-gate 			    "ehci_allocate_itds_for_itw: "
7737c478bd9Sstevel@tonic-gate 			    "Unable to allocate %d ITDs",
7747c478bd9Sstevel@tonic-gate 			    itd_count);
7757c478bd9Sstevel@tonic-gate 			break;
7767c478bd9Sstevel@tonic-gate 		}
7777c478bd9Sstevel@tonic-gate 		if (i > 0) {
7787c478bd9Sstevel@tonic-gate 			itd_addr = ehci_itd_cpu_to_iommu(ehcip,
7797c478bd9Sstevel@tonic-gate 			    itw->itw_itd_free_list);
7807c478bd9Sstevel@tonic-gate 			Set_ITD(itd->itd_link_ptr, itd_addr);
7817c478bd9Sstevel@tonic-gate 		}
782b3001defSlg 		Set_ITD_INDEX(itd, 0, EHCI_ITD_UNUSED_INDEX);
783b3001defSlg 		Set_ITD_INDEX(itd, 1, EHCI_ITD_UNUSED_INDEX);
784b3001defSlg 		Set_ITD_INDEX(itd, 2, EHCI_ITD_UNUSED_INDEX);
785b3001defSlg 		Set_ITD_INDEX(itd, 3, EHCI_ITD_UNUSED_INDEX);
786b3001defSlg 		Set_ITD_INDEX(itd, 4, EHCI_ITD_UNUSED_INDEX);
787b3001defSlg 		Set_ITD_INDEX(itd, 5, EHCI_ITD_UNUSED_INDEX);
788b3001defSlg 		Set_ITD_INDEX(itd, 6, EHCI_ITD_UNUSED_INDEX);
789b3001defSlg 		Set_ITD_INDEX(itd, 7, EHCI_ITD_UNUSED_INDEX);
7907c478bd9Sstevel@tonic-gate 		itw->itw_itd_free_list = itd;
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	return (error);
7947c478bd9Sstevel@tonic-gate }
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate /*
7987c478bd9Sstevel@tonic-gate  * ehci_deallocate_itds_for_itw:
7997c478bd9Sstevel@tonic-gate  *
8007c478bd9Sstevel@tonic-gate  * Free all allocated resources for Transaction Wrapper (TW).
8017c478bd9Sstevel@tonic-gate  * Does not free the iTW itself.
8027c478bd9Sstevel@tonic-gate  */
8037c478bd9Sstevel@tonic-gate static void
8047c478bd9Sstevel@tonic-gate ehci_deallocate_itds_for_itw(
8057c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
8067c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw)
8077c478bd9Sstevel@tonic-gate {
8087c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd = NULL;
8097c478bd9Sstevel@tonic-gate 	ehci_itd_t		*temp_itd = NULL;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
812112116d8Sfb 	    "ehci_free_itw_itd_resources: itw = 0x%p", (void *)itw);
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	itd = itw->itw_itd_free_list;
8157c478bd9Sstevel@tonic-gate 	while (itd != NULL) {
8167c478bd9Sstevel@tonic-gate 		/* Save the pointer to the next itd before destroying it */
8177c478bd9Sstevel@tonic-gate 		temp_itd = ehci_itd_iommu_to_cpu(ehcip,
8187c478bd9Sstevel@tonic-gate 		    Get_ITD(itd->itd_link_ptr));
8197c478bd9Sstevel@tonic-gate 		ehci_deallocate_itd(ehcip, itw, itd);
8207c478bd9Sstevel@tonic-gate 		itd = temp_itd;
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 	itw->itw_itd_free_list = NULL;
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate /*
8277c478bd9Sstevel@tonic-gate  * ehci_insert_itd_on_itw:
8287c478bd9Sstevel@tonic-gate  *
8297c478bd9Sstevel@tonic-gate  * The transfer wrapper keeps a list of all Transfer Descriptors (iTD) that
8307c478bd9Sstevel@tonic-gate  * are allocated for this transfer. Insert a iTD onto this list.
8317c478bd9Sstevel@tonic-gate  */
8327c478bd9Sstevel@tonic-gate void ehci_insert_itd_on_itw(
8337c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
8347c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
8357c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd)
8367c478bd9Sstevel@tonic-gate {
8377c478bd9Sstevel@tonic-gate 	/*
8387c478bd9Sstevel@tonic-gate 	 * Set the next pointer to NULL because
8397c478bd9Sstevel@tonic-gate 	 * this is the last ITD on list.
8407c478bd9Sstevel@tonic-gate 	 */
8417c478bd9Sstevel@tonic-gate 	Set_ITD(itd->itd_itw_next_itd, NULL);
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	if (itw->itw_itd_head == NULL) {
8447c478bd9Sstevel@tonic-gate 		ASSERT(itw->itw_itd_tail == NULL);
8457c478bd9Sstevel@tonic-gate 		itw->itw_itd_head = itd;
8467c478bd9Sstevel@tonic-gate 		itw->itw_itd_tail = itd;
8477c478bd9Sstevel@tonic-gate 	} else {
8487c478bd9Sstevel@tonic-gate 		ehci_itd_t *dummy = (ehci_itd_t *)itw->itw_itd_tail;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 		ASSERT(dummy != NULL);
8517c478bd9Sstevel@tonic-gate 		ASSERT(Get_ITD(itd->itd_state) == EHCI_ITD_ACTIVE);
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 		/* Add the itd to the end of the list */
8547c478bd9Sstevel@tonic-gate 		Set_ITD(dummy->itd_itw_next_itd,
8557c478bd9Sstevel@tonic-gate 		    ehci_itd_cpu_to_iommu(ehcip, itd));
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 		itw->itw_itd_tail = itd;
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	Set_ITD(itd->itd_trans_wrapper, (uint32_t)itw->itw_id);
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate /*
8657c478bd9Sstevel@tonic-gate  * ehci_insert_itd_into_active_list:
8667c478bd9Sstevel@tonic-gate  *
8677c478bd9Sstevel@tonic-gate  * Add current ITD into the active ITD list in reverse order.
8687c478bd9Sstevel@tonic-gate  * When he done list is created, remove it in the reverse order.
8697c478bd9Sstevel@tonic-gate  */
8707c478bd9Sstevel@tonic-gate void
8717c478bd9Sstevel@tonic-gate ehci_insert_itd_into_active_list(
8727c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
8737c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd)
8747c478bd9Sstevel@tonic-gate {
8757c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
8767c478bd9Sstevel@tonic-gate 	ASSERT(itd != NULL);
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	Set_ITD(itd->itd_next_active_itd,
8797c478bd9Sstevel@tonic-gate 	    ehci_itd_cpu_to_iommu(ehcip, ehcip->ehci_active_itd_list));
8807c478bd9Sstevel@tonic-gate 	ehcip->ehci_active_itd_list = itd;
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate /*
8857c478bd9Sstevel@tonic-gate  * ehci_remove_itd_from_active_list:
8867c478bd9Sstevel@tonic-gate  *
8877c478bd9Sstevel@tonic-gate  * Remove current ITD from the active ITD list.
8887c478bd9Sstevel@tonic-gate  */
8897c478bd9Sstevel@tonic-gate void
8907c478bd9Sstevel@tonic-gate ehci_remove_itd_from_active_list(
8917c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
8927c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd)
8937c478bd9Sstevel@tonic-gate {
8947c478bd9Sstevel@tonic-gate 	ehci_itd_t		*curr_itd, *next_itd;
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
8977c478bd9Sstevel@tonic-gate 	ASSERT(itd != NULL);
8987c478bd9Sstevel@tonic-gate 
899b3001defSlg 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
900b3001defSlg 	    "ehci_remove_itd_from_active_list: "
901b3001defSlg 	    "ehci_active_itd_list = 0x%p itd = 0x%p",
902112116d8Sfb 	    (void *)ehcip->ehci_active_itd_list, (void *)itd);
903b3001defSlg 
9047c478bd9Sstevel@tonic-gate 	curr_itd = ehcip->ehci_active_itd_list;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	if (curr_itd == itd) {
9077c478bd9Sstevel@tonic-gate 		ehcip->ehci_active_itd_list =
9087c478bd9Sstevel@tonic-gate 		    ehci_itd_iommu_to_cpu(ehcip, itd->itd_next_active_itd);
9097c478bd9Sstevel@tonic-gate 		itd->itd_next_active_itd = NULL;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 		return;
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	next_itd = ehci_itd_iommu_to_cpu(ehcip, curr_itd->itd_next_active_itd);
9157c478bd9Sstevel@tonic-gate 	while (next_itd != itd) {
9167c478bd9Sstevel@tonic-gate 		curr_itd = next_itd;
9177c478bd9Sstevel@tonic-gate 		if (curr_itd) {
9187c478bd9Sstevel@tonic-gate 			next_itd = ehci_itd_iommu_to_cpu(ehcip,
9197c478bd9Sstevel@tonic-gate 			    curr_itd->itd_next_active_itd);
9207c478bd9Sstevel@tonic-gate 		} else {
9217c478bd9Sstevel@tonic-gate 			break;
9227c478bd9Sstevel@tonic-gate 		}
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	if ((curr_itd) && (next_itd == itd)) {
9267c478bd9Sstevel@tonic-gate 		Set_ITD(curr_itd->itd_next_active_itd,
9277c478bd9Sstevel@tonic-gate 		    Get_ITD(itd->itd_next_active_itd));
9287c478bd9Sstevel@tonic-gate 		Set_ITD(itd->itd_next_active_itd, NULL);
9297c478bd9Sstevel@tonic-gate 	} else {
9307c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
9317c478bd9Sstevel@tonic-gate 		    "ehci_remove_itd_from_active_list: "
9320a05e705Slc 		    "Unable to find ITD in active_itd_list");
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate }
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate /*
9387c478bd9Sstevel@tonic-gate  * ehci_create_done_itd_list:
9397c478bd9Sstevel@tonic-gate  *
9407c478bd9Sstevel@tonic-gate  * Traverse the active list and create a done list and remove them
9417c478bd9Sstevel@tonic-gate  * from the active list.
9427c478bd9Sstevel@tonic-gate  */
9437c478bd9Sstevel@tonic-gate ehci_itd_t *
9447c478bd9Sstevel@tonic-gate ehci_create_done_itd_list(
9457c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip)
9467c478bd9Sstevel@tonic-gate {
9477c478bd9Sstevel@tonic-gate 	usb_frame_number_t	current_frame_number;
9487c478bd9Sstevel@tonic-gate 	usb_frame_number_t	itd_frame_number, itd_reclaim_number;
9497c478bd9Sstevel@tonic-gate 	ehci_itd_t		*curr_itd = NULL, *next_itd = NULL;
9507c478bd9Sstevel@tonic-gate 	ehci_itd_t		*done_itd_list = NULL;
9517c478bd9Sstevel@tonic-gate 	uint_t			state;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
9547c478bd9Sstevel@tonic-gate 	    "ehci_create_done_itd_list:");
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	/*
9597c478bd9Sstevel@tonic-gate 	 * Get the current frame number.
9607c478bd9Sstevel@tonic-gate 	 * Only process itd that were inserted before the current
9617c478bd9Sstevel@tonic-gate 	 * frame number.
9627c478bd9Sstevel@tonic-gate 	 */
9637c478bd9Sstevel@tonic-gate 	current_frame_number = ehci_get_current_frame_number(ehcip);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	curr_itd = ehcip->ehci_active_itd_list;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	while (curr_itd) {
9687c478bd9Sstevel@tonic-gate 		/* Get next itd from the active itd list */
9697c478bd9Sstevel@tonic-gate 		next_itd = ehci_itd_iommu_to_cpu(ehcip,
9707c478bd9Sstevel@tonic-gate 		    Get_ITD(curr_itd->itd_next_active_itd));
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 		/*
9737c478bd9Sstevel@tonic-gate 		 * If haven't past the frame number that the ITD was
9747c478bd9Sstevel@tonic-gate 		 * suppose to be executed, don't touch it.  Just in
9757c478bd9Sstevel@tonic-gate 		 * case it is being processed by the HCD and cause
9767c478bd9Sstevel@tonic-gate 		 * a race condition.
9777c478bd9Sstevel@tonic-gate 		 */
9787c478bd9Sstevel@tonic-gate 		itd_frame_number = Get_ITD_FRAME(curr_itd->itd_frame_number);
9797c478bd9Sstevel@tonic-gate 		itd_reclaim_number =
9807c478bd9Sstevel@tonic-gate 		    Get_ITD_FRAME(curr_itd->itd_reclaim_number);
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 		/* Get the ITD state */
9837c478bd9Sstevel@tonic-gate 		state = Get_ITD(curr_itd->itd_state);
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 		if (((state == EHCI_ITD_ACTIVE) &&
9860a05e705Slc 		    (itd_frame_number < current_frame_number)) ||
9877c478bd9Sstevel@tonic-gate 		    ((state == EHCI_ITD_RECLAIM) &&
9880a05e705Slc 		    (itd_reclaim_number < current_frame_number))) {
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 			/* Remove this ITD from active ITD list */
9917c478bd9Sstevel@tonic-gate 			ehci_remove_itd_from_active_list(ehcip, curr_itd);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 			/*
9947c478bd9Sstevel@tonic-gate 			 * Create the done list in reverse order, since the
9957c478bd9Sstevel@tonic-gate 			 * active list was also created in reverse order.
9967c478bd9Sstevel@tonic-gate 			 */
9977c478bd9Sstevel@tonic-gate 			Set_ITD(curr_itd->itd_next_active_itd,
9987c478bd9Sstevel@tonic-gate 			    ehci_itd_cpu_to_iommu(ehcip, done_itd_list));
9997c478bd9Sstevel@tonic-gate 			done_itd_list = curr_itd;
10007c478bd9Sstevel@tonic-gate 		}
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 		curr_itd = next_itd;
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	return (done_itd_list);
10067c478bd9Sstevel@tonic-gate }
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate /*
10107c478bd9Sstevel@tonic-gate  * ehci_insert_isoc_to_pfl:
10117c478bd9Sstevel@tonic-gate  *
10127c478bd9Sstevel@tonic-gate  * Insert a ITD request into the Host Controller's isochronous list.
10137c478bd9Sstevel@tonic-gate  * All the ITDs in the ITW will be added the PFL at once.  Either all
10147c478bd9Sstevel@tonic-gate  * of them will make it or none of them will.
10157c478bd9Sstevel@tonic-gate  */
10167c478bd9Sstevel@tonic-gate int
10177c478bd9Sstevel@tonic-gate ehci_insert_isoc_to_pfl(
10187c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
10197c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
10207c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw)
10217c478bd9Sstevel@tonic-gate {
10227c478bd9Sstevel@tonic-gate 	usb_isoc_req_t		*isoc_reqp = itw->itw_curr_xfer_reqp;
10237c478bd9Sstevel@tonic-gate 	usb_frame_number_t	current_frame_number, start_frame_number;
10247c478bd9Sstevel@tonic-gate 	uint_t			ddic, pfl_number;
10257c478bd9Sstevel@tonic-gate 	ehci_periodic_frame_list_t *periodic_frame_list =
10267c478bd9Sstevel@tonic-gate 	    ehcip->ehci_periodic_frame_list_tablep;
10277c478bd9Sstevel@tonic-gate 	uint32_t		addr, port_status;
10287c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
10317c478bd9Sstevel@tonic-gate 	    "ehci_insert_isoc_to_pfl: "
1032b3001defSlg 	    "isoc flags 0x%x itw = 0x%p",
1033112116d8Sfb 	    isoc_reqp->isoc_attributes, (void *)itw);
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	/*
10367c478bd9Sstevel@tonic-gate 	 * Enter critical, while programming the usb frame number
10377c478bd9Sstevel@tonic-gate 	 * and inserting current isochronous TD into the ED's list.
10387c478bd9Sstevel@tonic-gate 	 */
10397c478bd9Sstevel@tonic-gate 	ddic = ddi_enter_critical();
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	/* Get the current frame number */
10427c478bd9Sstevel@tonic-gate 	current_frame_number = ehci_get_current_frame_number(ehcip);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	/*
10457c478bd9Sstevel@tonic-gate 	 * Check the given isochronous flags and get the frame number
10467c478bd9Sstevel@tonic-gate 	 * to insert the itd into.
10477c478bd9Sstevel@tonic-gate 	 */
10487c478bd9Sstevel@tonic-gate 	switch (isoc_reqp->isoc_attributes &
10497c478bd9Sstevel@tonic-gate 	    (USB_ATTRS_ISOC_START_FRAME | USB_ATTRS_ISOC_XFER_ASAP)) {
10507c478bd9Sstevel@tonic-gate 	case USB_ATTRS_ISOC_START_FRAME:
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 		/* Starting frame number is specified */
10537c478bd9Sstevel@tonic-gate 		if (pp->pp_flag & EHCI_ISOC_XFER_CONTINUE) {
10547c478bd9Sstevel@tonic-gate 			/* Get the starting usb frame number */
10557c478bd9Sstevel@tonic-gate 			start_frame_number = pp->pp_next_frame_number;
10567c478bd9Sstevel@tonic-gate 		} else {
10577c478bd9Sstevel@tonic-gate 			/* Check for the Starting usb frame number */
10587c478bd9Sstevel@tonic-gate 			if ((isoc_reqp->isoc_frame_no == 0) ||
10597c478bd9Sstevel@tonic-gate 			    ((isoc_reqp->isoc_frame_no +
10600a05e705Slc 			    isoc_reqp->isoc_pkts_count) <
10610a05e705Slc 			    current_frame_number)) {
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 				/* Exit the critical */
10647c478bd9Sstevel@tonic-gate 				ddi_exit_critical(ddic);
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(PRINT_MASK_LISTS,
10677c478bd9Sstevel@tonic-gate 				    ehcip->ehci_log_hdl,
10687c478bd9Sstevel@tonic-gate 				    "ehci_insert_isoc_to_pfl:"
10697c478bd9Sstevel@tonic-gate 				    "Invalid starting frame number");
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 				return (USB_INVALID_START_FRAME);
10727c478bd9Sstevel@tonic-gate 			}
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 			/* Get the starting usb frame number */
10757c478bd9Sstevel@tonic-gate 			start_frame_number = isoc_reqp->isoc_frame_no;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 			pp->pp_next_frame_number = 0;
10787c478bd9Sstevel@tonic-gate 		}
10797c478bd9Sstevel@tonic-gate 		break;
10807c478bd9Sstevel@tonic-gate 	case USB_ATTRS_ISOC_XFER_ASAP:
10817c478bd9Sstevel@tonic-gate 		/* ehci has to specify starting frame number */
10827c478bd9Sstevel@tonic-gate 		if ((pp->pp_next_frame_number) &&
10837c478bd9Sstevel@tonic-gate 		    (pp->pp_next_frame_number > current_frame_number)) {
10847c478bd9Sstevel@tonic-gate 			/*
10857c478bd9Sstevel@tonic-gate 			 * Get the next usb frame number.
10867c478bd9Sstevel@tonic-gate 			 */
10877c478bd9Sstevel@tonic-gate 			start_frame_number = pp->pp_next_frame_number;
10887c478bd9Sstevel@tonic-gate 		} else {
10897c478bd9Sstevel@tonic-gate 			/*
10907c478bd9Sstevel@tonic-gate 			 * Add appropriate offset to the current usb
10917c478bd9Sstevel@tonic-gate 			 * frame number and use it as a starting frame
10927c478bd9Sstevel@tonic-gate 			 * number.
10937c478bd9Sstevel@tonic-gate 			 */
10947c478bd9Sstevel@tonic-gate 			start_frame_number =
10957c478bd9Sstevel@tonic-gate 			    current_frame_number + EHCI_FRAME_OFFSET;
10967c478bd9Sstevel@tonic-gate 		}
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 		if (!(pp->pp_flag & EHCI_ISOC_XFER_CONTINUE)) {
10997c478bd9Sstevel@tonic-gate 			isoc_reqp->isoc_frame_no = start_frame_number;
11007c478bd9Sstevel@tonic-gate 		}
11017c478bd9Sstevel@tonic-gate 		break;
11027c478bd9Sstevel@tonic-gate 	default:
11037c478bd9Sstevel@tonic-gate 		/* Exit the critical */
11047c478bd9Sstevel@tonic-gate 		ddi_exit_critical(ddic);
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
11077c478bd9Sstevel@tonic-gate 		    "ehci_insert_isoc_to_pfl: Either starting "
11087c478bd9Sstevel@tonic-gate 		    "frame number or ASAP flags are not set, attrs = 0x%x",
11097c478bd9Sstevel@tonic-gate 		    isoc_reqp->isoc_attributes);
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 		return (USB_NO_FRAME_NUMBER);
11127c478bd9Sstevel@tonic-gate 	}
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	if (itw->itw_port_status == USBA_HIGH_SPEED_DEV) {
1115b3001defSlg 		port_status = EHCI_ITD_LINK_REF_ITD;
11167c478bd9Sstevel@tonic-gate 	} else {
11177c478bd9Sstevel@tonic-gate 		port_status = EHCI_ITD_LINK_REF_SITD;
11187c478bd9Sstevel@tonic-gate 	}
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	itd = itw->itw_itd_head;
11217c478bd9Sstevel@tonic-gate 	while (itd) {
11227c478bd9Sstevel@tonic-gate 		/* Find the appropriate frame list to put the itd into */
11237c478bd9Sstevel@tonic-gate 		pfl_number = start_frame_number % EHCI_NUM_PERIODIC_FRAME_LISTS;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 		addr = Get_PFLT(periodic_frame_list->
11267c478bd9Sstevel@tonic-gate 		    ehci_periodic_frame_list_table[pfl_number]);
11277c478bd9Sstevel@tonic-gate 		Set_ITD(itd->itd_link_ptr, addr);
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 		/* Set the link_ref correctly as ITD or SITD. */
11307c478bd9Sstevel@tonic-gate 		addr = ehci_itd_cpu_to_iommu(ehcip, itd) & EHCI_ITD_LINK_PTR;
11317c478bd9Sstevel@tonic-gate 		addr |= port_status;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 		Set_PFLT(periodic_frame_list->
11347c478bd9Sstevel@tonic-gate 		    ehci_periodic_frame_list_table[pfl_number], addr);
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 		/* Save which frame the ITD was inserted into */
11377c478bd9Sstevel@tonic-gate 		Set_ITD_FRAME(itd->itd_frame_number, start_frame_number);
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 		ehci_insert_itd_into_active_list(ehcip, itd);
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 		/* Get the next ITD in the ITW */
11427c478bd9Sstevel@tonic-gate 		itd = ehci_itd_iommu_to_cpu(ehcip,
11437c478bd9Sstevel@tonic-gate 		    Get_ITD(itd->itd_itw_next_itd));
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 		start_frame_number++;
11467c478bd9Sstevel@tonic-gate 	}
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	/* Exit the critical */
11497c478bd9Sstevel@tonic-gate 	ddi_exit_critical(ddic);
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1152b3001defSlg 	    "ehci_insert_isoc_to_pfl: "
11537c478bd9Sstevel@tonic-gate 	    "current frame number 0x%llx start frame number 0x%llx num itds %d",
1154112116d8Sfb 	    (unsigned long long)current_frame_number,
1155112116d8Sfb 	    (unsigned long long)start_frame_number, itw->itw_num_itds);
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	/*
11587c478bd9Sstevel@tonic-gate 	 * Increment this saved frame number by current number
11597c478bd9Sstevel@tonic-gate 	 * of data packets needs to be transfer.
11607c478bd9Sstevel@tonic-gate 	 */
11617c478bd9Sstevel@tonic-gate 	pp->pp_next_frame_number = start_frame_number;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	/*
11647c478bd9Sstevel@tonic-gate 	 * Set EHCI_ISOC_XFER_CONTINUE flag in order to send other
11657c478bd9Sstevel@tonic-gate 	 * isochronous packets,  part of the current isoch request
11667c478bd9Sstevel@tonic-gate 	 * in the subsequent frames.
11677c478bd9Sstevel@tonic-gate 	 */
11687c478bd9Sstevel@tonic-gate 	pp->pp_flag |= EHCI_ISOC_XFER_CONTINUE;
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
11717c478bd9Sstevel@tonic-gate }
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate /*
11757c478bd9Sstevel@tonic-gate  * ehci_remove_isoc_to_pfl:
11767c478bd9Sstevel@tonic-gate  *
11777c478bd9Sstevel@tonic-gate  * Remove an ITD request from the Host Controller's isochronous list.
11787c478bd9Sstevel@tonic-gate  * If we can't find it, something has gone wrong.
11797c478bd9Sstevel@tonic-gate  */
11807c478bd9Sstevel@tonic-gate void
11817c478bd9Sstevel@tonic-gate ehci_remove_isoc_from_pfl(
11827c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
11837c478bd9Sstevel@tonic-gate 	ehci_itd_t		*curr_itd)
11847c478bd9Sstevel@tonic-gate {
11857c478bd9Sstevel@tonic-gate 	ehci_periodic_frame_list_t *periodic_frame_list;
11867c478bd9Sstevel@tonic-gate 	uint_t		pfl_number;
11877c478bd9Sstevel@tonic-gate 	uint32_t	next_addr, curr_itd_addr;
11887c478bd9Sstevel@tonic-gate 	uint32_t	link_ref;
11897c478bd9Sstevel@tonic-gate 	ehci_itd_t	*prev_itd = NULL;
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
11927c478bd9Sstevel@tonic-gate 	    "ehci_remove_isoc_from_pfl:");
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	/* Get the address of the current itd */
11977c478bd9Sstevel@tonic-gate 	curr_itd_addr = ehci_itd_cpu_to_iommu(ehcip, curr_itd);
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	/*
12007c478bd9Sstevel@tonic-gate 	 * Remove this ITD from the PFL
12017c478bd9Sstevel@tonic-gate 	 * But first we need to find it in the PFL
12027c478bd9Sstevel@tonic-gate 	 */
12037c478bd9Sstevel@tonic-gate 	periodic_frame_list = ehcip->ehci_periodic_frame_list_tablep;
12047c478bd9Sstevel@tonic-gate 	pfl_number = Get_ITD_FRAME(curr_itd->itd_frame_number) %
12057c478bd9Sstevel@tonic-gate 	    EHCI_NUM_PERIODIC_FRAME_LISTS;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
12087c478bd9Sstevel@tonic-gate 	    "ehci_remove_isoc_from_pfl: itd = 0x%p pfl number 0x%x",
1209112116d8Sfb 	    (void *)curr_itd, pfl_number);
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	next_addr = Get_PFLT(periodic_frame_list->
12127c478bd9Sstevel@tonic-gate 	    ehci_periodic_frame_list_table[pfl_number]);
12137c478bd9Sstevel@tonic-gate 	while ((next_addr & EHCI_ITD_LINK_PTR) !=
12147c478bd9Sstevel@tonic-gate 	    (curr_itd_addr & EHCI_ITD_LINK_PTR)) {
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 		link_ref = next_addr & EHCI_ITD_LINK_REF;
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 		if ((link_ref == EHCI_ITD_LINK_REF_ITD) ||
12197c478bd9Sstevel@tonic-gate 		    (link_ref == EHCI_ITD_LINK_REF_SITD)) {
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 			prev_itd = ehci_itd_iommu_to_cpu(ehcip,
12227c478bd9Sstevel@tonic-gate 			    (next_addr & EHCI_ITD_LINK_PTR));
12237c478bd9Sstevel@tonic-gate 			next_addr = Get_ITD(prev_itd->itd_link_ptr);
12247c478bd9Sstevel@tonic-gate 		} else {
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 			break;
12277c478bd9Sstevel@tonic-gate 		}
12287c478bd9Sstevel@tonic-gate 	}
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	/*
12317c478bd9Sstevel@tonic-gate 	 * If the next itd is the current itd, that means we found it.
12327c478bd9Sstevel@tonic-gate 	 * Set the previous's ITD link ptr to the Curr_ITD's link ptr.
12337c478bd9Sstevel@tonic-gate 	 * But do not touch the Curr_ITD's link ptr.
12347c478bd9Sstevel@tonic-gate 	 */
12357c478bd9Sstevel@tonic-gate 	if ((next_addr & EHCI_ITD_LINK_PTR) ==
12367c478bd9Sstevel@tonic-gate 	    (curr_itd_addr & EHCI_ITD_LINK_PTR)) {
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 		next_addr = Get_ITD(curr_itd->itd_link_ptr);
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 		if (prev_itd == NULL) {
12417c478bd9Sstevel@tonic-gate 			/* This means PFL points to this ITD */
12427c478bd9Sstevel@tonic-gate 			Set_PFLT(periodic_frame_list->
12437c478bd9Sstevel@tonic-gate 			    ehci_periodic_frame_list_table[pfl_number],
12447c478bd9Sstevel@tonic-gate 			    next_addr);
12457c478bd9Sstevel@tonic-gate 		} else {
12467c478bd9Sstevel@tonic-gate 			/* Set the previous ITD's itd_link_ptr */
12477c478bd9Sstevel@tonic-gate 			Set_ITD(prev_itd->itd_link_ptr, next_addr);
12487c478bd9Sstevel@tonic-gate 		}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 		Set_ITD_FRAME(curr_itd->itd_frame_number, 0);
12517c478bd9Sstevel@tonic-gate 	} else {
12527c478bd9Sstevel@tonic-gate 		ASSERT((next_addr & EHCI_ITD_LINK_PTR) ==
12537c478bd9Sstevel@tonic-gate 		    (curr_itd_addr & EHCI_ITD_LINK_PTR));
12547c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
12557c478bd9Sstevel@tonic-gate 		    "ehci_remove_isoc_from_pfl: Unable to find ITD in PFL");
12567c478bd9Sstevel@tonic-gate 	}
12577c478bd9Sstevel@tonic-gate }
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate /*
12617c478bd9Sstevel@tonic-gate  * Isochronous in resource functions
12627c478bd9Sstevel@tonic-gate  */
12637c478bd9Sstevel@tonic-gate /*
12647c478bd9Sstevel@tonic-gate  * ehci_allocate_periodic_in_resource
12657c478bd9Sstevel@tonic-gate  *
12667c478bd9Sstevel@tonic-gate  * Allocate interrupt request structure for the interrupt IN transfer.
12677c478bd9Sstevel@tonic-gate  */
12687c478bd9Sstevel@tonic-gate int
12697c478bd9Sstevel@tonic-gate ehci_allocate_isoc_in_resource(
12707c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
12717c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
12727c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
12737c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
12747c478bd9Sstevel@tonic-gate {
12757c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph = pp->pp_pipe_handle;
12767c478bd9Sstevel@tonic-gate 	usb_isoc_req_t		*orig_isoc_reqp, *clone_isoc_reqp;
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
12797c478bd9Sstevel@tonic-gate 	    "ehci_allocate_isoc_in_resource:"
1280112116d8Sfb 	    "pp = 0x%p itw = 0x%p flags = 0x%x", (void *)pp, (void *)itw,
1281112116d8Sfb 	    flags);
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
12847c478bd9Sstevel@tonic-gate 	ASSERT(itw->itw_curr_xfer_reqp == NULL);
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	/* Get the client periodic in request pointer */
12877c478bd9Sstevel@tonic-gate 	orig_isoc_reqp = (usb_isoc_req_t *)(pp->pp_client_periodic_in_reqp);
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	ASSERT(orig_isoc_reqp != NULL);
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	clone_isoc_reqp = usba_hcdi_dup_isoc_req(ph->p_dip,
12927c478bd9Sstevel@tonic-gate 	    orig_isoc_reqp, flags);
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	if (clone_isoc_reqp == NULL) {
12957c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
12967c478bd9Sstevel@tonic-gate 		    "ehci_allocate_isoc_in_resource: Isochronous"
12977c478bd9Sstevel@tonic-gate 		    "request structure allocation failed");
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 		return (USB_NO_RESOURCES);
13007c478bd9Sstevel@tonic-gate 	}
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	/*
13037c478bd9Sstevel@tonic-gate 	 * Save the client's isochronous request pointer and
13047c478bd9Sstevel@tonic-gate 	 * length of isochronous transfer in transfer wrapper.
13057c478bd9Sstevel@tonic-gate 	 * The dup'ed request is saved in pp_client_periodic_in_reqp
13067c478bd9Sstevel@tonic-gate 	 */
13077c478bd9Sstevel@tonic-gate 	itw->itw_curr_xfer_reqp = orig_isoc_reqp;
1308b3001defSlg 
13097c478bd9Sstevel@tonic-gate 	pp->pp_client_periodic_in_reqp = (usb_opaque_t)clone_isoc_reqp;
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_mutex);
13127c478bd9Sstevel@tonic-gate 	ph->p_req_count++;
13137c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_mutex);
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	pp->pp_state = EHCI_PIPE_STATE_ACTIVE;
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
13187c478bd9Sstevel@tonic-gate }
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate /*
13227c478bd9Sstevel@tonic-gate  * ehci_deallocate_isoc_in_resource
13237c478bd9Sstevel@tonic-gate  *
13247c478bd9Sstevel@tonic-gate  * Deallocate interrupt request structure for the interrupt IN transfer.
13257c478bd9Sstevel@tonic-gate  */
13267c478bd9Sstevel@tonic-gate void
13277c478bd9Sstevel@tonic-gate ehci_deallocate_isoc_in_resource(
13287c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
13297c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
13307c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw)
13317c478bd9Sstevel@tonic-gate {
13327c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph = pp->pp_pipe_handle;
13337c478bd9Sstevel@tonic-gate 	uchar_t			ep_attr = ph->p_ep.bmAttributes;
13347c478bd9Sstevel@tonic-gate 	usb_isoc_req_t		*isoc_reqp;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS,
13377c478bd9Sstevel@tonic-gate 	    ehcip->ehci_log_hdl,
13387c478bd9Sstevel@tonic-gate 	    "ehci_deallocate_isoc_in_resource: "
1339112116d8Sfb 	    "pp = 0x%p itw = 0x%p", (void *)pp, (void *)itw);
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
13427c478bd9Sstevel@tonic-gate 	ASSERT((ep_attr & USB_EP_ATTR_MASK) == USB_EP_ATTR_ISOCH);
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	isoc_reqp = itw->itw_curr_xfer_reqp;
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	/* Check the current periodic in request pointer */
13477c478bd9Sstevel@tonic-gate 	if (isoc_reqp) {
13487c478bd9Sstevel@tonic-gate 		itw->itw_curr_xfer_reqp = NULL;
13497c478bd9Sstevel@tonic-gate 		itw->itw_curr_isoc_pktp = NULL;
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 		mutex_enter(&ph->p_mutex);
13527c478bd9Sstevel@tonic-gate 		ph->p_req_count--;
13537c478bd9Sstevel@tonic-gate 		mutex_exit(&ph->p_mutex);
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 		usb_free_isoc_req(isoc_reqp);
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 		/* Set periodic in pipe state to idle */
13587c478bd9Sstevel@tonic-gate 		pp->pp_state = EHCI_PIPE_STATE_IDLE;
13597c478bd9Sstevel@tonic-gate 	}
13607c478bd9Sstevel@tonic-gate }
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate /*
13647c478bd9Sstevel@tonic-gate  * ehci_itd_cpu_to_iommu:
13657c478bd9Sstevel@tonic-gate  *
13667c478bd9Sstevel@tonic-gate  * This function converts for the given Transfer Descriptor (ITD) CPU address
13677c478bd9Sstevel@tonic-gate  * to IO address.
13687c478bd9Sstevel@tonic-gate  */
13697c478bd9Sstevel@tonic-gate uint32_t
13707c478bd9Sstevel@tonic-gate ehci_itd_cpu_to_iommu(
13717c478bd9Sstevel@tonic-gate 	ehci_state_t	*ehcip,
13727c478bd9Sstevel@tonic-gate 	ehci_itd_t	*addr)
13737c478bd9Sstevel@tonic-gate {
13747c478bd9Sstevel@tonic-gate 	uint32_t	td;
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	if (addr == NULL) {
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 		return (NULL);
13817c478bd9Sstevel@tonic-gate 	}
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	td = (uint32_t)ehcip->ehci_itd_pool_cookie.dmac_address +
13847c478bd9Sstevel@tonic-gate 	    (uint32_t)((uintptr_t)addr -
13850a05e705Slc 	    (uintptr_t)(ehcip->ehci_itd_pool_addr));
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	ASSERT(((uint32_t) (sizeof (ehci_itd_t) *
13880a05e705Slc 	    (addr - ehcip->ehci_itd_pool_addr))) ==
13897c478bd9Sstevel@tonic-gate 	    ((uint32_t)((uintptr_t)addr - (uintptr_t)
13900a05e705Slc 	    (ehcip->ehci_itd_pool_addr))));
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	ASSERT(td >= ehcip->ehci_itd_pool_cookie.dmac_address);
13937c478bd9Sstevel@tonic-gate 	ASSERT(td <= ehcip->ehci_itd_pool_cookie.dmac_address +
13947c478bd9Sstevel@tonic-gate 	    sizeof (ehci_itd_t) * ehci_itd_pool_size);
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	return (td);
13977c478bd9Sstevel@tonic-gate }
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate /*
14017c478bd9Sstevel@tonic-gate  * ehci_itd_iommu_to_cpu:
14027c478bd9Sstevel@tonic-gate  *
14037c478bd9Sstevel@tonic-gate  * This function converts for the given Transfer Descriptor (ITD) IO address
14047c478bd9Sstevel@tonic-gate  * to CPU address.
14057c478bd9Sstevel@tonic-gate  */
14067c478bd9Sstevel@tonic-gate ehci_itd_t *
14077c478bd9Sstevel@tonic-gate ehci_itd_iommu_to_cpu(
14087c478bd9Sstevel@tonic-gate 	ehci_state_t	*ehcip,
14097c478bd9Sstevel@tonic-gate 	uintptr_t	addr)
14107c478bd9Sstevel@tonic-gate {
14117c478bd9Sstevel@tonic-gate 	ehci_itd_t	*itd;
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	if (addr == NULL) {
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 		return (NULL);
14187c478bd9Sstevel@tonic-gate 	}
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 	itd = (ehci_itd_t *)((uintptr_t)
14217c478bd9Sstevel@tonic-gate 	    (addr - ehcip->ehci_itd_pool_cookie.dmac_address) +
14227c478bd9Sstevel@tonic-gate 	    (uintptr_t)ehcip->ehci_itd_pool_addr);
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	ASSERT(itd >= ehcip->ehci_itd_pool_addr);
14257c478bd9Sstevel@tonic-gate 	ASSERT((uintptr_t)itd <= (uintptr_t)ehcip->ehci_itd_pool_addr +
14267c478bd9Sstevel@tonic-gate 	    (uintptr_t)(sizeof (ehci_itd_t) * ehci_itd_pool_size));
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	return (itd);
14297c478bd9Sstevel@tonic-gate }
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate /*
14337c478bd9Sstevel@tonic-gate  * Error parsing functions
14347c478bd9Sstevel@tonic-gate  */
14357c478bd9Sstevel@tonic-gate void ehci_parse_isoc_error(
14367c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
14377c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
1438b3001defSlg 	ehci_itd_t		*itd)
1439b3001defSlg {
14407c478bd9Sstevel@tonic-gate 	usb_isoc_req_t		*isoc_reqp;
14410a05e705Slc 	usb_cr_t		error;
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	isoc_reqp = itw->itw_curr_xfer_reqp;
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 	if (itw->itw_port_status == USBA_HIGH_SPEED_DEV) {
14487c478bd9Sstevel@tonic-gate 		error = ehci_parse_itd_error(ehcip, itw, itd);
14497c478bd9Sstevel@tonic-gate 	} else {
14507c478bd9Sstevel@tonic-gate 		error = ehci_parse_sitd_error(ehcip, itw, itd);
14517c478bd9Sstevel@tonic-gate 
14520a05e705Slc 		if (error != USB_CR_OK) {
14530a05e705Slc 			isoc_reqp->isoc_error_count++;
14540a05e705Slc 
14550a05e705Slc 			USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
14560a05e705Slc 			    "ehci_parse_sitd_error: Error %d Device Address %d"
14570a05e705Slc 			    " Endpoint number %d", error, itw->itw_device_addr,
14580a05e705Slc 			    itw->itw_endpoint_num);
14590a05e705Slc 		}
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	}
14627c478bd9Sstevel@tonic-gate }
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate /* ARGSUSED */
14667c478bd9Sstevel@tonic-gate static usb_cr_t ehci_parse_itd_error(
14677c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
14687c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
1469b3001defSlg 	ehci_itd_t		*itd)
1470b3001defSlg {
1471b3001defSlg 	uint32_t		status, index;
14720a05e705Slc 	usb_cr_t		error = USB_CR_OK;
1473b3001defSlg 	uint32_t		i;
14740a05e705Slc 	usb_isoc_req_t		*isoc_reqp;
14750a05e705Slc 
14760a05e705Slc 	isoc_reqp = itw->itw_curr_xfer_reqp;
1477b3001defSlg 
1478b3001defSlg 	for (i = 0; i < EHCI_ITD_CTRL_LIST_SIZE; i++) {
1479b3001defSlg 		index = Get_ITD_INDEX(itd, i);
1480b3001defSlg 		if (index == 0xffffffff) {
1481b3001defSlg 
1482b3001defSlg 			continue;
1483b3001defSlg 		}
1484b3001defSlg 
14850a05e705Slc 		error = USB_CR_OK;
14860a05e705Slc 
1487b3001defSlg 		status = Get_ITD_BODY(itd, EHCI_ITD_CTRL0 + i) &
1488b3001defSlg 		    EHCI_ITD_XFER_STATUS_MASK;
1489b3001defSlg 
1490b3001defSlg 		if (status & EHCI_ITD_XFER_DATA_BUFFER_ERR) {
1491b3001defSlg 			if (itw->itw_direction == USB_EP_DIR_OUT) {
14920a05e705Slc 				USB_DPRINTF_L3(PRINT_MASK_INTR,
14930a05e705Slc 				    ehcip->ehci_log_hdl,
14940a05e705Slc 				    "ehci_parse_itd_error: BUFFER Underrun");
1495b3001defSlg 
14960a05e705Slc 				error = USB_CR_BUFFER_UNDERRUN;
1497b3001defSlg 			} else {
14980a05e705Slc 				USB_DPRINTF_L3(PRINT_MASK_INTR,
14990a05e705Slc 				    ehcip->ehci_log_hdl,
15000a05e705Slc 				    "ehci_parse_itd_error: BUFFER Overrun");
1501b3001defSlg 
15020a05e705Slc 				error = USB_CR_BUFFER_OVERRUN;
1503b3001defSlg 			}
1504b3001defSlg 		}
1505b3001defSlg 
1506b3001defSlg 		if (status & EHCI_ITD_XFER_BABBLE) {
1507b3001defSlg 			USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
1508b3001defSlg 			    "ehci_parse_itd_error: BABBLE DETECTED");
1509b3001defSlg 
1510b3001defSlg 			error = USB_CR_DATA_OVERRUN;
1511b3001defSlg 		}
15127c478bd9Sstevel@tonic-gate 
1513b3001defSlg 		if (status & EHCI_ITD_XFER_ERROR) {
1514b3001defSlg 			USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
1515b3001defSlg 			    "ehci_parse_itd_error: XACT ERROR");
1516b3001defSlg 
1517b3001defSlg 			error = USB_CR_DEV_NOT_RESP;
1518b3001defSlg 		}
1519b3001defSlg 
1520b3001defSlg 		if (status & EHCI_ITD_XFER_ACTIVE) {
1521b3001defSlg 			USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
1522b3001defSlg 			    "ehci_parse_itd_error: NOT ACCESSED");
1523b3001defSlg 
1524b3001defSlg 			error = USB_CR_NOT_ACCESSED;
1525b3001defSlg 		}
1526b3001defSlg 
1527b3001defSlg 		itw->itw_curr_isoc_pktp->isoc_pkt_actual_length = 0;
1528b3001defSlg 
1529b3001defSlg 		/* Write the status of isoc data packet */
1530b3001defSlg 		itw->itw_curr_isoc_pktp->isoc_pkt_status = error;
1531b3001defSlg 
15320a05e705Slc 		/* counts total number of error packets in this req */
15330a05e705Slc 		if (error != USB_CR_OK) {
15340a05e705Slc 			isoc_reqp->isoc_error_count++;
15350a05e705Slc 			USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
15360a05e705Slc 			    "ehci_parse_itd_error: Error %d Device Address %d "
15370a05e705Slc 			    "Endpoint number %d", error, itw->itw_device_addr,
15380a05e705Slc 			    itw->itw_endpoint_num);
15390a05e705Slc 		}
15400a05e705Slc 
1541b3001defSlg 		itw->itw_curr_isoc_pktp++;
1542b3001defSlg 	}
15430a05e705Slc 
1544b3001defSlg 	return (error);
15457c478bd9Sstevel@tonic-gate }
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate static usb_cr_t ehci_parse_sitd_error(
15487c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
15497c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
1550b3001defSlg 	ehci_itd_t		*itd)
1551b3001defSlg {
15527c478bd9Sstevel@tonic-gate 	uint32_t		status;
15530a05e705Slc 	usb_cr_t		error;
15547c478bd9Sstevel@tonic-gate 	usb_isoc_pkt_descr_t	*isoc_pkt_descr;
15557c478bd9Sstevel@tonic-gate 	uint32_t		residue;
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	isoc_pkt_descr = itw->itw_curr_isoc_pktp;
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	status = Get_ITD_BODY(itd, EHCI_SITD_XFER_STATE) &
15607c478bd9Sstevel@tonic-gate 	    EHCI_SITD_XFER_STATUS_MASK;
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	switch (status) {
15637c478bd9Sstevel@tonic-gate 	case EHCI_SITD_XFER_ACTIVE:
15647c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
15657c478bd9Sstevel@tonic-gate 		    "ehci_check_for_sitd_error: NOT ACCESSED");
15667c478bd9Sstevel@tonic-gate 		error = USB_CR_NOT_ACCESSED;
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 		break;
15697c478bd9Sstevel@tonic-gate 	case EHCI_SITD_XFER_ERROR:
15707c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
15717c478bd9Sstevel@tonic-gate 		    "ehci_check_for_sitd_error: TT ERROR");
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 		error = USB_CR_UNSPECIFIED_ERR;
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 		break;
15767c478bd9Sstevel@tonic-gate 	case EHCI_SITD_XFER_DATA_BUFFER_ERR:
15777c478bd9Sstevel@tonic-gate 		if (itw->itw_direction == USB_EP_DIR_OUT) {
15787c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
15797c478bd9Sstevel@tonic-gate 			    "ehci_check_for_sitd_error: BUFFER Underrun");
15807c478bd9Sstevel@tonic-gate 			error = USB_CR_BUFFER_UNDERRUN;
15817c478bd9Sstevel@tonic-gate 		} else {
15827c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
15837c478bd9Sstevel@tonic-gate 			    "ehci_check_for_sitd_error: BUFFER Overrun");
15847c478bd9Sstevel@tonic-gate 			error = USB_CR_BUFFER_OVERRUN;
15857c478bd9Sstevel@tonic-gate 		}
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 		break;
15887c478bd9Sstevel@tonic-gate 	case EHCI_SITD_XFER_BABBLE:
15897c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
15907c478bd9Sstevel@tonic-gate 		    "ehci_check_for_sitd_error: BABBLE");
15917c478bd9Sstevel@tonic-gate 		error = USB_CR_DATA_OVERRUN;
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 		break;
15947c478bd9Sstevel@tonic-gate 	case EHCI_SITD_XFER_XACT_ERROR:
15957c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
15967c478bd9Sstevel@tonic-gate 		    "ehci_check_for_sitd_error: XACT ERROR");
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 		error = USB_CR_DEV_NOT_RESP;
15997c478bd9Sstevel@tonic-gate 		break;
16007c478bd9Sstevel@tonic-gate 	case EHCI_SITD_XFER_MISSED_UFRAME:
16017c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
16027c478bd9Sstevel@tonic-gate 		    "ehci_check_for_sitd_error: MISSED UFRAME");
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 		error = USB_CR_NOT_ACCESSED;
16057c478bd9Sstevel@tonic-gate 		break;
16067c478bd9Sstevel@tonic-gate 	default:
16077c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
16087c478bd9Sstevel@tonic-gate 		    "ehci_check_for_sitd_error: NO ERROR");
16097c478bd9Sstevel@tonic-gate 		error = USB_CR_OK;
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 		break;
16127c478bd9Sstevel@tonic-gate 	}
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	/* This is HCD specific and may not have this information */
16157c478bd9Sstevel@tonic-gate 	residue =
16167c478bd9Sstevel@tonic-gate 	    (Get_ITD_BODY(itd, EHCI_SITD_XFER_STATE) &
16170a05e705Slc 	    EHCI_SITD_XFER_TOTAL_MASK) >>
16187c478bd9Sstevel@tonic-gate 	    EHCI_SITD_XFER_TOTAL_SHIFT;
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	/*
16217c478bd9Sstevel@tonic-gate 	 * Subtract the residue from the isoc_pkt_descr that
16227c478bd9Sstevel@tonic-gate 	 * was set when this ITD was inserted.
16237c478bd9Sstevel@tonic-gate 	 */
16247c478bd9Sstevel@tonic-gate 	isoc_pkt_descr->isoc_pkt_actual_length -= residue;
16257c478bd9Sstevel@tonic-gate 
1626b3001defSlg 	/* Write the status of isoc data packet */
1627b3001defSlg 	isoc_pkt_descr->isoc_pkt_status = error;
1628b3001defSlg 
1629b3001defSlg 	itw->itw_curr_isoc_pktp++;
1630b3001defSlg 
16317c478bd9Sstevel@tonic-gate 	return (error);
16327c478bd9Sstevel@tonic-gate }
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate /*
16367c478bd9Sstevel@tonic-gate  * debug print functions
16377c478bd9Sstevel@tonic-gate  */
16387c478bd9Sstevel@tonic-gate void
16397c478bd9Sstevel@tonic-gate ehci_print_itd(
16407c478bd9Sstevel@tonic-gate 	ehci_state_t	*ehcip,
16417c478bd9Sstevel@tonic-gate 	ehci_itd_t	*itd)
16427c478bd9Sstevel@tonic-gate {
16437c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
16447c478bd9Sstevel@tonic-gate 	    "ehci_print_itd: itd = 0x%p", (void *)itd);
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
16477c478bd9Sstevel@tonic-gate 	    "\titd_link_ptr: 0x%x ", Get_ITD(itd->itd_link_ptr));
16487c478bd9Sstevel@tonic-gate 
1649b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1650b3001defSlg 	    "\titd_ctrl0: 0x%x ",
1651b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_ITD_CTRL0]));
1652b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1653b3001defSlg 	    "\titd_ctrl1: 0x%x ",
1654b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_ITD_CTRL1]));
1655b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1656b3001defSlg 	    "\titd_ctrl2: 0x%x ",
1657b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_ITD_CTRL2]));
1658b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1659b3001defSlg 	    "\titd_ctrl3: 0x%x ",
1660b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_ITD_CTRL3]));
1661b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1662b3001defSlg 	    "\titd_ctrl4: 0x%x ",
1663b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_ITD_CTRL4]));
1664b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1665b3001defSlg 	    "\titd_ctrl5: 0x%x ",
1666b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_ITD_CTRL5]));
1667b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1668b3001defSlg 	    "\titd_ctrl6: 0x%x ",
1669b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_ITD_CTRL6]));
1670b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1671b3001defSlg 	    "\titd_ctrl7: 0x%x ",
1672b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_ITD_CTRL7]));
1673b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1674b3001defSlg 	    "\titd_buffer0: 0x%x ",
1675b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_ITD_BUFFER0]));
1676b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1677b3001defSlg 	    "\titd_buffer1: 0x%x ",
1678b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_ITD_BUFFER1]));
1679b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1680b3001defSlg 	    "\titd_buffer2: 0x%x ",
1681b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_ITD_BUFFER2]));
1682b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1683b3001defSlg 	    "\titd_buffer3: 0x%x ",
1684b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_ITD_BUFFER3]));
1685b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1686b3001defSlg 	    "\titd_buffer4: 0x%x ",
1687b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_ITD_BUFFER4]));
1688b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1689b3001defSlg 	    "\titd_buffer5: 0x%x ",
1690b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_ITD_BUFFER5]));
1691b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1692b3001defSlg 	    "\titd_buffer6: 0x%x ",
1693b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_ITD_BUFFER6]));
1694b3001defSlg 
1695b3001defSlg 	/* HCD private fields */
1696b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1697b3001defSlg 	    "\titd_trans_wrapper: 0x%x ",
1698b3001defSlg 	    Get_ITD(itd->itd_trans_wrapper));
1699b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1700b3001defSlg 	    "\titd_itw_next_itd: 0x%x ",
1701b3001defSlg 	    Get_ITD(itd->itd_itw_next_itd));
1702b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1703b3001defSlg 	    "\titd_state: 0x%x ",
1704b3001defSlg 	    Get_ITD(itd->itd_state));
1705b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1706b3001defSlg 	    "\titd_index: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x ",
1707b3001defSlg 	    Get_ITD_INDEX(itd, 0), Get_ITD_INDEX(itd, 1),
1708b3001defSlg 	    Get_ITD_INDEX(itd, 2), Get_ITD_INDEX(itd, 3),
1709b3001defSlg 	    Get_ITD_INDEX(itd, 4), Get_ITD_INDEX(itd, 5),
1710b3001defSlg 	    Get_ITD_INDEX(itd, 6), Get_ITD_INDEX(itd, 7));
1711b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1712b3001defSlg 	    "\titd_frame_number: 0x%x ",
1713b3001defSlg 	    Get_ITD(itd->itd_frame_number));
1714b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1715b3001defSlg 	    "\titd_reclaim_number: 0x%x ",
1716b3001defSlg 	    Get_ITD(itd->itd_reclaim_number));
1717b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1718b3001defSlg 	    "\titd_next_active_itd: 0x%x ",
1719b3001defSlg 	    Get_ITD(itd->itd_next_active_itd));
1720b3001defSlg }
1721b3001defSlg 
1722b3001defSlg 
1723b3001defSlg void
1724b3001defSlg ehci_print_sitd(
1725b3001defSlg 	ehci_state_t	*ehcip,
1726b3001defSlg 	ehci_itd_t	*itd)
1727b3001defSlg {
1728b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1729b3001defSlg 	    "ehci_print_itd: itd = 0x%p", (void *)itd);
1730b3001defSlg 
1731b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1732b3001defSlg 	    "\titd_link_ptr: 0x%x ", Get_ITD(itd->itd_link_ptr));
1733b3001defSlg 
1734b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1735b3001defSlg 	    "\tsitd_ctrl: 0x%x ",
1736b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_SITD_CTRL]));
1737b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1738b3001defSlg 	    "\tsitd_uframe_sched: 0x%x ",
1739b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_SITD_UFRAME_SCHED]));
1740b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1741b3001defSlg 	    "\tsitd_xfer_state: 0x%x ",
1742b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_SITD_XFER_STATE]));
1743b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1744b3001defSlg 	    "\tsitd_buffer0: 0x%x ",
1745b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_SITD_BUFFER0]));
1746b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1747b3001defSlg 	    "\tsitd_buffer1: 0x%x ",
1748b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_SITD_BUFFER1]));
1749b3001defSlg 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
1750b3001defSlg 	    "\tsitd_prev_sitd: 0x%x ",
1751b3001defSlg 	    Get_ITD(itd->itd_body[EHCI_SITD_PREV_SITD]));
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	/* HCD private fields */
17547c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
17557c478bd9Sstevel@tonic-gate 	    "\titd_trans_wrapper: 0x%x ",
17567c478bd9Sstevel@tonic-gate 	    Get_ITD(itd->itd_trans_wrapper));
17577c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
17587c478bd9Sstevel@tonic-gate 	    "\titd_itw_next_itd: 0x%x ",
17597c478bd9Sstevel@tonic-gate 	    Get_ITD(itd->itd_itw_next_itd));
17607c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
17617c478bd9Sstevel@tonic-gate 	    "\titd_state: 0x%x ",
17627c478bd9Sstevel@tonic-gate 	    Get_ITD(itd->itd_state));
17637c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
17647c478bd9Sstevel@tonic-gate 	    "\titd_frame_number: 0x%x ",
17657c478bd9Sstevel@tonic-gate 	    Get_ITD(itd->itd_frame_number));
17667c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
17677c478bd9Sstevel@tonic-gate 	    "\titd_reclaim_number: 0x%x ",
17687c478bd9Sstevel@tonic-gate 	    Get_ITD(itd->itd_reclaim_number));
17697c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
17707c478bd9Sstevel@tonic-gate 	    "\titd_next_active_itd: 0x%x ",
17717c478bd9Sstevel@tonic-gate 	    Get_ITD(itd->itd_next_active_itd));
17727c478bd9Sstevel@tonic-gate }
1773