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  */
21dfbb3a42SRaymond Chen 
227c478bd9Sstevel@tonic-gate /*
23dfbb3a42SRaymond Chen  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
2448bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
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
ehci_allocate_isoc_pools(ehci_state_t * ehcip)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
ehci_get_itd_pool_size()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 *
ehci_allocate_itw_resources(ehci_state_t * ehcip,ehci_pipe_private_t * pp,size_t itw_length,usb_flags_t usb_flags,size_t pkt_count)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 *
ehci_allocate_itw(ehci_state_t * ehcip,ehci_pipe_private_t * pp,size_t length,usb_flags_t usb_flags)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);
490*0a1044f1SToomas Soome 	ASSERT(itw->itw_id != 0);
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 
496dfbb3a42SRaymond Chen 	ehcip->ehci_periodic_req_count++;
497dfbb3a42SRaymond Chen 	ehci_toggle_scheduler(ehcip);
498dfbb3a42SRaymond Chen 
4997c478bd9Sstevel@tonic-gate 	return (itw);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate  * ehci_deallocate_itw:
5057c478bd9Sstevel@tonic-gate  *
5067c478bd9Sstevel@tonic-gate  * Deallocate of a Isochronous Transaction Wrapper (TW) and this involves the
5077c478bd9Sstevel@tonic-gate  * freeing of DMA resources.
5087c478bd9Sstevel@tonic-gate  */
5097c478bd9Sstevel@tonic-gate void
ehci_deallocate_itw(ehci_state_t * ehcip,ehci_pipe_private_t * pp,ehci_isoc_xwrapper_t * itw)5107c478bd9Sstevel@tonic-gate ehci_deallocate_itw(
5117c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
5127c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
5137c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw)
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*prev, *next;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
518112116d8Sfb 	    "ehci_deallocate_itw: itw = 0x%p", (void *)itw);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	/*
5217c478bd9Sstevel@tonic-gate 	 * If the transfer wrapper has no Host Controller (HC)
5227c478bd9Sstevel@tonic-gate 	 * Transfer Descriptors (ITD) associated with it,  then
5237c478bd9Sstevel@tonic-gate 	 * remove the transfer wrapper.
5247c478bd9Sstevel@tonic-gate 	 */
5257c478bd9Sstevel@tonic-gate 	if (itw->itw_itd_head) {
5267c478bd9Sstevel@tonic-gate 		ASSERT(itw->itw_itd_tail != NULL);
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 		return;
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	ASSERT(itw->itw_itd_tail == NULL);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	/* Make sure we return all the unused itd's to the pool as well */
5347c478bd9Sstevel@tonic-gate 	ehci_deallocate_itds_for_itw(ehcip, itw);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	/*
5377c478bd9Sstevel@tonic-gate 	 * If pp->pp_tw_head and pp->pp_tw_tail are pointing to
5387c478bd9Sstevel@tonic-gate 	 * given TW then set the head and  tail  equal to NULL.
5397c478bd9Sstevel@tonic-gate 	 * Otherwise search for this TW in the linked TW's list
5407c478bd9Sstevel@tonic-gate 	 * and then remove this TW from the list.
5417c478bd9Sstevel@tonic-gate 	 */
5427c478bd9Sstevel@tonic-gate 	if (pp->pp_itw_head == itw) {
5437c478bd9Sstevel@tonic-gate 		if (pp->pp_itw_tail == itw) {
5447c478bd9Sstevel@tonic-gate 			pp->pp_itw_head = NULL;
5457c478bd9Sstevel@tonic-gate 			pp->pp_itw_tail = NULL;
5467c478bd9Sstevel@tonic-gate 		} else {
5477c478bd9Sstevel@tonic-gate 			pp->pp_itw_head = itw->itw_next;
5487c478bd9Sstevel@tonic-gate 		}
5497c478bd9Sstevel@tonic-gate 	} else {
5507c478bd9Sstevel@tonic-gate 		prev = pp->pp_itw_head;
5517c478bd9Sstevel@tonic-gate 		next = prev->itw_next;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 		while (next && (next != itw)) {
5547c478bd9Sstevel@tonic-gate 			prev = next;
5557c478bd9Sstevel@tonic-gate 			next = next->itw_next;
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 		if (next == itw) {
5597c478bd9Sstevel@tonic-gate 			prev->itw_next = next->itw_next;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 			if (pp->pp_itw_tail == itw) {
5627c478bd9Sstevel@tonic-gate 				pp->pp_itw_tail = prev;
5637c478bd9Sstevel@tonic-gate 			}
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	/* Free this iTWs dma resources */
5687c478bd9Sstevel@tonic-gate 	ehci_free_itw_dma(ehcip, pp, itw);
569dfbb3a42SRaymond Chen 
570dfbb3a42SRaymond Chen 	ehcip->ehci_periodic_req_count--;
571dfbb3a42SRaymond Chen 	ehci_toggle_scheduler(ehcip);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate /*
5767c478bd9Sstevel@tonic-gate  * ehci_free_itw_dma:
5777c478bd9Sstevel@tonic-gate  *
5787c478bd9Sstevel@tonic-gate  * Free the Isochronous Transfer Wrapper dma resources.
5797c478bd9Sstevel@tonic-gate  */
5807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5817c478bd9Sstevel@tonic-gate static void
ehci_free_itw_dma(ehci_state_t * ehcip,ehci_pipe_private_t * pp,ehci_isoc_xwrapper_t * itw)5827c478bd9Sstevel@tonic-gate ehci_free_itw_dma(
5837c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
5847c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
5857c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw)
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate 	int	rval;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
590112116d8Sfb 	    "ehci_free_itw_dma: itw = 0x%p", (void *)itw);
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	ASSERT(itw != NULL);
593*0a1044f1SToomas Soome 	ASSERT(itw->itw_id != 0);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	/* Free 32bit ID */
5967c478bd9Sstevel@tonic-gate 	EHCI_FREE_ID((uint32_t)itw->itw_id);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	rval = ddi_dma_unbind_handle(itw->itw_dmahandle);
5997c478bd9Sstevel@tonic-gate 	ASSERT(rval == DDI_SUCCESS);
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	ddi_dma_mem_free(&itw->itw_accesshandle);
6027c478bd9Sstevel@tonic-gate 	ddi_dma_free_handle(&itw->itw_dmahandle);
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	/* Free transfer wrapper */
6057c478bd9Sstevel@tonic-gate 	kmem_free(itw, sizeof (ehci_isoc_xwrapper_t));
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate /*
6107c478bd9Sstevel@tonic-gate  * transfer descriptor functions
6117c478bd9Sstevel@tonic-gate  */
6127c478bd9Sstevel@tonic-gate /*
6137c478bd9Sstevel@tonic-gate  * ehci_allocate_itd:
6147c478bd9Sstevel@tonic-gate  *
6157c478bd9Sstevel@tonic-gate  * Allocate a Transfer Descriptor (iTD) from the iTD buffer pool.
6167c478bd9Sstevel@tonic-gate  */
6177c478bd9Sstevel@tonic-gate static ehci_itd_t *
ehci_allocate_itd(ehci_state_t * ehcip)6187c478bd9Sstevel@tonic-gate ehci_allocate_itd(ehci_state_t	*ehcip)
6197c478bd9Sstevel@tonic-gate {
6207c478bd9Sstevel@tonic-gate 	int		i, state;
6217c478bd9Sstevel@tonic-gate 	ehci_itd_t	*itd;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/*
6267c478bd9Sstevel@tonic-gate 	 * Search for a blank Transfer Descriptor (iTD)
6277c478bd9Sstevel@tonic-gate 	 * in the iTD buffer pool.
6287c478bd9Sstevel@tonic-gate 	 */
6297c478bd9Sstevel@tonic-gate 	for (i = 0; i < ehci_itd_pool_size; i ++) {
6307c478bd9Sstevel@tonic-gate 		state = Get_ITD(ehcip->ehci_itd_pool_addr[i].itd_state);
6317c478bd9Sstevel@tonic-gate 		if (state == EHCI_ITD_FREE) {
6327c478bd9Sstevel@tonic-gate 			break;
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	if (i >= ehci_itd_pool_size) {
6377c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
6387c478bd9Sstevel@tonic-gate 		    "ehci_allocate_itd: ITD exhausted");
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		return (NULL);
6417c478bd9Sstevel@tonic-gate 	}
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
6447c478bd9Sstevel@tonic-gate 	    "ehci_allocate_itd: Allocated %d", i);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	/* Create a new dummy for the end of the ITD list */
6477c478bd9Sstevel@tonic-gate 	itd = &ehcip->ehci_itd_pool_addr[i];
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
6507c478bd9Sstevel@tonic-gate 	    "ehci_allocate_itd: itd 0x%p", (void *)itd);
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	/* Mark the newly allocated ITD as a empty */
6537c478bd9Sstevel@tonic-gate 	Set_ITD(itd->itd_state, EHCI_ITD_DUMMY);
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	return (itd);
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate  * ehci_deallocate_itd:
6617c478bd9Sstevel@tonic-gate  *
6627c478bd9Sstevel@tonic-gate  * Deallocate a Host Controller's (HC) Transfer Descriptor (ITD).
6637c478bd9Sstevel@tonic-gate  *
6647c478bd9Sstevel@tonic-gate  */
6657c478bd9Sstevel@tonic-gate void
ehci_deallocate_itd(ehci_state_t * ehcip,ehci_isoc_xwrapper_t * itw,ehci_itd_t * old_itd)6667c478bd9Sstevel@tonic-gate ehci_deallocate_itd(
6677c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
6687c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
6697c478bd9Sstevel@tonic-gate 	ehci_itd_t		*old_itd)
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate 	ehci_itd_t	*itd, *next_itd;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
6747c478bd9Sstevel@tonic-gate 	    "ehci_deallocate_itd: old_itd = 0x%p", (void *)old_itd);
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	/* If it has been marked RECLAIM it has already been removed */
6797c478bd9Sstevel@tonic-gate 	if (Get_ITD(old_itd->itd_state) != EHCI_ITD_RECLAIM) {
6807c478bd9Sstevel@tonic-gate 		ehci_remove_isoc_from_pfl(ehcip, old_itd);
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	/* Make sure the ITD is not in the PFL */
6847c478bd9Sstevel@tonic-gate 	ASSERT(Get_ITD_FRAME(old_itd->itd_frame_number) == 0);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/* Remove the itd from the itw */
6877c478bd9Sstevel@tonic-gate 	itd = itw->itw_itd_head;
6887c478bd9Sstevel@tonic-gate 	if (old_itd != 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 		while (next_itd != old_itd) {
6937c478bd9Sstevel@tonic-gate 			itd = next_itd;
6947c478bd9Sstevel@tonic-gate 			next_itd = ehci_itd_iommu_to_cpu(ehcip,
6957c478bd9Sstevel@tonic-gate 			    Get_ITD(itd->itd_itw_next_itd));
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 		Set_ITD(itd->itd_itw_next_itd, old_itd->itd_itw_next_itd);
6997c478bd9Sstevel@tonic-gate 
7006aef9e11SToomas Soome 		if (itd->itd_itw_next_itd == 0) {
7017c478bd9Sstevel@tonic-gate 			itw->itw_itd_tail = itd;
7027c478bd9Sstevel@tonic-gate 		}
7037c478bd9Sstevel@tonic-gate 	} else {
7047c478bd9Sstevel@tonic-gate 		itw->itw_itd_head = ehci_itd_iommu_to_cpu(
7050a05e705Slc 		    ehcip, Get_ITD(old_itd->itd_itw_next_itd));
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 		if (itw->itw_itd_head == NULL) {
7087c478bd9Sstevel@tonic-gate 			itw->itw_itd_tail = NULL;
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	bzero((char *)old_itd, sizeof (ehci_itd_t));
7137c478bd9Sstevel@tonic-gate 	Set_ITD(old_itd->itd_state, EHCI_ITD_FREE);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
7167c478bd9Sstevel@tonic-gate 	    "Dealloc_itd: itd 0x%p", (void *)old_itd);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate /*
7217c478bd9Sstevel@tonic-gate  * ehci_calc_num_itds:
7227c478bd9Sstevel@tonic-gate  *
7237c478bd9Sstevel@tonic-gate  * Calculates how many ITDs are needed for this request.
7247c478bd9Sstevel@tonic-gate  * The calculation is based on weather it is an HIGH speed
7257c478bd9Sstevel@tonic-gate  * transaction of a FULL/LOW transaction.
7267c478bd9Sstevel@tonic-gate  *
7277c478bd9Sstevel@tonic-gate  * For FULL/LOW transaction more itds are necessary if it
7287c478bd9Sstevel@tonic-gate  * spans frames.
7297c478bd9Sstevel@tonic-gate  */
7307c478bd9Sstevel@tonic-gate uint_t
ehci_calc_num_itds(ehci_isoc_xwrapper_t * itw,size_t pkt_count)7317c478bd9Sstevel@tonic-gate ehci_calc_num_itds(
7327c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
7330a05e705Slc 	size_t			pkt_count)
7347c478bd9Sstevel@tonic-gate {
7357c478bd9Sstevel@tonic-gate 	uint_t			multiplier, itd_count;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	/* Allocate the appropriate isoc resources */
7387c478bd9Sstevel@tonic-gate 	if (itw->itw_port_status == USBA_HIGH_SPEED_DEV) {
7397c478bd9Sstevel@tonic-gate 		/* Multiplier needs to be passed in somehow */
7407c478bd9Sstevel@tonic-gate 		multiplier = 1 * 8;
7417c478bd9Sstevel@tonic-gate 		itd_count = pkt_count / multiplier;
7427c478bd9Sstevel@tonic-gate 		if (pkt_count % multiplier) {
7437c478bd9Sstevel@tonic-gate 			itd_count++;
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 	} else {
746d29f5a71Szhigang lu - Sun Microsystems - Beijing China 		itd_count = (uint_t)pkt_count;
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
749b3001defSlg 	return (itd_count);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate  * ehci_allocate_itds_for_itw:
7547c478bd9Sstevel@tonic-gate  *
7557c478bd9Sstevel@tonic-gate  * Allocate n Transfer Descriptors (TD) from the TD buffer pool and places it
7567c478bd9Sstevel@tonic-gate  * into the TW.
7577c478bd9Sstevel@tonic-gate  *
7587c478bd9Sstevel@tonic-gate  * Returns USB_NO_RESOURCES if it was not able to allocate all the requested TD
7597c478bd9Sstevel@tonic-gate  * otherwise USB_SUCCESS.
7607c478bd9Sstevel@tonic-gate  */
7617c478bd9Sstevel@tonic-gate int
ehci_allocate_itds_for_itw(ehci_state_t * ehcip,ehci_isoc_xwrapper_t * itw,uint_t itd_count)7627c478bd9Sstevel@tonic-gate ehci_allocate_itds_for_itw(
7637c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
7647c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
7657c478bd9Sstevel@tonic-gate 	uint_t			itd_count)
7667c478bd9Sstevel@tonic-gate {
7677c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd;
7687c478bd9Sstevel@tonic-gate 	uint32_t		itd_addr;
7697c478bd9Sstevel@tonic-gate 	int			i;
7707c478bd9Sstevel@tonic-gate 	int			error = USB_SUCCESS;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	for (i = 0; i < itd_count; i += 1) {
7737c478bd9Sstevel@tonic-gate 		itd = ehci_allocate_itd(ehcip);
7747c478bd9Sstevel@tonic-gate 		if (itd == NULL) {
7757c478bd9Sstevel@tonic-gate 			error = USB_NO_RESOURCES;
7767c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
7777c478bd9Sstevel@tonic-gate 			    "ehci_allocate_itds_for_itw: "
7787c478bd9Sstevel@tonic-gate 			    "Unable to allocate %d ITDs",
7797c478bd9Sstevel@tonic-gate 			    itd_count);
7807c478bd9Sstevel@tonic-gate 			break;
7817c478bd9Sstevel@tonic-gate 		}
7827c478bd9Sstevel@tonic-gate 		if (i > 0) {
7837c478bd9Sstevel@tonic-gate 			itd_addr = ehci_itd_cpu_to_iommu(ehcip,
7847c478bd9Sstevel@tonic-gate 			    itw->itw_itd_free_list);
7857c478bd9Sstevel@tonic-gate 			Set_ITD(itd->itd_link_ptr, itd_addr);
7867c478bd9Sstevel@tonic-gate 		}
787b3001defSlg 		Set_ITD_INDEX(itd, 0, EHCI_ITD_UNUSED_INDEX);
788b3001defSlg 		Set_ITD_INDEX(itd, 1, EHCI_ITD_UNUSED_INDEX);
789b3001defSlg 		Set_ITD_INDEX(itd, 2, EHCI_ITD_UNUSED_INDEX);
790b3001defSlg 		Set_ITD_INDEX(itd, 3, EHCI_ITD_UNUSED_INDEX);
791b3001defSlg 		Set_ITD_INDEX(itd, 4, EHCI_ITD_UNUSED_INDEX);
792b3001defSlg 		Set_ITD_INDEX(itd, 5, EHCI_ITD_UNUSED_INDEX);
793b3001defSlg 		Set_ITD_INDEX(itd, 6, EHCI_ITD_UNUSED_INDEX);
794b3001defSlg 		Set_ITD_INDEX(itd, 7, EHCI_ITD_UNUSED_INDEX);
7957c478bd9Sstevel@tonic-gate 		itw->itw_itd_free_list = itd;
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	return (error);
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate /*
8037c478bd9Sstevel@tonic-gate  * ehci_deallocate_itds_for_itw:
8047c478bd9Sstevel@tonic-gate  *
8057c478bd9Sstevel@tonic-gate  * Free all allocated resources for Transaction Wrapper (TW).
8067c478bd9Sstevel@tonic-gate  * Does not free the iTW itself.
8077c478bd9Sstevel@tonic-gate  */
8087c478bd9Sstevel@tonic-gate static void
ehci_deallocate_itds_for_itw(ehci_state_t * ehcip,ehci_isoc_xwrapper_t * itw)8097c478bd9Sstevel@tonic-gate ehci_deallocate_itds_for_itw(
8107c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
8117c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw)
8127c478bd9Sstevel@tonic-gate {
8137c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd = NULL;
8147c478bd9Sstevel@tonic-gate 	ehci_itd_t		*temp_itd = NULL;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
817112116d8Sfb 	    "ehci_free_itw_itd_resources: itw = 0x%p", (void *)itw);
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	itd = itw->itw_itd_free_list;
8207c478bd9Sstevel@tonic-gate 	while (itd != NULL) {
8217c478bd9Sstevel@tonic-gate 		/* Save the pointer to the next itd before destroying it */
8227c478bd9Sstevel@tonic-gate 		temp_itd = ehci_itd_iommu_to_cpu(ehcip,
8237c478bd9Sstevel@tonic-gate 		    Get_ITD(itd->itd_link_ptr));
8247c478bd9Sstevel@tonic-gate 		ehci_deallocate_itd(ehcip, itw, itd);
8257c478bd9Sstevel@tonic-gate 		itd = temp_itd;
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 	itw->itw_itd_free_list = NULL;
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate /*
8327c478bd9Sstevel@tonic-gate  * ehci_insert_itd_on_itw:
8337c478bd9Sstevel@tonic-gate  *
8347c478bd9Sstevel@tonic-gate  * The transfer wrapper keeps a list of all Transfer Descriptors (iTD) that
8357c478bd9Sstevel@tonic-gate  * are allocated for this transfer. Insert a iTD onto this list.
8367c478bd9Sstevel@tonic-gate  */
ehci_insert_itd_on_itw(ehci_state_t * ehcip,ehci_isoc_xwrapper_t * itw,ehci_itd_t * itd)8377c478bd9Sstevel@tonic-gate void ehci_insert_itd_on_itw(
8387c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
8397c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw,
8407c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate 	/*
8437c478bd9Sstevel@tonic-gate 	 * Set the next pointer to NULL because
8447c478bd9Sstevel@tonic-gate 	 * this is the last ITD on list.
8457c478bd9Sstevel@tonic-gate 	 */
8466aef9e11SToomas Soome 	Set_ITD(itd->itd_itw_next_itd, 0);
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	if (itw->itw_itd_head == NULL) {
8497c478bd9Sstevel@tonic-gate 		ASSERT(itw->itw_itd_tail == NULL);
8507c478bd9Sstevel@tonic-gate 		itw->itw_itd_head = itd;
8517c478bd9Sstevel@tonic-gate 		itw->itw_itd_tail = itd;
8527c478bd9Sstevel@tonic-gate 	} else {
8537c478bd9Sstevel@tonic-gate 		ehci_itd_t *dummy = (ehci_itd_t *)itw->itw_itd_tail;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 		ASSERT(dummy != NULL);
8567c478bd9Sstevel@tonic-gate 		ASSERT(Get_ITD(itd->itd_state) == EHCI_ITD_ACTIVE);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 		/* Add the itd to the end of the list */
8597c478bd9Sstevel@tonic-gate 		Set_ITD(dummy->itd_itw_next_itd,
8607c478bd9Sstevel@tonic-gate 		    ehci_itd_cpu_to_iommu(ehcip, itd));
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 		itw->itw_itd_tail = itd;
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	Set_ITD(itd->itd_trans_wrapper, (uint32_t)itw->itw_id);
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate /*
8707c478bd9Sstevel@tonic-gate  * ehci_insert_itd_into_active_list:
8717c478bd9Sstevel@tonic-gate  *
8727c478bd9Sstevel@tonic-gate  * Add current ITD into the active ITD list in reverse order.
87348bbca81SDaniel Hoffman  * When the done list is created, remove it in the reverse order.
8747c478bd9Sstevel@tonic-gate  */
8757c478bd9Sstevel@tonic-gate void
ehci_insert_itd_into_active_list(ehci_state_t * ehcip,ehci_itd_t * itd)8767c478bd9Sstevel@tonic-gate ehci_insert_itd_into_active_list(
8777c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
8787c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd)
8797c478bd9Sstevel@tonic-gate {
8807c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
8817c478bd9Sstevel@tonic-gate 	ASSERT(itd != NULL);
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	Set_ITD(itd->itd_next_active_itd,
8847c478bd9Sstevel@tonic-gate 	    ehci_itd_cpu_to_iommu(ehcip, ehcip->ehci_active_itd_list));
8857c478bd9Sstevel@tonic-gate 	ehcip->ehci_active_itd_list = itd;
8867c478bd9Sstevel@tonic-gate }
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate /*
8907c478bd9Sstevel@tonic-gate  * ehci_remove_itd_from_active_list:
8917c478bd9Sstevel@tonic-gate  *
8927c478bd9Sstevel@tonic-gate  * Remove current ITD from the active ITD list.
8937c478bd9Sstevel@tonic-gate  */
8947c478bd9Sstevel@tonic-gate void
ehci_remove_itd_from_active_list(ehci_state_t * ehcip,ehci_itd_t * itd)8957c478bd9Sstevel@tonic-gate ehci_remove_itd_from_active_list(
8967c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
8977c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd)
8987c478bd9Sstevel@tonic-gate {
8997c478bd9Sstevel@tonic-gate 	ehci_itd_t		*curr_itd, *next_itd;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
9027c478bd9Sstevel@tonic-gate 	ASSERT(itd != NULL);
9037c478bd9Sstevel@tonic-gate 
904b3001defSlg 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
905b3001defSlg 	    "ehci_remove_itd_from_active_list: "
906b3001defSlg 	    "ehci_active_itd_list = 0x%p itd = 0x%p",
907112116d8Sfb 	    (void *)ehcip->ehci_active_itd_list, (void *)itd);
908b3001defSlg 
9097c478bd9Sstevel@tonic-gate 	curr_itd = ehcip->ehci_active_itd_list;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	if (curr_itd == itd) {
9127c478bd9Sstevel@tonic-gate 		ehcip->ehci_active_itd_list =
9137c478bd9Sstevel@tonic-gate 		    ehci_itd_iommu_to_cpu(ehcip, itd->itd_next_active_itd);
9146aef9e11SToomas Soome 		itd->itd_next_active_itd = 0;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 		return;
9177c478bd9Sstevel@tonic-gate 	}
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	next_itd = ehci_itd_iommu_to_cpu(ehcip, curr_itd->itd_next_active_itd);
9207c478bd9Sstevel@tonic-gate 	while (next_itd != itd) {
9217c478bd9Sstevel@tonic-gate 		curr_itd = next_itd;
9227c478bd9Sstevel@tonic-gate 		if (curr_itd) {
9237c478bd9Sstevel@tonic-gate 			next_itd = ehci_itd_iommu_to_cpu(ehcip,
9247c478bd9Sstevel@tonic-gate 			    curr_itd->itd_next_active_itd);
9257c478bd9Sstevel@tonic-gate 		} else {
9267c478bd9Sstevel@tonic-gate 			break;
9277c478bd9Sstevel@tonic-gate 		}
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	if ((curr_itd) && (next_itd == itd)) {
9317c478bd9Sstevel@tonic-gate 		Set_ITD(curr_itd->itd_next_active_itd,
9327c478bd9Sstevel@tonic-gate 		    Get_ITD(itd->itd_next_active_itd));
9336aef9e11SToomas Soome 		Set_ITD(itd->itd_next_active_itd, 0);
9347c478bd9Sstevel@tonic-gate 	} else {
9357c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
9367c478bd9Sstevel@tonic-gate 		    "ehci_remove_itd_from_active_list: "
9370a05e705Slc 		    "Unable to find ITD in active_itd_list");
9387c478bd9Sstevel@tonic-gate 	}
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate /*
9437c478bd9Sstevel@tonic-gate  * ehci_create_done_itd_list:
9447c478bd9Sstevel@tonic-gate  *
9457c478bd9Sstevel@tonic-gate  * Traverse the active list and create a done list and remove them
9467c478bd9Sstevel@tonic-gate  * from the active list.
9477c478bd9Sstevel@tonic-gate  */
9487c478bd9Sstevel@tonic-gate ehci_itd_t *
ehci_create_done_itd_list(ehci_state_t * ehcip)9497c478bd9Sstevel@tonic-gate ehci_create_done_itd_list(
9507c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip)
9517c478bd9Sstevel@tonic-gate {
9527c478bd9Sstevel@tonic-gate 	usb_frame_number_t	current_frame_number;
9537c478bd9Sstevel@tonic-gate 	usb_frame_number_t	itd_frame_number, itd_reclaim_number;
9547c478bd9Sstevel@tonic-gate 	ehci_itd_t		*curr_itd = NULL, *next_itd = NULL;
9557c478bd9Sstevel@tonic-gate 	ehci_itd_t		*done_itd_list = NULL;
9567c478bd9Sstevel@tonic-gate 	uint_t			state;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
9597c478bd9Sstevel@tonic-gate 	    "ehci_create_done_itd_list:");
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	/*
9647c478bd9Sstevel@tonic-gate 	 * Get the current frame number.
9657c478bd9Sstevel@tonic-gate 	 * Only process itd that were inserted before the current
9667c478bd9Sstevel@tonic-gate 	 * frame number.
9677c478bd9Sstevel@tonic-gate 	 */
9687c478bd9Sstevel@tonic-gate 	current_frame_number = ehci_get_current_frame_number(ehcip);
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	curr_itd = ehcip->ehci_active_itd_list;
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	while (curr_itd) {
9737c478bd9Sstevel@tonic-gate 		/* Get next itd from the active itd list */
9747c478bd9Sstevel@tonic-gate 		next_itd = ehci_itd_iommu_to_cpu(ehcip,
9757c478bd9Sstevel@tonic-gate 		    Get_ITD(curr_itd->itd_next_active_itd));
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 		/*
9787c478bd9Sstevel@tonic-gate 		 * If haven't past the frame number that the ITD was
9797c478bd9Sstevel@tonic-gate 		 * suppose to be executed, don't touch it.  Just in
9807c478bd9Sstevel@tonic-gate 		 * case it is being processed by the HCD and cause
9817c478bd9Sstevel@tonic-gate 		 * a race condition.
9827c478bd9Sstevel@tonic-gate 		 */
9837c478bd9Sstevel@tonic-gate 		itd_frame_number = Get_ITD_FRAME(curr_itd->itd_frame_number);
9847c478bd9Sstevel@tonic-gate 		itd_reclaim_number =
9857c478bd9Sstevel@tonic-gate 		    Get_ITD_FRAME(curr_itd->itd_reclaim_number);
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 		/* Get the ITD state */
9887c478bd9Sstevel@tonic-gate 		state = Get_ITD(curr_itd->itd_state);
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 		if (((state == EHCI_ITD_ACTIVE) &&
9910a05e705Slc 		    (itd_frame_number < current_frame_number)) ||
9927c478bd9Sstevel@tonic-gate 		    ((state == EHCI_ITD_RECLAIM) &&
9930a05e705Slc 		    (itd_reclaim_number < current_frame_number))) {
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 			/* Remove this ITD from active ITD list */
9967c478bd9Sstevel@tonic-gate 			ehci_remove_itd_from_active_list(ehcip, curr_itd);
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 			/*
9997c478bd9Sstevel@tonic-gate 			 * Create the done list in reverse order, since the
10007c478bd9Sstevel@tonic-gate 			 * active list was also created in reverse order.
10017c478bd9Sstevel@tonic-gate 			 */
10027c478bd9Sstevel@tonic-gate 			Set_ITD(curr_itd->itd_next_active_itd,
10037c478bd9Sstevel@tonic-gate 			    ehci_itd_cpu_to_iommu(ehcip, done_itd_list));
10047c478bd9Sstevel@tonic-gate 			done_itd_list = curr_itd;
10057c478bd9Sstevel@tonic-gate 		}
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 		curr_itd = next_itd;
10087c478bd9Sstevel@tonic-gate 	}
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	return (done_itd_list);
10117c478bd9Sstevel@tonic-gate }
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate /*
10157c478bd9Sstevel@tonic-gate  * ehci_insert_isoc_to_pfl:
10167c478bd9Sstevel@tonic-gate  *
10177c478bd9Sstevel@tonic-gate  * Insert a ITD request into the Host Controller's isochronous list.
10187c478bd9Sstevel@tonic-gate  * All the ITDs in the ITW will be added the PFL at once.  Either all
10197c478bd9Sstevel@tonic-gate  * of them will make it or none of them will.
10207c478bd9Sstevel@tonic-gate  */
10217c478bd9Sstevel@tonic-gate int
ehci_insert_isoc_to_pfl(ehci_state_t * ehcip,ehci_pipe_private_t * pp,ehci_isoc_xwrapper_t * itw)10227c478bd9Sstevel@tonic-gate ehci_insert_isoc_to_pfl(
10237c478bd9Sstevel@tonic-gate 	ehci_state_t		*ehcip,
10247c478bd9Sstevel@tonic-gate 	ehci_pipe_private_t	*pp,
10257c478bd9Sstevel@tonic-gate 	ehci_isoc_xwrapper_t	*itw)
10267c478bd9Sstevel@tonic-gate {
10277c478bd9Sstevel@tonic-gate 	usb_isoc_req_t		*isoc_reqp = itw->itw_curr_xfer_reqp;
10287c478bd9Sstevel@tonic-gate 	usb_frame_number_t	current_frame_number, start_frame_number;
10297c478bd9Sstevel@tonic-gate 	uint_t			ddic, pfl_number;
10307c478bd9Sstevel@tonic-gate 	ehci_periodic_frame_list_t *periodic_frame_list =
10317c478bd9Sstevel@tonic-gate 	    ehcip->ehci_periodic_frame_list_tablep;
10327c478bd9Sstevel@tonic-gate 	uint32_t		addr, port_status;
10337c478bd9Sstevel@tonic-gate 	ehci_itd_t		*itd;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
10367c478bd9Sstevel@tonic-gate 	    "ehci_insert_isoc_to_pfl: "
1037b3001defSlg 	    "isoc flags 0x%x itw = 0x%p",
1038112116d8Sfb 	    isoc_reqp->isoc_attributes, (void *)itw);
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	/*
10417c478bd9Sstevel@tonic-gate 	 * Enter critical, while programming the usb frame number
10427c478bd9Sstevel@tonic-gate 	 * and inserting current isochronous TD into the ED's list.
10437c478bd9Sstevel@tonic-gate 	 */
10447c478bd9Sstevel@tonic-gate 	ddic = ddi_enter_critical();
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	/* Get the current frame number */
10477c478bd9Sstevel@tonic-gate 	current_frame_number = ehci_get_current_frame_number(ehcip);
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	/*
10507c478bd9Sstevel@tonic-gate 	 * Check the given isochronous flags and get the frame number
10517c478bd9Sstevel@tonic-gate 	 * to insert the itd into.
10527c478bd9Sstevel@tonic-gate 	 */
10537c478bd9Sstevel@tonic-gate 	switch (isoc_reqp->isoc_attributes &
10547c478bd9Sstevel@tonic-gate 	    (USB_ATTRS_ISOC_START_FRAME | USB_ATTRS_ISOC_XFER_ASAP)) {
10557c478bd9Sstevel@tonic-gate 	case USB_ATTRS_ISOC_START_FRAME:
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 		/* Starting frame number is specified */
10587c478bd9Sstevel@tonic-gate 		if (pp->pp_flag & EHCI_ISOC_XFER_CONTINUE) {
10597c478bd9Sstevel@tonic-gate 			/* Get the starting usb frame number */
10607c478bd9Sstevel@tonic-gate 			start_frame_number = pp->pp_next_frame_number;
10617c478bd9Sstevel@tonic-gate 		} else {
10627c478bd9Sstevel@tonic-gate 			/* Check for the Starting usb frame number */
10637c478bd9Sstevel@tonic-gate 			if ((isoc_reqp->isoc_frame_no == 0) ||
10647c478bd9Sstevel@tonic-gate 			    ((isoc_reqp->isoc_frame_no +
10650a05e705Slc 			    isoc_reqp->isoc_pkts_count) <
10660a05e705Slc 			    current_frame_number)) {
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 				/* Exit the critical */
10697c478bd9Sstevel@tonic-gate 				ddi_exit_critical(ddic);
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(PRINT_MASK_LISTS,
10727c478bd9Sstevel@tonic-gate 				    ehcip->ehci_log_hdl,
10737c478bd9Sstevel@tonic-gate 				    "ehci_insert_isoc_to_pfl:"
10747c478bd9Sstevel@tonic-gate 				    "Invalid starting frame number");
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 				return (USB_INVALID_START_FRAME);
10777c478bd9Sstevel@tonic-gate 			}
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 			/* Get the starting usb frame number */
10807c478bd9Sstevel@tonic-gate 			start_frame_number = isoc_reqp->isoc_frame_no;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 			pp->pp_next_frame_number = 0;
10837c478bd9Sstevel@tonic-gate 		}
10847c478bd9Sstevel@tonic-gate 		break;
10857c478bd9Sstevel@tonic-gate 	case USB_ATTRS_ISOC_XFER_ASAP:
10867c478bd9Sstevel@tonic-gate 		/* ehci has to specify starting frame number */
10877c478bd9Sstevel@tonic-gate 		if ((pp->pp_next_frame_number) &&
10887c478bd9Sstevel@tonic-gate 		    (pp->pp_next_frame_number > current_frame_number)) {
10897c478bd9Sstevel@tonic-gate 			/*
10907c478bd9Sstevel@tonic-gate 			 * Get the next usb frame number.
10917c478bd9Sstevel@tonic-gate 			 */
10927c478bd9Sstevel@tonic-gate 			start_frame_number = pp->pp_next_frame_number;
10937c478bd9Sstevel@tonic-gate 		} else {
10947c478bd9Sstevel@tonic-gate 			/*
10957c478bd9Sstevel@tonic-gate 			 * Add appropriate offset to the current usb
10967c478bd9Sstevel@tonic-gate 			 * frame number and use it as a starting frame
10977c478bd9Sstevel@tonic-gate 			 * number.
10987c478bd9Sstevel@tonic-gate 			 */
10997c478bd9Sstevel@tonic-gate 			start_frame_number =
11007c478bd9Sstevel@tonic-gate 			    current_frame_number + EHCI_FRAME_OFFSET;
11017c478bd9Sstevel@tonic-gate 		}
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 		if (!(pp->pp_flag & EHCI_ISOC_XFER_CONTINUE)) {
11047c478bd9Sstevel@tonic-gate 			isoc_reqp->isoc_frame_no = start_frame_number;
11057c478bd9Sstevel@tonic-gate 		}
11067c478bd9Sstevel@tonic-gate 		break;
11077c478bd9Sstevel@tonic-gate 	default:
11087c478bd9Sstevel@tonic-gate 		/* Exit the critical */
11097c478bd9Sstevel@tonic-gate 		ddi_exit_critical(ddic);
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
11127c478bd9Sstevel@tonic-gate 		    "ehci_insert_isoc_to_pfl: Either starting "
11137c478bd9Sstevel@tonic-gate 		    "frame number or ASAP flags are not set, attrs = 0x%x",
11147c478bd9Sstevel@tonic-gate 		    isoc_reqp->isoc_attributes);
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 		return (USB_NO_FRAME_NUMBER);
11177c478bd9Sstevel@tonic-gate 	}
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	if (itw->itw_port_status == USBA_HIGH_SPEED_DEV) {
1120b3001defSlg 		port_status = EHCI_ITD_LINK_REF_ITD;
11217c478bd9Sstevel@tonic-gate 	} else {
11227c478bd9Sstevel@tonic-gate 		port_status = EHCI_ITD_LINK_REF_SITD;
11237c478bd9Sstevel@tonic-gate 	}
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	itd = itw->itw_itd_head;
11267c478bd9Sstevel@tonic-gate 	while (itd) {
11277c478bd9Sstevel@tonic-gate 		/* Find the appropriate frame list to put the itd into */
11287c478bd9Sstevel@tonic-gate 		pfl_number = start_frame_number % EHCI_NUM_PERIODIC_FRAME_LISTS;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 		addr = Get_PFLT(periodic_frame_list->
11317c478bd9Sstevel@tonic-gate 		    ehci_periodic_frame_list_table[pfl_number]);
11327c478bd9Sstevel@tonic-gate 		Set_ITD(itd->itd_link_ptr, addr);
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 		/* Set the link_ref correctly as ITD or SITD. */
11357c478bd9Sstevel@tonic-gate 		addr = ehci_itd_cpu_to_iommu(ehcip, itd) & EHCI_ITD_LINK_PTR;
11367c478bd9Sstevel@tonic-gate 		addr |= port_status;
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 		Set_PFLT(periodic_frame_list->
11397c478bd9Sstevel@tonic-gate 		    ehci_periodic_frame_list_table[pfl_number], addr);
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 		/* Save which frame the ITD was inserted into */
11427c478bd9Sstevel@tonic-gate 		Set_ITD_FRAME(itd->itd_frame_number, start_frame_number);
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 		ehci_insert_itd_into_active_list(ehcip, itd);
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 		/* Get the next ITD in the ITW */
11477c478bd9Sstevel@tonic-gate 		itd = ehci_itd_iommu_to_cpu(ehcip,
11487c478bd9Sstevel@tonic-gate 		    Get_ITD(itd->itd_itw_next_itd));
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 		start_frame_number++;
11517c478bd9Sstevel@tonic-gate 	}
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	/* Exit the critical */
11547c478bd9Sstevel@tonic-gate 	ddi_exit_critical(ddic);
11557c478bd9Sstevel@tonic-gate 
11567c478bd9